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
);
21 static manconfig
man_configure(paragraph
*source
) {
28 ret
.headnumbers
= FALSE
;
31 for (; source
; source
= source
->next
) {
32 if (source
->type
== para_Config
) {
33 if (!ustricmp(source
->keyword
, L
"man-identity")) {
36 wp
= uadv(source
->keyword
);
40 ret
.th
= mknewa(wchar_t, ep
- wp
+ 1);
41 memcpy(ret
.th
, wp
, (ep
- wp
+ 1) * sizeof(wchar_t));
42 } else if (!ustricmp(source
->keyword
, L
"man-headnumbers")) {
43 ret
.headnumbers
= utob(uadv(source
->keyword
));
44 } else if (!ustricmp(source
->keyword
, L
"man-mindepth")) {
45 ret
.mindepth
= utoi(uadv(source
->keyword
));
53 static void man_conf_cleanup(manconfig cf
)
58 #define QUOTE_INITCTRL 1 /* quote initial . and ' on a line */
59 #define QUOTE_QUOTES 2 /* quote double quotes by doubling them */
61 void man_backend(paragraph
*sourceform
, keywordlist
*keywords
,
68 IGNORE(keywords
); /* we don't happen to need this */
69 IGNORE(idx
); /* or this */
71 conf
= man_configure(sourceform
);
74 * Determine the output file name, and open the output file
76 * FIXME: want configurable output file names here. For the
77 * moment, we'll just call it `output.1'.
79 fp
= fopen("output.1", "w");
81 error(err_cantopenw
, "output.1");
85 /* Do the version ID */
86 for (p
= sourceform
; p
; p
= p
->next
)
87 if (p
->type
== para_VersionID
) {
88 fprintf(fp
, ".\\\" ");
89 man_text(fp
, p
->words
, TRUE
, 0);
92 /* .TH name-of-program manual-section */
94 if (conf
.th
&& *conf
.th
) {
98 for (wp
= conf
.th
; *wp
; wp
= uadv(wp
)) {
100 man_convert(wp
, 0, &c
, QUOTE_QUOTES
);
108 fprintf(fp
, ".UC\n");
110 done_copyright
= FALSE
;
112 for (p
= sourceform
; p
; p
= p
->next
) switch (p
->type
) {
114 * Things we ignore because we've already processed them or
115 * aren't going to touch them in this pass.
119 case para_Biblio
: /* only touch BiblioCited */
131 case para_UnnumberedChapter
:
135 if (!done_copyright
) {
139 * The copyright comes just before the first chapter
142 for (p
= sourceform
; p
; p
= p
->next
)
143 if (p
->type
== para_Copyright
) {
144 fprintf(fp
, ".PP\n");
145 man_text(fp
, p
->words
, TRUE
, 0);
147 done_copyright
= TRUE
;
152 if (p
->type
== para_Subsect
)
154 else if (p
->type
== para_Heading
)
158 if (depth
>= conf
.mindepth
) {
159 fprintf(fp
, ".SH \"");
160 if (conf
.headnumbers
&& p
->kwtext
) {
161 man_text(fp
, p
->kwtext
, FALSE
, QUOTE_QUOTES
);
164 man_text(fp
, p
->words
, FALSE
, QUOTE_QUOTES
);
174 fprintf(fp
, ".PP\n");
175 man_codepara(fp
, p
->words
);
182 fprintf(fp
, ".PP\n");
183 man_text(fp
, p
->words
, TRUE
, 0);
189 case para_Description
:
190 case para_BiblioCited
:
192 case para_NumberedList
:
193 if (p
->type
== para_Bullet
) {
194 fprintf(fp
, ".IP \"\\fBo\\fP\"\n"); /* FIXME: configurable? */
195 } else if (p
->type
== para_NumberedList
) {
196 fprintf(fp
, ".IP \"");
197 man_text(fp
, p
->kwtext
, FALSE
, QUOTE_QUOTES
);
199 } else if (p
->type
== para_Description
) {
201 * Do nothing; the .xP for this paragraph is the .IP
202 * which has come before it in the DescribedThing.
204 } else if (p
->type
== para_BiblioCited
) {
205 fprintf(fp
, ".IP \"");
206 man_text(fp
, p
->kwtext
, FALSE
, QUOTE_QUOTES
);
209 man_text(fp
, p
->words
, TRUE
, 0);
212 case para_DescribedThing
:
213 fprintf(fp
, ".IP \"");
214 man_text(fp
, p
->words
, FALSE
, QUOTE_QUOTES
);
220 * This isn't terribly good. Anyone who wants to do better
223 fprintf(fp
, ".PP\n----------------------------------------\n");
228 fprintf(fp
, ".RS\n");
232 fprintf(fp
, ".RE\n");
240 man_conf_cleanup(conf
);
244 * Convert a wide string into a string of chars. If `result' is
245 * non-NULL, mallocs the resulting string and stores a pointer to
246 * it in `*result'. If `result' is NULL, merely checks whether all
247 * characters in the string are feasible for the output character
250 * Return is nonzero if all characters are OK. If not all
251 * characters are OK but `result' is non-NULL, a result _will_
252 * still be generated!
254 * FIXME: Here is probably also a good place to do escaping sorts
255 * of things. I know I at least need to escape backslash, and full
256 * stops at the starts of words are probably trouble as well.
258 static int man_convert(wchar_t *s
, int maxlen
,
259 char **result
, int quote_props
) {
261 * FIXME. Currently this is ISO8859-1 only.
263 int doing
= (result
!= 0);
266 int plen
= 0, psize
= 0;
271 for (; *s
&& maxlen
!= 0; s
++, maxlen
--) {
275 if ((c
>= 32 && c
<= 126) ||
276 (c
>= 160 && c
<= 255)) {
280 /* Char is not OK. */
282 outc
= 0xBF; /* approximate the good old DEC `uh?' */
285 if (plen
+3 >= psize
) {
287 p
= resize(p
, psize
);
289 if (plen
== 0 && (outc
== '.' || outc
== '\'') &&
290 (quote_props
& QUOTE_INITCTRL
)) {
292 * Control character (. or ') at the start of a
293 * line. Quote it by putting \& (troff zero-width
298 } else if (outc
== '\\') {
300 * Quote backslashes by doubling them, always.
303 } else if (outc
== '"' && (quote_props
& QUOTE_QUOTES
)) {
305 * Double quote within double quotes. Quote it by
314 p
= resize(p
, plen
+1);
321 static void man_rdaddwc(rdstringc
*rs
, word
*text
, word
*end
,
325 for (; text
&& text
!= end
; text
= text
->next
) switch (text
->type
) {
338 case word_WhiteSpace
:
341 case word_WkCodeSpace
:
345 case word_WkCodeQuote
:
346 assert(text
->type
!= word_CodeQuote
&&
347 text
->type
!= word_WkCodeQuote
);
348 if (towordstyle(text
->type
) == word_Emph
&&
349 (attraux(text
->aux
) == attr_First
||
350 attraux(text
->aux
) == attr_Only
))
352 else if ((towordstyle(text
->type
) == word_Code
||
353 towordstyle(text
->type
) == word_WeakCode
) &&
354 (attraux(text
->aux
) == attr_First
||
355 attraux(text
->aux
) == attr_Only
))
357 if (removeattr(text
->type
) == word_Normal
) {
359 quote_props
&= ~QUOTE_INITCTRL
; /* not at start any more */
360 if (man_convert(text
->text
, 0, &c
, quote_props
))
363 man_rdaddwc(rs
, text
->alt
, NULL
, quote_props
);
365 } else if (removeattr(text
->type
) == word_WhiteSpace
) {
367 } else if (removeattr(text
->type
) == word_Quote
) {
369 if (quote_props
& QUOTE_QUOTES
)
372 if (towordstyle(text
->type
) == word_Emph
&&
373 (attraux(text
->aux
) == attr_Last
||
374 attraux(text
->aux
) == attr_Only
))
376 else if ((towordstyle(text
->type
) == word_Code
||
377 towordstyle(text
->type
) == word_WeakCode
) &&
378 (attraux(text
->aux
) == attr_Last
||
379 attraux(text
->aux
) == attr_Only
))
385 static void man_text(FILE *fp
, word
*text
, int newline
, int quote_props
) {
386 rdstringc t
= { 0, 0, NULL
};
388 man_rdaddwc(&t
, text
, NULL
, quote_props
| QUOTE_INITCTRL
);
389 fprintf(fp
, "%s", t
.text
);
395 static void man_codepara(FILE *fp
, word
*text
) {
396 fprintf(fp
, ".nf\n");
397 for (; text
; text
= text
->next
) if (text
->type
== word_WeakCode
) {
400 int quote_props
= QUOTE_INITCTRL
;
403 if (text
->next
&& text
->next
->type
== word_Emph
) {
404 e
= text
->next
->text
;
409 while (e
&& *e
&& *t
) {
413 for (n
= 0; t
[n
] && e
[n
] && e
[n
] == ec
; n
++);
418 man_convert(t
, n
, &c
, quote_props
);
419 quote_props
&= ~QUOTE_INITCTRL
;
420 fprintf(fp
, "%s", c
);
422 if (ec
== 'i' || ec
== 'b')
427 man_convert(t
, 0, &c
, quote_props
);
428 fprintf(fp
, "%s\n", c
);
431 fprintf(fp
, ".fi\n");