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