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