1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: igraph.c,v 1.3 2003/11/14 00:14:40 schorsch Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* igraph.c - interactive graphing program. |
6 |
* |
7 |
* 6/30/86 |
8 |
* |
9 |
* Greg Ward Larson |
10 |
*/ |
11 |
|
12 |
#include <stdio.h> |
13 |
#include <ctype.h> |
14 |
#include <setjmp.h> |
15 |
|
16 |
#include "rtprocess.h" |
17 |
#include "rterror.h" |
18 |
#include "meta.h" |
19 |
#include "mgraph.h" |
20 |
#include "mgvars.h" |
21 |
|
22 |
typedef struct { |
23 |
char *descrip; /* description */ |
24 |
char *value; /* value */ |
25 |
} SMAP; |
26 |
|
27 |
#define NDEV 10 /* number of devices */ |
28 |
|
29 |
SMAP dev[NDEV] = { |
30 |
{ "Tektronix 4014", "t4014" }, |
31 |
{ "AED 512", "output aed5" }, |
32 |
{ "X10 Window System", "xmeta" }, |
33 |
{ "X11 Window System", "x11meta" }, |
34 |
{ "Epson Printer", "output mx80" }, |
35 |
{ "Mannesman-Tally Printer", "output mt160" }, |
36 |
{ "Mannesman-Tally High Density", "output mt160l" }, |
37 |
{ "Apple Imagewriter", "output imagew" }, |
38 |
{ "Imagen Printer", "impress | ipr" }, |
39 |
{ "Postscript Printer", "psmeta | lpr" }, |
40 |
}; |
41 |
|
42 |
#define NTYP 7 /* number of plot types */ |
43 |
|
44 |
SMAP typ[NTYP] = { |
45 |
{ "Cartesian Plot", "cartesian.plt" }, |
46 |
{ "Polar Plot (degrees)", "polar.plt" }, |
47 |
{ "Curve Plot (symbols & lines)", "curve.plt" }, |
48 |
{ "Scatter Plot (symbols only)", "scatter.plt" }, |
49 |
{ "Line Plot (lines only)", "line.plt" }, |
50 |
{ "Function Plot (lines)", "function.plt" }, |
51 |
{ "Box and Whisker Plot", "boxw.plt" }, |
52 |
}; |
53 |
|
54 |
#define NCAL 3 /* number of calculation types */ |
55 |
|
56 |
SMAP cal[NCAL] = { |
57 |
{ "Extrema, Average", "nma" }, |
58 |
{ "Integral", "ni" }, |
59 |
{ "Linear Regression", "nl" }, |
60 |
}; |
61 |
|
62 |
char *progname; |
63 |
|
64 |
char *libpath[4]; |
65 |
|
66 |
static int recover = 0; /* try to recover? */ |
67 |
|
68 |
static jmp_buf env; /* setjmp buffer */ |
69 |
|
70 |
static char curfile[64] = "temp.plt"; /* current file name */ |
71 |
|
72 |
static void mainmenu(void); |
73 |
static void uwait(void); |
74 |
static void gcompute(void); |
75 |
static void setvars(int nvar, VARIABLE vars[]); |
76 |
static void plotout(void); |
77 |
static void settype(void); |
78 |
static char *getfile(void); |
79 |
|
80 |
int |
81 |
main(argc, argv) |
82 |
int argc; |
83 |
char *argv[]; |
84 |
{ |
85 |
char *getenv(); |
86 |
int i; |
87 |
|
88 |
progname = argv[0]; |
89 |
libpath[0] = "./"; |
90 |
if ((libpath[i=1] = getenv("MDIR")) != NULL) |
91 |
i++; |
92 |
libpath[i++] = MDIR; |
93 |
libpath[i] = NULL; |
94 |
|
95 |
for (i = 1; i < argc; i++) |
96 |
mgload(strcpy(curfile, argv[i])); |
97 |
|
98 |
mainmenu(); |
99 |
|
100 |
quit(0); |
101 |
return 0; /* pro forma return */ |
102 |
} |
103 |
|
104 |
extern void |
105 |
eputs(msg) /* print error message */ |
106 |
char *msg; |
107 |
{ |
108 |
fputs(msg, stderr); |
109 |
} |
110 |
|
111 |
extern void |
112 |
quit(code) /* recover or quit */ |
113 |
int code; |
114 |
{ |
115 |
if (code && recover--) |
116 |
longjmp(env, 1); |
117 |
exit(code); |
118 |
} |
119 |
|
120 |
|
121 |
static void |
122 |
mainmenu(void) /* the main menu loop */ |
123 |
{ |
124 |
char sbuf[128]; |
125 |
|
126 |
for ( ; ; ) { |
127 |
printf("\nGRAPH MENU\n"); |
128 |
printf("\t1 New Plot\n"); |
129 |
printf("\t2 Set Plot Type\n"); |
130 |
printf("\t3 Load Plot File\n"); |
131 |
printf("\t4 Save Plot File\n"); |
132 |
printf("\t5 Change Plot Variable\n"); |
133 |
printf("\t6 Change Curve Variable\n"); |
134 |
printf("\t7 Output Plot\n"); |
135 |
printf("\t8 Escape Command\n"); |
136 |
printf("\t9 Computation\n"); |
137 |
printf("\t10 Quit\n"); |
138 |
|
139 |
printf("\nChoice: "); |
140 |
clearerr(stdin); fgets(sbuf, sizeof(sbuf), stdin); |
141 |
if (feof(stdin)) |
142 |
return; |
143 |
switch (atoi(sbuf)) { |
144 |
case 1: /* new plot */ |
145 |
mgclearall(); |
146 |
break; |
147 |
case 2: /* set plot type */ |
148 |
settype(); |
149 |
break; |
150 |
case 3: /* load plot file */ |
151 |
if (setjmp(env) == 0) { |
152 |
recover = 1; |
153 |
mgload(getfile()); |
154 |
recover = 0; |
155 |
} else |
156 |
uwait(); |
157 |
break; |
158 |
case 4: /* save plot file */ |
159 |
if (setjmp(env) == 0) { |
160 |
recover = 1; |
161 |
mgsave(getfile()); |
162 |
recover = 0; |
163 |
} else |
164 |
uwait(); |
165 |
break; |
166 |
case 5: /* set plot variable */ |
167 |
setvars(NVARS, gparam); |
168 |
break; |
169 |
case 6: /* set curve variable */ |
170 |
printf("\nCurve (A-%c): ", MAXCUR-1+'A'); |
171 |
clearerr(stdin); fgets(sbuf, sizeof(sbuf), stdin); |
172 |
if (islower(sbuf[0])) |
173 |
sbuf[0] -= 'a'; |
174 |
else if (isupper(sbuf[0])) |
175 |
sbuf[0] -= 'A'; |
176 |
else |
177 |
break; |
178 |
if (sbuf[0] < MAXCUR) |
179 |
setvars(NCVARS, cparam[(int)sbuf[0]]); |
180 |
break; |
181 |
case 7: /* output plot */ |
182 |
plotout(); |
183 |
break; |
184 |
case 8: /* escape command */ |
185 |
printf("\nCommand: "); |
186 |
clearerr(stdin); fgets(sbuf, sizeof(sbuf), stdin); |
187 |
if (sbuf[0]) { |
188 |
system(sbuf); |
189 |
uwait(); |
190 |
} |
191 |
break; |
192 |
case 9: /* computation */ |
193 |
gcompute(); |
194 |
break; |
195 |
case 10: /* quit */ |
196 |
return; |
197 |
} |
198 |
} |
199 |
} |
200 |
|
201 |
|
202 |
static char * |
203 |
getfile(void) /* get file name from user */ |
204 |
{ |
205 |
char sbuf[128]; |
206 |
|
207 |
printf("\nFile (%s): ", curfile); |
208 |
clearerr(stdin); fgets(sbuf, sizeof(sbuf), stdin); |
209 |
if (sbuf[0] == '-') |
210 |
return(NULL); |
211 |
if (sbuf[0]) |
212 |
strcpy(curfile, sbuf); |
213 |
return(curfile); |
214 |
} |
215 |
|
216 |
static void |
217 |
settype(void) /* set plot type */ |
218 |
{ |
219 |
char sbuf[128]; |
220 |
int i; |
221 |
|
222 |
printf("\nPLOT TYPE\n"); |
223 |
|
224 |
for (i = 0; i < NTYP; i++) |
225 |
printf("\t%d %s\n", i+1, typ[i].descrip); |
226 |
|
227 |
printf("\nChoice (0): "); |
228 |
clearerr(stdin); fgets(sbuf, sizeof(sbuf), stdin); |
229 |
i = atoi(sbuf) - 1; |
230 |
if (i < 0 || i >= NTYP) |
231 |
return; |
232 |
sprintf(sbuf, "include=%s", typ[i].value); |
233 |
setmgvar("plot type", stdin, sbuf); |
234 |
} |
235 |
|
236 |
|
237 |
static void |
238 |
plotout(void) /* output our graph */ |
239 |
{ |
240 |
extern FILE *pout; |
241 |
char sbuf[128]; |
242 |
char *command = NULL; |
243 |
int i; |
244 |
|
245 |
printf("\nOUTPUT PLOT\n"); |
246 |
|
247 |
for (i = 0; i < NDEV; i++) |
248 |
printf("\t%d %s\n", i+1, dev[i].descrip); |
249 |
printf("\t%d Dumb Terminal\n", NDEV+1); |
250 |
printf("\t%d Other\n", NDEV+2); |
251 |
|
252 |
printf("\nChoice (0): "); |
253 |
clearerr(stdin); fgets(sbuf, sizeof(sbuf), stdin); |
254 |
i = atoi(sbuf) - 1; |
255 |
if (i < 0 || i > NDEV+1) |
256 |
return; |
257 |
if (i < NDEV) |
258 |
command = dev[i].value; |
259 |
else if (i == NDEV) { |
260 |
if (setjmp(env) == 0) { |
261 |
recover = 1; |
262 |
cgraph(79, 22); |
263 |
recover = 0; |
264 |
} |
265 |
uwait(); |
266 |
return; |
267 |
} else if (i == NDEV+1) { |
268 |
printf("\nDriver command: "); |
269 |
clearerr(stdin); fgets(sbuf, sizeof(sbuf), stdin); |
270 |
if (!sbuf[0]) |
271 |
return; |
272 |
command = sbuf; |
273 |
} |
274 |
if ((pout = popen(command, "w")) == NULL) { |
275 |
fprintf(stderr, "%s: cannot execute device driver: %s\n", |
276 |
progname, dev[i].value); |
277 |
quit(1); |
278 |
} |
279 |
if (setjmp(env) == 0) { |
280 |
recover = 1; |
281 |
mgraph(); |
282 |
recover = 0; |
283 |
} else |
284 |
uwait(); |
285 |
mdone(); |
286 |
pclose(pout); |
287 |
} |
288 |
|
289 |
|
290 |
static void |
291 |
setvars( /* set variables */ |
292 |
int nvar, |
293 |
VARIABLE vars[]) |
294 |
{ |
295 |
char sbuf[128]; |
296 |
int i, j; |
297 |
|
298 |
for ( ; ; ) { |
299 |
printf("\nVARIABLES\n"); |
300 |
|
301 |
for (i = 0; i < nvar; i++) |
302 |
printf("\t%d %s\n", i+1, vars[i].descrip == NULL ? |
303 |
vars[i].name : vars[i].descrip); |
304 |
|
305 |
printf("\nChoice (0): "); |
306 |
clearerr(stdin); fgets(sbuf, sizeof(sbuf), stdin); |
307 |
i = atoi(sbuf) - 1; |
308 |
if (i < 0 || i >= nvar) |
309 |
return; |
310 |
printf("\n%s", vars[i].name); |
311 |
if (vars[i].type == FUNCTION) |
312 |
printf("(x)"); |
313 |
if (vars[i].flags & DEFINED) { |
314 |
mgtoa(sbuf, &vars[i]); |
315 |
printf(" (%s)", sbuf); |
316 |
} |
317 |
printf(": "); |
318 |
if (vars[i].type == FUNCTION) |
319 |
sprintf(sbuf, "%s(x)=", vars[i].name); |
320 |
else |
321 |
sprintf(sbuf, "%s=", vars[i].name); |
322 |
j = strlen(sbuf); |
323 |
clearerr(stdin); fgets(sbuf+j, sizeof(sbuf)-j, stdin); |
324 |
if (sbuf[j]) { |
325 |
if (vars[i].type == DATA && sbuf[j] == '-') |
326 |
sbuf[j] = '\0'; |
327 |
|
328 |
if (setjmp(env) == 0) { |
329 |
recover = 1; |
330 |
setmgvar("variable set", stdin, sbuf); |
331 |
recover = 0; |
332 |
} else |
333 |
uwait(); |
334 |
} |
335 |
} |
336 |
} |
337 |
|
338 |
|
339 |
static void |
340 |
gcompute(void) /* do a computation */ |
341 |
{ |
342 |
char sbuf[64]; |
343 |
int i; |
344 |
|
345 |
printf("\nCOMPUTE\n"); |
346 |
|
347 |
for (i = 0; i < NCAL; i++) |
348 |
printf("\t%d %s\n", i+1, cal[i].descrip); |
349 |
|
350 |
printf("\nChoice (0): "); |
351 |
clearerr(stdin); fgets(sbuf, sizeof(sbuf), stdin); |
352 |
i = atoi(sbuf) - 1; |
353 |
if (i < 0 || i >= NCAL) |
354 |
return; |
355 |
printf("\n"); |
356 |
if (setjmp(env) == 0) { |
357 |
recover = 1; |
358 |
gcalc(cal[i].value); |
359 |
recover = 0; |
360 |
} |
361 |
printf("\n"); |
362 |
uwait(); |
363 |
} |
364 |
|
365 |
|
366 |
static void |
367 |
uwait(void) /* wait for user after command, error */ |
368 |
{ |
369 |
printf("Hit return to continue: "); |
370 |
while (getchar() != '\n') |
371 |
; |
372 |
} |