home *** CD-ROM | disk | FTP | other *** search
/ Carousel Volume 2 #1 / carousel.iso / mactosh / unix / macps_pr.hqx / prepfix.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-09-22  |  5.4 KB  |  237 lines

  1. /*
  2.  * Copyright (c) 1988, The Trustees 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    1.1 9/19/88";
  16. #endif lint
  17.  
  18. #include <ctype.h>
  19. #include <stdio.h>
  20. #include <strings.h>
  21.  
  22. #define BIGBUFSIZE    10240
  23. #define    CLEARTOMARK    12
  24. #define EEXECLEN    80
  25. #define EXTRA        (NZEROLINE * ZEROLINE + CLEARTOMARK)
  26. #define LINELEN        256
  27. #define    NPRODUCT    16
  28. #define NZEROLINE    7
  29. #define    PRODUCTLEN    64
  30. #define ZEROLINE    65
  31.  
  32. char *Fgets();
  33. char *match();
  34. char *myname;
  35. char lproduct[PRODUCTLEN][NPRODUCT];
  36. char pproduct[PRODUCTLEN][NPRODUCT];
  37. char tempname[] = "/tmp/prepfixXXXXXX";
  38.  
  39. main(argc, argv)
  40. int argc;
  41. char **argv;
  42. {
  43.     register FILE *tp;
  44.     register int i, nl, np;
  45.     register char *lp, *pp;
  46.     char buf[BIGBUFSIZE];
  47.  
  48.     if(myname = rindex(*argv, '/'))
  49.         myname++;
  50.     else
  51.         myname = *argv;
  52.     nl = np = 0;
  53.     for(argc--, argv++ ; argc > 0 && **argv == '-' ; argc--, argv++) {
  54.         switch((*argv)[1]) {
  55.          case 'h':
  56.             usage();
  57.          case 'l':
  58.             if(nl >= NPRODUCT - 1) {
  59.                 fprintf(stderr,
  60.                  "%s: Too many LW-like product names\n",
  61.                  myname);
  62.                 exit(1);
  63.             }
  64.             if((*argv)[2])
  65.                 lp = &(*argv)[2];
  66.             else {
  67.                 if(argc < 2) {
  68.                     fprintf(stderr,
  69.                      "%s: No argument for -l\n", myname);
  70.                     exit(1);
  71.                 }
  72.                 argc--;
  73.                 argv++;
  74.                 lp = *argv;
  75.             }
  76.             strncpy(lproduct[nl], lp, PRODUCTLEN - 1);
  77.             lproduct[nl++][PRODUCTLEN - 1] = 0;
  78.             break;
  79.          case 'p':
  80.             if(np >= NPRODUCT - 1) {
  81.                 fprintf(stderr,
  82.                  "%s: Too many LW+-like product names\n",
  83.                  myname);
  84.                 exit(1);
  85.             }
  86.             if((*argv)[2])
  87.                 pp = &(*argv)[2];
  88.             else {
  89.                 if(argc < 2) {
  90.                     fprintf(stderr,
  91.                      "%s: No argument for -p\n", myname);
  92.                     exit(1);
  93.                 }
  94.                 argc--;
  95.                 argv++;
  96.                 pp = *argv;
  97.             }
  98.             strncpy(pproduct[np], pp, PRODUCTLEN - 1);
  99.             pproduct[np++][PRODUCTLEN - 1] = 0;
  100.             break;
  101.         }
  102.     }
  103.     if(argc > 1)
  104.         usage();
  105.     if(argc > 0 && freopen(*argv, "r", stdin) == NULL) {
  106.         fprintf(stderr, "%s: can't open %s\n", myname, *argv);
  107.         exit(1);
  108.     }
  109.     mktemp(tempname);
  110.     if((tp = fopen(tempname, "w+")) == NULL) {
  111.         fprintf(stderr, "%s: can't create temp file %s\n",
  112.          myname, tempname);
  113.         exit(1);
  114.     }
  115.     unlink(tempname);
  116.     if(Fgets(buf, BIGBUFSIZE, stdin) == NULL) {
  117.         fprintf(stderr, "%s: Null input\n", myname);
  118.         exit(1);
  119.     }
  120.     for( ; ; ) {
  121.         if(strncmp(buf, "% \251", 3) == 0) {
  122.             fputs("% ", tp);
  123.             buf[0] = '(';
  124.             buf[1] = 'C';
  125.             buf[2] = ')';
  126.         } else if(strncmp(buf, "%%BeginProcSet:", 15) == 0) {
  127.             Fputs(buf, stdout);
  128.             fseek(tp, 0L, 0);
  129.             while((i = fread(buf, 1, BIGBUFSIZE, tp)) > 0)
  130.                 fwrite(buf, 1, i, stdout);
  131.             fclose(tp);
  132.             break;
  133.         }
  134.         Fputs(buf, tp);
  135.         if(Fgets(buf, BIGBUFSIZE, stdin) == NULL) {
  136.             fprintf(stderr, "%s: No BeginProcSet\n",
  137.              myname);
  138.             exit(1);
  139.         }
  140.     }
  141.     while(Fgets(buf, BIGBUFSIZE, stdin)) {
  142.         if((nl || np) && strncmp(buf, "/LW{", 4) == 0) {
  143.             Fputs(buf, stdout);
  144.             if(!Fgets(buf, BIGBUFSIZE, stdin)) {
  145.                 fprintf(stderr,
  146.              "%s: Unexpected EOF while adding product names\n",
  147.                  myname);
  148.                 exit(1);
  149.             }
  150.             Fputs(buf, stdout);
  151.             for(i = 0 ; i < nl ; i++)
  152.                 printf("LW 0 eq {/LW{save statusdict/product get(%s)anchorsearch exch pop {pop 1}{0}ifelse exch restore}bind def}if\n",
  153.                  lproduct[i]);
  154.             for(i = 0 ; i < np ; i++)
  155.                 printf("LW 0 eq {/LW{save statusdict/product get(%s)anchorsearch exch pop {pop 2}{0}ifelse exch restore}bind def}if\n",
  156.                  pproduct[i]);
  157.             continue;
  158.         } else if(match(buf, "setdefaulttimeouts")
  159.          || match(buf, "setsccinteractive"))
  160.             putchar('%');
  161.         else if(match(buf, "eexec")) {
  162.             if(match(buf, "/stretch")) {
  163.                 eexec("stretch", buf);
  164.                 continue;
  165.             } else if(match(buf, "/smooth4")) {
  166.                 eexec("smooth4", buf);
  167.                 continue;
  168.             }
  169.         } else if(strncmp(buf, "%%EndProcSet", 12) == 0) {
  170.             Fputs(buf, stdout);
  171.             break;
  172.         }
  173.         Fputs(buf, stdout);
  174.     }
  175.     exit(0);
  176. }
  177.  
  178. char *match(str1, str2)
  179. register char *str1, *str2;
  180. {
  181.     register int firstchar = *str2;
  182.     register int len = strlen(str2);
  183.  
  184.     while(*str1) {
  185.         if(*str1 == firstchar && strncmp(str1, str2, len) == 0)
  186.             return(str1);
  187.         str1++;
  188.     }
  189.     return(NULL);
  190. }
  191.  
  192. eexec(name, buf)
  193. char *name;
  194. register char *buf;
  195. {
  196.     register int len;
  197.     register char *bp;
  198.  
  199.     if(!Fgets(buf, BIGBUFSIZE, stdin)) {
  200.         fprintf(stderr, "%s: EOF during reading eexec\n", myname);
  201.         exit(1);
  202.     }
  203.     len = strlen(bp = buf) - 1;
  204.     printf("ok userdict/%s known not and{currentfile eexec}{%d{currentfile read pop pop}repeat}ifelse\n",
  205.      name, len + (len / EEXECLEN) + (len % EEXECLEN ? 1 : 0) + EXTRA);
  206.     while(len >= 80) {
  207.         fwrite(bp, 80, 1, stdout);
  208.         putchar('\n');
  209.         bp += 80;
  210.         len -= 80;
  211.     }
  212.     if(len > 0) {
  213.         fwrite(bp, len, 1, stdout);
  214.         putchar('\n');
  215.     }
  216.     for( ; ; ) {
  217.         if(Fgets(buf, BIGBUFSIZE, stdin) == NULL) {
  218.             fprintf(stderr, "%s: EOF reached before cleartomark\n",
  219.              myname);
  220.             exit(1);
  221.         }
  222.         if(strncmp(buf, "cleartomark", 11) == 0) {
  223.             Fputs(buf, stdout);
  224.             return;
  225.         }
  226.         Fputs(buf, stdout);
  227.     }
  228. }
  229.  
  230. usage()
  231. {
  232.     fprintf(stderr, "Usage: %s [-l LW_name] [-p LW+_name] [file]\n",
  233.      myname);
  234.     fprintf(stderr, "       %s -help\n", myname);
  235.     exit(1);
  236. }
  237.