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