blob: 5a22ac72253b1d9be442a0d1fb15e30e2a07a175 [file] [log] [blame]
rsc61f5c352004-05-15 23:55:53 +00001#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
10int formsperpage = 1;
11int picflag = 1;
12double aspectratio = 1.0;
13int copies = 1;
14int landscape = 0;
15double magnification = 1.0;
16int linesperpage = 66;
17int pointsize = 10;
18double xoffset = .25;
19double yoffset = .25;
20char *passthrough = 0;
21
22Biobuf binp, *bstdout, bstderr;
23Biobuf *Bstdin, *Bstdout, *Bstderr;
24int debug = 0;
25
wkje8fb1d32004-05-17 02:24:15 +000026#ifndef MAXPATHLEN
27#define MAXPATHLEN 255
28#endif
29
wkjb8551482004-05-16 07:54:22 +000030char tmpfilename[MAXPATHLEN+1];
rsc61f5c352004-05-15 23:55:53 +000031char copybuf[BUFSIZ];
32
33
34struct charent **build_char_list = 0;
35int build_char_cnt = 0;
36
37void
38prologues(void) {
39 int i;
40 char charlibname[MAXTOKENSIZE];
41
42 Bprint(Bstdout, "%s", CONFORMING);
43 Bprint(Bstdout, "%s %s\n", VERSION, PROGRAMVERSION);
44 Bprint(Bstdout, "%s %s\n", DOCUMENTFONTS, ATEND);
45 Bprint(Bstdout, "%s %s\n", PAGES, ATEND);
46 Bprint(Bstdout, "%s", ENDCOMMENTS);
47
wkje8fb1d32004-05-17 02:24:15 +000048 if (cat(unsharp(DPOST))) {
rsc61f5c352004-05-15 23:55:53 +000049 Bprint(Bstderr, "can't read %s\n", DPOST);
50 exits("dpost prologue");
51 }
52
53 if (drawflag) {
wkje8fb1d32004-05-17 02:24:15 +000054 if (cat(unsharp(DRAW))) {
rsc61f5c352004-05-15 23:55:53 +000055 Bprint(Bstderr, "can't read %s\n", DRAW);
56 exits("draw prologue");
57 }
58 }
59
60 if (DOROUND)
wkje8fb1d32004-05-17 02:24:15 +000061 cat(unsharp(ROUNDPAGE));
rsc61f5c352004-05-15 23:55:53 +000062
63 Bprint(Bstdout, "%s", ENDPROLOG);
64 Bprint(Bstdout, "%s", BEGINSETUP);
65 Bprint(Bstdout, "mark\n");
66 if (formsperpage > 1) {
67 Bprint(Bstdout, "%s %d\n", FORMSPERPAGE, formsperpage);
68 Bprint(Bstdout, "/formsperpage %d def\n", formsperpage);
69 }
70 if (aspectratio != 1) Bprint(Bstdout, "/aspectratio %g def\n", aspectratio);
71 if (copies != 1) Bprint(Bstdout, "/#copies %d store\n", copies);
72 if (landscape) Bprint(Bstdout, "/landscape true def\n");
73 if (magnification != 1) Bprint(Bstdout, "/magnification %g def\n", magnification);
74 if (pointsize != 10) Bprint(Bstdout, "/pointsize %d def\n", pointsize);
75 if (xoffset != .25) Bprint(Bstdout, "/xoffset %g def\n", xoffset);
76 if (yoffset != .25) Bprint(Bstdout, "/yoffset %g def\n", yoffset);
wkje8fb1d32004-05-17 02:24:15 +000077 cat(unsharp(ENCODINGDIR"/Latin1.enc"));
rsc61f5c352004-05-15 23:55:53 +000078 if (passthrough != 0) Bprint(Bstdout, "%s\n", passthrough);
79
80 Bprint(Bstdout, "setup\n");
81 if (formsperpage > 1) {
wkje8fb1d32004-05-17 02:24:15 +000082 cat(unsharp(FORMFILE));
rsc61f5c352004-05-15 23:55:53 +000083 Bprint(Bstdout, "%d setupforms \n", formsperpage);
84 }
85/* output Build character info from charlib if necessary. */
86
87 for (i=0; i<build_char_cnt; i++) {
88 sprint(charlibname, "%s/%s", CHARLIB, build_char_list[i]->name);
wkje8fb1d32004-05-17 02:24:15 +000089 if (cat(unsharp(charlibname)))
rsc61f5c352004-05-15 23:55:53 +000090 Bprint(Bstderr, "cannot open %s\n", charlibname);
91 }
92
93 Bprint(Bstdout, "%s", ENDSETUP);
94}
95
96void
97cleanup(void) {
98 remove(tmpfilename);
99}
100
wkje8fb1d32004-05-17 02:24:15 +0000101int
rsc61f5c352004-05-15 23:55:53 +0000102main(int argc, char *argv[]) {
wkjb8551482004-05-16 07:54:22 +0000103 Biobuf btmp;
rsc61f5c352004-05-15 23:55:53 +0000104 Biobuf *binp;
wkje8fb1d32004-05-17 02:24:15 +0000105 Biobuf *Binp;
wkjb8551482004-05-16 07:54:22 +0000106 int i, tot, ifd, fd;
rsc61f5c352004-05-15 23:55:53 +0000107 char *t;
108
109 programname = argv[0];
110 if (Binit(&bstderr, 2, OWRITE) == Beof) {
111 exits("Binit");
112 }
wkjb8551482004-05-16 07:54:22 +0000113 Bstderr = &bstderr; /* &bstderr.Biobufhdr; */
rsc61f5c352004-05-15 23:55:53 +0000114
wkjb8551482004-05-16 07:54:22 +0000115 bstdout = &btmp;
116 fd = safe_tmpnam(tmpfilename);
117 if ((Binit(bstdout, fd, OWRITE)) == Beof) {
rsc61f5c352004-05-15 23:55:53 +0000118 Bprint(Bstderr, "cannot open temporary file %s\n", tmpfilename);
119 exits("Bopen");
120 }
121 atexit(cleanup);
wkjb8551482004-05-16 07:54:22 +0000122 Bstdout = bstdout; /* &bstdout->Biobufhdr; */
rsc61f5c352004-05-15 23:55:53 +0000123
124 ARGBEGIN{
125 case 'a': /* aspect ratio */
126 aspectratio = atof(ARGF());
127 break;
128 case 'c': /* copies */
129 copies = atoi(ARGF());
130 break;
131 case 'd':
132 debug = 1;
133 break;
134 case 'm': /* magnification */
135 magnification = atof(ARGF());
136 break;
137 case 'n': /* forms per page */
138 formsperpage = atoi(ARGF());
139 break;
140 case 'o': /* output page list */
141 pagelist(ARGF());
142 break;
143 case 'p': /* landscape or portrait mode */
144 if ( ARGF()[0] == 'l' )
145 landscape = 1;
146 else
147 landscape = 0;
148 break;
149 case 'x': /* shift things horizontally */
150 xoffset = atof(ARGF());
151 break;
152 case 'y': /* and vertically on the page */
153 yoffset = atof(ARGF());
154 break;
155 case 'P': /* PostScript pass through */
156 t = ARGF();
157 i = strlen(t) + 1;
158 passthrough = malloc(i);
159 if (passthrough == 0) {
160 Bprint(Bstderr, "cannot allocate memory for argument string\n");
161 exits("malloc");
162 }
163 strncpy(passthrough, t, i);
164 break;
165 default: /* don't know what to do for ch */
166 Bprint(Bstderr, "unknown option %C\n", ARGC());
167 break;
168 }ARGEND;
169 readDESC();
170 if (argc == 0) {
171 if ((binp = (Biobuf *)malloc(sizeof(Biobuf))) < (Biobuf *)0) {
172 Bprint(Bstderr, "malloc failed.\n");
173 exits("malloc");
174 }
175 if (Binit(binp, 0, OREAD) == Beof) {
176 Bprint(Bstderr, "Binit of <stdin> failed.\n");
177 exits("Binit");
178 }
wkjb8551482004-05-16 07:54:22 +0000179 Binp = binp; /* &(binp->Biobufhdr); */
rsc61f5c352004-05-15 23:55:53 +0000180 if (debug) Bprint(Bstderr, "using standard input\n");
181 conv(Binp);
182 Bterm(Binp);
183 }
184 for (i=0; i<argc; i++) {
185 if ((binp=Bopen(argv[i], OREAD)) == 0) {
186 Bprint(Bstderr, "cannot open file %s\n", argv[i]);
187 continue;
188 }
wkjb8551482004-05-16 07:54:22 +0000189 Binp = binp; /* &(binp->Biobufhdr); */
rsc61f5c352004-05-15 23:55:53 +0000190 inputfilename = argv[i];
191 conv(Binp);
192 Bterm(Binp);
193 }
194 Bterm(Bstdout);
195
196 if ((ifd=open(tmpfilename, OREAD)) < 0) {
197 Bprint(Bstderr, "open of %s failed.\n", tmpfilename);
198 exits("open");
199 }
200
201 bstdout = galloc(0, sizeof(Biobuf), "bstdout");
202 if (Binit(bstdout, 1, OWRITE) == Beof) {
203 Bprint(Bstderr, "Binit of <stdout> failed.\n");
204 exits("Binit");
205 }
wkjb8551482004-05-16 07:54:22 +0000206 Bstdout = bstdout; /* &(bstdout->Biobufhdr); */
rsc61f5c352004-05-15 23:55:53 +0000207 prologues();
208 Bflush(Bstdout);
209 tot = 0; i = 0;
210 while ((i=read(ifd, copybuf, BUFSIZ)) > 0) {
211 if (write(1, copybuf, i) != i) {
212 Bprint(Bstderr, "write error on copying from temp file.\n");
213 exits("write");
214 }
215 tot += i;
216 }
217 if (debug) Bprint(Bstderr, "copied %d bytes to final output i=%d\n", tot, i);
218 if (i < 0) {
219 Bprint(Bstderr, "read error on copying from temp file.\n");
220 exits("read");
221 }
222 finish();
223
224 exits("");
wkje8fb1d32004-05-17 02:24:15 +0000225 return 0;
rsc61f5c352004-05-15 23:55:53 +0000226}