jot.c revision 1.4 1 1.4 lukem /* $NetBSD: jot.c,v 1.4 1997/10/19 03:34:49 lukem Exp $ */
2 1.2 jtc
3 1.1 jtc /*-
4 1.1 jtc * Copyright (c) 1993
5 1.1 jtc * The Regents of the University of California. All rights reserved.
6 1.1 jtc *
7 1.1 jtc * Redistribution and use in source and binary forms, with or without
8 1.1 jtc * modification, are permitted provided that the following conditions
9 1.1 jtc * are met:
10 1.1 jtc * 1. Redistributions of source code must retain the above copyright
11 1.1 jtc * notice, this list of conditions and the following disclaimer.
12 1.1 jtc * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jtc * notice, this list of conditions and the following disclaimer in the
14 1.1 jtc * documentation and/or other materials provided with the distribution.
15 1.1 jtc * 3. All advertising materials mentioning features or use of this software
16 1.1 jtc * must display the following acknowledgement:
17 1.1 jtc * This product includes software developed by the University of
18 1.1 jtc * California, Berkeley and its contributors.
19 1.1 jtc * 4. Neither the name of the University nor the names of its contributors
20 1.1 jtc * may be used to endorse or promote products derived from this software
21 1.1 jtc * without specific prior written permission.
22 1.1 jtc *
23 1.1 jtc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 jtc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 jtc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 jtc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 jtc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 jtc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 jtc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 jtc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 jtc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 jtc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 jtc * SUCH DAMAGE.
34 1.1 jtc */
35 1.1 jtc
36 1.4 lukem #include <sys/cdefs.h>
37 1.1 jtc #ifndef lint
38 1.4 lukem __COPYRIGHT("@(#) Copyright (c) 1993\n\
39 1.4 lukem The Regents of the University of California. All rights reserved.\n");
40 1.1 jtc #endif /* not lint */
41 1.1 jtc
42 1.1 jtc #ifndef lint
43 1.2 jtc #if 0
44 1.1 jtc static char sccsid[] = "@(#)jot.c 8.1 (Berkeley) 6/6/93";
45 1.2 jtc #endif
46 1.4 lukem __RCSID("$NetBSD: jot.c,v 1.4 1997/10/19 03:34:49 lukem Exp $");
47 1.1 jtc #endif /* not lint */
48 1.1 jtc
49 1.1 jtc /*
50 1.1 jtc * jot - print sequential or random data
51 1.1 jtc *
52 1.1 jtc * Author: John Kunze, Office of Comp. Affairs, UCB
53 1.1 jtc */
54 1.1 jtc
55 1.1 jtc #include <ctype.h>
56 1.4 lukem #include <err.h>
57 1.1 jtc #include <limits.h>
58 1.1 jtc #include <stdio.h>
59 1.1 jtc #include <stdlib.h>
60 1.1 jtc #include <string.h>
61 1.1 jtc #include <time.h>
62 1.1 jtc
63 1.1 jtc #define REPS_DEF 100
64 1.1 jtc #define BEGIN_DEF 1
65 1.1 jtc #define ENDER_DEF 100
66 1.1 jtc #define STEP_DEF 1
67 1.1 jtc
68 1.1 jtc #define isdefault(s) (strcmp((s), "-") == 0)
69 1.1 jtc
70 1.1 jtc double begin;
71 1.1 jtc double ender;
72 1.1 jtc double s;
73 1.1 jtc long reps;
74 1.1 jtc int randomize;
75 1.1 jtc int infinity;
76 1.1 jtc int boring;
77 1.1 jtc int prec;
78 1.1 jtc int dox;
79 1.1 jtc int chardata;
80 1.1 jtc int nofinalnl;
81 1.3 pk char sepstring[BUFSIZ] = "\n";
82 1.1 jtc char format[BUFSIZ];
83 1.1 jtc
84 1.1 jtc void error __P((char *, char *));
85 1.1 jtc void getargs __P((int, char *[]));
86 1.1 jtc void getformat __P((void));
87 1.1 jtc int getprec __P((char *));
88 1.4 lukem int main __P((int, char **));
89 1.1 jtc void putdata __P((double, long));
90 1.1 jtc
91 1.1 jtc int
92 1.1 jtc main(argc, argv)
93 1.1 jtc int argc;
94 1.1 jtc char *argv[];
95 1.1 jtc {
96 1.1 jtc double xd, yd;
97 1.1 jtc long id;
98 1.4 lukem double *x = &xd;
99 1.4 lukem double *y = &yd;
100 1.4 lukem long *i = &id;
101 1.1 jtc
102 1.1 jtc getargs(argc, argv);
103 1.1 jtc if (randomize) {
104 1.1 jtc *x = (ender - begin) * (ender > begin ? 1 : -1);
105 1.1 jtc srandom((int) s);
106 1.1 jtc for (*i = 1; *i <= reps || infinity; (*i)++) {
107 1.1 jtc *y = (double) random() / INT_MAX;
108 1.1 jtc putdata(*y * *x + begin, reps - *i);
109 1.1 jtc }
110 1.1 jtc }
111 1.1 jtc else
112 1.1 jtc for (*i = 1, *x = begin; *i <= reps || infinity; (*i)++, *x += s)
113 1.1 jtc putdata(*x, reps - *i);
114 1.1 jtc if (!nofinalnl)
115 1.1 jtc putchar('\n');
116 1.1 jtc exit(0);
117 1.1 jtc }
118 1.1 jtc
119 1.1 jtc void
120 1.1 jtc getargs(ac, av)
121 1.1 jtc int ac;
122 1.1 jtc char *av[];
123 1.1 jtc {
124 1.4 lukem unsigned int mask = 0;
125 1.4 lukem int n = 0;
126 1.1 jtc
127 1.1 jtc while (--ac && **++av == '-' && !isdefault(*av))
128 1.1 jtc switch ((*av)[1]) {
129 1.1 jtc case 'r':
130 1.1 jtc randomize = 1;
131 1.1 jtc break;
132 1.1 jtc case 'c':
133 1.1 jtc chardata = 1;
134 1.1 jtc break;
135 1.1 jtc case 'n':
136 1.1 jtc nofinalnl = 1;
137 1.1 jtc break;
138 1.1 jtc case 'b':
139 1.1 jtc boring = 1;
140 1.1 jtc case 'w':
141 1.1 jtc if ((*av)[2])
142 1.1 jtc strcpy(format, *av + 2);
143 1.1 jtc else if (!--ac)
144 1.1 jtc error("Need context word after -w or -b", "");
145 1.1 jtc else
146 1.1 jtc strcpy(format, *++av);
147 1.1 jtc break;
148 1.1 jtc case 's':
149 1.1 jtc if ((*av)[2])
150 1.1 jtc strcpy(sepstring, *av + 2);
151 1.1 jtc else if (!--ac)
152 1.1 jtc error("Need string after -s", "");
153 1.1 jtc else
154 1.1 jtc strcpy(sepstring, *++av);
155 1.1 jtc break;
156 1.1 jtc case 'p':
157 1.1 jtc if ((*av)[2])
158 1.1 jtc prec = atoi(*av + 2);
159 1.1 jtc else if (!--ac)
160 1.1 jtc error("Need number after -p", "");
161 1.1 jtc else
162 1.1 jtc prec = atoi(*++av);
163 1.1 jtc if (prec <= 0)
164 1.1 jtc error("Bad precision value", "");
165 1.1 jtc break;
166 1.1 jtc default:
167 1.1 jtc error("Unknown option %s", *av);
168 1.1 jtc }
169 1.1 jtc
170 1.1 jtc switch (ac) { /* examine args right to left, falling thru cases */
171 1.1 jtc case 4:
172 1.1 jtc if (!isdefault(av[3])) {
173 1.1 jtc if (!sscanf(av[3], "%lf", &s))
174 1.1 jtc error("Bad s value: %s", av[3]);
175 1.1 jtc mask |= 01;
176 1.1 jtc }
177 1.1 jtc case 3:
178 1.1 jtc if (!isdefault(av[2])) {
179 1.1 jtc if (!sscanf(av[2], "%lf", &ender))
180 1.1 jtc ender = av[2][strlen(av[2])-1];
181 1.1 jtc mask |= 02;
182 1.1 jtc if (!prec)
183 1.1 jtc n = getprec(av[2]);
184 1.1 jtc }
185 1.1 jtc case 2:
186 1.1 jtc if (!isdefault(av[1])) {
187 1.1 jtc if (!sscanf(av[1], "%lf", &begin))
188 1.1 jtc begin = av[1][strlen(av[1])-1];
189 1.1 jtc mask |= 04;
190 1.1 jtc if (!prec)
191 1.1 jtc prec = getprec(av[1]);
192 1.1 jtc if (n > prec) /* maximum precision */
193 1.1 jtc prec = n;
194 1.1 jtc }
195 1.1 jtc case 1:
196 1.1 jtc if (!isdefault(av[0])) {
197 1.1 jtc if (!sscanf(av[0], "%ld", &reps))
198 1.1 jtc error("Bad reps value: %s", av[0]);
199 1.1 jtc mask |= 010;
200 1.1 jtc }
201 1.1 jtc break;
202 1.1 jtc case 0:
203 1.1 jtc error("jot - print sequential or random data", "");
204 1.1 jtc default:
205 1.1 jtc error("Too many arguments. What do you mean by %s?", av[4]);
206 1.1 jtc }
207 1.1 jtc getformat();
208 1.1 jtc while (mask) /* 4 bit mask has 1's where last 4 args were given */
209 1.1 jtc switch (mask) { /* fill in the 0's by default or computation */
210 1.1 jtc case 001:
211 1.1 jtc reps = REPS_DEF;
212 1.1 jtc mask = 011;
213 1.1 jtc break;
214 1.1 jtc case 002:
215 1.1 jtc reps = REPS_DEF;
216 1.1 jtc mask = 012;
217 1.1 jtc break;
218 1.1 jtc case 003:
219 1.1 jtc reps = REPS_DEF;
220 1.1 jtc mask = 013;
221 1.1 jtc break;
222 1.1 jtc case 004:
223 1.1 jtc reps = REPS_DEF;
224 1.1 jtc mask = 014;
225 1.1 jtc break;
226 1.1 jtc case 005:
227 1.1 jtc reps = REPS_DEF;
228 1.1 jtc mask = 015;
229 1.1 jtc break;
230 1.1 jtc case 006:
231 1.1 jtc reps = REPS_DEF;
232 1.1 jtc mask = 016;
233 1.1 jtc break;
234 1.1 jtc case 007:
235 1.1 jtc if (randomize) {
236 1.1 jtc reps = REPS_DEF;
237 1.1 jtc mask = 0;
238 1.1 jtc break;
239 1.1 jtc }
240 1.1 jtc if (s == 0.0) {
241 1.1 jtc reps = 0;
242 1.1 jtc mask = 0;
243 1.1 jtc break;
244 1.1 jtc }
245 1.1 jtc reps = (ender - begin + s) / s;
246 1.1 jtc if (reps <= 0)
247 1.1 jtc error("Impossible stepsize", "");
248 1.1 jtc mask = 0;
249 1.1 jtc break;
250 1.1 jtc case 010:
251 1.1 jtc begin = BEGIN_DEF;
252 1.1 jtc mask = 014;
253 1.1 jtc break;
254 1.1 jtc case 011:
255 1.1 jtc begin = BEGIN_DEF;
256 1.1 jtc mask = 015;
257 1.1 jtc break;
258 1.1 jtc case 012:
259 1.1 jtc s = (randomize ? time(0) : STEP_DEF);
260 1.1 jtc mask = 013;
261 1.1 jtc break;
262 1.1 jtc case 013:
263 1.1 jtc if (randomize)
264 1.1 jtc begin = BEGIN_DEF;
265 1.1 jtc else if (reps == 0)
266 1.1 jtc error("Must specify begin if reps == 0", "");
267 1.1 jtc begin = ender - reps * s + s;
268 1.1 jtc mask = 0;
269 1.1 jtc break;
270 1.1 jtc case 014:
271 1.1 jtc s = (randomize ? time(0) : STEP_DEF);
272 1.1 jtc mask = 015;
273 1.1 jtc break;
274 1.1 jtc case 015:
275 1.1 jtc if (randomize)
276 1.1 jtc ender = ENDER_DEF;
277 1.1 jtc else
278 1.1 jtc ender = begin + reps * s - s;
279 1.1 jtc mask = 0;
280 1.1 jtc break;
281 1.1 jtc case 016:
282 1.1 jtc if (randomize)
283 1.1 jtc s = time(0);
284 1.1 jtc else if (reps == 0)
285 1.1 jtc error("Infinite sequences cannot be bounded",
286 1.1 jtc "");
287 1.1 jtc else if (reps == 1)
288 1.1 jtc s = 0.0;
289 1.1 jtc else
290 1.1 jtc s = (ender - begin) / (reps - 1);
291 1.1 jtc mask = 0;
292 1.1 jtc break;
293 1.1 jtc case 017: /* if reps given and implied, */
294 1.1 jtc if (!randomize && s != 0.0) {
295 1.1 jtc long t = (ender - begin + s) / s;
296 1.1 jtc if (t <= 0)
297 1.1 jtc error("Impossible stepsize", "");
298 1.1 jtc if (t < reps) /* take lesser */
299 1.1 jtc reps = t;
300 1.1 jtc }
301 1.1 jtc mask = 0;
302 1.1 jtc break;
303 1.1 jtc default:
304 1.1 jtc error("Bad mask", "");
305 1.1 jtc }
306 1.1 jtc if (reps == 0)
307 1.1 jtc infinity = 1;
308 1.1 jtc }
309 1.1 jtc
310 1.1 jtc void
311 1.1 jtc putdata(x, notlast)
312 1.1 jtc double x;
313 1.1 jtc long notlast;
314 1.1 jtc {
315 1.4 lukem long d = x;
316 1.4 lukem long *dp = &d;
317 1.1 jtc
318 1.1 jtc if (boring) /* repeated word */
319 1.3 pk printf("%s", format);
320 1.1 jtc else if (dox) /* scalar */
321 1.1 jtc printf(format, *dp);
322 1.1 jtc else /* real */
323 1.1 jtc printf(format, x);
324 1.1 jtc if (notlast != 0)
325 1.1 jtc fputs(sepstring, stdout);
326 1.1 jtc }
327 1.1 jtc
328 1.1 jtc void
329 1.1 jtc error(msg, s)
330 1.1 jtc char *msg, *s;
331 1.1 jtc {
332 1.4 lukem warnx(msg, s);
333 1.1 jtc fprintf(stderr,
334 1.1 jtc "\nusage: jot [ options ] [ reps [ begin [ end [ s ] ] ] ]\n");
335 1.1 jtc if (strncmp("jot - ", msg, 6) == 0)
336 1.1 jtc fprintf(stderr, "Options:\n\t%s\t%s\t%s\t%s\t%s\t%s\t%s",
337 1.1 jtc "-r random data\n",
338 1.1 jtc "-c character data\n",
339 1.1 jtc "-n no final newline\n",
340 1.1 jtc "-b word repeated word\n",
341 1.1 jtc "-w word context word\n",
342 1.1 jtc "-s string data separator\n",
343 1.1 jtc "-p precision number of characters\n");
344 1.1 jtc exit(1);
345 1.1 jtc }
346 1.1 jtc
347 1.1 jtc int
348 1.1 jtc getprec(s)
349 1.1 jtc char *s;
350 1.1 jtc {
351 1.4 lukem char *p;
352 1.4 lukem char *q;
353 1.1 jtc
354 1.1 jtc for (p = s; *p; p++)
355 1.1 jtc if (*p == '.')
356 1.1 jtc break;
357 1.1 jtc if (!*p)
358 1.1 jtc return (0);
359 1.1 jtc for (q = ++p; *p; p++)
360 1.1 jtc if (!isdigit(*p))
361 1.1 jtc break;
362 1.1 jtc return (p - q);
363 1.1 jtc }
364 1.1 jtc
365 1.1 jtc void
366 1.1 jtc getformat()
367 1.1 jtc {
368 1.4 lukem char *p;
369 1.1 jtc
370 1.1 jtc if (boring) /* no need to bother */
371 1.1 jtc return;
372 1.1 jtc for (p = format; *p; p++) /* look for '%' */
373 1.1 jtc if (*p == '%' && *(p+1) != '%') /* leave %% alone */
374 1.1 jtc break;
375 1.1 jtc if (!*p && !chardata)
376 1.1 jtc sprintf(p, "%%.%df", prec);
377 1.1 jtc else if (!*p && chardata) {
378 1.1 jtc strcpy(p, "%c");
379 1.1 jtc dox = 1;
380 1.1 jtc }
381 1.1 jtc else if (!*(p+1))
382 1.1 jtc strcat(format, "%"); /* cannot end in single '%' */
383 1.1 jtc else {
384 1.1 jtc while (!isalpha(*p))
385 1.1 jtc p++;
386 1.1 jtc switch (*p) {
387 1.1 jtc case 'f': case 'e': case 'g': case '%':
388 1.1 jtc break;
389 1.1 jtc case 's':
390 1.1 jtc error("Cannot convert numeric data to strings", "");
391 1.1 jtc break;
392 1.1 jtc /* case 'd': case 'o': case 'x': case 'D': case 'O': case 'X':
393 1.1 jtc case 'c': case 'u': */
394 1.1 jtc default:
395 1.1 jtc dox = 1;
396 1.1 jtc break;
397 1.1 jtc }
398 1.1 jtc }
399 1.1 jtc }
400