home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume12 / psf2 / part04 / table.c < prev   
Encoding:
C/C++ Source or Header  |  1990-05-19  |  8.0 KB  |  396 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. ************************************************************************/
  9.  
  10. /*    Usage:    table files.ppd ...  destination.dir
  11.  
  12.         where:    files.ppd are the various .ppd files to be converted.
  13.                 destination.dir is the destination directory in which
  14.                     to place the generated .def file(s)
  15.         
  16.     The layout of the generated .def files is:
  17.  
  18. NEC LC-890 v47.0 (modified)             <-- printer name for installpsf
  19.                                             (from *Nickname)
  20. Letter                                  <-- user name (e.g.   psf -g Letter)
  21.                                             (from *PageSize)
  22. statusdict begin lettertray end         <-- actual postscript to select size
  23.                                             (from *PageSize)
  24.   612   792    18    15   599   776     <-- page width page height LLx LLy URx URy
  25.                                               (page width/height from *PaperDimension)
  26.                                             (LLx LLy URx URy from *ImageableArea)
  27. Legal
  28. statusdict begin legaltray end
  29.   612  1008    18    19   593   990
  30. A4
  31. statusdict begin a4tray end
  32.   595   842    10     8   585   813
  33. B5
  34. statusdict begin b5tray end
  35.   516   729    18     7   482   702
  36. *fonts                                  <-- marker for fonts
  37. AvantGarde-Book                         <-- real font names in font dictionary
  38. AvantGarde-Demi                                (from *Font)
  39. Bookman-Demi
  40. Bookman-Light
  41. Courier
  42. Helvetica
  43. Helvetica-Narrow
  44. NewCenturySchlbk-Roman
  45. Palatino-Roman
  46. Symbol
  47. Times-Roman
  48. ZapfChancery-MediumItalic
  49. ZapfDingbats
  50. *slots                                    <-- marker for paper tray select
  51. statusdict begin 1 setpapertray end     <-- select first tray
  52. statusdict begin 2 setpapertray end     <-- select second tray  etc..
  53.                                             (from *InputSlot)
  54. *eof                                    <-- logical end of file
  55. */
  56.  
  57. #include <stdio.h>
  58. #include <string.h>
  59. #include <sys/types.h>
  60. #include <sys/stat.h>
  61.  
  62. char    nickname[200];
  63. struct pdef
  64. {
  65.     char    name[500];
  66.     char    set[500];
  67.     char    tray[500];
  68.     int        lx, ly, ux, uy, sx, sy;
  69. } ;
  70.  
  71. char    *malloc();
  72. char    pname[100];
  73. struct pdef printer[40], pblank;
  74. int        nprinter = 0;
  75. char    *fonts[100];
  76. char    *slots[10];
  77. int        nfonts=0;
  78. int        nslots=0;
  79.  
  80. main (argc,argv)
  81. int     argc;
  82. char    *argv[];
  83. {
  84.     char    *fname;
  85.     FILE    *fp_in, *fp_out;
  86.     int        i, j;
  87.     char    *c;
  88.     char    outname[100], fnonly[50];
  89.     char    *destdir;
  90.     
  91.     if (argc < 3)
  92.         usage ();
  93.  
  94.     argc--;
  95.     destdir = argv[argc];
  96.     if (isdir (destdir) != 1)
  97.         usage ();
  98.  
  99.     for (i = 1;  i < argc;  i++)
  100.     {    if ((fp_in = fopen (argv[i], "r")) != NULL)
  101.         {
  102.             for (j = 0;  j < nprinter;  j++)
  103.                 printer[j] = pblank;
  104.             
  105.             for (j = 0;  j < nfonts;  j++)
  106.                 free (fonts[j]);
  107.             
  108.             for (j = 0;  j < nslots;  j++)
  109.                 free (slots[j]);
  110.             
  111.             nfonts = nslots = nprinter = 0;
  112.  
  113.             fnameonly (argv[i], fnonly);
  114.             if ((c = strchr(fnonly, '.')))
  115.                 *c = '\0';
  116.             strcpy (outname,destdir);
  117.             strcat (outname, "/");
  118.             strcat (outname, fnonly);
  119.             strcat (outname, ".def");
  120.  
  121.             if ((fp_out = fopen (outname, "w")) == NULL)
  122.             {    fprintf (stderr, "%s: cannot create file %s\n", argv[0], outname);
  123.                 exit (1);
  124.             }
  125.             build (fp_in);
  126.             display (fp_out);
  127.             fclose (fp_out);
  128.  
  129.             fclose (fp_in);
  130.         }
  131.     }
  132.         
  133. }
  134.  
  135. build (fp_in)
  136. FILE    *fp_in;
  137. {
  138.     char    s[1000];
  139.     int        n;
  140.     int        a, b, c, d;
  141.     char    which[500];
  142.     
  143.     
  144.     while (fgets (s, 999, fp_in) != NULL)
  145.     {    trim (s);
  146.     
  147.         if (strncmp (s, "*PageSize", 9) == 0)
  148.         {
  149.             n = ptype (s, which);
  150.             gettext (s, printer[n].set);
  151.         }
  152.         else if (strncmp (s, "*NickName:", 10) == 0)
  153.         {
  154.             gettext (s, nickname);
  155.         }
  156.         else if (strncmp (s, "*Font ", 6) == 0)
  157.         {
  158.             ftype (s);
  159.         }
  160.         else if (strncmp (s, "*PaperTray", 10) == 0)
  161.         {
  162.             n = ptype (s, which);
  163.             gettext (s, printer[n].tray);
  164.         }
  165.         else if (strncmp (s, "*InputSlot", 10) == 0)
  166.         {
  167.             gettext (s, which);
  168.             slots[nslots] = malloc (strlen(which) + 2);
  169.             strcpy (slots[nslots], which);
  170.             nslots++;
  171.         }
  172.         else if (strncmp (s, "*ImageableArea", 14) == 0)
  173.         {
  174.             n = ptype (s, which);
  175.             sscanf (strchr (s, '"') + 1, "%d%d%d%d", &a, &b, &c, &d);
  176.             printer[n].lx = a;
  177.             printer[n].ly = b;
  178.             printer[n].ux = c;
  179.             printer[n].uy = d;
  180.         }
  181.         else if (strncmp (s, "*PaperDimension", 15) == 0)
  182.         {
  183.             n = ptype (s, which);
  184.             sscanf (strchr (s, '"') + 1, "%d %d", &a, &b);
  185.             printer[n].sx = a;
  186.             printer[n].sy = b;
  187.         }
  188.     }
  189. }
  190.  
  191.  
  192. ptype (s, which)
  193. char    *s, *which;
  194. {
  195.     char    *w, *ww;
  196.     int        i;
  197.  
  198.     w = s;
  199.     while (*w++ != ' ')
  200.         ;
  201.     ww = which;
  202.     while (*w != ':')
  203.     {
  204.         if (*w == '/')
  205.         {
  206.  
  207.             while (*w++ != ':')
  208.                 ;
  209.             break;
  210. /*
  211.             ww = which;
  212.             w++;
  213.             continue;
  214. */
  215.         }
  216.         if (*w == ' ')
  217.             *w = '-';
  218.         *ww++ = *w++;
  219.     }
  220.     *ww = '\0';
  221.     while (*w == ' ')
  222.         w++;
  223. /*
  224.     sscanf (s, "%*s%s", which);
  225. */
  226. /*
  227.     w = which;
  228.     while (*w)
  229.     {    if (*w == ':')
  230.         {    *w = '\0';
  231.             break;
  232.         }
  233.         w++;
  234.     }
  235. */
  236.     for (i = 0;  i < nprinter;  i++)
  237.     {    if (strcmp (which, printer[i].name) == 0)
  238.             return (i);
  239.     }
  240.     strcpy (printer[nprinter].name, which);
  241.     nprinter++;
  242.     return (nprinter - 1);
  243. }
  244.  
  245. ftype (s)
  246. char    *s;
  247. {
  248.     char    *w, *ww;
  249.     char    which[100];
  250.     int        i;
  251.  
  252.     w = s;
  253.     while (*w++ != ' ')
  254.         ;
  255.     ww = which;
  256.     while (*w != ':')
  257.     {
  258.         if (*w == ' ')
  259.             *w = '-';
  260.         *ww++ = *w++;
  261.     }
  262.     *ww = '\0';
  263.     while (*w == ' ')
  264.         w++;
  265.  
  266.     if (strncmp (which, "Za", 2))
  267.     {    if (tscan (which, "Bold") >= 0   ||  tscan (which,"Italic") >= 0 
  268.             || tscan (which,"Obliq") >= 0)
  269.             return;
  270.     }
  271.     
  272.     fonts[nfonts] = malloc (strlen (which) + 1);
  273.     strcpy (fonts[nfonts++], which);
  274.     
  275. }
  276.  
  277.  
  278. display (fp_out)
  279. FILE    *fp_out;
  280. {
  281.     int        i;
  282.     
  283.     fprintf (fp_out, "%s\n", nickname);
  284.     for (i = 0;  i < nprinter;  i++)
  285.     {
  286.         fprintf (fp_out, "%s\n%s\n", printer[i].name, printer[i].set);
  287.         fprintf (fp_out, "%5d %5d %5d %5d %5d %5d\n",
  288.             printer[i].sx, printer[i].sy,
  289.             printer[i].lx, printer[i].ly, printer[i].ux, printer[i].uy);
  290.  
  291. /*        print the actual size of the image area 
  292.  
  293.         printf ("%-20s %-20s %4d %4d\n", pname, printer[i].name,
  294.             printer[i].ux - printer[i].lx + 1,
  295.             printer[i].uy - printer[i].ly + 1);
  296. */
  297.     }
  298.  
  299.     fprintf (fp_out, "*fonts\n");
  300.     for (i = 0;  i < nfonts;  i++)
  301.         fprintf (fp_out, "%s\n", fonts[i]);
  302.  
  303.     fprintf (fp_out, "*slots\n");
  304.     for (i = 0;  i < nslots;  i++)
  305.         fprintf (fp_out, "%s\n", slots[i]);
  306.     fprintf (fp_out, "*eof\n");
  307. }
  308.  
  309. gettext (s, t)
  310. char    *s, *t;
  311. {
  312.     strcpy (t, strchr (s, '"') + 1);
  313.     while (*t)
  314.     {    if (*t == '"')
  315.         {    *t = '\0';
  316.             break;
  317.         }
  318.         t++;
  319.     }
  320. }
  321.  
  322. tscan (s, t)
  323. char     s[], t[];
  324. {
  325.     int    i, j, k;
  326.     for (i = 0;  s[i] != '\0';  i++)
  327.     {    for (j = i, k=0;  t[k] != '\0'  &&  s[j] == t[k];  j++, k++)
  328.             ;
  329.         if (t[k] == '\0')
  330.             return (i);
  331.     }
  332.     return (-1);
  333. }
  334.  
  335. trim (s)
  336. char    *s;
  337. {
  338.     while (*s)
  339.     {    if (*s <  ' ')
  340.         {    *s = '\0';
  341.             break;
  342.         }
  343.         s++;
  344.     }
  345. }
  346.  
  347.  
  348. fnameonly (path, fn)
  349. char    *path, *fn;
  350. {
  351.     char    *strrchr(), *where;
  352.     
  353.     if ((where = strrchr (path, '/')) == NULL)
  354.         strcpy (fn, path);
  355.     else
  356.         strcpy (fn, where + 1);
  357.     return;
  358. }
  359.  
  360.  
  361. /****************************************************************************
  362. *    isdir (name)                                                            *
  363. *                                                                            *
  364. *    See if the 'name' is a directory entry.  If so, return 1.  If not,        *
  365. *    return a 0.                                                                *
  366. *                                                                            *
  367. *    A call to stat() returns a bunch of parameters into 'buf' that             *
  368. *    essentially decodes the inode of the file into useful information.        *
  369. *    The st_node variable indicate the type of file.  Examine the stat.h        *
  370. *    file to see all possible file types.  For this routine, we are             *
  371. *    interested only in the bits S_IFMT (0170000 mask) of st_mode.  If these    *
  372. *    bits are S_IFDR (0040000), then we have a directory, otherwise not.        *
  373. ****************************************************************************/
  374.  
  375. isdir (name)
  376. char    *name;
  377. {
  378.     struct stat buf;
  379.  
  380.     if (stat (name, &buf) != 0)                /* ensure stat() is happy        */
  381.         return (-1);
  382.     if ((buf.st_mode & S_IFMT) == S_IFDIR)     /* good call, see if             */
  383.         return (1);                            /*        is directory            */
  384.     if ((buf.st_mode & S_IFMT) == S_IFREG)     /* good call, see if            */
  385.         return (0);                            /*        is file                    */
  386.     return (-1);                            /* something else                */
  387. }
  388.  
  389. usage ()
  390. {    fprintf (stderr, "Usage: table f.ppd f.ppd ... /dest.dir\n");
  391.     fprintf (stderr, "   where   f.ppd    = source .ppd files\n");
  392.     fprintf (stderr, "           dest.dir = desination directory for .def files\n");
  393.     exit (1);
  394. }            
  395.     
  396.