1
2 // @(#)main.cc 1.4 93/08/01
3 //
4 // Takes a mail file, a news article or an ordinary file
5 // and pretty prints it on a Postscript printer.
6 //
7 // Copyright (c) Steve Holden and Rich Burridge.
8 // All rights reserved.
9 //
10 // Permission is given to distribute these sources, as long as the
11 // copyright messages are not removed, and no monies are exchanged.
12 //
13 // No responsibility is taken for any errors inherent either
14 // to the comments or the code of this program, but if reported
15 // to me then an attempt will be made to fix them.
16
17 #include <iostream>
18 using namespace std;
19 #include <stdio.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <ctype.h>
23 #include "header.hh"
24 #include "input.hh"
25 #include "print.hh"
26
27 int main(int, char **) ;
28
29 static void printfile(void) ;
30 static void process_postscript(void) ;
31
32 Header hdr ;
33 Input input ;
34 Print prt ;
35
36
37 int
main(int argc,char ** argv)38 main(int argc, char **argv)
39 {
40 int n = 0 ; // Index into namelist array.
41
42 hdr = Header() ;
43 input = Input() ;
44 prt = Print() ;
45
46 input.get_options(argc, argv) ; // Read and process command line options.
47
48 if (input.proname[0] == '\0')
49 (void) sprintf(input.proname, "%s/%s", input.prologue, input.ptype) ;
50
51 if (!input.numnames)
52 {
53 if (input.toprinter) prt.ofp = prt.makecmd("stdin") ;
54 else prt.ofp = stdout ; // Send output to standard output.
55 prt.show_prologue(input.proname) ; // Send prologue file to output.
56 input.ifp = stdin ; // Get input from standard input.
57 (void) strcpy(prt.curfname, "stdin") ;
58 printfile() ; // Pretty print *just* standard input.
59 }
60 else
61 while (n < input.numnames)
62 {
63 (void) strcpy(prt.curfname, input.namelist[n]) ;
64 if (input.toprinter) prt.ofp = prt.makecmd(prt.curfname) ;
65 else prt.ofp = stdout ; // Send output to standard output.
66 if (n == 0 || input.toprinter)
67 prt.show_prologue(input.proname) ; // Send prologue file to output.
68 if ((input.ifp = fopen(prt.curfname, "r")) == NULL)
69 {
70 cerr << input.progname << ": cannot open " <<
71 prt.curfname << "\n" ;
72 continue ;
73 }
74 prt.colct = 0 ;
75 prt.pageno = 1 ; // Initialise current page number.
76 input.end_of_file = 0 ; // Reset in case more files to print.
77 hdr.reset_headers() ;
78 printfile() ; // Pretty print current file.
79 n++ ;
80 }
81 prt.show_trailer() ; // Send trailer file to output.
82 exit(0) ;
83 /*NOTREACHED*/
84 }
85
86
87 static void
printfile(void)88 printfile(void) // Create PostScript to pretty print the current file.
89 {
90 int blankslate ; // Nothing set up for printing.
91 int eop ; // Set if ^L (form-feed) found.
92
93 if (input.number) input.linenum = 0 ; // Reset line counter.
94 input.readline() ;
95 if (input.end_of_file)
96 {
97 cerr << "mp: empty input file, nothing printed\n" ;
98 exit(1) ;
99 }
100
101 if (!input.text_doc)
102 hdr.parse_headers(0) ; // Parse headers of mail or news article.
103 input.init_setup() ; // Set values for remaining globals.
104
105 prt.start(IS_FILE) ;
106 prt.start(IS_PAGE) ; // Output initial definitions.
107 blankslate = 0 ;
108 eop = 0 ;
109
110 // Print the document.
111
112 if (input.doc_type != DO_TEXT) hdr.show_headers(0) ;
113
114 while (!input.end_of_file)
115 {
116 if (blankslate)
117 {
118 prt.start(IS_FILE) ;
119 prt.start(IS_PAGE) ; // Output initial definitions.
120 blankslate = 0 ;
121 }
122
123 if (input.content && input.folder && input.mlen <= 0)
124 {
125
126 // If the count has gone negative, then the Content-Length is wrong, so go
127 // back to looking for "\nFrom".
128
129 if (input.mlen < 0) input.content = 0 ;
130 else if ((hdr.hdr_equal(FROM_HDR) || hdr.hdr_equal(FROMHDR)) &&
131 isupper(input.nextline[0]))
132 {
133 eop = 0 ;
134 prt.linect = input.plen ;
135 hdr.reset_headers() ;
136 hdr.parse_headers(0) ;
137 hdr.show_headers(0) ;
138 }
139 else input.content = 0 ;
140 }
141
142 if (!input.content && input.folder &&
143 (!input.elm_if && hdr.hdr_equal(FROM_HDR) ||
144 input.elm_if && hdr.hdr_equal(FROMHDR)) &&
145 isupper(input.nextline[0]))
146 {
147 eop = 0 ;
148 prt.linect = input.plen ;
149 hdr.reset_headers() ;
150 hdr.parse_headers(0) ;
151 hdr.show_headers(0) ;
152 }
153 if (input.digest &&
154 (hdr.hdr_equal(FROMHDR) || hdr.hdr_equal(DATEHDR) ||
155 hdr.hdr_equal(SUBJECTHDR)) && isupper(input.nextline[0]))
156 {
157 prt.linect = input.plen ;
158 hdr.parse_headers(1) ;
159 hdr.show_headers(1) ;
160 }
161
162 if (input.print_ps && hdr.hdr_equal(POSTSCRIPT_MAGIC))
163 {
164 if (input.numcols) prt.end(IS_COL) ;
165 prt.end(IS_PAGE) ;
166 prt.end(IS_FILE) ;
167 process_postscript() ;
168 blankslate = 1 ;
169 }
170 else if (input.folder && input.end_of_page) eop = 1 ;
171 else
172 {
173 if (eop == 1) input.end_of_page = 1 ;
174 prt.show_text(T_PLAIN, NULL, input.nextline) ;
175 eop = 0 ;
176 }
177
178 if (input.content) input.mlen -= input.clen ;
179
180 input.readline() ;
181 }
182
183 if (!blankslate)
184 {
185 if (input.numcols) prt.end(IS_COL) ;
186 prt.end(IS_PAGE) ;
187 prt.end(IS_FILE) ;
188 }
189 (void) fclose(input.ifp) ;
190 }
191
192
193 static void
process_postscript(void)194 process_postscript(void)
195 {
196 int firstline = 1 ; // To allow a newline after the first line.
197
198 prt.start(IS_PAGE) ;
199 while (!hdr.hdr_equal(FROMHDR) && !hdr.hdr_equal(DATEHDR) &&
200 !hdr.hdr_equal(SUBJECTHDR) && !input.end_of_file)
201 {
202 (void) fputs(input.nextline, prt.ofp) ;
203 if (firstline) (void) fputs("\n", prt.ofp) ;
204 firstline = 0 ;
205 if (fgets(input.nextline, MAXLINE, input.ifp) == NULL)
206 input.end_of_file = 1 ;
207 }
208 prt.end(IS_PAGE) ;
209 }
210