/* * * postdaisy - PostScript translator for Diablo 1640 files. * * A program that translates Diablo 1640 files into PostScript. Absolutely nothing * is guaranteed. Quite a few things haven't been implemented, and what's been * done isn't well tested. Most of the documentation used to write this program * was taken from the 'Diablo Emulator' section of a recent Imagen manual. * * Some of document comments that are generated may not be right. Most of the test * files I used produced a trailing blank page. I've put a check in formfeed() that * won't print the last page if it doesn't contain any text, but PAGES comments may * not be right. The DOCUMENTFONTS comment will also be wrong if auto underline or * bold printing have been turned on by escape commands. * * The brute force approach used to implement horizontal and vertical tabs leaves * much to be desired, and may not work for very small initial hmi and vmi values. * At the very least I should have used malloc() to get space for the two tabstop * arrays after hmi and vmi are known! * * Reverse printing mode hasn't been tested at all, but what's here should be * close even though it's not efficient. * * The PostScript prologue is copied from *prologue before any of the input files * are translated. The program expects that the following PostScript procedures * are defined in that file: * * setup * * mark ... setup - * * Handles special initialization stuff that depends on how this program * was called. Expects to find a mark followed by key/value pairs on the * stack. The def operator is applied to each pair up to the mark, then * the default state is set up. * * pagesetup * * page pagesetup - * * Does whatever is needed to set things up for the next page. Expects to * find the current page number on the stack. * * t * * mark str1 x1 str2 x2 ... strn xn y hmi t mark * * Handles all the text on the stack. Characters in the strings are * printed using hmi as the character advance, and all strings are at * vertical position y. Each string is begins at the horizontal position * that preceeds it. * * f * * font f - * * Use font f, where f is the full PostScript font name. Only used when * we switch to auto underline (Courier-Italic) or bold (Courier-Bold) * printing. * * done * * done * * Makes sure the last page is printed. Only needed when we're printing * more than one page on each sheet of paper. * * Many default values, like the magnification and orientation, are defined in * the prologue, which is where they belong. If they're changed (by options), an * appropriate definition is made after the prologue is added to the output file. * The -P option passes arbitrary PostScript through to the output file. Among * other things it can be used to set (or change) values that can't be accessed by * other options. * */ #include <stdio.h>#include <signal.h>#include <ctype.h>#include <fcntl.h> #include "comments.h" /* PostScript file structuring comments */#include "gen.h" /* general purpose definitions */#include "path.h" /* for the prologue */#include "ext.h" /* external variable declarations */#include "postdaisy.h" /* a few special definitions */ char *optnames = "a:c:f:h:l:m:n:o:p:r:s:v:x:y:A:C:E:J:L:P:DI"; char *prologue = POSTDAISY; /* default PostScript prologue */char *formfile = FORMFILE; /* stuff for multiple pages per sheet */ int formsperpage = 1; /* page images on each piece of paper */int copies = 1; /* and this many copies of each sheet */ char htabstops[COLUMNS]; /* horizontal */char vtabstops[ROWS]; /* and vertical tabs */ int res = RES; /* input file resolution - sort of */ int hmi = HMI; /* horizontal motion index - 1/120 inch */int vmi = VMI; /* vertical motion index - 1/48 inch */int ohmi = HMI; /* original hmi */int ovmi = VMI; /* and vmi - for tabs and char size */ int hpos = 0; /* current horizontal */int vpos = 0; /* and vertical position */ int lastx = -1; /* printer's last horizontal */int lasty = -1; /* and vertical position */int lasthmi = -1; /* hmi for current text strings */ int lastc = -1; /* last printed character */int prevx = -1; /* at this position */ int leftmargin = LEFTMARGIN; /* page margins */int rightmargin = RIGHTMARGIN;int topmargin = TOPMARGIN;int bottommargin = BOTTOMMARGIN; int stringcount = 0; /* number of strings on the stack */int stringstart = 1; /* column where current one starts */int advance = 1; /* -1 if in backward print mode */ int lfiscr = OFF; /* line feed implies carriage return */int crislf = OFF; /* carriage return implies line feed */ int linespp = 0; /* lines per page if it's positive */int markedpage = FALSE; /* helps prevent trailing blank page */int page = 0; /* page we're working on */int printed = 0; /* printed this many pages */ Fontmap fontmap[] = FONTMAP; /* for translating font names */char *fontname = "Courier"; /* use this PostScript font */int shadowprint = OFF; /* automatic bold printing if ON */ FILE *fp_in; /* read from this file */FILE *fp_out = stdout; /* and write stuff here */FILE *fp_acct = NULL; /* for accounting data */ /*****************************************************************************/ main(agc, agv) int agc; char *agv[]; { /* * * A simple program that translates Diablo 1640 files into PostScript. Nothing * is guaranteed - the program not well tested and doesn't implement everything. * */ argc = agc; /* other routines may want them */ argv = agv; prog_name = argv[0]; /* really just for error messages */ init_signals(); /* sets up interrupt handling */ header(); /* PostScript header comments */ options(); /* handle the command line options */ setup(); /* for PostScript */ arguments(); /* followed by each input file */ done(); /* print the last page etc. */ account(); /* job accounting data */ exit(x_stat); /* not much could be wrong */ } /* End of main */ /*****************************************************************************/ init_signals() { int interrupt(); /* signal handler */ /* * * Makes sure we handle interrupts. * */ if ( signal(SIGINT, interrupt) == SIG_IGN ) { signal(SIGINT, SIG_IGN); signal(SIGQUIT, SIG_IGN); signal(SIGHUP, SIG_IGN); } else { signal(SIGHUP, interrupt); signal(SIGQUIT, interrupt); } /* End else */ signal(SIGTERM, interrupt); } /* End of init_signals */ /*****************************************************************************/ header() { int ch; /* return value from getopt() */ int old_optind = optind; /* for restoring optind - should be 1 */ /* * * Scans the option list looking for things, like the prologue file, that we need * right away but could be changed from the default. Doing things this way is an * attempt to conform to Adobe's latest file structuring conventions. In particular * they now say there should be nothing executed in the prologue, and they have * added two new comments that delimit global initialization calls. Once we know * where things really are we write out the job header, follow it by the prologue, * and then add the ENDPROLOG and BEGINSETUP comments. * */ while ( (ch = getopt(argc, argv, optnames)) != EOF ) if ( ch == 'L' ) prologue = optarg; else if ( ch == '?' ) error(FATAL, ""); optind = old_optind; /* get ready for option scanning */ fprintf(stdout, "%s", CONFORMING); fprintf(stdout, "%s %s\n", VERSION, PROGRAMVERSION); fprintf(stdout, "%s %s\n", DOCUMENTFONTS, ATEND); fprintf(stdout, "%s %s\n", PAGES, ATEND); fprintf(stdout, "%s", ENDCOMMENTS); if ( cat(prologue) == FALSE ) error(FATAL, "can't read %s", prologue); if ( DOROUND ) cat(ROUNDPAGE); fprintf(stdout, "%s", ENDPROLOG); fprintf(stdout, "%s", BEGINSETUP); fprintf(stdout, "mark\n"); } /* End of header */ /*****************************************************************************/ options() { int ch; /* return value from getopt() */ int n; /* for CR and LF modes */ /* * * Reads and processes the command line options. Added the -P option so arbitrary * PostScript code can be passed through. Expect it could be useful for changing * definitions in the prologue for which options have not been defined. * * Although any PostScript font can be used, things will only work for constant * width fonts. * */ while ( (ch = getopt(argc, argv, optnames)) != EOF ) { switch ( ch ) { case 'a': /* aspect ratio */ fprintf(stdout, "/aspectratio %s def\n", optarg); break; case 'c': /* copies */ copies = atoi(optarg); fprintf(stdout, "/#copies %s store\n", optarg); break; case 'f': /* use this PostScript font */ fontname = get_font(optarg); fprintf(stdout, "/font /%s def\n", fontname); break; case 'h': /* default character spacing */ ohmi = hmi = atoi(optarg) * HSCALE; fprintf(stdout, "/hmi %s def\n", optarg); break; case 'l': /* lines per page */ linespp = atoi(optarg); break; case 'm': /* magnification */ fprintf(stdout, "/magnification %s def\n", optarg); break; case 'n': /* forms per page */ formsperpage = atoi(optarg); fprintf(stdout, "%s %s\n", FORMSPERPAGE, optarg); fprintf(stdout, "/formsperpage %s def\n", optarg); break; case 'o': /* output page list */ out_list(optarg); break; case 'p': /* landscape or portrait mode */ if ( *optarg == 'l' ) fprintf(stdout, "/landscape true def\n"); else fprintf(stdout, "/landscape false def\n"); break; case 'r': /* set CR and LF modes */ n = atoi(optarg); if ( n & 01 ) lfiscr = ON; else lfiscr = OFF; if ( n & 02 ) crislf = ON; else crislf = OFF; break; case 's': /* point size */ fprintf(stdout, "/pointsize %s def\n", optarg); break; case 'v': /* default line spacing */ ovmi = vmi = atoi(optarg) * VSCALE; break; case 'x': /* shift things horizontally */ fprintf(stdout, "/xoffset %s def\n", optarg); break; case 'y': /* and vertically on the page */ fprintf(stdout, "/yoffset %s def\n", optarg); break; case 'A': /* force job accounting */ case 'J': if ( (fp_acct = fopen(optarg, "a")) == NULL ) error(FATAL, "can't open accounting file %s", optarg); break; case 'C': /* copy file straight to output */ if ( cat(optarg) == FALSE ) error(FATAL, "can't read %s", optarg); break; case 'E': /* text font encoding */ fontencoding = optarg; break; case 'L': /* PostScript prologue file */ prologue = optarg; break; case 'P': /* PostScript pass through */ fprintf(stdout, "%s\n", optarg); break; case 'R': /* special global or page level request */ saverequest(optarg); break; case 'D': /* debug flag */ debug = ON; break; case 'I': /* ignore FATAL errors */ ignore = ON; break; case '?': /* don't understand the option */ error(FATAL, ""); break; default: /* don't know what to do for ch */ error(FATAL, "missing case for option %c\n", ch); break; } /* End switch */ } /* End while */ argc -= optind; /* get ready for non-option args */ argv += optind; } /* End of options */ /*****************************************************************************/ char *get_font(name) char *name; /* name the user asked for */ { int i; /* for looking through fontmap[] */ /* * * Called from options() to map a user's font name into a legal PostScript name. * If the lookup fails *name is returned to the caller. That should let you choose * any PostScript font, although things will only work well for constant width * fonts. * */ for ( i = 0; fontmap[i].name != NULL; i++ ) if ( strcmp(name, fontmap[i].name) == 0 ) return(fontmap[i].val); return(name); } /* End of get_font */ /*****************************************************************************/ setup() { /* * * Handles things that must be done after the options are read but before the * input files are processed. * */ writerequest(0, stdout); /* global requests eg. manual feed */ setencoding(fontencoding); fprintf(stdout, "setup\n"); if ( formsperpage > 1 ) { if ( cat(formfile) == FALSE ) error(FATAL, "can't read %s", formfile); fprintf(stdout, "%d setupforms\n", formsperpage); } /* End if */ fprintf(stdout, "%s", ENDSETUP); } /* End of setup */ /*****************************************************************************/ arguments() { /* * * Makes sure all the non-option command line arguments are processed. If we get * here and there aren't any arguments left, or if '-' is one of the input files * we'll process stdin. * */ fp_in = stdin; if ( argc < 1 ) text(); else { /* at least one argument is left */ while ( argc > 0 ) { if ( strcmp(*argv, "-") == 0 ) fp_in = stdin; else if ( (fp_in = fopen(*argv, "r")) == NULL ) error(FATAL, "can't open %s", *argv); text(); if ( fp_in != stdin ) fclose(fp_in); argc--; argv++; } /* End while */ } /* End else */ } /* End of arguments */ /*****************************************************************************/ done() { /* * * Finished with all the input files, so mark the end of the pages, make sure the * last page is printed, and restore the initial environment. * */ fprintf(stdout, "%s", TRAILER); fprintf(stdout, "done\n"); fprintf(stdout, "%s %s\n", DOCUMENTFONTS, fontname); fprintf(stdout, "%s %d\n", PAGES, printed); } /* End of done */ /*****************************************************************************/ account() Lines 1–500 of 1222. Load more
/* * * postdaisy - PostScript translator for Diablo 1640 files. * * A program that translates Diablo 1640 files into PostScript. Absolutely nothing * is guaranteed. Quite a few things haven't been implemented, and what's been * done isn't well tested. Most of the documentation used to write this program * was taken from the 'Diablo Emulator' section of a recent Imagen manual. * * Some of document comments that are generated may not be right. Most of the test * files I used produced a trailing blank page. I've put a check in formfeed() that * won't print the last page if it doesn't contain any text, but PAGES comments may * not be right. The DOCUMENTFONTS comment will also be wrong if auto underline or * bold printing have been turned on by escape commands. * * The brute force approach used to implement horizontal and vertical tabs leaves * much to be desired, and may not work for very small initial hmi and vmi values. * At the very least I should have used malloc() to get space for the two tabstop * arrays after hmi and vmi are known! * * Reverse printing mode hasn't been tested at all, but what's here should be * close even though it's not efficient. * * The PostScript prologue is copied from *prologue before any of the input files * are translated. The program expects that the following PostScript procedures * are defined in that file: * * setup * * mark ... setup - * * Handles special initialization stuff that depends on how this program * was called. Expects to find a mark followed by key/value pairs on the * stack. The def operator is applied to each pair up to the mark, then * the default state is set up. * * pagesetup * * page pagesetup - * * Does whatever is needed to set things up for the next page. Expects to * find the current page number on the stack. * * t * * mark str1 x1 str2 x2 ... strn xn y hmi t mark * * Handles all the text on the stack. Characters in the strings are * printed using hmi as the character advance, and all strings are at * vertical position y. Each string is begins at the horizontal position * that preceeds it. * * f * * font f - * * Use font f, where f is the full PostScript font name. Only used when * we switch to auto underline (Courier-Italic) or bold (Courier-Bold) * printing. * * done * * done * * Makes sure the last page is printed. Only needed when we're printing * more than one page on each sheet of paper. * * Many default values, like the magnification and orientation, are defined in * the prologue, which is where they belong. If they're changed (by options), an * appropriate definition is made after the prologue is added to the output file. * The -P option passes arbitrary PostScript through to the output file. Among * other things it can be used to set (or change) values that can't be accessed by * other options. * */ #include <stdio.h> #include <signal.h> #include <ctype.h> #include <fcntl.h> #include "comments.h" /* PostScript file structuring comments */ #include "gen.h" /* general purpose definitions */ #include "path.h" /* for the prologue */ #include "ext.h" /* external variable declarations */ #include "postdaisy.h" /* a few special definitions */ char *optnames = "a:c:f:h:l:m:n:o:p:r:s:v:x:y:A:C:E:J:L:P:DI"; char *prologue = POSTDAISY; /* default PostScript prologue */ char *formfile = FORMFILE; /* stuff for multiple pages per sheet */ int formsperpage = 1; /* page images on each piece of paper */ int copies = 1; /* and this many copies of each sheet */ char htabstops[COLUMNS]; /* horizontal */ char vtabstops[ROWS]; /* and vertical tabs */ int res = RES; /* input file resolution - sort of */ int hmi = HMI; /* horizontal motion index - 1/120 inch */ int vmi = VMI; /* vertical motion index - 1/48 inch */ int ohmi = HMI; /* original hmi */ int ovmi = VMI; /* and vmi - for tabs and char size */ int hpos = 0; /* current horizontal */ int vpos = 0; /* and vertical position */ int lastx = -1; /* printer's last horizontal */ int lasty = -1; /* and vertical position */ int lasthmi = -1; /* hmi for current text strings */ int lastc = -1; /* last printed character */ int prevx = -1; /* at this position */ int leftmargin = LEFTMARGIN; /* page margins */ int rightmargin = RIGHTMARGIN; int topmargin = TOPMARGIN; int bottommargin = BOTTOMMARGIN; int stringcount = 0; /* number of strings on the stack */ int stringstart = 1; /* column where current one starts */ int advance = 1; /* -1 if in backward print mode */ int lfiscr = OFF; /* line feed implies carriage return */ int crislf = OFF; /* carriage return implies line feed */ int linespp = 0; /* lines per page if it's positive */ int markedpage = FALSE; /* helps prevent trailing blank page */ int page = 0; /* page we're working on */ int printed = 0; /* printed this many pages */ Fontmap fontmap[] = FONTMAP; /* for translating font names */ char *fontname = "Courier"; /* use this PostScript font */ int shadowprint = OFF; /* automatic bold printing if ON */ FILE *fp_in; /* read from this file */ FILE *fp_out = stdout; /* and write stuff here */ FILE *fp_acct = NULL; /* for accounting data */ /*****************************************************************************/ main(agc, agv) int agc; char *agv[]; { /* * * A simple program that translates Diablo 1640 files into PostScript. Nothing * is guaranteed - the program not well tested and doesn't implement everything. * */ argc = agc; /* other routines may want them */ argv = agv; prog_name = argv[0]; /* really just for error messages */ init_signals(); /* sets up interrupt handling */ header(); /* PostScript header comments */ options(); /* handle the command line options */ setup(); /* for PostScript */ arguments(); /* followed by each input file */ done(); /* print the last page etc. */ account(); /* job accounting data */ exit(x_stat); /* not much could be wrong */ } /* End of main */ /*****************************************************************************/ init_signals() { int interrupt(); /* signal handler */ /* * * Makes sure we handle interrupts. * */ if ( signal(SIGINT, interrupt) == SIG_IGN ) { signal(SIGINT, SIG_IGN); signal(SIGQUIT, SIG_IGN); signal(SIGHUP, SIG_IGN); } else { signal(SIGHUP, interrupt); signal(SIGQUIT, interrupt); } /* End else */ signal(SIGTERM, interrupt); } /* End of init_signals */ /*****************************************************************************/ header() { int ch; /* return value from getopt() */ int old_optind = optind; /* for restoring optind - should be 1 */ /* * * Scans the option list looking for things, like the prologue file, that we need * right away but could be changed from the default. Doing things this way is an * attempt to conform to Adobe's latest file structuring conventions. In particular * they now say there should be nothing executed in the prologue, and they have * added two new comments that delimit global initialization calls. Once we know * where things really are we write out the job header, follow it by the prologue, * and then add the ENDPROLOG and BEGINSETUP comments. * */ while ( (ch = getopt(argc, argv, optnames)) != EOF ) if ( ch == 'L' ) prologue = optarg; else if ( ch == '?' ) error(FATAL, ""); optind = old_optind; /* get ready for option scanning */ fprintf(stdout, "%s", CONFORMING); fprintf(stdout, "%s %s\n", VERSION, PROGRAMVERSION); fprintf(stdout, "%s %s\n", DOCUMENTFONTS, ATEND); fprintf(stdout, "%s %s\n", PAGES, ATEND); fprintf(stdout, "%s", ENDCOMMENTS); if ( cat(prologue) == FALSE ) error(FATAL, "can't read %s", prologue); if ( DOROUND ) cat(ROUNDPAGE); fprintf(stdout, "%s", ENDPROLOG); fprintf(stdout, "%s", BEGINSETUP); fprintf(stdout, "mark\n"); } /* End of header */ /*****************************************************************************/ options() { int ch; /* return value from getopt() */ int n; /* for CR and LF modes */ /* * * Reads and processes the command line options. Added the -P option so arbitrary * PostScript code can be passed through. Expect it could be useful for changing * definitions in the prologue for which options have not been defined. * * Although any PostScript font can be used, things will only work for constant * width fonts. * */ while ( (ch = getopt(argc, argv, optnames)) != EOF ) { switch ( ch ) { case 'a': /* aspect ratio */ fprintf(stdout, "/aspectratio %s def\n", optarg); break; case 'c': /* copies */ copies = atoi(optarg); fprintf(stdout, "/#copies %s store\n", optarg); break; case 'f': /* use this PostScript font */ fontname = get_font(optarg); fprintf(stdout, "/font /%s def\n", fontname); break; case 'h': /* default character spacing */ ohmi = hmi = atoi(optarg) * HSCALE; fprintf(stdout, "/hmi %s def\n", optarg); break; case 'l': /* lines per page */ linespp = atoi(optarg); break; case 'm': /* magnification */ fprintf(stdout, "/magnification %s def\n", optarg); break; case 'n': /* forms per page */ formsperpage = atoi(optarg); fprintf(stdout, "%s %s\n", FORMSPERPAGE, optarg); fprintf(stdout, "/formsperpage %s def\n", optarg); break; case 'o': /* output page list */ out_list(optarg); break; case 'p': /* landscape or portrait mode */ if ( *optarg == 'l' ) fprintf(stdout, "/landscape true def\n"); else fprintf(stdout, "/landscape false def\n"); break; case 'r': /* set CR and LF modes */ n = atoi(optarg); if ( n & 01 ) lfiscr = ON; else lfiscr = OFF; if ( n & 02 ) crislf = ON; else crislf = OFF; break; case 's': /* point size */ fprintf(stdout, "/pointsize %s def\n", optarg); break; case 'v': /* default line spacing */ ovmi = vmi = atoi(optarg) * VSCALE; break; case 'x': /* shift things horizontally */ fprintf(stdout, "/xoffset %s def\n", optarg); break; case 'y': /* and vertically on the page */ fprintf(stdout, "/yoffset %s def\n", optarg); break; case 'A': /* force job accounting */ case 'J': if ( (fp_acct = fopen(optarg, "a")) == NULL ) error(FATAL, "can't open accounting file %s", optarg); break; case 'C': /* copy file straight to output */ if ( cat(optarg) == FALSE ) error(FATAL, "can't read %s", optarg); break; case 'E': /* text font encoding */ fontencoding = optarg; break; case 'L': /* PostScript prologue file */ prologue = optarg; break; case 'P': /* PostScript pass through */ fprintf(stdout, "%s\n", optarg); break; case 'R': /* special global or page level request */ saverequest(optarg); break; case 'D': /* debug flag */ debug = ON; break; case 'I': /* ignore FATAL errors */ ignore = ON; break; case '?': /* don't understand the option */ error(FATAL, ""); break; default: /* don't know what to do for ch */ error(FATAL, "missing case for option %c\n", ch); break; } /* End switch */ } /* End while */ argc -= optind; /* get ready for non-option args */ argv += optind; } /* End of options */ /*****************************************************************************/ char *get_font(name) char *name; /* name the user asked for */ { int i; /* for looking through fontmap[] */ /* * * Called from options() to map a user's font name into a legal PostScript name. * If the lookup fails *name is returned to the caller. That should let you choose * any PostScript font, although things will only work well for constant width * fonts. * */ for ( i = 0; fontmap[i].name != NULL; i++ ) if ( strcmp(name, fontmap[i].name) == 0 ) return(fontmap[i].val); return(name); } /* End of get_font */ /*****************************************************************************/ setup() { /* * * Handles things that must be done after the options are read but before the * input files are processed. * */ writerequest(0, stdout); /* global requests eg. manual feed */ setencoding(fontencoding); fprintf(stdout, "setup\n"); if ( formsperpage > 1 ) { if ( cat(formfile) == FALSE ) error(FATAL, "can't read %s", formfile); fprintf(stdout, "%d setupforms\n", formsperpage); } /* End if */ fprintf(stdout, "%s", ENDSETUP); } /* End of setup */ /*****************************************************************************/ arguments() { /* * * Makes sure all the non-option command line arguments are processed. If we get * here and there aren't any arguments left, or if '-' is one of the input files * we'll process stdin. * */ fp_in = stdin; if ( argc < 1 ) text(); else { /* at least one argument is left */ while ( argc > 0 ) { if ( strcmp(*argv, "-") == 0 ) fp_in = stdin; else if ( (fp_in = fopen(*argv, "r")) == NULL ) error(FATAL, "can't open %s", *argv); text(); if ( fp_in != stdin ) fclose(fp_in); argc--; argv++; } /* End while */ } /* End else */ } /* End of arguments */ /*****************************************************************************/ done() { /* * * Finished with all the input files, so mark the end of the pages, make sure the * last page is printed, and restore the initial environment. * */ fprintf(stdout, "%s", TRAILER); fprintf(stdout, "done\n"); fprintf(stdout, "%s %s\n", DOCUMENTFONTS, fontname); fprintf(stdout, "%s %d\n", PAGES, printed); } /* End of done */ /*****************************************************************************/ account()