aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/postscript/tr2post/tr2post.c
blob: 5a22ac72253b1d9be442a0d1fb15e30e2a07a175 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <stdio.h>
#include "common.h"
#include "tr2post.h"
#include "comments.h"
#include "path.h"

int formsperpage = 1;
int picflag = 1;
double aspectratio = 1.0;
int copies = 1;
int landscape = 0;
double magnification = 1.0;
int linesperpage = 66;
int pointsize = 10;
double xoffset = .25;
double yoffset = .25;
char *passthrough = 0;

Biobuf binp, *bstdout, bstderr;
Biobuf *Bstdin, *Bstdout, *Bstderr;
int debug = 0;

#ifndef MAXPATHLEN
#define MAXPATHLEN 255
#endif

char tmpfilename[MAXPATHLEN+1];
char copybuf[BUFSIZ];


struct charent **build_char_list = 0;
int build_char_cnt = 0;

void
prologues(void) {
	int i;
	char charlibname[MAXTOKENSIZE];

	Bprint(Bstdout, "%s", CONFORMING);
	Bprint(Bstdout, "%s %s\n", VERSION, PROGRAMVERSION);
	Bprint(Bstdout, "%s %s\n", DOCUMENTFONTS, ATEND);
	Bprint(Bstdout, "%s %s\n", PAGES, ATEND);
	Bprint(Bstdout, "%s", ENDCOMMENTS);

	if (cat(unsharp(DPOST))) {
		Bprint(Bstderr, "can't read %s\n", DPOST);
		exits("dpost prologue");
	}

	if (drawflag) {
		if (cat(unsharp(DRAW))) {
			Bprint(Bstderr, "can't read %s\n", DRAW);
			exits("draw prologue");
		}
	}

	if (DOROUND)
		cat(unsharp(ROUNDPAGE));

	Bprint(Bstdout, "%s", ENDPROLOG);
	Bprint(Bstdout, "%s", BEGINSETUP);
	Bprint(Bstdout, "mark\n");
	if (formsperpage > 1) {
		Bprint(Bstdout, "%s %d\n", FORMSPERPAGE, formsperpage);
		Bprint(Bstdout, "/formsperpage %d def\n", formsperpage);
	}
	if (aspectratio != 1) Bprint(Bstdout, "/aspectratio %g def\n", aspectratio);
	if (copies != 1) Bprint(Bstdout, "/#copies %d store\n", copies);
	if (landscape) Bprint(Bstdout, "/landscape true def\n");
	if (magnification != 1) Bprint(Bstdout, "/magnification %g def\n", magnification);
	if (pointsize != 10) Bprint(Bstdout, "/pointsize %d def\n", pointsize);
	if (xoffset != .25) Bprint(Bstdout, "/xoffset %g def\n", xoffset);
	if (yoffset != .25) Bprint(Bstdout, "/yoffset %g def\n", yoffset);
	cat(unsharp(ENCODINGDIR"/Latin1.enc"));
	if (passthrough != 0) Bprint(Bstdout, "%s\n", passthrough);

	Bprint(Bstdout, "setup\n");
	if (formsperpage > 1) {
		cat(unsharp(FORMFILE));
		Bprint(Bstdout, "%d setupforms \n", formsperpage);
	}
/* output Build character info from charlib if necessary. */

	for (i=0; i<build_char_cnt; i++) {
		sprint(charlibname, "%s/%s", CHARLIB, build_char_list[i]->name);
		if (cat(unsharp(charlibname)))
		Bprint(Bstderr, "cannot open %s\n", charlibname);
	}

	Bprint(Bstdout, "%s", ENDSETUP);
}

void
cleanup(void) {
	remove(tmpfilename);
}

int
main(int argc, char *argv[]) {
	Biobuf btmp;
	Biobuf *binp;
	Biobuf *Binp;
	int i, tot, ifd, fd;
	char *t;

	programname = argv[0];
	if (Binit(&bstderr, 2, OWRITE) == Beof) {
		exits("Binit");
	}
	Bstderr = &bstderr;  /* &bstderr.Biobufhdr; */

	bstdout = &btmp;
	fd = safe_tmpnam(tmpfilename);
	if ((Binit(bstdout, fd, OWRITE)) == Beof) {
		Bprint(Bstderr, "cannot open temporary file %s\n", tmpfilename);
		exits("Bopen");
	}
	atexit(cleanup);
	Bstdout = bstdout; /* &bstdout->Biobufhdr; */
	
	ARGBEGIN{
		case 'a':			/* aspect ratio */
			aspectratio = atof(ARGF());
			break;
		case 'c':			/* copies */
			copies = atoi(ARGF());
			break;
		case 'd':
			debug = 1;
			break;
		case 'm':			/* magnification */
			magnification = atof(ARGF());
			break;
		case 'n':			/* forms per page */
			formsperpage = atoi(ARGF());
			break;
		case 'o':			/* output page list */
			pagelist(ARGF());
			break;
		case 'p':			/* landscape or portrait mode */
			if ( ARGF()[0] == 'l' )
				landscape = 1;
			else
				landscape = 0;
			break;
		case 'x':			/* shift things horizontally */
			xoffset = atof(ARGF());
			break;
		case 'y':			/* and vertically on the page */
			yoffset = atof(ARGF());
			break;
		case 'P':			/* PostScript pass through */
			t = ARGF();
			i = strlen(t) + 1;
			passthrough = malloc(i);
			if (passthrough == 0) {
				Bprint(Bstderr, "cannot allocate memory for argument string\n");
				exits("malloc");
			}
			strncpy(passthrough, t, i);
			break;
		default:			/* don't know what to do for ch */
			Bprint(Bstderr, "unknown option %C\n", ARGC());
			break;
	}ARGEND;
	readDESC();
	if (argc == 0) {
		if ((binp = (Biobuf *)malloc(sizeof(Biobuf))) < (Biobuf *)0) {
			Bprint(Bstderr, "malloc failed.\n");
			exits("malloc");
		}
		if (Binit(binp, 0, OREAD) == Beof) {
			Bprint(Bstderr, "Binit of <stdin> failed.\n");
			exits("Binit");
		}
		Binp = binp; /* &(binp->Biobufhdr); */
		if (debug) Bprint(Bstderr, "using standard input\n");
		conv(Binp);
		Bterm(Binp);
	}
	for (i=0; i<argc; i++) {
		if ((binp=Bopen(argv[i], OREAD)) == 0) {
			Bprint(Bstderr, "cannot open file %s\n", argv[i]);
			continue;
		}
		Binp = binp; /* &(binp->Biobufhdr); */
		inputfilename = argv[i];
		conv(Binp);
		Bterm(Binp);
	}
	Bterm(Bstdout);

	if ((ifd=open(tmpfilename, OREAD)) < 0) {
		Bprint(Bstderr, "open of %s failed.\n", tmpfilename);
		exits("open");
	}

	bstdout = galloc(0, sizeof(Biobuf), "bstdout");
	if (Binit(bstdout, 1, OWRITE) == Beof) {
		Bprint(Bstderr, "Binit of <stdout> failed.\n");
		exits("Binit");
	}
	Bstdout = bstdout; /* &(bstdout->Biobufhdr); */
	prologues();
	Bflush(Bstdout);
	tot = 0; i = 0;
	while ((i=read(ifd, copybuf, BUFSIZ)) > 0) {
		if (write(1, copybuf, i) != i) {
			Bprint(Bstderr, "write error on copying from temp file.\n");
			exits("write");
		}
		tot += i;
	}
	if (debug) Bprint(Bstderr, "copied %d bytes to final output i=%d\n", tot, i);
	if (i < 0) {
		Bprint(Bstderr, "read error on copying from temp file.\n");
		exits("read");
	}
	finish();
		
	exits("");
	return 0;
}