home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (c) 1988, The Trustees of the University of California.
- * Edward Moy, Workstation Software Support Group, Workstation Support Serices,
- * Information Systems and Technology.
- *
- * Permission is granted to any individual or institution to use, copy,
- * or redistribute this software so long as it is not sold for profit,
- * provided that this notice and the original copyright notices are
- * retained. The University of California makes no representations about the
- * suitability of this software for any purpose. It is provided "as is"
- * without express or implied warranty.
- */
-
- #ifndef lint
- static char *SCCSid = "@(#)prepfix.c 1.1 9/19/88";
- #endif lint
-
- #include <ctype.h>
- #include <stdio.h>
- #include <strings.h>
-
- #define BIGBUFSIZE 10240
- #define CLEARTOMARK 12
- #define EEXECLEN 80
- #define EXTRA (NZEROLINE * ZEROLINE + CLEARTOMARK)
- #define LINELEN 256
- #define NPRODUCT 16
- #define NZEROLINE 7
- #define PRODUCTLEN 64
- #define ZEROLINE 65
-
- char *Fgets();
- char *match();
- char *myname;
- char lproduct[PRODUCTLEN][NPRODUCT];
- char pproduct[PRODUCTLEN][NPRODUCT];
- char tempname[] = "/tmp/prepfixXXXXXX";
-
- main(argc, argv)
- int argc;
- char **argv;
- {
- register FILE *tp;
- register int i, nl, np;
- register char *lp, *pp;
- char buf[BIGBUFSIZE];
-
- if(myname = rindex(*argv, '/'))
- myname++;
- else
- myname = *argv;
- nl = np = 0;
- for(argc--, argv++ ; argc > 0 && **argv == '-' ; argc--, argv++) {
- switch((*argv)[1]) {
- case 'h':
- usage();
- case 'l':
- if(nl >= NPRODUCT - 1) {
- fprintf(stderr,
- "%s: Too many LW-like product names\n",
- myname);
- exit(1);
- }
- if((*argv)[2])
- lp = &(*argv)[2];
- else {
- if(argc < 2) {
- fprintf(stderr,
- "%s: No argument for -l\n", myname);
- exit(1);
- }
- argc--;
- argv++;
- lp = *argv;
- }
- strncpy(lproduct[nl], lp, PRODUCTLEN - 1);
- lproduct[nl++][PRODUCTLEN - 1] = 0;
- break;
- case 'p':
- if(np >= NPRODUCT - 1) {
- fprintf(stderr,
- "%s: Too many LW+-like product names\n",
- myname);
- exit(1);
- }
- if((*argv)[2])
- pp = &(*argv)[2];
- else {
- if(argc < 2) {
- fprintf(stderr,
- "%s: No argument for -p\n", myname);
- exit(1);
- }
- argc--;
- argv++;
- pp = *argv;
- }
- strncpy(pproduct[np], pp, PRODUCTLEN - 1);
- pproduct[np++][PRODUCTLEN - 1] = 0;
- break;
- }
- }
- if(argc > 1)
- usage();
- if(argc > 0 && freopen(*argv, "r", stdin) == NULL) {
- fprintf(stderr, "%s: can't open %s\n", myname, *argv);
- exit(1);
- }
- mktemp(tempname);
- if((tp = fopen(tempname, "w+")) == NULL) {
- fprintf(stderr, "%s: can't create temp file %s\n",
- myname, tempname);
- exit(1);
- }
- unlink(tempname);
- if(Fgets(buf, BIGBUFSIZE, stdin) == NULL) {
- fprintf(stderr, "%s: Null input\n", myname);
- exit(1);
- }
- for( ; ; ) {
- if(strncmp(buf, "% \251", 3) == 0) {
- fputs("% ", tp);
- buf[0] = '(';
- buf[1] = 'C';
- buf[2] = ')';
- } else if(strncmp(buf, "%%BeginProcSet:", 15) == 0) {
- Fputs(buf, stdout);
- fseek(tp, 0L, 0);
- while((i = fread(buf, 1, BIGBUFSIZE, tp)) > 0)
- fwrite(buf, 1, i, stdout);
- fclose(tp);
- break;
- }
- Fputs(buf, tp);
- if(Fgets(buf, BIGBUFSIZE, stdin) == NULL) {
- fprintf(stderr, "%s: No BeginProcSet\n",
- myname);
- exit(1);
- }
- }
- while(Fgets(buf, BIGBUFSIZE, stdin)) {
- if((nl || np) && strncmp(buf, "/LW{", 4) == 0) {
- Fputs(buf, stdout);
- if(!Fgets(buf, BIGBUFSIZE, stdin)) {
- fprintf(stderr,
- "%s: Unexpected EOF while adding product names\n",
- myname);
- exit(1);
- }
- Fputs(buf, stdout);
- for(i = 0 ; i < nl ; i++)
- printf("LW 0 eq {/LW{save statusdict/product get(%s)anchorsearch exch pop {pop 1}{0}ifelse exch restore}bind def}if\n",
- lproduct[i]);
- for(i = 0 ; i < np ; i++)
- printf("LW 0 eq {/LW{save statusdict/product get(%s)anchorsearch exch pop {pop 2}{0}ifelse exch restore}bind def}if\n",
- pproduct[i]);
- continue;
- } else if(match(buf, "setdefaulttimeouts")
- || match(buf, "setsccinteractive"))
- putchar('%');
- else if(match(buf, "eexec")) {
- if(match(buf, "/stretch")) {
- eexec("stretch", buf);
- continue;
- } else if(match(buf, "/smooth4")) {
- eexec("smooth4", buf);
- continue;
- }
- } else if(strncmp(buf, "%%EndProcSet", 12) == 0) {
- Fputs(buf, stdout);
- break;
- }
- Fputs(buf, stdout);
- }
- exit(0);
- }
-
- char *match(str1, str2)
- register char *str1, *str2;
- {
- register int firstchar = *str2;
- register int len = strlen(str2);
-
- while(*str1) {
- if(*str1 == firstchar && strncmp(str1, str2, len) == 0)
- return(str1);
- str1++;
- }
- return(NULL);
- }
-
- eexec(name, buf)
- char *name;
- register char *buf;
- {
- register int len;
- register char *bp;
-
- if(!Fgets(buf, BIGBUFSIZE, stdin)) {
- fprintf(stderr, "%s: EOF during reading eexec\n", myname);
- exit(1);
- }
- len = strlen(bp = buf) - 1;
- printf("ok userdict/%s known not and{currentfile eexec}{%d{currentfile read pop pop}repeat}ifelse\n",
- name, len + (len / EEXECLEN) + (len % EEXECLEN ? 1 : 0) + EXTRA);
- while(len >= 80) {
- fwrite(bp, 80, 1, stdout);
- putchar('\n');
- bp += 80;
- len -= 80;
- }
- if(len > 0) {
- fwrite(bp, len, 1, stdout);
- putchar('\n');
- }
- for( ; ; ) {
- if(Fgets(buf, BIGBUFSIZE, stdin) == NULL) {
- fprintf(stderr, "%s: EOF reached before cleartomark\n",
- myname);
- exit(1);
- }
- if(strncmp(buf, "cleartomark", 11) == 0) {
- Fputs(buf, stdout);
- return;
- }
- Fputs(buf, stdout);
- }
- }
-
- usage()
- {
- fprintf(stderr, "Usage: %s [-l LW_name] [-p LW+_name] [file]\n",
- myname);
- fprintf(stderr, " %s -help\n", myname);
- exit(1);
- }
-