/* * **** EMBED_PS **** * * This program builds a C program that will output a postscript file * The name of the file to be read is passed. It must end with .ps * The output will be of the same name, but suffixed with a .c * instead of a .ps * * Author: Bill Pringle (wrp@ivy.unisys.com) * * Change History: * * 1.0 Initial release * * 1.1 Added command options: help option & specify name of function * */ #include #include #ifdef __MSDOS__ #define DIRSLASH '\\' #else #define DIRSLASH '/' #endif void embed_ps(char *, char *, FILE *, FILE *); void prologue(char *, char *, FILE *); void do_help(char *); void do_usage(char *); main(int argc, char **argv) { char *Prog; /* name of program */ char *dir; /* directory name */ char buf[BUFSIZ]; /* input buffer */ FILE *infp; /* input file pointer */ FILE *ofp; /* output file pointer */ int len; /* length */ char *fname; /* name of function */ char *p; /* pointer for parsing */ if ((Prog = strrchr(argv[0],DIRSLASH)) == NULL) { /* no slash, use current directory */ Prog = argv[0]; /* program name */ dir = "./"; /* directory name */ } else { *Prog++ = '\0'; /* program name */ dir = argv[0]; /* directory name */ } #ifdef __MSDOS__ if ((p = strrchr(Prog,'.')) != NULL) *p = '\0'; for (p = Prog; *p != '\0'; p++) if (isupper(*p)) *p = tolower(*p); #endif if (argc == 1) { /* no arguments, display help message and quit */ do_help(Prog); return 0; } fname = "prologue_ps"; /* default name of function */ for (argc--, argv++; argc > 0; argc--, argv++) { /* process argument(s) */ if (argv[0][0] == '-') { /* command option - process it */ switch(argv[0][1]) { /* process option itself */ case 'h': /* help */ do_help(Prog); return 0; /* terminate */ case 'n': /* name of function */ if (argv[0][2] == '\0') { /* function name is next argument */ argc--; argv++; /* shift option out */ fname = argv[0]; /* name of function */ } else fname = argv[0] + 2; /* name of function */ break; default: /* bad option */ fprintf(stderr,"%s: Invalid option (%s)\n", Prog, argv[0]); do_usage(Prog); return 1; /* bad option */ } } else { /* input file name given */ if ((infp = fopen(argv[0],"r")) == NULL) { /* not found - try execute directory */ strcpy(buf,dir); /* directory */ len = strlen(buf); /* length of name */ buf[len] = DIRSLASH; /* add directory slash */ strcpy(buf+len+1,argv[0]); /* add file name */ if ((infp = fopen(buf,"r")) == NULL) { /* still not found - give up */ fprintf(stderr,"%s: Can't open file %s\n", Prog, argv[0]); continue; } } strcpy(buf,argv[0]); /* get file name */ len = strlen(buf); /* length of name */ if (strcmp(buf + len - 3, ".ps")) { /* file doesn't end in .ps */ fprintf(stderr,"%s: File (%s) must end in '.ps'\n", Prog, argv[0]); fclose(infp); /* close input file */ continue; } strcpy(buf + len - 3, ".c"); /* c file name */ if ((ofp = fopen(buf,"w")) == NULL) { /* can't open output file */ fprintf(stderr,"%s: Can't open output file %s.\n", Prog, buf); } embed_ps(Prog,fname,infp,ofp); } /* end of processing argument */ } return 0; } void embed_ps(char *Prog,char *fname, FILE *ifp, FILE *ofp) { /* convert PS file ifp into C function fname to file ofp */ int c; /* character read */ /* output prologue */ prologue(Prog, fname, ofp); fprintf(ofp,"fprintf(ofp,\""); while ((c = fgetc(ifp)) != EOF) switch(c) { /* check for output character */ case '\\': fprintf(ofp,"\\\\"); break; case '"': fprintf(ofp,"\\\""); break; case '%': fprintf(ofp,"%%%%"); break; case '\n': fprintf(ofp,"\\n\");\nfprintf(ofp,\""); break; default: fputc(c,ofp); } fprintf(ofp,"\\n\");\n}\n"); } void prologue(char *Prog, char *fname, FILE *ofp) { fprintf(ofp,"/*\t\t\t\t**** %s ****\n", fname); fprintf(ofp,"/* this program was automatically generated by %s. */\n", Prog); fprintf(ofp,"/* you should probably not edit it */\n"); fprintf(ofp,"#include \n"); fprintf(ofp,"void\n%s(FILE *ofp)\n{\n", fname); } void do_help(char *Prog) { /* display help message for user */ int i; /* loop variable */ char *msg[] = { "This program reads a PostScript program and produces a C program that will\n", "\tcreate a duplicate of the original file.\n", "\nThe user may specify a function name (the default is prologue_ps)\n", "\nThe program assumes that the input file has a suffix of `.ps'.\n", "The output file will have the suffix changed to `.c'\n", "\nFor example, the command:\n", "\n", "\t%s -f foo bar.ps\n", "\nwill read the PostScript file bar.ps and create a C source file bar.c\n", "which will contain a function `foo' which, when called, will output the\n", "contents of the original file.\n", NULL}; for (i=0; msg[i] != NULL; i++) fprintf(stderr,msg[i],Prog); do_usage(Prog); } void do_usage(char *Prog) { /* output a usage message */ int i; /* loop index variable */ char *msg[] = { "Usage:\t%s [-f fname] pfile ...\n", "-OR- for help, type:\n" "\t%s -h\n", "Where:\n" "\tfname\tis the name of the C function to be created.\n", "\tpfile\tis the name of the PostScript input file with a suffix of .ps\n", NULL}; for (i=0; msg[i] != NULL; i++) fprintf(stderr,msg[i],Prog); }