123945Sjaap #ifndef lint
2*24089Sjaap static char sccsid[] = "@(#)main.c	3.1 (CWI) 85/07/30";
323945Sjaap #endif lint
4*24089Sjaap 
523945Sjaap #include	<stdio.h>
624015Sjaap #include	<signal.h>
723945Sjaap #include	"pic.h"
823945Sjaap #include	"y.tab.h"
923945Sjaap 
1024015Sjaap obj	**objlist = 0;	/* store the elements here */
1124015Sjaap int	nobjlist = 0;		/* size of objlist array */
1223945Sjaap int	nobj	= 0;
1323945Sjaap 
14*24089Sjaap Attr	*attr;	/*;attributes stored here as collected */
1524015Sjaap int	nattrlist = 0;
1623945Sjaap int	nattr	= 0;	/* number of entries in attr_list */
1723945Sjaap 
1824015Sjaap Text	*text	= 0;	/* text strings stored here as collected */
1924015Sjaap int	ntextlist = 0;		/* size of text[] array */
2023945Sjaap int	ntext	= 0;
2123945Sjaap int	ntext1	= 0;	/* record ntext here on entry to each figure */
2223945Sjaap 
2323945Sjaap float	curx	= 0;
2423945Sjaap float	cury	= 0;
2523945Sjaap 
2623945Sjaap int	hvmode	= R_DIR;	/* R => join left to right, D => top to bottom, etc. */
2723945Sjaap 
2823945Sjaap int	codegen	= 0;	/* 1=>output for this picture; 0=>no output */
2923945Sjaap 
3023945Sjaap float	deltx	= 6;	/* max x value in output, for scaling */
3123945Sjaap float	delty	= 6;	/* max y value in output, for scaling */
3223945Sjaap int	dbg	= 0;
3323945Sjaap int	lineno	= 0;
3423945Sjaap char	*filename	= "-";
3523945Sjaap int	synerr	= 0;
3623945Sjaap char	*cmdname;
3723945Sjaap 
3823945Sjaap float	xmin	= 30000;	/* min values found in actual data */
3923945Sjaap float	ymin	= 30000;
4023945Sjaap float	xmax	= -30000;	/* max */
4123945Sjaap float	ymax	= -30000;
4223945Sjaap 
main(argc,argv)4323945Sjaap main(argc, argv)
4424015Sjaap 	char *argv[];
4523945Sjaap {
4624015Sjaap 	char buf[20];
4724015Sjaap 	extern int fpecatch();
4824015Sjaap 
4924015Sjaap 	signal(SIGFPE, fpecatch);
5023945Sjaap 	cmdname = argv[0];
5123945Sjaap 	while (argc > 1 && *argv[1] == '-') {
5223945Sjaap 		switch (argv[1][1]) {
5323945Sjaap 		case 'd':
5424015Sjaap 			dbg = atoi(&argv[1][2]);
5524015Sjaap 			if (dbg == 0)
5624015Sjaap 				dbg = 1;
5723945Sjaap 			break;
5823945Sjaap 		}
5923945Sjaap 		argc--;
6023945Sjaap 		argv++;
6123945Sjaap 	}
6223945Sjaap 	setdefaults();
6324015Sjaap 	objlist = (obj **) grow(objlist, "objlist", nobjlist += 1000, sizeof(obj *));
6424015Sjaap 	text = (Text *) grow(text, "text", ntextlist += 1000, sizeof(Text));
6524015Sjaap 	attr = (Attr *) grow(attr, "attr", nattrlist += 100, sizeof(Attr));
6624015Sjaap 
6724015Sjaap 	sprintf(buf, "/%d/", getpid());
6824015Sjaap 	pushsrc(String, buf);
6924015Sjaap 	definition("pid");
7024015Sjaap 
7124015Sjaap 	pushsrc(File, curfile = infile);
7223945Sjaap 	if (argc <= 1) {
7324015Sjaap 		curfile->fin = stdin;
7424015Sjaap 		curfile->fname = tostring("-");
7524015Sjaap 		getdata(curfile);
7623945Sjaap 	} else
7723945Sjaap 		while (argc-- > 1) {
7824015Sjaap 			if ((curfile->fin = fopen(*++argv, "r")) == NULL) {
7924015Sjaap 				fprintf(stderr, "%s: can't open %s\n", cmdname, *argv);
8023945Sjaap 				exit(1);
8123945Sjaap 			}
8224015Sjaap 			curfile->fname = tostring(*argv);
8324015Sjaap 			getdata(curfile);
8424015Sjaap 			fclose(curfile->fin);
8524015Sjaap 			free(curfile->fname);
8623945Sjaap 		}
8723945Sjaap 	exit(0);
8823945Sjaap }
8923945Sjaap 
fpecatch()9024015Sjaap fpecatch()
9124015Sjaap {
9224015Sjaap 	fatal("floating point exception");
9324015Sjaap }
9424015Sjaap 
grow(ptr,name,num,size)9524015Sjaap char *grow(ptr, name, num, size)	/* make array bigger */
9624015Sjaap 	char *ptr, *name;
9724015Sjaap 	int num, size;
9824015Sjaap {
9924015Sjaap 	char *p;
10024015Sjaap 
10124015Sjaap 	if (ptr == NULL)
10224015Sjaap 		p = malloc(num * size);
10324015Sjaap 	else
10424015Sjaap 		p = realloc(ptr, num * size);
10524015Sjaap 	if (p == NULL)
10624015Sjaap 		fatal("can't grow %s to %d", name, num * size);
10724015Sjaap 	return p;
10824015Sjaap }
10924015Sjaap 
11023945Sjaap static struct {
11123945Sjaap 	char *name;
11223945Sjaap 	float val;
11324015Sjaap 	short scalable;		/* 1 => adjust when "scale" changes */
11423945Sjaap } defaults[] ={
11524015Sjaap 	"scale", SCALE, 1,
11624015Sjaap 	"lineht", HT, 1,
11724015Sjaap 	"linewid", HT, 1,
11824015Sjaap 	"moveht", HT, 1,
11924015Sjaap 	"movewid", HT, 1,
12024015Sjaap 	"dashwid", HT10, 1,
12124015Sjaap 	"boxht", HT, 1,
12224015Sjaap 	"boxwid", WID, 1,
12324015Sjaap 	"circlerad", HT2, 1,
12424015Sjaap 	"arcrad", HT2, 1,
12524015Sjaap 	"ellipseht", HT, 1,
12624015Sjaap 	"ellipsewid", WID, 1,
12724015Sjaap 	"arrowht", HT5, 1,
12824015Sjaap 	"arrowwid", HT10, 1,
12924015Sjaap 	"arrowhead", 2, 0,		/* arrowhead style */
13024015Sjaap 	"textht", 0.0, 1,		/* 6 lines/inch is also a useful value */
13124015Sjaap 	"textwid", 0.0, 1,
13223945Sjaap 	NULL, 0
13323945Sjaap };
13423945Sjaap 
setdefaults()13523945Sjaap setdefaults()	/* set default sizes for variables like boxht */
13623945Sjaap {
13723945Sjaap 	int i;
13823945Sjaap 	YYSTYPE v;
13923945Sjaap 
14023945Sjaap 	for (i = 0; defaults[i].name != NULL; i++) {
14123945Sjaap 		v.f = defaults[i].val;
14223945Sjaap 		makevar(tostring(defaults[i].name), VARNAME, v);
14323945Sjaap 	}
14423945Sjaap }
14523945Sjaap 
resetvar()14624015Sjaap resetvar()	/* reset variables listed */
14724015Sjaap {
14824015Sjaap 	int i, j;
14923945Sjaap 
15024015Sjaap 	if (nattr == 0) {	/* none listed, so do all */
15124015Sjaap 		setdefaults();
15224015Sjaap 		return;
15324015Sjaap 	}
15424015Sjaap 	for (i = 0; i < nattr; i++) {
15524015Sjaap 		for (j = 0; defaults[j].name != NULL; j++)
15624015Sjaap 			if (strcmp(defaults[j].name, attr[i].a_val.p) == 0) {
15724015Sjaap 				setfval(defaults[j].name, defaults[j].val);
15824015Sjaap 				free(attr[i].a_val.p);
15924015Sjaap 				break;
16024015Sjaap 			}
16124015Sjaap 	}
16224015Sjaap }
16324015Sjaap 
checkscale(s)16423945Sjaap checkscale(s)	/* if s is "scale", adjust default variables */
16523945Sjaap 	char *s;
16623945Sjaap {
16723945Sjaap 	int i;
16823945Sjaap 	float scale;
16923945Sjaap 
17023945Sjaap 	if (strcmp(s, "scale") == 0) {
17123945Sjaap 		scale = getfval("scale");
17223945Sjaap 		for (i = 1; defaults[i].name != NULL; i++)
17324015Sjaap 			if (defaults[i].scalable)
17424015Sjaap 				setfval(defaults[i].name, defaults[i].val * scale);
17523945Sjaap 	}
17623945Sjaap }
17723945Sjaap 
getdata()17824015Sjaap getdata()
17923945Sjaap {
18024015Sjaap 	char *p, buf[1000], buf1[100];
18124015Sjaap 	int ln;
18223945Sjaap 
18324015Sjaap 	curfile->lineno = 0;
18424015Sjaap 	printf(".lf 1 %s\n", curfile->fname);
18524015Sjaap 	while (fgets(buf, sizeof buf, curfile->fin) != NULL) {
18624015Sjaap 		curfile->lineno++;
18723945Sjaap 		if (*buf == '.' && *(buf+1) == 'P' && *(buf+2) == 'S') {
18823945Sjaap 			for (p = &buf[3]; *p == ' '; p++)
18923945Sjaap 				;
19023945Sjaap 			if (*p++ == '<') {
19124015Sjaap 				Infile svfile;
19224015Sjaap 				svfile = *curfile;
19323945Sjaap 				sscanf(p, "%s", buf1);
19424015Sjaap 				if ((curfile->fin=fopen(buf1, "r")) == NULL)
19524015Sjaap 					fatal("can't open %s", buf1);
19624015Sjaap 				curfile->fname = tostring(buf1);
19724015Sjaap 				getdata();
19824015Sjaap 				fclose(curfile->fin);
19924015Sjaap 				free(curfile->fname);
20024015Sjaap 				*curfile = svfile;
20124015Sjaap 				printf(".lf %d %s\n", curfile->lineno, curfile->fname);
20223945Sjaap 				continue;
20323945Sjaap 			}
20423945Sjaap 			reset();
20523945Sjaap 			yyparse();
20624015Sjaap 			/* yylval.i now contains 'E' or 'F' from .PE or .PF */
20724015Sjaap 
20824015Sjaap 			deltx = (xmax - xmin) / getfval("scale");
20924015Sjaap 			delty = (ymax - ymin) / getfval("scale");
21024015Sjaap 			if (buf[3] == ' ') {	/* next things are wid & ht */
21124015Sjaap 				if (sscanf(&buf[4],"%f%f",&deltx,&delty) < 2)
21224015Sjaap 					delty = deltx * (ymax-ymin) / (xmax-xmin);
21323945Sjaap 			}
21424015Sjaap 			dprintf("deltx = %g, delty = %g\n", deltx, delty);
21523945Sjaap 			if (codegen && !synerr) {
21623945Sjaap 				openpl(&buf[3]);	/* puts out .PS, with ht & wid stuck in */
21724015Sjaap 				printf(".lf %d\n", curfile->lineno+1);
21823945Sjaap 				print();	/* assumes \n at end */
21924015Sjaap 				closepl(yylval.i);	/* does the .PE/F */
22023945Sjaap 			}
22124015Sjaap 			printf(".lf %d\n", curfile->lineno+1);
22223945Sjaap 			fflush(stdout);
22324015Sjaap 		} else if (buf[0] == '.' && buf[1] == 'l' && buf[2] == 'f') {
22424015Sjaap 			if (sscanf(buf+3, "%d %s", &ln, buf1) == 2) {
22524015Sjaap 				free(curfile->fname);
22624015Sjaap 				printf(".lf %d %s\n", curfile->lineno = ln, curfile->fname = tostring(buf1));
22724015Sjaap 			} else
22824015Sjaap 				printf(".lf %d\n", curfile->lineno = ln);
22924015Sjaap 		} else
23023945Sjaap 			fputs(buf, stdout);
23123945Sjaap 	}
23223945Sjaap }
23323945Sjaap 
reset()23423945Sjaap reset()
23523945Sjaap {
23624015Sjaap 	obj *op;
23723945Sjaap 	int i;
23823945Sjaap 	struct symtab *p;
23923945Sjaap 	extern int nstack;
24023945Sjaap 
24123945Sjaap 	for (i = 0; i < nobj; i++) {
24223945Sjaap 		op = objlist[i];
24323945Sjaap 		if (op->o_type == BLOCK)
24424015Sjaap 			freesymtab(op->o_symtab);
24523945Sjaap 		free(objlist[i]);
24623945Sjaap 	}
24723945Sjaap 	nobj = 0;
24823945Sjaap 	nattr = 0;
24923945Sjaap 	for (i = 0; i < ntext; i++)
25024015Sjaap 		if (text[i].t_val)
25124015Sjaap 			free(text[i].t_val);
25223945Sjaap 	ntext = ntext1 = 0;
25323945Sjaap 	codegen = synerr = 0;
25423945Sjaap 	nstack = 0;
25523945Sjaap 	curx = cury = 0;
25623945Sjaap 	hvmode = R_DIR;
25723945Sjaap 	xmin = ymin = 30000;
25823945Sjaap 	xmax = ymax = -30000;
25923945Sjaap }
260