home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume6 / plot.uport / plot.c < prev    next >
C/C++ Source or Header  |  1989-04-08  |  4KB  |  168 lines

  1. /*
  2.  * plot(1) clone. This filter implements the commands described in the 
  3.  * V7 UNIX manual pages, using the gl graphics library routines.
  4.  *
  5.  * Author: Wietse Venema (wietse@wzv.UUCP)
  6.  *
  7.  * Options: -Tdevice, where device is one of:
  8.  *
  9.  *    cga (CGA)    cga adapter, low (high) resolution
  10.  *    herc (HERC)    hercules adapter, page 0 (page 1)
  11.  *    ega        ega adapter
  12.  *    lp        matrix printer 
  13.  *    lj        laserjet printer
  14.  *
  15.  * The output device can also be specified with the GLMODE environment
  16.  * variable. See the gl graphics library documentation for details.
  17.  */
  18.  
  19. #include <stdio.h>
  20. #include <modes.h>
  21.  
  22. static void     confirm();
  23.  
  24. /*
  25.  * If the output device is specified on the command line we pass it on to
  26.  * the gl library routines by setting the GLMODE environment variable...
  27.  */
  28.  
  29. struct Modetab {
  30.     char           *modename;
  31.     int             modeval;
  32. };
  33.  
  34. struct Modetab  modetab[] = {
  35.     "cga",    CGA_COLOR_MODE,        /* cga lo-res */
  36.     "CGA",    CGA_HI_RES_MODE,    /* cga hi-res */
  37.     "herc",    HERC_P0_MODE,        /* hercules page 0 */
  38.     "HERC",    HERC_P1_MODE,        /* hercules page 1 */
  39.     "ega",    EGA_COLOR_MODE,        /* ega */
  40.     "lp",    IBM_PRINTER,        /* matrix printer */
  41.     "lj",    LJ_PRINTER,        /* laserjet printer */
  42.     0, 0,
  43. };
  44.  
  45. /* various shorthands */
  46.  
  47. #define    READ(x)            fread((char *) &x, sizeof(x), 1, stdin)
  48. #define    READ2(a,b)        READ(a); READ(b)
  49. #define    READ3(a,b,c)        READ2(a,b); READ(c)
  50. #define    READ4(a,b,c,d)        READ2(a,b); READ2(c,d);
  51. #define    READ6(a,b,c,d,e,f)    READ4(a,b,c,d); READ2(e,f);
  52.  
  53. /* 
  54.  * Process the plotfile. The program terminates with a diagnostic
  55.  * in case of unrecognized data.
  56.  */
  57.  
  58. main(argc, argv)
  59. int             argc;
  60. char          **argv;
  61. {
  62.     register struct Modetab *mp;
  63.     register int    c;
  64.     char            buf[BUFSIZ];
  65.     int             x, y, x0, y0, x1, y1, x2, y2, r, glmode;
  66.     static char     envstring[] = "GLMODE=xxxxxx";
  67.  
  68.     /* process command-line arguments */
  69.  
  70.     while (--argc && *++argv) {
  71.     if (strncmp(*argv, "-T", 2) == 0) {
  72.         for (mp = modetab; mp->modename; mp++) {
  73.         if (strcmp(*argv + 2, mp->modename) == 0) {
  74.             sprintf(envstring, "GLMODE=%d", glmode = mp->modeval);
  75.             putenv(envstring);
  76.         }
  77.         }
  78.     } else {
  79.         fprintf(stderr, "bad argument: %s\n", *argv);
  80.         exit(1);
  81.     }
  82.     }
  83.  
  84. #ifndef    unix
  85.     you may have to select binary mode for stdin
  86. #endif
  87.  
  88.     /* process the plotfile */
  89.  
  90.     openpl();
  91.  
  92.     while ((c = getchar()) != EOF) {
  93.     switch (c) {
  94.     case 'm':                /* move */
  95.         READ2(x, y);
  96.         move(x, y);
  97.         break;
  98.     case 'n':                /* cont */
  99.         READ2(x, y);
  100.         cont(x, y);
  101.         break;
  102.     case 'p':                /* point */
  103.         READ2(x, y);
  104.         point(x, y);
  105.         break;
  106.     case 'l':                /* line */
  107.         READ4(x1, y1, x2, y2);
  108.         line(x1, y1, x2, y2);
  109.         break;
  110.     case 't':                /* label */
  111.         {
  112.         register char  *p = buf;
  113.  
  114.         while ((c = getchar()) != EOF && c)
  115.             *p++ = c;
  116.         *p = '\0';
  117.         label(buf);
  118.         }
  119.         break;
  120.     case 'a':                /* arc */
  121.         READ6(x, y, x0, y0, x1, y1);
  122.         arc(x, y, x0, y0, x1, y1);
  123.         break;
  124.     case 'c':                /* circle */
  125.         READ3(x, y, r);
  126.         circle(x, y, r);
  127.         break;
  128.     case 'e':                /* erase */
  129.         if (glmode <= MAXVIDEO)
  130.         confirm();
  131.         erase();
  132.         break;
  133.     case 'f':                /* linemod */
  134.         gets(buf);
  135.         linemod(buf);
  136.         break;
  137.     case 's':                /* space */
  138.         READ4(x0, y0, x1, y1);
  139.         space(x0, y0, x1, y1);
  140.         break;
  141.     default:                /* corrupt */
  142.         closepl();
  143.         fprintf(stderr,"corrupted plotfile -- giving up\n");
  144.         exit(1);
  145.         /* NOTREACHED */
  146.     }
  147.     }
  148.  
  149.     if (glmode <= MAXVIDEO)
  150.     confirm();
  151.     closepl();
  152.     exit(0);
  153.     /* NOTREACHED */
  154. }
  155.  
  156. /* give them a chance before erase() or closepl() clobber the screen */
  157.  
  158. static void     confirm()
  159. {
  160.     FILE           *fp;
  161.     int             c;
  162.  
  163.     if (fp = fopen("/dev/tty", "r")) {
  164.     while ((c = getc(fp)) != EOF && c != '\n');
  165.     fclose(fp);
  166.     }
  167. }
  168.