xref: /plan9/sys/src/cmd/postscript/tr2post/tr2post.c (revision f9e1cf08d3be51592e03e639fc848a68dc31a55e)
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", CREATOR, PROGRAMNAME);
41 	Bprint(Bstdout, "%s %s\n", DOCUMENTFONTS, ATEND);
42 	Bprint(Bstdout, "%s %s\n", PAGES, ATEND);
43 	Bprint(Bstdout, "%s", ENDCOMMENTS);
44 
45 	if (cat(DPOST)) {
46 		Bprint(Bstderr, "can't read %s\n", DPOST);
47 		exits("dpost prologue");
48 	}
49 
50 	if (drawflag) {
51 		if (cat(DRAW)) {
52 			Bprint(Bstderr, "can't read %s\n", DRAW);
53 			exits("draw prologue");
54 		}
55 	}
56 
57 	if (DOROUND)
58 		cat(ROUNDPAGE);
59 
60 	Bprint(Bstdout, "%s", ENDPROLOG);
61 	Bprint(Bstdout, "%s", BEGINSETUP);
62 	Bprint(Bstdout, "mark\n");
63 	if (formsperpage > 1) {
64 		Bprint(Bstdout, "%s %d\n", FORMSPERPAGE, formsperpage);
65 		Bprint(Bstdout, "/formsperpage %d def\n", formsperpage);
66 	}
67 	if (aspectratio != 1) Bprint(Bstdout, "/aspectratio %g def\n", aspectratio);
68 	if (copies != 1) Bprint(Bstdout, "/#copies %d store\n", copies);
69 	if (landscape) Bprint(Bstdout, "/landscape true def\n");
70 	if (magnification != 1) Bprint(Bstdout, "/magnification %g def\n", magnification);
71 	if (pointsize != 10) Bprint(Bstdout, "/pointsize %d def\n", pointsize);
72 	if (xoffset != .25) Bprint(Bstdout, "/xoffset %g def\n", xoffset);
73 	if (yoffset != .25) Bprint(Bstdout, "/yoffset %g def\n", yoffset);
74 	cat(ENCODINGDIR"/Latin1.enc");
75 	if (passthrough != 0) Bprint(Bstdout, "%s\n", passthrough);
76 
77 	Bprint(Bstdout, "setup\n");
78 	if (formsperpage > 1) {
79 		cat(FORMFILE);
80 		Bprint(Bstdout, "%d setupforms \n", formsperpage);
81 	}
82 /* output Build character info from charlib if necessary. */
83 
84 	for (i=0; i<build_char_cnt; i++) {
85 		sprint(charlibname, "%s/%s", CHARLIB, build_char_list[i]->name);
86 		if (cat(charlibname))
87 		Bprint(Bstderr, "cannot open %s\n", charlibname);
88 	}
89 
90 	Bprint(Bstdout, "%s", ENDSETUP);
91 }
92 
93 void
94 cleanup(void) {
95 	remove(tmpfilename);
96 }
97 
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 		exits("Binit");
107 	}
108 	Bstderr = &bstderr.Biobufhdr;
109 
110 	tmpnam(tmpfilename);
111 	if ((bstdout=Bopen(tmpfilename, OWRITE)) == 0) {
112 		Bprint(Bstderr, "cannot open temporary file %s\n", tmpfilename);
113 		exits("Bopen");
114 	}
115 	atexit(cleanup);
116 	Bstdout = &bstdout->Biobufhdr;
117 
118 	ARGBEGIN{
119 		case 'a':			/* aspect ratio */
120 			aspectratio = atof(ARGF());
121 			break;
122 		case 'c':			/* copies */
123 			copies = atoi(ARGF());
124 			break;
125 		case 'd':
126 			debug = 1;
127 			break;
128 		case 'm':			/* magnification */
129 			magnification = atof(ARGF());
130 			break;
131 		case 'n':			/* forms per page */
132 			formsperpage = atoi(ARGF());
133 			break;
134 		case 'o':			/* output page list */
135 			pagelist(ARGF());
136 			break;
137 		case 'p':			/* landscape or portrait mode */
138 			if ( ARGF()[0] == 'l' )
139 				landscape = 1;
140 			else
141 				landscape = 0;
142 			break;
143 		case 'x':			/* shift things horizontally */
144 			xoffset = atof(ARGF());
145 			break;
146 		case 'y':			/* and vertically on the page */
147 			yoffset = atof(ARGF());
148 			break;
149 		case 'P':			/* PostScript pass through */
150 			t = ARGF();
151 			i = strlen(t) + 1;
152 			passthrough = malloc(i);
153 			if (passthrough == 0) {
154 				Bprint(Bstderr, "cannot allocate memory for argument string\n");
155 				exits("malloc");
156 			}
157 			strncpy(passthrough, t, i);
158 			break;
159 		default:			/* don't know what to do for ch */
160 			Bprint(Bstderr, "unknown option %C\n", ARGC());
161 			break;
162 	}ARGEND;
163 	readDESC();
164 	if (argc == 0) {
165 		if ((binp = (Biobuf *)malloc(sizeof(Biobuf))) < (Biobuf *)0) {
166 			Bprint(Bstderr, "malloc failed.\n");
167 			exits("malloc");
168 		}
169 		if (Binit(binp, 0, OREAD) == Beof) {
170 			Bprint(Bstderr, "Binit of <stdin> failed.\n");
171 			exits("Binit");
172 		}
173 		Binp = &(binp->Biobufhdr);
174 		if (debug) Bprint(Bstderr, "using standard input\n");
175 		conv(Binp);
176 		Bterm(Binp);
177 	}
178 	for (i=0; i<argc; i++) {
179 		if ((binp=Bopen(argv[i], OREAD)) == 0) {
180 			Bprint(Bstderr, "cannot open file %s\n", argv[i]);
181 			continue;
182 		}
183 		Binp = &(binp->Biobufhdr);
184 		inputfilename = argv[i];
185 		conv(Binp);
186 		Bterm(Binp);
187 	}
188 	Bterm(Bstdout);
189 
190 	if ((ifd=open(tmpfilename, OREAD)) < 0) {
191 		Bprint(Bstderr, "open of %s failed.\n", tmpfilename);
192 		exits("open");
193 	}
194 
195 	bstdout = galloc(0, sizeof(Biobuf), "bstdout");
196 	if (Binit(bstdout, 1, OWRITE) == Beof) {
197 		Bprint(Bstderr, "Binit of <stdout> failed.\n");
198 		exits("Binit");
199 	}
200 	Bstdout = &(bstdout->Biobufhdr);
201 	prologues();
202 	Bflush(Bstdout);
203 	tot = 0; i = 0;
204 	while ((i=read(ifd, copybuf, BUFSIZ)) > 0) {
205 		if (write(1, copybuf, i) != i) {
206 			Bprint(Bstderr, "write error on copying from temp file.\n");
207 			exits("write");
208 		}
209 		tot += i;
210 	}
211 	if (debug) Bprint(Bstderr, "copied %d bytes to final output i=%d\n", tot, i);
212 	if (i < 0) {
213 		Bprint(Bstderr, "read error on copying from temp file.\n");
214 		exits("read");
215 	}
216 	finish();
217 
218 	exits("");
219 }
220