home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / macps / prepfix.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-12  |  5.9 KB  |  252 lines

  1. /*
  2.  * Copyright (c) 1988, The Regents of the University of California.
  3.  * Edward Moy, Workstation Software Support Group, Workstation Support Serices,
  4.  * Information Systems and Technology.
  5.  *
  6.  * Permission is granted to any individual or institution to use, copy,
  7.  * or redistribute this software so long as it is not sold for profit,
  8.  * provided that this notice and the original copyright notices are
  9.  * retained.  The University of California makes no representations about the
  10.  * suitability of this software for any purpose.  It is provided "as is"
  11.  * without express or implied warranty.
  12.  */
  13.  
  14. #ifndef lint
  15. static char *SCCSid = "@(#)prepfix.c    2.2 10/25/89";
  16. #endif lint
  17.  
  18. #include <ctype.h>
  19. #include <stdio.h>
  20. #ifdef SYSV
  21. #include <string.h>
  22. #else SYSV
  23. #include <strings.h>
  24. #endif SYSV
  25. #include "str.h"
  26.  
  27. #define    CLEARTOMARK    12
  28. #define EEXECLEN    80
  29. #define EXTRA        (NZEROLINE * ZEROLINE + CLEARTOMARK)
  30. #define LINELEN        256
  31. #define    NPRODUCTS    32
  32. #define NZEROLINE    7
  33. #define ZEROLINE    65
  34. #ifdef SYSV
  35. #define    index        strchr
  36. #define    rindex        strrchr
  37. #endif SYSV
  38.  
  39. char exstr[] = "\
  40. %ck userdict/%s known not and{currentfile eexec}{%d{currentfile read\n\
  41. pop pop}repeat}ifelse\n\
  42. ";
  43. char *match();
  44. char *myname;
  45. int maxproducts = NPRODUCTS;
  46. int nproducts = 0;
  47. char Ok[] = "\
  48. /Ok{ok{true}{save /Pd statusdict /product get def false 0 1 ProdArr length\n\
  49. 1 sub{Pd exch ProdArr exch get anchorsearch exch pop{pop pop true exit}if}for\n\
  50. exch restore}ifelse}bind def\n\
  51. ";
  52. char ProdArr0[] = "/ProdArr [\n";
  53. char ProdArr1[] = "] def\n";
  54. char **products;
  55. char tempname[] = "/tmp/prepfixXXXXXX";
  56.  
  57. main(argc, argv)
  58. int argc;
  59. char **argv;
  60. {
  61.     register STR *str;
  62.     register FILE *tp;
  63.     register int i;
  64.     register unsigned char *lp;
  65.     char buf[BUFSIZ];
  66.     char *malloc(), *realloc();
  67.  
  68.     if(myname = rindex(*argv, '/'))
  69.         myname++;
  70.     else
  71.         myname = *argv;
  72.     for(argc--, argv++ ; argc > 0 && **argv == '-' ; argc--, argv++) {
  73.         switch((*argv)[1]) {
  74.          case 'h':
  75.             usage();
  76.          case 'l':
  77.             if(nproducts <= 0 && (products =
  78.              (char **)malloc(maxproducts*sizeof(char *))) == NULL) {
  79.                 fprintf(stderr,
  80.                  "%s: Out of memory creating products array\n",
  81.                  myname);
  82.                 exit(1);
  83.             } else if(nproducts >= maxproducts - 1 && (products =
  84.              (char **)realloc(products, (maxproducts += NPRODUCTS)
  85.              * sizeof(char *))) == NULL) {
  86.                 fprintf(stderr,
  87.                  "%s: Out of memory expanding products array\n",
  88.                  myname);
  89.                 exit(1);
  90.             }
  91.             if((*argv)[2])
  92.                 products[nproducts++] = &(*argv)[2];
  93.             else {
  94.                 if(argc < 2) {
  95.                     fprintf(stderr,
  96.                      "%s: No argument for -l\n", myname);
  97.                     exit(1);
  98.                 }
  99.                 argc--;
  100.                 argv++;
  101.                 products[nproducts++] = *argv;
  102.             }
  103.             break;
  104.         }
  105.     }
  106.     if(argc > 1)
  107.         usage();
  108.     if(argc > 0 && freopen(*argv, "r", stdin) == NULL) {
  109.         fprintf(stderr, "%s: Can't open %s\n", myname, *argv);
  110.         exit(1);
  111.     }
  112.     mktemp(tempname);
  113.     if((tp = fopen(tempname, "w+")) == NULL) {
  114.         fprintf(stderr, "%s: Can't create temp file %s\n",
  115.          myname, tempname);
  116.         exit(1);
  117.     }
  118.     unlink(tempname);
  119.     str = STRalloc();
  120.     if(!STRgets(str, stdin)) {
  121.         fprintf(stderr, "%s: Null input\n", myname);
  122.         exit(1);
  123.     }
  124.     for( ; ; ) {
  125.         if(STRheadcompare(str, "% \251") == 0) {
  126.             fputs("% ", tp);
  127.             str->bufptr[0] = '(';
  128.             str->bufptr[1] = 'C';
  129.             str->bufptr[2] = ')';
  130.         } else if(STRheadcompare(str, "%%BeginProcSet:") == 0) {
  131.             STRputs(str, stdout);
  132.             fseek(tp, 0L, 0);
  133.             while((i = fread(buf, 1, BUFSIZ, tp)) > 0)
  134.                 fwrite(buf, 1, i, stdout);
  135.             fclose(tp);
  136.             break;
  137.         }
  138.         STRputs(str, tp);
  139.         if(!STRgets(str, stdin)) {
  140.             fprintf(stderr, "%s: No BeginProcSet\n", myname);
  141.             exit(1);
  142.         }
  143.     }
  144.     while(STRgets(str, stdin)) {
  145.         if(nproducts > 0 && STRheadcompare(str, "/ok{") == 0) {
  146.             STRputs(str, stdout);
  147.             fputs(ProdArr0, stdout);
  148.             for(i = 0 ; i < nproducts ; i++)
  149.                 printf("(%s)\n", products[i]);
  150.             fputs(ProdArr1, stdout);
  151.             fputs(Ok, stdout);
  152.             continue;
  153.         } else if(STRmatch(str, "setdefaulttimeouts")
  154.          || STRmatch(str, "setsccinteractive"))
  155.             continue;
  156.         else if(STRmatch(str, "/stretch") && STRmatch(str, "eexec")) {
  157.             eexec("stretch", str);
  158.             continue;
  159.         } else if(STRmatch(str, "/smooth4") && STRmatch(str, "eexec")) {
  160.             eexec("smooth4", str);
  161.             continue;
  162.         } else if(STRmatch(str, " checkload")) {
  163.             checkload(str);
  164.             continue;
  165.         } else if(STRmatch(str, "(LaserWriter II NT)")) {
  166.             while(STRgets(str, stdin) && STRheadcompare(str, "35de")
  167.              != 0)
  168.                 { /* ignore line */ }
  169.             while(STRgets(str, stdin) && isxdigit(*str->bufptr))
  170.                 { /* ignore line */ }
  171.         } else if(lp = STRmatch(str, "scaleby96{ppr")) {
  172.             STRputsptr(str, lp, stdout);
  173.             continue;
  174.         } else if(STRmatch(str, "waittimeout"))
  175.             continue;
  176.         else if(STRheadcompare(str, "%%EndProcSet") == 0) {
  177.             STRputs(str, stdout);
  178.             break;
  179.         }
  180.         STRputs(str, stdout);
  181.     }
  182.     exit(0);
  183. }
  184.  
  185. eexec(name, str)
  186. char *name;
  187. register STR *str;
  188. {
  189.     register int len;
  190.  
  191.     if(!STRgets(str, stdin)) {
  192.         fprintf(stderr, "%s: EOF during reading eexec\n", myname);
  193.         exit(1);
  194.     }
  195.     len = (str->curendptr - str->bufptr) - 1;
  196.     printf(exstr, nproducts > 0 ? 'O' : 'o', name, len + (len / EEXECLEN)
  197.      + (len % EEXECLEN ? 1 : 0) + EXTRA);
  198.     spliteexec(str);
  199. }
  200.  
  201. checkload(str)
  202. register STR *str;
  203. {
  204.     if(nproducts > 0)
  205.         *str->bufptr = 'O';
  206.     STRputs(str, stdout);
  207.     if(!STRgets(str, stdin)) {
  208.         fprintf(stderr, "%s: EOF during reading eexec\n", myname);
  209.         exit(1);
  210.     }
  211.     spliteexec(str);
  212. }
  213.  
  214. spliteexec(str)
  215. register STR *str;
  216. {
  217.     register int len;
  218.     register unsigned char *bp;
  219.  
  220.     bp = str->bufptr;
  221.     len = (str->curendptr - bp) - 1;
  222.     while(len >= 80) {
  223.         fwrite(bp, 80, 1, stdout);
  224.         putchar('\n');
  225.         bp += 80;
  226.         len -= 80;
  227.     }
  228.     if(len > 0) {
  229.         fwrite(bp, len, 1, stdout);
  230.         putchar('\n');
  231.     }
  232.     for( ; ; ) {
  233.         if(!STRgets(str, stdin)) {
  234.             fprintf(stderr, "%s: EOF reached before cleartomark\n",
  235.              myname);
  236.             exit(1);
  237.         }
  238.         STRputs(str, stdout);
  239.         if(STRheadcompare(str, "cleartomark") == 0)
  240.             return;
  241.     }
  242. }
  243.  
  244. usage()
  245. {
  246.     fprintf(stderr,
  247.      "Usage: %s [-l product_name1 [-l product_name2]...] [file]\n",
  248.      myname);
  249.     fprintf(stderr, "       %s -help\n", myname);
  250.     exit(1);
  251. }
  252.