1 /* $Id: mdoc_term.c,v 1.110 2010/01/30 08:42:21 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
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.
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.
17 #include <sys/types.h>
35 struct termpair *ppair;
39 #define DECL_ARGS struct termp *p, \
40 struct termpair *pair, \
41 const struct mdoc_meta *m, \
42 const struct mdoc_node *n
45 int (*pre)(DECL_ARGS);
46 void (*post)(DECL_ARGS);
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 *);
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);
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);
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);
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 */
261 terminal_mdoc(void *arg, const struct mdoc *mdoc)
263 const struct mdoc_node *n;
264 const struct mdoc_meta *m;
267 p = (struct termp *)arg;
269 if (NULL == p->symtab)
271 case (TERMENC_ASCII):
272 p->symtab = chars_init(CHARS_ASCII);
282 print_mdoc_head(p, NULL, m, n);
284 print_mdoc_nodelist(p, NULL, m, n->child);
285 print_foot(p, NULL, m, n);
290 print_mdoc_nodelist(DECL_ARGS)
293 print_mdoc_node(p, pair, m, n);
295 print_mdoc_nodelist(p, pair, m, n->next);
301 print_mdoc_node(DECL_ARGS)
305 struct termpair npair;
306 size_t offset, rmargin;
310 rmargin = p->rmargin;
311 font = term_fontq(p);
313 memset(&npair, 0, sizeof(struct termpair));
316 if (MDOC_TEXT != n->type) {
317 if (termacts[n->tok].pre)
318 chld = (*termacts[n->tok].pre)(p, &npair, m, n);
320 term_word(p, n->string);
322 if (chld && n->child)
323 print_mdoc_nodelist(p, &npair, m, n->child);
325 term_fontpopq(p, font);
327 if (MDOC_TEXT != n->type)
328 if (termacts[n->tok].post)
329 (*termacts[n->tok].post)(p, &npair, m, n);
332 p->rmargin = rmargin;
338 print_foot(DECL_ARGS)
340 char buf[DATESIZ], os[BUFSIZ];
342 term_fontrepl(p, TERMFONT_NONE);
345 * Output the footer in new-groff style, that is, three columns
346 * with the middle being the manual date and flanking columns
347 * being the operating system:
352 time2a(m->date, buf, DATESIZ);
353 strlcpy(os, m->os, BUFSIZ);
358 p->rmargin = (p->maxrmargin - strlen(buf) + 1) / 2;
359 p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
364 p->offset = p->rmargin;
365 p->rmargin = p->maxrmargin - strlen(os);
366 p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
371 p->offset = p->rmargin;
372 p->rmargin = p->maxrmargin;
373 p->flags &= ~TERMP_NOBREAK;
374 p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
380 p->rmargin = p->maxrmargin;
387 print_mdoc_head(DECL_ARGS)
389 char buf[BUFSIZ], title[BUFSIZ];
391 p->rmargin = p->maxrmargin;
395 * The header is strange. It has three components, which are
396 * really two with the first duplicated. It goes like this:
398 * IDENTIFIER TITLE IDENTIFIER
400 * The IDENTIFIER is NAME(SECTION), which is the command-name
401 * (if given, or "unknown" if not) followed by the manual page
402 * section. These are given in `Dt'. The TITLE is a free-form
403 * string depending on the manual volume. If not specified, it
404 * switches on the manual section.
408 strlcpy(buf, m->vol, BUFSIZ);
411 strlcat(buf, " (", BUFSIZ);
412 strlcat(buf, m->arch, BUFSIZ);
413 strlcat(buf, ")", BUFSIZ);
416 snprintf(title, BUFSIZ, "%s(%d)", m->title, m->msec);
419 p->rmargin = (p->maxrmargin - strlen(buf) + 1) / 2;
420 p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
425 p->offset = p->rmargin;
426 p->rmargin = p->maxrmargin - strlen(title);
427 p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
432 p->offset = p->rmargin;
433 p->rmargin = p->maxrmargin;
434 p->flags &= ~TERMP_NOBREAK;
435 p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
441 p->rmargin = p->maxrmargin;
442 p->flags &= ~TERMP_NOSPACE;
447 a2height(const struct mdoc_node *n)
451 assert(MDOC_TEXT == n->type);
453 if ( ! a2roffsu(n->string, &su, SCALE_VS))
454 SCALE_VS_INIT(&su, strlen(n->string));
456 return(term_vspan(&su));
461 a2width(const struct mdoc_argv *arg, int pos)
465 assert(arg->value[pos]);
466 if ( ! a2roffsu(arg->value[pos], &su, SCALE_MAX))
467 SCALE_HS_INIT(&su, strlen(arg->value[pos]));
469 return(term_hspan(&su));
474 arg_listtype(const struct mdoc_node *n)
478 assert(MDOC_BLOCK == n->type);
480 len = (int)(n->args ? n->args->argc : 0);
482 for (i = 0; i < len; i++)
483 switch (n->args->argv[i].arg) {
505 return(n->args->argv[i].arg);
515 a2offs(const struct mdoc_argv *arg)
519 if ('\0' == arg->value[0][0])
521 else if (0 == strcmp(arg->value[0], "left"))
523 else if (0 == strcmp(arg->value[0], "indent"))
525 else if (0 == strcmp(arg->value[0], "indent-two"))
526 return((INDENT + 1) * 2);
527 else if ( ! a2roffsu(arg->value[0], &su, SCALE_MAX))
528 SCALE_HS_INIT(&su, strlen(arg->value[0]));
530 return(term_hspan(&su));
535 * Return 1 if an argument has a particular argument value or 0 if it
536 * does not. See arg_getattr().
539 arg_hasattr(int arg, const struct mdoc_node *n)
542 return(-1 != arg_getattr(arg, n));
547 * Get the index of an argument in a node's argument list or -1 if it
548 * does not exist. See arg_getattrs().
551 arg_getattr(int v, const struct mdoc_node *n)
555 return(arg_getattrs(&v, &val, 1, n) ? val : -1);
560 * Walk through the argument list for a node and fill an array "vals"
561 * with the positions of the argument structures listed in "keys".
562 * Return the number of elements that were written into "vals", which
566 arg_getattrs(const int *keys, int *vals,
567 size_t sz, const struct mdoc_node *n)
574 for (k = i = 0; i < (int)n->args->argc; i++)
575 for (j = 0; j < (int)sz; j++)
576 if (n->args->argv[i].arg == keys[j]) {
585 * Determine how much space to print out before block elements of `It'
586 * (and thus `Bl') and `Bd'. And then go ahead and print that space,
590 print_bvspace(struct termp *p,
591 const struct mdoc_node *bl,
592 const struct mdoc_node *n)
594 const struct mdoc_node *nn;
597 if (arg_hasattr(MDOC_Compact, bl))
600 /* Do not vspace directly after Ss/Sh. */
602 for (nn = n; nn; nn = nn->parent) {
603 if (MDOC_BLOCK != nn->type)
605 if (MDOC_Ss == nn->tok)
607 if (MDOC_Sh == nn->tok)
609 if (NULL == nn->prev)
614 /* A `-column' does not assert vspace within the list. */
616 if (MDOC_Bl == bl->tok && arg_hasattr(MDOC_Column, bl))
617 if (n->prev && MDOC_It == n->prev->tok)
620 /* A `-diag' without body does not vspace. */
622 if (MDOC_Bl == bl->tok && arg_hasattr(MDOC_Diag, bl))
623 if (n->prev && MDOC_It == n->prev->tok) {
624 assert(n->prev->body);
625 if (NULL == n->prev->body->child)
635 termp_dq_pre(DECL_ARGS)
638 if (MDOC_BODY != n->type)
641 term_word(p, "\\(lq");
642 p->flags |= TERMP_NOSPACE;
649 termp_dq_post(DECL_ARGS)
652 if (MDOC_BODY != n->type)
655 p->flags |= TERMP_NOSPACE;
656 term_word(p, "\\(rq");
662 termp_it_pre(DECL_ARGS)
664 const struct mdoc_node *bl, *nn;
666 int i, type, keys[3], vals[3];
667 size_t width, offset, ncols, dcol;
669 if (MDOC_BLOCK == n->type) {
670 print_bvspace(p, n->parent->parent, n);
674 bl = n->parent->parent->parent;
676 /* Get list width, offset, and list type from argument list. */
678 keys[0] = MDOC_Width;
679 keys[1] = MDOC_Offset;
680 keys[2] = MDOC_Column;
682 vals[0] = vals[1] = vals[2] = -1;
684 arg_getattrs(keys, vals, 3, bl);
686 type = arg_listtype(bl);
690 * First calculate width and offset. This is pretty easy unless
691 * we're a -column list, in which case all prior columns must
698 offset = a2offs(&bl->args->argv[vals[1]]);
702 if (MDOC_BODY == n->type)
705 * Imitate groff's column handling:
706 * - For each earlier column, add its width.
707 * - For less than 5 columns, add four more blanks per
709 * - For exactly 5 columns, add three more blank per
711 * - For more than 5 columns, add only one column.
713 ncols = bl->args->argv[vals[2]].sz;
715 dcol = ncols < 5 ? 4 : ncols == 5 ? 3 : 1;
717 for (i = 0, nn = n->prev;
718 nn && i < (int)ncols;
720 offset += dcol + a2width
721 (&bl->args->argv[vals[2]], i);
725 * When exceeding the declared number of columns, leave
726 * the remaining widths at 0. This will later be
727 * adjusted to the default width of 10, or, for the last
728 * column, stretched to the right margin.
734 * Use the declared column widths, extended as explained
735 * in the preceding paragraph.
737 width = a2width(&bl->args->argv[vals[2]], i) + dcol;
744 * Note: buffer the width by 2, which is groff's magic
745 * number for buffering single arguments. See the above
746 * handling for column for how this changes.
748 width = a2width(&bl->args->argv[vals[0]], 0) + 2;
753 * List-type can override the width in the case of fixed-head
754 * values (bullet, dash/hyphen, enum). Tags need a non-zero
786 * Whitespace control. Inset bodies need an initial space,
787 * while diagonal bodies need two.
790 p->flags |= TERMP_NOSPACE;
794 if (MDOC_BODY == n->type)
795 term_word(p, "\\ \\ ");
798 if (MDOC_BODY == n->type)
805 p->flags |= TERMP_NOSPACE;
809 if (MDOC_HEAD == n->type)
810 term_fontpush(p, TERMFONT_BOLD);
817 * Pad and break control. This is the tricky part. These flags
818 * are documented in term_flushln() in term.c. Note that we're
819 * going to unset all of these flags in termp_it_post() when we
831 if (MDOC_HEAD == n->type)
832 p->flags |= TERMP_NOBREAK;
834 p->flags |= TERMP_NOLPAD;
837 if (MDOC_HEAD == n->type)
838 p->flags |= TERMP_NOBREAK;
840 p->flags |= TERMP_NOLPAD;
842 if (MDOC_HEAD != n->type)
846 * This is ugly. If `-hang' is specified and the body
847 * is a `Bl' or `Bd', then we want basically to nullify
848 * the "overstep" effect in term_flushln() and treat
849 * this as a `-ohang' list instead.
851 if (n->next->child &&
852 (MDOC_Bl == n->next->child->tok ||
853 MDOC_Bd == n->next->child->tok)) {
854 p->flags &= ~TERMP_NOBREAK;
855 p->flags &= ~TERMP_NOLPAD;
857 p->flags |= TERMP_HANG;
860 if (MDOC_HEAD == n->type)
861 p->flags |= TERMP_NOBREAK | TERMP_TWOSPACE;
863 p->flags |= TERMP_NOLPAD;
865 if (MDOC_HEAD != n->type)
867 if (NULL == n->next || NULL == n->next->child)
868 p->flags |= TERMP_DANGLE;
871 if (MDOC_HEAD == n->type) {
873 if (MDOC_BODY == n->next->type)
874 p->flags &= ~TERMP_NOBREAK;
876 p->flags |= TERMP_NOBREAK;
878 p->flags |= TERMP_NOLPAD;
882 if (MDOC_HEAD == n->type)
883 p->flags |= TERMP_NOBREAK;
890 * Margin control. Set-head-width lists have their right
891 * margins shortened. The body for these lists has the offset
892 * necessarily lengthened. Everybody gets the offset.
900 * Same stipulation as above, regarding `-hang'. We
901 * don't want to recalculate rmargin and offsets when
902 * using `Bd' or `Bl' within `-hang' overstep lists.
904 if (MDOC_HEAD == n->type && n->next->child &&
905 (MDOC_Bl == n->next->child->tok ||
906 MDOC_Bd == n->next->child->tok))
919 if (MDOC_HEAD == n->type)
920 p->rmargin = p->offset + width;
926 p->rmargin = p->offset + width;
928 * XXX - this behaviour is not documented: the
929 * right-most column is filled to the right margin.
931 if (MDOC_HEAD == n->type &&
932 MDOC_BODY == n->next->type &&
933 p->rmargin < p->maxrmargin)
934 p->rmargin = p->maxrmargin;
941 * The dash, hyphen, bullet and enum lists all have a special
942 * HEAD character (temporarily bold, in some cases).
945 if (MDOC_HEAD == n->type)
948 term_fontpush(p, TERMFONT_BOLD);
949 term_word(p, "\\[bu]");
955 term_fontpush(p, TERMFONT_BOLD);
956 term_word(p, "\\(hy");
960 (pair->ppair->ppair->count)++;
961 snprintf(buf, sizeof(buf), "%d.",
962 pair->ppair->ppair->count);
970 * If we're not going to process our children, indicate so here.
983 if (MDOC_HEAD == n->type)
987 if (MDOC_BODY == n->type)
1000 termp_it_post(DECL_ARGS)
1004 if (MDOC_BLOCK == n->type)
1007 type = arg_listtype(n->parent->parent->parent);
1016 if (MDOC_BODY == n->type)
1020 if (MDOC_HEAD == n->type)
1029 * Now that our output is flushed, we can reset our tags. Since
1030 * only `It' sets these flags, we're free to assume that nobody
1031 * has munged them in the meanwhile.
1034 p->flags &= ~TERMP_DANGLE;
1035 p->flags &= ~TERMP_NOBREAK;
1036 p->flags &= ~TERMP_TWOSPACE;
1037 p->flags &= ~TERMP_NOLPAD;
1038 p->flags &= ~TERMP_HANG;
1044 termp_nm_pre(DECL_ARGS)
1047 if (SEC_SYNOPSIS == n->sec)
1050 term_fontpush(p, TERMFONT_BOLD);
1052 if (NULL == n->child)
1053 term_word(p, m->name);
1060 termp_fl_pre(DECL_ARGS)
1063 term_fontpush(p, TERMFONT_BOLD);
1064 term_word(p, "\\-");
1066 /* A blank `Fl' should incur a subsequent space. */
1069 p->flags |= TERMP_NOSPACE;
1077 termp_an_pre(DECL_ARGS)
1080 if (NULL == n->child)
1084 * If not in the AUTHORS section, `An -split' will cause
1085 * newlines to occur before the author name. If in the AUTHORS
1086 * section, by default, the first `An' invocation is nosplit,
1087 * then all subsequent ones, regardless of whether interspersed
1088 * with other macros/text, are split. -split, in this case,
1089 * will override the condition of the implied first -nosplit.
1092 if (n->sec == SEC_AUTHORS) {
1093 if ( ! (TERMP_ANPREC & p->flags)) {
1094 if (TERMP_SPLIT & p->flags)
1098 if (TERMP_NOSPLIT & p->flags)
1104 if (TERMP_SPLIT & p->flags)
1113 termp_an_post(DECL_ARGS)
1117 if (SEC_AUTHORS == n->sec)
1118 p->flags |= TERMP_ANPREC;
1122 if (arg_getattr(MDOC_Split, n) > -1) {
1123 p->flags &= ~TERMP_NOSPLIT;
1124 p->flags |= TERMP_SPLIT;
1126 p->flags &= ~TERMP_SPLIT;
1127 p->flags |= TERMP_NOSPLIT;
1135 termp_ns_pre(DECL_ARGS)
1138 p->flags |= TERMP_NOSPACE;
1145 termp_rs_pre(DECL_ARGS)
1148 if (SEC_SEE_ALSO != n->sec)
1150 if (MDOC_BLOCK == n->type && n->prev)
1158 termp_rv_pre(DECL_ARGS)
1160 const struct mdoc_node *nn;
1163 term_word(p, "The");
1165 for (nn = n->child; nn; nn = nn->next) {
1166 term_fontpush(p, TERMFONT_BOLD);
1167 term_word(p, nn->string);
1169 p->flags |= TERMP_NOSPACE;
1170 if (nn->next && NULL == nn->next->next)
1171 term_word(p, "(), and");
1173 term_word(p, "(),");
1179 term_word(p, "functions return");
1181 term_word(p, "function returns");
1183 term_word(p, "the value 0 if successful; otherwise the value "
1184 "-1 is returned and the global variable");
1186 term_fontpush(p, TERMFONT_UNDER);
1187 term_word(p, "errno");
1190 term_word(p, "is set to indicate the error.");
1198 termp_ex_pre(DECL_ARGS)
1200 const struct mdoc_node *nn;
1202 term_word(p, "The");
1204 for (nn = n->child; nn; nn = nn->next) {
1205 term_fontpush(p, TERMFONT_BOLD);
1206 term_word(p, nn->string);
1208 p->flags |= TERMP_NOSPACE;
1209 if (nn->next && NULL == nn->next->next)
1210 term_word(p, ", and");
1214 p->flags &= ~TERMP_NOSPACE;
1218 term_word(p, "utilities exit");
1220 term_word(p, "utility exits");
1222 term_word(p, "0 on success, and >0 if an error occurs.");
1230 termp_nd_pre(DECL_ARGS)
1233 if (MDOC_BODY != n->type)
1236 #if defined(__OpenBSD__) || defined(__linux__)
1237 term_word(p, "\\(en");
1239 term_word(p, "\\(em");
1247 termp_bl_post(DECL_ARGS)
1250 if (MDOC_BLOCK == n->type)
1257 termp_op_post(DECL_ARGS)
1260 if (MDOC_BODY != n->type)
1262 p->flags |= TERMP_NOSPACE;
1263 term_word(p, "\\(rB");
1269 termp_xr_pre(DECL_ARGS)
1271 const struct mdoc_node *nn;
1273 assert(n->child && MDOC_TEXT == n->child->type);
1276 term_word(p, nn->string);
1277 if (NULL == (nn = nn->next))
1279 p->flags |= TERMP_NOSPACE;
1281 p->flags |= TERMP_NOSPACE;
1282 term_word(p, nn->string);
1283 p->flags |= TERMP_NOSPACE;
1291 termp_vt_pre(DECL_ARGS)
1294 if (MDOC_ELEM == n->type)
1295 return(termp_under_pre(p, pair, m, n));
1296 else if (MDOC_HEAD == n->type)
1298 else if (MDOC_BLOCK == n->type)
1301 return(termp_under_pre(p, pair, m, n));
1307 termp_vt_post(DECL_ARGS)
1310 if (MDOC_BLOCK != n->type)
1312 if (n->next && MDOC_Vt == n->next->tok)
1321 termp_bold_pre(DECL_ARGS)
1324 term_fontpush(p, TERMFONT_BOLD);
1331 termp_fd_post(DECL_ARGS)
1334 if (n->sec != SEC_SYNOPSIS)
1338 if (n->next && MDOC_Fd != n->next->tok)
1345 termp_sh_pre(DECL_ARGS)
1348 /* No vspace between consecutive `Sh' calls. */
1352 if (n->prev && MDOC_Sh == n->prev->tok)
1353 if (NULL == n->prev->body->child)
1358 term_fontpush(p, TERMFONT_BOLD);
1372 termp_sh_post(DECL_ARGS)
1391 termp_op_pre(DECL_ARGS)
1396 term_word(p, "\\(lB");
1397 p->flags |= TERMP_NOSPACE;
1408 termp_bt_pre(DECL_ARGS)
1411 term_word(p, "is currently in beta test.");
1418 termp_lb_post(DECL_ARGS)
1421 if (SEC_LIBRARY == n->sec)
1428 termp_ud_pre(DECL_ARGS)
1431 term_word(p, "currently under development.");
1438 termp_d1_pre(DECL_ARGS)
1441 if (MDOC_BLOCK != n->type)
1444 p->offset += (INDENT + 1);
1451 termp_d1_post(DECL_ARGS)
1454 if (MDOC_BLOCK != n->type)
1462 termp_aq_pre(DECL_ARGS)
1465 if (MDOC_BODY != n->type)
1467 term_word(p, "\\(la");
1468 p->flags |= TERMP_NOSPACE;
1475 termp_aq_post(DECL_ARGS)
1478 if (MDOC_BODY != n->type)
1480 p->flags |= TERMP_NOSPACE;
1481 term_word(p, "\\(ra");
1487 termp_ft_pre(DECL_ARGS)
1490 if (SEC_SYNOPSIS == n->sec)
1491 if (n->prev && MDOC_Fo == n->prev->tok)
1494 term_fontpush(p, TERMFONT_UNDER);
1501 termp_ft_post(DECL_ARGS)
1504 if (SEC_SYNOPSIS == n->sec)
1511 termp_fn_pre(DECL_ARGS)
1513 const struct mdoc_node *nn;
1515 term_fontpush(p, TERMFONT_BOLD);
1516 term_word(p, n->child->string);
1519 p->flags |= TERMP_NOSPACE;
1522 for (nn = n->child->next; nn; nn = nn->next) {
1523 term_fontpush(p, TERMFONT_UNDER);
1524 term_word(p, nn->string);
1533 if (SEC_SYNOPSIS == n->sec)
1542 termp_fn_post(DECL_ARGS)
1545 if (n->sec == SEC_SYNOPSIS && n->next)
1552 termp_fa_pre(DECL_ARGS)
1554 const struct mdoc_node *nn;
1556 if (n->parent->tok != MDOC_Fo) {
1557 term_fontpush(p, TERMFONT_UNDER);
1561 for (nn = n->child; nn; nn = nn->next) {
1562 term_fontpush(p, TERMFONT_UNDER);
1563 term_word(p, nn->string);
1570 if (n->child && n->next && n->next->tok == MDOC_Fa)
1579 termp_bd_pre(DECL_ARGS)
1582 const struct mdoc_node *nn;
1584 if (MDOC_BLOCK == n->type) {
1585 print_bvspace(p, n, n);
1587 } else if (MDOC_BODY != n->type)
1592 for (type = -1, i = 0; i < (int)nn->args->argc; i++) {
1593 switch (nn->args->argv[i].arg) {
1594 case (MDOC_Centred):
1600 case (MDOC_Unfilled):
1602 case (MDOC_Literal):
1603 type = nn->args->argv[i].arg;
1606 p->offset += a2offs(&nn->args->argv[i]);
1614 * If -ragged or -filled are specified, the block does nothing
1615 * but change the indentation. If -unfilled or -literal are
1616 * specified, text is printed exactly as entered in the display:
1617 * for macro lines, a newline is appended to the line. Blank
1618 * lines are allowed.
1622 if (MDOC_Literal != type && MDOC_Unfilled != type)
1625 for (nn = n->child; nn; nn = nn->next) {
1626 p->flags |= TERMP_NOSPACE;
1627 print_mdoc_node(p, pair, m, nn);
1628 if (NULL == nn->next)
1630 if (nn->prev && nn->prev->line < nn->line)
1632 else if (NULL == nn->prev)
1642 termp_bd_post(DECL_ARGS)
1645 if (MDOC_BODY != n->type)
1647 p->flags |= TERMP_NOSPACE;
1654 termp_qq_pre(DECL_ARGS)
1657 if (MDOC_BODY != n->type)
1660 p->flags |= TERMP_NOSPACE;
1667 termp_qq_post(DECL_ARGS)
1670 if (MDOC_BODY != n->type)
1672 p->flags |= TERMP_NOSPACE;
1679 termp_bx_post(DECL_ARGS)
1683 p->flags |= TERMP_NOSPACE;
1684 term_word(p, "BSD");
1690 termp_xx_pre(DECL_ARGS)
1726 termp_sq_pre(DECL_ARGS)
1729 if (MDOC_BODY != n->type)
1731 term_word(p, "\\(oq");
1732 p->flags |= TERMP_NOSPACE;
1739 termp_sq_post(DECL_ARGS)
1742 if (MDOC_BODY != n->type)
1744 p->flags |= TERMP_NOSPACE;
1745 term_word(p, "\\(aq");
1751 termp_pf_pre(DECL_ARGS)
1754 p->flags |= TERMP_IGNDELIM;
1761 termp_pf_post(DECL_ARGS)
1764 p->flags &= ~TERMP_IGNDELIM;
1765 p->flags |= TERMP_NOSPACE;
1771 termp_ss_pre(DECL_ARGS)
1781 term_fontpush(p, TERMFONT_BOLD);
1782 p->offset = HALFINDENT;
1794 termp_ss_post(DECL_ARGS)
1797 if (MDOC_HEAD == n->type)
1804 termp_cd_pre(DECL_ARGS)
1807 term_fontpush(p, TERMFONT_BOLD);
1815 termp_in_pre(DECL_ARGS)
1818 term_fontpush(p, TERMFONT_BOLD);
1819 if (SEC_SYNOPSIS == n->sec)
1820 term_word(p, "#include");
1823 p->flags |= TERMP_NOSPACE;
1830 termp_in_post(DECL_ARGS)
1833 term_fontpush(p, TERMFONT_BOLD);
1834 p->flags |= TERMP_NOSPACE;
1838 if (SEC_SYNOPSIS != n->sec)
1843 * XXX Not entirely correct. If `.In foo bar' is specified in
1844 * the SYNOPSIS section, then it produces a single break after
1845 * the <foo>; mandoc asserts a vertical space. Since this
1846 * construction is rarely used, I think it's fine.
1848 if (n->next && MDOC_In != n->next->tok)
1855 termp_sp_pre(DECL_ARGS)
1861 len = n->child ? a2height(n->child) : 1;
1873 for (i = 0; i < len; i++)
1882 termp_brq_pre(DECL_ARGS)
1885 if (MDOC_BODY != n->type)
1887 term_word(p, "\\(lC");
1888 p->flags |= TERMP_NOSPACE;
1895 termp_brq_post(DECL_ARGS)
1898 if (MDOC_BODY != n->type)
1900 p->flags |= TERMP_NOSPACE;
1901 term_word(p, "\\(rC");
1907 termp_bq_pre(DECL_ARGS)
1910 if (MDOC_BODY != n->type)
1912 term_word(p, "\\(lB");
1913 p->flags |= TERMP_NOSPACE;
1920 termp_bq_post(DECL_ARGS)
1923 if (MDOC_BODY != n->type)
1925 p->flags |= TERMP_NOSPACE;
1926 term_word(p, "\\(rB");
1932 termp_pq_pre(DECL_ARGS)
1935 if (MDOC_BODY != n->type)
1937 term_word(p, "\\&(");
1938 p->flags |= TERMP_NOSPACE;
1945 termp_pq_post(DECL_ARGS)
1948 if (MDOC_BODY != n->type)
1956 termp_fo_pre(DECL_ARGS)
1958 const struct mdoc_node *nn;
1960 if (MDOC_BODY == n->type) {
1961 p->flags |= TERMP_NOSPACE;
1963 p->flags |= TERMP_NOSPACE;
1965 } else if (MDOC_HEAD != n->type)
1968 term_fontpush(p, TERMFONT_BOLD);
1969 for (nn = n->child; nn; nn = nn->next) {
1970 assert(MDOC_TEXT == nn->type);
1971 term_word(p, nn->string);
1981 termp_fo_post(DECL_ARGS)
1984 if (MDOC_BODY != n->type)
1986 p->flags |= TERMP_NOSPACE;
1988 p->flags |= TERMP_NOSPACE;
1996 termp_bf_pre(DECL_ARGS)
1998 const struct mdoc_node *nn;
2000 if (MDOC_HEAD == n->type)
2002 else if (MDOC_BLOCK != n->type)
2005 if (NULL == (nn = n->head->child)) {
2006 if (arg_hasattr(MDOC_Emphasis, n))
2007 term_fontpush(p, TERMFONT_UNDER);
2008 else if (arg_hasattr(MDOC_Symbolic, n))
2009 term_fontpush(p, TERMFONT_BOLD);
2011 term_fontpush(p, TERMFONT_NONE);
2016 assert(MDOC_TEXT == nn->type);
2017 if (0 == strcmp("Em", nn->string))
2018 term_fontpush(p, TERMFONT_UNDER);
2019 else if (0 == strcmp("Sy", nn->string))
2020 term_fontpush(p, TERMFONT_BOLD);
2022 term_fontpush(p, TERMFONT_NONE);
2030 termp_sm_pre(DECL_ARGS)
2033 assert(n->child && MDOC_TEXT == n->child->type);
2034 if (0 == strcmp("on", n->child->string))
2035 p->flags &= ~TERMP_NONOSPACE;
2037 p->flags |= TERMP_NONOSPACE;
2045 termp_ap_pre(DECL_ARGS)
2048 p->flags |= TERMP_NOSPACE;
2049 term_word(p, "\\(aq");
2050 p->flags |= TERMP_NOSPACE;
2057 termp____post(DECL_ARGS)
2062 p->flags |= TERMP_NOSPACE;
2065 term_word(p, "\\(rq");
2066 p->flags |= TERMP_NOSPACE;
2071 term_word(p, n->next ? "," : ".");
2077 termp_li_pre(DECL_ARGS)
2080 term_fontpush(p, TERMFONT_NONE);
2087 termp_lk_pre(DECL_ARGS)
2089 const struct mdoc_node *nn;
2091 term_fontpush(p, TERMFONT_UNDER);
2094 if (NULL == nn->next)
2097 term_word(p, nn->string);
2100 p->flags |= TERMP_NOSPACE;
2103 term_fontpush(p, TERMFONT_BOLD);
2104 for (nn = nn->next; nn; nn = nn->next)
2105 term_word(p, nn->string);
2114 termp_under_pre(DECL_ARGS)
2117 term_fontpush(p, TERMFONT_UNDER);
2124 termp__t_pre(DECL_ARGS)
2127 term_word(p, "\\(lq");
2128 p->flags |= TERMP_NOSPACE;