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