2 * man page backend for Halibut
16 wchar_t *bullet
, *lquote
, *rquote
;
19 static void man_text(FILE *, word
*,
20 int newline
, int quote_props
, manconfig
*conf
);
21 static void man_codepara(FILE *, word
*, int charset
);
22 static int man_convert(wchar_t const *s
, int maxlen
,
23 char **result
, int quote_props
,
24 int charset
, charset_state
*state
);
26 static manconfig
man_configure(paragraph
*source
) {
34 ret
.headnumbers
= FALSE
;
36 ret
.filename
= dupstr("output.1");
37 ret
.charset
= CS_ASCII
;
38 ret
.bullet
= L
"\x2022\0o\0\0";
39 ret
.lquote
= L
"\x2018\0\x2019\0\"\0\"\0\0";
40 ret
.rquote
= uadv(ret
.lquote
);
43 * Two-pass configuration so that we can pick up global config
44 * (e.g. `quotes') before having it overridden by specific
45 * config (`man-quotes'), irrespective of the order in which
48 for (p
= source
; p
; p
= p
->next
) {
49 if (p
->type
== para_Config
) {
50 if (!ustricmp(p
->keyword
, L
"quotes")) {
51 if (*uadv(p
->keyword
) && *uadv(uadv(p
->keyword
))) {
52 ret
.lquote
= uadv(p
->keyword
);
53 ret
.rquote
= uadv(ret
.lquote
);
59 for (p
= source
; p
; p
= p
->next
) {
60 if (p
->type
== para_Config
) {
61 if (!ustricmp(p
->keyword
, L
"man-identity")) {
64 wp
= uadv(p
->keyword
);
69 ret
.th
= snewn(ep
- wp
+ 1, wchar_t);
70 memcpy(ret
.th
, wp
, (ep
- wp
+ 1) * sizeof(wchar_t));
71 } else if (!ustricmp(p
->keyword
, L
"man-charset")) {
72 char *csname
= utoa_dup(uadv(p
->keyword
), CS_ASCII
);
73 ret
.charset
= charset_from_localenc(csname
);
75 } else if (!ustricmp(p
->keyword
, L
"man-headnumbers")) {
76 ret
.headnumbers
= utob(uadv(p
->keyword
));
77 } else if (!ustricmp(p
->keyword
, L
"man-mindepth")) {
78 ret
.mindepth
= utoi(uadv(p
->keyword
));
79 } else if (!ustricmp(p
->keyword
, L
"man-filename")) {
81 ret
.filename
= dupstr(adv(p
->origkeyword
));
82 } else if (!ustricmp(p
->keyword
, L
"man-bullet")) {
83 ret
.bullet
= uadv(p
->keyword
);
84 } else if (!ustricmp(p
->keyword
, L
"man-quotes")) {
85 if (*uadv(p
->keyword
) && *uadv(uadv(p
->keyword
))) {
86 ret
.lquote
= uadv(p
->keyword
);
87 ret
.rquote
= uadv(ret
.lquote
);
94 * Now process fallbacks on quote characters and bullets.
96 while (*uadv(ret
.rquote
) && *uadv(uadv(ret
.rquote
)) &&
97 (!cvt_ok(ret
.charset
, ret
.lquote
) ||
98 !cvt_ok(ret
.charset
, ret
.rquote
))) {
99 ret
.lquote
= uadv(ret
.rquote
);
100 ret
.rquote
= uadv(ret
.lquote
);
103 while (*ret
.bullet
&& *uadv(ret
.bullet
) &&
104 !cvt_ok(ret
.charset
, ret
.bullet
))
105 ret
.bullet
= uadv(ret
.bullet
);
110 static void man_conf_cleanup(manconfig cf
)
116 paragraph
*man_config_filename(char *filename
)
118 return cmdline_cfg_simple("man-filename", filename
, NULL
);
121 #define QUOTE_INITCTRL 1 /* quote initial . and ' on a line */
122 #define QUOTE_QUOTES 2 /* quote double quotes by doubling them */
124 void man_backend(paragraph
*sourceform
, keywordlist
*keywords
,
125 indexdata
*idx
, void *unused
) {
134 conf
= man_configure(sourceform
);
137 * Open the output file.
139 fp
= fopen(conf
.filename
, "w");
141 error(err_cantopenw
, conf
.filename
);
145 /* Do the version ID */
146 for (p
= sourceform
; p
; p
= p
->next
)
147 if (p
->type
== para_VersionID
) {
148 fprintf(fp
, ".\\\" ");
149 man_text(fp
, p
->words
, TRUE
, 0, &conf
);
152 /* .TH name-of-program manual-section */
154 if (conf
.th
&& *conf
.th
) {
158 for (wp
= conf
.th
; *wp
; wp
= uadv(wp
)) {
160 man_convert(wp
, 0, &c
, QUOTE_QUOTES
, conf
.charset
, NULL
);
168 fprintf(fp
, ".UC\n");
170 for (p
= sourceform
; p
; p
= p
->next
) switch (p
->type
) {
172 * Things we ignore because we've already processed them or
173 * aren't going to touch them in this pass.
177 case para_Biblio
: /* only touch BiblioCited */
188 case para_UnnumberedChapter
:
194 if (p
->type
== para_Subsect
)
196 else if (p
->type
== para_Heading
)
200 if (depth
>= conf
.mindepth
) {
201 fprintf(fp
, ".SH \"");
202 if (conf
.headnumbers
&& p
->kwtext
) {
203 man_text(fp
, p
->kwtext
, FALSE
, QUOTE_QUOTES
, &conf
);
206 man_text(fp
, p
->words
, FALSE
, QUOTE_QUOTES
, &conf
);
216 fprintf(fp
, ".PP\n");
217 man_codepara(fp
, p
->words
, conf
.charset
);
225 fprintf(fp
, ".PP\n");
226 man_text(fp
, p
->words
, TRUE
, 0, &conf
);
232 case para_Description
:
233 case para_BiblioCited
:
235 case para_NumberedList
:
236 if (p
->type
== para_Bullet
) {
238 man_convert(conf
.bullet
, -1, &bullettext
, QUOTE_QUOTES
,
240 fprintf(fp
, ".IP \"\\fB%s\\fP\"\n", bullettext
);
242 } else if (p
->type
== para_NumberedList
) {
243 fprintf(fp
, ".IP \"");
244 man_text(fp
, p
->kwtext
, FALSE
, QUOTE_QUOTES
, &conf
);
246 } else if (p
->type
== para_Description
) {
248 * Do nothing; the .xP for this paragraph is the .IP
249 * which has come before it in the DescribedThing.
251 } else if (p
->type
== para_BiblioCited
) {
252 fprintf(fp
, ".IP \"");
253 man_text(fp
, p
->kwtext
, FALSE
, QUOTE_QUOTES
, &conf
);
256 man_text(fp
, p
->words
, TRUE
, 0, &conf
);
259 case para_DescribedThing
:
260 fprintf(fp
, ".IP \"");
261 man_text(fp
, p
->words
, FALSE
, QUOTE_QUOTES
, &conf
);
267 * This isn't terribly good. Anyone who wants to do better
270 fprintf(fp
, ".PP\n----------------------------------------\n");
275 fprintf(fp
, ".RS\n");
279 fprintf(fp
, ".RE\n");
287 man_conf_cleanup(conf
);
291 * Convert a wide string into a string of chars; mallocs the
292 * resulting string and stores a pointer to it in `*result'.
294 * If `state' is non-NULL, updates the charset state pointed to. If
295 * `state' is NULL, this function uses its own state, initialises
296 * it from scratch, and cleans it up when finished. If `state' is
297 * non-NULL but _s_ is NULL, cleans up a provided state.
299 * Return is nonzero if all characters are OK. If not all
300 * characters are OK but `result' is non-NULL, a result _will_
301 * still be generated!
303 * This function also does escaping of groff special characters.
305 static int man_convert(wchar_t const *s
, int maxlen
,
306 char **result
, int quote_props
,
307 int charset
, charset_state
*state
) {
308 charset_state internal_state
= CHARSET_INIT_STATE
;
311 int plen
= 0, psize
= 0;
312 rdstringc out
= {0, 0, NULL
};
315 state
= &internal_state
;
317 slen
= (s ?
ustrlen(s
) : 0);
319 if (slen
> maxlen
&& maxlen
> 0)
324 p
= snewn(psize
, char);
328 int ret
= charset_from_unicode(&s
, &slen
, p
+plen
, psize
-plen
,
329 charset
, state
, (err ? NULL
: &err
));
332 if (psize
- plen
< 256) {
334 p
= sresize(p
, psize
, char);
339 if (state
== &internal_state
|| s
== NULL
) {
340 int ret
= charset_from_unicode(NULL
, 0, p
+plen
, psize
-plen
,
341 charset
, state
, NULL
);
346 for (q
= p
; q
< p
+plen
; q
++) {
347 if (q
== p
&& (*q
== '.' || *q
== '\'') &&
348 (quote_props
& QUOTE_INITCTRL
)) {
350 * Control character (. or ') at the start of a
351 * line. Quote it by putting \& (troff zero-width
356 } else if (*q
== '\\') {
358 * Quote backslashes by doubling them, always.
361 } else if (*q
== '"' && (quote_props
& QUOTE_QUOTES
)) {
363 * Double quote within double quotes. Quote it by
374 *result
= rdtrimc(&out
);
376 *result
= dupstr("");
381 static void man_rdaddwc(rdstringc
*rs
, word
*text
, word
*end
,
382 int quote_props
, manconfig
*conf
,
383 charset_state
*state
) {
386 for (; text
&& text
!= end
; text
= text
->next
) switch (text
->type
) {
399 case word_WhiteSpace
:
402 case word_WkCodeSpace
:
406 case word_WkCodeQuote
:
407 assert(text
->type
!= word_CodeQuote
&&
408 text
->type
!= word_WkCodeQuote
);
410 if (towordstyle(text
->type
) == word_Emph
&&
411 (attraux(text
->aux
) == attr_First
||
412 attraux(text
->aux
) == attr_Only
)) {
414 quote_props
&= ~QUOTE_INITCTRL
; /* not at start any more */
415 man_convert(NULL
, 0, &c
, quote_props
, conf
->charset
, state
);
418 *state
= charset_init_state
;
420 } else if ((towordstyle(text
->type
) == word_Code
||
421 towordstyle(text
->type
) == word_WeakCode
) &&
422 (attraux(text
->aux
) == attr_First
||
423 attraux(text
->aux
) == attr_Only
)) {
425 quote_props
&= ~QUOTE_INITCTRL
; /* not at start any more */
426 man_convert(NULL
, 0, &c
, quote_props
, conf
->charset
, state
);
429 *state
= charset_init_state
;
433 if (removeattr(text
->type
) == word_Normal
) {
434 charset_state s2
= *state
;
437 quote_props
&= ~QUOTE_INITCTRL
; /* not at start any more */
438 if (man_convert(text
->text
, 0, &c
, quote_props
, conf
->charset
, &s2
) ||
443 man_rdaddwc(rs
, text
->alt
, NULL
, quote_props
, conf
, state
);
446 } else if (removeattr(text
->type
) == word_WhiteSpace
) {
448 quote_props
&= ~QUOTE_INITCTRL
; /* not at start any more */
449 man_convert(L
" ", 1, &c
, quote_props
, conf
->charset
, state
);
452 } else if (removeattr(text
->type
) == word_Quote
) {
454 quote_props
&= ~QUOTE_INITCTRL
; /* not at start any more */
455 man_convert(quoteaux(text
->aux
) == quote_Open ?
456 conf
->lquote
: conf
->rquote
, 0,
457 &c
, quote_props
, conf
->charset
, state
);
461 if (towordstyle(text
->type
) != word_Normal
&&
462 (attraux(text
->aux
) == attr_Last
||
463 attraux(text
->aux
) == attr_Only
)) {
465 quote_props
&= ~QUOTE_INITCTRL
; /* not at start any more */
466 man_convert(NULL
, 0, &c
, quote_props
, conf
->charset
, state
);
469 *state
= charset_init_state
;
474 man_convert(NULL
, 0, &c
, quote_props
, conf
->charset
, state
);
479 static void man_text(FILE *fp
, word
*text
, int newline
,
480 int quote_props
, manconfig
*conf
) {
481 rdstringc t
= { 0, 0, NULL
};
482 charset_state state
= CHARSET_INIT_STATE
;
484 man_rdaddwc(&t
, text
, NULL
, quote_props
| QUOTE_INITCTRL
, conf
, &state
);
485 fprintf(fp
, "%s", t
.text
);
491 static void man_codepara(FILE *fp
, word
*text
, int charset
) {
492 fprintf(fp
, ".nf\n");
493 for (; text
; text
= text
->next
) if (text
->type
== word_WeakCode
) {
496 int quote_props
= QUOTE_INITCTRL
;
499 if (text
->next
&& text
->next
->type
== word_Emph
) {
500 e
= text
->next
->text
;
505 while (e
&& *e
&& *t
) {
509 for (n
= 0; t
[n
] && e
[n
] && e
[n
] == ec
; n
++);
514 man_convert(t
, n
, &c
, quote_props
, charset
, NULL
);
515 quote_props
&= ~QUOTE_INITCTRL
;
516 fprintf(fp
, "%s", c
);
518 if (ec
== 'i' || ec
== 'b')
523 man_convert(t
, 0, &c
, quote_props
, charset
, NULL
);
524 fprintf(fp
, "%s\n", c
);
527 fprintf(fp
, ".fi\n");