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 paragraph
*man_config_filename(char *filename
)
68 wchar_t *ufilename
, *up
;
72 memset(p
, 0, sizeof(*p
));
73 p
->type
= para_Config
;
75 p
->fpos
.filename
= "<command line>";
76 p
->fpos
.line
= p
->fpos
.col
= -1;
78 ufilename
= ufroma_dup(filename
);
79 len
= ustrlen(ufilename
) + 2 + lenof(L
"man-filename");
80 p
->keyword
= mknewa(wchar_t, len
);
82 ustrcpy(up
, L
"man-filename");
84 ustrcpy(up
, ufilename
);
87 assert(up
- p
->keyword
< len
);
93 #define QUOTE_INITCTRL 1 /* quote initial . and ' on a line */
94 #define QUOTE_QUOTES 2 /* quote double quotes by doubling them */
96 void man_backend(paragraph
*sourceform
, keywordlist
*keywords
,
97 indexdata
*idx
, void *unused
) {
106 conf
= man_configure(sourceform
);
109 * Open the output file.
111 fp
= fopen(conf
.filename
, "w");
113 error(err_cantopenw
, conf
.filename
);
117 /* Do the version ID */
118 for (p
= sourceform
; p
; p
= p
->next
)
119 if (p
->type
== para_VersionID
) {
120 fprintf(fp
, ".\\\" ");
121 man_text(fp
, p
->words
, TRUE
, 0);
124 /* .TH name-of-program manual-section */
126 if (conf
.th
&& *conf
.th
) {
130 for (wp
= conf
.th
; *wp
; wp
= uadv(wp
)) {
132 man_convert(wp
, 0, &c
, QUOTE_QUOTES
);
140 fprintf(fp
, ".UC\n");
142 for (p
= sourceform
; p
; p
= p
->next
) switch (p
->type
) {
144 * Things we ignore because we've already processed them or
145 * aren't going to touch them in this pass.
149 case para_Biblio
: /* only touch BiblioCited */
160 case para_UnnumberedChapter
:
166 if (p
->type
== para_Subsect
)
168 else if (p
->type
== para_Heading
)
172 if (depth
>= conf
.mindepth
) {
173 fprintf(fp
, ".SH \"");
174 if (conf
.headnumbers
&& p
->kwtext
) {
175 man_text(fp
, p
->kwtext
, FALSE
, QUOTE_QUOTES
);
178 man_text(fp
, p
->words
, FALSE
, QUOTE_QUOTES
);
188 fprintf(fp
, ".PP\n");
189 man_codepara(fp
, p
->words
);
197 fprintf(fp
, ".PP\n");
198 man_text(fp
, p
->words
, TRUE
, 0);
204 case para_Description
:
205 case para_BiblioCited
:
207 case para_NumberedList
:
208 if (p
->type
== para_Bullet
) {
209 fprintf(fp
, ".IP \"\\fBo\\fP\"\n"); /* FIXME: configurable? */
210 } else if (p
->type
== para_NumberedList
) {
211 fprintf(fp
, ".IP \"");
212 man_text(fp
, p
->kwtext
, FALSE
, QUOTE_QUOTES
);
214 } else if (p
->type
== para_Description
) {
216 * Do nothing; the .xP for this paragraph is the .IP
217 * which has come before it in the DescribedThing.
219 } else if (p
->type
== para_BiblioCited
) {
220 fprintf(fp
, ".IP \"");
221 man_text(fp
, p
->kwtext
, FALSE
, QUOTE_QUOTES
);
224 man_text(fp
, p
->words
, TRUE
, 0);
227 case para_DescribedThing
:
228 fprintf(fp
, ".IP \"");
229 man_text(fp
, p
->words
, FALSE
, QUOTE_QUOTES
);
235 * This isn't terribly good. Anyone who wants to do better
238 fprintf(fp
, ".PP\n----------------------------------------\n");
243 fprintf(fp
, ".RS\n");
247 fprintf(fp
, ".RE\n");
255 man_conf_cleanup(conf
);
259 * Convert a wide string into a string of chars. If `result' is
260 * non-NULL, mallocs the resulting string and stores a pointer to
261 * it in `*result'. If `result' is NULL, merely checks whether all
262 * characters in the string are feasible for the output character
265 * Return is nonzero if all characters are OK. If not all
266 * characters are OK but `result' is non-NULL, a result _will_
267 * still be generated!
269 * FIXME: Here is probably also a good place to do escaping sorts
270 * of things. I know I at least need to escape backslash, and full
271 * stops at the starts of words are probably trouble as well.
273 static int man_convert(wchar_t *s
, int maxlen
,
274 char **result
, int quote_props
) {
276 * FIXME. Currently this is ISO8859-1 only.
278 int doing
= (result
!= 0);
281 int plen
= 0, psize
= 0;
286 for (; *s
&& maxlen
!= 0; s
++, maxlen
--) {
290 if ((c
>= 32 && c
<= 126) ||
291 (c
>= 160 && c
<= 255)) {
295 /* Char is not OK. */
297 outc
= 0xBF; /* approximate the good old DEC `uh?' */
300 if (plen
+3 >= psize
) {
302 p
= resize(p
, psize
);
304 if (plen
== 0 && (outc
== '.' || outc
== '\'') &&
305 (quote_props
& QUOTE_INITCTRL
)) {
307 * Control character (. or ') at the start of a
308 * line. Quote it by putting \& (troff zero-width
313 } else if (outc
== '\\') {
315 * Quote backslashes by doubling them, always.
318 } else if (outc
== '"' && (quote_props
& QUOTE_QUOTES
)) {
320 * Double quote within double quotes. Quote it by
329 p
= resize(p
, plen
+1);
336 static void man_rdaddwc(rdstringc
*rs
, word
*text
, word
*end
,
340 for (; text
&& text
!= end
; text
= text
->next
) switch (text
->type
) {
353 case word_WhiteSpace
:
356 case word_WkCodeSpace
:
360 case word_WkCodeQuote
:
361 assert(text
->type
!= word_CodeQuote
&&
362 text
->type
!= word_WkCodeQuote
);
363 if (towordstyle(text
->type
) == word_Emph
&&
364 (attraux(text
->aux
) == attr_First
||
365 attraux(text
->aux
) == attr_Only
))
367 else if ((towordstyle(text
->type
) == word_Code
||
368 towordstyle(text
->type
) == word_WeakCode
) &&
369 (attraux(text
->aux
) == attr_First
||
370 attraux(text
->aux
) == attr_Only
))
372 if (removeattr(text
->type
) == word_Normal
) {
374 quote_props
&= ~QUOTE_INITCTRL
; /* not at start any more */
375 if (man_convert(text
->text
, 0, &c
, quote_props
) || !text
->alt
)
378 man_rdaddwc(rs
, text
->alt
, NULL
, quote_props
);
380 } else if (removeattr(text
->type
) == word_WhiteSpace
) {
382 } else if (removeattr(text
->type
) == word_Quote
) {
384 if (quote_props
& QUOTE_QUOTES
)
387 if (towordstyle(text
->type
) == word_Emph
&&
388 (attraux(text
->aux
) == attr_Last
||
389 attraux(text
->aux
) == attr_Only
))
391 else if ((towordstyle(text
->type
) == word_Code
||
392 towordstyle(text
->type
) == word_WeakCode
) &&
393 (attraux(text
->aux
) == attr_Last
||
394 attraux(text
->aux
) == attr_Only
))
400 static void man_text(FILE *fp
, word
*text
, int newline
, int quote_props
) {
401 rdstringc t
= { 0, 0, NULL
};
403 man_rdaddwc(&t
, text
, NULL
, quote_props
| QUOTE_INITCTRL
);
404 fprintf(fp
, "%s", t
.text
);
410 static void man_codepara(FILE *fp
, word
*text
) {
411 fprintf(fp
, ".nf\n");
412 for (; text
; text
= text
->next
) if (text
->type
== word_WeakCode
) {
415 int quote_props
= QUOTE_INITCTRL
;
418 if (text
->next
&& text
->next
->type
== word_Emph
) {
419 e
= text
->next
->text
;
424 while (e
&& *e
&& *t
) {
428 for (n
= 0; t
[n
] && e
[n
] && e
[n
] == ec
; n
++);
433 man_convert(t
, n
, &c
, quote_props
);
434 quote_props
&= ~QUOTE_INITCTRL
;
435 fprintf(fp
, "%s", c
);
437 if (ec
== 'i' || ec
== 'b')
442 man_convert(t
, 0, &c
, quote_props
);
443 fprintf(fp
, "%s\n", c
);
446 fprintf(fp
, ".fi\n");