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
= dupstr(adv(source
->origkeyword
));
59 static void man_conf_cleanup(manconfig cf
)
65 paragraph
*man_config_filename(char *filename
)
67 return cmdline_cfg_simple("man-filename", filename
, NULL
);
70 #define QUOTE_INITCTRL 1 /* quote initial . and ' on a line */
71 #define QUOTE_QUOTES 2 /* quote double quotes by doubling them */
73 void man_backend(paragraph
*sourceform
, keywordlist
*keywords
,
74 indexdata
*idx
, void *unused
) {
83 conf
= man_configure(sourceform
);
86 * Open the output file.
88 fp
= fopen(conf
.filename
, "w");
90 error(err_cantopenw
, conf
.filename
);
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);
101 /* .TH name-of-program manual-section */
103 if (conf
.th
&& *conf
.th
) {
107 for (wp
= conf
.th
; *wp
; wp
= uadv(wp
)) {
109 man_convert(wp
, 0, &c
, QUOTE_QUOTES
);
117 fprintf(fp
, ".UC\n");
119 for (p
= sourceform
; p
; p
= p
->next
) switch (p
->type
) {
121 * Things we ignore because we've already processed them or
122 * aren't going to touch them in this pass.
126 case para_Biblio
: /* only touch BiblioCited */
137 case para_UnnumberedChapter
:
143 if (p
->type
== para_Subsect
)
145 else if (p
->type
== para_Heading
)
149 if (depth
>= conf
.mindepth
) {
150 fprintf(fp
, ".SH \"");
151 if (conf
.headnumbers
&& p
->kwtext
) {
152 man_text(fp
, p
->kwtext
, FALSE
, QUOTE_QUOTES
);
155 man_text(fp
, p
->words
, FALSE
, QUOTE_QUOTES
);
165 fprintf(fp
, ".PP\n");
166 man_codepara(fp
, p
->words
);
174 fprintf(fp
, ".PP\n");
175 man_text(fp
, p
->words
, TRUE
, 0);
181 case para_Description
:
182 case para_BiblioCited
:
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
);
191 } else if (p
->type
== para_Description
) {
193 * Do nothing; the .xP for this paragraph is the .IP
194 * which has come before it in the DescribedThing.
196 } else if (p
->type
== para_BiblioCited
) {
197 fprintf(fp
, ".IP \"");
198 man_text(fp
, p
->kwtext
, FALSE
, QUOTE_QUOTES
);
201 man_text(fp
, p
->words
, TRUE
, 0);
204 case para_DescribedThing
:
205 fprintf(fp
, ".IP \"");
206 man_text(fp
, p
->words
, FALSE
, QUOTE_QUOTES
);
212 * This isn't terribly good. Anyone who wants to do better
215 fprintf(fp
, ".PP\n----------------------------------------\n");
220 fprintf(fp
, ".RS\n");
224 fprintf(fp
, ".RE\n");
232 man_conf_cleanup(conf
);
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
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!
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.
250 static int man_convert(wchar_t *s
, int maxlen
,
251 char **result
, int quote_props
) {
253 * FIXME. Currently this is ISO8859-1 only.
255 int doing
= (result
!= 0);
258 int plen
= 0, psize
= 0;
263 for (; *s
&& maxlen
!= 0; s
++, maxlen
--) {
267 if ((c
>= 32 && c
<= 126) ||
268 (c
>= 160 && c
<= 255)) {
272 /* Char is not OK. */
274 outc
= 0xBF; /* approximate the good old DEC `uh?' */
277 if (plen
+3 >= psize
) {
279 p
= resize(p
, psize
);
281 if (plen
== 0 && (outc
== '.' || outc
== '\'') &&
282 (quote_props
& QUOTE_INITCTRL
)) {
284 * Control character (. or ') at the start of a
285 * line. Quote it by putting \& (troff zero-width
290 } else if (outc
== '\\') {
292 * Quote backslashes by doubling them, always.
295 } else if (outc
== '"' && (quote_props
& QUOTE_QUOTES
)) {
297 * Double quote within double quotes. Quote it by
306 p
= resize(p
, plen
+1);
313 static void man_rdaddwc(rdstringc
*rs
, word
*text
, word
*end
,
317 for (; text
&& text
!= end
; text
= text
->next
) switch (text
->type
) {
330 case word_WhiteSpace
:
333 case word_WkCodeSpace
:
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
))
344 else if ((towordstyle(text
->type
) == word_Code
||
345 towordstyle(text
->type
) == word_WeakCode
) &&
346 (attraux(text
->aux
) == attr_First
||
347 attraux(text
->aux
) == attr_Only
))
349 if (removeattr(text
->type
) == word_Normal
) {
351 quote_props
&= ~QUOTE_INITCTRL
; /* not at start any more */
352 if (man_convert(text
->text
, 0, &c
, quote_props
) || !text
->alt
)
355 man_rdaddwc(rs
, text
->alt
, NULL
, quote_props
);
357 } else if (removeattr(text
->type
) == word_WhiteSpace
) {
359 } else if (removeattr(text
->type
) == word_Quote
) {
361 if (quote_props
& QUOTE_QUOTES
)
364 if (towordstyle(text
->type
) == word_Emph
&&
365 (attraux(text
->aux
) == attr_Last
||
366 attraux(text
->aux
) == attr_Only
))
368 else if ((towordstyle(text
->type
) == word_Code
||
369 towordstyle(text
->type
) == word_WeakCode
) &&
370 (attraux(text
->aux
) == attr_Last
||
371 attraux(text
->aux
) == attr_Only
))
377 static void man_text(FILE *fp
, word
*text
, int newline
, int quote_props
) {
378 rdstringc t
= { 0, 0, NULL
};
380 man_rdaddwc(&t
, text
, NULL
, quote_props
| QUOTE_INITCTRL
);
381 fprintf(fp
, "%s", t
.text
);
387 static void man_codepara(FILE *fp
, word
*text
) {
388 fprintf(fp
, ".nf\n");
389 for (; text
; text
= text
->next
) if (text
->type
== word_WeakCode
) {
392 int quote_props
= QUOTE_INITCTRL
;
395 if (text
->next
&& text
->next
->type
== word_Emph
) {
396 e
= text
->next
->text
;
401 while (e
&& *e
&& *t
) {
405 for (n
= 0; t
[n
] && e
[n
] && e
[n
] == ec
; n
++);
410 man_convert(t
, n
, &c
, quote_props
);
411 quote_props
&= ~QUOTE_INITCTRL
;
412 fprintf(fp
, "%s", c
);
414 if (ec
== 'i' || ec
== 'b')
419 man_convert(t
, 0, &c
, quote_props
);
420 fprintf(fp
, "%s\n", c
);
423 fprintf(fp
, ".fi\n");