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