dpost.ps: increase linewidth to match groff
[troff.git] / tr2ps / common.c
blobd672e3f32c51d64782dd6e3a06911ea46733e8a0
1 #include <ctype.h>
2 #include <fcntl.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <stdarg.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <sys/stat.h>
9 #include "common.h"
10 #include "comments.h"
11 #include "path.h"
13 struct strtab charcode[256] = {
14 {4, "\\000"}, {4, "\\001"}, {4, "\\002"}, {4, "\\003"},
15 {4, "\\004"}, {4, "\\005"}, {4, "\\006"}, {4, "\\007"},
16 {4, "\\010"}, {4, "\\011"}, {4, "\\012"}, {4, "\\013"},
17 {4, "\\014"}, {4, "\\015"}, {4, "\\016"}, {4, "\\017"},
18 {4, "\\020"}, {4, "\\021"}, {4, "\\022"}, {4, "\\023"},
19 {4, "\\024"}, {4, "\\025"}, {4, "\\026"}, {4, "\\027"},
20 {4, "\\030"}, {4, "\\031"}, {4, "\\032"}, {4, "\\033"},
21 {4, "\\034"}, {4, "\\035"}, {4, "\\036"}, {4, "\\037"},
22 {1, " "}, {1, "!"}, {1, "\""}, {1, "#"},
23 {1, "$"}, {1, "%"}, {1, "&"}, {1, "'"},
24 {2, "\\("}, {2, "\\)"}, {1, "*"}, {1, "+"},
25 {1, ","}, {1, "-"}, {1, "."}, {1, "/"},
26 {1, "0"}, {1, "1"}, {1, "2"}, {1, "3"},
27 {1, "4"}, {1, "5"}, {1, "6"}, {1, "7"},
28 {1, "8"}, {1, "9"}, {1, ":"}, {1, ";"},
29 {1, "<"}, {1, "="}, {1, ">"}, {1, "?"},
30 {1, "@"}, {1, "A"}, {1, "B"}, {1, "C"},
31 {1, "D"}, {1, "E"}, {1, "F"}, {1, "G"},
32 {1, "H"}, {1, "I"}, {1, "J"}, {1, "K"},
33 {1, "L"}, {1, "M"}, {1, "N"}, {1, "O"},
34 {1, "P"}, {1, "Q"}, {1, "R"}, {1, "S"},
35 {1, "T"}, {1, "U"}, {1, "V"}, {1, "W"},
36 {1, "X"}, {1, "Y"}, {1, "Z"}, {1, "["},
37 {2, "\\\\"}, {1, "]"}, {1, "^"}, {1, "_"},
38 {1, "`"}, {1, "a"}, {1, "b"}, {1, "c"},
39 {1, "d"}, {1, "e"}, {1, "f"}, {1, "g"},
40 {1, "h"}, {1, "i"}, {1, "j"}, {1, "k"},
41 {1, "l"}, {1, "m"}, {1, "n"}, {1, "o"},
42 {1, "p"}, {1, "q"}, {1, "r"}, {1, "s"},
43 {1, "t"}, {1, "u"}, {1, "v"}, {1, "w"},
44 {1, "x"}, {1, "y"}, {1, "z"}, {1, "{"},
45 {1, "|"}, {1, "}"}, {1, "~"}, {4, "\\177"},
46 {4, "\\200"}, {4, "\\201"}, {4, "\\202"}, {4, "\\203"},
47 {4, "\\204"}, {4, "\\205"}, {4, "\\206"}, {4, "\\207"},
48 {4, "\\210"}, {4, "\\211"}, {4, "\\212"}, {4, "\\213"},
49 {4, "\\214"}, {4, "\\215"}, {4, "\\216"}, {4, "\\217"},
50 {4, "\\220"}, {4, "\\221"}, {4, "\\222"}, {4, "\\223"},
51 {4, "\\224"}, {4, "\\225"}, {4, "\\226"}, {4, "\\227"},
52 {4, "\\230"}, {4, "\\231"}, {4, "\\232"}, {4, "\\233"},
53 {4, "\\234"}, {4, "\\235"}, {4, "\\236"}, {4, "\\237"},
54 {4, "\\240"}, {4, "\\241"}, {4, "\\242"}, {4, "\\243"},
55 {4, "\\244"}, {4, "\\245"}, {4, "\\246"}, {4, "\\247"},
56 {4, "\\250"}, {4, "\\251"}, {4, "\\252"}, {4, "\\253"},
57 {4, "\\254"}, {4, "\\255"}, {4, "\\256"}, {4, "\\257"},
58 {4, "\\260"}, {4, "\\261"}, {4, "\\262"}, {4, "\\263"},
59 {4, "\\264"}, {4, "\\265"}, {4, "\\266"}, {4, "\\267"},
60 {4, "\\270"}, {4, "\\271"}, {4, "\\272"}, {4, "\\273"},
61 {4, "\\274"}, {4, "\\275"}, {4, "\\276"}, {4, "\\277"},
62 {4, "\\300"}, {4, "\\301"}, {4, "\\302"}, {4, "\\303"},
63 {4, "\\304"}, {4, "\\305"}, {4, "\\306"}, {4, "\\307"},
64 {4, "\\310"}, {4, "\\311"}, {4, "\\312"}, {4, "\\313"},
65 {4, "\\314"}, {4, "\\315"}, {4, "\\316"}, {4, "\\317"},
66 {4, "\\320"}, {4, "\\321"}, {4, "\\322"}, {4, "\\323"},
67 {4, "\\324"}, {4, "\\325"}, {4, "\\326"}, {4, "\\327"},
68 {4, "\\330"}, {4, "\\331"}, {4, "\\332"}, {4, "\\333"},
69 {4, "\\334"}, {4, "\\335"}, {4, "\\336"}, {4, "\\337"},
70 {4, "\\340"}, {4, "\\341"}, {4, "\\342"}, {4, "\\343"},
71 {4, "\\344"}, {4, "\\345"}, {4, "\\346"}, {4, "\\347"},
72 {4, "\\350"}, {4, "\\351"}, {4, "\\352"}, {4, "\\353"},
73 {4, "\\354"}, {4, "\\355"}, {4, "\\356"}, {4, "\\357"},
74 {4, "\\360"}, {4, "\\361"}, {4, "\\362"}, {4, "\\363"},
75 {4, "\\364"}, {4, "\\365"}, {4, "\\366"}, {4, "\\367"},
76 {4, "\\370"}, {4, "\\371"}, {4, "\\372"}, {4, "\\373"},
77 {4, "\\374"}, {4, "\\375"}, {4, "\\376"}, {4, "\\377"}
80 static int in_string; /* inside the parenthesis before w command */
81 static int in_byname; /* inside the brackets before g command */
82 int char_no = 0;
83 int line_no = 0;
84 int page_no = 0; /* page number in a document */
85 int pages_printed = 0;
87 static struct {
88 int beg;
89 int end;
90 } pplist[128]; /* list of pages to print */
91 static int pplist_cnt;
93 void pagelist(char *list)
95 char *next, *dash;
97 while (list && *list) {
98 next = strchr(list, ',');
99 dash = strchr(list, '-');
100 pplist[pplist_cnt].beg = atoi(list);
101 if (dash && (!next || dash < next))
102 pplist[pplist_cnt].end = atoi(dash + 1);
103 else
104 pplist[pplist_cnt].end = atoi(list);
105 list = next ? next + 1 : NULL;
106 pplist_cnt++;
110 int pageon(void)
112 extern int debug;
113 static int _debug; /* previous value of debug */
114 int i;
116 for (i = 0; i < pplist_cnt; i++)
117 if (page_no >= pplist[i].beg && page_no <= pplist[i].end)
118 break;
120 if (pplist_cnt == 0 && page_no != 0 || i < pplist_cnt) {
121 if (_debug && !debug) {
122 _debug = FALSE;
123 debug = TRUE;
125 return 1; /* no page list, print all pages */
126 } else {
127 if (!_debug && debug) {
128 _debug = TRUE;
129 debug = FALSE;
131 return 0;
135 static int stringhpos, stringvpos;
137 static void startstring(void)
139 if (in_byname)
140 endstring();
141 if (!in_string) {
142 stringhpos = hpos;
143 stringvpos = vpos;
144 if (pageon())
145 fprintf(fout, "(");
146 in_string = 1;
150 static void startbyname(void)
152 if (in_string)
153 endstring();
154 if (!in_byname) {
155 stringhpos = hpos;
156 stringvpos = vpos;
157 if (pageon())
158 fprintf(fout, "[");
159 in_byname = 1;
163 void endstring(void)
165 if (in_string) {
166 if (pageon())
167 fprintf(fout, ") %d %d w\n", stringhpos, stringvpos);
168 in_string = 0;
170 if (in_byname) {
171 if (pageon())
172 fprintf(fout, "] %d %d g\n", stringhpos, stringvpos);
173 in_byname = 0;
177 int isinstring(void)
179 return in_string;
182 void showglyph(char *s)
184 if (!in_string)
185 startstring();
186 fprintf(fout, "%s", s);
189 void showglyph_byname(char *s)
191 if (!in_byname)
192 startbyname();
193 fprintf(fout, "/%s", s);
196 void startpage(void)
198 ++char_no;
199 ++line_no;
200 ++page_no;
201 if (pageon()) {
202 ++pages_printed;
203 fprintf(fout, "%s %d %d\n", PAGE, page_no, pages_printed);
204 fprintf(fout, "/saveobj save def\n");
205 fprintf(fout, "mark\n");
206 fprintf(fout, "%d pagesetup\n", pages_printed);
210 void endpage(void)
212 endstring();
213 curpostfontid = -1;
214 line_no = 0;
215 char_no = 0;
216 if (pageon()) {
217 fprintf(fout, "cleartomark\n");
218 fprintf(fout, "showpage\n");
219 fprintf(fout, "saveobj restore\n");
220 fprintf(fout, "%s %d %d\n", ENDPAGE, page_no, pages_printed);
224 /* This was taken from postprint */
226 int cat(char *filename)
228 int n;
229 int fd;
230 static char buf[1 << 20];
232 if ((fd = open(filename, O_RDONLY)) < 0)
233 return 1;
234 while ((n = read(fd, buf, sizeof(buf))) > 0)
235 if (fwrite(buf, n, 1, fout) < 0)
236 break;
237 close(fd);
238 if (n != 0)
239 return 1;
240 return 0;
243 static char *errorstrings[] = {
244 "", /* NONE */
245 "WARNING",
246 "FATAL"
249 char *programname;
250 char *inputfilename = "<stdin>";
251 int inputlineno;
253 void error(int errtype, char *fmt, ...)
255 va_list arg;
257 fprintf(stderr, "%s: %s:%d :%s: ", programname,
258 inputfilename, inputlineno, errorstrings[errtype]);
259 va_start(arg, fmt);
260 vfprintf(stderr, fmt, arg);
261 va_end(arg);
262 if (errtype == FATAL)
263 exit(1);