Oops, nearly forgot. Nesting one numbered list inside another should
[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 *);
12
13#define QUOTE_INITCTRL 1 /* quote initial . and ' on a line */
14#define QUOTE_QUOTES 2 /* quote double quotes by doubling them */
15
16void man_backend(paragraph *sourceform, keywordlist *keywords,
17 indexdata *idx) {
18 paragraph *p;
19 FILE *fp;
20 char const *sep;
21
22 IGNORE(keywords); /* we don't happen to need this */
23 IGNORE(idx); /* or this */
24
25 /*
26 * Determine the output file name, and open the output file
27 *
28 * FIXME: want configurable output file names here. For the
29 * moment, we'll just call it `output.1'.
30 */
31 fp = fopen("output.1", "w");
32 if (!fp) {
33 error(err_cantopenw, "output.1");
34 return;
35 }
36
37 /* Do the version ID */
38 for (p = sourceform; p; p = p->next)
39 if (p->type == para_VersionID) {
40 fprintf(fp, ".\\\" ");
41 man_text(fp, p->words, TRUE, 0);
42 }
43
44 /* FIXME: .TH name-of-program manual-section */
45 fprintf(fp, ".TH FIXME 1\n");
46
47 fprintf(fp, ".UC\n");
48
49 /* Do the preamble and copyright */
50 sep = "";
51 for (p = sourceform; p; p = p->next)
52 if (p->type == para_Preamble) {
53 fprintf(fp, "%s", sep);
54 man_text(fp, p->words, TRUE, 0);
55 sep = "\n";
56 }
57 for (p = sourceform; p; p = p->next)
58 if (p->type == para_Copyright) {
59 fprintf(fp, "%s", sep);
60 man_text(fp, p->words, TRUE, 0);
61 sep = "\n";
62 }
63
64 /*
65 * FIXME:
66 *
67 * - figure out precisely what needs to be escaped.
68 * * A dot or apostrophe at the start of a line wants to be
69 * preceded by `\&', which is a zero-width space.
70 * * Literal backslashes always want doubling.
71 * * Within double quotes, a double quote needs doubling
72 * too.
73 *
74 * - work out what to do about hyphens / minuses...
75 */
76 for (p = sourceform; p; p = p->next) switch (p->type) {
77 /*
78 * Things we ignore because we've already processed them or
79 * aren't going to touch them in this pass.
80 */
81 case para_IM:
82 case para_BR:
83 case para_Biblio: /* only touch BiblioCited */
84 case para_VersionID:
85 case para_Copyright:
86 case para_Preamble:
87 case para_NoCite:
88 case para_Title:
89 break;
90
91 /*
92 * Headings.
93 */
94 case para_Chapter:
95 case para_Appendix:
96 case para_UnnumberedChapter:
97 case para_Heading:
98 case para_Subsect:
99 fprintf(fp, ".SH \"");
100 /* FIXME: disable this, at _least_ by default */
101 if (p->kwtext)
102 man_text(fp, p->kwtext, FALSE, QUOTE_QUOTES);
103 fprintf(fp, " ");
104 man_text(fp, p->words, FALSE, QUOTE_QUOTES);
105 fprintf(fp, "\"\n");
106 break;
107
108 /*
109 * Code paragraphs.
110 */
111 case para_Code:
112 fprintf(fp, ".PP\n");
113 man_codepara(fp, p->words);
114 break;
115
116 /*
117 * Normal paragraphs.
118 */
119 case para_Normal:
120 fprintf(fp, ".PP\n");
121 man_text(fp, p->words, TRUE, 0);
122 break;
123
124 /*
125 * List paragraphs.
126 */
127 case para_Description:
128 case para_BiblioCited:
129 case para_Bullet:
130 case para_NumberedList:
131 if (p->type == para_Bullet) {
132 fprintf(fp, ".IP \"\\fBo\\fP\"\n"); /* FIXME: configurable? */
133 } else if (p->type == para_NumberedList) {
134 fprintf(fp, ".IP \"");
135 man_text(fp, p->kwtext, FALSE, QUOTE_QUOTES);
136 fprintf(fp, "\"\n");
137 } else if (p->type == para_Description) {
138 /*
139 * Do nothing; the .xP for this paragraph is the .IP
140 * which has come before it in the DescribedThing.
141 */
142 } else if (p->type == para_BiblioCited) {
143 fprintf(fp, ".IP \"");
144 man_text(fp, p->kwtext, FALSE, QUOTE_QUOTES);
145 fprintf(fp, "\"\n");
146 }
147 man_text(fp, p->words, TRUE, 0);
148 break;
149
150 case para_DescribedThing:
151 fprintf(fp, ".IP \"");
152 man_text(fp, p->words, FALSE, QUOTE_QUOTES);
153 fprintf(fp, "\"\n");
154 break;
155
156 case para_Rule:
157 /*
158 * FIXME.
159 */
160 break;
161
162 case para_LcontPush:
163 fprintf(fp, ".RS\n");
164 break;
165 case para_LcontPop:
166 fprintf(fp, ".RE\n");
167 break;
168 }
169
170 /*
171 * Tidy up.
172 */
173 fclose(fp);
174}
175
176/*
177 * Convert a wide string into a string of chars. If `result' is
178 * non-NULL, mallocs the resulting string and stores a pointer to
179 * it in `*result'. If `result' is NULL, merely checks whether all
180 * characters in the string are feasible for the output character
181 * set.
182 *
183 * Return is nonzero if all characters are OK. If not all
184 * characters are OK but `result' is non-NULL, a result _will_
185 * still be generated!
186 *
187 * FIXME: Here is probably also a good place to do escaping sorts
188 * of things. I know I at least need to escape backslash, and full
189 * stops at the starts of words are probably trouble as well.
190 */
191static int man_convert(wchar_t *s, char **result, int quote_props) {
192 /*
193 * FIXME. Currently this is ISO8859-1 only.
194 */
195 int doing = (result != 0);
196 int ok = TRUE;
197 char *p = NULL;
198 int plen = 0, psize = 0;
199
200 for (; *s; s++) {
201 wchar_t c = *s;
202 char outc;
203
204 if ((c >= 32 && c <= 126) ||
205 (c >= 160 && c <= 255)) {
206 /* Char is OK. */
207 outc = (char)c;
208 } else {
209 /* Char is not OK. */
210 ok = FALSE;
211 outc = 0xBF; /* approximate the good old DEC `uh?' */
212 }
213 if (doing) {
214 if (plen+3 >= psize) {
215 psize = plen + 256;
216 p = resize(p, psize);
217 }
218 if (plen == 0 && (outc == '.' || outc == '\'') &&
219 (quote_props & QUOTE_INITCTRL)) {
220 /*
221 * Control character (. or ') at the start of a
222 * line. Quote it by putting \& (troff zero-width
223 * space) before it.
224 */
225 p[plen++] = '\\';
226 p[plen++] = '&';
227 } else if (outc == '\\') {
228 /*
229 * Quote backslashes by doubling them, always.
230 */
231 p[plen++] = '\\';
232 } else if (outc == '"' && (quote_props & QUOTE_QUOTES)) {
233 /*
234 * Double quote within double quotes. Quote it by
235 * doubling.
236 */
237 p[plen++] = '"';
238 }
239 p[plen++] = outc;
240 }
241 }
242 if (doing) {
243 p = resize(p, plen+1);
244 p[plen] = '\0';
245 *result = p;
246 }
247 return ok;
248}
249
250static void man_rdaddwc(rdstringc *rs, word *text, word *end,
251 int quote_props) {
252 char *c;
253
254 for (; text && text != end; text = text->next) switch (text->type) {
255 case word_HyperLink:
256 case word_HyperEnd:
257 case word_UpperXref:
258 case word_LowerXref:
259 case word_XrefEnd:
260 case word_IndexRef:
261 break;
262
263 case word_Normal:
264 case word_Emph:
265 case word_Code:
266 case word_WeakCode:
267 case word_WhiteSpace:
268 case word_EmphSpace:
269 case word_CodeSpace:
270 case word_WkCodeSpace:
271 case word_Quote:
272 case word_EmphQuote:
273 case word_CodeQuote:
274 case word_WkCodeQuote:
275 assert(text->type != word_CodeQuote &&
276 text->type != word_WkCodeQuote);
277 if (towordstyle(text->type) == word_Emph &&
278 (attraux(text->aux) == attr_First ||
279 attraux(text->aux) == attr_Only))
280 rdaddsc(rs, "\\fI");
281 else if (towordstyle(text->type) == word_Code &&
282 (attraux(text->aux) == attr_First ||
283 attraux(text->aux) == attr_Only))
284 rdaddsc(rs, "\\fB");
285 if (removeattr(text->type) == word_Normal) {
286 if (rs->pos > 0)
287 quote_props &= ~QUOTE_INITCTRL; /* not at start any more */
288 if (man_convert(text->text, &c, quote_props))
289 rdaddsc(rs, c);
290 else
291 man_rdaddwc(rs, text->alt, NULL, quote_props);
292 sfree(c);
293 } else if (removeattr(text->type) == word_WhiteSpace) {
294 rdaddc(rs, ' ');
295 } else if (removeattr(text->type) == word_Quote) {
296 rdaddc(rs, quoteaux(text->aux) == quote_Open ? '`' : '\'');
297 /* FIXME: configurability */
298 }
299 if (towordstyle(text->type) == word_Emph &&
300 (attraux(text->aux) == attr_Last ||
301 attraux(text->aux) == attr_Only))
302 rdaddsc(rs, "\\fP");
303 else if (towordstyle(text->type) == word_Code &&
304 (attraux(text->aux) == attr_Last ||
305 attraux(text->aux) == attr_Only))
306 rdaddsc(rs, "\\fP");
307 break;
308 }
309}
310
311static void man_text(FILE *fp, word *text, int newline, int quote_props) {
312 rdstringc t = { 0, 0, NULL };
313
314 man_rdaddwc(&t, text, NULL, quote_props | QUOTE_INITCTRL);
315 fprintf(fp, "%s", t.text);
316 sfree(t.text);
317 if (newline)
318 fputc('\n', fp);
319}
320
321static void man_codepara(FILE *fp, word *text) {
322 fprintf(fp, ".nf\n");
323 for (; text; text = text->next) if (text->type == word_WeakCode) {
324 char *c;
325 man_convert(text->text, &c, QUOTE_INITCTRL);
326 fprintf(fp, "%s\n", c);
327 sfree(c);
328 }
329 fprintf(fp, ".fi\n");
330}