home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume10 / tek / tek.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-20  |  4.2 KB  |  196 lines

  1. /*
  2.  *  tek.c  
  3.  *  copyright 1988 Ronald Florence
  4.  *
  5.  *    as "tek" - Tek4014 picture files to CGI or WY99
  6.  *      -w  Accu-Weather maps
  7.  *    as "poly" - reads points for polyline on CGI or WY99
  8.  *      -s  read xmin, ymin, xmax, ymax scaling points first 
  9.  *          (default is 4096 x 3120)
  10.  *    both:
  11.  *      -p  use plotter or printer (default display)
  12.  *      -a  preserve aspect ratio
  13.  *
  14.  *    bugs: SIGINT doesn't work with wy99 modes.  Possible solution is to
  15.  *    poll terminal on every iteration, with terminal set in raw mode.
  16.  */
  17.  
  18. #include  <signal.h>
  19. #include  <stdio.h>
  20. #include  <termio.h>
  21. #include  <fcntl.h>
  22.  
  23. #define  int        short        /* for CGI functions    */
  24. #define     CRT        (wout[45] == 0)
  25. #define     To_tek        "\033[?38h"
  26. #define     To_vt220    "\033[?38l"
  27. #define     Clr_tek    "\033\f"
  28. #define  Vt_reset    "\033!p"
  29.  
  30. int    dev;            /* device id */
  31. static  int    tty;
  32. struct  termio new, old;
  33. static  int  tek;        /* =1 if invoked as tek */
  34. static  char  *progn;
  35. static  char  *dspec[] = {"CGIDISP", "CGIPRNT", 0 };
  36.  
  37. main (argc, argv)
  38.      int    argc;
  39.      char    **argv;
  40. {
  41.   FILE  *fi = stdin;
  42.   int    win[19],         /* device input info */
  43.            wout[66],         /* device output info */
  44.         h = 0, 
  45.     aspect = 0,        /* map to max CGI space */
  46.     opt = 0,
  47.     sig_handle(), quit_wyse();
  48.     char    *device, *p, *getenv(), *strrchr(); 
  49.  
  50.   progn = (p = strrchr(*argv, '/')) ? p + 1 : *argv;    
  51.   tek = strcmp(progn, "poly");
  52.   if (strlen(getenv("ASPECT")))
  53.     aspect = 3;
  54.  
  55.   while (argc-- && **++argv == '-') 
  56.     for (p = *argv+1; *p; p++)
  57.       switch (*p) 
  58.     {
  59.     case 'w' :
  60.       if (tek)
  61.         ++opt;
  62.       else
  63.         usage();
  64.       break;
  65.     case 's' :
  66.       if (!tek)
  67.         ++opt;
  68.       else
  69.         usage();
  70.       break;
  71.     case 'p' :
  72.       ++h;
  73.       break;
  74.     case 'a' :
  75.       aspect = 3;
  76.       break;
  77.     default :
  78.       usage();
  79.     }
  80.                 /* we cannot do freopen(stdin) for the */
  81.                 /* input because the CGI functions */
  82.                 /* read from stdin  */
  83.   if (argc > 0 && (fi = fopen(*argv, "r")) == NULL)  
  84.     {
  85.       printf("%s: can't open %s\n", progn, *argv);
  86.       exit (-1);
  87.     }
  88.                 /* open /dev/tty to read keystrokes */
  89.   tty = open("/dev/tty", O_RDWR);
  90.   ioctl(tty, TCGETA, &new);
  91.   ioctl(tty, TCGETA, &old);
  92.                 /* wyse 99 terminal? */
  93.   if (!h && !strncmp(getenv("TERM"), "wy99", 4))
  94.     {
  95.                  /* not this easy, alas ... */
  96.       signal(SIGINT, quit_wyse);
  97.       printf("%s%s", To_tek, Clr_tek);
  98.       if (tek)
  99.     vt_tek(fi, opt);
  100.       else
  101.     tekpts(fi, opt);
  102.       fflush(stdout);
  103.       getkey();
  104.       quit_wyse();
  105.     }
  106.                 /* check for valid CGI device */
  107.   for ( ; (device = dspec[h]) && getenv(dspec[h]) == NULL; h++) ;
  108.   if (!device)
  109.     {
  110.       printf("%s: no device\n", progn);
  111.       exit (-1);
  112.     }
  113.   signal( SIGHUP, sig_handle );
  114.   signal( SIGINT, sig_handle );
  115.   signal( SIGQUIT, SIG_IGN );
  116.  
  117.   win[0] = aspect;
  118.   win[1] = 1;            /* solid line */
  119.   win[2] = 1;            /* line color */
  120.   win[3] = 1;            /* marker type . */
  121.   win[4] = 1;            /* marker color */
  122.   win[5] = 1;            /* graphics text font */
  123.   win[6] = 1;            /* graphics text color */
  124.   win[7] = 0;            /* fill interior style */
  125.   win[8] = 0;            /* fill style index */
  126.   win[9] = 1;            /* fill color index */
  127.   win[10] = 1;            /* prompt for paper changes */
  128.   for (h = 0; device[h]; h++)
  129.     win[11+h] = device[h];
  130.   win[18] = ' ';
  131.  
  132.   if (v_opnwk(win, &dev, wout) < 0) 
  133.     {
  134.       printf("%s: error %d opening %s\n", progn, -vq_error(), device);
  135.       exit (-1);
  136.     }    
  137.   if (tek)
  138.     tekdecode(fi, opt);
  139.   else
  140.     do_poly(fi, opt);
  141.   if (CRT)  
  142.     getkey();
  143.   v_clswk(dev);
  144. }
  145.  
  146.  
  147. usage ()
  148. {
  149.   printf("usage: %s file\n", tek ? "tek [-wpa]" : "poly [-spa]");
  150.   exit (-1);
  151. }
  152.  
  153.  
  154. err (func)
  155.      char  func[];
  156. {
  157.   int    errnum;
  158.  
  159.   errnum = strcmp(func, "data") ? -(vq_error()) : 0;
  160.   v_clswk(dev);
  161.   if (errnum)
  162.     printf ("%s: error no. %d in CGI function %s\n", progn, errnum, func);
  163.   else
  164.     printf (tek ? "tek: too many data points\n" : "poly: invalid scaling points\n");
  165.   exit (-2);
  166. }
  167.  
  168.  
  169. int  sig_handle (sig)
  170.      int  sig;
  171. {
  172.   v_clswk( dev );
  173.   exit( sig );
  174. }
  175.  
  176. getkey()
  177. {
  178.   char    c;
  179.  
  180.   new.c_lflag &= ~(ECHO|ICANON);
  181.   new.c_cc[VMIN] = 1;
  182.   ioctl(tty, TCSETAW, &new);
  183.   read(tty, &c, 1);
  184.   ioctl(tty, TCSETA, &old);
  185. }
  186.  
  187. int  quit_wyse ()        /* needs all of it to restore wy99 */
  188. {
  189.   fflush (stdout);
  190.   fprintf (stderr, "%s%s", Clr_tek, To_vt220);
  191.   sleep (1);
  192.   fprintf (stderr, "%s", Vt_reset);
  193.   exit (0);
  194. }
  195.  
  196.