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

  1. /***( getprint.c )**************************************************************
  2. *                                                                              *
  3. *  Written:  Brent Faulkner - May 25, 1989                                     *
  4. *  Updated:  Brent Faulkner - June 13, 1989                                    *
  5. *                                                                              *
  6. ********************************************************************************
  7. *                                                                              *
  8. * Contents:    get_print() - get a prtcap entry                                *
  9. *                                                                              *
  10. *            open_prtcap() - open prtcap and find printer                      *
  11. *           close_prtcap() - close it                                          *
  12. *           next_prtline() - get next line from prtcap                         *
  13. *                 getdef() - translate a prtcap line                           *
  14. *                                                                              *
  15. *******************************************************************************/
  16. /* include files */
  17. #include <stdio.h>
  18. #include <bench.h>
  19. #include "prt.h"
  20.  
  21. /* define macros */
  22. #define isoctal(dig)    ((dig) >= '0' && (dig) <= '7')
  23.  
  24. /* global variables */
  25. char *prtdef[LAST] = { 0 };               /* table to hold prtcap entries */
  26. int sizdef[LAST] = { 0 };                 /* table of sizes of corresponding entries */
  27. FILE *prtfp = 0;                      /* stream for prtcap file */
  28.  
  29. /* function prototypes/declarations */
  30. #ifdef ANSI
  31. static void close_prtcap(void);
  32. static char *getdef(char *, int *);
  33. static int next_prtline(char*);
  34. static int open_prtcap(char *);
  35. #else
  36. static void close_prtcap();
  37. static char *getdef();
  38. static int next_prtline();
  39. static int open_prtcap();
  40. #endif
  41.  
  42. /* structure definitions */
  43. struct tokstruct                  /* token structure */
  44. {
  45.     char *tokstring;              /* token */
  46.     int    defindex;                 /* index into prtcap table */
  47. };
  48.  
  49. /* initialize table of token structures */
  50. struct tokstruct token[] =
  51. {
  52.     { "NUM_LINES", NUM_LINES },
  53.     { "NUM_COLS", NUM_COLS },
  54.     { "BS", BS },
  55.     { "HT", HT },
  56.     { "FF", FORMFEED },
  57.     { "CR", CR },
  58.     { "SP", SP },
  59.     { "LF", LINEFEED },
  60.     { "ITALIC_ON", ITALIC_ON },
  61.     { "ITALIC_OFF", ITALIC_OFF },
  62.     { "BOLD_ON", BOLD_ON },
  63.     { "BOLD_OFF", BOLD_OFF },
  64.     { "UNDER_ON", UNDER_ON },
  65.     { "UNDER_OFF", UNDER_OFF },
  66.     { "LQ_ON", LQ_ON },
  67.     { "LQ_OFF", LQ_OFF },
  68.     { "PS_ON", PS_ON },
  69.     { "PS_OFF", PS_OFF },
  70.     { "SUBSCRIPT_ON", SUBSCRIPT_ON },
  71.     { "SUBSCRIPT_OFF", SUBSCRIPT_OFF },
  72.     { "SUPERSCRIPT_ON", SUPERSCRIPT_ON },
  73.     { "SUPERSCRIPT_OFF", SUPERSCRIPT_OFF },
  74.     { "DBL_WIDE_ON", DBL_WIDE_ON },
  75.     { "DBL_WIDE_OFF", DBL_WIDE_OFF },
  76.     { "DBL_HIGH_ON", DBL_HIGH_ON },
  77.     { "DBL_HIGH_OFF", DBL_HIGH_OFF },
  78.     { "CONDENSED_ON", CONDENSED_ON },
  79.     { "CONDENSED_OFF", CONDENSED_OFF },
  80.     { "BOX_ON", BOX_ON },
  81.     { "BOX_OFF", BOX_OFF },
  82.     { "SOLID_BLOCK", SOLID_BLOCK },
  83.     { "SHADE_BLOCK", SHADE_BLOCK },
  84.     { "BOT_BLOCK", BOT_BLOCK },
  85.     { "TOP_BLOCK", TOP_BLOCK },
  86.     { "BOX1_VERT", BOX1_VERT },
  87.     { "BOX1_HORIZ", BOX1_HORIZ },
  88.     { "BOX1_TOP_LFT", BOX1_TOP_LFT },
  89.     { "BOX1_TOP_RT", BOX1_TOP_RT },
  90.     { "BOX1_BOT_LFT", BOX1_BOT_LFT },
  91.     { "BOX1_BOT_RT", BOX1_BOT_RT },
  92.     { "BOX1_TOP_TEE", BOX1_TOP_TEE },
  93.     { "BOX1_BOT_TEE", BOX1_BOT_TEE },
  94.     { "BOX1_LFT_TEE", BOX1_LFT_TEE },
  95.     { "BOX1_RT_TEE", BOX1_RT_TEE },
  96.     { "BOX1_CROSS", BOX1_CROSS },
  97.     { "BOX2_VERT", BOX2_VERT },
  98.     { "BOX2_HORIZ", BOX2_HORIZ },
  99.     { "BOX2_TOP_LFT", BOX2_TOP_LFT },
  100.     { "BOX2_TOP_RT", BOX2_TOP_RT },
  101.     { "BOX2_BOT_LFT", BOX2_BOT_LFT },
  102.     { "BOX2_BOT_RT", BOX2_BOT_RT },
  103.     { "BOX2_TOP_TEE", BOX2_TOP_TEE },
  104.     { "BOX2_BOT_TEE", BOX2_BOT_TEE },
  105.     { "BOX2_LFT_TEE", BOX2_LFT_TEE },
  106.     { "BOX2_RT_TEE", BOX2_RT_TEE },
  107.     { "BOX2_CROSS", BOX2_CROSS },
  108.     { "BOX3_VERT", BOX3_VERT },
  109.     { "BOX3_HORIZ", BOX3_HORIZ },
  110.     { "BOX3_TOP_LFT", BOX3_TOP_LFT },
  111.     { "BOX3_TOP_RT", BOX3_TOP_RT },
  112.     { "BOX3_BOT_LFT", BOX3_BOT_LFT },
  113.     { "BOX3_BOT_RT", BOX3_BOT_RT },
  114.     { "BOX3_TOP_TEE", BOX3_TOP_TEE },
  115.     { "BOX3_BOT_TEE", BOX3_BOT_TEE },
  116.     { "BOX3_LFT_TEE", BOX3_LFT_TEE },
  117.     { "BOX3_RT_TEE", BOX3_RT_TEE },
  118.     { "BOX3_CROSS", BOX3_CROSS },
  119.     { "BOX4_VERT", BOX4_VERT },
  120.     { "BOX4_HORIZ", BOX4_HORIZ },
  121.     { "BOX4_TOP_LFT", BOX4_TOP_LFT },
  122.     { "BOX4_TOP_RT", BOX4_TOP_RT },
  123.     { "BOX4_BOT_LFT", BOX4_BOT_LFT },
  124.     { "BOX4_BOT_RT", BOX4_BOT_RT },
  125.     { "BOX4_TOP_TEE", BOX4_TOP_TEE },
  126.     { "BOX4_BOT_TEE", BOX4_BOT_TEE },
  127.     { "BOX4_LFT_TEE", BOX4_LFT_TEE },
  128.     { "BOX4_RT_TEE", BOX4_RT_TEE },
  129.     { "BOX4_CROSS", BOX4_CROSS },
  130.     { "LPI1", LPI1 },
  131.     { "LPI2", LPI2 },
  132.     { "LPI3", LPI3 },
  133.     { "LPI4", LPI4 },
  134.     { "LPI6", LPI6 },
  135.     { "LPI8", LPI8 },
  136.     { "LPI12", LPI12 },
  137.     { "CPI5", CPI5 },
  138.     { "CPI10", CPI10 },
  139.     { "CPI12", CPI12 },
  140.     { "CPI16", CPI16 },
  141.     { "POSTSCRIPT", POSTSCRIPT },
  142.     { "ATTR_REFRESH", ATTR_REFRESH },
  143.     { NULL, LAST }
  144.     };
  145.  
  146. /*
  147.  * Get a prtcap entry from the file
  148. */
  149. void get_print(prt_name)
  150. char *prt_name;
  151. {
  152.     char lbuf[256];
  153.     int i;
  154.     char *p;
  155.     int defindex;
  156.  
  157. #ifdef PDEBUG
  158.     int dbi;
  159.     FILE *stdbug;
  160.  
  161.     stdbug = fopen("get_print.dbg", "w");
  162. #endif
  163.  
  164.     /*
  165.      * In case it's not the first printer used empty the printer definition
  166.     */
  167.     for(i = 0; i < LAST; i ++)
  168.     {
  169.         if (prtdef[i] != NULL)
  170.         {
  171.             free(prtdef[i]);
  172.             prtdef[i] = NULL;
  173.         }
  174.         if (sizdef[i] != 0)
  175.             sizdef[i] = 0;
  176.     }
  177.  
  178.     /*
  179.      * establish defaults
  180.     */
  181.     prtdef[NUM_COLS] = alloc(sizeof(char));
  182.     sizdef[NUM_COLS] = sizeof(char);
  183.     *prtdef[NUM_COLS] = 80;
  184.  
  185.     prtdef[NUM_LINES] = alloc(sizeof(char));
  186.     sizdef[NUM_LINES] = sizeof(char);
  187.     *prtdef[NUM_LINES] = 66;
  188.  
  189.     prtdef[CR] = alloc(sizeof(char));
  190.     sizdef[CR] = sizeof(char);
  191.     *prtdef[CR] = '\r';
  192.  
  193.     prtdef[LINEFEED] = alloc(sizeof(char));
  194.     sizdef[LINEFEED] = sizeof(char);
  195.     *prtdef[LINEFEED] = '\n';
  196.  
  197.     if (strcmp(prt_name, "") == 0)
  198.         return;
  199.                                   /* open the file */
  200.     if (-1 == open_prtcap(prt_name))
  201.         abort_mess("Unable to get member %s from prtcap\n", prt_name);
  202.  
  203.     for(;;)
  204.     {                             /* get the next prtcap line */
  205.         if (next_prtline(lbuf) == 0 && feof(prtfp))
  206.             abort_mess("Unexpected EOF\n");
  207.                                   /* is it the END? */
  208.         if (strncmp(&lbuf[strspn(lbuf, "\t ")], "END", 3) == 0)
  209.             break;
  210.                                   /* is it a new BEGIN? */
  211.         if (strncmp(&lbuf[strspn(lbuf, "\t ")], "BEGIN(", 6) == 0)
  212.         {
  213.             fprintf(stderr,
  214.                 "WARNING: BEGIN without END.  Assuming entry is complete.\n");
  215.             break;
  216.         }
  217.         
  218.         p = strtok(lbuf, "\t \n");     /* find token in buffer */
  219.         if (p == NULL)                 /* NULL means blank line */
  220.         {
  221. #ifdef PDEBUG
  222.             fprintf(stderr, "DEBUG: warning - Skipping blank line.\n");
  223. #endif
  224.             continue;
  225.         }
  226.         for(i = 0; token[i].tokstring != NULL; i++)    /* look thru token list */
  227.         {
  228.             if (strcmp(p, token[i].tokstring) == 0)
  229.                 break;
  230.         }
  231.  
  232.         if (token[i].tokstring == NULL)    /* unrecognized token or comment */
  233.         {
  234.             if (*p == '#')
  235.             {
  236. #ifdef PDEBUG
  237.                 fprintf(stderr, "DEBUG: warning - Skipping comment.\n");
  238. #endif
  239.             }
  240.             else
  241.                 fprintf(stderr, "WARNING: Skipping unrecognized token (%s).\n", p);
  242.         }
  243.         else
  244.         {
  245.             p = strtok(NULL, "\t \n#");   /* get the data */
  246.  
  247.             defindex = token[i].defindex;   /* get index into prtdef array */
  248.  
  249. #ifdef PDEBUG
  250.             if (prtdef[defindex] != NULL)
  251.                 fprintf(stderr, "WARNING: Redefining %s\n",    token[i].tokstring);
  252. #endif
  253.  
  254.             switch (defindex)
  255.             {
  256.                 case NUM_LINES :                  /* integer members */
  257.                 case NUM_COLS :
  258.                     if (prtdef[defindex] != NULL) /* free if already defined */
  259.                         free(prtdef[defindex]);
  260.                     if (p == NULL)                /* undef table entry */
  261.                     {
  262.                         prtdef[defindex] = p;
  263.                         sizdef[defindex] = 0;
  264.                     }
  265.                     else
  266.                     {                             /* def table entry */
  267.                         prtdef[defindex] = alloc(sizeof(char));
  268.                         *prtdef[defindex] = (char)atoi(p);
  269.                         sizdef[defindex] = sizeof(char);
  270.                     }
  271. #ifdef PDEBUG
  272.                     fputc(*prtdef[defindex], stdbug);
  273.                     fputc(0xff, stdbug);
  274. #endif
  275.                     break;
  276.                 case POSTSCRIPT :
  277.                 case ATTR_REFRESH :
  278.                     if (prtdef[defindex] != NULL)
  279.                         free(prtdef[defindex]);
  280.                     prtdef[defindex] = alloc(1);
  281.                     *prtdef[defindex] = (char) 1;
  282.                     sizdef[defindex] = 1;
  283.                     break;
  284.                 default:                    /* string members */
  285.                     if (prtdef[defindex] != NULL)   /* free if already def'ed */
  286.                         free(prtdef[defindex]);
  287.                     if (p == NULL)                  /* undef table entry */
  288.                         prtdef[defindex] = p;
  289.                     else                            /* def table entry */
  290.                     {
  291.                         prtdef[defindex] = getdef(p, &sizdef[defindex]);
  292.                         if (prtdef[defindex] == NULL)
  293.                             abort_mess("Unable to allocate buffer for %s.\n",
  294.                                 token[i].tokstring);
  295.                     }
  296. #ifdef PDEBUG
  297.                     if (prtdef[defindex] != NULL)
  298.                     for(dbi = 0; dbi < sizdef[defindex]; dbi++)
  299.                         fputc(prtdef[defindex][dbi], stdbug);
  300.                     fputc(0xff, stdbug);
  301. #endif
  302.                     break;
  303.             }
  304.         }
  305.     }
  306.  
  307.     close_prtcap();                    /* close prtcap file */
  308. #ifdef PDEBUG
  309.     fclose(stdbug);
  310. #endif
  311. }
  312.  
  313. /*
  314.  * open prtcap and seek to beginning of entry
  315. */
  316. static int open_prtcap(prt_name)
  317. char *prt_name;
  318. {
  319.     char lbuf[256];
  320.     char searchstr[90];
  321.  
  322.     search_path("prtcap", lbuf, "");
  323.  
  324.     prtfp = fopen(lbuf, "r");      /* open the file */
  325.     if (prtfp == (FILE *)0)
  326.         return(-1);                    /* couldn't open it */
  327.  
  328.                                        /* here's what we're lookin' for */
  329.     sprintf(searchstr, "BEGIN(%s)", prt_name);
  330.  
  331.     for(;;)
  332.     {
  333.         if (next_prtline(lbuf) == 0 && feof(prtfp))    
  334.             break; /* break out if this is EOF */
  335.         if (strncmp(lbuf, searchstr, strlen(searchstr)) == 0)
  336.             break; /* break out if this is it!!! */
  337.     } /* "...and I still haven't found what I'm lookin' for." - U2 */
  338.  
  339.     if (feof(prtfp) && strlen(lbuf) == 0)
  340.         return(-1); /* EOF */
  341.     else
  342.         return(0);  /* BEGIN(...) */
  343. }
  344.  
  345. /*
  346.  * close the file
  347. */
  348. static void close_prtcap()
  349. {
  350.     fclose(prtfp);
  351. }
  352.  
  353. /*
  354.  * get the next line from the prtcap file
  355. */
  356. static int next_prtline(lbuf)
  357. char *lbuf;
  358. {
  359.     int c;
  360.     char *p;
  361.  
  362.     for(p = lbuf;;p++)
  363.     {
  364.         c = fgetc(prtfp);              /* get next character */
  365.         if (c == '\n' || c == EOF)     /* EOL or EOF ?? */
  366.         {
  367.             *p = '\0';
  368.             return(strlen(lbuf));
  369.         }
  370.         *p = (char)c;                        /* put character into buffer */
  371.     }
  372. }
  373.  
  374. /*
  375.  * translate a prtcap entry
  376.  *    \nnn  = octal number
  377.  *    \E    = escape
  378.  *    \S    = space
  379.  *    \n    = newline
  380.  *    \r    = carriage return
  381.  *    \t    = tab
  382.  *    \b    = backspace
  383.  *    \f    = formfeed
  384. */ 
  385. static char *getdef(defptr, defsiz)
  386. char *defptr;
  387. int *defsiz;
  388. {
  389.     char dbuf[128];
  390.     char *p1;
  391.     char *p2;
  392.     int n;
  393.     int i;
  394.     char *retptr;
  395.     int size = 0;
  396.  
  397.     memset(p2 = dbuf, '\0', 128);      /* zero out the buffer */
  398.  
  399.     for(p1 = defptr; *p1 != '\0'; p1++)
  400.     {
  401.         if (*p1 == '\\')               /* escaped char(s) */
  402.         {
  403.             p1++;
  404.             if (isoctal(*p1))          /* octal conversion */
  405.             {
  406.                 n = 0;                 /* up to 3 digit octal number */
  407.                 for(i = 0; (isoctal(*p1) && i < 3); i++)
  408.                 {
  409.                     n <<= 3;
  410.                     n += (*p1 - '0');
  411.                     p1++;
  412.                 }
  413.                 *p2++ = (char)n;
  414.                 size++;
  415.                 p1--;
  416.             }
  417.             else     /* meta character */
  418.             {
  419.                 switch(*p1)
  420.                 {
  421.                     case 'E':          /* escape */
  422.                         *p2++ = 0x1b;
  423.                         break;
  424.                     case 'S':          /* space */
  425.                         *p2++ = ' ';
  426.                         break;
  427.                     case 'n':          /* newline */
  428.                         *p2++ = '\n';
  429.                         break;
  430.                     case 'r':          /* carriage return */
  431.                         *p2++ = '\r';
  432.                         break;
  433.                     case 't':          /* tab */
  434.                         *p2++ = '\t';
  435.                         break;
  436.                     case 'b':          /* backspace */
  437.                         *p2++ = '\b';
  438.                         break;
  439.                     case 'f':          /* formfeed */
  440.                         *p2++ = '\f';
  441.                         break;
  442.                     default:           /* other character (no translation) */
  443.                         *p2++ = *p1;
  444.                         break;
  445.                 }
  446.                 size++;
  447.             }
  448.         }
  449.         else
  450.         {
  451.             *p2++ = *p1;               /* standard character (no translation) */
  452.             size++;
  453.         }
  454.     }
  455.     retptr = alloc(size);             /* allocate buffer */
  456.     *defsiz = size;                    /* store size of buffer */
  457.     if (retptr != NULL)
  458.         memcpy(retptr, dbuf, size);
  459.     return(retptr);
  460. }
  461.  
  462.  
  463.  
  464. /**********************************************************************/
  465. /* GET_PRINTERS - get a list of all the available printers            */ 
  466. /**********************************************************************/
  467.  
  468. # define MAX_PRINTERS 64
  469.  
  470. int nprts = 0;
  471. char *pnames[MAX_PRINTERS];
  472.  
  473. # ifdef ANSI
  474. int get_printers(void);
  475. # else
  476. int get_printers();
  477. # endif
  478.  
  479. int get_printers()
  480. {
  481.     char lbuf[256];
  482.     char searchstr[25];
  483.     int  length = 0;
  484.  
  485.     if( nprts )
  486.         return(0); /* we already read in the list */
  487.  
  488.     search_path("prtcap", lbuf, "");
  489.     prtfp = fopen(lbuf, "r");     /* open the file */
  490.     if (prtfp == (FILE *)0)
  491.         return(-1);                    /* couldn't open it */
  492.  
  493.     strcpy(searchstr, "BEGIN(");      /* here's what we're lookin' for */
  494.  
  495.     while (TRUE)
  496.     {
  497.         if (next_prtline(lbuf) == 0 && feof(prtfp))    
  498.             break; /* break out if this is EOF */
  499.  
  500.         if (strncmp(lbuf, searchstr, strlen(searchstr)) == 0)
  501.         {
  502.             length = strlen( lbuf + strlen( searchstr ) );
  503.             /* 
  504.             * allocate space for another printer name  
  505.             */
  506.             pnames[ nprts ] = alloc( length + 1 );
  507.  
  508.             if ( pnames[ nprts ] == NULL )
  509.             {
  510.                 errmsg("Not enough memory for printer selection.");
  511.                 break;
  512.             }
  513.             strncpy( pnames[ nprts ],
  514.                 lbuf + strlen( searchstr ), length - 1);
  515.             pnames[ nprts ][ length - 1 ] = '\0';
  516.  
  517.             nprts++;
  518.  
  519.             if( nprts > MAX_PRINTERS )
  520.                 break;
  521.         }
  522.     } 
  523.     fclose (prtfp);
  524.  
  525.     return(0);
  526. }
  527.  
  528. /*
  529. void main( void );
  530.  
  531. void main()
  532. {
  533.     int i;
  534.     get_printers();
  535.     i = rpt_selectkey( pnames, nprts , " Printer Selection ") - 1;
  536.     printf("we selected printer %d '%s' ",i,pnames[i]);
  537. }
  538. */
  539.