home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume27 / psf3 / part06 / table.c < prev   
Encoding:
C/C++ Source or Header  |  1992-01-20  |  9.4 KB  |  472 lines

  1. /* ta=4 */
  2. /************************************************************************
  3. *                    t a b l e . c                                        *
  4. *                                                                        *
  5. *    Generate the *.def tables required by psf to describe a printer        *
  6. *                                                                        *
  7. *    Scans the Adobe *.ppd files for specific parametric lines.            *
  8. *    Tony FIeld,   tony@ajfcal.cuc.ab.ca                                `    *
  9. ************************************************************************/
  10. /*
  11.  * $Id: table.c,v 3.2 1992/01/19 05:50:33 ajf Exp ajf $
  12.  *
  13. */
  14.  
  15. /*    Usage:    table files.ppd ...  destination.dir
  16.  
  17.         where:    files.ppd are the various .ppd files to be converted.
  18.                 destination.dir is the destination directory in which
  19.                     to place the generated .def file(s)
  20.         
  21.     The layout of the generated .def files is:
  22.  
  23. *printer                                <-- marker
  24. NEC LC-890 v47.0 (modified)             <-- printer name for selectdef
  25.                                             (from *Nickname)
  26. Letter                                  <-- user name (e.g.   psf -g Letter)
  27.                                             (from *PageSize)
  28. statusdict begin lettertray end         <-- actual postscript to select size
  29.                                             (from *PageSize)
  30.   612   792    18    15   599   776     <-- page width page height LLx LLy URx URy
  31.                                               (page width/height from *PaperDimension)
  32.                                             (LLx LLy URx URy from *ImageableArea)
  33. Legal
  34. statusdict begin legaltray end
  35.   612  1008    18    19   593   990
  36. A4
  37. statusdict begin a4tray end
  38.   595   842    10     8   585   813
  39. B5
  40. statusdict begin b5tray end
  41.   516   729    18     7   482   702
  42. *order                                    <-- marker for output order
  43.                                             from DefaultOutputOrder or
  44.                                             OutputOrder
  45. Normal                                    <-- default output order
  46. statusdict begin 0 setoutputtray end    <-- set to normal
  47. statusdict begin 1 setoutputtray end    <-- set to reverse
  48. *fonts                                  <-- marker for fonts
  49. AvantGarde-Book                         <-- real font names in font dictionary
  50. AvantGarde-Demi                                (from *Font)
  51. Bookman-Demi
  52. Bookman-Light
  53. Courier
  54. Helvetica
  55. Helvetica-Narrow
  56. NewCenturySchlbk-Roman
  57. Palatino-Roman
  58. Symbol
  59. Times-Roman
  60. ZapfChancery-MediumItalic
  61. ZapfDingbats
  62. *slots                                    <-- marker for paper tray select
  63. statusdict begin 1 setpapertray end     <-- select first tray
  64. statusdict begin 2 setpapertray end     <-- select second tray  etc..
  65.                                             (from *InputSlot)
  66. *eof                                    <-- logical end of printer definition
  67. */
  68.  
  69. #include <stdio.h>
  70. #include <string.h>
  71. #include <sys/types.h>
  72. #include <sys/stat.h>
  73. #include <ctype.h>
  74.  
  75. char    nickname[200];
  76. struct pdef
  77. {
  78.     char    name[500];
  79.     char    set[500];
  80.     char    tray[500];
  81.     int        lx, ly, ux, uy, sx, sy;
  82. } ;
  83.  
  84. struct outorder
  85. {    char    bydefault[100];
  86.     char    normal[150];
  87.     char    reverse[150];
  88. } order;
  89.  
  90. char    *malloc();
  91. char    pname[100];
  92. struct pdef printer[40], pblank;
  93. int        nprinter = 0;
  94. char    *fonts[100];
  95. char    *slots[10];
  96. int        nfonts=0;
  97. int        nslots=0;
  98. void    trim();
  99.  
  100. main (argc,argv)
  101. int     argc;
  102. char    *argv[];
  103. {
  104.     char    *fname;
  105.     FILE    *fp_in, *fp_out;
  106.     int        i, j;
  107.     char    *c;
  108.     char    outname[100], fnonly[50];
  109.     char    *destdir;
  110.     
  111.     if (argc < 3)
  112.         usage ();
  113.  
  114.     argc--;
  115.     destdir = argv[argc];
  116.     if (isdir (destdir) != 1)
  117.         usage ();
  118.  
  119.     for (i = 1;  i < argc;  i++)
  120.     {    if ((fp_in = fopen (argv[i], "r")) != NULL)
  121.         {
  122.             for (j = 0;  j < nprinter;  j++)
  123.                 printer[j] = pblank;
  124.             
  125.             for (j = 0;  j < nfonts;  j++)
  126.                 free (fonts[j]);
  127.             
  128.             for (j = 0;  j < nslots;  j++)
  129.                 free (slots[j]);
  130.             
  131.             nfonts = nslots = nprinter = 0;
  132.  
  133.             fnameonly (argv[i], fnonly);
  134.             if ((c = strchr(fnonly, '.')))
  135.                 *c = '\0';
  136.             strcpy (outname,destdir);
  137.             strcat (outname, "/");
  138.             strcat (outname, fnonly);
  139.             strcat (outname, ".def");
  140.  
  141.             if ((fp_out = fopen (outname, "w")) == NULL)
  142.             {    fprintf (stderr, "%s: cannot create file %s\n", argv[0], outname);
  143.                 exit (1);
  144.             }
  145.             build (fp_in);
  146.             display (fp_out);
  147.             fclose (fp_out);
  148.  
  149.             fclose (fp_in);
  150.         }
  151.     }
  152.         
  153. }
  154.  
  155. build (fp_in)
  156. FILE    *fp_in;
  157. {
  158.     char    s[1000];
  159.     int        n;
  160.     int        a, b, c, d;
  161.     char    which[500];
  162.  
  163.     order.bydefault[0] = 0;
  164.     order.normal[0] = 0;
  165.     order.reverse[0] = 0;
  166.     
  167.     while (fgets (s, 999, fp_in) != NULL)
  168.     {    trim (s);
  169.     
  170.         if (strncmp (s, "*PageSize", 9) == 0)
  171.         {
  172.             n = ptype (s, which);
  173.             gettext (s, printer[n].set);
  174.         }
  175.         else if (strncmp (s, "*NickName:", 10) == 0)
  176.         {
  177.             gettext (s, nickname);
  178.         }
  179.         else if (strncmp (s, "*Font ", 6) == 0)
  180.         {
  181.             ftype (s);
  182.         }
  183.         else if (strncmp (s, "*DefaultOutputOrder:", 20) == 0)
  184.         {    strcpy (order.bydefault, s + 21);
  185.         }
  186.         else if (strncmp (s, "*OutputOrder Normal:", 20) == 0)
  187.         {
  188.             gettext (s, order.normal);
  189.         }
  190.         else if (strncmp (s, "*OutputOrder Reverse:", 21) == 0)
  191.         {
  192.             gettext (s, order.reverse);
  193.         }
  194.         else if (strncmp (s, "*PaperTray", 10) == 0)
  195.         {
  196.             n = ptype (s, which);
  197.             gettext (s, printer[n].tray);
  198.         }
  199.         else if (strncmp (s, "*InputSlot", 10) == 0)
  200.         {
  201.             getmultitext (fp_in, s, which);
  202.             if (*which)
  203.             {    slots[nslots] = malloc (strlen(which) + 2);
  204.                 strcpy (slots[nslots], which);
  205.                 nslots++;
  206.             }
  207.         }
  208.         else if (strncmp (s, "*ImageableArea", 14) == 0)
  209.         {
  210.             n = ptype (s, which);
  211.             sscanf (strchr (s, '"') + 1, "%d%d%d%d", &a, &b, &c, &d);
  212.             printer[n].lx = a;
  213.             printer[n].ly = b;
  214.             printer[n].ux = c;
  215.             printer[n].uy = d;
  216.         }
  217.         else if (strncmp (s, "*PaperDimension", 15) == 0)
  218.         {
  219.             n = ptype (s, which);
  220.             sscanf (strchr (s, '"') + 1, "%d %d", &a, &b);
  221.             printer[n].sx = a;
  222.             printer[n].sy = b;
  223.         }
  224.     }
  225. }
  226.  
  227.  
  228. ptype (s, which)
  229. char    *s, *which;
  230. {
  231.     char    *w, *ww;
  232.     int        i;
  233.  
  234.     w = s;
  235.     while (*w++ != ' ')
  236.         ;
  237.     ww = which;
  238.     while (*w != ':')
  239.     {
  240.         if (*w == '/')
  241.         {
  242.  
  243.             while (*w++ != ':')
  244.                 ;
  245.             break;
  246. /*
  247.             ww = which;
  248.             w++;
  249.             continue;
  250. */
  251.         }
  252.         if (*w == ' ')
  253.             *w = '-';
  254.         *ww++ = *w++;
  255.     }
  256.     *ww = '\0';
  257.     while (*w == ' ')
  258.         w++;
  259. /*
  260.     sscanf (s, "%*s%s", which);
  261. */
  262. /*
  263.     w = which;
  264.     while (*w)
  265.     {    if (*w == ':')
  266.         {    *w = '\0';
  267.             break;
  268.         }
  269.         w++;
  270.     }
  271. */
  272.     for (i = 0;  i < nprinter;  i++)
  273.     {    if (strcmp (which, printer[i].name) == 0)
  274.             return (i);
  275.     }
  276.     strcpy (printer[nprinter].name, which);
  277.     nprinter++;
  278.     return (nprinter - 1);
  279. }
  280.  
  281. ftype (s)
  282. char    *s;
  283. {
  284.     char    *w, *ww;
  285.     char    which[100];
  286.     int        i;
  287.  
  288.     w = s;
  289.     while (*w++ != ' ')
  290.         ;
  291.     ww = which;
  292.     while (*w != ':')
  293.     {
  294.         if (*w == ' ')
  295.             *w = '-';
  296.         *ww++ = *w++;
  297.     }
  298.     *ww = '\0';
  299.     while (*w == ' ')
  300.         w++;
  301.  
  302.     if (strncmp (which, "Za", 2))
  303.     {    if (tscan (which, "Bold") >= 0   ||  tscan (which,"Italic") >= 0 
  304.             || tscan (which,"Obliq") >= 0)
  305.             return;
  306.     }
  307.     
  308.     fonts[nfonts] = malloc (strlen (which) + 1);
  309.     strcpy (fonts[nfonts++], which);
  310.     
  311. }
  312.  
  313.  
  314. display (fp_out)
  315. FILE    *fp_out;
  316. {
  317.     int        i;
  318.     
  319.     fprintf (fp_out, "*printer\n");
  320.     fprintf (fp_out, "%s\n", nickname);
  321.     fprintf (fp_out, "*paper %d\n", nprinter);
  322.     for (i = 0;  i < nprinter;  i++)
  323.     {
  324.         fprintf (fp_out, "%s\n%s\n", printer[i].name, printer[i].set);
  325.         fprintf (fp_out, "%5d %5d %5d %5d %5d %5d\n",
  326.             printer[i].sx, printer[i].sy,
  327.             printer[i].lx, printer[i].ly, printer[i].ux, printer[i].uy);
  328.  
  329. /*        print the actual size of the image area 
  330.  
  331.         printf ("%-20s %-20s %4d %4d\n", pname, printer[i].name,
  332.             printer[i].ux - printer[i].lx + 1,
  333.             printer[i].uy - printer[i].ly + 1);
  334. */
  335.     }
  336.  
  337.     
  338.     if (order.normal[0]  ||  order.reverse[0])
  339.         fprintf (fp_out, "*order 3\n");
  340.     else
  341.         fprintf (fp_out, "*order 1\n");
  342.     fprintf (fp_out, "%s\n", order.bydefault);
  343.     if (order.normal[0]  ||  order.reverse[0])
  344.     {    fprintf (fp_out, "%s\n", order.normal);
  345.         fprintf (fp_out, "%s\n", order.reverse);
  346.     }
  347.     fprintf (fp_out, "*fonts %d\n", nfonts);
  348.     for (i = 0;  i < nfonts;  i++)
  349.         fprintf (fp_out, "%s\n", fonts[i]);
  350.  
  351.     fprintf (fp_out, "*slots %d\n", nslots);
  352.     for (i = 0;  i < nslots;  i++)
  353.         fprintf (fp_out, "%s\n", slots[i]);
  354.     fprintf (fp_out, "*eof\n");
  355. }
  356.  
  357. gettext (s, t)
  358. char    *s, *t;
  359. {
  360.     strcpy (t, strchr (s, '"') + 1);
  361.     while (*t)
  362.     {    if (*t == '"')
  363.         {    *t = '\0';
  364.             break;
  365.         }
  366.         t++;
  367.     }
  368. }
  369.  
  370. getmultitext (fp, s, t)
  371. FILE    *fp;
  372. char    *s, *t;
  373. {    int    eos;
  374.     char    xlin[500], *c;
  375.  
  376.     eos = 0;
  377.     c = strchr (s, '"');
  378.     if (c == NULL)
  379.     {    *t = 0;
  380.         return;
  381.     }
  382.     strcpy (t, c + 1);
  383.     while (eos == 0)
  384.     {    while (*t)
  385.         {    if (*t == '"')
  386.             {    *t = '\0';
  387.                 eos = 1;
  388.                 break;
  389.             }
  390.             t++;
  391.         }
  392.         if (eos == 0)
  393.         {    fgets (xlin, 499, fp);
  394.             trim (xlin);
  395.             c = xlin;
  396.             while (isspace (*c))
  397.                 c++;
  398.             if (strlen (t))
  399.                 strcat (t, " ");
  400.             strcat (t, c);
  401.         }
  402.     }
  403. }
  404.  
  405. tscan (s, t)
  406. char     s[], t[];
  407. {
  408.     int    i, j, k;
  409.     for (i = 0;  s[i] != '\0';  i++)
  410.     {    for (j = i, k=0;  t[k] != '\0'  &&  s[j] == t[k];  j++, k++)
  411.             ;
  412.         if (t[k] == '\0')
  413.             return (i);
  414.     }
  415.     return (-1);
  416. }
  417.  
  418. void trim (s)                    /*    trim trailing blanks  and \n */
  419. char    *s;
  420. {    int many;
  421.  
  422.     for (many = strlen (s) - 1;  many >= 0;  many--)
  423.     {    if (isgraph (s[many]))
  424.             break;
  425.         else
  426.             s[many] = '\0';
  427.     }
  428. }
  429.  
  430.  
  431.  
  432. fnameonly (path, fn)
  433. char    *path, *fn;
  434. {
  435.     char    *strrchr(), *where;
  436.     
  437.     if ((where = strrchr (path, '/')) == NULL)
  438.         strcpy (fn, path);
  439.     else
  440.         strcpy (fn, where + 1);
  441.     return;
  442. }
  443.  
  444.  
  445. /****************************************************************************
  446. *    isdir (name)                                                            *
  447. *    See if the 'name' is a directory entry.  If so, return 1.  If not,        *
  448. *    return a 0.                                                                *
  449. ****************************************************************************/
  450.  
  451. isdir (name)
  452. char    *name;
  453. {
  454.     struct stat buf;
  455.  
  456.     if (stat (name, &buf) != 0)                /* ensure stat() is happy        */
  457.         return (-1);
  458.     if ((buf.st_mode & S_IFMT) == S_IFDIR)     /* good call, see if             */
  459.         return (1);                            /*        is directory            */
  460.     if ((buf.st_mode & S_IFMT) == S_IFREG)     /* good call, see if            */
  461.         return (0);                            /*        is file                    */
  462.     return (-1);                            /* something else                */
  463. }
  464.  
  465. usage ()
  466. {    fprintf (stderr, "Usage: table f.ppd f.ppd ... /dest.dir\n");
  467.     fprintf (stderr, "   where   f.ppd    = source .ppd files\n");
  468.     fprintf (stderr, "           dest.dir = desination directory for .def files\n");
  469.     exit (1);
  470. }            
  471.     
  472.