Subversion Repositories planix.SVN

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

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