Blob


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"
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;
22 Biobuf binp, *bstdout, bstderr;
23 Biobuf *Bstdin, *Bstdout, *Bstderr;
24 int debug = 0;
26 #ifndef MAXPATHLEN
27 #define MAXPATHLEN 255
28 #endif
30 char tmpfilename[MAXPATHLEN+1];
31 char copybuf[BUFSIZ];
34 struct charent **build_char_list = 0;
35 int build_char_cnt = 0;
37 void
38 prologues(void) {
39 int i;
40 char charlibname[MAXTOKENSIZE];
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);
48 if (cat(unsharp(DPOST))) {
49 Bprint(Bstderr, "can't read %s\n", DPOST);
50 exits("dpost prologue");
51 }
53 if (drawflag) {
54 if (cat(unsharp(DRAW))) {
55 Bprint(Bstderr, "can't read %s\n", DRAW);
56 exits("draw prologue");
57 }
58 }
60 if (DOROUND)
61 cat(unsharp(ROUNDPAGE));
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);
77 cat(unsharp(ENCODINGDIR"/Latin1.enc"));
78 if (passthrough != 0) Bprint(Bstdout, "%s\n", passthrough);
80 Bprint(Bstdout, "setup\n");
81 if (formsperpage > 1) {
82 cat(unsharp(FORMFILE));
83 Bprint(Bstdout, "%d setupforms \n", formsperpage);
84 }
85 /* output Build character info from charlib if necessary. */
87 for (i=0; i<build_char_cnt; i++) {
88 // Rewrite file name for case-insensitive or non-UTF-8 file systems.
89 // _x means a lowercase x; #1234 means Unicode 0x1234.
90 char buf[100];
91 char *r, *w;
92 for(w=buf, r=build_char_list[i]->name; *r && w<buf+sizeof buf-8; ){
93 if((uchar)*r >= 0x80){
94 Rune rr;
95 r += chartorune(&rr, r);
96 sprint(w, "#%04x", rr);
97 w += strlen(w);
98 continue;
99 }
100 if(('a' <= *r && *r <= 'z') || *r == '_')
101 *w++ = '_';
102 if(*r == '#')
103 *w++ = '#';
104 *w++ = *r++;
106 *w = 0;
107 sprint(charlibname, "%s/%s", CHARLIB, buf);
108 if (cat(unsharp(charlibname)))
109 Bprint(Bstderr, "cannot open %s\n", charlibname);
112 Bprint(Bstdout, "%s", ENDSETUP);
115 void
116 cleanup(void) {
117 remove(tmpfilename);
120 int
121 main(int argc, char *argv[]) {
122 Biobuf btmp;
123 Biobuf *binp;
124 Biobuf *Binp;
125 int i, tot, ifd, fd;
126 char *t;
128 programname = argv[0];
129 if (Binit(&bstderr, 2, OWRITE) == Beof) {
130 exits("Binit");
132 Bstderr = &bstderr; /* &bstderr.Biobufhdr; */
134 bstdout = &btmp;
135 fd = safe_tmpnam(tmpfilename);
136 if ((Binit(bstdout, fd, OWRITE)) == Beof) {
137 Bprint(Bstderr, "cannot open temporary file %s\n", tmpfilename);
138 exits("Bopen");
140 atexit(cleanup);
141 Bstdout = bstdout; /* &bstdout->Biobufhdr; */
143 ARGBEGIN{
144 case 'a': /* aspect ratio */
145 aspectratio = atof(ARGF());
146 break;
147 case 'c': /* copies */
148 copies = atoi(ARGF());
149 break;
150 case 'd':
151 debug = 1;
152 break;
153 case 'm': /* magnification */
154 magnification = atof(ARGF());
155 break;
156 case 'n': /* forms per page */
157 formsperpage = atoi(ARGF());
158 break;
159 case 'o': /* output page list */
160 pagelist(ARGF());
161 break;
162 case 'p': /* landscape or portrait mode */
163 if ( ARGF()[0] == 'l' )
164 landscape = 1;
165 else
166 landscape = 0;
167 break;
168 case 'x': /* shift things horizontally */
169 xoffset = atof(ARGF());
170 break;
171 case 'y': /* and vertically on the page */
172 yoffset = atof(ARGF());
173 break;
174 case 'P': /* PostScript pass through */
175 t = ARGF();
176 i = strlen(t) + 1;
177 passthrough = malloc(i);
178 if (passthrough == 0) {
179 Bprint(Bstderr, "cannot allocate memory for argument string\n");
180 exits("malloc");
182 strncpy(passthrough, t, i);
183 break;
184 default: /* don't know what to do for ch */
185 Bprint(Bstderr, "unknown option %C\n", ARGC());
186 break;
187 }ARGEND;
188 readDESC();
189 if (argc == 0) {
190 if ((binp = (Biobuf *)malloc(sizeof(Biobuf))) < (Biobuf *)0) {
191 Bprint(Bstderr, "malloc failed.\n");
192 exits("malloc");
194 if (Binit(binp, 0, OREAD) == Beof) {
195 Bprint(Bstderr, "Binit of <stdin> failed.\n");
196 exits("Binit");
198 Binp = binp; /* &(binp->Biobufhdr); */
199 if (debug) Bprint(Bstderr, "using standard input\n");
200 conv(Binp);
201 Bterm(Binp);
203 for (i=0; i<argc; i++) {
204 if ((binp=Bopen(argv[i], OREAD)) == 0) {
205 Bprint(Bstderr, "cannot open file %s\n", argv[i]);
206 continue;
208 Binp = binp; /* &(binp->Biobufhdr); */
209 inputfilename = argv[i];
210 conv(Binp);
211 Bterm(Binp);
213 Bterm(Bstdout);
215 if ((ifd=open(tmpfilename, OREAD)) < 0) {
216 Bprint(Bstderr, "open of %s failed.\n", tmpfilename);
217 exits("open");
220 bstdout = galloc(0, sizeof(Biobuf), "bstdout");
221 if (Binit(bstdout, 1, OWRITE) == Beof) {
222 Bprint(Bstderr, "Binit of <stdout> failed.\n");
223 exits("Binit");
225 Bstdout = bstdout; /* &(bstdout->Biobufhdr); */
226 prologues();
227 Bflush(Bstdout);
228 tot = 0; i = 0;
229 while ((i=read(ifd, copybuf, BUFSIZ)) > 0) {
230 if (write(1, copybuf, i) != i) {
231 Bprint(Bstderr, "write error on copying from temp file.\n");
232 exits("write");
234 tot += i;
236 if (debug) Bprint(Bstderr, "copied %d bytes to final output i=%d\n", tot, i);
237 if (i < 0) {
238 Bprint(Bstderr, "read error on copying from temp file.\n");
239 exits("read");
241 finish();
243 exits("");
244 return 0;