Back to index

radiance  4R0+20100331
igraph.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: igraph.c,v 1.4 2003/11/15 02:13:37 schorsch Exp $";
00003 #endif
00004 /*
00005  *  igraph.c - interactive graphing program.
00006  *
00007  *     6/30/86
00008  *
00009  *     Greg Ward Larson
00010  */
00011 
00012 #include  <stdio.h>
00013 #include  <ctype.h>
00014 #include  <setjmp.h>
00015 
00016 #include  "rtprocess.h"
00017 #include  "rterror.h"
00018 #include  "meta.h"
00019 #include  "mgraph.h"
00020 #include  "mgvars.h"
00021 
00022 typedef struct {
00023        char  *descrip;             /* description */
00024        char  *value;        /* value */
00025 }  SMAP;
00026 
00027 #define  NDEV        10     /* number of devices */
00028 
00029 SMAP  dev[NDEV] = {
00030        { "Tektronix 4014", "t4014" },
00031        { "AED 512", "output aed5" },
00032        { "X10 Window System", "xmeta" },
00033        { "X11 Window System", "x11meta" },
00034        { "Epson Printer", "output mx80" },
00035        { "Mannesman-Tally Printer", "output mt160" },
00036        { "Mannesman-Tally High Density", "output mt160l" },
00037        { "Apple Imagewriter", "output imagew" },
00038        { "Imagen Printer", "impress | ipr" },
00039        { "Postscript Printer", "psmeta | lpr" },
00040 };
00041 
00042 #define  NTYP        7      /* number of plot types */
00043 
00044 SMAP  typ[NTYP] = {
00045        { "Cartesian Plot", "cartesian.plt" },
00046        { "Polar Plot (degrees)", "polar.plt" },
00047        { "Curve Plot (symbols & lines)", "curve.plt" },
00048        { "Scatter Plot (symbols only)", "scatter.plt" },
00049        { "Line Plot (lines only)", "line.plt" },
00050        { "Function Plot (lines)", "function.plt" },
00051        { "Box and Whisker Plot", "boxw.plt" },
00052 };
00053 
00054 #define  NCAL        3      /* number of calculation types */
00055 
00056 SMAP  cal[NCAL] = {
00057        { "Extrema, Average", "nma" },
00058        { "Integral", "ni" },
00059        { "Linear Regression", "nl" },
00060 };
00061 
00062 char  *progname;
00063 
00064 char  *libpath[4];
00065 
00066 static int  recover = 0;    /* try to recover? */
00067 
00068 static jmp_buf  env;        /* setjmp buffer */
00069 
00070 static char  curfile[64] = "temp.plt";    /* current file name */
00071 
00072 static void mainmenu(void);
00073 static void uwait(void);
00074 static void gcompute(void);
00075 static void setvars(int  nvar, VARIABLE  vars[]);
00076 static void plotout(void);
00077 static void settype(void);
00078 static char  *getfile(void);
00079 
00080 int
00081 main(argc, argv)
00082 int  argc;
00083 char  *argv[];
00084 {
00085        char  *getenv();
00086        int  i;
00087 
00088        progname = argv[0];
00089        libpath[0] = "./";
00090        if ((libpath[i=1] = getenv("MDIR")) != NULL)
00091               i++;
00092        libpath[i++] = MDIR;
00093        libpath[i] = NULL;
00094 
00095        for (i = 1; i < argc; i++)
00096               mgload(strcpy(curfile, argv[i]));
00097 
00098        mainmenu();
00099 
00100        quit(0);
00101        return 0; /* pro forma return */
00102 }
00103 
00104 extern void
00105 eputs(msg)                         /* print error message */
00106 char  *msg;
00107 {
00108        fputs(msg, stderr);
00109 }
00110 
00111 extern void
00112 quit(code)                         /* recover or quit */
00113 int  code;
00114 {
00115        if (code && recover--)
00116               longjmp(env, 1);
00117        exit(code);
00118 }
00119 
00120 
00121 static void
00122 mainmenu(void)                     /* the main menu loop */
00123 {
00124        char  sbuf[128];
00125 
00126        for ( ; ; ) {
00127               printf("\nGRAPH MENU\n");
00128               printf("\t1   New Plot\n");
00129               printf("\t2   Set Plot Type\n");
00130               printf("\t3   Load Plot File\n");
00131               printf("\t4   Save Plot File\n");
00132               printf("\t5   Change Plot Variable\n");
00133               printf("\t6   Change Curve Variable\n");
00134               printf("\t7   Output Plot\n");
00135               printf("\t8   Escape Command\n");
00136               printf("\t9   Computation\n");
00137               printf("\t10  Quit\n");
00138 
00139               printf("\nChoice: ");
00140               clearerr(stdin); fgets(sbuf, sizeof(sbuf), stdin);
00141               if (feof(stdin))
00142                      return;
00143               switch (atoi(sbuf)) {
00144               case 1:                            /* new plot */
00145                      mgclearall();
00146                      break;
00147               case 2:                            /* set plot type */
00148                      settype();
00149                      break;
00150               case 3:                            /* load plot file */
00151                      if (setjmp(env) == 0) {
00152                             recover = 1;
00153                             mgload(getfile());
00154                             recover = 0;
00155                      } else
00156                             uwait();
00157                      break;
00158               case 4:                            /* save plot file */
00159                      if (setjmp(env) == 0) {
00160                          recover = 1;
00161                          mgsave(getfile());
00162                          recover = 0;
00163                      } else
00164                             uwait();
00165                      break;
00166               case 5:                            /* set plot variable */
00167                      setvars(NVARS, gparam);
00168                      break;
00169               case 6:                            /* set curve variable */
00170                      printf("\nCurve (A-%c): ", MAXCUR-1+'A');
00171                      clearerr(stdin); fgets(sbuf, sizeof(sbuf), stdin);
00172                      if (islower(sbuf[0]))
00173                             sbuf[0] -= 'a';
00174                      else if (isupper(sbuf[0]))
00175                             sbuf[0] -= 'A';
00176                      else
00177                             break;
00178                      if (sbuf[0] < MAXCUR)
00179                             setvars(NCVARS, cparam[(int)sbuf[0]]);
00180                      break;
00181               case 7:                            /* output plot */
00182                      plotout();
00183                      break;
00184               case 8:                            /* escape command */
00185                      printf("\nCommand: ");
00186                      clearerr(stdin); fgets(sbuf, sizeof(sbuf), stdin);
00187                      if (sbuf[0]) {
00188                             system(sbuf);
00189                             uwait();
00190                      }
00191                      break;
00192               case 9:                            /* computation */
00193                      gcompute();
00194                      break;
00195               case 10:                    /* quit */
00196                      return;
00197               }
00198        }
00199 }
00200 
00201 
00202 static char *
00203 getfile(void)               /* get file name from user */
00204 {
00205        char  sbuf[128];
00206 
00207        printf("\nFile (%s): ", curfile);
00208        clearerr(stdin); fgets(sbuf, sizeof(sbuf), stdin);
00209        if (sbuf[0] == '-')
00210               return(NULL);
00211        if (sbuf[0])
00212               strcpy(curfile, sbuf);
00213        return(curfile);
00214 }
00215 
00216 static void
00217 settype(void)               /* set plot type */
00218 {
00219        char  sbuf[128];
00220        int  i;
00221 
00222        printf("\nPLOT TYPE\n");
00223 
00224        for (i = 0; i < NTYP; i++)
00225               printf("\t%d  %s\n", i+1, typ[i].descrip);
00226        
00227        printf("\nChoice (0): ");
00228        clearerr(stdin); fgets(sbuf, sizeof(sbuf), stdin);
00229        i = atoi(sbuf) - 1;
00230        if (i < 0 || i >= NTYP)
00231               return;
00232        sprintf(sbuf, "include=%s", typ[i].value);
00233        setmgvar("plot type", stdin, sbuf);
00234 }
00235 
00236 
00237 static void
00238 plotout(void)               /* output our graph */
00239 {
00240        extern FILE  *pout;
00241        char  sbuf[128];
00242        char  *command = NULL;
00243        int  i;
00244 
00245        printf("\nOUTPUT PLOT\n");
00246 
00247        for (i = 0; i < NDEV; i++)
00248               printf("\t%d  %s\n", i+1, dev[i].descrip);
00249        printf("\t%d  Dumb Terminal\n", NDEV+1);
00250        printf("\t%d  Other\n", NDEV+2);
00251        
00252        printf("\nChoice (0): ");
00253        clearerr(stdin); fgets(sbuf, sizeof(sbuf), stdin);
00254        i = atoi(sbuf) - 1;
00255        if (i < 0 || i > NDEV+1)
00256               return;
00257        if (i < NDEV)
00258               command = dev[i].value;
00259        else if (i == NDEV) {
00260               if (setjmp(env) == 0) {
00261                      recover = 1;
00262                      cgraph(79, 22);
00263                      recover = 0;
00264               }
00265               uwait();
00266               return;
00267        } else if (i == NDEV+1) {
00268               printf("\nDriver command: ");
00269               clearerr(stdin); fgets(sbuf, sizeof(sbuf), stdin);
00270               if (!sbuf[0])
00271                      return;
00272               command = sbuf;
00273        }
00274        if ((pout = popen(command, "w")) == NULL) {
00275               fprintf(stderr, "%s: cannot execute device driver: %s\n",
00276                             progname, dev[i].value);
00277               quit(1);
00278        }
00279        if (setjmp(env) == 0) {
00280               recover = 1;
00281               mgraph();
00282               recover = 0;
00283        } else
00284               uwait();
00285        mdone();
00286        pclose(pout);
00287 }
00288 
00289 
00290 static void
00291 setvars(             /* set variables */
00292 int  nvar,
00293 VARIABLE  vars[])
00294 {
00295        char  sbuf[128];
00296        int  i, j;
00297 
00298        for ( ; ; ) {
00299               printf("\nVARIABLES\n");
00300 
00301               for (i = 0; i < nvar; i++)
00302                      printf("\t%d  %s\n", i+1, vars[i].descrip == NULL ?
00303                                    vars[i].name : vars[i].descrip);
00304 
00305               printf("\nChoice (0): ");
00306               clearerr(stdin); fgets(sbuf, sizeof(sbuf), stdin);
00307               i = atoi(sbuf) - 1;
00308               if (i < 0 || i >= nvar)
00309                      return;
00310               printf("\n%s", vars[i].name);
00311               if (vars[i].type == FUNCTION)
00312                      printf("(x)");
00313               if (vars[i].flags & DEFINED) {
00314                      mgtoa(sbuf, &vars[i]);
00315                      printf(" (%s)", sbuf);
00316               }
00317               printf(": ");
00318               if (vars[i].type == FUNCTION)
00319                      sprintf(sbuf, "%s(x)=", vars[i].name);
00320               else
00321                      sprintf(sbuf, "%s=", vars[i].name);
00322               j = strlen(sbuf);
00323               clearerr(stdin); fgets(sbuf+j, sizeof(sbuf)-j, stdin);
00324               if (sbuf[j]) {
00325                      if (vars[i].type == DATA && sbuf[j] == '-')
00326                             sbuf[j] = '\0';
00327 
00328                      if (setjmp(env) == 0) {
00329                             recover = 1;
00330                             setmgvar("variable set", stdin, sbuf);
00331                             recover = 0;
00332                      } else
00333                             uwait();
00334               }
00335        }
00336 }
00337 
00338 
00339 static void
00340 gcompute(void)                     /* do a computation */
00341 {
00342        char  sbuf[64];
00343        int  i;
00344 
00345        printf("\nCOMPUTE\n");
00346 
00347        for (i = 0; i < NCAL; i++)
00348               printf("\t%d  %s\n", i+1, cal[i].descrip);
00349        
00350        printf("\nChoice (0): ");
00351        clearerr(stdin); fgets(sbuf, sizeof(sbuf), stdin);
00352        i = atoi(sbuf) - 1;
00353        if (i < 0 || i >= NCAL)
00354               return;
00355        printf("\n");
00356        if (setjmp(env) == 0) {
00357               recover = 1;
00358               gcalc(cal[i].value);
00359               recover = 0;
00360        }
00361        printf("\n");
00362        uwait();
00363 }
00364 
00365 
00366 static void
00367 uwait(void)                        /* wait for user after command, error */
00368 {
00369        printf("Hit return to continue: ");
00370        while (getchar() != '\n')
00371               ;
00372 }