dpost.ps: increase linewidth to match groff
[troff.git] / tr2ps / tr2ps.c
blob333a4c3d11e6d59089849b03a2d34b45a7028455
1 #include <fcntl.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <sys/stat.h>
6 #include <unistd.h>
7 #include "ustr.h"
8 #include "common.h"
9 #include "tr2ps.h"
10 #include "comments.h"
11 #include "path.h"
13 #define MAXBCHARS 512
15 static int formsperpage = 1;
16 static double aspectratio = 1.0;
17 static int copies = 1;
18 static int landscape = 0;
19 static double magnification = 1.0;
20 static int pointsize = 10;
21 static char *passthrough = 0;
22 int picflag = 1;
23 double xoffset = .25;
24 double yoffset = .25;
26 FILE *fout, *ferr;
27 struct ustr *fin;
29 static char tmpfilename[MAXTOKENSIZE];
30 static char copybuf[BUFSIZ];
32 static struct charent *build_char_list[MAXBCHARS];
33 static int build_char_cnt = 0;
36 * stash charent pointer in a list so that we can
37 * print these character definitions in the prologue.
39 void build_char(struct charent *cp)
41 int i;
42 for (i = 0; i < build_char_cnt; i++)
43 if (cp == build_char_list[i])
44 break;
45 if (i == build_char_cnt)
46 build_char_list[build_char_cnt++] = cp;
49 void prologues(void)
51 int i;
52 char charlibname[MAXTOKENSIZE];
54 fprintf(fout, "%s", CONFORMING);
55 fprintf(fout, "%s %s\n", VERSION, PROGVER);
56 fprintf(fout, "%s %s\n", CREATOR, PROGNAME);
57 fprintf(fout, "%s %s\n", DOCUMENTFONTS, ATEND);
58 fprintf(fout, "%s %s\n", PAGES, ATEND);
59 fprintf(fout, "%s", ENDCOMMENTS);
61 if (cat(DPOST)) {
62 fprintf(ferr, "can't read %s\n", DPOST);
63 exit(1);
66 if (drawflag) {
67 if (cat(DRAW)) {
68 fprintf(ferr, "can't read %s\n", DRAW);
69 exit(1);
72 if (DOROUND)
73 cat(ROUNDPAGE);
75 fprintf(fout, "%s", ENDPROLOG);
76 fprintf(fout, "%s", BEGINSETUP);
77 fprintf(fout, "mark\n");
78 if (formsperpage > 1) {
79 fprintf(fout, "%s %d\n", FORMSPERPAGE, formsperpage);
80 fprintf(fout, "/formsperpage %d def\n", formsperpage);
82 if (aspectratio != 1)
83 fprintf(fout, "/aspectratio %g def\n", aspectratio);
84 if (copies != 1)
85 fprintf(fout, "/#copies %d store\n", copies);
86 if (landscape)
87 fprintf(fout, "/landscape true def\n");
88 if (magnification != 1)
89 fprintf(fout, "/magnification %g def\n", magnification);
90 if (pointsize != 10)
91 fprintf(fout, "/pointsize %d def\n", pointsize);
92 if (xoffset != .25)
93 fprintf(fout, "/xoffset %g def\n", xoffset);
94 if (yoffset != .25)
95 fprintf(fout, "/yoffset %g def\n", yoffset);
96 if (passthrough != 0)
97 fprintf(fout, "%s\n", passthrough);
99 fprintf(fout, "setup\n");
100 if (formsperpage > 1) {
101 cat(FORMFILE);
102 fprintf(fout, "%d setupforms \n", formsperpage);
104 /* output Build character info from charlib if necessary. */
105 for (i = 0; i < build_char_cnt; i++) {
106 sprintf(charlibname, "%s/dev%s/charlib/%s",
107 FONTDIR, devname, build_char_list[i]->name);
108 if (cat(charlibname))
109 fprintf(fout, "cannot open %s\n", charlibname);
112 fprintf(fout, "%s", ENDSETUP);
115 void cleanup(void)
117 remove(tmpfilename);
120 int main(int argc, char *argv[])
122 int i, tot, ifd;
124 fout = stdout;
125 ferr = stderr;
126 programname = argv[0];
128 tmpnam(tmpfilename);
129 if (!(fout = fopen(tmpfilename, "w"))) {
130 fprintf(ferr, "cannot open temporary file %s\n", tmpfilename);
131 exit(1);
133 atexit(cleanup);
135 i = 1;
136 while (i < argc && argv[i][0] == '-') {
137 switch (argv[i][1]) {
138 case 'a': /* aspect ratio */
139 aspectratio = atof(argv[++i]);
140 break;
141 case 'c': /* copies */
142 copies = atoi(argv[++i]);
143 break;
144 case 'd':
145 debug = 1;
146 break;
147 case 'm': /* magnification */
148 magnification = atof(argv[++i]);
149 break;
150 case 'n': /* forms per page */
151 formsperpage = atoi(argv[++i]);
152 break;
153 case 'o': /* output page list */
154 pagelist(argv[++i]);
155 break;
156 case 'p': /* landscape or portrait mode */
157 if (argv[++i][0] == 'l')
158 landscape = 1;
159 else
160 landscape = 0;
161 break;
162 case 'x': /* shift things horizontally */
163 xoffset = atof(argv[++i]);
164 break;
165 case 'y': /* and vertically on the page */
166 yoffset = atof(argv[++i]);
167 break;
168 case 'P': /* PostScript pass through */
169 passthrough = argv[++i];
170 break;
171 default: /* don't know what to do for ch */
172 fprintf(ferr, "unknown option %s\n", argv[i]);
174 i++;
176 if (i == argc) {
177 fin = ustr_fill(0);
178 if (debug)
179 fprintf(ferr, "using standard input\n");
180 conv(fin);
181 ustr_free(fin);
183 for (; i < argc; i++) {
184 int fd = open(argv[i], O_RDONLY);
185 if (fd < 0) {
186 fprintf(ferr, "cannot open file %s\n", argv[i]);
187 continue;
189 fin = ustr_fill(fd);
190 close(fd);
191 conv(fin);
192 ustr_free(fin);
194 fclose(fout);
196 if ((ifd = open(tmpfilename, O_RDONLY)) < 0) {
197 fprintf(ferr, "open of %s failed.\n", tmpfilename);
198 exit(1);
201 fout = stdout;
202 prologues();
203 fflush(fout);
204 tot = 0;
205 i = 0;
206 while ((i = read(ifd, copybuf, BUFSIZ)) > 0) {
207 if (write(1, copybuf, i) != i) {
208 fprintf(ferr, "write error on copying from temp file.\n");
209 exit(1);
211 tot += i;
213 if (debug)
214 fprintf(ferr, "copied %d bytes to final output i=%d\n", tot, i);
215 if (i < 0) {
216 fprintf(ferr, "read error on copying from temp file.\n");
217 exit(1);
219 finish();
221 return 0;