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