1*26821Sslatteng /* main.c	(Berkeley)	1.9	86/03/11	*/
214392Sslatteng #include	<stdio.h>
314392Sslatteng #include	"pic.h"
414392Sslatteng #include	"y.tab.h"
514403Sslatteng #include	"dev.h"
614392Sslatteng 
715746Sslatteng #ifndef DEVDIR
814403Sslatteng #define DEVDIR	"/usr/lib/font"		/* place to look up device files */
915745Sslatteng #endif
1014403Sslatteng 
1114403Sslatteng char	*devdir = DEVDIR;
1215179Sslatteng char	*dev = "va";			/* default typesetter is varian */
13*26821Sslatteng char	*getenv();
1414403Sslatteng 
1514392Sslatteng struct	obj	*objlist[MAXOBJ];	/* store the elements here */
1614392Sslatteng int	nobj	= 0;
1714392Sslatteng 
1814392Sslatteng struct attr	attr[40];	/* attributes stored here as collected */
1914392Sslatteng int	nattr	= 0;	/* number of entries in attr_list */
2014392Sslatteng 
2114392Sslatteng struct	text	text[MAXTEXT];	/* text strings stored here as collected */
2214392Sslatteng int	ntext	= 0;
2314392Sslatteng int	ntext1	= 0;	/* record ntext here on entry to each figure */
2414392Sslatteng 
2514392Sslatteng float	curx	= 0;
2614392Sslatteng float	cury	= 0;
2714392Sslatteng 
2814392Sslatteng int	hvmode	= R_DIR;	/* R => join left to right, D => top to bottom, etc. */
2914392Sslatteng 
3014392Sslatteng int	codegen	= 0;	/* 1=>output for this picture; 0=>no output */
3114392Sslatteng 
3214641Sslatteng float	deltx	= 6.0;	/* max x value in output, for scaling */
3314641Sslatteng float	delty	= 6.0;	/* max y value in output, for scaling */
3414641Sslatteng float	xbound	= 8.0;	/* max allowed x value in output */
3514641Sslatteng float	ybound	= 10.0;	/* max allowed y value in output */
3614392Sslatteng int	dbg	= 0;
3714392Sslatteng extern	FILE	*yyin,	/* input file pointer */
3814392Sslatteng 		*skeldb; /* output pointer for dbg messeges */
3914392Sslatteng int	lineno	= 0;
4014392Sslatteng char	*filename	= "-";
4114392Sslatteng int	synerr	= 0;
4214392Sslatteng char	*cmdname;
4314392Sslatteng int	crop	= 1;	/* trim off exterior white space if non-zero */
4414392Sslatteng extern int	useDline;	/* if set, use \D for all lines */
4514403Sslatteng int	res;		/* resolution of output device (dots/inch) */
4614403Sslatteng int	DX;		/* smallest change in X, and Y for output device */
4714403Sslatteng int	DY;
4814392Sslatteng 
4914392Sslatteng 
5014392Sslatteng float	hshift	= 0;	/* move this far left for text (in em's) */
5114392Sslatteng float	vshift	= 0.2;	/* this far down */
5214392Sslatteng 
5314392Sslatteng float	sxmin;		/* lower limit from s command */
5414392Sslatteng float	symin;
5514392Sslatteng float	sxmax	= 4096;	/* upper */
5614392Sslatteng float	symax	= 4096;
5714392Sslatteng 
5814392Sslatteng float	xmin	= 30000;	/* min values found in actual data */
5914392Sslatteng float	ymin	= 30000;
6014392Sslatteng float	xmax	= -30000;	/* max */
6114392Sslatteng float	ymax	= -30000;
6214392Sslatteng 
main(argc,argv)6314392Sslatteng main(argc, argv)
6414392Sslatteng 	char **argv;
6514392Sslatteng {
66*26821Sslatteng 	register char *cp;
67*26821Sslatteng 	if ((cp = getenv("PRINTER")) != NULL) dev = cp;
68*26821Sslatteng 	if ((cp = getenv("TYPESETTER")) != NULL) dev = cp;
6914392Sslatteng 	cmdname = argv[0];
7015788Sslatteng 	while (argc > 1 && *argv[1] == '-' && argv[1][1]) {
7114392Sslatteng 		switch (argv[1][1]) {
7214403Sslatteng 		case 'F':
7314403Sslatteng 			devdir = &argv[1][2];
7414403Sslatteng 			break;
7514392Sslatteng 		case 'T':
7615179Sslatteng 		case 'P':
7714403Sslatteng 			dev = &argv[1][2];
7814392Sslatteng 			break;
7914392Sslatteng 		case 'd':
8014392Sslatteng 			dbg = 1;
8114392Sslatteng 			break;
8214392Sslatteng 		case 'D':
8314392Sslatteng 			useDline = !useDline;
8414392Sslatteng 			break;
8514641Sslatteng 		case 'x':
8614641Sslatteng 			xbound = atof(&argv[1][2]);
8714641Sslatteng 			break;
8814641Sslatteng 		case 'y':
8914641Sslatteng 			ybound = atof(&argv[1][2]);
9014641Sslatteng 			break;
9114392Sslatteng 		}
9214392Sslatteng 		argc--;
9314392Sslatteng 		argv++;
9414392Sslatteng 	}
9514403Sslatteng 
9614403Sslatteng 	fileinit();
9714392Sslatteng 	setdefaults();
9814392Sslatteng 	if (argc <= 1) {
9914392Sslatteng 		yyin = stdin;
10014392Sslatteng 		getdata(yyin);
10114392Sslatteng 	} else
10214392Sslatteng 		while (argc-- > 1) {
10315788Sslatteng 			filename = *++argv;
10415788Sslatteng 			if ((yyin = fopen(filename, "r")) == NULL) {
10515788Sslatteng 			    if (filename[0] != '-' || filename[1]) {
10614392Sslatteng 				fprintf(stderr, "pic: can't open %s\n", *argv);
10714392Sslatteng 				exit(1);
10815788Sslatteng 			    } else {
10915788Sslatteng 				yyin = stdin;
11015788Sslatteng 				filename = "standard input";
11115788Sslatteng 			    }
11214392Sslatteng 			}
11314392Sslatteng 			getdata(yyin);
11414392Sslatteng 			fclose(yyin);
11514392Sslatteng 		}
11615262Sslatteng 	exit(synerr);
11714392Sslatteng }
11814392Sslatteng 
11914403Sslatteng 
fileinit()12014403Sslatteng fileinit()
12114403Sslatteng {
12214403Sslatteng 	int fin;
12314403Sslatteng 	struct dev device;
12414403Sslatteng 	char temp[100];
12514403Sslatteng 
12614403Sslatteng 	sprintf(temp, "%s/dev%s/DESC.out", devdir, dev);
12714403Sslatteng 	if ((fin = open(temp, 0)) < 0) {
12814403Sslatteng 	    fprintf(stderr, "can't open tables for %s\n", temp);
12914403Sslatteng 	    exit(1);
13014403Sslatteng 	}
13114403Sslatteng 	read(fin, &device, sizeof(struct dev));
13214403Sslatteng 	res = device.res;
13314403Sslatteng 	DX = device.hor;
13414403Sslatteng 	DY = device.vert;
13514403Sslatteng 	close(fin);
13614403Sslatteng }
13714403Sslatteng 
13814403Sslatteng 
13914392Sslatteng static struct {
14014392Sslatteng 	char *name;
14114392Sslatteng 	float val;
14214392Sslatteng } defaults[] ={
14314392Sslatteng 	"scale", SCALE,
14414392Sslatteng 	"lineht", HT,
14514392Sslatteng 	"linewid", HT,
14614392Sslatteng 	"moveht", HT,
14714392Sslatteng 	"movewid", HT,
14814392Sslatteng 	"dashwid", HT10,
14914392Sslatteng 	"boxht", HT,
15014392Sslatteng 	"boxwid", WID,
15114392Sslatteng 	"circlerad", HT2,
15214392Sslatteng 	"arcrad", HT2,
15314392Sslatteng 	"ellipseht", HT,
15414392Sslatteng 	"ellipsewid", WID,
15514392Sslatteng 	"arrowht", HT5,
15614392Sslatteng 	"arrowwid", HT10,
15714392Sslatteng 	"textht", HT,
15814392Sslatteng 	"textwid", WID,
15914392Sslatteng 	NULL, 0
16014392Sslatteng };
16114392Sslatteng 
setdefaults()16214392Sslatteng setdefaults()	/* set default sizes for variables like boxht */
16314392Sslatteng {
16414392Sslatteng 	int i;
16514392Sslatteng 	YYSTYPE v;
16614392Sslatteng 
16714392Sslatteng 	for (i = 0; defaults[i].name != NULL; i++) {
16814392Sslatteng 		v.f = defaults[i].val;
16914392Sslatteng 		makevar(tostring(defaults[i].name), VARNAME, v);
17014392Sslatteng 	}
17114392Sslatteng }
17214392Sslatteng 
17314392Sslatteng 
checkscale(s)17414392Sslatteng checkscale(s)	/* if s is "scale", adjust default variables */
17514392Sslatteng 	char *s;
17614392Sslatteng {
17714392Sslatteng 	int i;
17814392Sslatteng 	float scale;
17914392Sslatteng 
18014392Sslatteng 	if (strcmp(s, "scale") == 0) {
18114392Sslatteng 		scale = getfval("scale");
18214392Sslatteng 		for (i = 1; defaults[i].name != NULL; i++)
18314392Sslatteng 			setfval(defaults[i].name, defaults[i].val * scale);
18414392Sslatteng 	}
18514392Sslatteng }
18614392Sslatteng 
getdata(fin)18714392Sslatteng getdata(fin)
18814392Sslatteng 	register FILE *fin;
18914392Sslatteng {
19014392Sslatteng 	char buf[1000], buf1[50];
19114392Sslatteng 	FILE *svyyin;
19214392Sslatteng 	int svlineno;
19314392Sslatteng 	char *svfilename, *p;
19414392Sslatteng 
19514392Sslatteng 	lineno = 0;
19614392Sslatteng 	while (fgets(buf, sizeof buf, fin) != NULL) {
19714392Sslatteng 		lineno++;
19814392Sslatteng 		if (*buf == '.' && *(buf+1) == 'P' && *(buf+2) == 'S') {
19914392Sslatteng 			for (p = &buf[3]; *p == ' '; p++)
20014392Sslatteng 				;
20114392Sslatteng 			if (*p++ == '<') {
20214392Sslatteng 				svyyin = yyin;
20314392Sslatteng 				svlineno = lineno;
20414392Sslatteng 				svfilename = filename;
20514392Sslatteng 				sscanf(p, "%s", buf1);
20614392Sslatteng 				if ((yyin = fopen(buf1, "r")) == NULL) {
20714392Sslatteng 					fprintf(stderr, "pic: can't open %s\n", buf1);
20814392Sslatteng 					exit(1);
20914392Sslatteng 				}
21014392Sslatteng 				lineno = 0;
21114392Sslatteng 				filename = p;
21214392Sslatteng 				getdata(yyin);
21314392Sslatteng 				fclose(yyin);
21414392Sslatteng 				lineno = svlineno;
21514392Sslatteng 				yyin = svyyin;
21614392Sslatteng 				filename = svfilename;
21714392Sslatteng 				continue;
21814392Sslatteng 			}
21914392Sslatteng 			reset();
22014392Sslatteng 			yyparse();
22114392Sslatteng 			/* yylval now contains 'E' or 'F' from .PE or .PF */
22214392Sslatteng 			if (buf[3] == ' ')	/* assume next thing is width */
22314392Sslatteng 				deltx = delty = atof(&buf[4]);
22414392Sslatteng 			else {
22514392Sslatteng 				deltx = xmax - xmin;
22614392Sslatteng 				if (deltx <= 0)
22714392Sslatteng 					deltx = ymax - ymin;
22814392Sslatteng 				deltx /= getfval("scale");
22914392Sslatteng 				delty = deltx;
23014392Sslatteng 			}
23114392Sslatteng 			dprintf("deltx = %.3f\n", deltx);
23214392Sslatteng 			if (codegen && !synerr) {
23314392Sslatteng 				openpl(&buf[3]);	/* puts out .PS, with ht & wid stuck in */
23414392Sslatteng 				print();	/* assumes \n at end */
23514392Sslatteng 				closepl(yylval.p);	/* does the .PE/F */
23614392Sslatteng 			}
23714392Sslatteng 			fflush(stdout);
23814392Sslatteng 		}
23914392Sslatteng 		else
24014392Sslatteng 			fputs(buf, stdout);
24114392Sslatteng 	}
24214392Sslatteng }
24314392Sslatteng 
reset()24414392Sslatteng reset()
24514392Sslatteng {
24614392Sslatteng 	struct obj *op;
24714392Sslatteng 	int i;
24814392Sslatteng 	struct symtab *p;
24914392Sslatteng 	extern int nstack;
25014392Sslatteng 
25114392Sslatteng 	for (i = 0; i < nobj; i++) {
25214392Sslatteng 		op = objlist[i];
25314392Sslatteng 		if (op->o_type == BLOCK)
25414392Sslatteng 			freesymtab(op->o_dotdash);	/* funny place */
25514392Sslatteng 		free(objlist[i]);
25614392Sslatteng 	}
25714392Sslatteng 	nobj = 0;
25814392Sslatteng 	nattr = 0;
25914392Sslatteng 	for (i = 0; i < ntext; i++)
26014392Sslatteng 		free(text[i].t_val);
26114392Sslatteng 	ntext = ntext1 = 0;
26214392Sslatteng 	codegen = synerr = 0;
26314392Sslatteng 	nstack = 0;
26414392Sslatteng 	curx = cury = 0;
26514392Sslatteng 	hvmode = R_DIR;
26614392Sslatteng 	sxmin = symin = 0;
26714392Sslatteng 	sxmax = symax = 4096;
26814392Sslatteng 	xmin = ymin = 30000;
26914392Sslatteng 	xmax = ymax = -30000;
27014392Sslatteng }
271