Charset support for the info backend (\cfg{info-charset}). (This
[sgt/halibut] / bk_man.c
CommitLineData
7136a6c7 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
10static void man_text(FILE *, word *, int newline, int quote_props);
11static void man_codepara(FILE *, word *);
4b3c5afb 12static int man_convert(wchar_t *s, int maxlen,
13 char **result, int quote_props);
14
15typedef struct {
16 wchar_t *th;
17 int headnumbers;
18 int mindepth;
50d6b4bd 19 char *filename;
4b3c5afb 20} manconfig;
21
22static 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;
50d6b4bd 31 ret.filename = dupstr("output.1");
4b3c5afb 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);
50d6b4bd 42 sfree(ret.th);
4b3c5afb 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));
50d6b4bd 49 } else if (!ustricmp(source->keyword, L"man-filename")) {
50 sfree(ret.filename);
e4ea58f8 51 ret.filename = dupstr(adv(source->origkeyword));
4b3c5afb 52 }
53 }
54 }
55
56 return ret;
57}
58
59static void man_conf_cleanup(manconfig cf)
60{
61 sfree(cf.th);
50d6b4bd 62 sfree(cf.filename);
4b3c5afb 63}
7136a6c7 64
ba9c1487 65paragraph *man_config_filename(char *filename)
66{
e4ea58f8 67 return cmdline_cfg_simple("man-filename", filename, NULL);
ba9c1487 68}
69
7136a6c7 70#define QUOTE_INITCTRL 1 /* quote initial . and ' on a line */
71#define QUOTE_QUOTES 2 /* quote double quotes by doubling them */
72
73void man_backend(paragraph *sourceform, keywordlist *keywords,
43341922 74 indexdata *idx, void *unused) {
7136a6c7 75 paragraph *p;
76 FILE *fp;
4b3c5afb 77 manconfig conf;
7136a6c7 78
43341922 79 IGNORE(unused);
80 IGNORE(keywords);
81 IGNORE(idx);
7136a6c7 82
4b3c5afb 83 conf = man_configure(sourceform);
84
7136a6c7 85 /*
50d6b4bd 86 * Open the output file.
7136a6c7 87 */
50d6b4bd 88 fp = fopen(conf.filename, "w");
7136a6c7 89 if (!fp) {
50d6b4bd 90 error(err_cantopenw, conf.filename);
7136a6c7 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
4b3c5afb 101 /* .TH name-of-program manual-section */
22905f72 102 fprintf(fp, ".TH");
103 if (conf.th && *conf.th) {
4b3c5afb 104 char *c;
22905f72 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);
4b3c5afb 113 }
114 }
22905f72 115 fputc('\n', fp);
7136a6c7 116
117 fprintf(fp, ".UC\n");
118
7136a6c7 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:
7136a6c7 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:
8902e0ed 140
4b3c5afb 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 }
7136a6c7 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:
9057a0a8 173 case para_Copyright:
7136a6c7 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 /*
4b3c5afb 212 * This isn't terribly good. Anyone who wants to do better
213 * should feel free!
7136a6c7 214 */
4b3c5afb 215 fprintf(fp, ".PP\n----------------------------------------\n");
7136a6c7 216 break;
217
218 case para_LcontPush:
2614b01d 219 case para_QuotePush:
7136a6c7 220 fprintf(fp, ".RS\n");
221 break;
222 case para_LcontPop:
2614b01d 223 case para_QuotePop:
7136a6c7 224 fprintf(fp, ".RE\n");
225 break;
226 }
227
228 /*
229 * Tidy up.
230 */
231 fclose(fp);
4b3c5afb 232 man_conf_cleanup(conf);
7136a6c7 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 */
4b3c5afb 250static int man_convert(wchar_t *s, int maxlen,
251 char **result, int quote_props) {
7136a6c7 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
4b3c5afb 260 if (maxlen <= 0)
261 maxlen = -1;
262
263 for (; *s && maxlen != 0; s++, maxlen--) {
7136a6c7 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
313static 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");
4b3c5afb 344 else if ((towordstyle(text->type) == word_Code ||
345 towordstyle(text->type) == word_WeakCode) &&
7136a6c7 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 */
12efc259 352 if (man_convert(text->text, 0, &c, quote_props) || !text->alt)
7136a6c7 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) {
4b3c5afb 360 rdaddc(rs, '"');
361 if (quote_props & QUOTE_QUOTES)
362 rdaddc(rs, '"');
7136a6c7 363 }
364 if (towordstyle(text->type) == word_Emph &&
365 (attraux(text->aux) == attr_Last ||
366 attraux(text->aux) == attr_Only))
367 rdaddsc(rs, "\\fP");
4b3c5afb 368 else if ((towordstyle(text->type) == word_Code ||
369 towordstyle(text->type) == word_WeakCode) &&
7136a6c7 370 (attraux(text->aux) == attr_Last ||
371 attraux(text->aux) == attr_Only))
372 rdaddsc(rs, "\\fP");
373 break;
374 }
375}
376
377static 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
387static 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;
4b3c5afb 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);
7136a6c7 420 fprintf(fp, "%s\n", c);
421 sfree(c);
422 }
423 fprintf(fp, ".fi\n");
424}