2 * man page backend for Halibut
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
);
22 static manconfig
man_configure(paragraph
*source
) {
29 ret
.headnumbers
= FALSE
;
31 ret
.filename
= dupstr("output.1");
33 for (; source
; source
= source
->next
) {
34 if (source
->type
== para_Config
) {
35 if (!ustricmp(source
->keyword
, L
"man-identity")) {
38 wp
= uadv(source
->keyword
);
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")) {
51 ret
.filename
= utoa_dup(uadv(source
->keyword
));
59 static void man_conf_cleanup(manconfig cf
)
65 #define QUOTE_INITCTRL 1 /* quote initial . and ' on a line */
66 #define QUOTE_QUOTES 2 /* quote double quotes by doubling them */
68 void man_backend(paragraph
*sourceform
, keywordlist
*keywords
,
74 IGNORE(keywords
); /* we don't happen to need this */
75 IGNORE(idx
); /* or this */
77 conf
= man_configure(sourceform
);
80 * Open the output file.
82 fp
= fopen(conf
.filename
, "w");
84 error(err_cantopenw
, conf
.filename
);
88 /* Do the version ID */
89 for (p
= sourceform
; p
; p
= p
->next
)
90 if (p
->type
== para_VersionID
) {
91 fprintf(fp
, ".\\\" ");
92 man_text(fp
, p
->words
, TRUE
, 0);
95 /* .TH name-of-program manual-section */
97 if (conf
.th
&& *conf
.th
) {
101 for (wp
= conf
.th
; *wp
; wp
= uadv(wp
)) {
103 man_convert(wp
, 0, &c
, QUOTE_QUOTES
);
111 fprintf(fp
, ".UC\n");
113 for (p
= sourceform
; p
; p
= p
->next
) switch (p
->type
) {
115 * Things we ignore because we've already processed them or
116 * aren't going to touch them in this pass.
120 case para_Biblio
: /* only touch BiblioCited */
131 case para_UnnumberedChapter
:
137 if (p
->type
== para_Subsect
)
139 else if (p
->type
== para_Heading
)
143 if (depth
>= conf
.mindepth
) {
144 fprintf(fp
, ".SH \"");
145 if (conf
.headnumbers
&& p
->kwtext
) {
146 man_text(fp
, p
->kwtext
, FALSE
, QUOTE_QUOTES
);
149 man_text(fp
, p
->words
, FALSE
, QUOTE_QUOTES
);
159 fprintf(fp
, ".PP\n");
160 man_codepara(fp
, p
->words
);
168 fprintf(fp
, ".PP\n");
169 man_text(fp
, p
->words
, TRUE
, 0);
175 case para_Description
:
176 case para_BiblioCited
:
178 case para_NumberedList
:
179 if (p
->type
== para_Bullet
) {
180 fprintf(fp
, ".IP \"\\fBo\\fP\"\n"); /* FIXME: configurable? */
181 } else if (p
->type
== para_NumberedList
) {
182 fprintf(fp
, ".IP \"");
183 man_text(fp
, p
->kwtext
, FALSE
, QUOTE_QUOTES
);
185 } else if (p
->type
== para_Description
) {
187 * Do nothing; the .xP for this paragraph is the .IP
188 * which has come before it in the DescribedThing.
190 } else if (p
->type
== para_BiblioCited
) {
191 fprintf(fp
, ".IP \"");
192 man_text(fp
, p
->kwtext
, FALSE
, QUOTE_QUOTES
);
195 man_text(fp
, p
->words
, TRUE
, 0);
198 case para_DescribedThing
:
199 fprintf(fp
, ".IP \"");
200 man_text(fp
, p
->words
, FALSE
, QUOTE_QUOTES
);
206 * This isn't terribly good. Anyone who wants to do better
209 fprintf(fp
, ".PP\n----------------------------------------\n");
214 fprintf(fp
, ".RS\n");
218 fprintf(fp
, ".RE\n");
226 man_conf_cleanup(conf
);
230 * Convert a wide string into a string of chars. If `result' is
231 * non-NULL, mallocs the resulting string and stores a pointer to
232 * it in `*result'. If `result' is NULL, merely checks whether all
233 * characters in the string are feasible for the output character
236 * Return is nonzero if all characters are OK. If not all
237 * characters are OK but `result' is non-NULL, a result _will_
238 * still be generated!
240 * FIXME: Here is probably also a good place to do escaping sorts
241 * of things. I know I at least need to escape backslash, and full
242 * stops at the starts of words are probably trouble as well.
244 static int man_convert(wchar_t *s
, int maxlen
,
245 char **result
, int quote_props
) {
247 * FIXME. Currently this is ISO8859-1 only.
249 int doing
= (result
!= 0);
252 int plen
= 0, psize
= 0;
257 for (; *s
&& maxlen
!= 0; s
++, maxlen
--) {
261 if ((c
>= 32 && c
<= 126) ||
262 (c
>= 160 && c
<= 255)) {
266 /* Char is not OK. */
268 outc
= 0xBF; /* approximate the good old DEC `uh?' */
271 if (plen
+3 >= psize
) {
273 p
= resize(p
, psize
);
275 if (plen
== 0 && (outc
== '.' || outc
== '\'') &&
276 (quote_props
& QUOTE_INITCTRL
)) {
278 * Control character (. or ') at the start of a
279 * line. Quote it by putting \& (troff zero-width
284 } else if (outc
== '\\') {
286 * Quote backslashes by doubling them, always.
289 } else if (outc
== '"' && (quote_props
& QUOTE_QUOTES
)) {
291 * Double quote within double quotes. Quote it by
300 p
= resize(p
, plen
+1);
307 static void man_rdaddwc(rdstringc
*rs
, word
*text
, word
*end
,
311 for (; text
&& text
!= end
; text
= text
->next
) switch (text
->type
) {
324 case word_WhiteSpace
:
327 case word_WkCodeSpace
:
331 case word_WkCodeQuote
:
332 assert(text
->type
!= word_CodeQuote
&&
333 text
->type
!= word_WkCodeQuote
);
334 if (towordstyle(text
->type
) == word_Emph
&&
335 (attraux(text
->aux
) == attr_First
||
336 attraux(text
->aux
) == attr_Only
))
338 else if ((towordstyle(text
->type
) == word_Code
||
339 towordstyle(text
->type
) == word_WeakCode
) &&
340 (attraux(text
->aux
) == attr_First
||
341 attraux(text
->aux
) == attr_Only
))
343 if (removeattr(text
->type
) == word_Normal
) {
345 quote_props
&= ~QUOTE_INITCTRL
; /* not at start any more */
346 if (man_convert(text
->text
, 0, &c
, quote_props
))
349 man_rdaddwc(rs
, text
->alt
, NULL
, quote_props
);
351 } else if (removeattr(text
->type
) == word_WhiteSpace
) {
353 } else if (removeattr(text
->type
) == word_Quote
) {
355 if (quote_props
& QUOTE_QUOTES
)
358 if (towordstyle(text
->type
) == word_Emph
&&
359 (attraux(text
->aux
) == attr_Last
||
360 attraux(text
->aux
) == attr_Only
))
362 else if ((towordstyle(text
->type
) == word_Code
||
363 towordstyle(text
->type
) == word_WeakCode
) &&
364 (attraux(text
->aux
) == attr_Last
||
365 attraux(text
->aux
) == attr_Only
))
371 static void man_text(FILE *fp
, word
*text
, int newline
, int quote_props
) {
372 rdstringc t
= { 0, 0, NULL
};
374 man_rdaddwc(&t
, text
, NULL
, quote_props
| QUOTE_INITCTRL
);
375 fprintf(fp
, "%s", t
.text
);
381 static void man_codepara(FILE *fp
, word
*text
) {
382 fprintf(fp
, ".nf\n");
383 for (; text
; text
= text
->next
) if (text
->type
== word_WeakCode
) {
386 int quote_props
= QUOTE_INITCTRL
;
389 if (text
->next
&& text
->next
->type
== word_Emph
) {
390 e
= text
->next
->text
;
395 while (e
&& *e
&& *t
) {
399 for (n
= 0; t
[n
] && e
[n
] && e
[n
] == ec
; n
++);
404 man_convert(t
, n
, &c
, quote_props
);
405 quote_props
&= ~QUOTE_INITCTRL
;
406 fprintf(fp
, "%s", c
);
408 if (ec
== 'i' || ec
== 'b')
413 man_convert(t
, 0, &c
, quote_props
);
414 fprintf(fp
, "%s\n", c
);
417 fprintf(fp
, ".fi\n");