home *** CD-ROM | disk | FTP | other *** search
/ Jason Aller Floppy Collection / 125.img / PRO-C4.ZIP / BENCH1.ZIP / PRINT / PINIT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-28  |  7.7 KB  |  330 lines

  1. /***( init.c )******************************************************************
  2. *                                                                              *
  3. *  Written:  Brent Faulkner - May 27, 1989                                     *
  4. *  Updated:  Brent Faulkner - June 13, 1989                                    *
  5. *                                                                              *
  6. ********************************************************************************
  7. *                                                                              *
  8. * Contents:       open_p() - open a printer device (setup if serial device)    *
  9. *               isserial() - check if a device is serial                       *
  10. *                 init_p() - allocate page buffers                             *
  11. *                  end_p() - free page buffers                                 *
  12. *                                                                              *
  13. *******************************************************************************/
  14. /* include files */
  15. #include <stdio.h>
  16. #include <bench.h>
  17. #include "prt.h"
  18.  
  19. #ifdef MSDOS
  20. extern int com_id;     /* extern to take24.c used to identify com port */
  21. #endif
  22.  
  23. /* function prototypes/declarations */
  24. #ifdef ANSI
  25. # ifdef QNX
  26. static int isserial(FILE *);
  27. #endif
  28. # ifdef MSDOS
  29. static int isserial(char *);
  30. # endif
  31. #else
  32. # ifndef UNIX
  33. static int isserial();
  34. # endif
  35. #endif
  36.  
  37. /* global variables */
  38. FILE *devfp;                 /* stream for device */
  39. unsigned char **pagebuff;             /* page buffer for contents */
  40. unsigned int **attrbuff;     /* page buffer for attributes */
  41. char *vprtbuff;              /* buffer for varargs printing */
  42.  
  43. /*
  44.  * open a printer device (initialize it if it's a serial device)
  45. */
  46. # ifdef UNIX
  47. static char *utmpnam;        /* have to save the temp filename somewhere */
  48. static char *uprtdev;        /* save the print device too! */
  49.  
  50. int open_p(prt_dev, prt_param)
  51. char *prt_dev;
  52. char *prt_param;   /* ignored in UNIX since there is no direct access */
  53. {
  54.     uprtdev = strsave(prt_dev);
  55.     utmpnam = strsave(tmpnam(NULL));
  56.  
  57.     devfp = fopen(utmpnam, "w");       /* open the device */
  58.     if (devfp == (FILE *) 0)
  59.         return(FALSE);                 /* couldn't open it */
  60.  
  61.     return(TRUE);
  62. }
  63. # endif
  64.  
  65. #ifdef QNX
  66. int open_p(prt_dev, prt_param)
  67. char *prt_dev;
  68. char *prt_param;   /* ie. 2400N81 = 2400 baud, No parity, 8 data, 1 stop */
  69. {
  70.     char *nptr;
  71.     struct stty_entry sttyent;
  72.  
  73.     devfp = fopen(prt_dev, "w");       /* open the device */
  74.     if (devfp == (FILE *) 0)
  75.         return(FALSE);                 /* couldn't open it */
  76.  
  77.     if (isserial(devfp))               /* setup serial device */
  78.     {
  79.         /* set up the paramaters from prt_param */
  80.         tty_get_stty(devfp, &sttyent);
  81.                                        /* baud rate */
  82.         sttyent.stty_baud = (unsigned)(strtol(prt_param, &nptr, 10));
  83.         switch(*nptr)                  /* parity */
  84.         {
  85.             case 'n':
  86.             case 'N':
  87.                 sttyent.stty_parity = 0;
  88.                 break;
  89.             case 'o':
  90.             case 'O':
  91.                 sttyent.stty_parity = 1;
  92.                 break;
  93.             case 'e':
  94.             case 'E':
  95.                 sttyent.stty_parity = 3;
  96.                 break;
  97.             case 'm':
  98.             case 'M':
  99.                 sttyent.stty_parity = 5;
  100.                 break;
  101.             case 's':
  102.             case 'S':
  103.                 sttyent.stty_parity = 7;
  104.                 break;
  105.         }
  106.         sttyent.stty_data_bits = *(nptr + 1) - '0';  /* data bits */
  107.         sttyent.stty_stop_bits = *(nptr + 2) - '0';  /* stop bits */
  108. #ifdef PDEBUG
  109.         fprintf(stderr, "Setting serial parameters.\n");
  110.         fprintf(stderr, "Baud = %d, parity = %c, data = %c, stop = %c.\n",
  111.             sttyent.stty_baud, *nptr, *(nptr + 1), *(nptr + 2));
  112. #endif
  113.         tty_set_stty(devfp, &sttyent);
  114.     }
  115.     return(TRUE);
  116. }
  117. #endif
  118. #ifdef MSDOS
  119. #define RS232     0x14
  120.  
  121. #define BIT7      0x02
  122. #define BIT8      0x03
  123. #define STOP1     0x00
  124. #define STOP2     0x04
  125. #define NONE      0x00
  126. #define ODD       0x08
  127. #define EVEN      0x18
  128. #define BPS150    0x20
  129. #define BPS300    0x40
  130. #define BPS600    0x60
  131. #define BPS1200   0x80
  132. #define BPS2400   0xA0
  133. #define BPS4800   0xC0
  134. #define BPS9600   0xE0
  135.  
  136. int open_p(prt_dev, prt_param)
  137. char *prt_dev;
  138. char *prt_param;   /* ie. 2400N81 = 2400 baud, No parity, 8 data, 1 stop */
  139. {
  140.     unsigned char setup = 0;
  141.     char *nptr;
  142.     union REGS r;
  143.     int port;
  144.  
  145.     devfp = fopen(prt_dev, "w");             /* open the device */
  146.     if (devfp == (FILE *) 0)
  147.         return(FALSE);                        /* sorry, no can do!! */
  148.  
  149.     if (isserial(prt_dev))                   /* setup the Wheaties device */
  150.     {
  151.         com_id = port = *(prt_dev + 3) - '1';
  152.         switch((unsigned)(strtol(prt_param, &nptr, 10)))
  153.         {
  154.             case 150:
  155.                 setup |= BPS150;
  156.                 break;
  157.             case 300:
  158.                 setup |= BPS300;
  159.                 break;
  160.             case 600:
  161.                 setup |= BPS600;
  162.                 break;
  163.             case 1200:
  164.                 setup |= BPS1200;
  165.                 break;
  166.             case 2400:
  167.                 setup |= BPS2400;
  168.                 break;
  169.             case 4800:
  170.                 setup |= BPS4800;
  171.                 break;
  172.             case 9600:
  173.                 setup |= BPS9600;
  174.                 break;
  175.         }
  176.         switch(*nptr)                  /* parity */
  177.         {
  178.             case 'n':
  179.             case 'N':
  180.                 setup |= NONE;
  181.                 break;
  182.             case 'o':
  183.             case 'O':
  184.                 setup |= ODD;
  185.                 break;
  186.             case 'e':
  187.             case 'E':
  188.                 setup |= EVEN;
  189.                 break;
  190.         }
  191.         switch(*(nptr + 1))
  192.         {
  193.             case '7':
  194.                 setup |= BIT7;
  195.                 break;
  196.             case '8':
  197.                 setup |= BIT8;
  198.                 break;
  199.         }
  200.         switch(*(nptr + 2))
  201.         {
  202.             case '1':
  203.                 setup |= STOP1;
  204.                 break;
  205.             case '2':
  206.                 setup |= STOP2;
  207.                 break;
  208.         }
  209.         r.h.ah = 0;
  210.         r.x.dx = port;
  211.         r.h.al = setup;
  212.         
  213.         int86(RS232, &r, &r);
  214.     }
  215.     return(TRUE);
  216. }
  217. #endif
  218.  
  219. /*
  220.  * check whether it's a serial port or not 
  221. */
  222. #ifdef QNX
  223. static int isserial(thedev)
  224. FILE *thedev;
  225. {
  226.     struct dev_entry devent;
  227.  
  228.     get_attr(thedev, &devent);
  229.     if (devent.tty_type == DEV_ASCII)
  230.         return(TRUE);
  231.     else
  232.         return(FALSE);
  233. }
  234. #endif
  235. #ifdef MSDOS
  236. static int isserial(thedev)
  237. char *thedev;
  238. {
  239.     strupr(thedev);
  240.     if (0 == strncmp(thedev, "COM", 3))
  241.         return(TRUE);
  242.     else
  243.         return(FALSE);
  244. }
  245. #endif
  246.  
  247. /*
  248.  * allocate page buffers
  249. */
  250. void init_p(lines, cols)
  251. int lines;
  252. int cols;
  253. {
  254.     int i;
  255.     char *buf1;
  256.     unsigned int *buf2;
  257.  
  258.     vprtbuff = alloc(cols);       /* allocate the varargs buffer */
  259.                                   /* allocate the line pointers */
  260.     pagebuff = (unsigned char **)alloc(lines * sizeof(char *));
  261.     attrbuff = (unsigned int **)alloc(lines * sizeof(unsigned int *));
  262.  
  263.     for(i = 0; i < lines; i ++)   /* allocate the lines */
  264.     {
  265.         buf1 = (unsigned char *)alloc(cols);
  266.         buf2 = (unsigned int *)alloc(cols * sizeof(unsigned int));
  267.         pagebuff[i] = buf1;
  268.         attrbuff[i] = buf2;
  269.     }
  270.  
  271.     if (prtdef[POSTSCRIPT] != NULL) /* if it's a postscript printer */
  272.         ps_init();                  /* do the prologue (in flush.c) */
  273. }
  274.  
  275. /*
  276.  * free page buffers
  277. */
  278. void end_p(flag)
  279. int flag;
  280. {
  281.     int i;
  282. #ifdef UNIX
  283.     char ucmdbuf[128];
  284. #endif
  285.  
  286.     for(i = 0; i < (unsigned char)*prtdef[NUM_LINES]; i++)        
  287.     {
  288.         free(pagebuff[i]);
  289.         free(attrbuff[i]);
  290.     }
  291.     free(vprtbuff);
  292.     free(pagebuff);
  293.     free(attrbuff);
  294.  
  295.     fclose( devfp );
  296. # ifdef UNIX
  297. #  ifdef PDEBUG
  298.     fprintf(stderr, "Temporary file is '%s'.\n", utmpnam);
  299.     fprintf(stderr, "Print device is '%s'.\n", uprtdev);
  300. #  endif
  301. /* Unable to use do_cmd right now so use system instead
  302.    (do_cmd turns raw mode on)
  303.  
  304.     do_cmd("procprnt", utmpnam, NULL);
  305. */
  306.     sprintf(ucmdbuf, "procprnt %s %s %s", utmpnam, uprtdev, flag ? "Printer" : "File");
  307.     system(ucmdbuf);
  308.  
  309. /*
  310.         unlink(utmpnam);
  311. */
  312.     free(utmpnam);
  313.     free(uprtdev);
  314. # endif
  315. }
  316.  
  317. void clear_p()
  318. {
  319.     int i;
  320.  
  321.     for(i = 0; i < (unsigned char)*prtdef[NUM_LINES]; i++)
  322.     {
  323.         memset(pagebuff[i], '\0', (unsigned char)*prtdef[NUM_COLS]);
  324.         memset(attrbuff[i], '\0', sizeof(unsigned int) * (unsigned char)*prtdef[NUM_COLS]);
  325.     }
  326. #ifdef PDEBUG
  327.     fprintf(stderr, "DEBUG: clear_p - zeroed pagebuff and attrbuff\n");
  328. #endif
  329. }
  330.