/* **** CASSETTE **** * * This program parses a cassette cover data file * and produces a postscript program * * Author: Bill Pringle (wrp@ivy.unisys.com) * * Change History: * * 1.0 Original version * * 1.1 Added code to handle DAT covers, thanks to Brian Redman * (ber@bellcore.com) * * Added -i (interractive) option to run easier from windows * Added -o (output file) option * */ #include #include #include "pscover.h" main (int argc, char **argv) { /* produce a cassette cover */ FILE *fp; /* input file pointer */ FILE *ofp=stdout; /* output file pointer */ int optflag=0; /* option flag */ int cov_type='c'; /* default cover is cassette) */ char *p; /* pointer for parsing, perhaps :^) */ /* get program name */ if ((Prog = strrchr(argv[0],DIRSLASH)) == NULL) Prog = argv[0]; else Prog++; #ifdef __MSDOS__ if ((p = strrchr(Prog,'.')) != NULL) *p = '\0'; /* drop .exe extention */ for (p=Prog; *p != '\0'; p++) if (isupper(*p)) *p = tolower(*p); #endif init(); for (argc--, argv++; argc > 0; argc--, argv++) { if (argv[0][0] == '-') switch(argv[0][1]) { /* process option */ case 'c': /* cassette cover (default) */ cov_type = 'c'; break; case 'd': /* DAT cover */ cov_type = 'd'; break; case 'h': /* help option */ do_help(Prog); break; case 'i': /* interractive mode */ while (1) { if ((fp = open_file("input","r")) == NULL) return 0; if ((ofp = open_file("output","w")) == NULL) return 0; pscover(fp,ofp,cov_type,optflag); } case 'o': /* output file */ if (argv[0][2] == '\0') { /* file name is next argument */ argc--; argv++; p = argv[0]; /* file name */ } else { /* file name right after -o */ p = argv[0] + 2; /* file name */ } if ((fp = fopen(p,"w")) == NULL) { /* problems */ fprintf(stderr,"%s: Can't open output file %s\n", Prog, p); return 1; } break; case 't': /* test mode */ optflag = argv[0][1]; break; default: /* bad option */ fprintf(stderr,"%s: Invalid option (%s)\n", Prog, argv[0]); do_usage(Prog); } else { /* not option - must be file name */ if ((fp = fopen(argv[0],"r")) == NULL) { /* problems */ fprintf(stderr,"%s: Can't open input file %s\n", Prog, argv[0]); continue; } pscover(fp,ofp,cov_type,optflag); fputs("WrapUp",ofp); } } return 0; } void pscover(FILE *fp, FILE *ofp, int cov_type, int optflag) { /* parse the input file */ char buf[BUFSIZ]; /* input buffer */ struct keytab *cmd; /* return from find_cmd */ LineNo = 0; /* initialize line counter */ switch (optflag) { /* process as per option */ case 't': /* test */ fprintf(ofp,"%s: Testing input cassette file ...\n", Prog); break; case 0: /* normal */ prologue_ps(ofp); /* parse the file */ /* specify what type of cover */ if (cov_type == 'c') fprintf(ofp,"CassCover\n"); else fprintf(ofp,"DatCover\n"); PageNo = 0; /* initialize page number */ new_page(ofp); /* initialize a new page */ break; default: fprintf(stderr,"%s: Internal error. optflag=%x (%c)\n", Prog, optflag, optflag); } while (get_line(buf,BUFSIZ,fp) != NULL) { /* loop reading the input file */ if (*buf == '%' || *buf == '\0') { /* comment or empty line - just copy */ fputs(buf,ofp); } else if (*buf == '\t' || *buf == ' ') { /* syntax checking - report if found */ if (optflag == 't') copy_line(fp,buf); else print_song(buf, ofp); } else { /* should be a command line */ cmd = find_cmd(buf); /* find command */ if (optflag == 't') parse_cmd(fp,buf,cmd); else cmd -> key_fcn(fp,ofp,buf,cmd); /* process command */ } } if (ofp != stdout) fclose(ofp); /* close if actual file */ } void parse_cmd(FILE *ofp, char *buf, struct keytab *cmd) { /* tell user what command */ fprintf(ofp,"Command found (%s) >>%s<<\n", cmd->key_word, buf); } char * get_line(char *buf, int bsize, FILE *fp) { /* read next line */ char *p; /* pointer for parsing */ if (fgets(buf,bsize,fp) == NULL) return NULL; LineNo++; /* increment line number */ for (p=buf; *p != '\r' && *p != '\n'; p++); *p = '\0'; return buf; } void copy_line(FILE *ofp, char *buf) { fprintf(ofp,"Copying line: "); fputs(buf,ofp); } void do_usage(char *pgm) { fprintf(stderr,"Usage:\t%s [-i] [-t] [files]\n", pgm); } void do_help(char *pgm) { fprintf(stderr,"This program creates postscript programs for cassette covers.\n"); do_usage(pgm); } void new_page(FILE *ofp) { if (PageNo++ > 0) { /* finish previous page */ fputs("WrapUp",ofp); } fprintf(ofp,"%%%%Page: %d %d\n", PageNo, PageNo); fputs("LeftCover",ofp); } FILE * open_file(char *ftype, char *fmode) { /* open a file */ char buf[BUFSIZ]; /* input buffer */ FILE *fp; /* file pointer */ do { /* keep trying */ printf("Enter name of %s file, or press ENTER to quit: ", ftype); gets(buf); if (*buf == '\0') return NULL; if ((fp = fopen(buf,fmode)) == NULL) printf("Couldn't open %s\n", buf); } while (fp == NULL); /* file open */ return fp; }