1 /* $Id: mdoc_term.c,v 1.311 2015/02/17 20:37:17 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2012-2015 Ingo Schwarze <schwarze@openbsd.org>
5 * Copyright (c) 2013 Franco Fichtner <franco@lastsummer.de>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/types.h>
32 #include "mandoc_aux.h"
39 struct termpair *ppair;
43 #define DECL_ARGS struct termp *p, \
44 struct termpair *pair, \
45 const struct mdoc_meta *meta, \
49 int (*pre)(DECL_ARGS);
50 void (*post)(DECL_ARGS);
53 static int a2width(const struct termp *, const char *);
55 static void print_bvspace(struct termp *,
56 const struct mdoc_node *,
57 const struct mdoc_node *);
58 static void print_mdoc_node(DECL_ARGS);
59 static void print_mdoc_nodelist(DECL_ARGS);
60 static void print_mdoc_head(struct termp *, const void *);
61 static void print_mdoc_foot(struct termp *, const void *);
62 static void synopsis_pre(struct termp *,
63 const struct mdoc_node *);
65 static void termp____post(DECL_ARGS);
66 static void termp__t_post(DECL_ARGS);
67 static void termp_bd_post(DECL_ARGS);
68 static void termp_bk_post(DECL_ARGS);
69 static void termp_bl_post(DECL_ARGS);
70 static void termp_eo_post(DECL_ARGS);
71 static void termp_fd_post(DECL_ARGS);
72 static void termp_fo_post(DECL_ARGS);
73 static void termp_in_post(DECL_ARGS);
74 static void termp_it_post(DECL_ARGS);
75 static void termp_lb_post(DECL_ARGS);
76 static void termp_nm_post(DECL_ARGS);
77 static void termp_pf_post(DECL_ARGS);
78 static void termp_quote_post(DECL_ARGS);
79 static void termp_sh_post(DECL_ARGS);
80 static void termp_ss_post(DECL_ARGS);
82 static int termp__a_pre(DECL_ARGS);
83 static int termp__t_pre(DECL_ARGS);
84 static int termp_an_pre(DECL_ARGS);
85 static int termp_ap_pre(DECL_ARGS);
86 static int termp_bd_pre(DECL_ARGS);
87 static int termp_bf_pre(DECL_ARGS);
88 static int termp_bk_pre(DECL_ARGS);
89 static int termp_bl_pre(DECL_ARGS);
90 static int termp_bold_pre(DECL_ARGS);
91 static int termp_bt_pre(DECL_ARGS);
92 static int termp_bx_pre(DECL_ARGS);
93 static int termp_cd_pre(DECL_ARGS);
94 static int termp_d1_pre(DECL_ARGS);
95 static int termp_eo_pre(DECL_ARGS);
96 static int termp_ex_pre(DECL_ARGS);
97 static int termp_fa_pre(DECL_ARGS);
98 static int termp_fd_pre(DECL_ARGS);
99 static int termp_fl_pre(DECL_ARGS);
100 static int termp_fn_pre(DECL_ARGS);
101 static int termp_fo_pre(DECL_ARGS);
102 static int termp_ft_pre(DECL_ARGS);
103 static int termp_in_pre(DECL_ARGS);
104 static int termp_it_pre(DECL_ARGS);
105 static int termp_li_pre(DECL_ARGS);
106 static int termp_ll_pre(DECL_ARGS);
107 static int termp_lk_pre(DECL_ARGS);
108 static int termp_nd_pre(DECL_ARGS);
109 static int termp_nm_pre(DECL_ARGS);
110 static int termp_ns_pre(DECL_ARGS);
111 static int termp_quote_pre(DECL_ARGS);
112 static int termp_rs_pre(DECL_ARGS);
113 static int termp_rv_pre(DECL_ARGS);
114 static int termp_sh_pre(DECL_ARGS);
115 static int termp_skip_pre(DECL_ARGS);
116 static int termp_sm_pre(DECL_ARGS);
117 static int termp_sp_pre(DECL_ARGS);
118 static int termp_ss_pre(DECL_ARGS);
119 static int termp_under_pre(DECL_ARGS);
120 static int termp_ud_pre(DECL_ARGS);
121 static int termp_vt_pre(DECL_ARGS);
122 static int termp_xr_pre(DECL_ARGS);
123 static int termp_xx_pre(DECL_ARGS);
125 static const struct termact termacts[MDOC_MAX] = {
126 { termp_ap_pre, NULL }, /* Ap */
127 { NULL, NULL }, /* Dd */
128 { NULL, NULL }, /* Dt */
129 { NULL, NULL }, /* Os */
130 { termp_sh_pre, termp_sh_post }, /* Sh */
131 { termp_ss_pre, termp_ss_post }, /* Ss */
132 { termp_sp_pre, NULL }, /* Pp */
133 { termp_d1_pre, termp_bl_post }, /* D1 */
134 { termp_d1_pre, termp_bl_post }, /* Dl */
135 { termp_bd_pre, termp_bd_post }, /* Bd */
136 { NULL, NULL }, /* Ed */
137 { termp_bl_pre, termp_bl_post }, /* Bl */
138 { NULL, NULL }, /* El */
139 { termp_it_pre, termp_it_post }, /* It */
140 { termp_under_pre, NULL }, /* Ad */
141 { termp_an_pre, NULL }, /* An */
142 { termp_under_pre, NULL }, /* Ar */
143 { termp_cd_pre, NULL }, /* Cd */
144 { termp_bold_pre, NULL }, /* Cm */
145 { NULL, NULL }, /* Dv */
146 { NULL, NULL }, /* Er */
147 { NULL, NULL }, /* Ev */
148 { termp_ex_pre, NULL }, /* Ex */
149 { termp_fa_pre, NULL }, /* Fa */
150 { termp_fd_pre, termp_fd_post }, /* Fd */
151 { termp_fl_pre, NULL }, /* Fl */
152 { termp_fn_pre, NULL }, /* Fn */
153 { termp_ft_pre, NULL }, /* Ft */
154 { termp_bold_pre, NULL }, /* Ic */
155 { termp_in_pre, termp_in_post }, /* In */
156 { termp_li_pre, NULL }, /* Li */
157 { termp_nd_pre, NULL }, /* Nd */
158 { termp_nm_pre, termp_nm_post }, /* Nm */
159 { termp_quote_pre, termp_quote_post }, /* Op */
160 { termp_ft_pre, NULL }, /* Ot */
161 { termp_under_pre, NULL }, /* Pa */
162 { termp_rv_pre, NULL }, /* Rv */
163 { NULL, NULL }, /* St */
164 { termp_under_pre, NULL }, /* Va */
165 { termp_vt_pre, NULL }, /* Vt */
166 { termp_xr_pre, NULL }, /* Xr */
167 { termp__a_pre, termp____post }, /* %A */
168 { termp_under_pre, termp____post }, /* %B */
169 { NULL, termp____post }, /* %D */
170 { termp_under_pre, termp____post }, /* %I */
171 { termp_under_pre, termp____post }, /* %J */
172 { NULL, termp____post }, /* %N */
173 { NULL, termp____post }, /* %O */
174 { NULL, termp____post }, /* %P */
175 { NULL, termp____post }, /* %R */
176 { termp__t_pre, termp__t_post }, /* %T */
177 { NULL, termp____post }, /* %V */
178 { NULL, NULL }, /* Ac */
179 { termp_quote_pre, termp_quote_post }, /* Ao */
180 { termp_quote_pre, termp_quote_post }, /* Aq */
181 { NULL, NULL }, /* At */
182 { NULL, NULL }, /* Bc */
183 { termp_bf_pre, NULL }, /* Bf */
184 { termp_quote_pre, termp_quote_post }, /* Bo */
185 { termp_quote_pre, termp_quote_post }, /* Bq */
186 { termp_xx_pre, NULL }, /* Bsx */
187 { termp_bx_pre, NULL }, /* Bx */
188 { termp_skip_pre, NULL }, /* Db */
189 { NULL, NULL }, /* Dc */
190 { termp_quote_pre, termp_quote_post }, /* Do */
191 { termp_quote_pre, termp_quote_post }, /* Dq */
192 { NULL, NULL }, /* Ec */ /* FIXME: no space */
193 { NULL, NULL }, /* Ef */
194 { termp_under_pre, NULL }, /* Em */
195 { termp_eo_pre, termp_eo_post }, /* Eo */
196 { termp_xx_pre, NULL }, /* Fx */
197 { termp_bold_pre, NULL }, /* Ms */
198 { termp_li_pre, NULL }, /* No */
199 { termp_ns_pre, NULL }, /* Ns */
200 { termp_xx_pre, NULL }, /* Nx */
201 { termp_xx_pre, NULL }, /* Ox */
202 { NULL, NULL }, /* Pc */
203 { NULL, termp_pf_post }, /* Pf */
204 { termp_quote_pre, termp_quote_post }, /* Po */
205 { termp_quote_pre, termp_quote_post }, /* Pq */
206 { NULL, NULL }, /* Qc */
207 { termp_quote_pre, termp_quote_post }, /* Ql */
208 { termp_quote_pre, termp_quote_post }, /* Qo */
209 { termp_quote_pre, termp_quote_post }, /* Qq */
210 { NULL, NULL }, /* Re */
211 { termp_rs_pre, NULL }, /* Rs */
212 { NULL, NULL }, /* Sc */
213 { termp_quote_pre, termp_quote_post }, /* So */
214 { termp_quote_pre, termp_quote_post }, /* Sq */
215 { termp_sm_pre, NULL }, /* Sm */
216 { termp_under_pre, NULL }, /* Sx */
217 { termp_bold_pre, NULL }, /* Sy */
218 { NULL, NULL }, /* Tn */
219 { termp_xx_pre, NULL }, /* Ux */
220 { NULL, NULL }, /* Xc */
221 { NULL, NULL }, /* Xo */
222 { termp_fo_pre, termp_fo_post }, /* Fo */
223 { NULL, NULL }, /* Fc */
224 { termp_quote_pre, termp_quote_post }, /* Oo */
225 { NULL, NULL }, /* Oc */
226 { termp_bk_pre, termp_bk_post }, /* Bk */
227 { NULL, NULL }, /* Ek */
228 { termp_bt_pre, NULL }, /* Bt */
229 { NULL, NULL }, /* Hf */
230 { termp_under_pre, NULL }, /* Fr */
231 { termp_ud_pre, NULL }, /* Ud */
232 { NULL, termp_lb_post }, /* Lb */
233 { termp_sp_pre, NULL }, /* Lp */
234 { termp_lk_pre, NULL }, /* Lk */
235 { termp_under_pre, NULL }, /* Mt */
236 { termp_quote_pre, termp_quote_post }, /* Brq */
237 { termp_quote_pre, termp_quote_post }, /* Bro */
238 { NULL, NULL }, /* Brc */
239 { NULL, termp____post }, /* %C */
240 { termp_skip_pre, NULL }, /* Es */
241 { termp_quote_pre, termp_quote_post }, /* En */
242 { termp_xx_pre, NULL }, /* Dx */
243 { NULL, termp____post }, /* %Q */
244 { termp_sp_pre, NULL }, /* br */
245 { termp_sp_pre, NULL }, /* sp */
246 { NULL, termp____post }, /* %U */
247 { NULL, NULL }, /* Ta */
248 { termp_ll_pre, NULL }, /* ll */
253 terminal_mdoc(void *arg, const struct mdoc *mdoc)
255 const struct mdoc_meta *meta;
259 p = (struct termp *)arg;
262 p->rmargin = p->maxrmargin = p->defrmargin;
263 p->tabwidth = term_len(p, 5);
265 n = mdoc_node(mdoc)->child;
266 meta = mdoc_meta(mdoc);
268 if (p->synopsisonly) {
270 if (n->tok == MDOC_Sh && n->sec == SEC_SYNOPSIS) {
271 if (n->child->next->child != NULL)
272 print_mdoc_nodelist(p, NULL,
273 meta, n->child->next->child);
280 if (p->defindent == 0)
282 term_begin(p, print_mdoc_head, print_mdoc_foot, meta);
284 if (n->tok != MDOC_Sh)
286 print_mdoc_nodelist(p, NULL, meta, n);
293 print_mdoc_nodelist(DECL_ARGS)
297 print_mdoc_node(p, pair, meta, n);
303 print_mdoc_node(DECL_ARGS)
306 struct termpair npair;
307 size_t offset, rmargin;
311 rmargin = p->rmargin;
312 n->flags &= ~MDOC_ENDED;
313 n->prev_font = p->fonti;
315 memset(&npair, 0, sizeof(struct termpair));
319 * Keeps only work until the end of a line. If a keep was
320 * invoked in a prior line, revert it to PREKEEP.
323 if (p->flags & TERMP_KEEP && n->flags & MDOC_LINE) {
324 p->flags &= ~TERMP_KEEP;
325 p->flags |= TERMP_PREKEEP;
329 * After the keep flags have been set up, we may now
330 * produce output. Note that some pre-handlers do so.
335 if (' ' == *n->string && MDOC_LINE & n->flags)
337 if (MDOC_DELIMC & n->flags)
338 p->flags |= TERMP_NOSPACE;
339 term_word(p, n->string);
340 if (MDOC_DELIMO & n->flags)
341 p->flags |= TERMP_NOSPACE;
344 if ( ! (n->flags & MDOC_LINE))
345 p->flags |= TERMP_NOSPACE;
347 if (n->next != NULL && ! (n->next->flags & MDOC_LINE))
348 p->flags |= TERMP_NOSPACE;
351 term_tbl(p, n->span);
354 if (termacts[n->tok].pre &&
355 (n->end == ENDBODY_NOT || n->nchild))
356 chld = (*termacts[n->tok].pre)
357 (p, &npair, meta, n);
361 if (chld && n->child)
362 print_mdoc_nodelist(p, &npair, meta, n->child);
365 (ENDBODY_NOT == n->end ? n : n->body)->prev_font);
375 if ( ! termacts[n->tok].post || MDOC_ENDED & n->flags)
377 (void)(*termacts[n->tok].post)(p, &npair, meta, n);
380 * Explicit end tokens not only call the post
381 * handler, but also tell the respective block
382 * that it must not call the post handler again.
384 if (ENDBODY_NOT != n->end)
385 n->body->flags |= MDOC_ENDED;
388 * End of line terminating an implicit block
389 * while an explicit block is still open.
390 * Continue the explicit block without spacing.
392 if (ENDBODY_NOSPACE == n->end)
393 p->flags |= TERMP_NOSPACE;
397 if (MDOC_EOS & n->flags)
398 p->flags |= TERMP_SENTENCE;
400 if (MDOC_ll != n->tok) {
402 p->rmargin = rmargin;
407 print_mdoc_foot(struct termp *p, const void *arg)
409 const struct mdoc_meta *meta;
412 meta = (const struct mdoc_meta *)arg;
414 term_fontrepl(p, TERMFONT_NONE);
417 * Output the footer in new-groff style, that is, three columns
418 * with the middle being the manual date and flanking columns
419 * being the operating system:
427 sz = term_strlen(p, meta->date);
428 p->rmargin = p->maxrmargin > sz ?
429 (p->maxrmargin + term_len(p, 1) - sz) / 2 : 0;
431 p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
433 term_word(p, meta->os);
436 p->offset = p->rmargin;
437 sz = term_strlen(p, meta->os);
438 p->rmargin = p->maxrmargin > sz ? p->maxrmargin - sz : 0;
439 p->flags |= TERMP_NOSPACE;
441 term_word(p, meta->date);
444 p->offset = p->rmargin;
445 p->rmargin = p->maxrmargin;
447 p->flags &= ~TERMP_NOBREAK;
448 p->flags |= TERMP_NOSPACE;
450 term_word(p, meta->os);
454 p->rmargin = p->maxrmargin;
459 print_mdoc_head(struct termp *p, const void *arg)
461 const struct mdoc_meta *meta;
462 char *volume, *title;
463 size_t vollen, titlen;
465 meta = (const struct mdoc_meta *)arg;
468 * The header is strange. It has three components, which are
469 * really two with the first duplicated. It goes like this:
471 * IDENTIFIER TITLE IDENTIFIER
473 * The IDENTIFIER is NAME(SECTION), which is the command-name
474 * (if given, or "unknown" if not) followed by the manual page
475 * section. These are given in `Dt'. The TITLE is a free-form
476 * string depending on the manual volume. If not specified, it
477 * switches on the manual section.
481 if (NULL == meta->arch)
482 volume = mandoc_strdup(meta->vol);
484 mandoc_asprintf(&volume, "%s (%s)",
485 meta->vol, meta->arch);
486 vollen = term_strlen(p, volume);
488 if (NULL == meta->msec)
489 title = mandoc_strdup(meta->title);
491 mandoc_asprintf(&title, "%s(%s)",
492 meta->title, meta->msec);
493 titlen = term_strlen(p, title);
495 p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
498 p->rmargin = 2 * (titlen+1) + vollen < p->maxrmargin ?
499 (p->maxrmargin - vollen + term_len(p, 1)) / 2 :
500 vollen < p->maxrmargin ? p->maxrmargin - vollen : 0;
505 p->flags |= TERMP_NOSPACE;
506 p->offset = p->rmargin;
507 p->rmargin = p->offset + vollen + titlen < p->maxrmargin ?
508 p->maxrmargin - titlen : p->maxrmargin;
510 term_word(p, volume);
513 p->flags &= ~TERMP_NOBREAK;
515 if (p->rmargin + titlen <= p->maxrmargin) {
516 p->flags |= TERMP_NOSPACE;
517 p->offset = p->rmargin;
518 p->rmargin = p->maxrmargin;
523 p->flags &= ~TERMP_NOSPACE;
525 p->rmargin = p->maxrmargin;
531 a2width(const struct termp *p, const char *v)
535 if (a2roffsu(v, &su, SCALE_MAX) < 2) {
536 SCALE_HS_INIT(&su, term_strlen(p, v));
537 su.scale /= term_strlen(p, "0");
539 return(term_hspan(p, &su));
543 * Determine how much space to print out before block elements of `It'
544 * (and thus `Bl') and `Bd'. And then go ahead and print that space,
548 print_bvspace(struct termp *p,
549 const struct mdoc_node *bl,
550 const struct mdoc_node *n)
552 const struct mdoc_node *nn;
558 if (MDOC_Bd == bl->tok && bl->norm->Bd.comp)
560 if (MDOC_Bl == bl->tok && bl->norm->Bl.comp)
563 /* Do not vspace directly after Ss/Sh. */
566 while (nn->prev == NULL) {
569 if (nn->type == MDOC_ROOT)
571 } while (nn->type != MDOC_BLOCK);
572 if (nn->tok == MDOC_Sh || nn->tok == MDOC_Ss)
574 if (nn->tok == MDOC_It &&
575 nn->parent->parent->norm->Bl.type != LIST_item)
579 /* A `-column' does not assert vspace within the list. */
581 if (MDOC_Bl == bl->tok && LIST_column == bl->norm->Bl.type)
582 if (n->prev && MDOC_It == n->prev->tok)
585 /* A `-diag' without body does not vspace. */
587 if (MDOC_Bl == bl->tok && LIST_diag == bl->norm->Bl.type)
588 if (n->prev && MDOC_It == n->prev->tok) {
589 assert(n->prev->body);
590 if (NULL == n->prev->body->child)
599 termp_ll_pre(DECL_ARGS)
602 term_setwidth(p, n->nchild ? n->child->string : NULL);
607 termp_it_pre(DECL_ARGS)
610 const struct mdoc_node *bl, *nn;
612 int i, offset, width;
615 if (MDOC_BLOCK == n->type) {
616 print_bvspace(p, n->parent->parent, n);
620 bl = n->parent->parent->parent;
621 type = bl->norm->Bl.type;
624 * Defaults for specific list types.
635 width = term_len(p, 2);
638 width = term_len(p, 8);
643 width = term_len(p, 10);
652 * First calculate width and offset. This is pretty easy unless
653 * we're a -column list, in which case all prior columns must
657 if (bl->norm->Bl.offs != NULL) {
658 offset = a2width(p, bl->norm->Bl.offs);
659 if (offset < 0 && (size_t)(-offset) > p->offset)
661 else if (offset > SHRT_MAX)
667 if (MDOC_HEAD == n->type)
671 * Imitate groff's column handling:
672 * - For each earlier column, add its width.
673 * - For less than 5 columns, add four more blanks per
675 * - For exactly 5 columns, add three more blank per
677 * - For more than 5 columns, add only one column.
679 ncols = bl->norm->Bl.ncols;
680 dcol = ncols < 5 ? term_len(p, 4) :
681 ncols == 5 ? term_len(p, 3) : term_len(p, 1);
684 * Calculate the offset by applying all prior MDOC_BODY,
685 * so we stop at the MDOC_HEAD (NULL == nn->prev).
688 for (i = 0, nn = n->prev;
689 nn->prev && i < (int)ncols;
691 offset += dcol + a2width(p,
692 bl->norm->Bl.cols[i]);
695 * When exceeding the declared number of columns, leave
696 * the remaining widths at 0. This will later be
697 * adjusted to the default width of 10, or, for the last
698 * column, stretched to the right margin.
704 * Use the declared column widths, extended as explained
705 * in the preceding paragraph.
707 width = a2width(p, bl->norm->Bl.cols[i]) + dcol;
710 if (NULL == bl->norm->Bl.width)
714 * Note: buffer the width by 2, which is groff's magic
715 * number for buffering single arguments. See the above
716 * handling for column for how this changes.
718 width = a2width(p, bl->norm->Bl.width) + term_len(p, 2);
719 if (width < 0 && (size_t)(-width) > p->offset)
721 else if (width > SHRT_MAX)
727 * Whitespace control. Inset bodies need an initial space,
728 * while diagonal bodies need two.
731 p->flags |= TERMP_NOSPACE;
735 if (MDOC_BODY == n->type)
736 term_word(p, "\\ \\ ");
739 if (MDOC_BODY == n->type && n->parent->head->nchild)
746 p->flags |= TERMP_NOSPACE;
750 if (MDOC_HEAD == n->type)
751 term_fontpush(p, TERMFONT_BOLD);
758 * Pad and break control. This is the tricky part. These flags
759 * are documented in term_flushln() in term.c. Note that we're
760 * going to unset all of these flags in termp_it_post() when we
767 * Weird special case.
768 * Some very narrow lists actually hang.
776 if (width <= (int)term_len(p, 2))
777 p->flags |= TERMP_HANG;
778 if (MDOC_HEAD != n->type)
780 p->flags |= TERMP_NOBREAK;
784 if (MDOC_HEAD != n->type)
788 * This is ugly. If `-hang' is specified and the body
789 * is a `Bl' or `Bd', then we want basically to nullify
790 * the "overstep" effect in term_flushln() and treat
791 * this as a `-ohang' list instead.
793 if (NULL != n->next &&
794 NULL != n->next->child &&
795 (MDOC_Bl == n->next->child->tok ||
796 MDOC_Bd == n->next->child->tok))
799 p->flags |= TERMP_NOBREAK | TERMP_BRIND | TERMP_HANG;
803 if (MDOC_HEAD != n->type)
806 p->flags |= TERMP_NOBREAK | TERMP_BRIND;
809 if (NULL == n->next || NULL == n->next->child)
810 p->flags |= TERMP_DANGLE;
813 if (MDOC_HEAD == n->type)
816 if (NULL == n->next) {
817 p->flags &= ~TERMP_NOBREAK;
820 p->flags |= TERMP_NOBREAK;
826 if (MDOC_HEAD != n->type)
828 p->flags |= TERMP_NOBREAK | TERMP_BRIND;
836 * Margin control. Set-head-width lists have their right
837 * margins shortened. The body for these lists has the offset
838 * necessarily lengthened. Everybody gets the offset.
846 * Same stipulation as above, regarding `-hang'. We
847 * don't want to recalculate rmargin and offsets when
848 * using `Bd' or `Bl' within `-hang' overstep lists.
850 if (MDOC_HEAD == n->type &&
852 NULL != n->next->child &&
853 (MDOC_Bl == n->next->child->tok ||
854 MDOC_Bd == n->next->child->tok))
866 if (MDOC_HEAD == n->type)
867 p->rmargin = p->offset + width;
873 p->rmargin = p->offset + width;
875 * XXX - this behaviour is not documented: the
876 * right-most column is filled to the right margin.
878 if (MDOC_HEAD == n->type)
880 if (NULL == n->next && p->rmargin < p->maxrmargin)
881 p->rmargin = p->maxrmargin;
888 * The dash, hyphen, bullet and enum lists all have a special
889 * HEAD character (temporarily bold, in some cases).
892 if (MDOC_HEAD == n->type)
895 term_fontpush(p, TERMFONT_BOLD);
896 term_word(p, "\\[bu]");
902 term_fontpush(p, TERMFONT_BOLD);
903 term_word(p, "\\(hy");
907 (pair->ppair->ppair->count)++;
908 (void)snprintf(buf, sizeof(buf), "%d.",
909 pair->ppair->ppair->count);
917 * If we're not going to process our children, indicate so here.
930 if (MDOC_HEAD == n->type)
934 if (MDOC_HEAD == n->type)
945 termp_it_post(DECL_ARGS)
949 if (MDOC_BLOCK == n->type)
952 type = n->parent->parent->parent->norm->Bl.type;
960 if (MDOC_BODY == n->type)
964 if (MDOC_BODY == n->type)
973 * Now that our output is flushed, we can reset our tags. Since
974 * only `It' sets these flags, we're free to assume that nobody
975 * has munged them in the meanwhile.
978 p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND |
979 TERMP_DANGLE | TERMP_HANG);
984 termp_nm_pre(DECL_ARGS)
988 if (MDOC_BLOCK == n->type) {
989 p->flags |= TERMP_PREKEEP;
993 if (MDOC_BODY == n->type) {
994 if (NULL == n->child)
996 p->flags |= TERMP_NOSPACE;
998 if (n->prev->child != NULL)
999 cp = n->prev->child->string;
1003 p->offset += term_len(p, 6);
1005 p->offset += term_len(p, 1) + term_strlen(p, cp);
1009 if (NULL == n->child && NULL == meta->name)
1012 if (MDOC_HEAD == n->type)
1013 synopsis_pre(p, n->parent);
1015 if (MDOC_HEAD == n->type &&
1016 NULL != n->next && NULL != n->next->child) {
1017 p->flags |= TERMP_NOSPACE | TERMP_NOBREAK | TERMP_BRIND;
1019 p->rmargin = p->offset + term_len(p, 1);
1020 if (NULL == n->child) {
1021 p->rmargin += term_strlen(p, meta->name);
1022 } else if (MDOC_TEXT == n->child->type) {
1023 p->rmargin += term_strlen(p, n->child->string);
1025 p->flags |= TERMP_HANG;
1027 p->rmargin += term_len(p, 5);
1028 p->flags |= TERMP_HANG;
1032 term_fontpush(p, TERMFONT_BOLD);
1033 if (NULL == n->child)
1034 term_word(p, meta->name);
1039 termp_nm_post(DECL_ARGS)
1042 if (MDOC_BLOCK == n->type) {
1043 p->flags &= ~(TERMP_KEEP | TERMP_PREKEEP);
1044 } else if (MDOC_HEAD == n->type &&
1045 NULL != n->next && NULL != n->next->child) {
1047 p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND | TERMP_HANG);
1049 } else if (MDOC_BODY == n->type && n->child)
1054 termp_fl_pre(DECL_ARGS)
1057 term_fontpush(p, TERMFONT_BOLD);
1058 term_word(p, "\\-");
1060 if ( ! (n->nchild == 0 &&
1062 n->next->type == MDOC_TEXT ||
1063 n->next->flags & MDOC_LINE)))
1064 p->flags |= TERMP_NOSPACE;
1070 termp__a_pre(DECL_ARGS)
1073 if (n->prev && MDOC__A == n->prev->tok)
1074 if (NULL == n->next || MDOC__A != n->next->tok)
1075 term_word(p, "and");
1081 termp_an_pre(DECL_ARGS)
1084 if (n->norm->An.auth == AUTH_split) {
1085 p->flags &= ~TERMP_NOSPLIT;
1086 p->flags |= TERMP_SPLIT;
1089 if (n->norm->An.auth == AUTH_nosplit) {
1090 p->flags &= ~TERMP_SPLIT;
1091 p->flags |= TERMP_NOSPLIT;
1095 if (p->flags & TERMP_SPLIT)
1098 if (n->sec == SEC_AUTHORS && ! (p->flags & TERMP_NOSPLIT))
1099 p->flags |= TERMP_SPLIT;
1105 termp_ns_pre(DECL_ARGS)
1108 if ( ! (MDOC_LINE & n->flags))
1109 p->flags |= TERMP_NOSPACE;
1114 termp_rs_pre(DECL_ARGS)
1117 if (SEC_SEE_ALSO != n->sec)
1119 if (MDOC_BLOCK == n->type && n->prev)
1125 termp_rv_pre(DECL_ARGS)
1133 term_word(p, "The");
1135 for (n = n->child; n; n = n->next) {
1136 term_fontpush(p, TERMFONT_BOLD);
1137 term_word(p, n->string);
1140 p->flags |= TERMP_NOSPACE;
1143 if (n->next == NULL)
1147 p->flags |= TERMP_NOSPACE;
1150 if (n->next->next == NULL)
1151 term_word(p, "and");
1155 term_word(p, "functions return");
1157 term_word(p, "function returns");
1159 term_word(p, "the value\\~0 if successful;");
1161 term_word(p, "Upon successful completion,"
1162 " the value\\~0 is returned;");
1164 term_word(p, "otherwise the value\\~\\-1 is returned"
1165 " and the global variable");
1167 term_fontpush(p, TERMFONT_UNDER);
1168 term_word(p, "errno");
1171 term_word(p, "is set to indicate the error.");
1172 p->flags |= TERMP_SENTENCE;
1178 termp_ex_pre(DECL_ARGS)
1183 term_word(p, "The");
1186 for (n = n->child; n; n = n->next) {
1187 term_fontpush(p, TERMFONT_BOLD);
1188 term_word(p, n->string);
1191 if (nchild > 2 && n->next) {
1192 p->flags |= TERMP_NOSPACE;
1196 if (n->next && NULL == n->next->next)
1197 term_word(p, "and");
1201 term_word(p, "utilities exit\\~0");
1203 term_word(p, "utility exits\\~0");
1205 term_word(p, "on success, and\\~>0 if an error occurs.");
1207 p->flags |= TERMP_SENTENCE;
1212 termp_nd_pre(DECL_ARGS)
1215 if (n->type == MDOC_BODY)
1216 term_word(p, "\\(en");
1221 termp_bl_pre(DECL_ARGS)
1224 return(MDOC_HEAD != n->type);
1228 termp_bl_post(DECL_ARGS)
1231 if (MDOC_BLOCK == n->type)
1236 termp_xr_pre(DECL_ARGS)
1239 if (NULL == (n = n->child))
1242 assert(MDOC_TEXT == n->type);
1243 term_word(p, n->string);
1245 if (NULL == (n = n->next))
1248 p->flags |= TERMP_NOSPACE;
1250 p->flags |= TERMP_NOSPACE;
1252 assert(MDOC_TEXT == n->type);
1253 term_word(p, n->string);
1255 p->flags |= TERMP_NOSPACE;
1262 * This decides how to assert whitespace before any of the SYNOPSIS set
1263 * of macros (which, as in the case of Ft/Fo and Ft/Fn, may contain
1267 synopsis_pre(struct termp *p, const struct mdoc_node *n)
1270 * Obviously, if we're not in a SYNOPSIS or no prior macros
1271 * exist, do nothing.
1273 if (NULL == n->prev || ! (MDOC_SYNPRETTY & n->flags))
1277 * If we're the second in a pair of like elements, emit our
1278 * newline and return. UNLESS we're `Fo', `Fn', `Fn', in which
1279 * case we soldier on.
1281 if (n->prev->tok == n->tok &&
1282 MDOC_Ft != n->tok &&
1283 MDOC_Fo != n->tok &&
1284 MDOC_Fn != n->tok) {
1290 * If we're one of the SYNOPSIS set and non-like pair-wise after
1291 * another (or Fn/Fo, which we've let slip through) then assert
1292 * vertical space, else only newline and move on.
1294 switch (n->prev->tok) {
1307 if (MDOC_Fn != n->tok && MDOC_Fo != n->tok) {
1319 termp_vt_pre(DECL_ARGS)
1322 if (MDOC_ELEM == n->type) {
1324 return(termp_under_pre(p, pair, meta, n));
1325 } else if (MDOC_BLOCK == n->type) {
1328 } else if (MDOC_HEAD == n->type)
1331 return(termp_under_pre(p, pair, meta, n));
1335 termp_bold_pre(DECL_ARGS)
1338 term_fontpush(p, TERMFONT_BOLD);
1343 termp_fd_pre(DECL_ARGS)
1347 return(termp_bold_pre(p, pair, meta, n));
1351 termp_fd_post(DECL_ARGS)
1358 termp_sh_pre(DECL_ARGS)
1364 * Vertical space before sections, except
1365 * when the previous section was empty.
1367 if (n->prev == NULL ||
1368 MDOC_Sh != n->prev->tok ||
1369 (n->prev->body != NULL &&
1370 n->prev->body->child != NULL))
1374 term_fontpush(p, TERMFONT_BOLD);
1377 p->offset = term_len(p, p->defindent);
1378 if (SEC_AUTHORS == n->sec)
1379 p->flags &= ~(TERMP_SPLIT|TERMP_NOSPLIT);
1388 termp_sh_post(DECL_ARGS)
1405 termp_bt_pre(DECL_ARGS)
1408 term_word(p, "is currently in beta test.");
1409 p->flags |= TERMP_SENTENCE;
1414 termp_lb_post(DECL_ARGS)
1417 if (SEC_LIBRARY == n->sec && MDOC_LINE & n->flags)
1422 termp_ud_pre(DECL_ARGS)
1425 term_word(p, "currently under development.");
1426 p->flags |= TERMP_SENTENCE;
1431 termp_d1_pre(DECL_ARGS)
1434 if (MDOC_BLOCK != n->type)
1437 p->offset += term_len(p, p->defindent + 1);
1442 termp_ft_pre(DECL_ARGS)
1445 /* NB: MDOC_LINE does not effect this! */
1447 term_fontpush(p, TERMFONT_UNDER);
1452 termp_fn_pre(DECL_ARGS)
1457 pretty = MDOC_SYNPRETTY & n->flags;
1461 if (NULL == (n = n->child))
1465 rmargin = p->rmargin;
1466 p->rmargin = p->offset + term_len(p, 4);
1467 p->flags |= TERMP_NOBREAK | TERMP_BRIND | TERMP_HANG;
1470 assert(MDOC_TEXT == n->type);
1471 term_fontpush(p, TERMFONT_BOLD);
1472 term_word(p, n->string);
1477 p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND | TERMP_HANG);
1478 p->offset = p->rmargin;
1479 p->rmargin = rmargin;
1482 p->flags |= TERMP_NOSPACE;
1484 p->flags |= TERMP_NOSPACE;
1486 for (n = n->next; n; n = n->next) {
1487 assert(MDOC_TEXT == n->type);
1488 term_fontpush(p, TERMFONT_UNDER);
1490 p->flags |= TERMP_NBRWORD;
1491 term_word(p, n->string);
1495 p->flags |= TERMP_NOSPACE;
1500 p->flags |= TERMP_NOSPACE;
1504 p->flags |= TERMP_NOSPACE;
1513 termp_fa_pre(DECL_ARGS)
1515 const struct mdoc_node *nn;
1517 if (n->parent->tok != MDOC_Fo) {
1518 term_fontpush(p, TERMFONT_UNDER);
1522 for (nn = n->child; nn; nn = nn->next) {
1523 term_fontpush(p, TERMFONT_UNDER);
1524 p->flags |= TERMP_NBRWORD;
1525 term_word(p, nn->string);
1528 if (nn->next || (n->next && n->next->tok == MDOC_Fa)) {
1529 p->flags |= TERMP_NOSPACE;
1538 termp_bd_pre(DECL_ARGS)
1540 size_t tabwidth, lm, len, rm, rmax;
1541 struct mdoc_node *nn;
1544 if (MDOC_BLOCK == n->type) {
1545 print_bvspace(p, n, n);
1547 } else if (MDOC_HEAD == n->type)
1550 /* Handle the -offset argument. */
1552 if (n->norm->Bd.offs == NULL ||
1553 ! strcmp(n->norm->Bd.offs, "left"))
1555 else if ( ! strcmp(n->norm->Bd.offs, "indent"))
1556 p->offset += term_len(p, p->defindent + 1);
1557 else if ( ! strcmp(n->norm->Bd.offs, "indent-two"))
1558 p->offset += term_len(p, (p->defindent + 1) * 2);
1560 offset = a2width(p, n->norm->Bd.offs);
1561 if (offset < 0 && (size_t)(-offset) > p->offset)
1563 else if (offset < SHRT_MAX)
1564 p->offset += offset;
1568 * If -ragged or -filled are specified, the block does nothing
1569 * but change the indentation. If -unfilled or -literal are
1570 * specified, text is printed exactly as entered in the display:
1571 * for macro lines, a newline is appended to the line. Blank
1572 * lines are allowed.
1575 if (DISP_literal != n->norm->Bd.type &&
1576 DISP_unfilled != n->norm->Bd.type &&
1577 DISP_centered != n->norm->Bd.type)
1580 tabwidth = p->tabwidth;
1581 if (DISP_literal == n->norm->Bd.type)
1582 p->tabwidth = term_len(p, 8);
1586 rmax = p->maxrmargin;
1587 p->rmargin = p->maxrmargin = TERM_MAXMARGIN;
1589 for (nn = n->child; nn; nn = nn->next) {
1590 if (DISP_centered == n->norm->Bd.type) {
1591 if (MDOC_TEXT == nn->type) {
1592 len = term_strlen(p, nn->string);
1593 p->offset = len >= rm ? 0 :
1594 lm + len >= rm ? rm - len :
1595 (lm + rm - len) / 2;
1599 print_mdoc_node(p, pair, meta, nn);
1601 * If the printed node flushes its own line, then we
1602 * needn't do it here as well. This is hacky, but the
1603 * notion of selective eoln whitespace is pretty dumb
1604 * anyway, so don't sweat it.
1626 if (p->flags & TERMP_NONEWLINE ||
1627 (nn->next && ! (nn->next->flags & MDOC_LINE)))
1630 p->flags |= TERMP_NOSPACE;
1633 p->tabwidth = tabwidth;
1635 p->maxrmargin = rmax;
1640 termp_bd_post(DECL_ARGS)
1644 if (MDOC_BODY != n->type)
1648 rmax = p->maxrmargin;
1650 if (DISP_literal == n->norm->Bd.type ||
1651 DISP_unfilled == n->norm->Bd.type)
1652 p->rmargin = p->maxrmargin = TERM_MAXMARGIN;
1654 p->flags |= TERMP_NOSPACE;
1658 p->maxrmargin = rmax;
1662 termp_bx_pre(DECL_ARGS)
1665 if (NULL != (n = n->child)) {
1666 term_word(p, n->string);
1667 p->flags |= TERMP_NOSPACE;
1668 term_word(p, "BSD");
1670 term_word(p, "BSD");
1674 if (NULL != (n = n->next)) {
1675 p->flags |= TERMP_NOSPACE;
1677 p->flags |= TERMP_NOSPACE;
1678 term_word(p, n->string);
1685 termp_xx_pre(DECL_ARGS)
1718 p->flags |= TERMP_KEEP;
1719 term_word(p, n->child->string);
1726 termp_pf_post(DECL_ARGS)
1729 if ( ! (n->next == NULL || n->next->flags & MDOC_LINE))
1730 p->flags |= TERMP_NOSPACE;
1734 termp_ss_pre(DECL_ARGS)
1744 term_fontpush(p, TERMFONT_BOLD);
1745 p->offset = term_len(p, (p->defindent+1)/2);
1748 p->offset = term_len(p, p->defindent);
1758 termp_ss_post(DECL_ARGS)
1761 if (n->type == MDOC_HEAD || n->type == MDOC_BODY)
1766 termp_cd_pre(DECL_ARGS)
1770 term_fontpush(p, TERMFONT_BOLD);
1775 termp_in_pre(DECL_ARGS)
1780 if (MDOC_SYNPRETTY & n->flags && MDOC_LINE & n->flags) {
1781 term_fontpush(p, TERMFONT_BOLD);
1782 term_word(p, "#include");
1786 term_fontpush(p, TERMFONT_UNDER);
1789 p->flags |= TERMP_NOSPACE;
1794 termp_in_post(DECL_ARGS)
1797 if (MDOC_SYNPRETTY & n->flags)
1798 term_fontpush(p, TERMFONT_BOLD);
1800 p->flags |= TERMP_NOSPACE;
1803 if (MDOC_SYNPRETTY & n->flags)
1808 termp_sp_pre(DECL_ARGS)
1816 if ( ! a2roffsu(n->child->string, &su, SCALE_VS))
1818 len = term_vspan(p, &su);
1832 for (i = 0; i < len; i++)
1839 termp_skip_pre(DECL_ARGS)
1846 termp_quote_pre(DECL_ARGS)
1849 if (MDOC_BODY != n->type && MDOC_ELEM != n->type)
1856 term_word(p, n->nchild == 1 &&
1857 n->child->tok == MDOC_Mt ? "<" : "\\(la");
1876 term_word(p, "\\(Lq");
1879 if (NULL == n->norm->Es ||
1880 NULL == n->norm->Es->child)
1882 term_word(p, n->norm->Es->child->string);
1901 term_word(p, "\\(oq");
1908 p->flags |= TERMP_NOSPACE;
1913 termp_quote_post(DECL_ARGS)
1916 if (n->type != MDOC_BODY && n->type != MDOC_ELEM)
1919 p->flags |= TERMP_NOSPACE;
1925 term_word(p, n->nchild == 1 &&
1926 n->child->tok == MDOC_Mt ? ">" : "\\(ra");
1945 term_word(p, "\\(Rq");
1948 if (n->norm->Es == NULL ||
1949 n->norm->Es->child == NULL ||
1950 n->norm->Es->child->next == NULL)
1951 p->flags &= ~TERMP_NOSPACE;
1953 term_word(p, n->norm->Es->child->next->string);
1972 term_word(p, "\\(cq");
1981 termp_eo_pre(DECL_ARGS)
1984 if (n->type != MDOC_BODY)
1987 if (n->end == ENDBODY_NOT &&
1988 n->parent->head->child == NULL &&
1990 n->child->end != ENDBODY_NOT)
1991 term_word(p, "\\&");
1992 else if (n->end != ENDBODY_NOT ? n->child != NULL :
1993 n->parent->head->child != NULL && (n->child != NULL ||
1994 (n->parent->tail != NULL && n->parent->tail->child != NULL)))
1995 p->flags |= TERMP_NOSPACE;
2001 termp_eo_post(DECL_ARGS)
2005 if (n->type != MDOC_BODY)
2008 if (n->end != ENDBODY_NOT) {
2009 p->flags &= ~TERMP_NOSPACE;
2013 body = n->child != NULL || n->parent->head->child != NULL;
2014 tail = n->parent->tail != NULL && n->parent->tail->child != NULL;
2017 p->flags |= TERMP_NOSPACE;
2018 else if ( ! (body || tail))
2019 term_word(p, "\\&");
2021 p->flags &= ~TERMP_NOSPACE;
2025 termp_fo_pre(DECL_ARGS)
2030 pretty = MDOC_SYNPRETTY & n->flags;
2032 if (MDOC_BLOCK == n->type) {
2035 } else if (MDOC_BODY == n->type) {
2037 rmargin = p->rmargin;
2038 p->rmargin = p->offset + term_len(p, 4);
2039 p->flags |= TERMP_NOBREAK | TERMP_BRIND |
2042 p->flags |= TERMP_NOSPACE;
2044 p->flags |= TERMP_NOSPACE;
2047 p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND |
2049 p->offset = p->rmargin;
2050 p->rmargin = rmargin;
2055 if (NULL == n->child)
2058 /* XXX: we drop non-initial arguments as per groff. */
2060 assert(n->child->string);
2061 term_fontpush(p, TERMFONT_BOLD);
2062 term_word(p, n->child->string);
2067 termp_fo_post(DECL_ARGS)
2070 if (MDOC_BODY != n->type)
2073 p->flags |= TERMP_NOSPACE;
2076 if (MDOC_SYNPRETTY & n->flags) {
2077 p->flags |= TERMP_NOSPACE;
2084 termp_bf_pre(DECL_ARGS)
2087 if (MDOC_HEAD == n->type)
2089 else if (MDOC_BODY != n->type)
2092 if (FONT_Em == n->norm->Bf.font)
2093 term_fontpush(p, TERMFONT_UNDER);
2094 else if (FONT_Sy == n->norm->Bf.font)
2095 term_fontpush(p, TERMFONT_BOLD);
2097 term_fontpush(p, TERMFONT_NONE);
2103 termp_sm_pre(DECL_ARGS)
2106 if (NULL == n->child)
2107 p->flags ^= TERMP_NONOSPACE;
2108 else if (0 == strcmp("on", n->child->string))
2109 p->flags &= ~TERMP_NONOSPACE;
2111 p->flags |= TERMP_NONOSPACE;
2113 if (p->col && ! (TERMP_NONOSPACE & p->flags))
2114 p->flags &= ~TERMP_NOSPACE;
2120 termp_ap_pre(DECL_ARGS)
2123 p->flags |= TERMP_NOSPACE;
2125 p->flags |= TERMP_NOSPACE;
2130 termp____post(DECL_ARGS)
2134 * Handle lists of authors. In general, print each followed by
2135 * a comma. Don't print the comma if there are only two
2138 if (MDOC__A == n->tok && n->next && MDOC__A == n->next->tok)
2139 if (NULL == n->next->next || MDOC__A != n->next->next->tok)
2140 if (NULL == n->prev || MDOC__A != n->prev->tok)
2145 if (NULL == n->parent || MDOC_Rs != n->parent->tok)
2148 p->flags |= TERMP_NOSPACE;
2149 if (NULL == n->next) {
2151 p->flags |= TERMP_SENTENCE;
2157 termp_li_pre(DECL_ARGS)
2160 term_fontpush(p, TERMFONT_NONE);
2165 termp_lk_pre(DECL_ARGS)
2167 const struct mdoc_node *link, *descr;
2169 if (NULL == (link = n->child))
2172 if (NULL != (descr = link->next)) {
2173 term_fontpush(p, TERMFONT_UNDER);
2174 while (NULL != descr) {
2175 term_word(p, descr->string);
2176 descr = descr->next;
2178 p->flags |= TERMP_NOSPACE;
2183 term_fontpush(p, TERMFONT_BOLD);
2184 term_word(p, link->string);
2191 termp_bk_pre(DECL_ARGS)
2200 if (n->parent->args || 0 == n->prev->nchild)
2201 p->flags |= TERMP_PREKEEP;
2212 termp_bk_post(DECL_ARGS)
2215 if (MDOC_BODY == n->type)
2216 p->flags &= ~(TERMP_KEEP | TERMP_PREKEEP);
2220 termp__t_post(DECL_ARGS)
2224 * If we're in an `Rs' and there's a journal present, then quote
2225 * us instead of underlining us (for disambiguation).
2227 if (n->parent && MDOC_Rs == n->parent->tok &&
2228 n->parent->norm->Rs.quote_T)
2229 termp_quote_post(p, pair, meta, n);
2231 termp____post(p, pair, meta, n);
2235 termp__t_pre(DECL_ARGS)
2239 * If we're in an `Rs' and there's a journal present, then quote
2240 * us instead of underlining us (for disambiguation).
2242 if (n->parent && MDOC_Rs == n->parent->tok &&
2243 n->parent->norm->Rs.quote_T)
2244 return(termp_quote_pre(p, pair, meta, n));
2246 term_fontpush(p, TERMFONT_UNDER);
2251 termp_under_pre(DECL_ARGS)
2254 term_fontpush(p, TERMFONT_UNDER);