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