Import mdocml-1.13.1
[dragonfly.git] / contrib / mdocml / mdoc_man.c
CommitLineData
070c62a6 1/* $Id: mdoc_man.c,v 1.68 2014/08/06 15:09:05 schwarze Exp $ */
36342e81 2/*
070c62a6 3 * Copyright (c) 2011, 2012, 2013, 2014 Ingo Schwarze <schwarze@openbsd.org>
36342e81
SW
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17#ifdef HAVE_CONFIG_H
18#include "config.h"
19#endif
20
f88b6c16 21#include <assert.h>
36342e81
SW
22#include <stdio.h>
23#include <string.h>
24
25#include "mandoc.h"
070c62a6 26#include "mandoc_aux.h"
f88b6c16 27#include "out.h"
36342e81
SW
28#include "man.h"
29#include "mdoc.h"
30#include "main.h"
31
f88b6c16
FF
32#define DECL_ARGS const struct mdoc_meta *meta, \
33 const struct mdoc_node *n
36342e81
SW
34
35struct manact {
36 int (*cond)(DECL_ARGS); /* DON'T run actions */
37 int (*pre)(DECL_ARGS); /* pre-node action */
38 void (*post)(DECL_ARGS); /* post-node action */
39 const char *prefix; /* pre-node string constant */
40 const char *suffix; /* post-node string constant */
41};
42
43static int cond_body(DECL_ARGS);
44static int cond_head(DECL_ARGS);
f88b6c16
FF
45static void font_push(char);
46static void font_pop(void);
47static void mid_it(void);
48static void post__t(DECL_ARGS);
36342e81 49static void post_bd(DECL_ARGS);
f88b6c16
FF
50static void post_bf(DECL_ARGS);
51static void post_bk(DECL_ARGS);
52static void post_bl(DECL_ARGS);
36342e81 53static void post_dl(DECL_ARGS);
070c62a6 54static void post_en(DECL_ARGS);
36342e81 55static void post_enc(DECL_ARGS);
f88b6c16
FF
56static void post_eo(DECL_ARGS);
57static void post_fa(DECL_ARGS);
58static void post_fd(DECL_ARGS);
59static void post_fl(DECL_ARGS);
60static void post_fn(DECL_ARGS);
61static void post_fo(DECL_ARGS);
62static void post_font(DECL_ARGS);
63static void post_in(DECL_ARGS);
64static void post_it(DECL_ARGS);
65static void post_lb(DECL_ARGS);
36342e81
SW
66static void post_nm(DECL_ARGS);
67static void post_percent(DECL_ARGS);
68static void post_pf(DECL_ARGS);
69static void post_sect(DECL_ARGS);
70static void post_sp(DECL_ARGS);
f88b6c16
FF
71static void post_vt(DECL_ARGS);
72static int pre__t(DECL_ARGS);
73static int pre_an(DECL_ARGS);
36342e81
SW
74static int pre_ap(DECL_ARGS);
75static int pre_bd(DECL_ARGS);
f88b6c16
FF
76static int pre_bf(DECL_ARGS);
77static int pre_bk(DECL_ARGS);
78static int pre_bl(DECL_ARGS);
36342e81
SW
79static int pre_br(DECL_ARGS);
80static int pre_bx(DECL_ARGS);
81static int pre_dl(DECL_ARGS);
070c62a6 82static int pre_en(DECL_ARGS);
36342e81 83static int pre_enc(DECL_ARGS);
f88b6c16 84static int pre_em(DECL_ARGS);
070c62a6
FF
85static int pre_es(DECL_ARGS);
86static int pre_ex(DECL_ARGS);
f88b6c16
FF
87static int pre_fa(DECL_ARGS);
88static int pre_fd(DECL_ARGS);
89static int pre_fl(DECL_ARGS);
90static int pre_fn(DECL_ARGS);
91static int pre_fo(DECL_ARGS);
92static int pre_ft(DECL_ARGS);
93static int pre_in(DECL_ARGS);
36342e81 94static int pre_it(DECL_ARGS);
f88b6c16
FF
95static int pre_lk(DECL_ARGS);
96static int pre_li(DECL_ARGS);
070c62a6 97static int pre_ll(DECL_ARGS);
36342e81 98static int pre_nm(DECL_ARGS);
f88b6c16 99static int pre_no(DECL_ARGS);
36342e81
SW
100static int pre_ns(DECL_ARGS);
101static int pre_pp(DECL_ARGS);
f88b6c16 102static int pre_rs(DECL_ARGS);
070c62a6 103static int pre_rv(DECL_ARGS);
f88b6c16 104static int pre_sm(DECL_ARGS);
36342e81
SW
105static int pre_sp(DECL_ARGS);
106static int pre_sect(DECL_ARGS);
f88b6c16
FF
107static int pre_sy(DECL_ARGS);
108static void pre_syn(const struct mdoc_node *);
109static int pre_vt(DECL_ARGS);
36342e81
SW
110static int pre_ux(DECL_ARGS);
111static int pre_xr(DECL_ARGS);
f88b6c16
FF
112static void print_word(const char *);
113static void print_line(const char *, int);
114static void print_block(const char *, int);
115static void print_offs(const char *);
116static void print_width(const char *,
117 const struct mdoc_node *, size_t);
118static void print_count(int *);
36342e81
SW
119static void print_node(DECL_ARGS);
120
121static const struct manact manacts[MDOC_MAX + 1] = {
122 { NULL, pre_ap, NULL, NULL, NULL }, /* Ap */
123 { NULL, NULL, NULL, NULL, NULL }, /* Dd */
124 { NULL, NULL, NULL, NULL, NULL }, /* Dt */
125 { NULL, NULL, NULL, NULL, NULL }, /* Os */
126 { NULL, pre_sect, post_sect, ".SH", NULL }, /* Sh */
127 { NULL, pre_sect, post_sect, ".SS", NULL }, /* Ss */
128 { NULL, pre_pp, NULL, NULL, NULL }, /* Pp */
129 { cond_body, pre_dl, post_dl, NULL, NULL }, /* D1 */
130 { cond_body, pre_dl, post_dl, NULL, NULL }, /* Dl */
131 { cond_body, pre_bd, post_bd, NULL, NULL }, /* Bd */
132 { NULL, NULL, NULL, NULL, NULL }, /* Ed */
f88b6c16 133 { cond_body, pre_bl, post_bl, NULL, NULL }, /* Bl */
36342e81 134 { NULL, NULL, NULL, NULL, NULL }, /* El */
f88b6c16
FF
135 { NULL, pre_it, post_it, NULL, NULL }, /* It */
136 { NULL, pre_em, post_font, NULL, NULL }, /* Ad */
137 { NULL, pre_an, NULL, NULL, NULL }, /* An */
138 { NULL, pre_em, post_font, NULL, NULL }, /* Ar */
139 { NULL, pre_sy, post_font, NULL, NULL }, /* Cd */
140 { NULL, pre_sy, post_font, NULL, NULL }, /* Cm */
141 { NULL, pre_li, post_font, NULL, NULL }, /* Dv */
142 { NULL, pre_li, post_font, NULL, NULL }, /* Er */
143 { NULL, pre_li, post_font, NULL, NULL }, /* Ev */
070c62a6 144 { NULL, pre_ex, NULL, NULL, NULL }, /* Ex */
f88b6c16
FF
145 { NULL, pre_fa, post_fa, NULL, NULL }, /* Fa */
146 { NULL, pre_fd, post_fd, NULL, NULL }, /* Fd */
147 { NULL, pre_fl, post_fl, NULL, NULL }, /* Fl */
148 { NULL, pre_fn, post_fn, NULL, NULL }, /* Fn */
149 { NULL, pre_ft, post_font, NULL, NULL }, /* Ft */
150 { NULL, pre_sy, post_font, NULL, NULL }, /* Ic */
151 { NULL, pre_in, post_in, NULL, NULL }, /* In */
152 { NULL, pre_li, post_font, NULL, NULL }, /* Li */
36342e81
SW
153 { cond_head, pre_enc, NULL, "\\- ", NULL }, /* Nd */
154 { NULL, pre_nm, post_nm, NULL, NULL }, /* Nm */
155 { cond_body, pre_enc, post_enc, "[", "]" }, /* Op */
070c62a6 156 { NULL, pre_ft, post_font, NULL, NULL }, /* Ot */
f88b6c16 157 { NULL, pre_em, post_font, NULL, NULL }, /* Pa */
070c62a6 158 { NULL, pre_rv, NULL, NULL, NULL }, /* Rv */
36342e81 159 { NULL, NULL, NULL, NULL, NULL }, /* St */
f88b6c16
FF
160 { NULL, pre_em, post_font, NULL, NULL }, /* Va */
161 { NULL, pre_vt, post_vt, NULL, NULL }, /* Vt */
36342e81 162 { NULL, pre_xr, NULL, NULL, NULL }, /* Xr */
f88b6c16
FF
163 { NULL, NULL, post_percent, NULL, NULL }, /* %A */
164 { NULL, pre_em, post_percent, NULL, NULL }, /* %B */
165 { NULL, NULL, post_percent, NULL, NULL }, /* %D */
166 { NULL, pre_em, post_percent, NULL, NULL }, /* %I */
167 { NULL, pre_em, post_percent, NULL, NULL }, /* %J */
168 { NULL, NULL, post_percent, NULL, NULL }, /* %N */
169 { NULL, NULL, post_percent, NULL, NULL }, /* %O */
170 { NULL, NULL, post_percent, NULL, NULL }, /* %P */
171 { NULL, NULL, post_percent, NULL, NULL }, /* %R */
172 { NULL, pre__t, post__t, NULL, NULL }, /* %T */
173 { NULL, NULL, post_percent, NULL, NULL }, /* %V */
36342e81
SW
174 { NULL, NULL, NULL, NULL, NULL }, /* Ac */
175 { cond_body, pre_enc, post_enc, "<", ">" }, /* Ao */
176 { cond_body, pre_enc, post_enc, "<", ">" }, /* Aq */
177 { NULL, NULL, NULL, NULL, NULL }, /* At */
178 { NULL, NULL, NULL, NULL, NULL }, /* Bc */
f88b6c16 179 { NULL, pre_bf, post_bf, NULL, NULL }, /* Bf */
36342e81
SW
180 { cond_body, pre_enc, post_enc, "[", "]" }, /* Bo */
181 { cond_body, pre_enc, post_enc, "[", "]" }, /* Bq */
182 { NULL, pre_ux, NULL, "BSD/OS", NULL }, /* Bsx */
183 { NULL, pre_bx, NULL, NULL, NULL }, /* Bx */
184 { NULL, NULL, NULL, NULL, NULL }, /* Db */
185 { NULL, NULL, NULL, NULL, NULL }, /* Dc */
f88b6c16
FF
186 { cond_body, pre_enc, post_enc, "\\(lq", "\\(rq" }, /* Do */
187 { cond_body, pre_enc, post_enc, "\\(lq", "\\(rq" }, /* Dq */
188 { NULL, NULL, NULL, NULL, NULL }, /* Ec */
189 { NULL, NULL, NULL, NULL, NULL }, /* Ef */
190 { NULL, pre_em, post_font, NULL, NULL }, /* Em */
191 { NULL, NULL, post_eo, NULL, NULL }, /* Eo */
36342e81 192 { NULL, pre_ux, NULL, "FreeBSD", NULL }, /* Fx */
f88b6c16
FF
193 { NULL, pre_sy, post_font, NULL, NULL }, /* Ms */
194 { NULL, pre_no, NULL, NULL, NULL }, /* No */
36342e81
SW
195 { NULL, pre_ns, NULL, NULL, NULL }, /* Ns */
196 { NULL, pre_ux, NULL, "NetBSD", NULL }, /* Nx */
197 { NULL, pre_ux, NULL, "OpenBSD", NULL }, /* Ox */
198 { NULL, NULL, NULL, NULL, NULL }, /* Pc */
199 { NULL, NULL, post_pf, NULL, NULL }, /* Pf */
200 { cond_body, pre_enc, post_enc, "(", ")" }, /* Po */
201 { cond_body, pre_enc, post_enc, "(", ")" }, /* Pq */
202 { NULL, NULL, NULL, NULL, NULL }, /* Qc */
f88b6c16 203 { cond_body, pre_enc, post_enc, "\\(oq", "\\(cq" }, /* Ql */
36342e81
SW
204 { cond_body, pre_enc, post_enc, "\"", "\"" }, /* Qo */
205 { cond_body, pre_enc, post_enc, "\"", "\"" }, /* Qq */
206 { NULL, NULL, NULL, NULL, NULL }, /* Re */
f88b6c16 207 { cond_body, pre_rs, NULL, NULL, NULL }, /* Rs */
36342e81 208 { NULL, NULL, NULL, NULL, NULL }, /* Sc */
f88b6c16
FF
209 { cond_body, pre_enc, post_enc, "\\(oq", "\\(cq" }, /* So */
210 { cond_body, pre_enc, post_enc, "\\(oq", "\\(cq" }, /* Sq */
211 { NULL, pre_sm, NULL, NULL, NULL }, /* Sm */
212 { NULL, pre_em, post_font, NULL, NULL }, /* Sx */
213 { NULL, pre_sy, post_font, NULL, NULL }, /* Sy */
214 { NULL, pre_li, post_font, NULL, NULL }, /* Tn */
36342e81 215 { NULL, pre_ux, NULL, "UNIX", NULL }, /* Ux */
f88b6c16
FF
216 { NULL, NULL, NULL, NULL, NULL }, /* Xc */
217 { NULL, NULL, NULL, NULL, NULL }, /* Xo */
218 { NULL, pre_fo, post_fo, NULL, NULL }, /* Fo */
219 { NULL, NULL, NULL, NULL, NULL }, /* Fc */
36342e81
SW
220 { cond_body, pre_enc, post_enc, "[", "]" }, /* Oo */
221 { NULL, NULL, NULL, NULL, NULL }, /* Oc */
f88b6c16
FF
222 { NULL, pre_bk, post_bk, NULL, NULL }, /* Bk */
223 { NULL, NULL, NULL, NULL, NULL }, /* Ek */
36342e81
SW
224 { NULL, pre_ux, NULL, "is currently in beta test.", NULL }, /* Bt */
225 { NULL, NULL, NULL, NULL, NULL }, /* Hf */
070c62a6 226 { NULL, pre_em, post_font, NULL, NULL }, /* Fr */
36342e81 227 { NULL, pre_ux, NULL, "currently under development.", NULL }, /* Ud */
f88b6c16 228 { NULL, NULL, post_lb, NULL, NULL }, /* Lb */
36342e81 229 { NULL, pre_pp, NULL, NULL, NULL }, /* Lp */
f88b6c16
FF
230 { NULL, pre_lk, NULL, NULL, NULL }, /* Lk */
231 { NULL, pre_em, post_font, NULL, NULL }, /* Mt */
36342e81
SW
232 { cond_body, pre_enc, post_enc, "{", "}" }, /* Brq */
233 { cond_body, pre_enc, post_enc, "{", "}" }, /* Bro */
234 { NULL, NULL, NULL, NULL, NULL }, /* Brc */
f88b6c16 235 { NULL, NULL, post_percent, NULL, NULL }, /* %C */
070c62a6
FF
236 { NULL, pre_es, NULL, NULL, NULL }, /* Es */
237 { cond_body, pre_en, post_en, NULL, NULL }, /* En */
36342e81 238 { NULL, pre_ux, NULL, "DragonFly", NULL }, /* Dx */
f88b6c16 239 { NULL, NULL, post_percent, NULL, NULL }, /* %Q */
36342e81
SW
240 { NULL, pre_br, NULL, NULL, NULL }, /* br */
241 { NULL, pre_sp, post_sp, NULL, NULL }, /* sp */
f88b6c16
FF
242 { NULL, NULL, post_percent, NULL, NULL }, /* %U */
243 { NULL, NULL, NULL, NULL, NULL }, /* Ta */
070c62a6 244 { NULL, pre_ll, post_sp, NULL, NULL }, /* ll */
36342e81
SW
245 { NULL, NULL, NULL, NULL, NULL }, /* ROOT */
246};
247
f88b6c16
FF
248static int outflags;
249#define MMAN_spc (1 << 0) /* blank character before next word */
250#define MMAN_spc_force (1 << 1) /* even before trailing punctuation */
251#define MMAN_nl (1 << 2) /* break man(7) code line */
252#define MMAN_br (1 << 3) /* break output line */
253#define MMAN_sp (1 << 4) /* insert a blank output line */
254#define MMAN_PP (1 << 5) /* reset indentation etc. */
255#define MMAN_Sm (1 << 6) /* horizontal spacing mode */
256#define MMAN_Bk (1 << 7) /* word keep mode */
257#define MMAN_Bk_susp (1 << 8) /* suspend this (after a macro) */
258#define MMAN_An_split (1 << 9) /* author mode is "split" */
259#define MMAN_An_nosplit (1 << 10) /* author mode is "nosplit" */
260#define MMAN_PD (1 << 11) /* inter-paragraph spacing disabled */
7888c61d 261#define MMAN_nbrword (1 << 12) /* do not break the next word */
f88b6c16
FF
262
263#define BL_STACK_MAX 32
264
265static size_t Bl_stack[BL_STACK_MAX]; /* offsets [chars] */
266static int Bl_stack_post[BL_STACK_MAX]; /* add final .RE */
267static int Bl_stack_len; /* number of nested Bl blocks */
268static int TPremain; /* characters before tag is full */
269
270static struct {
271 char *head;
272 char *tail;
273 size_t size;
274} fontqueue;
275
070c62a6 276
f88b6c16
FF
277static void
278font_push(char newfont)
279{
280
281 if (fontqueue.head + fontqueue.size <= ++fontqueue.tail) {
282 fontqueue.size += 8;
283 fontqueue.head = mandoc_realloc(fontqueue.head,
070c62a6 284 fontqueue.size);
f88b6c16
FF
285 }
286 *fontqueue.tail = newfont;
287 print_word("");
288 printf("\\f");
289 putchar(newfont);
290 outflags &= ~MMAN_spc;
291}
292
36342e81 293static void
f88b6c16 294font_pop(void)
36342e81
SW
295{
296
f88b6c16
FF
297 if (fontqueue.tail > fontqueue.head)
298 fontqueue.tail--;
299 outflags &= ~MMAN_spc;
300 print_word("");
301 printf("\\f");
302 putchar(*fontqueue.tail);
303}
304
305static void
306print_word(const char *s)
307{
308
309 if ((MMAN_PP | MMAN_sp | MMAN_br | MMAN_nl) & outflags) {
070c62a6 310 /*
36342e81
SW
311 * If we need a newline, print it now and start afresh.
312 */
f88b6c16
FF
313 if (MMAN_PP & outflags) {
314 if (MMAN_sp & outflags) {
315 if (MMAN_PD & outflags) {
316 printf("\n.PD");
317 outflags &= ~MMAN_PD;
318 }
319 } else if ( ! (MMAN_PD & outflags)) {
320 printf("\n.PD 0");
321 outflags |= MMAN_PD;
322 }
323 printf("\n.PP\n");
324 } else if (MMAN_sp & outflags)
325 printf("\n.sp\n");
326 else if (MMAN_br & outflags)
327 printf("\n.br\n");
328 else if (MMAN_nl & outflags)
329 putchar('\n');
330 outflags &= ~(MMAN_PP|MMAN_sp|MMAN_br|MMAN_nl|MMAN_spc);
331 if (1 == TPremain)
332 printf(".br\n");
333 TPremain = 0;
334 } else if (MMAN_spc & outflags) {
36342e81 335 /*
f88b6c16
FF
336 * If we need a space, only print it if
337 * (1) it is forced by `No' or
338 * (2) what follows is not terminating punctuation or
339 * (3) what follows is longer than one character.
36342e81 340 */
f88b6c16
FF
341 if (MMAN_spc_force & outflags || '\0' == s[0] ||
342 NULL == strchr(".,:;)]?!", s[0]) || '\0' != s[1]) {
343 if (MMAN_Bk & outflags &&
344 ! (MMAN_Bk_susp & outflags))
345 putchar('\\');
36342e81 346 putchar(' ');
f88b6c16
FF
347 if (TPremain)
348 TPremain--;
349 }
350 }
36342e81
SW
351
352 /*
353 * Reassign needing space if we're not following opening
354 * punctuation.
355 */
f88b6c16
FF
356 if (MMAN_Sm & outflags && ('\0' == s[0] ||
357 (('(' != s[0] && '[' != s[0]) || '\0' != s[1])))
358 outflags |= MMAN_spc;
359 else
360 outflags &= ~MMAN_spc;
361 outflags &= ~(MMAN_spc_force | MMAN_Bk_susp);
36342e81
SW
362
363 for ( ; *s; s++) {
364 switch (*s) {
070c62a6 365 case ASCII_NBRSP:
f88b6c16 366 printf("\\ ");
36342e81 367 break;
070c62a6 368 case ASCII_HYPH:
36342e81
SW
369 putchar('-');
370 break;
070c62a6
FF
371 case ASCII_BREAK:
372 printf("\\:");
373 break;
374 case ' ':
7888c61d
FF
375 if (MMAN_nbrword & outflags) {
376 printf("\\ ");
377 break;
378 }
379 /* FALLTHROUGH */
36342e81
SW
380 default:
381 putchar((unsigned char)*s);
382 break;
383 }
f88b6c16
FF
384 if (TPremain)
385 TPremain--;
386 }
7888c61d 387 outflags &= ~MMAN_nbrword;
f88b6c16
FF
388}
389
390static void
391print_line(const char *s, int newflags)
392{
393
394 outflags &= ~MMAN_br;
395 outflags |= MMAN_nl;
396 print_word(s);
397 outflags |= newflags;
398}
399
400static void
401print_block(const char *s, int newflags)
402{
403
404 outflags &= ~MMAN_PP;
405 if (MMAN_sp & outflags) {
406 outflags &= ~(MMAN_sp | MMAN_br);
407 if (MMAN_PD & outflags) {
408 print_line(".PD", 0);
409 outflags &= ~MMAN_PD;
410 }
411 } else if (! (MMAN_PD & outflags))
412 print_line(".PD 0", MMAN_PD);
413 outflags |= MMAN_nl;
414 print_word(s);
415 outflags |= MMAN_Bk_susp | newflags;
416}
417
418static void
419print_offs(const char *v)
420{
421 char buf[24];
422 struct roffsu su;
423 size_t sz;
424
425 print_line(".RS", MMAN_Bk_susp);
426
427 /* Convert v into a number (of characters). */
428 if (NULL == v || '\0' == *v || 0 == strcmp(v, "left"))
429 sz = 0;
430 else if (0 == strcmp(v, "indent"))
431 sz = 6;
432 else if (0 == strcmp(v, "indent-two"))
433 sz = 12;
434 else if (a2roffsu(v, &su, SCALE_MAX)) {
435 if (SCALE_EN == su.unit)
436 sz = su.scale;
437 else {
438 /*
439 * XXX
440 * If we are inside an enclosing list,
441 * there is no easy way to add the two
442 * indentations because they are provided
443 * in terms of different units.
444 */
445 print_word(v);
446 outflags |= MMAN_nl;
447 return;
448 }
449 } else
450 sz = strlen(v);
451
452 /*
453 * We are inside an enclosing list.
454 * Add the two indentations.
455 */
456 if (Bl_stack_len)
457 sz += Bl_stack[Bl_stack_len - 1];
458
070c62a6 459 (void)snprintf(buf, sizeof(buf), "%zun", sz);
f88b6c16
FF
460 print_word(buf);
461 outflags |= MMAN_nl;
462}
463
464/*
465 * Set up the indentation for a list item; used from pre_it().
466 */
070c62a6 467static void
f88b6c16
FF
468print_width(const char *v, const struct mdoc_node *child, size_t defsz)
469{
470 char buf[24];
471 struct roffsu su;
472 size_t sz, chsz;
473 int numeric, remain;
474
475 numeric = 1;
476 remain = 0;
477
478 /* Convert v into a number (of characters). */
479 if (NULL == v)
480 sz = defsz;
481 else if (a2roffsu(v, &su, SCALE_MAX)) {
482 if (SCALE_EN == su.unit)
483 sz = su.scale;
484 else {
485 sz = 0;
486 numeric = 0;
487 }
488 } else
489 sz = strlen(v);
490
491 /* XXX Rough estimation, might have multiple parts. */
492 chsz = (NULL != child && MDOC_TEXT == child->type) ?
070c62a6 493 strlen(child->string) : 0;
f88b6c16
FF
494
495 /* Maybe we are inside an enclosing list? */
496 mid_it();
497
498 /*
499 * Save our own indentation,
500 * such that child lists can use it.
501 */
502 Bl_stack[Bl_stack_len++] = sz + 2;
503
504 /* Set up the current list. */
505 if (defsz && chsz > sz)
506 print_block(".HP", 0);
507 else {
508 print_block(".TP", 0);
509 remain = sz + 2;
36342e81 510 }
f88b6c16 511 if (numeric) {
070c62a6 512 (void)snprintf(buf, sizeof(buf), "%zun", sz + 2);
f88b6c16
FF
513 print_word(buf);
514 } else
515 print_word(v);
516 TPremain = remain;
517}
518
070c62a6 519static void
f88b6c16
FF
520print_count(int *count)
521{
070c62a6 522 char buf[24];
f88b6c16 523
070c62a6 524 (void)snprintf(buf, sizeof(buf), "%d.", ++*count);
f88b6c16 525 print_word(buf);
36342e81
SW
526}
527
528void
529man_man(void *arg, const struct man *man)
530{
531
532 /*
533 * Dump the keep buffer.
534 * We're guaranteed by now that this exists (is non-NULL).
535 * Flush stdout afterward, just in case.
536 */
537 fputs(mparse_getkeep(man_mparse(man)), stdout);
538 fflush(stdout);
539}
540
541void
542man_mdoc(void *arg, const struct mdoc *mdoc)
543{
f88b6c16 544 const struct mdoc_meta *meta;
36342e81 545 const struct mdoc_node *n;
36342e81 546
f88b6c16 547 meta = mdoc_meta(mdoc);
36342e81
SW
548 n = mdoc_node(mdoc);
549
f88b6c16 550 printf(".TH \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"\n",
070c62a6
FF
551 meta->title,
552 (meta->msec == NULL ? "" : meta->msec),
553 meta->date, meta->os, meta->vol);
36342e81 554
f88b6c16
FF
555 /* Disable hyphenation and if nroff, disable justification. */
556 printf(".nh\n.if n .ad l");
36342e81 557
f88b6c16
FF
558 outflags = MMAN_nl | MMAN_Sm;
559 if (0 == fontqueue.size) {
560 fontqueue.size = 8;
561 fontqueue.head = fontqueue.tail = mandoc_malloc(8);
562 *fontqueue.tail = 'R';
563 }
564 print_node(meta, n);
36342e81
SW
565 putchar('\n');
566}
567
568static void
569print_node(DECL_ARGS)
570{
f88b6c16 571 const struct mdoc_node *sub;
36342e81
SW
572 const struct manact *act;
573 int cond, do_sub;
f88b6c16 574
36342e81
SW
575 /*
576 * Break the line if we were parsed subsequent the current node.
577 * This makes the page structure be more consistent.
578 */
f88b6c16
FF
579 if (MMAN_spc & outflags && MDOC_LINE & n->flags)
580 outflags |= MMAN_nl;
36342e81
SW
581
582 act = NULL;
583 cond = 0;
584 do_sub = 1;
585
586 if (MDOC_TEXT == n->type) {
587 /*
588 * Make sure that we don't happen to start with a
589 * control character at the start of a line.
590 */
070c62a6
FF
591 if (MMAN_nl & outflags &&
592 ('.' == *n->string || '\'' == *n->string)) {
f88b6c16
FF
593 print_word("");
594 printf("\\&");
595 outflags &= ~MMAN_spc;
36342e81 596 }
f88b6c16 597 print_word(n->string);
36342e81
SW
598 } else {
599 /*
600 * Conditionally run the pre-node action handler for a
601 * node.
602 */
603 act = manacts + n->tok;
f88b6c16 604 cond = NULL == act->cond || (*act->cond)(meta, n);
070c62a6 605 if (cond && act->pre && ENDBODY_NOT == n->end)
f88b6c16 606 do_sub = (*act->pre)(meta, n);
36342e81
SW
607 }
608
070c62a6 609 /*
36342e81
SW
610 * Conditionally run all child nodes.
611 * Note that this iterates over children instead of using
612 * recursion. This prevents unnecessary depth in the stack.
613 */
614 if (do_sub)
615 for (sub = n->child; sub; sub = sub->next)
f88b6c16 616 print_node(meta, sub);
36342e81
SW
617
618 /*
619 * Lastly, conditionally run the post-node handler.
620 */
070c62a6
FF
621 if (MDOC_ENDED & n->flags)
622 return;
623
36342e81 624 if (cond && act->post)
f88b6c16 625 (*act->post)(meta, n);
070c62a6
FF
626
627 if (ENDBODY_NOT != n->end)
628 n->pending->flags |= MDOC_ENDED;
629
630 if (ENDBODY_NOSPACE == n->end)
631 outflags &= ~(MMAN_spc | MMAN_nl);
36342e81
SW
632}
633
634static int
635cond_head(DECL_ARGS)
636{
637
638 return(MDOC_HEAD == n->type);
639}
640
641static int
642cond_body(DECL_ARGS)
643{
644
645 return(MDOC_BODY == n->type);
646}
647
36342e81
SW
648static int
649pre_enc(DECL_ARGS)
650{
651 const char *prefix;
652
653 prefix = manacts[n->tok].prefix;
654 if (NULL == prefix)
655 return(1);
f88b6c16
FF
656 print_word(prefix);
657 outflags &= ~MMAN_spc;
36342e81
SW
658 return(1);
659}
660
36342e81
SW
661static void
662post_enc(DECL_ARGS)
663{
664 const char *suffix;
665
666 suffix = manacts[n->tok].suffix;
667 if (NULL == suffix)
668 return;
070c62a6 669 outflags &= ~(MMAN_spc | MMAN_nl);
f88b6c16
FF
670 print_word(suffix);
671}
672
070c62a6
FF
673static int
674pre_ex(DECL_ARGS)
675{
676 int nchild;
677
678 outflags |= MMAN_br | MMAN_nl;
679
680 print_word("The");
681
682 nchild = n->nchild;
683 for (n = n->child; n; n = n->next) {
684 font_push('B');
685 print_word(n->string);
686 font_pop();
687
688 if (n->next == NULL)
689 continue;
690
691 if (nchild > 2) {
692 outflags &= ~MMAN_spc;
693 print_word(",");
694 }
695 if (n->next->next == NULL)
696 print_word("and");
697 }
698
699 if (nchild > 1)
700 print_word("utilities exit\\~0");
701 else
702 print_word("utility exits\\~0");
703
704 print_word("on success, and\\~>0 if an error occurs.");
705 outflags |= MMAN_nl;
706 return(0);
707}
708
f88b6c16
FF
709static void
710post_font(DECL_ARGS)
711{
712
713 font_pop();
36342e81
SW
714}
715
36342e81
SW
716static void
717post_percent(DECL_ARGS)
718{
719
f88b6c16
FF
720 if (pre_em == manacts[n->tok].pre)
721 font_pop();
722 if (n->next) {
723 print_word(",");
724 if (n->prev && n->prev->tok == n->tok &&
725 n->next->tok == n->tok)
726 print_word("and");
727 } else {
728 print_word(".");
729 outflags |= MMAN_nl;
36342e81
SW
730 }
731}
732
f88b6c16
FF
733static int
734pre__t(DECL_ARGS)
735{
736
070c62a6
FF
737 if (n->parent && MDOC_Rs == n->parent->tok &&
738 n->parent->norm->Rs.quote_T) {
f88b6c16
FF
739 print_word("");
740 putchar('\"');
741 outflags &= ~MMAN_spc;
742 } else
743 font_push('I');
744 return(1);
745}
746
747static void
748post__t(DECL_ARGS)
749{
750
070c62a6
FF
751 if (n->parent && MDOC_Rs == n->parent->tok &&
752 n->parent->norm->Rs.quote_T) {
f88b6c16
FF
753 outflags &= ~MMAN_spc;
754 print_word("");
755 putchar('\"');
756 } else
757 font_pop();
758 post_percent(meta, n);
759}
760
36342e81
SW
761/*
762 * Print before a section header.
763 */
764static int
765pre_sect(DECL_ARGS)
766{
767
7888c61d 768 if (MDOC_HEAD == n->type) {
f88b6c16
FF
769 outflags |= MMAN_sp;
770 print_block(manacts[n->tok].prefix, 0);
771 print_word("");
772 putchar('\"');
773 outflags &= ~MMAN_spc;
f88b6c16 774 }
36342e81
SW
775 return(1);
776}
777
778/*
779 * Print subsequent a section header.
780 */
781static void
782post_sect(DECL_ARGS)
783{
784
785 if (MDOC_HEAD != n->type)
786 return;
f88b6c16
FF
787 outflags &= ~MMAN_spc;
788 print_word("");
789 putchar('\"');
790 outflags |= MMAN_nl;
791 if (MDOC_Sh == n->tok && SEC_AUTHORS == n->sec)
792 outflags &= ~(MMAN_An_split | MMAN_An_nosplit);
793}
794
795/* See mdoc_term.c, synopsis_pre() for comments. */
796static void
797pre_syn(const struct mdoc_node *n)
798{
799
800 if (NULL == n->prev || ! (MDOC_SYNPRETTY & n->flags))
801 return;
802
803 if (n->prev->tok == n->tok &&
070c62a6
FF
804 MDOC_Ft != n->tok &&
805 MDOC_Fo != n->tok &&
806 MDOC_Fn != n->tok) {
f88b6c16
FF
807 outflags |= MMAN_br;
808 return;
809 }
810
811 switch (n->prev->tok) {
070c62a6 812 case MDOC_Fd:
f88b6c16 813 /* FALLTHROUGH */
070c62a6 814 case MDOC_Fn:
f88b6c16 815 /* FALLTHROUGH */
070c62a6 816 case MDOC_Fo:
f88b6c16 817 /* FALLTHROUGH */
070c62a6 818 case MDOC_In:
f88b6c16 819 /* FALLTHROUGH */
070c62a6 820 case MDOC_Vt:
f88b6c16
FF
821 outflags |= MMAN_sp;
822 break;
070c62a6 823 case MDOC_Ft:
f88b6c16
FF
824 if (MDOC_Fn != n->tok && MDOC_Fo != n->tok) {
825 outflags |= MMAN_sp;
826 break;
827 }
828 /* FALLTHROUGH */
829 default:
830 outflags |= MMAN_br;
831 break;
832 }
833}
834
835static int
836pre_an(DECL_ARGS)
837{
838
839 switch (n->norm->An.auth) {
070c62a6 840 case AUTH_split:
f88b6c16
FF
841 outflags &= ~MMAN_An_nosplit;
842 outflags |= MMAN_An_split;
843 return(0);
070c62a6 844 case AUTH_nosplit:
f88b6c16
FF
845 outflags &= ~MMAN_An_split;
846 outflags |= MMAN_An_nosplit;
847 return(0);
848 default:
849 if (MMAN_An_split & outflags)
850 outflags |= MMAN_br;
851 else if (SEC_AUTHORS == n->sec &&
852 ! (MMAN_An_nosplit & outflags))
853 outflags |= MMAN_An_split;
854 return(1);
855 }
36342e81
SW
856}
857
858static int
859pre_ap(DECL_ARGS)
860{
861
f88b6c16
FF
862 outflags &= ~MMAN_spc;
863 print_word("'");
864 outflags &= ~MMAN_spc;
36342e81
SW
865 return(0);
866}
867
868static int
869pre_bd(DECL_ARGS)
870{
871
f88b6c16
FF
872 outflags &= ~(MMAN_PP | MMAN_sp | MMAN_br);
873
36342e81 874 if (DISP_unfilled == n->norm->Bd.type ||
f88b6c16
FF
875 DISP_literal == n->norm->Bd.type)
876 print_line(".nf", 0);
877 if (0 == n->norm->Bd.comp && NULL != n->parent->prev)
878 outflags |= MMAN_sp;
879 print_offs(n->norm->Bd.offs);
36342e81
SW
880 return(1);
881}
882
883static void
884post_bd(DECL_ARGS)
885{
886
f88b6c16
FF
887 /* Close out this display. */
888 print_line(".RE", MMAN_nl);
36342e81 889 if (DISP_unfilled == n->norm->Bd.type ||
f88b6c16
FF
890 DISP_literal == n->norm->Bd.type)
891 print_line(".fi", MMAN_nl);
892
893 /* Maybe we are inside an enclosing list? */
894 if (NULL != n->parent->next)
895 mid_it();
896}
897
898static int
899pre_bf(DECL_ARGS)
900{
901
902 switch (n->type) {
070c62a6 903 case MDOC_BLOCK:
f88b6c16 904 return(1);
070c62a6 905 case MDOC_BODY:
f88b6c16
FF
906 break;
907 default:
908 return(0);
909 }
910 switch (n->norm->Bf.font) {
070c62a6 911 case FONT_Em:
f88b6c16
FF
912 font_push('I');
913 break;
070c62a6 914 case FONT_Sy:
f88b6c16
FF
915 font_push('B');
916 break;
917 default:
918 font_push('R');
919 break;
920 }
921 return(1);
922}
923
924static void
925post_bf(DECL_ARGS)
926{
927
928 if (MDOC_BODY == n->type)
929 font_pop();
930}
931
932static int
933pre_bk(DECL_ARGS)
934{
935
936 switch (n->type) {
070c62a6 937 case MDOC_BLOCK:
f88b6c16 938 return(1);
070c62a6 939 case MDOC_BODY:
f88b6c16
FF
940 outflags |= MMAN_Bk;
941 return(1);
942 default:
943 return(0);
944 }
945}
946
947static void
948post_bk(DECL_ARGS)
949{
950
7888c61d 951 if (MDOC_BODY == n->type)
f88b6c16
FF
952 outflags &= ~MMAN_Bk;
953}
954
955static int
956pre_bl(DECL_ARGS)
957{
958 size_t icol;
959
960 /*
961 * print_offs() will increase the -offset to account for
962 * a possible enclosing .It, but any enclosed .It blocks
963 * just nest and do not add up their indentation.
964 */
965 if (n->norm->Bl.offs) {
966 print_offs(n->norm->Bl.offs);
967 Bl_stack[Bl_stack_len++] = 0;
968 }
969
970 switch (n->norm->Bl.type) {
070c62a6 971 case LIST_enum:
f88b6c16
FF
972 n->norm->Bl.count = 0;
973 return(1);
070c62a6 974 case LIST_column:
f88b6c16
FF
975 break;
976 default:
977 return(1);
978 }
979
980 print_line(".TS", MMAN_nl);
981 for (icol = 0; icol < n->norm->Bl.ncols; icol++)
982 print_word("l");
983 print_word(".");
984 outflags |= MMAN_nl;
985 return(1);
986}
987
988static void
989post_bl(DECL_ARGS)
990{
991
992 switch (n->norm->Bl.type) {
070c62a6 993 case LIST_column:
f88b6c16
FF
994 print_line(".TE", 0);
995 break;
070c62a6 996 case LIST_enum:
f88b6c16
FF
997 n->norm->Bl.count = 0;
998 break;
999 default:
1000 break;
1001 }
1002
1003 if (n->norm->Bl.offs) {
1004 print_line(".RE", MMAN_nl);
1005 assert(Bl_stack_len);
1006 Bl_stack_len--;
1007 assert(0 == Bl_stack[Bl_stack_len]);
1008 } else {
1009 outflags |= MMAN_PP | MMAN_nl;
1010 outflags &= ~(MMAN_sp | MMAN_br);
36342e81 1011 }
f88b6c16
FF
1012
1013 /* Maybe we are inside an enclosing list? */
1014 if (NULL != n->parent->next)
1015 mid_it();
1016
36342e81
SW
1017}
1018
1019static int
1020pre_br(DECL_ARGS)
1021{
1022
f88b6c16 1023 outflags |= MMAN_br;
36342e81
SW
1024 return(0);
1025}
1026
1027static int
1028pre_bx(DECL_ARGS)
1029{
1030
1031 n = n->child;
1032 if (n) {
f88b6c16
FF
1033 print_word(n->string);
1034 outflags &= ~MMAN_spc;
36342e81
SW
1035 n = n->next;
1036 }
f88b6c16 1037 print_word("BSD");
36342e81
SW
1038 if (NULL == n)
1039 return(0);
f88b6c16
FF
1040 outflags &= ~MMAN_spc;
1041 print_word("-");
1042 outflags &= ~MMAN_spc;
1043 print_word(n->string);
36342e81
SW
1044 return(0);
1045}
1046
1047static int
1048pre_dl(DECL_ARGS)
1049{
1050
f88b6c16 1051 print_offs("6n");
36342e81
SW
1052 return(1);
1053}
1054
1055static void
1056post_dl(DECL_ARGS)
1057{
1058
f88b6c16
FF
1059 print_line(".RE", MMAN_nl);
1060
1061 /* Maybe we are inside an enclosing list? */
1062 if (NULL != n->parent->next)
1063 mid_it();
1064}
1065
1066static int
1067pre_em(DECL_ARGS)
1068{
1069
1070 font_push('I');
1071 return(1);
1072}
1073
070c62a6
FF
1074static int
1075pre_en(DECL_ARGS)
1076{
1077
1078 if (NULL == n->norm->Es ||
1079 NULL == n->norm->Es->child)
1080 return(1);
1081
1082 print_word(n->norm->Es->child->string);
1083 outflags &= ~MMAN_spc;
1084 return(1);
1085}
1086
1087static void
1088post_en(DECL_ARGS)
1089{
1090
1091 if (NULL == n->norm->Es ||
1092 NULL == n->norm->Es->child ||
1093 NULL == n->norm->Es->child->next)
1094 return;
1095
1096 outflags &= ~MMAN_spc;
1097 print_word(n->norm->Es->child->next->string);
1098 return;
1099}
1100
f88b6c16
FF
1101static void
1102post_eo(DECL_ARGS)
1103{
1104
1105 if (MDOC_HEAD == n->type || MDOC_BODY == n->type)
1106 outflags &= ~MMAN_spc;
1107}
1108
070c62a6
FF
1109static int
1110pre_es(DECL_ARGS)
1111{
1112
1113 return(0);
1114}
1115
f88b6c16
FF
1116static int
1117pre_fa(DECL_ARGS)
1118{
7888c61d 1119 int am_Fa;
f88b6c16 1120
7888c61d
FF
1121 am_Fa = MDOC_Fa == n->tok;
1122
1123 if (am_Fa)
f88b6c16
FF
1124 n = n->child;
1125
1126 while (NULL != n) {
1127 font_push('I');
7888c61d
FF
1128 if (am_Fa || MDOC_SYNPRETTY & n->flags)
1129 outflags |= MMAN_nbrword;
f88b6c16
FF
1130 print_node(meta, n);
1131 font_pop();
1132 if (NULL != (n = n->next))
1133 print_word(",");
1134 }
1135 return(0);
1136}
1137
1138static void
1139post_fa(DECL_ARGS)
1140{
1141
1142 if (NULL != n->next && MDOC_Fa == n->next->tok)
1143 print_word(",");
1144}
1145
1146static int
1147pre_fd(DECL_ARGS)
1148{
1149
1150 pre_syn(n);
1151 font_push('B');
1152 return(1);
1153}
1154
1155static void
1156post_fd(DECL_ARGS)
1157{
1158
1159 font_pop();
1160 outflags |= MMAN_br;
1161}
1162
1163static int
1164pre_fl(DECL_ARGS)
1165{
1166
1167 font_push('B');
1168 print_word("\\-");
1169 outflags &= ~MMAN_spc;
1170 return(1);
1171}
1172
1173static void
1174post_fl(DECL_ARGS)
1175{
1176
1177 font_pop();
1178 if (0 == n->nchild && NULL != n->next &&
1179 n->next->line == n->line)
1180 outflags &= ~MMAN_spc;
1181}
1182
1183static int
1184pre_fn(DECL_ARGS)
1185{
1186
1187 pre_syn(n);
1188
1189 n = n->child;
1190 if (NULL == n)
1191 return(0);
1192
7888c61d
FF
1193 if (MDOC_SYNPRETTY & n->flags)
1194 print_block(".HP 4n", MMAN_nl);
1195
f88b6c16
FF
1196 font_push('B');
1197 print_node(meta, n);
1198 font_pop();
1199 outflags &= ~MMAN_spc;
1200 print_word("(");
1201 outflags &= ~MMAN_spc;
1202
1203 n = n->next;
1204 if (NULL != n)
1205 pre_fa(meta, n);
1206 return(0);
1207}
1208
1209static void
1210post_fn(DECL_ARGS)
1211{
1212
1213 print_word(")");
1214 if (MDOC_SYNPRETTY & n->flags) {
1215 print_word(";");
7888c61d 1216 outflags |= MMAN_PP;
f88b6c16
FF
1217 }
1218}
1219
1220static int
1221pre_fo(DECL_ARGS)
1222{
1223
1224 switch (n->type) {
070c62a6 1225 case MDOC_BLOCK:
f88b6c16
FF
1226 pre_syn(n);
1227 break;
070c62a6 1228 case MDOC_HEAD:
7888c61d
FF
1229 if (MDOC_SYNPRETTY & n->flags)
1230 print_block(".HP 4n", MMAN_nl);
f88b6c16
FF
1231 font_push('B');
1232 break;
070c62a6 1233 case MDOC_BODY:
f88b6c16
FF
1234 outflags &= ~MMAN_spc;
1235 print_word("(");
1236 outflags &= ~MMAN_spc;
1237 break;
1238 default:
1239 break;
1240 }
1241 return(1);
1242}
7888c61d 1243
f88b6c16
FF
1244static void
1245post_fo(DECL_ARGS)
1246{
1247
1248 switch (n->type) {
070c62a6 1249 case MDOC_HEAD:
f88b6c16
FF
1250 font_pop();
1251 break;
070c62a6 1252 case MDOC_BODY:
f88b6c16
FF
1253 post_fn(meta, n);
1254 break;
1255 default:
1256 break;
1257 }
1258}
1259
1260static int
1261pre_ft(DECL_ARGS)
1262{
1263
1264 pre_syn(n);
1265 font_push('I');
1266 return(1);
1267}
1268
1269static int
1270pre_in(DECL_ARGS)
1271{
1272
1273 if (MDOC_SYNPRETTY & n->flags) {
1274 pre_syn(n);
1275 font_push('B');
1276 print_word("#include <");
1277 outflags &= ~MMAN_spc;
1278 } else {
1279 print_word("<");
1280 outflags &= ~MMAN_spc;
1281 font_push('I');
1282 }
1283 return(1);
1284}
1285
1286static void
1287post_in(DECL_ARGS)
1288{
1289
1290 if (MDOC_SYNPRETTY & n->flags) {
1291 outflags &= ~MMAN_spc;
1292 print_word(">");
1293 font_pop();
1294 outflags |= MMAN_br;
1295 } else {
1296 font_pop();
1297 outflags &= ~MMAN_spc;
1298 print_word(">");
1299 }
36342e81
SW
1300}
1301
1302static int
1303pre_it(DECL_ARGS)
1304{
1305 const struct mdoc_node *bln;
1306
f88b6c16 1307 switch (n->type) {
070c62a6 1308 case MDOC_HEAD:
f88b6c16
FF
1309 outflags |= MMAN_PP | MMAN_nl;
1310 bln = n->parent->parent;
1311 if (0 == bln->norm->Bl.comp ||
1312 (NULL == n->parent->prev &&
1313 NULL == bln->parent->prev))
1314 outflags |= MMAN_sp;
1315 outflags &= ~MMAN_br;
36342e81 1316 switch (bln->norm->Bl.type) {
070c62a6 1317 case LIST_item:
f88b6c16 1318 return(0);
070c62a6 1319 case LIST_inset:
f88b6c16 1320 /* FALLTHROUGH */
070c62a6 1321 case LIST_diag:
f88b6c16 1322 /* FALLTHROUGH */
070c62a6 1323 case LIST_ohang:
f88b6c16
FF
1324 if (bln->norm->Bl.type == LIST_diag)
1325 print_line(".B \"", 0);
1326 else
1327 print_line(".R \"", 0);
1328 outflags &= ~MMAN_spc;
1329 return(1);
070c62a6 1330 case LIST_bullet:
f88b6c16 1331 /* FALLTHROUGH */
070c62a6 1332 case LIST_dash:
f88b6c16 1333 /* FALLTHROUGH */
070c62a6 1334 case LIST_hyphen:
f88b6c16
FF
1335 print_width(bln->norm->Bl.width, NULL, 0);
1336 TPremain = 0;
1337 outflags |= MMAN_nl;
1338 font_push('B');
1339 if (LIST_bullet == bln->norm->Bl.type)
1340 print_word("o");
1341 else
1342 print_word("-");
1343 font_pop();
070c62a6
FF
1344 outflags |= MMAN_nl;
1345 return(0);
1346 case LIST_enum:
f88b6c16
FF
1347 print_width(bln->norm->Bl.width, NULL, 0);
1348 TPremain = 0;
1349 outflags |= MMAN_nl;
1350 print_count(&bln->norm->Bl.count);
070c62a6
FF
1351 outflags |= MMAN_nl;
1352 return(0);
1353 case LIST_hang:
f88b6c16
FF
1354 print_width(bln->norm->Bl.width, n->child, 6);
1355 TPremain = 0;
070c62a6
FF
1356 outflags |= MMAN_nl;
1357 return(1);
1358 case LIST_tag:
f88b6c16
FF
1359 print_width(bln->norm->Bl.width, n->child, 0);
1360 putchar('\n');
1361 outflags &= ~MMAN_spc;
1362 return(1);
1363 default:
1364 return(1);
1365 }
f88b6c16
FF
1366 default:
1367 break;
1368 }
1369 return(1);
1370}
1371
1372/*
1373 * This function is called after closing out an indented block.
1374 * If we are inside an enclosing list, restore its indentation.
1375 */
1376static void
1377mid_it(void)
1378{
1379 char buf[24];
1380
1381 /* Nothing to do outside a list. */
1382 if (0 == Bl_stack_len || 0 == Bl_stack[Bl_stack_len - 1])
1383 return;
1384
1385 /* The indentation has already been set up. */
1386 if (Bl_stack_post[Bl_stack_len - 1])
1387 return;
1388
1389 /* Restore the indentation of the enclosing list. */
1390 print_line(".RS", MMAN_Bk_susp);
070c62a6
FF
1391 (void)snprintf(buf, sizeof(buf), "%zun",
1392 Bl_stack[Bl_stack_len - 1]);
f88b6c16
FF
1393 print_word(buf);
1394
1395 /* Remeber to close out this .RS block later. */
1396 Bl_stack_post[Bl_stack_len - 1] = 1;
1397}
1398
1399static void
1400post_it(DECL_ARGS)
1401{
1402 const struct mdoc_node *bln;
1403
1404 bln = n->parent->parent;
1405
1406 switch (n->type) {
070c62a6 1407 case MDOC_HEAD:
f88b6c16 1408 switch (bln->norm->Bl.type) {
070c62a6 1409 case LIST_diag:
f88b6c16
FF
1410 outflags &= ~MMAN_spc;
1411 print_word("\\ ");
1412 break;
070c62a6 1413 case LIST_ohang:
f88b6c16
FF
1414 outflags |= MMAN_br;
1415 break;
1416 default:
1417 break;
1418 }
1419 break;
070c62a6 1420 case MDOC_BODY:
f88b6c16 1421 switch (bln->norm->Bl.type) {
070c62a6 1422 case LIST_bullet:
f88b6c16 1423 /* FALLTHROUGH */
070c62a6 1424 case LIST_dash:
f88b6c16 1425 /* FALLTHROUGH */
070c62a6 1426 case LIST_hyphen:
f88b6c16 1427 /* FALLTHROUGH */
070c62a6 1428 case LIST_enum:
f88b6c16 1429 /* FALLTHROUGH */
070c62a6 1430 case LIST_hang:
f88b6c16 1431 /* FALLTHROUGH */
070c62a6 1432 case LIST_tag:
f88b6c16
FF
1433 assert(Bl_stack_len);
1434 Bl_stack[--Bl_stack_len] = 0;
1435
1436 /*
1437 * Our indentation had to be restored
1438 * after a child display or child list.
1439 * Close out that indentation block now.
1440 */
1441 if (Bl_stack_post[Bl_stack_len]) {
1442 print_line(".RE", MMAN_nl);
1443 Bl_stack_post[Bl_stack_len] = 0;
1444 }
1445 break;
070c62a6 1446 case LIST_column:
f88b6c16
FF
1447 if (NULL != n->next) {
1448 putchar('\t');
1449 outflags &= ~MMAN_spc;
1450 }
36342e81
SW
1451 break;
1452 default:
36342e81
SW
1453 break;
1454 }
f88b6c16
FF
1455 break;
1456 default:
1457 break;
36342e81 1458 }
f88b6c16
FF
1459}
1460
1461static void
1462post_lb(DECL_ARGS)
1463{
1464
1465 if (SEC_LIBRARY == n->sec)
1466 outflags |= MMAN_br;
1467}
1468
1469static int
1470pre_lk(DECL_ARGS)
1471{
1472 const struct mdoc_node *link, *descr;
1473
1474 if (NULL == (link = n->child))
1475 return(0);
1476
1477 if (NULL != (descr = link->next)) {
1478 font_push('I');
1479 while (NULL != descr) {
1480 print_word(descr->string);
1481 descr = descr->next;
1482 }
1483 print_word(":");
1484 font_pop();
1485 }
1486
1487 font_push('B');
1488 print_word(link->string);
1489 font_pop();
1490 return(0);
1491}
1492
070c62a6
FF
1493static int
1494pre_ll(DECL_ARGS)
1495{
1496
1497 print_line(".ll", 0);
1498 return(1);
1499}
1500
f88b6c16
FF
1501static int
1502pre_li(DECL_ARGS)
1503{
1504
1505 font_push('R');
36342e81
SW
1506 return(1);
1507}
1508
1509static int
1510pre_nm(DECL_ARGS)
1511{
f88b6c16 1512 char *name;
36342e81 1513
7888c61d
FF
1514 if (MDOC_BLOCK == n->type) {
1515 outflags |= MMAN_Bk;
f88b6c16 1516 pre_syn(n);
7888c61d 1517 }
36342e81
SW
1518 if (MDOC_ELEM != n->type && MDOC_HEAD != n->type)
1519 return(1);
f88b6c16
FF
1520 name = n->child ? n->child->string : meta->name;
1521 if (NULL == name)
1522 return(0);
1523 if (MDOC_HEAD == n->type) {
1524 if (NULL == n->parent->prev)
1525 outflags |= MMAN_sp;
1526 print_block(".HP", 0);
7888c61d 1527 printf(" %zun", strlen(name) + 1);
f88b6c16
FF
1528 outflags |= MMAN_nl;
1529 }
1530 font_push('B');
36342e81 1531 if (NULL == n->child)
f88b6c16 1532 print_word(meta->name);
36342e81
SW
1533 return(1);
1534}
1535
1536static void
1537post_nm(DECL_ARGS)
1538{
1539
7888c61d 1540 switch (n->type) {
070c62a6 1541 case MDOC_BLOCK:
7888c61d
FF
1542 outflags &= ~MMAN_Bk;
1543 break;
070c62a6 1544 case MDOC_HEAD:
7888c61d 1545 /* FALLTHROUGH */
070c62a6
FF
1546 case MDOC_ELEM:
1547 if (n->child != NULL || meta->name != NULL)
1548 font_pop();
7888c61d
FF
1549 break;
1550 default:
1551 break;
1552 }
f88b6c16
FF
1553}
1554
1555static int
1556pre_no(DECL_ARGS)
1557{
1558
1559 outflags |= MMAN_spc_force;
1560 return(1);
36342e81
SW
1561}
1562
1563static int
1564pre_ns(DECL_ARGS)
1565{
1566
f88b6c16 1567 outflags &= ~MMAN_spc;
36342e81
SW
1568 return(0);
1569}
1570
1571static void
1572post_pf(DECL_ARGS)
1573{
1574
f88b6c16 1575 outflags &= ~MMAN_spc;
36342e81
SW
1576}
1577
1578static int
1579pre_pp(DECL_ARGS)
1580{
1581
f88b6c16
FF
1582 if (MDOC_It != n->parent->tok)
1583 outflags |= MMAN_PP;
1584 outflags |= MMAN_sp | MMAN_nl;
1585 outflags &= ~MMAN_br;
1586 return(0);
1587}
1588
1589static int
1590pre_rs(DECL_ARGS)
1591{
1592
1593 if (SEC_SEE_ALSO == n->sec) {
1594 outflags |= MMAN_PP | MMAN_sp | MMAN_nl;
1595 outflags &= ~MMAN_br;
1596 }
36342e81
SW
1597 return(1);
1598}
1599
070c62a6
FF
1600static int
1601pre_rv(DECL_ARGS)
1602{
1603 int nchild;
1604
1605 outflags |= MMAN_br | MMAN_nl;
1606
1607 nchild = n->nchild;
1608 if (nchild > 0) {
1609 print_word("The");
1610
1611 for (n = n->child; n; n = n->next) {
1612 font_push('B');
1613 print_word(n->string);
1614 font_pop();
1615
1616 outflags &= ~MMAN_spc;
1617 print_word("()");
1618
1619 if (n->next == NULL)
1620 continue;
1621
1622 if (nchild > 2) {
1623 outflags &= ~MMAN_spc;
1624 print_word(",");
1625 }
1626 if (n->next->next == NULL)
1627 print_word("and");
1628 }
1629
1630 if (nchild > 1)
1631 print_word("functions return");
1632 else
1633 print_word("function returns");
1634
1635 print_word("the value\\~0 if successful;");
1636 } else
1637 print_word("Upon successful completion, "
1638 "the value\\~0 is returned;");
1639
1640 print_word("otherwise the value\\~\\-1 is returned"
1641 " and the global variable");
1642
1643 font_push('I');
1644 print_word("errno");
1645 font_pop();
1646
1647 print_word("is set to indicate the error.");
1648 outflags |= MMAN_nl;
1649 return(0);
1650}
1651
f88b6c16
FF
1652static int
1653pre_sm(DECL_ARGS)
1654{
1655
070c62a6
FF
1656 if (NULL == n->child)
1657 outflags ^= MMAN_Sm;
1658 else if (0 == strcmp("on", n->child->string))
1659 outflags |= MMAN_Sm;
f88b6c16
FF
1660 else
1661 outflags &= ~MMAN_Sm;
070c62a6
FF
1662
1663 if (MMAN_Sm & outflags)
1664 outflags |= MMAN_spc;
1665
f88b6c16
FF
1666 return(0);
1667}
1668
36342e81
SW
1669static int
1670pre_sp(DECL_ARGS)
1671{
1672
f88b6c16
FF
1673 if (MMAN_PP & outflags) {
1674 outflags &= ~MMAN_PP;
1675 print_line(".PP", 0);
1676 } else
1677 print_line(".sp", 0);
36342e81
SW
1678 return(1);
1679}
1680
1681static void
1682post_sp(DECL_ARGS)
1683{
1684
f88b6c16
FF
1685 outflags |= MMAN_nl;
1686}
1687
1688static int
1689pre_sy(DECL_ARGS)
1690{
1691
1692 font_push('B');
1693 return(1);
1694}
1695
1696static int
1697pre_vt(DECL_ARGS)
1698{
1699
1700 if (MDOC_SYNPRETTY & n->flags) {
1701 switch (n->type) {
070c62a6 1702 case MDOC_BLOCK:
f88b6c16
FF
1703 pre_syn(n);
1704 return(1);
070c62a6 1705 case MDOC_BODY:
f88b6c16
FF
1706 break;
1707 default:
1708 return(0);
1709 }
1710 }
1711 font_push('I');
1712 return(1);
1713}
1714
1715static void
1716post_vt(DECL_ARGS)
1717{
1718
1719 if (MDOC_SYNPRETTY & n->flags && MDOC_BODY != n->type)
1720 return;
1721 font_pop();
36342e81
SW
1722}
1723
1724static int
1725pre_xr(DECL_ARGS)
1726{
1727
1728 n = n->child;
1729 if (NULL == n)
1730 return(0);
f88b6c16 1731 print_node(meta, n);
36342e81
SW
1732 n = n->next;
1733 if (NULL == n)
1734 return(0);
f88b6c16
FF
1735 outflags &= ~MMAN_spc;
1736 print_word("(");
1737 print_node(meta, n);
1738 print_word(")");
36342e81
SW
1739 return(0);
1740}
1741
1742static int
1743pre_ux(DECL_ARGS)
1744{
1745
f88b6c16 1746 print_word(manacts[n->tok].prefix);
36342e81
SW
1747 if (NULL == n->child)
1748 return(0);
f88b6c16
FF
1749 outflags &= ~MMAN_spc;
1750 print_word("\\ ");
1751 outflags &= ~MMAN_spc;
36342e81
SW
1752 return(1);
1753}