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