home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / macps / macps.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-12  |  4.9 KB  |  230 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 = "@(#)macps.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 <sys/types.h>
  26. #include <sys/file.h>
  27. #include "str.h"
  28. #include "ucbwhich.h"
  29.  
  30. #define    CONFIG        "macps.config"
  31. #ifdef SYSV
  32. #define    index        strchr
  33. #define    rindex        strrchr
  34. #endif SYSV
  35.  
  36. #ifdef SAVE
  37. char *finale = "clear countdictstack 2 sub{end}repeat macps restore\n";
  38. char intro[] = "\
  39. %%! *** Created by macps: %s\
  40. /macps save def\n\
  41. ";
  42. #else SAVE
  43. char intro[] = "\
  44. %%! *** Created by macps: %s\
  45. ";
  46. #endif SAVE
  47. char *myname;
  48. int ncopies = 0;
  49. #ifdef CONFIGDIR
  50. char ucblib[UCBMAXPATHLEN] = CONFIGDIR;
  51. #else CONFIGDIR
  52. int ucbalternate;
  53. char ucbpath[UCBMAXPATHLEN];
  54. char ucblib[UCBMAXPATHLEN];
  55. #endif CONFIGDIR
  56.  
  57. main(argc, argv)
  58. int argc;
  59. char **argv;
  60. {
  61.     register STR* str;
  62.     register char *cp, *pp;
  63.     register FILE *fp;
  64.     register int i, fd;
  65.     char line[BUFSIZ];
  66.     char path[UCBMAXPATHLEN];
  67.     long ltime;
  68.     char *ctime();
  69.  
  70. #ifndef CONFIGDIR
  71.     ucbwhich(*argv);
  72. #endif CONFIGDIR
  73.     if(myname = rindex(*argv, '/'))
  74.         myname++;
  75.     else
  76.         myname = *argv;
  77.     cp = NULL;
  78.     for(argc--, argv++ ; argc > 0 && **argv == '-' ; argc--, argv++) {
  79.         switch((*argv)[1]) {
  80.          case 'c':    /* multiple copies */
  81.             if((*argv)[2])
  82.                 ncopies = atoi(&(*argv[2]));
  83.             else {
  84.                 if(argc < 2)
  85.                     Usage();    /* never returns */
  86.                 argc--;
  87.                 ncopies = atoi(*++argv);
  88.             }
  89.             if(ncopies <= 0)
  90.                 Usage();    /* never returns */
  91.             break;
  92.          case 'd':    /* alternate directory for config file */
  93.             if((*argv)[2])
  94.                 cp = &(*argv[2]);
  95.             else {
  96.                 if(argc < 2)
  97.                     Usage();    /* never returns */
  98.                 argc--;
  99.                 cp = *++argv;
  100.             }
  101.             strcpy(ucblib, cp);
  102.             break;
  103.          case 'r':    /* raw mode */
  104.             rawmode++;
  105.             break;
  106.          default:
  107.             Usage();    /* never returns */
  108.         }
  109.     }
  110.     if(argc > 1)
  111.         Usage();    /* never returns */
  112.     if(argc == 1 && freopen(*argv, "r", stdin) == NULL) {
  113.         fprintf(stderr, "%s: can't open %s\n", myname, *argv);
  114.         exit(1);
  115.     }
  116.     str = STRalloc();
  117.     if(!STRgets(str, stdin)) {
  118.         fprintf(stderr, "%s: Null input\n", myname);
  119.         exit(1);
  120.     }
  121.     strcat(ucblib, "/");
  122.     strcpy(path, ucblib);
  123.     strcat(path, CONFIG);
  124.     if((fp = fopen(path, "r")) == NULL) {
  125.         fprintf(stderr, "%s: Can't open %s\n", myname, path);
  126.         exit(1);
  127.     }
  128.     time(<ime);
  129.     printf(intro, ctime(<ime));
  130.     do {
  131.         if(ncopies != 0 && STRheadcompare(str, "userdict /#copies ")
  132.          == 0)
  133.             continue;
  134.         if(STRcompare(str, "%%EOF\n") == 0) {
  135. #ifdef SAVE
  136.             if(finale) {
  137.                 fputs(finale, stdout);
  138.                 finale = NULL;
  139.             }
  140. #endif SAVE
  141.             STRputs(str, stdout);
  142.             continue;
  143.         }
  144.         if(STRheadcompare(str, "%%IncludeProcSet:") == 0) {
  145.             for(cp = (char *)&str->bufptr[17] ; ; cp++) {
  146.                 if(!*cp) {
  147.                     fprintf(stderr,
  148.                  "%s: Syntax error on IncludeProcSet line\n",
  149.                      myname);
  150.                     exit(1);
  151.                 }
  152.                 if(!isascii(*cp) || !isspace(*cp))
  153.                     break;
  154.             }
  155.             pp = (char *)str->curendptr;
  156.             while(--pp >= cp) {
  157.                 if(!isascii(*pp) || !isspace(*pp))
  158.                     break;
  159.                 *pp = 0;
  160.             }
  161.             str->curendptr = (unsigned char *)(pp + 1);
  162.             fseek(fp, 0L, 0);
  163.             for( ; ; ) {
  164.                 if(!fgets(line, BUFSIZ, fp)) {
  165.                     fprintf(stderr,
  166.                      "%s: Unknown IncludeProcSet %s\n",
  167.                      myname, cp);
  168.                     exit(1);
  169.                 }
  170.                 if(*line == '#')
  171.                     continue;
  172.                 if(pp = index(line, '\n')) {
  173.                     if(pp == line)
  174.                         continue;
  175.                     *pp = 0;
  176.                 }
  177.                 if(!(pp = index(line, '\t'))) {
  178.                     fprintf(stderr,
  179.                      "%s: Syntax error in macps.config\n",
  180.                      myname);
  181.                     exit(1);
  182.                 }
  183.                 *pp++ = 0;
  184.                 if(STRcompareptr(str, cp, line) == 0)
  185.                     break;
  186.             }
  187.             if(*pp == '/')
  188.                 strcpy(path, pp);
  189.             else {
  190.                 strcpy(path, ucblib);
  191.                 strcat(path, pp);
  192.             }
  193.             fflush(stdout);
  194.             if((fd = open(path, O_RDONLY, 0)) < 0) {
  195.                 fprintf(stderr, "%s: Can't open %s\n", myname,
  196.                  path);
  197.                 exit(1);
  198.             }
  199.             while((i = read(fd, line, BUFSIZ)) > 0)
  200.                 write(1, line, i);
  201.             close(fd);
  202.             continue;
  203.         }
  204.         STRputs(str, stdout);
  205.         if(ncopies > 1 && isascii(*str->bufptr) &&
  206.          isdigit(*str->bufptr)) {
  207.             cp = (char *)str->bufptr;
  208.             while(cp < (char *)str->curendptr && isascii(*cp)
  209.              && isdigit(*cp))
  210.                 cp++;
  211.             if((char *)str->curendptr - cp == 4 &&
  212.              STRcompareptr(str, cp, " mf\n") == 0) {
  213.                 printf("userdict /#copies %d put\n", ncopies);
  214.                 ncopies = -1;
  215.             }
  216.         }
  217.     } while(STRgets(str, stdin));
  218. #ifdef SAVE
  219.     if(finale)
  220.         fputs(finale, stdout);
  221. #endif SAVE
  222.     exit(0);
  223. }
  224.  
  225. Usage()
  226. {
  227.     fputs("Usage: macps [-c #] [-d directory] [-r] [file]\n", stderr);
  228.     exit(1);
  229. }
  230.