xref: /plan9-contrib/sys/src/cmd/postscript/tr2post/tr2post.c (revision 219b2ee8daee37f4aad58d63f21287faa8e4ffdc)
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 
30 struct charent **build_char_list = 0;
31 int build_char_cnt = 0;
32 
33 void
34 prologues(void) {
35 	int i;
36 	char charlibname[MAXTOKENSIZE];
37 
38 	Bprint(Bstdout, "%s", CONFORMING);
39 	Bprint(Bstdout, "%s %s\n", VERSION, PROGRAMVERSION);
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 		Bprint(Bstderr, "can't read %s\n", DPOST);
46 		exits("dpost prologue");
47 	}
48 
49 	if (drawflag) {
50 		if (cat(DRAW)) {
51 			Bprint(Bstderr, "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 		Bprint(Bstderr, "cannot open %s\n", charlibname);
87 	}
88 
89 	Bprint(Bstdout, "%s", ENDSETUP);
90 }
91 
92 void
93 cleanup(void) {
94 	remove(tmpfilename);
95 }
96 
97 main(int argc, char *argv[]) {
98 	Biobuf *binp;
99 	Biobufhdr *Binp;
100 	int i, tot, ifd;
101 	char *t;
102 
103 	programname = argv[0];
104 	if (Binit(&bstderr, 2, OWRITE) == Beof) {
105 		exits("Binit");
106 	}
107 	Bstderr = &bstderr.Biobufhdr;
108 
109 	tmpnam(tmpfilename);
110 	if ((bstdout=Bopen(tmpfilename, OWRITE)) == 0) {
111 		Bprint(Bstderr, "cannot open temporary file %s\n", tmpfilename);
112 		exits("Bopen");
113 	}
114 	atexit(cleanup);
115 	Bstdout = &bstdout->Biobufhdr;
116 
117 	ARGBEGIN{
118 		case 'a':			/* aspect ratio */
119 			aspectratio = atof(ARGF());
120 			break;
121 		case 'c':			/* copies */
122 			copies = atoi(ARGF());
123 			break;
124 		case 'd':
125 			debug = 1;
126 			break;
127 		case 'm':			/* magnification */
128 			magnification = atof(ARGF());
129 			break;
130 		case 'n':			/* forms per page */
131 			formsperpage = atoi(ARGF());
132 			break;
133 		case 'o':			/* output page list */
134 			pagelist(ARGF());
135 			break;
136 		case 'p':			/* landscape or portrait mode */
137 			if ( ARGF()[0] == 'l' )
138 				landscape = 1;
139 			else
140 				landscape = 0;
141 			break;
142 		case 'x':			/* shift things horizontally */
143 			xoffset = atof(ARGF());
144 			break;
145 		case 'y':			/* and vertically on the page */
146 			yoffset = atof(ARGF());
147 			break;
148 		case 'P':			/* PostScript pass through */
149 			t = ARGF();
150 			i = strlen(t) + 1;
151 			passthrough = malloc(i);
152 			if (passthrough == 0) {
153 				Bprint(Bstderr, "cannot allocate memory for argument string\n");
154 				exits("malloc");
155 			}
156 			strncpy(passthrough, t, i);
157 			break;
158 		default:			/* don't know what to do for ch */
159 			Bprint(Bstderr, "unknown option %C\n", ARGC());
160 			break;
161 	}ARGEND;
162 	readDESC();
163 	if (argc == 0) {
164 		if ((binp = (Biobuf *)malloc(sizeof(Biobuf))) < (Biobuf *)0) {
165 			Bprint(Bstderr, "malloc failed.\n");
166 			exits("malloc");
167 		}
168 		if (Binit(binp, 0, OREAD) == Beof) {
169 			Bprint(Bstderr, "Binit of <stdin> failed.\n");
170 			exits("Binit");
171 		}
172 		Binp = &(binp->Biobufhdr);
173 		if (debug) Bprint(Bstderr, "using standard input\n");
174 		conv(Binp);
175 		Bterm(Binp);
176 	}
177 	for (i=0; i<argc; i++) {
178 		if ((binp=Bopen(argv[i], OREAD)) == 0) {
179 			Bprint(Bstderr, "cannot open file %s\n", argv[i]);
180 			continue;
181 		}
182 		Binp = &(binp->Biobufhdr);
183 		inputfilename = argv[i];
184 		conv(Binp);
185 		Bterm(Binp);
186 	}
187 	Bterm(Bstdout);
188 
189 	if ((ifd=open(tmpfilename, OREAD)) < 0) {
190 		Bprint(Bstderr, "open of %s failed.\n", tmpfilename);
191 		exits("open");
192 	}
193 
194 	bstdout = galloc(0, sizeof(Biobuf), "bstdout");
195 	if (Binit(bstdout, 1, OWRITE) == Beof) {
196 		Bprint(Bstderr, "Binit of <stdout> failed.\n");
197 		exits("Binit");
198 	}
199 	Bstdout = &(bstdout->Biobufhdr);
200 	prologues();
201 	Bflush(Bstdout);
202 	tot = 0; i = 0;
203 	while ((i=read(ifd, copybuf, BUFSIZ)) > 0) {
204 		if (write(1, copybuf, i) != i) {
205 			Bprint(Bstderr, "write error on copying from temp file.\n");
206 			exits("write");
207 		}
208 		tot += i;
209 	}
210 	if (debug) Bprint(Bstderr, "copied %d bytes to final output i=%d\n", tot, i);
211 	if (i < 0) {
212 		Bprint(Bstderr, "read error on copying from temp file.\n");
213 		exits("read");
214 	}
215 	finish();
216 
217 	exits("");
218 }
219