ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/meta/igraph.c
Revision: 1.3
Committed: Fri Nov 14 00:14:40 2003 UTC (20 years, 5 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 1.2: +38 -28 lines
Log Message:
Continued ansification, and got rid of gets().

File Contents

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