home *** CD-ROM | disk | FTP | other *** search
/ Phoenix CD 2.0 / Phoenix_CD.cdr / 02a / pctj1186.zip / PLOTTER.C < prev    next >
Text File  |  1986-07-28  |  6KB  |  277 lines

  1. /*
  2.  *  plotter.c
  3.  *  output driver for HP7470A plotter
  4.  *  copyright 1986 Maple Lawn Farm, Inc.
  5.  *
  6.  *  options:
  7.  *  -t        reads points scaled to Tektronix (4096x3120)
  8.  *  -p        reads xmin,ymin,xmax,ymax scaling points from input
  9.  *  -l title    left-justified title
  10.  *  -c title    centered title
  11.  *  -d dev    specifies output device (tty00 is default)
  12.  *
  13.  *  compile:  cc -O -s -o plotter plotter.c
  14.  */
  15.  
  16. #include <stdio.h>
  17. #include <fcntl.h>
  18. #include <sys/ioctl.h>
  19. #include <sys/types.h>
  20. #include <termio.h>
  21. #include <signal.h>
  22.  
  23. #define CENTER    02
  24. #define SCALED    04
  25. #define TEK    01
  26. #define    BAUD     B9600
  27. #define HPXON    "\033.I80;;17:"        /* 80 char buffer, xon char */
  28. #define HPXOFF    "\033.N;19:"        /* xoff char */
  29. #define HPSTAT    "\033.O"
  30. #define HPABORT    "\033.K"
  31. #define HPRSERR    "\033.E"
  32. #define    LOCK    "/usr/spool/uucp/LCK.."
  33. #define USAGE    "usage:    %s [-tp] [-d device] [-c | -l title]\n"
  34. #define ERR(a,b)    fprintf(stderr, "%s: ", prognm),\
  35.             fprintf(stderr, a, b)
  36.  
  37. char    *title, 
  38.     *prognm, 
  39.     *plotdev = "/dev/tty00",    /* default device */
  40.     lock[sizeof(LOCK) + 5] = LOCK; 
  41. FILE    *plr, 
  42.     *plw,
  43.     *fi = stdin;
  44. int    hflag,                 /* heading flag */
  45.     pflag,                /* points, not HP-GL */
  46.     die(), quit();
  47.  
  48.  
  49. main(argc, argv)
  50. int  argc;
  51. char **argv;
  52. {
  53.     int    fd, hperr, alrmint();  
  54.     char    buf[BUFSIZ];
  55.  
  56.     prognm = *argv;
  57.     scanarg(argc, argv);        
  58.                     /* check/make lock */
  59.     do_lock();
  60.                     /* trap exits */
  61.     signal(SIGQUIT, die);
  62.     signal(SIGINT, die);
  63.     signal(SIGALRM, alrmint);
  64.                     /* line OK? */
  65.     if ((fd=open(plotdev, O_RDWR|O_NDELAY)) < 0 ) 
  66.         perror(plotdev), die();
  67.                     /* set line & handshake */
  68.     setline(fd);
  69.     fputs(HPXON, plw);
  70.     fputs(HPXOFF, plw);
  71.                     /* check plotter */
  72.     alarm(5);
  73.     do {
  74.         hperr = readhp(HPSTAT);
  75.     } while (hperr < 0 || hperr > 40);
  76.     if (hperr > 8)
  77.         ERR("not ready\n", NULL);
  78.     alarm(0);
  79.                     /* make sure we clean up */
  80.     signal(SIGINT, quit);
  81.                     /* initialize plotter */
  82.     fputs("in;", plw);
  83.     if (hflag)
  84.         heading(title);    
  85.                     /* pen 1 */    
  86.     fputs("sp1;", plw);
  87.                     /* xy points ? */
  88.     if (pflag)    
  89.         points();    
  90.                     /* HP-GL instructions */
  91.     else             
  92.         while (fgets(buf, sizeof buf, fi) != NULL)
  93.             fputs(buf, plw);
  94.                     /* check for errors */
  95.     alarm(60);
  96.     if ((hperr = readhp("OE;")))
  97.         ERR("HP-GL error = %d\n", hperr);
  98.     if ((hperr = readhp(HPRSERR)))
  99.         ERR("rs232 error = %d\n", hperr);
  100.     alarm(0);
  101.                     /* pen home, unlock */
  102.     fputs("sp0;", plw);
  103.     die();
  104. }
  105.  
  106.  
  107. heading(title)
  108. char  *title;
  109. {
  110.     int    tx, ty;
  111.                     /* fix location of title */
  112.     ty = 7350;
  113.     tx = (hflag & CENTER) ? 5150 : 500;
  114.                     /* pen 2, char size */
  115.     fprintf(plw, "sp2;si.30,.48;pu%d,%d;", tx, ty);
  116.     if (hflag & CENTER) 
  117.         fprintf(plw, "cp -%d,0;", strlen(title)/2);
  118.     fprintf(plw, "lb%s\003", title);    
  119. }
  120.  
  121.  
  122. points()
  123. {
  124.     int    first=0;
  125.     double    x, y, ipx1, 
  126.         xmin = 0.0,         /* default tektronix scaling */
  127.         ymin = 0.0, 
  128.         xmax = 4096.0, 
  129.         ymax = 3120.0; 
  130.  
  131.                     /* need scaling points? */
  132.     if (pflag & SCALED)  { 
  133.         if (fscanf(fi, "%f%f%f%f", 
  134.             &xmin, &ymin, &xmax, &ymax) == EOF)
  135.             quit();
  136.         if (xmin >= xmax || ymin >= ymax)
  137.             ERR("invalid scaling points", NULL), quit();
  138.     }
  139.                     /* get aspect ratio 
  140.                          * set new p1 
  141.                      */
  142.     ipx1 = 10250 - (7200 * (xmax-xmin)/(ymax-ymin));
  143.     fprintf(plw, "ip %.f,279,10250,7479;", ipx1);
  144.  
  145.                     /* scale to user units */
  146.     fprintf(plw, "sc %.f,%.f,%.f,%.f;", 
  147.             xmin, xmax, ymin, ymax);
  148.                     /* read and output points */
  149.     while (fscanf(fi, "%f%f", &x, &y) != EOF) 
  150.         fprintf(plw,(++first==1) ?\
  151.             "pu%.4f,%.4f;pd" : "%.4f,%.4f ",x,y);
  152.     fputs("pu;", plw);
  153. }
  154.  
  155.  
  156. alrmint()
  157. {
  158.     ERR("no response\n", NULL);
  159.     die();
  160. }
  161.  
  162.  
  163. die()
  164. {
  165.     if (unlink(lock) == -1) 
  166.         perror(lock);
  167.     exit(0);
  168. }
  169.  
  170.  
  171. quit()
  172. {
  173.     fputs(HPABORT, plw);
  174.     fputs(" sp0;", plw);
  175.     die();
  176. }
  177.  
  178.  
  179. scanarg(argc, argv)
  180. int  argc;
  181. char **argv;
  182. {
  183.     extern    int    optind;
  184.     extern    char    *optarg;
  185.     int    i;
  186.  
  187.         while ((i = getopt(argc, argv, "d:tpc:l:h?")) != EOF)
  188.                 switch (i)  {
  189.         case 'd' :        /* device specified */
  190.             strcpy(plotdev, optarg);  
  191.             break;
  192.         case 'p' :        /* scaling points */
  193.             pflag |= SCALED;
  194.             break;
  195.         case 't' :        /* tektronix points */
  196.             pflag |= TEK;
  197.             break;
  198.         case 'c' :        /* centered title */
  199.             hflag |= CENTER;
  200.                 case 'l' :        /* left-justified */
  201.             hflag |= 01;
  202.             title = optarg;
  203.             break;
  204.         case 'h' :
  205.         case '?' :
  206.             fprintf(stderr, USAGE, prognm), exit(1);
  207.         }
  208.         if (argc > 1 && argc != optind)
  209.         if ((fi=fopen(argv[optind], "r")) == NULL)
  210.             ERR("cannot find %s\n", 
  211.                 argv[optind]), exit(1);
  212. }
  213.  
  214.  
  215. setline(fd)
  216. int  fd;
  217. {    
  218.     struct termio term;
  219.  
  220.     ioctl(fd, TCGETA, &term);
  221.  
  222.     term.c_cflag &= ~CBAUD;
  223.     term.c_cflag |= BAUD|CLOCAL;
  224.     term.c_lflag &= ~ECHO;
  225.     term.c_iflag |= ICRNL|IXON;
  226.         term.c_cc[VMIN] = 1;
  227.         term.c_cc[VTIME] = 0;
  228.                 /* drain output 
  229.                  * flush input queue 
  230.                  */
  231.         ioctl(fd, TCSETAF, &term);
  232.     if(!(plr=fopen(plotdev,"r")) || !(plw=fopen(plotdev,"w")))
  233.         die();
  234.                 /* unbuffered output */
  235.     setbuf(plw,0);
  236. }
  237.  
  238.  
  239. readhp(query)
  240. char  *query;
  241. {
  242.     int  hperr;
  243.  
  244.      fputs(query, plw);
  245.     if (fscanf(plr, "%d", &hperr) == EOF) 
  246.         die();
  247.     return(hperr);
  248. }
  249.  
  250.  
  251. do_lock()
  252. {
  253.                  /* alternate lock */
  254.     char    *cu = "/usr/spool/uucp/LCK..cul ",
  255.         dvc[5], *strchr();
  256.     int    ld;
  257.  
  258.                 /* parse device name */
  259.     if (strncmp(plotdev, "/dev/", 5))  {
  260.         strcpy(dvc, plotdev);
  261.         strcpy(plotdev, "/dev/");
  262.         strcat(plotdev, dvc);
  263.     }
  264.     else 
  265.         strcpy(dvc, strchr(plotdev, 't'));
  266.                 /* setup lock file names */    
  267.     strcat(lock, dvc);
  268.     cu[strlen(cu) -1] = dvc[strlen(dvc) -1];
  269.                 /* check for locks */
  270.     if (!access(lock,0) || !access(cu,0))
  271.         ERR("%s is locked\n", dvc), exit(1);
  272.                 /* create a lock */
  273.     if ((ld = creat(lock, 0644)) < 0)
  274.         perror(lock), exit(1);
  275.     close(ld);
  276. }
  277.