📄 postprint.c
字号:
/* * * postprint - PostScript translator for ASCII files. * * A simple program that translates ASCII files into PostScript. All it really * does is expand tabs and backspaces, handle character quoting, print text lines, * and control when pages are started based on the requested number of lines per * page. * * The PostScript prologue is copied from *prologue before any of the input files * are translated. The program expects that the following procedures are defined * in that file: * * setup * * mark ... setup - * * Handles special initialization stuff that depends on how the 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. * * l * * string l - * * Prints string starting in the first column and then goes to the next * line. * * L * * mark string column string column ... L mark * * Prints each string on the stack starting at the horizontal position * selected by column. Used when tabs and spaces can be sufficiently well * compressed to make the printer overhead worthwhile. Always used when * we have to back up. * * LL * * mark string column string column ... LL mark * * Like L, but only used to prevent potential PostScript stack overflow * from too many string/column pairs. Stays on the current line. It will * not be needed often!! * * done * * done * * Makes sure the last page is printed. Only needed when we're printing * more than one page on each sheet of paper. * * Almost everything has been changed in this version of postprint. The program * is more intelligent, especially about tabs, spaces, and backspacing, and as a * result output files usually print faster. Output files also now conform to * Adobe's file structuring conventions, which is undoubtedly something I should * have done in the first version of the program. If the number of lines per page * is set to 0, which can be done using the -l option, pointsize will be used to * guess a reasonable value. The estimate is based on the values of LINESPP, * POINTSIZE, and pointsize, and assumes LINESPP lines would fit on a page if * we printed in size POINTSIZE. Selecting a point size using the -s option and * adding -l0 to the command line forces the guess to be made. * * 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>#ifdef plan9#define isascii(c) ((unsigned char)(c)<=0177)#endif#include <sys/types.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 "postprint.h" /* a few special definitions */char *optnames = "a:c:ef:l:m:n:o:p:r:s:t:x:y:A:C:E:J:L:P:R:DI";char *prologue = POSTPRINT; /* 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 */int linespp = LINESPP; /* number of lines per page */int pointsize = POINTSIZE; /* in this point size */int tabstops = TABSTOPS; /* tabs set at these columns */int crmode = 0; /* carriage return mode - 0, 1, or 2 */int extended = TRUE; /* use escapes for unprintable chars */int col = 1; /* next character goes in this column */int line = 1; /* on this line */int stringcount = 0; /* number of strings on the stack */int stringstart = 1; /* column where current one starts */Fontmap fontmap[] = FONTMAP; /* for translating font names */char *fontname = "Courier"; /* use this PostScript font */int page = 0; /* page we're working on */int printed = 0; /* printed this many pages */FILE *fp_in = stdin; /* 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 ASCII files into PostScript. If there's more * than one input file, each begins on a new page. * */ 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 and prologue */ 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(){/* * * 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() *//* * * 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 well 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 'e': /* obsolete - it's now always on */ extended = TRUE; break; case 'f': /* use this PostScript font */ fontname = get_font(optarg); fprintf(stdout, "/font /%s def\n", fontname); 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': /* carriage return mode */ crmode = atoi(optarg); break; case 's': /* point size */ pointsize = atoi(optarg); fprintf(stdout, "/pointsize %s def\n", optarg); break; case 't': /* tabstops */ tabstops = atoi(optarg); 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(){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -