home *** CD-ROM | disk | FTP | other *** search
- /***( getprint.c )**************************************************************
- * *
- * Written: Brent Faulkner - May 25, 1989 *
- * Updated: Brent Faulkner - June 13, 1989 *
- * *
- ********************************************************************************
- * *
- * Contents: get_print() - get a prtcap entry *
- * *
- * open_prtcap() - open prtcap and find printer *
- * close_prtcap() - close it *
- * next_prtline() - get next line from prtcap *
- * getdef() - translate a prtcap line *
- * *
- *******************************************************************************/
- /* include files */
- #include <stdio.h>
- #include <bench.h>
- #include "prt.h"
-
- /* define macros */
- #define isoctal(dig) ((dig) >= '0' && (dig) <= '7')
-
- /* global variables */
- char *prtdef[LAST] = { 0 }; /* table to hold prtcap entries */
- int sizdef[LAST] = { 0 }; /* table of sizes of corresponding entries */
- FILE *prtfp = 0; /* stream for prtcap file */
-
- /* function prototypes/declarations */
- #ifdef ANSI
- static void close_prtcap(void);
- static char *getdef(char *, int *);
- static int next_prtline(char*);
- static int open_prtcap(char *);
- #else
- static void close_prtcap();
- static char *getdef();
- static int next_prtline();
- static int open_prtcap();
- #endif
-
- /* structure definitions */
- struct tokstruct /* token structure */
- {
- char *tokstring; /* token */
- int defindex; /* index into prtcap table */
- };
-
- /* initialize table of token structures */
- struct tokstruct token[] =
- {
- { "NUM_LINES", NUM_LINES },
- { "NUM_COLS", NUM_COLS },
- { "BS", BS },
- { "HT", HT },
- { "FF", FORMFEED },
- { "CR", CR },
- { "SP", SP },
- { "LF", LINEFEED },
- { "ITALIC_ON", ITALIC_ON },
- { "ITALIC_OFF", ITALIC_OFF },
- { "BOLD_ON", BOLD_ON },
- { "BOLD_OFF", BOLD_OFF },
- { "UNDER_ON", UNDER_ON },
- { "UNDER_OFF", UNDER_OFF },
- { "LQ_ON", LQ_ON },
- { "LQ_OFF", LQ_OFF },
- { "PS_ON", PS_ON },
- { "PS_OFF", PS_OFF },
- { "SUBSCRIPT_ON", SUBSCRIPT_ON },
- { "SUBSCRIPT_OFF", SUBSCRIPT_OFF },
- { "SUPERSCRIPT_ON", SUPERSCRIPT_ON },
- { "SUPERSCRIPT_OFF", SUPERSCRIPT_OFF },
- { "DBL_WIDE_ON", DBL_WIDE_ON },
- { "DBL_WIDE_OFF", DBL_WIDE_OFF },
- { "DBL_HIGH_ON", DBL_HIGH_ON },
- { "DBL_HIGH_OFF", DBL_HIGH_OFF },
- { "CONDENSED_ON", CONDENSED_ON },
- { "CONDENSED_OFF", CONDENSED_OFF },
- { "BOX_ON", BOX_ON },
- { "BOX_OFF", BOX_OFF },
- { "SOLID_BLOCK", SOLID_BLOCK },
- { "SHADE_BLOCK", SHADE_BLOCK },
- { "BOT_BLOCK", BOT_BLOCK },
- { "TOP_BLOCK", TOP_BLOCK },
- { "BOX1_VERT", BOX1_VERT },
- { "BOX1_HORIZ", BOX1_HORIZ },
- { "BOX1_TOP_LFT", BOX1_TOP_LFT },
- { "BOX1_TOP_RT", BOX1_TOP_RT },
- { "BOX1_BOT_LFT", BOX1_BOT_LFT },
- { "BOX1_BOT_RT", BOX1_BOT_RT },
- { "BOX1_TOP_TEE", BOX1_TOP_TEE },
- { "BOX1_BOT_TEE", BOX1_BOT_TEE },
- { "BOX1_LFT_TEE", BOX1_LFT_TEE },
- { "BOX1_RT_TEE", BOX1_RT_TEE },
- { "BOX1_CROSS", BOX1_CROSS },
- { "BOX2_VERT", BOX2_VERT },
- { "BOX2_HORIZ", BOX2_HORIZ },
- { "BOX2_TOP_LFT", BOX2_TOP_LFT },
- { "BOX2_TOP_RT", BOX2_TOP_RT },
- { "BOX2_BOT_LFT", BOX2_BOT_LFT },
- { "BOX2_BOT_RT", BOX2_BOT_RT },
- { "BOX2_TOP_TEE", BOX2_TOP_TEE },
- { "BOX2_BOT_TEE", BOX2_BOT_TEE },
- { "BOX2_LFT_TEE", BOX2_LFT_TEE },
- { "BOX2_RT_TEE", BOX2_RT_TEE },
- { "BOX2_CROSS", BOX2_CROSS },
- { "BOX3_VERT", BOX3_VERT },
- { "BOX3_HORIZ", BOX3_HORIZ },
- { "BOX3_TOP_LFT", BOX3_TOP_LFT },
- { "BOX3_TOP_RT", BOX3_TOP_RT },
- { "BOX3_BOT_LFT", BOX3_BOT_LFT },
- { "BOX3_BOT_RT", BOX3_BOT_RT },
- { "BOX3_TOP_TEE", BOX3_TOP_TEE },
- { "BOX3_BOT_TEE", BOX3_BOT_TEE },
- { "BOX3_LFT_TEE", BOX3_LFT_TEE },
- { "BOX3_RT_TEE", BOX3_RT_TEE },
- { "BOX3_CROSS", BOX3_CROSS },
- { "BOX4_VERT", BOX4_VERT },
- { "BOX4_HORIZ", BOX4_HORIZ },
- { "BOX4_TOP_LFT", BOX4_TOP_LFT },
- { "BOX4_TOP_RT", BOX4_TOP_RT },
- { "BOX4_BOT_LFT", BOX4_BOT_LFT },
- { "BOX4_BOT_RT", BOX4_BOT_RT },
- { "BOX4_TOP_TEE", BOX4_TOP_TEE },
- { "BOX4_BOT_TEE", BOX4_BOT_TEE },
- { "BOX4_LFT_TEE", BOX4_LFT_TEE },
- { "BOX4_RT_TEE", BOX4_RT_TEE },
- { "BOX4_CROSS", BOX4_CROSS },
- { "LPI1", LPI1 },
- { "LPI2", LPI2 },
- { "LPI3", LPI3 },
- { "LPI4", LPI4 },
- { "LPI6", LPI6 },
- { "LPI8", LPI8 },
- { "LPI12", LPI12 },
- { "CPI5", CPI5 },
- { "CPI10", CPI10 },
- { "CPI12", CPI12 },
- { "CPI16", CPI16 },
- { "POSTSCRIPT", POSTSCRIPT },
- { "ATTR_REFRESH", ATTR_REFRESH },
- { NULL, LAST }
- };
-
- /*
- * Get a prtcap entry from the file
- */
- void get_print(prt_name)
- char *prt_name;
- {
- char lbuf[256];
- int i;
- char *p;
- int defindex;
-
- #ifdef PDEBUG
- int dbi;
- FILE *stdbug;
-
- stdbug = fopen("get_print.dbg", "w");
- #endif
-
- /*
- * In case it's not the first printer used empty the printer definition
- */
- for(i = 0; i < LAST; i ++)
- {
- if (prtdef[i] != NULL)
- {
- free(prtdef[i]);
- prtdef[i] = NULL;
- }
- if (sizdef[i] != 0)
- sizdef[i] = 0;
- }
-
- /*
- * establish defaults
- */
- prtdef[NUM_COLS] = alloc(sizeof(char));
- sizdef[NUM_COLS] = sizeof(char);
- *prtdef[NUM_COLS] = 80;
-
- prtdef[NUM_LINES] = alloc(sizeof(char));
- sizdef[NUM_LINES] = sizeof(char);
- *prtdef[NUM_LINES] = 66;
-
- prtdef[CR] = alloc(sizeof(char));
- sizdef[CR] = sizeof(char);
- *prtdef[CR] = '\r';
-
- prtdef[LINEFEED] = alloc(sizeof(char));
- sizdef[LINEFEED] = sizeof(char);
- *prtdef[LINEFEED] = '\n';
-
- if (strcmp(prt_name, "") == 0)
- return;
- /* open the file */
- if (-1 == open_prtcap(prt_name))
- abort_mess("Unable to get member %s from prtcap\n", prt_name);
-
- for(;;)
- { /* get the next prtcap line */
- if (next_prtline(lbuf) == 0 && feof(prtfp))
- abort_mess("Unexpected EOF\n");
- /* is it the END? */
- if (strncmp(&lbuf[strspn(lbuf, "\t ")], "END", 3) == 0)
- break;
- /* is it a new BEGIN? */
- if (strncmp(&lbuf[strspn(lbuf, "\t ")], "BEGIN(", 6) == 0)
- {
- fprintf(stderr,
- "WARNING: BEGIN without END. Assuming entry is complete.\n");
- break;
- }
-
- p = strtok(lbuf, "\t \n"); /* find token in buffer */
- if (p == NULL) /* NULL means blank line */
- {
- #ifdef PDEBUG
- fprintf(stderr, "DEBUG: warning - Skipping blank line.\n");
- #endif
- continue;
- }
- for(i = 0; token[i].tokstring != NULL; i++) /* look thru token list */
- {
- if (strcmp(p, token[i].tokstring) == 0)
- break;
- }
-
- if (token[i].tokstring == NULL) /* unrecognized token or comment */
- {
- if (*p == '#')
- {
- #ifdef PDEBUG
- fprintf(stderr, "DEBUG: warning - Skipping comment.\n");
- #endif
- }
- else
- fprintf(stderr, "WARNING: Skipping unrecognized token (%s).\n", p);
- }
- else
- {
- p = strtok(NULL, "\t \n#"); /* get the data */
-
- defindex = token[i].defindex; /* get index into prtdef array */
-
- #ifdef PDEBUG
- if (prtdef[defindex] != NULL)
- fprintf(stderr, "WARNING: Redefining %s\n", token[i].tokstring);
- #endif
-
- switch (defindex)
- {
- case NUM_LINES : /* integer members */
- case NUM_COLS :
- if (prtdef[defindex] != NULL) /* free if already defined */
- free(prtdef[defindex]);
- if (p == NULL) /* undef table entry */
- {
- prtdef[defindex] = p;
- sizdef[defindex] = 0;
- }
- else
- { /* def table entry */
- prtdef[defindex] = alloc(sizeof(char));
- *prtdef[defindex] = (char)atoi(p);
- sizdef[defindex] = sizeof(char);
- }
- #ifdef PDEBUG
- fputc(*prtdef[defindex], stdbug);
- fputc(0xff, stdbug);
- #endif
- break;
- case POSTSCRIPT :
- case ATTR_REFRESH :
- if (prtdef[defindex] != NULL)
- free(prtdef[defindex]);
- prtdef[defindex] = alloc(1);
- *prtdef[defindex] = (char) 1;
- sizdef[defindex] = 1;
- break;
- default: /* string members */
- if (prtdef[defindex] != NULL) /* free if already def'ed */
- free(prtdef[defindex]);
- if (p == NULL) /* undef table entry */
- prtdef[defindex] = p;
- else /* def table entry */
- {
- prtdef[defindex] = getdef(p, &sizdef[defindex]);
- if (prtdef[defindex] == NULL)
- abort_mess("Unable to allocate buffer for %s.\n",
- token[i].tokstring);
- }
- #ifdef PDEBUG
- if (prtdef[defindex] != NULL)
- for(dbi = 0; dbi < sizdef[defindex]; dbi++)
- fputc(prtdef[defindex][dbi], stdbug);
- fputc(0xff, stdbug);
- #endif
- break;
- }
- }
- }
-
- close_prtcap(); /* close prtcap file */
- #ifdef PDEBUG
- fclose(stdbug);
- #endif
- }
-
- /*
- * open prtcap and seek to beginning of entry
- */
- static int open_prtcap(prt_name)
- char *prt_name;
- {
- char lbuf[256];
- char searchstr[90];
-
- search_path("prtcap", lbuf, "");
-
- prtfp = fopen(lbuf, "r"); /* open the file */
- if (prtfp == (FILE *)0)
- return(-1); /* couldn't open it */
-
- /* here's what we're lookin' for */
- sprintf(searchstr, "BEGIN(%s)", prt_name);
-
- for(;;)
- {
- if (next_prtline(lbuf) == 0 && feof(prtfp))
- break; /* break out if this is EOF */
- if (strncmp(lbuf, searchstr, strlen(searchstr)) == 0)
- break; /* break out if this is it!!! */
- } /* "...and I still haven't found what I'm lookin' for." - U2 */
-
- if (feof(prtfp) && strlen(lbuf) == 0)
- return(-1); /* EOF */
- else
- return(0); /* BEGIN(...) */
- }
-
- /*
- * close the file
- */
- static void close_prtcap()
- {
- fclose(prtfp);
- }
-
- /*
- * get the next line from the prtcap file
- */
- static int next_prtline(lbuf)
- char *lbuf;
- {
- int c;
- char *p;
-
- for(p = lbuf;;p++)
- {
- c = fgetc(prtfp); /* get next character */
- if (c == '\n' || c == EOF) /* EOL or EOF ?? */
- {
- *p = '\0';
- return(strlen(lbuf));
- }
- *p = (char)c; /* put character into buffer */
- }
- }
-
- /*
- * translate a prtcap entry
- * \nnn = octal number
- * \E = escape
- * \S = space
- * \n = newline
- * \r = carriage return
- * \t = tab
- * \b = backspace
- * \f = formfeed
- */
- static char *getdef(defptr, defsiz)
- char *defptr;
- int *defsiz;
- {
- char dbuf[128];
- char *p1;
- char *p2;
- int n;
- int i;
- char *retptr;
- int size = 0;
-
- memset(p2 = dbuf, '\0', 128); /* zero out the buffer */
-
- for(p1 = defptr; *p1 != '\0'; p1++)
- {
- if (*p1 == '\\') /* escaped char(s) */
- {
- p1++;
- if (isoctal(*p1)) /* octal conversion */
- {
- n = 0; /* up to 3 digit octal number */
- for(i = 0; (isoctal(*p1) && i < 3); i++)
- {
- n <<= 3;
- n += (*p1 - '0');
- p1++;
- }
- *p2++ = (char)n;
- size++;
- p1--;
- }
- else /* meta character */
- {
- switch(*p1)
- {
- case 'E': /* escape */
- *p2++ = 0x1b;
- break;
- case 'S': /* space */
- *p2++ = ' ';
- break;
- case 'n': /* newline */
- *p2++ = '\n';
- break;
- case 'r': /* carriage return */
- *p2++ = '\r';
- break;
- case 't': /* tab */
- *p2++ = '\t';
- break;
- case 'b': /* backspace */
- *p2++ = '\b';
- break;
- case 'f': /* formfeed */
- *p2++ = '\f';
- break;
- default: /* other character (no translation) */
- *p2++ = *p1;
- break;
- }
- size++;
- }
- }
- else
- {
- *p2++ = *p1; /* standard character (no translation) */
- size++;
- }
- }
- retptr = alloc(size); /* allocate buffer */
- *defsiz = size; /* store size of buffer */
- if (retptr != NULL)
- memcpy(retptr, dbuf, size);
- return(retptr);
- }
-
-
-
- /**********************************************************************/
- /* GET_PRINTERS - get a list of all the available printers */
- /**********************************************************************/
-
- # define MAX_PRINTERS 64
-
- int nprts = 0;
- char *pnames[MAX_PRINTERS];
-
- # ifdef ANSI
- int get_printers(void);
- # else
- int get_printers();
- # endif
-
- int get_printers()
- {
- char lbuf[256];
- char searchstr[25];
- int length = 0;
-
- if( nprts )
- return(0); /* we already read in the list */
-
- search_path("prtcap", lbuf, "");
- prtfp = fopen(lbuf, "r"); /* open the file */
- if (prtfp == (FILE *)0)
- return(-1); /* couldn't open it */
-
- strcpy(searchstr, "BEGIN("); /* here's what we're lookin' for */
-
- while (TRUE)
- {
- if (next_prtline(lbuf) == 0 && feof(prtfp))
- break; /* break out if this is EOF */
-
- if (strncmp(lbuf, searchstr, strlen(searchstr)) == 0)
- {
- length = strlen( lbuf + strlen( searchstr ) );
- /*
- * allocate space for another printer name
- */
- pnames[ nprts ] = alloc( length + 1 );
-
- if ( pnames[ nprts ] == NULL )
- {
- errmsg("Not enough memory for printer selection.");
- break;
- }
- strncpy( pnames[ nprts ],
- lbuf + strlen( searchstr ), length - 1);
- pnames[ nprts ][ length - 1 ] = '\0';
-
- nprts++;
-
- if( nprts > MAX_PRINTERS )
- break;
- }
- }
- fclose (prtfp);
-
- return(0);
- }
-
- /*
- void main( void );
-
- void main()
- {
- int i;
- get_printers();
- i = rpt_selectkey( pnames, nprts , " Printer Selection ") - 1;
- printf("we selected printer %d '%s' ",i,pnames[i]);
- }
- */
-