mandoc(1): Update to 1.9.19.
[dragonfly.git] / usr.bin / mandoc / mdoc_term.c
1 /*      $Id: mdoc_term.c,v 1.111 2010/03/23 12:42:22 kristaps Exp $ */
2 /*
3  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
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 #include <sys/types.h>
18
19 #include <assert.h>
20 #include <ctype.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "out.h"
26 #include "term.h"
27 #include "mdoc.h"
28 #include "chars.h"
29 #include "main.h"
30
31 #define INDENT            5
32 #define HALFINDENT        3
33
34 struct  termpair {
35         struct termpair  *ppair;
36         int               count;
37 };
38
39 #define DECL_ARGS struct termp *p, \
40                   struct termpair *pair, \
41                   const struct mdoc_meta *m, \
42                   const struct mdoc_node *n
43
44 struct  termact {
45         int     (*pre)(DECL_ARGS);
46         void    (*post)(DECL_ARGS);
47 };
48
49 static  size_t    a2width(const struct mdoc_argv *, int);
50 static  size_t    a2height(const struct mdoc_node *);
51 static  size_t    a2offs(const struct mdoc_argv *);
52
53 static  int       arg_hasattr(int, const struct mdoc_node *);
54 static  int       arg_getattrs(const int *, int *, size_t,
55                         const struct mdoc_node *);
56 static  int       arg_getattr(int, const struct mdoc_node *);
57 static  int       arg_listtype(const struct mdoc_node *);
58 static  void      print_bvspace(struct termp *,
59                         const struct mdoc_node *,
60                         const struct mdoc_node *);
61 static  void      print_mdoc_node(DECL_ARGS);
62 static  void      print_mdoc_head(DECL_ARGS);
63 static  void      print_mdoc_nodelist(DECL_ARGS);
64 static  void      print_foot(DECL_ARGS);
65
66 static  void      termp____post(DECL_ARGS);
67 static  void      termp_an_post(DECL_ARGS);
68 static  void      termp_aq_post(DECL_ARGS);
69 static  void      termp_bd_post(DECL_ARGS);
70 static  void      termp_bl_post(DECL_ARGS);
71 static  void      termp_bq_post(DECL_ARGS);
72 static  void      termp_brq_post(DECL_ARGS);
73 static  void      termp_bx_post(DECL_ARGS);
74 static  void      termp_d1_post(DECL_ARGS);
75 static  void      termp_dq_post(DECL_ARGS);
76 static  void      termp_fd_post(DECL_ARGS);
77 static  void      termp_fn_post(DECL_ARGS);
78 static  void      termp_fo_post(DECL_ARGS);
79 static  void      termp_ft_post(DECL_ARGS);
80 static  void      termp_in_post(DECL_ARGS);
81 static  void      termp_it_post(DECL_ARGS);
82 static  void      termp_lb_post(DECL_ARGS);
83 static  void      termp_op_post(DECL_ARGS);
84 static  void      termp_pf_post(DECL_ARGS);
85 static  void      termp_pq_post(DECL_ARGS);
86 static  void      termp_qq_post(DECL_ARGS);
87 static  void      termp_sh_post(DECL_ARGS);
88 static  void      termp_sq_post(DECL_ARGS);
89 static  void      termp_ss_post(DECL_ARGS);
90 static  void      termp_vt_post(DECL_ARGS);
91
92 static  int       termp__t_pre(DECL_ARGS);
93 static  int       termp_an_pre(DECL_ARGS);
94 static  int       termp_ap_pre(DECL_ARGS);
95 static  int       termp_aq_pre(DECL_ARGS);
96 static  int       termp_bd_pre(DECL_ARGS);
97 static  int       termp_bf_pre(DECL_ARGS);
98 static  int       termp_bold_pre(DECL_ARGS);
99 static  int       termp_bq_pre(DECL_ARGS);
100 static  int       termp_brq_pre(DECL_ARGS);
101 static  int       termp_bt_pre(DECL_ARGS);
102 static  int       termp_cd_pre(DECL_ARGS);
103 static  int       termp_d1_pre(DECL_ARGS);
104 static  int       termp_dq_pre(DECL_ARGS);
105 static  int       termp_ex_pre(DECL_ARGS);
106 static  int       termp_fa_pre(DECL_ARGS);
107 static  int       termp_fl_pre(DECL_ARGS);
108 static  int       termp_fn_pre(DECL_ARGS);
109 static  int       termp_fo_pre(DECL_ARGS);
110 static  int       termp_ft_pre(DECL_ARGS);
111 static  int       termp_in_pre(DECL_ARGS);
112 static  int       termp_it_pre(DECL_ARGS);
113 static  int       termp_li_pre(DECL_ARGS);
114 static  int       termp_lk_pre(DECL_ARGS);
115 static  int       termp_nd_pre(DECL_ARGS);
116 static  int       termp_nm_pre(DECL_ARGS);
117 static  int       termp_ns_pre(DECL_ARGS);
118 static  int       termp_op_pre(DECL_ARGS);
119 static  int       termp_pf_pre(DECL_ARGS);
120 static  int       termp_pq_pre(DECL_ARGS);
121 static  int       termp_qq_pre(DECL_ARGS);
122 static  int       termp_rs_pre(DECL_ARGS);
123 static  int       termp_rv_pre(DECL_ARGS);
124 static  int       termp_sh_pre(DECL_ARGS);
125 static  int       termp_sm_pre(DECL_ARGS);
126 static  int       termp_sp_pre(DECL_ARGS);
127 static  int       termp_sq_pre(DECL_ARGS);
128 static  int       termp_ss_pre(DECL_ARGS);
129 static  int       termp_under_pre(DECL_ARGS);
130 static  int       termp_ud_pre(DECL_ARGS);
131 static  int       termp_vt_pre(DECL_ARGS);
132 static  int       termp_xr_pre(DECL_ARGS);
133 static  int       termp_xx_pre(DECL_ARGS);
134
135 static  const struct termact termacts[MDOC_MAX] = {
136         { termp_ap_pre, NULL }, /* Ap */
137         { NULL, NULL }, /* Dd */
138         { NULL, NULL }, /* Dt */
139         { NULL, NULL }, /* Os */
140         { termp_sh_pre, termp_sh_post }, /* Sh */
141         { termp_ss_pre, termp_ss_post }, /* Ss */
142         { termp_sp_pre, NULL }, /* Pp */
143         { termp_d1_pre, termp_d1_post }, /* D1 */
144         { termp_d1_pre, termp_d1_post }, /* Dl */
145         { termp_bd_pre, termp_bd_post }, /* Bd */
146         { NULL, NULL }, /* Ed */
147         { NULL, termp_bl_post }, /* Bl */
148         { NULL, NULL }, /* El */
149         { termp_it_pre, termp_it_post }, /* It */
150         { NULL, NULL }, /* Ad */
151         { termp_an_pre, termp_an_post }, /* An */
152         { termp_under_pre, NULL }, /* Ar */
153         { termp_cd_pre, NULL }, /* Cd */
154         { termp_bold_pre, NULL }, /* Cm */
155         { NULL, NULL }, /* Dv */
156         { NULL, NULL }, /* Er */
157         { NULL, NULL }, /* Ev */
158         { termp_ex_pre, NULL }, /* Ex */
159         { termp_fa_pre, NULL }, /* Fa */
160         { termp_bold_pre, termp_fd_post }, /* Fd */
161         { termp_fl_pre, NULL }, /* Fl */
162         { termp_fn_pre, termp_fn_post }, /* Fn */
163         { termp_ft_pre, termp_ft_post }, /* Ft */
164         { termp_bold_pre, NULL }, /* Ic */
165         { termp_in_pre, termp_in_post }, /* In */
166         { termp_li_pre, NULL }, /* Li */
167         { termp_nd_pre, NULL }, /* Nd */
168         { termp_nm_pre, NULL }, /* Nm */
169         { termp_op_pre, termp_op_post }, /* Op */
170         { NULL, NULL }, /* Ot */
171         { termp_under_pre, NULL }, /* Pa */
172         { termp_rv_pre, NULL }, /* Rv */
173         { NULL, NULL }, /* St */
174         { termp_under_pre, NULL }, /* Va */
175         { termp_vt_pre, termp_vt_post }, /* Vt */
176         { termp_xr_pre, NULL }, /* Xr */
177         { NULL, termp____post }, /* %A */
178         { termp_under_pre, termp____post }, /* %B */
179         { NULL, termp____post }, /* %D */
180         { termp_under_pre, termp____post }, /* %I */
181         { termp_under_pre, termp____post }, /* %J */
182         { NULL, termp____post }, /* %N */
183         { NULL, termp____post }, /* %O */
184         { NULL, termp____post }, /* %P */
185         { NULL, termp____post }, /* %R */
186         { termp__t_pre, termp____post }, /* %T */
187         { NULL, termp____post }, /* %V */
188         { NULL, NULL }, /* Ac */
189         { termp_aq_pre, termp_aq_post }, /* Ao */
190         { termp_aq_pre, termp_aq_post }, /* Aq */
191         { NULL, NULL }, /* At */
192         { NULL, NULL }, /* Bc */
193         { termp_bf_pre, NULL }, /* Bf */
194         { termp_bq_pre, termp_bq_post }, /* Bo */
195         { termp_bq_pre, termp_bq_post }, /* Bq */
196         { termp_xx_pre, NULL }, /* Bsx */
197         { NULL, termp_bx_post }, /* Bx */
198         { NULL, NULL }, /* Db */
199         { NULL, NULL }, /* Dc */
200         { termp_dq_pre, termp_dq_post }, /* Do */
201         { termp_dq_pre, termp_dq_post }, /* Dq */
202         { NULL, NULL }, /* Ec */
203         { NULL, NULL }, /* Ef */
204         { termp_under_pre, NULL }, /* Em */
205         { NULL, NULL }, /* Eo */
206         { termp_xx_pre, NULL }, /* Fx */
207         { termp_bold_pre, NULL }, /* Ms */ /* FIXME: convert to symbol? */
208         { NULL, NULL }, /* No */
209         { termp_ns_pre, NULL }, /* Ns */
210         { termp_xx_pre, NULL }, /* Nx */
211         { termp_xx_pre, NULL }, /* Ox */
212         { NULL, NULL }, /* Pc */
213         { termp_pf_pre, termp_pf_post }, /* Pf */
214         { termp_pq_pre, termp_pq_post }, /* Po */
215         { termp_pq_pre, termp_pq_post }, /* Pq */
216         { NULL, NULL }, /* Qc */
217         { termp_sq_pre, termp_sq_post }, /* Ql */
218         { termp_qq_pre, termp_qq_post }, /* Qo */
219         { termp_qq_pre, termp_qq_post }, /* Qq */
220         { NULL, NULL }, /* Re */
221         { termp_rs_pre, NULL }, /* Rs */
222         { NULL, NULL }, /* Sc */
223         { termp_sq_pre, termp_sq_post }, /* So */
224         { termp_sq_pre, termp_sq_post }, /* Sq */
225         { termp_sm_pre, NULL }, /* Sm */
226         { termp_under_pre, NULL }, /* Sx */
227         { termp_bold_pre, NULL }, /* Sy */
228         { NULL, NULL }, /* Tn */
229         { termp_xx_pre, NULL }, /* Ux */
230         { NULL, NULL }, /* Xc */
231         { NULL, NULL }, /* Xo */
232         { termp_fo_pre, termp_fo_post }, /* Fo */
233         { NULL, NULL }, /* Fc */
234         { termp_op_pre, termp_op_post }, /* Oo */
235         { NULL, NULL }, /* Oc */
236         { NULL, NULL }, /* Bk */
237         { NULL, NULL }, /* Ek */
238         { termp_bt_pre, NULL }, /* Bt */
239         { NULL, NULL }, /* Hf */
240         { NULL, NULL }, /* Fr */
241         { termp_ud_pre, NULL }, /* Ud */
242         { NULL, termp_lb_post }, /* Lb */
243         { termp_sp_pre, NULL }, /* Lp */
244         { termp_lk_pre, NULL }, /* Lk */
245         { termp_under_pre, NULL }, /* Mt */
246         { termp_brq_pre, termp_brq_post }, /* Brq */
247         { termp_brq_pre, termp_brq_post }, /* Bro */
248         { NULL, NULL }, /* Brc */
249         { NULL, termp____post }, /* %C */
250         { NULL, NULL }, /* Es */ /* TODO */
251         { NULL, NULL }, /* En */ /* TODO */
252         { termp_xx_pre, NULL }, /* Dx */
253         { NULL, termp____post }, /* %Q */
254         { termp_sp_pre, NULL }, /* br */
255         { termp_sp_pre, NULL }, /* sp */
256         { termp_under_pre, termp____post }, /* %U */
257 };
258
259
260 void
261 terminal_mdoc(void *arg, const struct mdoc *mdoc)
262 {
263         const struct mdoc_node  *n;
264         const struct mdoc_meta  *m;
265         struct termp            *p;
266
267         p = (struct termp *)arg;
268
269         p->overstep = 0;
270         p->maxrmargin = 78;
271
272         if (NULL == p->symtab)
273                 switch (p->enc) {
274                 case (TERMENC_ASCII):
275                         p->symtab = chars_init(CHARS_ASCII);
276                         break;
277                 default:
278                         abort();
279                         /* NOTREACHED */
280                 }
281
282         n = mdoc_node(mdoc);
283         m = mdoc_meta(mdoc);
284
285         print_mdoc_head(p, NULL, m, n);
286         if (n->child)
287                 print_mdoc_nodelist(p, NULL, m, n->child);
288         print_foot(p, NULL, m, n);
289 }
290
291
292 static void
293 print_mdoc_nodelist(DECL_ARGS)
294 {
295
296         print_mdoc_node(p, pair, m, n);
297         if (n->next)
298                 print_mdoc_nodelist(p, pair, m, n->next);
299 }
300
301
302 /* ARGSUSED */
303 static void
304 print_mdoc_node(DECL_ARGS)
305 {
306         int              chld;
307         const void      *font;
308         struct termpair  npair;
309         size_t           offset, rmargin;
310
311         chld = 1;
312         offset = p->offset;
313         rmargin = p->rmargin;
314         font = term_fontq(p);
315
316         memset(&npair, 0, sizeof(struct termpair));
317         npair.ppair = pair;
318
319         if (MDOC_TEXT != n->type) {
320                 if (termacts[n->tok].pre)
321                         chld = (*termacts[n->tok].pre)(p, &npair, m, n);
322         } else
323                 term_word(p, n->string);
324
325         if (chld && n->child)
326                 print_mdoc_nodelist(p, &npair, m, n->child);
327
328         term_fontpopq(p, font);
329
330         if (MDOC_TEXT != n->type)
331                 if (termacts[n->tok].post)
332                         (*termacts[n->tok].post)(p, &npair, m, n);
333
334         p->offset = offset;
335         p->rmargin = rmargin;
336 }
337
338
339 /* ARGSUSED */
340 static void
341 print_foot(DECL_ARGS)
342 {
343         char            buf[DATESIZ], os[BUFSIZ];
344
345         term_fontrepl(p, TERMFONT_NONE);
346
347         /*
348          * Output the footer in new-groff style, that is, three columns
349          * with the middle being the manual date and flanking columns
350          * being the operating system:
351          *
352          * SYSTEM                  DATE                    SYSTEM
353          */
354
355         time2a(m->date, buf, DATESIZ);
356         strlcpy(os, m->os, BUFSIZ);
357
358         term_vspace(p);
359
360         p->offset = 0;
361         p->rmargin = (p->maxrmargin - strlen(buf) + 1) / 2;
362         p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
363
364         term_word(p, os);
365         term_flushln(p);
366
367         p->offset = p->rmargin;
368         p->rmargin = p->maxrmargin - strlen(os);
369         p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
370
371         term_word(p, buf);
372         term_flushln(p);
373
374         p->offset = p->rmargin;
375         p->rmargin = p->maxrmargin;
376         p->flags &= ~TERMP_NOBREAK;
377         p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
378
379         term_word(p, os);
380         term_flushln(p);
381
382         p->offset = 0;
383         p->rmargin = p->maxrmargin;
384         p->flags = 0;
385 }
386
387
388 /* ARGSUSED */
389 static void
390 print_mdoc_head(DECL_ARGS)
391 {
392         char            buf[BUFSIZ], title[BUFSIZ];
393
394         p->rmargin = p->maxrmargin;
395         p->offset = 0;
396
397         /*
398          * The header is strange.  It has three components, which are
399          * really two with the first duplicated.  It goes like this:
400          *
401          * IDENTIFIER              TITLE                   IDENTIFIER
402          *
403          * The IDENTIFIER is NAME(SECTION), which is the command-name
404          * (if given, or "unknown" if not) followed by the manual page
405          * section.  These are given in `Dt'.  The TITLE is a free-form
406          * string depending on the manual volume.  If not specified, it
407          * switches on the manual section.
408          */
409
410         assert(m->vol);
411         strlcpy(buf, m->vol, BUFSIZ);
412
413         if (m->arch) {
414                 strlcat(buf, " (", BUFSIZ);
415                 strlcat(buf, m->arch, BUFSIZ);
416                 strlcat(buf, ")", BUFSIZ);
417         }
418
419         snprintf(title, BUFSIZ, "%s(%d)", m->title, m->msec);
420
421         p->offset = 0;
422         p->rmargin = (p->maxrmargin - strlen(buf) + 1) / 2;
423         p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
424
425         term_word(p, title);
426         term_flushln(p);
427
428         p->offset = p->rmargin;
429         p->rmargin = p->maxrmargin - strlen(title);
430         p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
431
432         term_word(p, buf);
433         term_flushln(p);
434
435         p->offset = p->rmargin;
436         p->rmargin = p->maxrmargin;
437         p->flags &= ~TERMP_NOBREAK;
438         p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
439
440         term_word(p, title);
441         term_flushln(p);
442
443         p->offset = 0;
444         p->rmargin = p->maxrmargin;
445         p->flags &= ~TERMP_NOSPACE;
446 }
447
448
449 static size_t
450 a2height(const struct mdoc_node *n)
451 {
452         struct roffsu    su;
453
454         assert(MDOC_TEXT == n->type);
455         assert(n->string);
456         if ( ! a2roffsu(n->string, &su, SCALE_VS))
457                 SCALE_VS_INIT(&su, strlen(n->string));
458
459         return(term_vspan(&su));
460 }
461
462
463 static size_t
464 a2width(const struct mdoc_argv *arg, int pos)
465 {
466         struct roffsu    su;
467
468         assert(arg->value[pos]);
469         if ( ! a2roffsu(arg->value[pos], &su, SCALE_MAX))
470                 SCALE_HS_INIT(&su, strlen(arg->value[pos]));
471
472         return(term_hspan(&su));
473 }
474
475
476 static int
477 arg_listtype(const struct mdoc_node *n)
478 {
479         int              i, len;
480
481         assert(MDOC_BLOCK == n->type);
482
483         len = (int)(n->args ? n->args->argc : 0);
484
485         for (i = 0; i < len; i++)
486                 switch (n->args->argv[i].arg) {
487                 case (MDOC_Bullet):
488                         /* FALLTHROUGH */
489                 case (MDOC_Dash):
490                         /* FALLTHROUGH */
491                 case (MDOC_Enum):
492                         /* FALLTHROUGH */
493                 case (MDOC_Hyphen):
494                         /* FALLTHROUGH */
495                 case (MDOC_Tag):
496                         /* FALLTHROUGH */
497                 case (MDOC_Inset):
498                         /* FALLTHROUGH */
499                 case (MDOC_Diag):
500                         /* FALLTHROUGH */
501                 case (MDOC_Item):
502                         /* FALLTHROUGH */
503                 case (MDOC_Column):
504                         /* FALLTHROUGH */
505                 case (MDOC_Hang):
506                         /* FALLTHROUGH */
507                 case (MDOC_Ohang):
508                         return(n->args->argv[i].arg);
509                 default:
510                         break;
511                 }
512
513         return(-1);
514 }
515
516
517 static size_t
518 a2offs(const struct mdoc_argv *arg)
519 {
520         struct roffsu    su;
521
522         if ('\0' == arg->value[0][0])
523                 return(0);
524         else if (0 == strcmp(arg->value[0], "left"))
525                 return(0);
526         else if (0 == strcmp(arg->value[0], "indent"))
527                 return(INDENT + 1);
528         else if (0 == strcmp(arg->value[0], "indent-two"))
529                 return((INDENT + 1) * 2);
530         else if ( ! a2roffsu(arg->value[0], &su, SCALE_MAX))
531                 SCALE_HS_INIT(&su, strlen(arg->value[0]));
532
533         return(term_hspan(&su));
534 }
535
536
537 /*
538  * Return 1 if an argument has a particular argument value or 0 if it
539  * does not.  See arg_getattr().
540  */
541 static int
542 arg_hasattr(int arg, const struct mdoc_node *n)
543 {
544
545         return(-1 != arg_getattr(arg, n));
546 }
547
548
549 /*
550  * Get the index of an argument in a node's argument list or -1 if it
551  * does not exist.  See arg_getattrs().
552  */
553 static int
554 arg_getattr(int v, const struct mdoc_node *n)
555 {
556         int              val;
557
558         return(arg_getattrs(&v, &val, 1, n) ? val : -1);
559 }
560
561
562 /*
563  * Walk through the argument list for a node and fill an array "vals"
564  * with the positions of the argument structures listed in "keys".
565  * Return the number of elements that were written into "vals", which
566  * can be zero.
567  */
568 static int
569 arg_getattrs(const int *keys, int *vals,
570                 size_t sz, const struct mdoc_node *n)
571 {
572         int              i, j, k;
573
574         if (NULL == n->args)
575                 return(0);
576
577         for (k = i = 0; i < (int)n->args->argc; i++)
578                 for (j = 0; j < (int)sz; j++)
579                         if (n->args->argv[i].arg == keys[j]) {
580                                 vals[j] = i;
581                                 k++;
582                         }
583         return(k);
584 }
585
586
587 /*
588  * Determine how much space to print out before block elements of `It'
589  * (and thus `Bl') and `Bd'.  And then go ahead and print that space,
590  * too.
591  */
592 static void
593 print_bvspace(struct termp *p,
594                 const struct mdoc_node *bl,
595                 const struct mdoc_node *n)
596 {
597         const struct mdoc_node  *nn;
598
599         term_newln(p);
600         if (arg_hasattr(MDOC_Compact, bl))
601                 return;
602
603         /* Do not vspace directly after Ss/Sh. */
604
605         for (nn = n; nn; nn = nn->parent) {
606                 if (MDOC_BLOCK != nn->type)
607                         continue;
608                 if (MDOC_Ss == nn->tok)
609                         return;
610                 if (MDOC_Sh == nn->tok)
611                         return;
612                 if (NULL == nn->prev)
613                         continue;
614                 break;
615         }
616
617         /* A `-column' does not assert vspace within the list. */
618
619         if (MDOC_Bl == bl->tok && arg_hasattr(MDOC_Column, bl))
620                 if (n->prev && MDOC_It == n->prev->tok)
621                         return;
622
623         /* A `-diag' without body does not vspace. */
624
625         if (MDOC_Bl == bl->tok && arg_hasattr(MDOC_Diag, bl))
626                 if (n->prev && MDOC_It == n->prev->tok) {
627                         assert(n->prev->body);
628                         if (NULL == n->prev->body->child)
629                                 return;
630                 }
631
632         term_vspace(p);
633 }
634
635
636 /* ARGSUSED */
637 static int
638 termp_dq_pre(DECL_ARGS)
639 {
640
641         if (MDOC_BODY != n->type)
642                 return(1);
643
644         term_word(p, "\\(lq");
645         p->flags |= TERMP_NOSPACE;
646         return(1);
647 }
648
649
650 /* ARGSUSED */
651 static void
652 termp_dq_post(DECL_ARGS)
653 {
654
655         if (MDOC_BODY != n->type)
656                 return;
657
658         p->flags |= TERMP_NOSPACE;
659         term_word(p, "\\(rq");
660 }
661
662
663 /* ARGSUSED */
664 static int
665 termp_it_pre(DECL_ARGS)
666 {
667         const struct mdoc_node *bl, *nn;
668         char                    buf[7];
669         int                     i, type, keys[3], vals[3];
670         size_t                  width, offset, ncols, dcol;
671
672         if (MDOC_BLOCK == n->type) {
673                 print_bvspace(p, n->parent->parent, n);
674                 return(1);
675         }
676
677         bl = n->parent->parent->parent;
678
679         /* Get list width, offset, and list type from argument list. */
680
681         keys[0] = MDOC_Width;
682         keys[1] = MDOC_Offset;
683         keys[2] = MDOC_Column;
684
685         vals[0] = vals[1] = vals[2] = -1;
686
687         arg_getattrs(keys, vals, 3, bl);
688
689         type = arg_listtype(bl);
690         assert(-1 != type);
691
692         /*
693          * First calculate width and offset.  This is pretty easy unless
694          * we're a -column list, in which case all prior columns must
695          * be accounted for.
696          */
697
698         width = offset = 0;
699
700         if (vals[1] >= 0)
701                 offset = a2offs(&bl->args->argv[vals[1]]);
702
703         switch (type) {
704         case (MDOC_Column):
705                 if (MDOC_BODY == n->type)
706                         break;
707                 /*
708                  * Imitate groff's column handling:
709                  * - For each earlier column, add its width.
710                  * - For less than 5 columns, add four more blanks per
711                  *   column.
712                  * - For exactly 5 columns, add three more blank per
713                  *   column.
714                  * - For more than 5 columns, add only one column.
715                  */
716                 ncols = bl->args->argv[vals[2]].sz;
717                 /* LINTED */
718                 dcol = ncols < 5 ? 4 : ncols == 5 ? 3 : 1;
719
720                 for (i = 0, nn = n->prev;
721                                 nn && i < (int)ncols;
722                                 nn = nn->prev, i++)
723                         offset += dcol + a2width
724                                 (&bl->args->argv[vals[2]], i);
725
726
727                 /*
728                  * When exceeding the declared number of columns, leave
729                  * the remaining widths at 0.  This will later be
730                  * adjusted to the default width of 10, or, for the last
731                  * column, stretched to the right margin.
732                  */
733                 if (i >= (int)ncols)
734                         break;
735
736                 /*
737                  * Use the declared column widths, extended as explained
738                  * in the preceding paragraph.
739                  */
740                 width = a2width(&bl->args->argv[vals[2]], i) + dcol;
741                 break;
742         default:
743                 if (vals[0] < 0)
744                         break;
745
746                 /*
747                  * Note: buffer the width by 2, which is groff's magic
748                  * number for buffering single arguments.  See the above
749                  * handling for column for how this changes.
750                  */
751                 width = a2width(&bl->args->argv[vals[0]], 0) + 2;
752                 break;
753         }
754
755         /*
756          * List-type can override the width in the case of fixed-head
757          * values (bullet, dash/hyphen, enum).  Tags need a non-zero
758          * offset.
759          */
760
761         switch (type) {
762         case (MDOC_Bullet):
763                 /* FALLTHROUGH */
764         case (MDOC_Dash):
765                 /* FALLTHROUGH */
766         case (MDOC_Hyphen):
767                 if (width < 4)
768                         width = 4;
769                 break;
770         case (MDOC_Enum):
771                 if (width < 5)
772                         width = 5;
773                 break;
774         case (MDOC_Hang):
775                 if (0 == width)
776                         width = 8;
777                 break;
778         case (MDOC_Column):
779                 /* FALLTHROUGH */
780         case (MDOC_Tag):
781                 if (0 == width)
782                         width = 10;
783                 break;
784         default:
785                 break;
786         }
787
788         /*
789          * Whitespace control.  Inset bodies need an initial space,
790          * while diagonal bodies need two.
791          */
792
793         p->flags |= TERMP_NOSPACE;
794
795         switch (type) {
796         case (MDOC_Diag):
797                 if (MDOC_BODY == n->type)
798                         term_word(p, "\\ \\ ");
799                 break;
800         case (MDOC_Inset):
801                 if (MDOC_BODY == n->type)
802                         term_word(p, "\\ ");
803                 break;
804         default:
805                 break;
806         }
807
808         p->flags |= TERMP_NOSPACE;
809
810         switch (type) {
811         case (MDOC_Diag):
812                 if (MDOC_HEAD == n->type)
813                         term_fontpush(p, TERMFONT_BOLD);
814                 break;
815         default:
816                 break;
817         }
818
819         /*
820          * Pad and break control.  This is the tricky part.  These flags
821          * are documented in term_flushln() in term.c.  Note that we're
822          * going to unset all of these flags in termp_it_post() when we
823          * exit.
824          */
825
826         switch (type) {
827         case (MDOC_Bullet):
828                 /* FALLTHROUGH */
829         case (MDOC_Dash):
830                 /* FALLTHROUGH */
831         case (MDOC_Enum):
832                 /* FALLTHROUGH */
833         case (MDOC_Hyphen):
834                 if (MDOC_HEAD == n->type)
835                         p->flags |= TERMP_NOBREAK;
836                 else
837                         p->flags |= TERMP_NOLPAD;
838                 break;
839         case (MDOC_Hang):
840                 if (MDOC_HEAD == n->type)
841                         p->flags |= TERMP_NOBREAK;
842                 else
843                         p->flags |= TERMP_NOLPAD;
844
845                 if (MDOC_HEAD != n->type)
846                         break;
847
848                 /*
849                  * This is ugly.  If `-hang' is specified and the body
850                  * is a `Bl' or `Bd', then we want basically to nullify
851                  * the "overstep" effect in term_flushln() and treat
852                  * this as a `-ohang' list instead.
853                  */
854                 if (n->next->child &&
855                                 (MDOC_Bl == n->next->child->tok ||
856                                  MDOC_Bd == n->next->child->tok)) {
857                         p->flags &= ~TERMP_NOBREAK;
858                         p->flags &= ~TERMP_NOLPAD;
859                 } else
860                         p->flags |= TERMP_HANG;
861                 break;
862         case (MDOC_Tag):
863                 if (MDOC_HEAD == n->type)
864                         p->flags |= TERMP_NOBREAK | TERMP_TWOSPACE;
865                 else
866                         p->flags |= TERMP_NOLPAD;
867
868                 if (MDOC_HEAD != n->type)
869                         break;
870                 if (NULL == n->next || NULL == n->next->child)
871                         p->flags |= TERMP_DANGLE;
872                 break;
873         case (MDOC_Column):
874                 if (MDOC_HEAD == n->type) {
875                         assert(n->next);
876                         if (MDOC_BODY == n->next->type)
877                                 p->flags &= ~TERMP_NOBREAK;
878                         else
879                                 p->flags |= TERMP_NOBREAK;
880                         if (n->prev)
881                                 p->flags |= TERMP_NOLPAD;
882                 }
883                 break;
884         case (MDOC_Diag):
885                 if (MDOC_HEAD == n->type)
886                         p->flags |= TERMP_NOBREAK;
887                 break;
888         default:
889                 break;
890         }
891
892         /*
893          * Margin control.  Set-head-width lists have their right
894          * margins shortened.  The body for these lists has the offset
895          * necessarily lengthened.  Everybody gets the offset.
896          */
897
898         p->offset += offset;
899
900         switch (type) {
901         case (MDOC_Hang):
902                 /*
903                  * Same stipulation as above, regarding `-hang'.  We
904                  * don't want to recalculate rmargin and offsets when
905                  * using `Bd' or `Bl' within `-hang' overstep lists.
906                  */
907                 if (MDOC_HEAD == n->type && n->next->child &&
908                                 (MDOC_Bl == n->next->child->tok ||
909                                  MDOC_Bd == n->next->child->tok))
910                         break;
911                 /* FALLTHROUGH */
912         case (MDOC_Bullet):
913                 /* FALLTHROUGH */
914         case (MDOC_Dash):
915                 /* FALLTHROUGH */
916         case (MDOC_Enum):
917                 /* FALLTHROUGH */
918         case (MDOC_Hyphen):
919                 /* FALLTHROUGH */
920         case (MDOC_Tag):
921                 assert(width);
922                 if (MDOC_HEAD == n->type)
923                         p->rmargin = p->offset + width;
924                 else
925                         p->offset += width;
926                 break;
927         case (MDOC_Column):
928                 assert(width);
929                 p->rmargin = p->offset + width;
930                 /*
931                  * XXX - this behaviour is not documented: the
932                  * right-most column is filled to the right margin.
933                  */
934                 if (MDOC_HEAD == n->type &&
935                                 MDOC_BODY == n->next->type &&
936                                 p->rmargin < p->maxrmargin)
937                         p->rmargin = p->maxrmargin;
938                 break;
939         default:
940                 break;
941         }
942
943         /*
944          * The dash, hyphen, bullet and enum lists all have a special
945          * HEAD character (temporarily bold, in some cases).
946          */
947
948         if (MDOC_HEAD == n->type)
949                 switch (type) {
950                 case (MDOC_Bullet):
951                         term_fontpush(p, TERMFONT_BOLD);
952                         term_word(p, "\\[bu]");
953                         term_fontpop(p);
954                         break;
955                 case (MDOC_Dash):
956                         /* FALLTHROUGH */
957                 case (MDOC_Hyphen):
958                         term_fontpush(p, TERMFONT_BOLD);
959                         term_word(p, "\\(hy");
960                         term_fontpop(p);
961                         break;
962                 case (MDOC_Enum):
963                         (pair->ppair->ppair->count)++;
964                         snprintf(buf, sizeof(buf), "%d.",
965                                         pair->ppair->ppair->count);
966                         term_word(p, buf);
967                         break;
968                 default:
969                         break;
970                 }
971
972         /*
973          * If we're not going to process our children, indicate so here.
974          */
975
976         switch (type) {
977         case (MDOC_Bullet):
978                 /* FALLTHROUGH */
979         case (MDOC_Item):
980                 /* FALLTHROUGH */
981         case (MDOC_Dash):
982                 /* FALLTHROUGH */
983         case (MDOC_Hyphen):
984                 /* FALLTHROUGH */
985         case (MDOC_Enum):
986                 if (MDOC_HEAD == n->type)
987                         return(0);
988                 break;
989         case (MDOC_Column):
990                 if (MDOC_BODY == n->type)
991                         return(0);
992                 break;
993         default:
994                 break;
995         }
996
997         return(1);
998 }
999
1000
1001 /* ARGSUSED */
1002 static void
1003 termp_it_post(DECL_ARGS)
1004 {
1005         int                type;
1006
1007         if (MDOC_BLOCK == n->type)
1008                 return;
1009
1010         type = arg_listtype(n->parent->parent->parent);
1011         assert(-1 != type);
1012
1013         switch (type) {
1014         case (MDOC_Item):
1015                 /* FALLTHROUGH */
1016         case (MDOC_Diag):
1017                 /* FALLTHROUGH */
1018         case (MDOC_Inset):
1019                 if (MDOC_BODY == n->type)
1020                         term_flushln(p);
1021                 break;
1022         case (MDOC_Column):
1023                 if (MDOC_HEAD == n->type)
1024                         term_flushln(p);
1025                 break;
1026         default:
1027                 term_flushln(p);
1028                 break;
1029         }
1030
1031         /*
1032          * Now that our output is flushed, we can reset our tags.  Since
1033          * only `It' sets these flags, we're free to assume that nobody
1034          * has munged them in the meanwhile.
1035          */
1036
1037         p->flags &= ~TERMP_DANGLE;
1038         p->flags &= ~TERMP_NOBREAK;
1039         p->flags &= ~TERMP_TWOSPACE;
1040         p->flags &= ~TERMP_NOLPAD;
1041         p->flags &= ~TERMP_HANG;
1042 }
1043
1044
1045 /* ARGSUSED */
1046 static int
1047 termp_nm_pre(DECL_ARGS)
1048 {
1049
1050         if (SEC_SYNOPSIS == n->sec)
1051                 term_newln(p);
1052
1053         term_fontpush(p, TERMFONT_BOLD);
1054
1055         if (NULL == n->child)
1056                 term_word(p, m->name);
1057         return(1);
1058 }
1059
1060
1061 /* ARGSUSED */
1062 static int
1063 termp_fl_pre(DECL_ARGS)
1064 {
1065
1066         term_fontpush(p, TERMFONT_BOLD);
1067         term_word(p, "\\-");
1068
1069         /* A blank `Fl' should incur a subsequent space. */
1070
1071         if (n->child)
1072                 p->flags |= TERMP_NOSPACE;
1073
1074         return(1);
1075 }
1076
1077
1078 /* ARGSUSED */
1079 static int
1080 termp_an_pre(DECL_ARGS)
1081 {
1082
1083         if (NULL == n->child)
1084                 return(1);
1085
1086         /*
1087          * If not in the AUTHORS section, `An -split' will cause
1088          * newlines to occur before the author name.  If in the AUTHORS
1089          * section, by default, the first `An' invocation is nosplit,
1090          * then all subsequent ones, regardless of whether interspersed
1091          * with other macros/text, are split.  -split, in this case,
1092          * will override the condition of the implied first -nosplit.
1093          */
1094
1095         if (n->sec == SEC_AUTHORS) {
1096                 if ( ! (TERMP_ANPREC & p->flags)) {
1097                         if (TERMP_SPLIT & p->flags)
1098                                 term_newln(p);
1099                         return(1);
1100                 }
1101                 if (TERMP_NOSPLIT & p->flags)
1102                         return(1);
1103                 term_newln(p);
1104                 return(1);
1105         }
1106
1107         if (TERMP_SPLIT & p->flags)
1108                 term_newln(p);
1109
1110         return(1);
1111 }
1112
1113
1114 /* ARGSUSED */
1115 static void
1116 termp_an_post(DECL_ARGS)
1117 {
1118
1119         if (n->child) {
1120                 if (SEC_AUTHORS == n->sec)
1121                         p->flags |= TERMP_ANPREC;
1122                 return;
1123         }
1124
1125         if (arg_getattr(MDOC_Split, n) > -1) {
1126                 p->flags &= ~TERMP_NOSPLIT;
1127                 p->flags |= TERMP_SPLIT;
1128         } else {
1129                 p->flags &= ~TERMP_SPLIT;
1130                 p->flags |= TERMP_NOSPLIT;
1131         }
1132
1133 }
1134
1135
1136 /* ARGSUSED */
1137 static int
1138 termp_ns_pre(DECL_ARGS)
1139 {
1140
1141         p->flags |= TERMP_NOSPACE;
1142         return(1);
1143 }
1144
1145
1146 /* ARGSUSED */
1147 static int
1148 termp_rs_pre(DECL_ARGS)
1149 {
1150
1151         if (SEC_SEE_ALSO != n->sec)
1152                 return(1);
1153         if (MDOC_BLOCK == n->type && n->prev)
1154                 term_vspace(p);
1155         return(1);
1156 }
1157
1158
1159 /* ARGSUSED */
1160 static int
1161 termp_rv_pre(DECL_ARGS)
1162 {
1163         const struct mdoc_node  *nn;
1164
1165         term_newln(p);
1166         term_word(p, "The");
1167
1168         for (nn = n->child; nn; nn = nn->next) {
1169                 term_fontpush(p, TERMFONT_BOLD);
1170                 term_word(p, nn->string);
1171                 term_fontpop(p);
1172                 p->flags |= TERMP_NOSPACE;
1173                 if (nn->next && NULL == nn->next->next)
1174                         term_word(p, "(), and");
1175                 else if (nn->next)
1176                         term_word(p, "(),");
1177                 else
1178                         term_word(p, "()");
1179         }
1180
1181         if (n->child->next)
1182                 term_word(p, "functions return");
1183         else
1184                 term_word(p, "function returns");
1185
1186         term_word(p, "the value 0 if successful; otherwise the value "
1187                         "-1 is returned and the global variable");
1188
1189         term_fontpush(p, TERMFONT_UNDER);
1190         term_word(p, "errno");
1191         term_fontpop(p);
1192
1193         term_word(p, "is set to indicate the error.");
1194
1195         return(0);
1196 }
1197
1198
1199 /* ARGSUSED */
1200 static int
1201 termp_ex_pre(DECL_ARGS)
1202 {
1203         const struct mdoc_node  *nn;
1204
1205         term_word(p, "The");
1206
1207         for (nn = n->child; nn; nn = nn->next) {
1208                 term_fontpush(p, TERMFONT_BOLD);
1209                 term_word(p, nn->string);
1210                 term_fontpop(p);
1211                 p->flags |= TERMP_NOSPACE;
1212                 if (nn->next && NULL == nn->next->next)
1213                         term_word(p, ", and");
1214                 else if (nn->next)
1215                         term_word(p, ",");
1216                 else
1217                         p->flags &= ~TERMP_NOSPACE;
1218         }
1219
1220         if (n->child->next)
1221                 term_word(p, "utilities exit");
1222         else
1223                 term_word(p, "utility exits");
1224
1225         term_word(p, "0 on success, and >0 if an error occurs.");
1226
1227         return(0);
1228 }
1229
1230
1231 /* ARGSUSED */
1232 static int
1233 termp_nd_pre(DECL_ARGS)
1234 {
1235
1236         if (MDOC_BODY != n->type)
1237                 return(1);
1238
1239 #if defined(__OpenBSD__) || defined(__linux__)
1240         term_word(p, "\\(en");
1241 #else
1242         term_word(p, "\\(em");
1243 #endif
1244         return(1);
1245 }
1246
1247
1248 /* ARGSUSED */
1249 static void
1250 termp_bl_post(DECL_ARGS)
1251 {
1252
1253         if (MDOC_BLOCK == n->type)
1254                 term_newln(p);
1255 }
1256
1257
1258 /* ARGSUSED */
1259 static void
1260 termp_op_post(DECL_ARGS)
1261 {
1262
1263         if (MDOC_BODY != n->type)
1264                 return;
1265         p->flags |= TERMP_NOSPACE;
1266         term_word(p, "\\(rB");
1267 }
1268
1269
1270 /* ARGSUSED */
1271 static int
1272 termp_xr_pre(DECL_ARGS)
1273 {
1274         const struct mdoc_node *nn;
1275
1276         assert(n->child && MDOC_TEXT == n->child->type);
1277         nn = n->child;
1278
1279         term_word(p, nn->string);
1280         if (NULL == (nn = nn->next))
1281                 return(0);
1282         p->flags |= TERMP_NOSPACE;
1283         term_word(p, "(");
1284         p->flags |= TERMP_NOSPACE;
1285         term_word(p, nn->string);
1286         p->flags |= TERMP_NOSPACE;
1287         term_word(p, ")");
1288
1289         return(0);
1290 }
1291
1292
1293 static int
1294 termp_vt_pre(DECL_ARGS)
1295 {
1296
1297         if (MDOC_ELEM == n->type)
1298                 return(termp_under_pre(p, pair, m, n));
1299         else if (MDOC_HEAD == n->type)
1300                 return(0);
1301         else if (MDOC_BLOCK == n->type)
1302                 return(1);
1303
1304         return(termp_under_pre(p, pair, m, n));
1305 }
1306
1307
1308 /* ARGSUSED */
1309 static void
1310 termp_vt_post(DECL_ARGS)
1311 {
1312
1313         if (MDOC_BLOCK != n->type)
1314                 return;
1315         if (n->next && MDOC_Vt == n->next->tok)
1316                 term_newln(p);
1317         else if (n->next)
1318                 term_vspace(p);
1319 }
1320
1321
1322 /* ARGSUSED */
1323 static int
1324 termp_bold_pre(DECL_ARGS)
1325 {
1326
1327         term_fontpush(p, TERMFONT_BOLD);
1328         return(1);
1329 }
1330
1331
1332 /* ARGSUSED */
1333 static void
1334 termp_fd_post(DECL_ARGS)
1335 {
1336
1337         if (n->sec != SEC_SYNOPSIS)
1338                 return;
1339
1340         term_newln(p);
1341         if (n->next && MDOC_Fd != n->next->tok)
1342                 term_vspace(p);
1343 }
1344
1345
1346 /* ARGSUSED */
1347 static int
1348 termp_sh_pre(DECL_ARGS)
1349 {
1350
1351         /* No vspace between consecutive `Sh' calls. */
1352
1353         switch (n->type) {
1354         case (MDOC_BLOCK):
1355                 if (n->prev && MDOC_Sh == n->prev->tok)
1356                         if (NULL == n->prev->body->child)
1357                                 break;
1358                 term_vspace(p);
1359                 break;
1360         case (MDOC_HEAD):
1361                 term_fontpush(p, TERMFONT_BOLD);
1362                 break;
1363         case (MDOC_BODY):
1364                 p->offset = INDENT;
1365                 break;
1366         default:
1367                 break;
1368         }
1369         return(1);
1370 }
1371
1372
1373 /* ARGSUSED */
1374 static void
1375 termp_sh_post(DECL_ARGS)
1376 {
1377
1378         switch (n->type) {
1379         case (MDOC_HEAD):
1380                 term_newln(p);
1381                 break;
1382         case (MDOC_BODY):
1383                 term_newln(p);
1384                 p->offset = 0;
1385                 break;
1386         default:
1387                 break;
1388         }
1389 }
1390
1391
1392 /* ARGSUSED */
1393 static int
1394 termp_op_pre(DECL_ARGS)
1395 {
1396
1397         switch (n->type) {
1398         case (MDOC_BODY):
1399                 term_word(p, "\\(lB");
1400                 p->flags |= TERMP_NOSPACE;
1401                 break;
1402         default:
1403                 break;
1404         }
1405         return(1);
1406 }
1407
1408
1409 /* ARGSUSED */
1410 static int
1411 termp_bt_pre(DECL_ARGS)
1412 {
1413
1414         term_word(p, "is currently in beta test.");
1415         return(0);
1416 }
1417
1418
1419 /* ARGSUSED */
1420 static void
1421 termp_lb_post(DECL_ARGS)
1422 {
1423
1424         if (SEC_LIBRARY == n->sec)
1425                 term_newln(p);
1426 }
1427
1428
1429 /* ARGSUSED */
1430 static int
1431 termp_ud_pre(DECL_ARGS)
1432 {
1433
1434         term_word(p, "currently under development.");
1435         return(0);
1436 }
1437
1438
1439 /* ARGSUSED */
1440 static int
1441 termp_d1_pre(DECL_ARGS)
1442 {
1443
1444         if (MDOC_BLOCK != n->type)
1445                 return(1);
1446         term_newln(p);
1447         p->offset += (INDENT + 1);
1448         return(1);
1449 }
1450
1451
1452 /* ARGSUSED */
1453 static void
1454 termp_d1_post(DECL_ARGS)
1455 {
1456
1457         if (MDOC_BLOCK != n->type)
1458                 return;
1459         term_newln(p);
1460 }
1461
1462
1463 /* ARGSUSED */
1464 static int
1465 termp_aq_pre(DECL_ARGS)
1466 {
1467
1468         if (MDOC_BODY != n->type)
1469                 return(1);
1470         term_word(p, "\\(la");
1471         p->flags |= TERMP_NOSPACE;
1472         return(1);
1473 }
1474
1475
1476 /* ARGSUSED */
1477 static void
1478 termp_aq_post(DECL_ARGS)
1479 {
1480
1481         if (MDOC_BODY != n->type)
1482                 return;
1483         p->flags |= TERMP_NOSPACE;
1484         term_word(p, "\\(ra");
1485 }
1486
1487
1488 /* ARGSUSED */
1489 static int
1490 termp_ft_pre(DECL_ARGS)
1491 {
1492
1493         if (SEC_SYNOPSIS == n->sec)
1494                 if (n->prev && MDOC_Fo == n->prev->tok)
1495                         term_vspace(p);
1496
1497         term_fontpush(p, TERMFONT_UNDER);
1498         return(1);
1499 }
1500
1501
1502 /* ARGSUSED */
1503 static void
1504 termp_ft_post(DECL_ARGS)
1505 {
1506
1507         if (SEC_SYNOPSIS == n->sec)
1508                 term_newln(p);
1509 }
1510
1511
1512 /* ARGSUSED */
1513 static int
1514 termp_fn_pre(DECL_ARGS)
1515 {
1516         const struct mdoc_node  *nn;
1517
1518         term_fontpush(p, TERMFONT_BOLD);
1519         term_word(p, n->child->string);
1520         term_fontpop(p);
1521
1522         p->flags |= TERMP_NOSPACE;
1523         term_word(p, "(");
1524
1525         for (nn = n->child->next; nn; nn = nn->next) {
1526                 term_fontpush(p, TERMFONT_UNDER);
1527                 term_word(p, nn->string);
1528                 term_fontpop(p);
1529
1530                 if (nn->next)
1531                         term_word(p, ",");
1532         }
1533
1534         term_word(p, ")");
1535
1536         if (SEC_SYNOPSIS == n->sec)
1537                 term_word(p, ";");
1538
1539         return(0);
1540 }
1541
1542
1543 /* ARGSUSED */
1544 static void
1545 termp_fn_post(DECL_ARGS)
1546 {
1547
1548         if (n->sec == SEC_SYNOPSIS && n->next)
1549                 term_vspace(p);
1550 }
1551
1552
1553 /* ARGSUSED */
1554 static int
1555 termp_fa_pre(DECL_ARGS)
1556 {
1557         const struct mdoc_node  *nn;
1558
1559         if (n->parent->tok != MDOC_Fo) {
1560                 term_fontpush(p, TERMFONT_UNDER);
1561                 return(1);
1562         }
1563
1564         for (nn = n->child; nn; nn = nn->next) {
1565                 term_fontpush(p, TERMFONT_UNDER);
1566                 term_word(p, nn->string);
1567                 term_fontpop(p);
1568
1569                 if (nn->next)
1570                         term_word(p, ",");
1571         }
1572
1573         if (n->child && n->next && n->next->tok == MDOC_Fa)
1574                 term_word(p, ",");
1575
1576         return(0);
1577 }
1578
1579
1580 /* ARGSUSED */
1581 static int
1582 termp_bd_pre(DECL_ARGS)
1583 {
1584         int                      i, type;
1585         const struct mdoc_node  *nn;
1586
1587         if (MDOC_BLOCK == n->type) {
1588                 print_bvspace(p, n, n);
1589                 return(1);
1590         } else if (MDOC_BODY != n->type)
1591                 return(1);
1592
1593         nn = n->parent;
1594
1595         for (type = -1, i = 0; i < (int)nn->args->argc; i++) {
1596                 switch (nn->args->argv[i].arg) {
1597                 case (MDOC_Centred):
1598                         /* FALLTHROUGH */
1599                 case (MDOC_Ragged):
1600                         /* FALLTHROUGH */
1601                 case (MDOC_Filled):
1602                         /* FALLTHROUGH */
1603                 case (MDOC_Unfilled):
1604                         /* FALLTHROUGH */
1605                 case (MDOC_Literal):
1606                         type = nn->args->argv[i].arg;
1607                         break;
1608                 case (MDOC_Offset):
1609                         p->offset += a2offs(&nn->args->argv[i]);
1610                         break;
1611                 default:
1612                         break;
1613                 }
1614         }
1615
1616         /*
1617          * If -ragged or -filled are specified, the block does nothing
1618          * but change the indentation.  If -unfilled or -literal are
1619          * specified, text is printed exactly as entered in the display:
1620          * for macro lines, a newline is appended to the line.  Blank
1621          * lines are allowed.
1622          */
1623
1624         assert(type > -1);
1625         if (MDOC_Literal != type && MDOC_Unfilled != type)
1626                 return(1);
1627
1628         for (nn = n->child; nn; nn = nn->next) {
1629                 p->flags |= TERMP_NOSPACE;
1630                 print_mdoc_node(p, pair, m, nn);
1631                 if (NULL == nn->next)
1632                         continue;
1633                 if (nn->prev && nn->prev->line < nn->line)
1634                         term_flushln(p);
1635                 else if (NULL == nn->prev)
1636                         term_flushln(p);
1637         }
1638
1639         return(0);
1640 }
1641
1642
1643 /* ARGSUSED */
1644 static void
1645 termp_bd_post(DECL_ARGS)
1646 {
1647
1648         if (MDOC_BODY != n->type)
1649                 return;
1650         p->flags |= TERMP_NOSPACE;
1651         term_flushln(p);
1652 }
1653
1654
1655 /* ARGSUSED */
1656 static int
1657 termp_qq_pre(DECL_ARGS)
1658 {
1659
1660         if (MDOC_BODY != n->type)
1661                 return(1);
1662         term_word(p, "\"");
1663         p->flags |= TERMP_NOSPACE;
1664         return(1);
1665 }
1666
1667
1668 /* ARGSUSED */
1669 static void
1670 termp_qq_post(DECL_ARGS)
1671 {
1672
1673         if (MDOC_BODY != n->type)
1674                 return;
1675         p->flags |= TERMP_NOSPACE;
1676         term_word(p, "\"");
1677 }
1678
1679
1680 /* ARGSUSED */
1681 static void
1682 termp_bx_post(DECL_ARGS)
1683 {
1684
1685         if (n->child)
1686                 p->flags |= TERMP_NOSPACE;
1687         term_word(p, "BSD");
1688 }
1689
1690
1691 /* ARGSUSED */
1692 static int
1693 termp_xx_pre(DECL_ARGS)
1694 {
1695         const char      *pp;
1696
1697         pp = NULL;
1698         switch (n->tok) {
1699         case (MDOC_Bsx):
1700                 pp = "BSDI BSD/OS";
1701                 break;
1702         case (MDOC_Dx):
1703                 pp = "DragonFly";
1704                 break;
1705         case (MDOC_Fx):
1706                 pp = "FreeBSD";
1707                 break;
1708         case (MDOC_Nx):
1709                 pp = "NetBSD";
1710                 break;
1711         case (MDOC_Ox):
1712                 pp = "OpenBSD";
1713                 break;
1714         case (MDOC_Ux):
1715                 pp = "UNIX";
1716                 break;
1717         default:
1718                 break;
1719         }
1720
1721         assert(pp);
1722         term_word(p, pp);
1723         return(1);
1724 }
1725
1726
1727 /* ARGSUSED */
1728 static int
1729 termp_sq_pre(DECL_ARGS)
1730 {
1731
1732         if (MDOC_BODY != n->type)
1733                 return(1);
1734         term_word(p, "\\(oq");
1735         p->flags |= TERMP_NOSPACE;
1736         return(1);
1737 }
1738
1739
1740 /* ARGSUSED */
1741 static void
1742 termp_sq_post(DECL_ARGS)
1743 {
1744
1745         if (MDOC_BODY != n->type)
1746                 return;
1747         p->flags |= TERMP_NOSPACE;
1748         term_word(p, "\\(aq");
1749 }
1750
1751
1752 /* ARGSUSED */
1753 static int
1754 termp_pf_pre(DECL_ARGS)
1755 {
1756
1757         p->flags |= TERMP_IGNDELIM;
1758         return(1);
1759 }
1760
1761
1762 /* ARGSUSED */
1763 static void
1764 termp_pf_post(DECL_ARGS)
1765 {
1766
1767         p->flags &= ~TERMP_IGNDELIM;
1768         p->flags |= TERMP_NOSPACE;
1769 }
1770
1771
1772 /* ARGSUSED */
1773 static int
1774 termp_ss_pre(DECL_ARGS)
1775 {
1776
1777         switch (n->type) {
1778         case (MDOC_BLOCK):
1779                 term_newln(p);
1780                 if (n->prev)
1781                         term_vspace(p);
1782                 break;
1783         case (MDOC_HEAD):
1784                 term_fontpush(p, TERMFONT_BOLD);
1785                 p->offset = HALFINDENT;
1786                 break;
1787         default:
1788                 break;
1789         }
1790
1791         return(1);
1792 }
1793
1794
1795 /* ARGSUSED */
1796 static void
1797 termp_ss_post(DECL_ARGS)
1798 {
1799
1800         if (MDOC_HEAD == n->type)
1801                 term_newln(p);
1802 }
1803
1804
1805 /* ARGSUSED */
1806 static int
1807 termp_cd_pre(DECL_ARGS)
1808 {
1809
1810         term_fontpush(p, TERMFONT_BOLD);
1811         term_newln(p);
1812         return(1);
1813 }
1814
1815
1816 /* ARGSUSED */
1817 static int
1818 termp_in_pre(DECL_ARGS)
1819 {
1820
1821         term_fontpush(p, TERMFONT_BOLD);
1822         if (SEC_SYNOPSIS == n->sec)
1823                 term_word(p, "#include");
1824
1825         term_word(p, "<");
1826         p->flags |= TERMP_NOSPACE;
1827         return(1);
1828 }
1829
1830
1831 /* ARGSUSED */
1832 static void
1833 termp_in_post(DECL_ARGS)
1834 {
1835
1836         term_fontpush(p, TERMFONT_BOLD);
1837         p->flags |= TERMP_NOSPACE;
1838         term_word(p, ">");
1839         term_fontpop(p);
1840
1841         if (SEC_SYNOPSIS != n->sec)
1842                 return;
1843
1844         term_newln(p);
1845         /*
1846          * XXX Not entirely correct.  If `.In foo bar' is specified in
1847          * the SYNOPSIS section, then it produces a single break after
1848          * the <foo>; mandoc asserts a vertical space.  Since this
1849          * construction is rarely used, I think it's fine.
1850          */
1851         if (n->next && MDOC_In != n->next->tok)
1852                 term_vspace(p);
1853 }
1854
1855
1856 /* ARGSUSED */
1857 static int
1858 termp_sp_pre(DECL_ARGS)
1859 {
1860         size_t           i, len;
1861
1862         switch (n->tok) {
1863         case (MDOC_sp):
1864                 len = n->child ? a2height(n->child) : 1;
1865                 break;
1866         case (MDOC_br):
1867                 len = 0;
1868                 break;
1869         default:
1870                 len = 1;
1871                 break;
1872         }
1873
1874         if (0 == len)
1875                 term_newln(p);
1876         for (i = 0; i < len; i++)
1877                 term_vspace(p);
1878
1879         return(0);
1880 }
1881
1882
1883 /* ARGSUSED */
1884 static int
1885 termp_brq_pre(DECL_ARGS)
1886 {
1887
1888         if (MDOC_BODY != n->type)
1889                 return(1);
1890         term_word(p, "\\(lC");
1891         p->flags |= TERMP_NOSPACE;
1892         return(1);
1893 }
1894
1895
1896 /* ARGSUSED */
1897 static void
1898 termp_brq_post(DECL_ARGS)
1899 {
1900
1901         if (MDOC_BODY != n->type)
1902                 return;
1903         p->flags |= TERMP_NOSPACE;
1904         term_word(p, "\\(rC");
1905 }
1906
1907
1908 /* ARGSUSED */
1909 static int
1910 termp_bq_pre(DECL_ARGS)
1911 {
1912
1913         if (MDOC_BODY != n->type)
1914                 return(1);
1915         term_word(p, "\\(lB");
1916         p->flags |= TERMP_NOSPACE;
1917         return(1);
1918 }
1919
1920
1921 /* ARGSUSED */
1922 static void
1923 termp_bq_post(DECL_ARGS)
1924 {
1925
1926         if (MDOC_BODY != n->type)
1927                 return;
1928         p->flags |= TERMP_NOSPACE;
1929         term_word(p, "\\(rB");
1930 }
1931
1932
1933 /* ARGSUSED */
1934 static int
1935 termp_pq_pre(DECL_ARGS)
1936 {
1937
1938         if (MDOC_BODY != n->type)
1939                 return(1);
1940         term_word(p, "\\&(");
1941         p->flags |= TERMP_NOSPACE;
1942         return(1);
1943 }
1944
1945
1946 /* ARGSUSED */
1947 static void
1948 termp_pq_post(DECL_ARGS)
1949 {
1950
1951         if (MDOC_BODY != n->type)
1952                 return;
1953         term_word(p, ")");
1954 }
1955
1956
1957 /* ARGSUSED */
1958 static int
1959 termp_fo_pre(DECL_ARGS)
1960 {
1961         const struct mdoc_node *nn;
1962
1963         if (MDOC_BODY == n->type) {
1964                 p->flags |= TERMP_NOSPACE;
1965                 term_word(p, "(");
1966                 p->flags |= TERMP_NOSPACE;
1967                 return(1);
1968         } else if (MDOC_HEAD != n->type)
1969                 return(1);
1970
1971         term_fontpush(p, TERMFONT_BOLD);
1972         for (nn = n->child; nn; nn = nn->next) {
1973                 assert(MDOC_TEXT == nn->type);
1974                 term_word(p, nn->string);
1975         }
1976         term_fontpop(p);
1977
1978         return(0);
1979 }
1980
1981
1982 /* ARGSUSED */
1983 static void
1984 termp_fo_post(DECL_ARGS)
1985 {
1986
1987         if (MDOC_BODY != n->type)
1988                 return;
1989         p->flags |= TERMP_NOSPACE;
1990         term_word(p, ")");
1991         p->flags |= TERMP_NOSPACE;
1992         term_word(p, ";");
1993         term_newln(p);
1994 }
1995
1996
1997 /* ARGSUSED */
1998 static int
1999 termp_bf_pre(DECL_ARGS)
2000 {
2001         const struct mdoc_node  *nn;
2002
2003         if (MDOC_HEAD == n->type)
2004                 return(0);
2005         else if (MDOC_BLOCK != n->type)
2006                 return(1);
2007
2008         if (NULL == (nn = n->head->child)) {
2009                 if (arg_hasattr(MDOC_Emphasis, n))
2010                         term_fontpush(p, TERMFONT_UNDER);
2011                 else if (arg_hasattr(MDOC_Symbolic, n))
2012                         term_fontpush(p, TERMFONT_BOLD);
2013                 else
2014                         term_fontpush(p, TERMFONT_NONE);
2015
2016                 return(1);
2017         }
2018
2019         assert(MDOC_TEXT == nn->type);
2020         if (0 == strcmp("Em", nn->string))
2021                 term_fontpush(p, TERMFONT_UNDER);
2022         else if (0 == strcmp("Sy", nn->string))
2023                 term_fontpush(p, TERMFONT_BOLD);
2024         else
2025                 term_fontpush(p, TERMFONT_NONE);
2026
2027         return(1);
2028 }
2029
2030
2031 /* ARGSUSED */
2032 static int
2033 termp_sm_pre(DECL_ARGS)
2034 {
2035
2036         assert(n->child && MDOC_TEXT == n->child->type);
2037         if (0 == strcmp("on", n->child->string))
2038                 p->flags &= ~TERMP_NONOSPACE;
2039         else
2040                 p->flags |= TERMP_NONOSPACE;
2041
2042         return(0);
2043 }
2044
2045
2046 /* ARGSUSED */
2047 static int
2048 termp_ap_pre(DECL_ARGS)
2049 {
2050
2051         p->flags |= TERMP_NOSPACE;
2052         term_word(p, "\\(aq");
2053         p->flags |= TERMP_NOSPACE;
2054         return(1);
2055 }
2056
2057
2058 /* ARGSUSED */
2059 static void
2060 termp____post(DECL_ARGS)
2061 {
2062
2063         /* TODO: %U. */
2064
2065         p->flags |= TERMP_NOSPACE;
2066         switch (n->tok) {
2067         case (MDOC__T):
2068                 term_word(p, "\\(rq");
2069                 p->flags |= TERMP_NOSPACE;
2070                 break;
2071         default:
2072                 break;
2073         }
2074         term_word(p, n->next ? "," : ".");
2075 }
2076
2077
2078 /* ARGSUSED */
2079 static int
2080 termp_li_pre(DECL_ARGS)
2081 {
2082
2083         term_fontpush(p, TERMFONT_NONE);
2084         return(1);
2085 }
2086
2087
2088 /* ARGSUSED */
2089 static int
2090 termp_lk_pre(DECL_ARGS)
2091 {
2092         const struct mdoc_node *nn;
2093
2094         term_fontpush(p, TERMFONT_UNDER);
2095         nn = n->child;
2096
2097         if (NULL == nn->next)
2098                 return(1);
2099
2100         term_word(p, nn->string);
2101         term_fontpop(p);
2102
2103         p->flags |= TERMP_NOSPACE;
2104         term_word(p, ":");
2105
2106         term_fontpush(p, TERMFONT_BOLD);
2107         for (nn = nn->next; nn; nn = nn->next)
2108                 term_word(p, nn->string);
2109         term_fontpop(p);
2110
2111         return(0);
2112 }
2113
2114
2115 /* ARGSUSED */
2116 static int
2117 termp_under_pre(DECL_ARGS)
2118 {
2119
2120         term_fontpush(p, TERMFONT_UNDER);
2121         return(1);
2122 }
2123
2124
2125 /* ARGSUSED */
2126 static int
2127 termp__t_pre(DECL_ARGS)
2128 {
2129
2130         term_word(p, "\\(lq");
2131         p->flags |= TERMP_NOSPACE;
2132         return(1);
2133 }