home *** CD-ROM | disk | FTP | other *** search
- /***( init.c )******************************************************************
- * *
- * Written: Brent Faulkner - May 27, 1989 *
- * Updated: Brent Faulkner - June 13, 1989 *
- * *
- ********************************************************************************
- * *
- * Contents: open_p() - open a printer device (setup if serial device) *
- * isserial() - check if a device is serial *
- * init_p() - allocate page buffers *
- * end_p() - free page buffers *
- * *
- *******************************************************************************/
- /* include files */
- #include <stdio.h>
- #include <bench.h>
- #include "prt.h"
-
- #ifdef MSDOS
- extern int com_id; /* extern to take24.c used to identify com port */
- #endif
-
- /* function prototypes/declarations */
- #ifdef ANSI
- # ifdef QNX
- static int isserial(FILE *);
- #endif
- # ifdef MSDOS
- static int isserial(char *);
- # endif
- #else
- # ifndef UNIX
- static int isserial();
- # endif
- #endif
-
- /* global variables */
- FILE *devfp; /* stream for device */
- unsigned char **pagebuff; /* page buffer for contents */
- unsigned int **attrbuff; /* page buffer for attributes */
- char *vprtbuff; /* buffer for varargs printing */
-
- /*
- * open a printer device (initialize it if it's a serial device)
- */
- # ifdef UNIX
- static char *utmpnam; /* have to save the temp filename somewhere */
- static char *uprtdev; /* save the print device too! */
-
- int open_p(prt_dev, prt_param)
- char *prt_dev;
- char *prt_param; /* ignored in UNIX since there is no direct access */
- {
- uprtdev = strsave(prt_dev);
- utmpnam = strsave(tmpnam(NULL));
-
- devfp = fopen(utmpnam, "w"); /* open the device */
- if (devfp == (FILE *) 0)
- return(FALSE); /* couldn't open it */
-
- return(TRUE);
- }
- # endif
-
- #ifdef QNX
- int open_p(prt_dev, prt_param)
- char *prt_dev;
- char *prt_param; /* ie. 2400N81 = 2400 baud, No parity, 8 data, 1 stop */
- {
- char *nptr;
- struct stty_entry sttyent;
-
- devfp = fopen(prt_dev, "w"); /* open the device */
- if (devfp == (FILE *) 0)
- return(FALSE); /* couldn't open it */
-
- if (isserial(devfp)) /* setup serial device */
- {
- /* set up the paramaters from prt_param */
- tty_get_stty(devfp, &sttyent);
- /* baud rate */
- sttyent.stty_baud = (unsigned)(strtol(prt_param, &nptr, 10));
- switch(*nptr) /* parity */
- {
- case 'n':
- case 'N':
- sttyent.stty_parity = 0;
- break;
- case 'o':
- case 'O':
- sttyent.stty_parity = 1;
- break;
- case 'e':
- case 'E':
- sttyent.stty_parity = 3;
- break;
- case 'm':
- case 'M':
- sttyent.stty_parity = 5;
- break;
- case 's':
- case 'S':
- sttyent.stty_parity = 7;
- break;
- }
- sttyent.stty_data_bits = *(nptr + 1) - '0'; /* data bits */
- sttyent.stty_stop_bits = *(nptr + 2) - '0'; /* stop bits */
- #ifdef PDEBUG
- fprintf(stderr, "Setting serial parameters.\n");
- fprintf(stderr, "Baud = %d, parity = %c, data = %c, stop = %c.\n",
- sttyent.stty_baud, *nptr, *(nptr + 1), *(nptr + 2));
- #endif
- tty_set_stty(devfp, &sttyent);
- }
- return(TRUE);
- }
- #endif
- #ifdef MSDOS
- #define RS232 0x14
-
- #define BIT7 0x02
- #define BIT8 0x03
- #define STOP1 0x00
- #define STOP2 0x04
- #define NONE 0x00
- #define ODD 0x08
- #define EVEN 0x18
- #define BPS150 0x20
- #define BPS300 0x40
- #define BPS600 0x60
- #define BPS1200 0x80
- #define BPS2400 0xA0
- #define BPS4800 0xC0
- #define BPS9600 0xE0
-
- int open_p(prt_dev, prt_param)
- char *prt_dev;
- char *prt_param; /* ie. 2400N81 = 2400 baud, No parity, 8 data, 1 stop */
- {
- unsigned char setup = 0;
- char *nptr;
- union REGS r;
- int port;
-
- devfp = fopen(prt_dev, "w"); /* open the device */
- if (devfp == (FILE *) 0)
- return(FALSE); /* sorry, no can do!! */
-
- if (isserial(prt_dev)) /* setup the Wheaties device */
- {
- com_id = port = *(prt_dev + 3) - '1';
- switch((unsigned)(strtol(prt_param, &nptr, 10)))
- {
- case 150:
- setup |= BPS150;
- break;
- case 300:
- setup |= BPS300;
- break;
- case 600:
- setup |= BPS600;
- break;
- case 1200:
- setup |= BPS1200;
- break;
- case 2400:
- setup |= BPS2400;
- break;
- case 4800:
- setup |= BPS4800;
- break;
- case 9600:
- setup |= BPS9600;
- break;
- }
- switch(*nptr) /* parity */
- {
- case 'n':
- case 'N':
- setup |= NONE;
- break;
- case 'o':
- case 'O':
- setup |= ODD;
- break;
- case 'e':
- case 'E':
- setup |= EVEN;
- break;
- }
- switch(*(nptr + 1))
- {
- case '7':
- setup |= BIT7;
- break;
- case '8':
- setup |= BIT8;
- break;
- }
- switch(*(nptr + 2))
- {
- case '1':
- setup |= STOP1;
- break;
- case '2':
- setup |= STOP2;
- break;
- }
- r.h.ah = 0;
- r.x.dx = port;
- r.h.al = setup;
-
- int86(RS232, &r, &r);
- }
- return(TRUE);
- }
- #endif
-
- /*
- * check whether it's a serial port or not
- */
- #ifdef QNX
- static int isserial(thedev)
- FILE *thedev;
- {
- struct dev_entry devent;
-
- get_attr(thedev, &devent);
- if (devent.tty_type == DEV_ASCII)
- return(TRUE);
- else
- return(FALSE);
- }
- #endif
- #ifdef MSDOS
- static int isserial(thedev)
- char *thedev;
- {
- strupr(thedev);
- if (0 == strncmp(thedev, "COM", 3))
- return(TRUE);
- else
- return(FALSE);
- }
- #endif
-
- /*
- * allocate page buffers
- */
- void init_p(lines, cols)
- int lines;
- int cols;
- {
- int i;
- char *buf1;
- unsigned int *buf2;
-
- vprtbuff = alloc(cols); /* allocate the varargs buffer */
- /* allocate the line pointers */
- pagebuff = (unsigned char **)alloc(lines * sizeof(char *));
- attrbuff = (unsigned int **)alloc(lines * sizeof(unsigned int *));
-
- for(i = 0; i < lines; i ++) /* allocate the lines */
- {
- buf1 = (unsigned char *)alloc(cols);
- buf2 = (unsigned int *)alloc(cols * sizeof(unsigned int));
- pagebuff[i] = buf1;
- attrbuff[i] = buf2;
- }
-
- if (prtdef[POSTSCRIPT] != NULL) /* if it's a postscript printer */
- ps_init(); /* do the prologue (in flush.c) */
- }
-
- /*
- * free page buffers
- */
- void end_p(flag)
- int flag;
- {
- int i;
- #ifdef UNIX
- char ucmdbuf[128];
- #endif
-
- for(i = 0; i < (unsigned char)*prtdef[NUM_LINES]; i++)
- {
- free(pagebuff[i]);
- free(attrbuff[i]);
- }
- free(vprtbuff);
- free(pagebuff);
- free(attrbuff);
-
- fclose( devfp );
- # ifdef UNIX
- # ifdef PDEBUG
- fprintf(stderr, "Temporary file is '%s'.\n", utmpnam);
- fprintf(stderr, "Print device is '%s'.\n", uprtdev);
- # endif
- /* Unable to use do_cmd right now so use system instead
- (do_cmd turns raw mode on)
-
- do_cmd("procprnt", utmpnam, NULL);
- */
- sprintf(ucmdbuf, "procprnt %s %s %s", utmpnam, uprtdev, flag ? "Printer" : "File");
- system(ucmdbuf);
-
- /*
- unlink(utmpnam);
- */
- free(utmpnam);
- free(uprtdev);
- # endif
- }
-
- void clear_p()
- {
- int i;
-
- for(i = 0; i < (unsigned char)*prtdef[NUM_LINES]; i++)
- {
- memset(pagebuff[i], '\0', (unsigned char)*prtdef[NUM_COLS]);
- memset(attrbuff[i], '\0', sizeof(unsigned int) * (unsigned char)*prtdef[NUM_COLS]);
- }
- #ifdef PDEBUG
- fprintf(stderr, "DEBUG: clear_p - zeroed pagebuff and attrbuff\n");
- #endif
- }
-