1*23970Sjaap #ifndef lint
2*23970Sjaap static char sccsid[] = "@(#)main.c	1.1 (CWI) 85/07/19";
3*23970Sjaap #endif lint
4*23970Sjaap #include	<stdio.h>
5*23970Sjaap #include	<signal.h>
6*23970Sjaap #include	"grap.h"
7*23970Sjaap #include	"y.tab.h"
8*23970Sjaap 
9*23970Sjaap int	dbg	= 0;
10*23970Sjaap char	*lib_defines	= "/usr/local/lib/grap/grap.defines";
11*23970Sjaap int	lib	= 1;		/* 1 to include lib_defines */
12*23970Sjaap FILE	*tfd	= NULL;
13*23970Sjaap /*
14*23970Sjaap char	*tempfile = NULL;
15*23970Sjaap */
16*23970Sjaap char	*tempfile = "/tmp/XXXXXX";
17*23970Sjaap 
18*23970Sjaap int	synerr	= 0;
19*23970Sjaap int	codegen	= 0;   /* 1=>output for this picture; 0=>no output */
20*23970Sjaap char	*cmdname;
21*23970Sjaap 
22*23970Sjaap Obj	*objlist = NULL;	/* all names stored here */
23*23970Sjaap 
24*23970Sjaap Point	ptmin	= { NULL, -1.0e10, -1.0e10 };
25*23970Sjaap Point	ptmax	= { NULL, 1.0e10, 1.0e10 };
26*23970Sjaap 
main(argc,argv)27*23970Sjaap main(argc, argv)
28*23970Sjaap 	char **argv;
29*23970Sjaap {
30*23970Sjaap 	extern int onintr(), fpecatch();
31*23970Sjaap 	char *p, *getenv();
32*23970Sjaap 
33*23970Sjaap 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
34*23970Sjaap 		signal(SIGINT, onintr);
35*23970Sjaap 	signal(SIGFPE, fpecatch);
36*23970Sjaap 	tempfile = "/tmp/grap.XXXXXX";
37*23970Sjaap 	cmdname = argv[0];
38*23970Sjaap 	while (argc > 1 && *argv[1] == '-') {
39*23970Sjaap 		switch (argv[1][1]) {
40*23970Sjaap 		case 'd':
41*23970Sjaap 			dbg = 1;
42*23970Sjaap 			tfd = stdout;
43*23970Sjaap 			tempfile = "grap.temp";
44*23970Sjaap 			unlink(tempfile);
45*23970Sjaap 			break;
46*23970Sjaap 		case 'l':	/* turn off /usr/lib inclusion */
47*23970Sjaap 			lib = 0;
48*23970Sjaap 			break;
49*23970Sjaap 		}
50*23970Sjaap 		argc--;
51*23970Sjaap 		argv++;
52*23970Sjaap 	}
53*23970Sjaap 	setdefaults();
54*23970Sjaap 	if (!dbg)
55*23970Sjaap 		mktemp(tempfile);
56*23970Sjaap 	curfile = infile;
57*23970Sjaap 	pushsrc(File, curfile);
58*23970Sjaap 	if (argc <= 1) {
59*23970Sjaap 		curfile->fin = stdin;
60*23970Sjaap 		curfile->fname = tostring("-");
61*23970Sjaap 		getdata();
62*23970Sjaap 	} else
63*23970Sjaap 		while (argc-- > 1) {
64*23970Sjaap 			if ((curfile->fin = fopen(*++argv, "r")) == NULL) {
65*23970Sjaap 				fprintf(stderr, "grap: can't open %s\n", *argv);
66*23970Sjaap 				onintr();
67*23970Sjaap 			}
68*23970Sjaap 			curfile->fname = tostring(*argv);
69*23970Sjaap 			getdata();
70*23970Sjaap 			fclose(curfile->fin);
71*23970Sjaap 			free(curfile->fname);
72*23970Sjaap 		}
73*23970Sjaap 	if (!dbg)
74*23970Sjaap 		unlink(tempfile);
75*23970Sjaap 	exit(0);
76*23970Sjaap }
77*23970Sjaap 
onintr()78*23970Sjaap onintr()
79*23970Sjaap {
80*23970Sjaap 	if (!dbg)
81*23970Sjaap 		unlink(tempfile);
82*23970Sjaap 	exit(1);
83*23970Sjaap }
84*23970Sjaap 
fpecatch()85*23970Sjaap fpecatch()
86*23970Sjaap {
87*23970Sjaap 	yyerror("floating point exception");
88*23970Sjaap 	onintr();
89*23970Sjaap }
90*23970Sjaap 
grow(ptr,name,num,size)91*23970Sjaap char *grow(ptr, name, num, size)	/* make array bigger */
92*23970Sjaap 	char *ptr, *name;
93*23970Sjaap 	int num, size;
94*23970Sjaap {
95*23970Sjaap 	char *p;
96*23970Sjaap 
97*23970Sjaap 	if (ptr == NULL)
98*23970Sjaap 		p = malloc(num * size);
99*23970Sjaap 	else
100*23970Sjaap 		p = realloc(ptr, num * size);
101*23970Sjaap 	if (p == NULL)
102*23970Sjaap 		fatal("can't grow %s to %d", name, num * size);
103*23970Sjaap 	return p;
104*23970Sjaap }
105*23970Sjaap 
106*23970Sjaap static struct {
107*23970Sjaap 	char	*name;
108*23970Sjaap 	double	val;
109*23970Sjaap } defaults[] ={
110*23970Sjaap 	"frameht", FRAMEHT,
111*23970Sjaap 	"framewid", FRAMEWID,
112*23970Sjaap 	"ticklen", TICKLEN,
113*23970Sjaap 	"slop", SLOP,
114*23970Sjaap 	NULL, 0
115*23970Sjaap };
116*23970Sjaap 
setdefaults()117*23970Sjaap setdefaults()	/* set default sizes for variables */
118*23970Sjaap {
119*23970Sjaap 	int i;
120*23970Sjaap 	Obj *p;
121*23970Sjaap 
122*23970Sjaap 	for (i = 0; defaults[i].name != NULL; i++) {
123*23970Sjaap 		p = lookup(defaults[i].name, 1);
124*23970Sjaap 		setvar(p, defaults[i].val);
125*23970Sjaap 	}
126*23970Sjaap }
127*23970Sjaap 
getdata()128*23970Sjaap getdata()		/* read input */
129*23970Sjaap {
130*23970Sjaap 	register FILE *fin;
131*23970Sjaap 	char buf[1000], buf1[100], *p;
132*23970Sjaap 	int ln;
133*23970Sjaap 
134*23970Sjaap 	fin = curfile->fin;
135*23970Sjaap 	curfile->lineno = 0;
136*23970Sjaap 	printf(".lf 1 %s\n", curfile->fname);
137*23970Sjaap 	while (fgets(buf, sizeof buf, fin) != NULL) {
138*23970Sjaap 		curfile->lineno++;
139*23970Sjaap 		if (*buf == '.' && *(buf+1) == 'G' && *(buf+2) == '1') {
140*23970Sjaap 			setup();
141*23970Sjaap 			fprintf(stdout, ".PS%s", &buf[3]);	/* maps .G1 [w] to .PS w */
142*23970Sjaap 			printf(".lf %d\n", curfile->lineno+1);
143*23970Sjaap 			yyparse();
144*23970Sjaap 			fprintf(stdout, ".PE\n");
145*23970Sjaap 			printf(".lf %d\n", curfile->lineno+1);
146*23970Sjaap 			fflush(stdout);
147*23970Sjaap 		} else if (buf[0] == '.' && buf[1] == 'l' && buf[2] == 'f') {
148*23970Sjaap 			if (sscanf(buf+3, "%d %s", &ln, buf1) == 2) {
149*23970Sjaap 				free(curfile->fname);
150*23970Sjaap 				printf(".lf %d %s\n", curfile->lineno = ln, curfile->fname = tostring(buf1));
151*23970Sjaap 			} else
152*23970Sjaap 				printf(".lf %d\n", curfile->lineno = ln);
153*23970Sjaap 		} else
154*23970Sjaap 			fputs(buf, stdout);
155*23970Sjaap 	}
156*23970Sjaap }
157