Infrastructure changes for character set support. ustrtoa,
[sgt/halibut] / bk_man.c
1 /*
2 * man page backend for Halibut
3 */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <assert.h>
8 #include "halibut.h"
9
10 static void man_text(FILE *, word *, int newline, int quote_props);
11 static void man_codepara(FILE *, word *);
12 static int man_convert(wchar_t *s, int maxlen,
13 char **result, int quote_props);
14
15 typedef struct {
16 wchar_t *th;
17 int headnumbers;
18 int mindepth;
19 char *filename;
20 } manconfig;
21
22 static manconfig man_configure(paragraph *source) {
23 manconfig ret;
24
25 /*
26 * Defaults.
27 */
28 ret.th = NULL;
29 ret.headnumbers = FALSE;
30 ret.mindepth = 0;
31 ret.filename = dupstr("output.1");
32
33 for (; source; source = source->next) {
34 if (source->type == para_Config) {
35 if (!ustricmp(source->keyword, L"man-identity")) {
36 wchar_t *wp, *ep;
37
38 wp = uadv(source->keyword);
39 ep = wp;
40 while (*ep)
41 ep = uadv(ep);
42 sfree(ret.th);
43 ret.th = mknewa(wchar_t, ep - wp + 1);
44 memcpy(ret.th, wp, (ep - wp + 1) * sizeof(wchar_t));
45 } else if (!ustricmp(source->keyword, L"man-headnumbers")) {
46 ret.headnumbers = utob(uadv(source->keyword));
47 } else if (!ustricmp(source->keyword, L"man-mindepth")) {
48 ret.mindepth = utoi(uadv(source->keyword));
49 } else if (!ustricmp(source->keyword, L"man-filename")) {
50 sfree(ret.filename);
51 ret.filename = dupstr(adv(source->origkeyword));
52 }
53 }
54 }
55
56 return ret;
57 }
58
59 static void man_conf_cleanup(manconfig cf)
60 {
61 sfree(cf.th);
62 sfree(cf.filename);
63 }
64
65 paragraph *man_config_filename(char *filename)
66 {
67 return cmdline_cfg_simple("man-filename", filename, NULL);
68 }
69
70 #define QUOTE_INITCTRL 1 /* quote initial . and ' on a line */
71 #define QUOTE_QUOTES 2 /* quote double quotes by doubling them */
72
73 void man_backend(paragraph *sourceform, keywordlist *keywords,
74 indexdata *idx, void *unused) {
75 paragraph *p;
76 FILE *fp;
77 manconfig conf;
78
79 IGNORE(unused);
80 IGNORE(keywords);
81 IGNORE(idx);
82
83 conf = man_configure(sourceform);
84
85 /*
86 * Open the output file.
87 */
88 fp = fopen(conf.filename, "w");
89 if (!fp) {
90 error(err_cantopenw, conf.filename);
91 return;
92 }
93
94 /* Do the version ID */
95 for (p = sourceform; p; p = p->next)
96 if (p->type == para_VersionID) {
97 fprintf(fp, ".\\\" ");
98 man_text(fp, p->words, TRUE, 0);
99 }
100
101 /* .TH name-of-program manual-section */
102 fprintf(fp, ".TH");
103 if (conf.th && *conf.th) {
104 char *c;
105 wchar_t *wp;
106
107 for (wp = conf.th; *wp; wp = uadv(wp)) {
108 fputs(" \"", fp);
109 man_convert(wp, 0, &c, QUOTE_QUOTES);
110 fputs(c, fp);
111 sfree(c);
112 fputc('"', fp);
113 }
114 }
115 fputc('\n', fp);
116
117 fprintf(fp, ".UC\n");
118
119 for (p = sourceform; p; p = p->next) switch (p->type) {
120 /*
121 * Things we ignore because we've already processed them or
122 * aren't going to touch them in this pass.
123 */
124 case para_IM:
125 case para_BR:
126 case para_Biblio: /* only touch BiblioCited */
127 case para_VersionID:
128 case para_NoCite:
129 case para_Title:
130 break;
131
132 /*
133 * Headings.
134 */
135 case para_Chapter:
136 case para_Appendix:
137 case para_UnnumberedChapter:
138 case para_Heading:
139 case para_Subsect:
140
141 {
142 int depth;
143 if (p->type == para_Subsect)
144 depth = p->aux + 2;
145 else if (p->type == para_Heading)
146 depth = 1;
147 else
148 depth = 0;
149 if (depth >= conf.mindepth) {
150 fprintf(fp, ".SH \"");
151 if (conf.headnumbers && p->kwtext) {
152 man_text(fp, p->kwtext, FALSE, QUOTE_QUOTES);
153 fprintf(fp, " ");
154 }
155 man_text(fp, p->words, FALSE, QUOTE_QUOTES);
156 fprintf(fp, "\"\n");
157 }
158 break;
159 }
160
161 /*
162 * Code paragraphs.
163 */
164 case para_Code:
165 fprintf(fp, ".PP\n");
166 man_codepara(fp, p->words);
167 break;
168
169 /*
170 * Normal paragraphs.
171 */
172 case para_Normal:
173 case para_Copyright:
174 fprintf(fp, ".PP\n");
175 man_text(fp, p->words, TRUE, 0);
176 break;
177
178 /*
179 * List paragraphs.
180 */
181 case para_Description:
182 case para_BiblioCited:
183 case para_Bullet:
184 case para_NumberedList:
185 if (p->type == para_Bullet) {
186 fprintf(fp, ".IP \"\\fBo\\fP\"\n"); /* FIXME: configurable? */
187 } else if (p->type == para_NumberedList) {
188 fprintf(fp, ".IP \"");
189 man_text(fp, p->kwtext, FALSE, QUOTE_QUOTES);
190 fprintf(fp, "\"\n");
191 } else if (p->type == para_Description) {
192 /*
193 * Do nothing; the .xP for this paragraph is the .IP
194 * which has come before it in the DescribedThing.
195 */
196 } else if (p->type == para_BiblioCited) {
197 fprintf(fp, ".IP \"");
198 man_text(fp, p->kwtext, FALSE, QUOTE_QUOTES);
199 fprintf(fp, "\"\n");
200 }
201 man_text(fp, p->words, TRUE, 0);
202 break;
203
204 case para_DescribedThing:
205 fprintf(fp, ".IP \"");
206 man_text(fp, p->words, FALSE, QUOTE_QUOTES);
207 fprintf(fp, "\"\n");
208 break;
209
210 case para_Rule:
211 /*
212 * This isn't terribly good. Anyone who wants to do better
213 * should feel free!
214 */
215 fprintf(fp, ".PP\n----------------------------------------\n");
216 break;
217
218 case para_LcontPush:
219 case para_QuotePush:
220 fprintf(fp, ".RS\n");
221 break;
222 case para_LcontPop:
223 case para_QuotePop:
224 fprintf(fp, ".RE\n");
225 break;
226 }
227
228 /*
229 * Tidy up.
230 */
231 fclose(fp);
232 man_conf_cleanup(conf);
233 }
234
235 /*
236 * Convert a wide string into a string of chars. If `result' is
237 * non-NULL, mallocs the resulting string and stores a pointer to
238 * it in `*result'. If `result' is NULL, merely checks whether all
239 * characters in the string are feasible for the output character
240 * set.
241 *
242 * Return is nonzero if all characters are OK. If not all
243 * characters are OK but `result' is non-NULL, a result _will_
244 * still be generated!
245 *
246 * FIXME: Here is probably also a good place to do escaping sorts
247 * of things. I know I at least need to escape backslash, and full
248 * stops at the starts of words are probably trouble as well.
249 */
250 static int man_convert(wchar_t *s, int maxlen,
251 char **result, int quote_props) {
252 /*
253 * FIXME. Currently this is ISO8859-1 only.
254 */
255 int doing = (result != 0);
256 int ok = TRUE;
257 char *p = NULL;
258 int plen = 0, psize = 0;
259
260 if (maxlen <= 0)
261 maxlen = -1;
262
263 for (; *s && maxlen != 0; s++, maxlen--) {
264 wchar_t c = *s;
265 char outc;
266
267 if ((c >= 32 && c <= 126) ||
268 (c >= 160 && c <= 255)) {
269 /* Char is OK. */
270 outc = (char)c;
271 } else {
272 /* Char is not OK. */
273 ok = FALSE;
274 outc = 0xBF; /* approximate the good old DEC `uh?' */
275 }
276 if (doing) {
277 if (plen+3 >= psize) {
278 psize = plen + 256;
279 p = resize(p, psize);
280 }
281 if (plen == 0 && (outc == '.' || outc == '\'') &&
282 (quote_props & QUOTE_INITCTRL)) {
283 /*
284 * Control character (. or ') at the start of a
285 * line. Quote it by putting \& (troff zero-width
286 * space) before it.
287 */
288 p[plen++] = '\\';
289 p[plen++] = '&';
290 } else if (outc == '\\') {
291 /*
292 * Quote backslashes by doubling them, always.
293 */
294 p[plen++] = '\\';
295 } else if (outc == '"' && (quote_props & QUOTE_QUOTES)) {
296 /*
297 * Double quote within double quotes. Quote it by
298 * doubling.
299 */
300 p[plen++] = '"';
301 }
302 p[plen++] = outc;
303 }
304 }
305 if (doing) {
306 p = resize(p, plen+1);
307 p[plen] = '\0';
308 *result = p;
309 }
310 return ok;
311 }
312
313 static void man_rdaddwc(rdstringc *rs, word *text, word *end,
314 int quote_props) {
315 char *c;
316
317 for (; text && text != end; text = text->next) switch (text->type) {
318 case word_HyperLink:
319 case word_HyperEnd:
320 case word_UpperXref:
321 case word_LowerXref:
322 case word_XrefEnd:
323 case word_IndexRef:
324 break;
325
326 case word_Normal:
327 case word_Emph:
328 case word_Code:
329 case word_WeakCode:
330 case word_WhiteSpace:
331 case word_EmphSpace:
332 case word_CodeSpace:
333 case word_WkCodeSpace:
334 case word_Quote:
335 case word_EmphQuote:
336 case word_CodeQuote:
337 case word_WkCodeQuote:
338 assert(text->type != word_CodeQuote &&
339 text->type != word_WkCodeQuote);
340 if (towordstyle(text->type) == word_Emph &&
341 (attraux(text->aux) == attr_First ||
342 attraux(text->aux) == attr_Only))
343 rdaddsc(rs, "\\fI");
344 else if ((towordstyle(text->type) == word_Code ||
345 towordstyle(text->type) == word_WeakCode) &&
346 (attraux(text->aux) == attr_First ||
347 attraux(text->aux) == attr_Only))
348 rdaddsc(rs, "\\fB");
349 if (removeattr(text->type) == word_Normal) {
350 if (rs->pos > 0)
351 quote_props &= ~QUOTE_INITCTRL; /* not at start any more */
352 if (man_convert(text->text, 0, &c, quote_props) || !text->alt)
353 rdaddsc(rs, c);
354 else
355 man_rdaddwc(rs, text->alt, NULL, quote_props);
356 sfree(c);
357 } else if (removeattr(text->type) == word_WhiteSpace) {
358 rdaddc(rs, ' ');
359 } else if (removeattr(text->type) == word_Quote) {
360 rdaddc(rs, '"');
361 if (quote_props & QUOTE_QUOTES)
362 rdaddc(rs, '"');
363 }
364 if (towordstyle(text->type) == word_Emph &&
365 (attraux(text->aux) == attr_Last ||
366 attraux(text->aux) == attr_Only))
367 rdaddsc(rs, "\\fP");
368 else if ((towordstyle(text->type) == word_Code ||
369 towordstyle(text->type) == word_WeakCode) &&
370 (attraux(text->aux) == attr_Last ||
371 attraux(text->aux) == attr_Only))
372 rdaddsc(rs, "\\fP");
373 break;
374 }
375 }
376
377 static void man_text(FILE *fp, word *text, int newline, int quote_props) {
378 rdstringc t = { 0, 0, NULL };
379
380 man_rdaddwc(&t, text, NULL, quote_props | QUOTE_INITCTRL);
381 fprintf(fp, "%s", t.text);
382 sfree(t.text);
383 if (newline)
384 fputc('\n', fp);
385 }
386
387 static void man_codepara(FILE *fp, word *text) {
388 fprintf(fp, ".nf\n");
389 for (; text; text = text->next) if (text->type == word_WeakCode) {
390 char *c;
391 wchar_t *t, *e;
392 int quote_props = QUOTE_INITCTRL;
393
394 t = text->text;
395 if (text->next && text->next->type == word_Emph) {
396 e = text->next->text;
397 text = text->next;
398 } else
399 e = NULL;
400
401 while (e && *e && *t) {
402 int n;
403 int ec = *e;
404
405 for (n = 0; t[n] && e[n] && e[n] == ec; n++);
406 if (ec == 'i')
407 fprintf(fp, "\\fI");
408 else if (ec == 'b')
409 fprintf(fp, "\\fB");
410 man_convert(t, n, &c, quote_props);
411 quote_props &= ~QUOTE_INITCTRL;
412 fprintf(fp, "%s", c);
413 sfree(c);
414 if (ec == 'i' || ec == 'b')
415 fprintf(fp, "\\fP");
416 t += n;
417 e += n;
418 }
419 man_convert(t, 0, &c, quote_props);
420 fprintf(fp, "%s\n", c);
421 sfree(c);
422 }
423 fprintf(fp, ".fi\n");
424 }