// @(#)main.cc 1.4 93/08/01 // // Takes a mail file, a news article or an ordinary file // and pretty prints it on a Postscript printer. // // Copyright (c) Steve Holden and Rich Burridge. // All rights reserved. // // Permission is given to distribute these sources, as long as the // copyright messages are not removed, and no monies are exchanged. // // No responsibility is taken for any errors inherent either // to the comments or the code of this program, but if reported // to me then an attempt will be made to fix them. #include using namespace std; #include #include #include #include #include "header.hh" #include "input.hh" #include "print.hh" int main(int, char **) ; static void printfile(void) ; static void process_postscript(void) ; Header hdr ; Input input ; Print prt ; int main(int argc, char **argv) { int n = 0 ; // Index into namelist array. hdr = Header() ; input = Input() ; prt = Print() ; input.get_options(argc, argv) ; // Read and process command line options. if (input.proname[0] == '\0') (void) sprintf(input.proname, "%s/%s", input.prologue, input.ptype) ; if (!input.numnames) { if (input.toprinter) prt.ofp = prt.makecmd("stdin") ; else prt.ofp = stdout ; // Send output to standard output. prt.show_prologue(input.proname) ; // Send prologue file to output. input.ifp = stdin ; // Get input from standard input. (void) strcpy(prt.curfname, "stdin") ; printfile() ; // Pretty print *just* standard input. } else while (n < input.numnames) { (void) strcpy(prt.curfname, input.namelist[n]) ; if (input.toprinter) prt.ofp = prt.makecmd(prt.curfname) ; else prt.ofp = stdout ; // Send output to standard output. if (n == 0 || input.toprinter) prt.show_prologue(input.proname) ; // Send prologue file to output. if ((input.ifp = fopen(prt.curfname, "r")) == NULL) { cerr << input.progname << ": cannot open " << prt.curfname << "\n" ; continue ; } prt.colct = 0 ; prt.pageno = 1 ; // Initialise current page number. input.end_of_file = 0 ; // Reset in case more files to print. hdr.reset_headers() ; printfile() ; // Pretty print current file. n++ ; } prt.show_trailer() ; // Send trailer file to output. exit(0) ; /*NOTREACHED*/ } static void printfile(void) // Create PostScript to pretty print the current file. { int blankslate ; // Nothing set up for printing. int eop ; // Set if ^L (form-feed) found. if (input.number) input.linenum = 0 ; // Reset line counter. input.readline() ; if (input.end_of_file) { cerr << "mp: empty input file, nothing printed\n" ; exit(1) ; } if (!input.text_doc) hdr.parse_headers(0) ; // Parse headers of mail or news article. input.init_setup() ; // Set values for remaining globals. prt.start(IS_FILE) ; prt.start(IS_PAGE) ; // Output initial definitions. blankslate = 0 ; eop = 0 ; // Print the document. if (input.doc_type != DO_TEXT) hdr.show_headers(0) ; while (!input.end_of_file) { if (blankslate) { prt.start(IS_FILE) ; prt.start(IS_PAGE) ; // Output initial definitions. blankslate = 0 ; } if (input.content && input.folder && input.mlen <= 0) { // If the count has gone negative, then the Content-Length is wrong, so go // back to looking for "\nFrom". if (input.mlen < 0) input.content = 0 ; else if ((hdr.hdr_equal(FROM_HDR) || hdr.hdr_equal(FROMHDR)) && isupper(input.nextline[0])) { eop = 0 ; prt.linect = input.plen ; hdr.reset_headers() ; hdr.parse_headers(0) ; hdr.show_headers(0) ; } else input.content = 0 ; } if (!input.content && input.folder && (!input.elm_if && hdr.hdr_equal(FROM_HDR) || input.elm_if && hdr.hdr_equal(FROMHDR)) && isupper(input.nextline[0])) { eop = 0 ; prt.linect = input.plen ; hdr.reset_headers() ; hdr.parse_headers(0) ; hdr.show_headers(0) ; } if (input.digest && (hdr.hdr_equal(FROMHDR) || hdr.hdr_equal(DATEHDR) || hdr.hdr_equal(SUBJECTHDR)) && isupper(input.nextline[0])) { prt.linect = input.plen ; hdr.parse_headers(1) ; hdr.show_headers(1) ; } if (input.print_ps && hdr.hdr_equal(POSTSCRIPT_MAGIC)) { if (input.numcols) prt.end(IS_COL) ; prt.end(IS_PAGE) ; prt.end(IS_FILE) ; process_postscript() ; blankslate = 1 ; } else if (input.folder && input.end_of_page) eop = 1 ; else { if (eop == 1) input.end_of_page = 1 ; prt.show_text(T_PLAIN, NULL, input.nextline) ; eop = 0 ; } if (input.content) input.mlen -= input.clen ; input.readline() ; } if (!blankslate) { if (input.numcols) prt.end(IS_COL) ; prt.end(IS_PAGE) ; prt.end(IS_FILE) ; } (void) fclose(input.ifp) ; } static void process_postscript(void) { int firstline = 1 ; // To allow a newline after the first line. prt.start(IS_PAGE) ; while (!hdr.hdr_equal(FROMHDR) && !hdr.hdr_equal(DATEHDR) && !hdr.hdr_equal(SUBJECTHDR) && !input.end_of_file) { (void) fputs(input.nextline, prt.ofp) ; if (firstline) (void) fputs("\n", prt.ofp) ; firstline = 0 ; if (fgets(input.nextline, MAXLINE, input.ifp) == NULL) input.end_of_file = 1 ; } prt.end(IS_PAGE) ; }