1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include <stdio.h>
5 #include "common.h"
6 #include "tr2post.h"
7 #include "comments.h"
8 #include "path.h"
9
10 int formsperpage = 1;
11 int picflag = 1;
12 double aspectratio = 1.0;
13 int copies = 1;
14 int landscape = 0;
15 double magnification = 1.0;
16 int linesperpage = 66;
17 int pointsize = 10;
18 double xoffset = .25;
19 double yoffset = .25;
20 char *passthrough = 0;
21
22 Biobuf binp, *bstdout, bstderr;
23 Biobufhdr *Bstdin, *Bstdout, *Bstderr;
24 int debug = 0;
25
26 char tmpfilename[MAXTOKENSIZE];
27 char copybuf[BUFSIZ];
28
29 struct charent **build_char_list = 0;
30 int build_char_cnt = 0;
31
32 void
prologues(void)33 prologues(void) {
34 int i;
35 char charlibname[MAXTOKENSIZE];
36
37 Bprint(Bstdout, "%s", CONFORMING);
38 Bprint(Bstdout, "%s %s\n", VERSION, PROGRAMVERSION);
39 Bprint(Bstdout, "%s %s\n", CREATOR, PROGRAMNAME);
40 Bprint(Bstdout, "%s %s\n", DOCUMENTFONTS, ATEND);
41 Bprint(Bstdout, "%s %s\n", PAGES, ATEND);
42 Bprint(Bstdout, "%s", ENDCOMMENTS);
43
44 if (cat(DPOST)) {
45 fprint(2, "can't read %s\n", DPOST);
46 exits("dpost prologue");
47 }
48
49 if (drawflag) {
50 if (cat(DRAW)) {
51 fprint(2, "can't read %s\n", DRAW);
52 exits("draw prologue");
53 }
54 }
55
56 if (DOROUND)
57 cat(ROUNDPAGE);
58
59 Bprint(Bstdout, "%s", ENDPROLOG);
60 Bprint(Bstdout, "%s", BEGINSETUP);
61 Bprint(Bstdout, "mark\n");
62 if (formsperpage > 1) {
63 Bprint(Bstdout, "%s %d\n", FORMSPERPAGE, formsperpage);
64 Bprint(Bstdout, "/formsperpage %d def\n", formsperpage);
65 }
66 if (aspectratio != 1) Bprint(Bstdout, "/aspectratio %g def\n", aspectratio);
67 if (copies != 1) Bprint(Bstdout, "/#copies %d store\n", copies);
68 if (landscape) Bprint(Bstdout, "/landscape true def\n");
69 if (magnification != 1) Bprint(Bstdout, "/magnification %g def\n", magnification);
70 if (pointsize != 10) Bprint(Bstdout, "/pointsize %d def\n", pointsize);
71 if (xoffset != .25) Bprint(Bstdout, "/xoffset %g def\n", xoffset);
72 if (yoffset != .25) Bprint(Bstdout, "/yoffset %g def\n", yoffset);
73 cat(ENCODINGDIR"/Latin1.enc");
74 if (passthrough != 0) Bprint(Bstdout, "%s\n", passthrough);
75
76 Bprint(Bstdout, "setup\n");
77 if (formsperpage > 1) {
78 cat(FORMFILE);
79 Bprint(Bstdout, "%d setupforms \n", formsperpage);
80 }
81 /* output Build character info from charlib if necessary. */
82
83 for (i=0; i<build_char_cnt; i++) {
84 sprint(charlibname, "%s/%s", CHARLIB, build_char_list[i]->name);
85 if (cat(charlibname))
86 fprint(2, "cannot open %s\n", charlibname);
87 }
88
89 Bprint(Bstdout, "%s", ENDSETUP);
90 }
91
92 void
cleanup(void)93 cleanup(void) {
94 remove(tmpfilename);
95 }
96
97 void
main(int argc,char * argv[])98 main(int argc, char *argv[]) {
99 Biobuf *binp;
100 Biobufhdr *Binp;
101 int i, tot, ifd;
102 char *t;
103
104 programname = argv[0];
105 if (Binit(&bstderr, 2, OWRITE) == Beof)
106 sysfatal("Binit");
107 Bstderr = &bstderr.Biobufhdr;
108
109 tmpnam(tmpfilename);
110 if ((bstdout=Bopen(tmpfilename, OWRITE)) == 0)
111 sysfatal("cannot open temporary file %s: %r", tmpfilename);
112 atexit(cleanup);
113 Bstdout = &bstdout->Biobufhdr;
114
115 ARGBEGIN{
116 case 'a': /* aspect ratio */
117 aspectratio = atof(ARGF());
118 break;
119 case 'c': /* copies */
120 copies = atoi(ARGF());
121 break;
122 case 'd':
123 debug = 1;
124 break;
125 case 'm': /* magnification */
126 magnification = atof(ARGF());
127 break;
128 case 'n': /* forms per page */
129 formsperpage = atoi(ARGF());
130 break;
131 case 'o': /* output page list */
132 pagelist(ARGF());
133 break;
134 case 'p': /* landscape or portrait mode */
135 if ( ARGF()[0] == 'l' )
136 landscape = 1;
137 else
138 landscape = 0;
139 break;
140 case 'x': /* shift things horizontally */
141 xoffset = atof(ARGF());
142 break;
143 case 'y': /* and vertically on the page */
144 yoffset = atof(ARGF());
145 break;
146 case 'P': /* PostScript pass through */
147 t = ARGF();
148 i = strlen(t) + 1;
149 passthrough = malloc(i);
150 if (passthrough == 0)
151 sysfatal("malloc");
152 strncpy(passthrough, t, i);
153 break;
154 default: /* don't know what to do for ch */
155 fprint(2, "unknown option %C\n", ARGC());
156 break;
157 }ARGEND;
158
159 readDESC();
160 if (argc == 0) {
161 if ((binp = (Biobuf *)malloc(sizeof(Biobuf))) == nil)
162 sysfatal("malloc");
163 if (Binit(binp, 0, OREAD) == Beof)
164 sysfatal("Binit of <stdin> failed.");
165 Binp = &binp->Biobufhdr;
166 if (debug) fprint(2, "using standard input\n");
167 conv(Binp);
168 Bterm(Binp);
169 }
170 for (i=0; i<argc; i++) {
171 if ((binp=Bopen(argv[i], OREAD)) == 0) {
172 fprint(2, "cannot open file %s\n", argv[i]);
173 continue;
174 }
175 Binp = &binp->Biobufhdr;
176 inputfilename = argv[i];
177 conv(Binp);
178 Bterm(Binp);
179 }
180 Bterm(Bstdout);
181
182 if ((ifd=open(tmpfilename, OREAD)) < 0)
183 sysfatal("open of %s failed: %r", tmpfilename);
184
185 bstdout = galloc(0, sizeof(Biobuf), "bstdout");
186 if (Binit(bstdout, 1, OWRITE) == Beof)
187 sysfatal("Binit of <stdout> failed.");
188 Bstdout = &(bstdout->Biobufhdr);
189 prologues();
190 Bflush(Bstdout);
191 tot = 0;
192 while ((i=read(ifd, copybuf, BUFSIZ)) > 0) {
193 if (write(1, copybuf, i) != i) {
194 fprint(2, "write error on copying from temp file.\n");
195 exits("write");
196 }
197 tot += i;
198 }
199 if (debug) fprint(2, "copied %d bytes to final output i=%d\n", tot, i);
200 if (i < 0)
201 sysfatal("read error copying from temp file: %r");
202 finish();
203 exits("");
204 }
205