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