1 #include	<stdio.h>
2 #include	<ctype.h>
3 #include	<strings.h>
4 #include	"pic.h"
5 #include	"picy.h"
6 
7 static reset();
8 
9 struct	obj	*objlist[MAXOBJ];	/* store the elements here */
10 int	nobj	= 0;
11 
12 struct attr	attr[40];	/* attributes stored here as collected */
13 int	nattr	= 0;	/* number of entries in attr_list */
14 
15 struct	text	text[MAXTEXT];	/* text strings stored here as collected */
16 int	ntext	= 0;
17 int	ntext1	= 0;	/* record ntext here on entry to each figure */
18 
19 float	curx	= 0;
20 float	cury	= 0;
21 
22 int	hvmode	= R_DIR;	/* R => join left to right, D => top to bottom,
23  etc. */
24 
25 int	codegen	= 0;	/* 1=>output for this picture; 0=>no output */
26 
27 float	deltx	= 6;	/* max x value in output, for scaling */
28 float	delty	= 6;	/* max y value in output, for scaling */
29 int	dbg	= 0;
30 extern	FILE	*yyin, *yyout;	/* input and out file pointers */
31 FILE	*TEXFILE;	/* Output file */
32 int	lineno	= 0;
33 char	*filename	= "-";
34 int	synerr	= 0;
35 char	*cmdname;
36 int	crop	= 1;	/* trim off exterior white space if non-zero */
37 int	res	= 1000;		/* pseudo-resolution */
38 
39 float	sxmin;		/* lower limit from s command */
40 float	symin;
41 float	sxmax	= 4096;	/* upper */
42 float	symax	= 4096;
43 
44 float	xmin	= 30000;	/* min values found in actual data */
45 float	ymin	= 30000;
46 float	xmax	= -30000;	/* max */
47 float	ymax	= -30000;
48 
main(argc,argv)49 main(argc, argv)
50 	char **argv;
51 {
52 	char TEXfilename[BUFSIZ], buffer[BUFSIZ], *bp, *rindex();
53 
54 	yyin = stdin; yyout = stdout;
55 	cmdname = argv[0];
56 	while (argc > 1 && *argv[1] == '-') {
57 		switch (argv[1][1]) {
58 		case 'd':
59 			dbg = 1;
60 			break;
61 		}
62 		argc--;
63 		argv++;
64 	}
65 	setdefaults();
66 	if (argc <= 1) {
67 		fprintf(stderr, "pic2fig: No input file specified\n");
68 	} else
69 		while (argc-- > 1) {
70 			if ((yyin = fopen(*++argv, "r")) == NULL) {
71 				(void) sprintf(TEXfilename, "%s.pic", *argv);
72 				if ((yyin = fopen(TEXfilename, "r")) == NULL) {
73 				    fprintf(stderr, "pic2fig: can't open %s\n",
74 					*argv);
75 				    exit(1);
76 				}
77 			}
78 			filename = *argv;
79 			(void) sprintf(TEXfilename, "%s.fig", filename);
80 			(void) strcpy (buffer, filename);
81 			if ((bp = rindex (buffer, '.')) && strcmp (bp, ".pic")
82 == 0) {
83 			    *bp = NULL;
84 			    (void) sprintf(TEXfilename, "%s.fig", buffer);
85 			}
86 #ifdef EUNICE
87 	/* Open up a VMS style file, can't use standard fopen */
88 			{int tmpfd;
89 			  if (((tmpfd = creat(TEXfilename,0644,"txt"))==-1) ||
90 			      ((TEXFILE = fdopen(tmpfd,"w"))==NULL)) {
91 			    fprintf(stderr,"Unable to open %s\n",TEXfilename);
92 			    exit(1);
93 			  }
94 			}
95 #else EUNICE
96 			TEXFILE = fopen(TEXfilename, "w");
97 			if (TEXFILE == NULL) {
98 			   fprintf(stderr, "Unable to open %s\n", TEXfilename);
99 			   exit(1);
100 			}
101 #endif EUNICE
102 			getdata(yyin);
103 			fclose(yyin);
104 			fclose(TEXFILE);
105 		}
106 	exit(0);
107 }
108 
109 static struct {
110 	char *name;
111 	float val;
112 } defaults[] ={
113 	"scale", SCALE,
114 	"lineht", HT,
115 	"linewid", HT,
116 	"moveht", HT,
117 	"movewid", HT,
118 	"dashwid", HT10,
119 	"boxht", HT,
120 	"boxwid", WID,
121 	"circlerad", HT2,
122 	"arcrad", HT2,
123 	"ellipseht", HT,
124 	"ellipsewid", WID,
125 	"arrowht", HT5,
126 	"arrowwid", HT10,
127 	"textht", HT,
128 	"textwid", WID,
129 	NULL, 0
130 };
131 
setdefaults()132 setdefaults()	/* set default sizes for variables like boxht */
133 {
134 	int i;
135 	YYSTYPE v;
136 
137 	for (i = 0; defaults[i].name != NULL; i++) {
138 		v.f = defaults[i].val;
139 		makevar(tostring(defaults[i].name), VARNAME, v);
140 	}
141 }
142 
143 
checkscale(s)144 checkscale(s)	/* if s is "scale", adjust default variables */
145 	char *s;
146 {
147 	int i;
148 	float scale;
149 
150 	if (strcmp(s, "scale") == 0) {
151 		scale = getfval("scale");
152 		for (i = 1; defaults[i].name != NULL; i++)
153 			setfval(defaults[i].name, defaults[i].val * scale);
154 	}
155 }
156 
getdata(fin)157 getdata(fin)
158 	register FILE *fin;
159 {
160 	char buf[1000], buf1[50];
161 	FILE *svyyin;
162 	int svlineno;
163 	char *svfilename, *p;
164 
165 	lineno = 0;
166 	while (fgets(buf, sizeof buf, fin) != NULL) {
167 		lineno++;
168 		if (*buf == '.' && *(buf+1) == 'P' && *(buf+2) == 'S') {
169 			for (p = &buf[3]; isspace(*p); p++)
170 				;
171 			if (*p++ == '<') {
172 				svyyin = yyin;
173 				svlineno = lineno;
174 				svfilename = filename;
175 				sscanf(p, "%s", buf1);
176 				if ((yyin = fopen(buf1, "r")) == NULL) {
177 					fprintf(stderr, "pic2fig: can't open %s\n"
178 , buf1);
179 					exit(1);
180 				}
181 				lineno = 0;
182 				filename = p;
183 				getdata(yyin);
184 				fclose(yyin);
185 				lineno = svlineno;
186 				yyin = svyyin;
187 				filename = svfilename;
188 				continue;
189 			}
190 			reset();
191 			yyparse();
192 			/* yylval now contains 'E' or 'F' from .PE or .PF */
193 			if (isspace(buf[3]) &&
194 				(isdigit(buf[4]) ||
195 					(buf[4]=='.' && isdigit(buf[5]))))
196 						/* assume next thing is width */
197 				deltx = delty = atof(&buf[4]);
198 			else {
199 				deltx = xmax - xmin;
200 				if (deltx <= 0)
201 					deltx = ymax - ymin;
202 				deltx = deltx / getfval("scale");
203 				delty = deltx;
204 			}
205 			dprintf("deltx = %.3f\n", deltx);
206 			if (codegen && !synerr) {
207 				openpl();
208 				print();	/* assumes \n at end */
209 				closepl();
210 			}
211 			fflush(stdout);
212 		}
213 		else
214 			fputs(buf, TEXFILE);
215 	}
216 }
217 
reset()218 static reset()
219 {
220 	struct obj *op;
221 	int i;
222 	extern int nstack;
223 
224 	for (i = 0; i < nobj; i++) {
225 		op = objlist[i];
226 		if (op->o_type == BLOCK)
227 			freesymtab((struct symtab *)op->o_dotdash);	/* funn
228 y place */
229 		free((char *)objlist[i]);
230 	}
231 	nobj = 0;
232 	nattr = 0;
233 	for (i = 0; i < ntext; i++)
234 		if (text[i].t_val) free(text[i].t_val);
235 	ntext = ntext1 = 0;
236 	codegen = synerr = 0;
237 	nstack = 0;
238 	curx = cury = 0;
239 	hvmode = R_DIR;
240 	sxmin = symin = 0;
241 	sxmax = symax = 4096;
242 	xmin = ymin = 30000;
243 	xmax = ymax = -30000;
244 }
245