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

  1. /* ta=4 */
  2. /*                    s e l e c t d e f . c
  3.  
  4.     allow user to select printer from various *.def files
  5.     and also select defaule page size (letter, a4 etc)
  6.     
  7.     Generate a postscript programme (psizes.ps) that should be
  8.     sent to the printer to print out the actual imageable area.
  9. */
  10. /*
  11.  * $Id: selectdef.c,v 3.2 1992/01/19 05:50:33 ajf Exp ajf $
  12.  *
  13. */
  14.  
  15. #include <stdio.h>
  16. #include "ctp.h"
  17.  
  18. typedef struct                     /*    measurement in point                    */
  19. {    char    paper_name[60];        /*    name of paper size (for command line)    */
  20.     char    paper_tray[200];    /*    postscript operator to select this tray    */
  21.     int        width;                /*    portrait point width of paper            */
  22.     int        height;                /*    portrait point height of paper            */
  23. } measure;
  24.  
  25. char *psheader = "%!\n\
  26. /getsize {\n\
  27.     /nf nf 1 add def\n\
  28.     clippath\n\
  29.     pathbbox\n\
  30. } def\n\
  31. /sendsize {\n\
  32.     /ury exch def\n\
  33.     /urx exch def\n\
  34.     /lly exch def\n\
  35.     /llx exch def\n\
  36.     /str 64 string def\n\
  37.     show\n\
  38.     (  ) show\n\
  39.     llx    ceiling cvi str cvs show\n\
  40.     (  ) show\n\
  41.     lly    ceiling cvi str cvs show\n\
  42.     (  ) show\n\
  43.     urx    floor cvi str cvs show\n\
  44.     (  ) show\n\
  45.     ury    floor cvi str cvs show\n\
  46. } def\n\
  47. /pg save def\n\
  48. newpath\n\
  49. /nf 0 def\n";
  50.  
  51. char *psfont = "/Courier findfont 12 scalefont setfont\n";
  52.  
  53. char *pstail = "{    /ycoord ycoord -15 add def\n\
  54.     50 ycoord moveto\n\
  55.     sendsize\n\
  56.     /nf nf 1 sub def\n\
  57.     nf 0 eq { exit } if\n\
  58. } loop\n\
  59. showpage \n\
  60. pg restore\n\004";
  61.  
  62. char    printername[100];
  63. int        defaultsize;
  64.  
  65. int        screen_lines = 20;
  66. void trim();
  67.  
  68. main (argc, argv)
  69. int        argc;
  70. char    *argv[];
  71. {    int        i,j;
  72.     int        many, n, pnumber;
  73.     char    printers[100];
  74.     FILE    *fp, *fpdef;
  75.     char    cmd[300];
  76.     char    resp;
  77.     int        lines;
  78.     char    *env_lines, *getenv();
  79.  
  80.  
  81.     if (env_lines = getenv ("LINES"))
  82.         screen_lines = atoi (env_lines) - 4;
  83.     
  84.     if (argc < 2)
  85.     {    fprintf (stderr, "Cannot find printer definition files (*.def)...\n");
  86.         exit (1);
  87.     }
  88. redo:
  89.     do
  90.     {
  91.         pnumber = 0;
  92.         printf ("\n\n\n                         SELECT PRINTER TYPE\n\n");
  93.         many = 0;
  94.         lines = 0;
  95.         for (i = 1;  i < argc;  i++)
  96.         {    if ((fp = fopen (argv[i], "r")) == NULL)
  97.                 exit (1);
  98.             fgets (printers, 99, fp);    /* skip *printer */
  99.             fgets (printers, 99, fp);
  100.             trim (printers);
  101.             n = strlen (printers);
  102.             if (n < 33)
  103.                 n = 33;
  104.             many += n;    
  105.             if (many > 66)
  106.             {    many = n;
  107.                 lines++;
  108.                 if (lines % screen_lines == 0)
  109.                 {    if ((pnumber = request (&resp, argc)) > 0)
  110.                         goto gotit;
  111.                     if (resp == 'R')
  112.                         goto redo;
  113.                 }
  114.                 else printf ("\n");
  115.             }
  116.             printf ("%3d: %-34s", i, printers);
  117.             fclose (fp);
  118.         }
  119.     } while ((pnumber = request (&resp, argc)) == 0);
  120. gotit:
  121.     printf ("\n");
  122.     if (access (argv[pnumber], 4) == 0)
  123.     {
  124.         if (getsize (argv[pnumber]) == 0)
  125.         {    sprintf (cmd, "cp %s psfprint.def", argv[pnumber]);
  126.             system (cmd);
  127.             genpost (argv[pnumber]);
  128.         }
  129.     }
  130.     else
  131.     {    fprintf (stderr, "Cannot find printer definition file: %s\n", argv[pnumber]);
  132.         exit (1);
  133.     }
  134.     exit (0);
  135. }
  136.  
  137. request (r, maxno)
  138. char    *r;
  139. int        maxno;
  140. {    char printers[100];
  141.     int     pnumber;
  142.     do
  143.     {    *printers = '\0';
  144.         printf ("\n==> ( q = quit, r = refresh display, ENTER = next screen)\n    SELECT PRINTER NUMBER: ");
  145.         gets (printers);
  146.         if (UCCHAR (*printers) == 'Q')
  147.             incomplete ();
  148.         if (UCCHAR (*printers) == 'R')
  149.         {    *r = 'R';
  150.             return (0);
  151.         }
  152.         pnumber = atoi (printers);
  153.         if (pnumber < 1)
  154.             break;
  155.         else if (pnumber > maxno)
  156.             printf ("... Invalid printer number\n");
  157.         else
  158.             break;
  159.     } while (1);
  160.     *r = ' ';
  161.     return (pnumber);
  162. }
  163.  
  164. getsize (fn)
  165. char    *fn;
  166. {    char    ptypes[40][100];
  167.     char    line[100];
  168.     int        ntypes, i, ncol, pnumber;
  169.     FILE    *fpdef;
  170.  
  171.     if ((fpdef = fopen (fn, "r")) != NULL)
  172.     {    fgets (printername, 99, fpdef);    /* skip *printer */
  173.         fgets (printername, 99, fpdef);
  174.         trim (printername);
  175.         ntypes = 1;
  176.         for ( ; ; )
  177.         {    fgets (line, 99, fpdef);
  178.             if (*line == '*')
  179.                 break;
  180.             trim (line);
  181.             strcpy (ptypes[ntypes], line);
  182.             fgets (line, 99, fpdef);
  183.             fgets (line, 99, fpdef);
  184.             ntypes++;
  185.         }
  186.         fclose (fpdef);
  187.         printf ("\nPrinter is: %s\n\n", printername);
  188.     }
  189.     else
  190.     {    fprintf (stderr, "Cannot find printer definition file: %s\n", fn);
  191.         return (1);
  192.     }
  193.     return (0);
  194. }
  195.  
  196. genpost (f)
  197. char    *f;
  198. {
  199.     FILE *pdef, *psfile;
  200.     char    line[500];
  201.     measure    page_types[40];
  202.     int        i, j;
  203.     int        x, y;
  204.  
  205.     if ((pdef = fopen (f, "r")) != NULL)
  206.     {    fgets (line, 100, pdef);        /* skip *printer         */
  207.         fgets (line, 100, pdef);        /*    skip printer name     */
  208.         fgets (line, 100, pdef);        /*    skip *paper            */
  209.         for (i = 0;  i < 28;  i++)
  210.         {    if (fgets (page_types[i].paper_name, 59, pdef) == NULL)
  211.                 break;
  212.             trim (page_types[i].paper_name);
  213.             if (page_types[i].paper_name[0] ==  '*')
  214.                 break;
  215.             if (fgets (page_types[i].paper_tray, 199, pdef) == NULL)
  216.                 bad_file(f);
  217.             trim (page_types[i].paper_tray);
  218.             if (fgets (line, 100, pdef) == NULL)
  219.                 bad_file(f);
  220.             trim (line);
  221.             sscanf (line, "%d%d",
  222.                 &page_types[i].width,
  223.                 &page_types[i].height);
  224.  
  225.         }
  226.         page_types[i].paper_name[0] = '\0';
  227.         fclose (pdef);
  228.         if ((psfile = fopen ("psizes.ps", "w")) != NULL)
  229.         {    fprintf (psfile, "%s", psheader);
  230.             for (j = i-1;  j >= 0;  j--)
  231.             {    lcase (line, page_types[j].paper_name);
  232.                 fprintf (psfile, "%s\n(%-10s:%5d%5d) getsize\n",
  233.                             line,
  234.                             page_types[j].paper_name,
  235.                             page_types[j].width, 
  236.                             page_types[j].height);
  237.             }
  238. /*
  239.             fprintf (psfile, "%s\n", page_types[defaultsize].paper_tray);
  240. */
  241.             fprintf (psfile, "%s", psfont);
  242.             x = 50;
  243.             y = 500;
  244.             fprintf (psfile, "%d %d moveto\n", x, y);
  245.             fprintf (psfile, "(Imageable area) show\n");
  246.             y -= 15;
  247.             fprintf (psfile, "%d %d moveto\n", x, y);
  248.             fprintf (psfile, "(");
  249.             send (printername, psfile);
  250.             fprintf (psfile, ") show\n");
  251.             fprintf (psfile, "/ycoord %d def\n", y);
  252.             fprintf (psfile, "%s", pstail);
  253.         }
  254.         else
  255.             fprintf (stderr, "Cannot create psizes.ps\n");
  256.     }
  257.     else
  258.         fprintf (stderr, "Cannot open %s for reading\n", f);
  259. }
  260.  
  261.  
  262. void trim (s)                    /*    trim trailing blanks  and \n */
  263. char    *s;
  264. {    int many;
  265.  
  266.     for (many = strlen (s) - 1;  many >= 0;  many--)
  267.     {    if (isgraph (s[many]))
  268.             break;
  269.         else
  270.             s[many] = '\0';
  271.     }
  272. }
  273.  
  274.  
  275.  
  276. bad_file(f)
  277. char    *f;
  278. {
  279.     fprintf (stderr, "Bad %s file\n", f);
  280.     exit (1);
  281. }
  282.  
  283. lcase (a, b)
  284. char    *a, *b;
  285. {
  286.     while (*b)
  287.     {    *a++ = LCCHAR (*b);
  288.         b++;
  289.     }
  290.     *a = '\0';
  291. }
  292.  
  293. send (s, fp)
  294. char    *s;
  295. FILE    *fp;
  296. {
  297.     char    *strchr();
  298.     
  299.     while (*s)
  300.     {
  301.         if (strchr ("\r\b\\()", *s) != NULL)
  302.             fputc ('\\', fp);
  303.         fputc (*s, fp);
  304.         s++;
  305.     }
  306. }
  307.  
  308. incomplete ()
  309. {
  310.     printf ("Incomplete printer installation\n");
  311.     exit (44);
  312. }
  313.