xref: /netbsd/usr.bin/jot/jot.c (revision bf9ec67e)
1 /*	$NetBSD: jot.c,v 1.8 2001/03/17 11:43:06 simonb 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.8 2001/03/17 11:43:06 simonb 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 
63 #define	REPS_DEF	100
64 #define	BEGIN_DEF	1
65 #define	ENDER_DEF	100
66 #define	STEP_DEF	1
67 
68 #define	is_default(s)	(strcmp((s), "-") == 0)
69 
70 double	begin;
71 double	ender;
72 double	s;
73 long	reps;
74 int	randomize;
75 int	infinity;
76 int	boring;
77 int	prec;
78 int	dox;
79 int	chardata;
80 int	nofinalnl;
81 char	sepstring[BUFSIZ] = "\n";
82 char	format[BUFSIZ];
83 
84 void	getargs __P((int, char *[]));
85 void	getformat __P((void));
86 int	getprec __P((char *));
87 int	main __P((int, char **));
88 void	putdata __P((double, long));
89 static void	usage __P((void));
90 
91 int
92 main(argc, argv)
93 	int argc;
94 	char *argv[];
95 {
96 	double	xd, yd;
97 	long	id;
98 	double	*x = &xd;
99 	double	*y = &yd;
100 	long	*i = &id;
101 
102 	getargs(argc, argv);
103 	if (randomize) {
104 		*x = (ender - begin) * (ender > begin ? 1 : -1);
105 		srandom((int) s);
106 		for (*i = 1; *i <= reps || infinity; (*i)++) {
107 			*y = (double) random() / INT_MAX;
108 			putdata(*y * *x + begin, reps - *i);
109 		}
110 	}
111 	else
112 		for (*i = 1, *x = begin; *i <= reps || infinity; (*i)++, *x += s)
113 			putdata(*x, reps - *i);
114 	if (!nofinalnl)
115 		putchar('\n');
116 	exit(0);
117 }
118 
119 void
120 getargs(argc, argv)
121 	int argc;
122 	char *argv[];
123 {
124 	unsigned int	mask = 0;
125 	int		n = 0;
126 
127 	while (--argc && **++argv == '-' && !is_default(*argv))
128 		switch ((*argv)[1]) {
129 		case 'r':
130 			randomize = 1;
131 			break;
132 		case 'c':
133 			chardata = 1;
134 			break;
135 		case 'n':
136 			nofinalnl = 1;
137 			break;
138 		case 'b':
139 			boring = 1;
140 		case 'w':
141 			if ((*argv)[2])
142 				strcpy(format, *argv + 2);
143 			else if (!--argc)
144 				errx(1, "Need context word after -w or -b");
145 			else
146 				strcpy(format, *++argv);
147 			break;
148 		case 's':
149 			if ((*argv)[2])
150 				strcpy(sepstring, *argv + 2);
151 			else if (!--argc)
152 				errx(1, "Need string after -s");
153 			else
154 				strcpy(sepstring, *++argv);
155 			break;
156 		case 'p':
157 			if ((*argv)[2])
158 				prec = atoi(*argv + 2);
159 			else if (!--argc)
160 				errx(1, "Need number after -p");
161 			else
162 				prec = atoi(*++argv);
163 			if (prec <= 0)
164 				errx(1, "Bad precision value");
165 			break;
166 		default:
167 			warnx("unknown option `%s'", *argv);
168 			usage();
169 		}
170 
171 	switch (argc) {	/* examine args right to left, falling thru cases */
172 	case 4:
173 		if (!is_default(argv[3])) {
174 			if (!sscanf(argv[3], "%lf", &s))
175 				errx(1, "Bad s value:  %s", argv[3]);
176 			mask |= 01;
177 		}
178 	case 3:
179 		if (!is_default(argv[2])) {
180 			if (!sscanf(argv[2], "%lf", &ender))
181 				ender = argv[2][strlen(argv[2])-1];
182 			mask |= 02;
183 			if (!prec)
184 				n = getprec(argv[2]);
185 		}
186 	case 2:
187 		if (!is_default(argv[1])) {
188 			if (!sscanf(argv[1], "%lf", &begin))
189 				begin = argv[1][strlen(argv[1])-1];
190 			mask |= 04;
191 			if (!prec)
192 				prec = getprec(argv[1]);
193 			if (n > prec)		/* maximum precision */
194 				prec = n;
195 		}
196 	case 1:
197 		if (!is_default(argv[0])) {
198 			if (!sscanf(argv[0], "%ld", &reps))
199 				errx(1, "Bad reps value:  %s", argv[0]);
200 			mask |= 010;
201 		}
202 		break;
203 	case 0:
204 		usage();
205 		break;
206 	default:
207 		errx(1, "Too many arguments.  What do you mean by %s?", argv[4]);
208 	}
209 	getformat();
210 	while (mask)	/* 4 bit mask has 1's where last 4 args were given */
211 		switch (mask) {	/* fill in the 0's by default or computation */
212 		case 001:
213 			reps = REPS_DEF;
214 			mask = 011;
215 			break;
216 		case 002:
217 			reps = REPS_DEF;
218 			mask = 012;
219 			break;
220 		case 003:
221 			reps = REPS_DEF;
222 			mask = 013;
223 			break;
224 		case 004:
225 			reps = REPS_DEF;
226 			mask = 014;
227 			break;
228 		case 005:
229 			reps = REPS_DEF;
230 			mask = 015;
231 			break;
232 		case 006:
233 			reps = REPS_DEF;
234 			mask = 016;
235 			break;
236 		case 007:
237 			if (randomize) {
238 				reps = REPS_DEF;
239 				mask = 0;
240 				break;
241 			}
242 			if (s == 0.0) {
243 				reps = 0;
244 				mask = 0;
245 				break;
246 			}
247 			reps = (ender - begin + s) / s;
248 			if (reps <= 0)
249 				errx(1, "Impossible stepsize");
250 			mask = 0;
251 			break;
252 		case 010:
253 			begin = BEGIN_DEF;
254 			mask = 014;
255 			break;
256 		case 011:
257 			begin = BEGIN_DEF;
258 			mask = 015;
259 			break;
260 		case 012:
261 			s = (randomize ? time(NULL) : STEP_DEF);
262 			mask = 013;
263 			break;
264 		case 013:
265 			if (randomize)
266 				begin = BEGIN_DEF;
267 			else if (reps == 0)
268 				errx(1, "Must specify begin if reps == 0");
269 			begin = ender - reps * s + s;
270 			mask = 0;
271 			break;
272 		case 014:
273 			s = (randomize ? time(NULL) : STEP_DEF);
274 			mask = 015;
275 			break;
276 		case 015:
277 			if (randomize)
278 				ender = ENDER_DEF;
279 			else
280 				ender = begin + reps * s - s;
281 			mask = 0;
282 			break;
283 		case 016:
284 			if (randomize)
285 				s = time(NULL);
286 			else if (reps == 0)
287 				errx(1, "Infinite sequences cannot be bounded");
288 			else if (reps == 1)
289 				s = 0.0;
290 			else
291 				s = (ender - begin) / (reps - 1);
292 			mask = 0;
293 			break;
294 		case 017:		/* if reps given and implied, */
295 			if (!randomize && s != 0.0) {
296 				long t = (ender - begin + s) / s;
297 				if (t <= 0)
298 					errx(1, "Impossible stepsize");
299 				if (t < reps)		/* take lesser */
300 					reps = t;
301 			}
302 			mask = 0;
303 			break;
304 		default:
305 			errx(1, "bad mask");
306 		}
307 	if (reps == 0)
308 		infinity = 1;
309 }
310 
311 void
312 putdata(x, notlast)
313 	double x;
314 	long notlast;
315 {
316 	long	d = x;
317 	long	*dp = &d;
318 
319 	if (boring)				/* repeated word */
320 		printf("%s", format);
321 	else if (dox)				/* scalar */
322 		printf(format, *dp);
323 	else					/* real */
324 		printf(format, x);
325 	if (notlast != 0)
326 		fputs(sepstring, stdout);
327 }
328 
329 static void
330 usage(void)
331 {
332 	fprintf(stderr, "jot - print sequential or random data\n\n");
333 	fprintf(stderr,
334 	    "Usage:\n\tjot [ options ] [ reps [ begin [ end [ s ] ] ] ]\n\n");
335 	fprintf(stderr, "Options:\n\t%s\t%s\t%s\t%s\t%s\t%s\t%s",
336 			"-r		random data\n",
337 			"-c		character data\n",
338 			"-n		no final newline\n",
339 			"-b word		repeated word\n",
340 			"-w word		context word\n",
341 			"-s string	data separator\n",
342 			"-p precision	number of characters\n");
343 	exit(1);
344 }
345 
346 int
347 getprec(s)
348 	char *s;
349 {
350 	char	*p;
351 	char	*q;
352 
353 	for (p = s; *p; p++)
354 		if (*p == '.')
355 			break;
356 	if (!*p)
357 		return (0);
358 	for (q = ++p; *p; p++)
359 		if (!isdigit((unsigned char)*p))
360 			break;
361 	return (p - q);
362 }
363 
364 void
365 getformat()
366 {
367 	char	*p;
368 	size_t	sz;
369 
370 	if (boring)				/* no need to bother */
371 		return;
372 	for (p = format; *p; p++)		/* look for '%' */
373 		if (*p == '%') {
374 			if (*(p+1) != '%')
375 				break;
376 			p++;		/* leave %% alone */
377 		}
378 	sz = sizeof(format) - strlen(format) - 1;
379 	if (!*p) {
380 		if (chardata) {
381 			if (strlcpy(p, "%c", sz) >= sz)
382 				errx(1, "-w word too long");
383 			dox = 1;
384 		} else {
385 			if (snprintf(p, sz, "%%.%df", prec) >= (int)sz)
386 				errx(1, "-w word too long");
387 		}
388 	} else if (!*(p+1)) {
389 		if (sz <= 0)
390 			errx(1, "-w word too long");
391 		strcat(format, "%");		/* cannot end in single '%' */
392 	} else {
393 		p++;				/* skip leading % */
394 		for(; *p && !isalpha((unsigned char)*p); p++) {
395 			/* allow all valid printf(3) flags, but deny '*' */
396 			if (!strchr("0123456789#-+. ", *p))
397 				break;
398 		}
399 		/* Allow 'l' prefix, but no other. */
400 		if (*p == 'l')
401 			p++;
402 		switch (*p) {
403 		case 'f': case 'e': case 'g': case '%':
404 		case 'E': case 'G':
405 			break;
406 		case 's':
407 			errx(1, "cannot convert numeric data to strings");
408 			break;
409 		case 'd': case 'o': case 'x': case 'u':
410 		case 'D': case 'O': case 'X': case 'U':
411 		case 'c': case 'i':
412 			dox = 1;
413 			break;
414 		default:
415 			errx(1, "unknown or invalid format `%s'", format);
416 		}
417 		/* Need to check for trailing stuff to print */
418 		for (; *p; p++)		/* look for '%' */
419 			if (*p == '%') {
420 				if (*(p+1) != '%')
421 					break;
422 				p++;		/* leave %% alone */
423 			}
424 		if (*p)
425 			errx(1, "unknown or invalid format `%s'", format);
426 	}
427 }
428