1 /* $Id: mdoc_term.c,v 1.380 2020/04/06 10:16:17 schwarze Exp $ */
3 * Copyright (c) 2010, 2012-2020 Ingo Schwarze <schwarze@openbsd.org>
4 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
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 AUTHORS DISCLAIM ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS 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.
19 * Plain text formatter for mdoc(7), used by mandoc(1)
20 * for ASCII, UTF-8, PostScript, and PDF output.
24 #include <sys/types.h>
34 #include "mandoc_aux.h"
43 struct termpair *ppair;
47 #define DECL_ARGS struct termp *p, \
48 struct termpair *pair, \
49 const struct roff_meta *meta, \
52 struct mdoc_term_act {
53 int (*pre)(DECL_ARGS);
54 void (*post)(DECL_ARGS);
57 static int a2width(const struct termp *, const char *);
59 static void print_bvspace(struct termp *,
60 struct roff_node *, struct roff_node *);
61 static void print_mdoc_node(DECL_ARGS);
62 static void print_mdoc_nodelist(DECL_ARGS);
63 static void print_mdoc_head(struct termp *, const struct roff_meta *);
64 static void print_mdoc_foot(struct termp *, const struct roff_meta *);
65 static void synopsis_pre(struct termp *, struct roff_node *);
67 static void termp____post(DECL_ARGS);
68 static void termp__t_post(DECL_ARGS);
69 static void termp_bd_post(DECL_ARGS);
70 static void termp_bk_post(DECL_ARGS);
71 static void termp_bl_post(DECL_ARGS);
72 static void termp_eo_post(DECL_ARGS);
73 static void termp_fd_post(DECL_ARGS);
74 static void termp_fo_post(DECL_ARGS);
75 static void termp_in_post(DECL_ARGS);
76 static void termp_it_post(DECL_ARGS);
77 static void termp_lb_post(DECL_ARGS);
78 static void termp_nm_post(DECL_ARGS);
79 static void termp_pf_post(DECL_ARGS);
80 static void termp_quote_post(DECL_ARGS);
81 static void termp_sh_post(DECL_ARGS);
82 static void termp_ss_post(DECL_ARGS);
83 static void termp_xx_post(DECL_ARGS);
85 static int termp__a_pre(DECL_ARGS);
86 static int termp__t_pre(DECL_ARGS);
87 static int termp_abort_pre(DECL_ARGS);
88 static int termp_an_pre(DECL_ARGS);
89 static int termp_ap_pre(DECL_ARGS);
90 static int termp_bd_pre(DECL_ARGS);
91 static int termp_bf_pre(DECL_ARGS);
92 static int termp_bk_pre(DECL_ARGS);
93 static int termp_bl_pre(DECL_ARGS);
94 static int termp_bold_pre(DECL_ARGS);
95 static int termp_d1_pre(DECL_ARGS);
96 static int termp_eo_pre(DECL_ARGS);
97 static int termp_ex_pre(DECL_ARGS);
98 static int termp_fa_pre(DECL_ARGS);
99 static int termp_fd_pre(DECL_ARGS);
100 static int termp_fl_pre(DECL_ARGS);
101 static int termp_fn_pre(DECL_ARGS);
102 static int termp_fo_pre(DECL_ARGS);
103 static int termp_ft_pre(DECL_ARGS);
104 static int termp_in_pre(DECL_ARGS);
105 static int termp_it_pre(DECL_ARGS);
106 static int termp_li_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_sh_pre(DECL_ARGS);
114 static int termp_skip_pre(DECL_ARGS);
115 static int termp_sm_pre(DECL_ARGS);
116 static int termp_pp_pre(DECL_ARGS);
117 static int termp_ss_pre(DECL_ARGS);
118 static int termp_under_pre(DECL_ARGS);
119 static int termp_vt_pre(DECL_ARGS);
120 static int termp_xr_pre(DECL_ARGS);
121 static int termp_xx_pre(DECL_ARGS);
123 static const struct mdoc_term_act mdoc_term_acts[MDOC_MAX - MDOC_Dd] = {
124 { NULL, NULL }, /* Dd */
125 { NULL, NULL }, /* Dt */
126 { NULL, NULL }, /* Os */
127 { termp_sh_pre, termp_sh_post }, /* Sh */
128 { termp_ss_pre, termp_ss_post }, /* Ss */
129 { termp_pp_pre, NULL }, /* Pp */
130 { termp_d1_pre, termp_bl_post }, /* D1 */
131 { termp_d1_pre, termp_bl_post }, /* Dl */
132 { termp_bd_pre, termp_bd_post }, /* Bd */
133 { NULL, NULL }, /* Ed */
134 { termp_bl_pre, termp_bl_post }, /* Bl */
135 { NULL, NULL }, /* El */
136 { termp_it_pre, termp_it_post }, /* It */
137 { termp_under_pre, NULL }, /* Ad */
138 { termp_an_pre, NULL }, /* An */
139 { termp_ap_pre, NULL }, /* Ap */
140 { termp_under_pre, NULL }, /* Ar */
141 { termp_fd_pre, NULL }, /* Cd */
142 { termp_bold_pre, NULL }, /* Cm */
143 { termp_li_pre, NULL }, /* Dv */
144 { NULL, NULL }, /* Er */
145 { NULL, NULL }, /* Ev */
146 { termp_ex_pre, NULL }, /* Ex */
147 { termp_fa_pre, NULL }, /* Fa */
148 { termp_fd_pre, termp_fd_post }, /* Fd */
149 { termp_fl_pre, NULL }, /* Fl */
150 { termp_fn_pre, NULL }, /* Fn */
151 { termp_ft_pre, NULL }, /* Ft */
152 { termp_bold_pre, NULL }, /* Ic */
153 { termp_in_pre, termp_in_post }, /* In */
154 { termp_li_pre, NULL }, /* Li */
155 { termp_nd_pre, NULL }, /* Nd */
156 { termp_nm_pre, termp_nm_post }, /* Nm */
157 { termp_quote_pre, termp_quote_post }, /* Op */
158 { termp_abort_pre, NULL }, /* Ot */
159 { termp_under_pre, NULL }, /* Pa */
160 { termp_ex_pre, NULL }, /* Rv */
161 { NULL, NULL }, /* St */
162 { termp_under_pre, NULL }, /* Va */
163 { termp_vt_pre, NULL }, /* Vt */
164 { termp_xr_pre, NULL }, /* Xr */
165 { termp__a_pre, termp____post }, /* %A */
166 { termp_under_pre, termp____post }, /* %B */
167 { NULL, termp____post }, /* %D */
168 { termp_under_pre, termp____post }, /* %I */
169 { termp_under_pre, termp____post }, /* %J */
170 { NULL, termp____post }, /* %N */
171 { NULL, termp____post }, /* %O */
172 { NULL, termp____post }, /* %P */
173 { NULL, termp____post }, /* %R */
174 { termp__t_pre, termp__t_post }, /* %T */
175 { NULL, termp____post }, /* %V */
176 { NULL, NULL }, /* Ac */
177 { termp_quote_pre, termp_quote_post }, /* Ao */
178 { termp_quote_pre, termp_quote_post }, /* Aq */
179 { NULL, NULL }, /* At */
180 { NULL, NULL }, /* Bc */
181 { termp_bf_pre, NULL }, /* Bf */
182 { termp_quote_pre, termp_quote_post }, /* Bo */
183 { termp_quote_pre, termp_quote_post }, /* Bq */
184 { termp_xx_pre, termp_xx_post }, /* Bsx */
185 { NULL, NULL }, /* Bx */
186 { termp_skip_pre, NULL }, /* Db */
187 { NULL, NULL }, /* Dc */
188 { termp_quote_pre, termp_quote_post }, /* Do */
189 { termp_quote_pre, termp_quote_post }, /* Dq */
190 { NULL, NULL }, /* Ec */ /* FIXME: no space */
191 { NULL, NULL }, /* Ef */
192 { termp_under_pre, NULL }, /* Em */
193 { termp_eo_pre, termp_eo_post }, /* Eo */
194 { termp_xx_pre, termp_xx_post }, /* Fx */
195 { termp_bold_pre, NULL }, /* Ms */
196 { termp_li_pre, NULL }, /* No */
197 { termp_ns_pre, NULL }, /* Ns */
198 { termp_xx_pre, termp_xx_post }, /* Nx */
199 { termp_xx_pre, termp_xx_post }, /* Ox */
200 { NULL, NULL }, /* Pc */
201 { NULL, termp_pf_post }, /* Pf */
202 { termp_quote_pre, termp_quote_post }, /* Po */
203 { termp_quote_pre, termp_quote_post }, /* Pq */
204 { NULL, NULL }, /* Qc */
205 { termp_quote_pre, termp_quote_post }, /* Ql */
206 { termp_quote_pre, termp_quote_post }, /* Qo */
207 { termp_quote_pre, termp_quote_post }, /* Qq */
208 { NULL, NULL }, /* Re */
209 { termp_rs_pre, NULL }, /* Rs */
210 { NULL, NULL }, /* Sc */
211 { termp_quote_pre, termp_quote_post }, /* So */
212 { termp_quote_pre, termp_quote_post }, /* Sq */
213 { termp_sm_pre, NULL }, /* Sm */
214 { termp_under_pre, NULL }, /* Sx */
215 { termp_bold_pre, NULL }, /* Sy */
216 { NULL, NULL }, /* Tn */
217 { termp_xx_pre, termp_xx_post }, /* Ux */
218 { NULL, NULL }, /* Xc */
219 { NULL, NULL }, /* Xo */
220 { termp_fo_pre, termp_fo_post }, /* Fo */
221 { NULL, NULL }, /* Fc */
222 { termp_quote_pre, termp_quote_post }, /* Oo */
223 { NULL, NULL }, /* Oc */
224 { termp_bk_pre, termp_bk_post }, /* Bk */
225 { NULL, NULL }, /* Ek */
226 { NULL, NULL }, /* Bt */
227 { NULL, NULL }, /* Hf */
228 { termp_under_pre, NULL }, /* Fr */
229 { NULL, NULL }, /* Ud */
230 { NULL, termp_lb_post }, /* Lb */
231 { termp_abort_pre, NULL }, /* Lp */
232 { termp_lk_pre, NULL }, /* Lk */
233 { termp_under_pre, NULL }, /* Mt */
234 { termp_quote_pre, termp_quote_post }, /* Brq */
235 { termp_quote_pre, termp_quote_post }, /* Bro */
236 { NULL, NULL }, /* Brc */
237 { NULL, termp____post }, /* %C */
238 { termp_skip_pre, NULL }, /* Es */
239 { termp_quote_pre, termp_quote_post }, /* En */
240 { termp_xx_pre, termp_xx_post }, /* Dx */
241 { NULL, termp____post }, /* %Q */
242 { NULL, termp____post }, /* %U */
243 { NULL, NULL }, /* Ta */
244 { termp_skip_pre, NULL }, /* Tg */
249 terminal_mdoc(void *arg, const struct roff_meta *mdoc)
251 struct roff_node *n, *nn;
253 size_t save_defindent;
255 p = (struct termp *)arg;
256 p->tcol->rmargin = p->maxrmargin = p->defrmargin;
257 term_tab_set(p, NULL);
258 term_tab_set(p, "T");
259 term_tab_set(p, ".5i");
261 n = mdoc->first->child;
262 if (p->synopsisonly) {
263 for (nn = NULL; n != NULL; n = n->next) {
264 if (n->tok != MDOC_Sh)
266 if (n->sec == SEC_SYNOPSIS)
268 if (nn == NULL && n->sec == SEC_NAME)
273 p->flags |= TERMP_NOSPACE;
274 if (n != NULL && (n = n->child->next->child) != NULL)
275 print_mdoc_nodelist(p, NULL, mdoc, n);
278 save_defindent = p->defindent;
279 if (p->defindent == 0)
281 term_begin(p, print_mdoc_head, print_mdoc_foot, mdoc);
283 (n->type == ROFFT_COMMENT ||
284 n->flags & NODE_NOPRT))
287 if (n->tok != MDOC_Sh)
289 print_mdoc_nodelist(p, NULL, mdoc, n);
292 p->defindent = save_defindent;
297 print_mdoc_nodelist(DECL_ARGS)
300 print_mdoc_node(p, pair, meta, n);
306 print_mdoc_node(DECL_ARGS)
308 const struct mdoc_term_act *act;
309 struct termpair npair;
310 size_t offset, rmargin;
314 * In no-fill mode, break the output line at the beginning
315 * of new input lines except after \c, and nowhere else.
318 if (n->flags & NODE_NOFILL) {
319 if (n->flags & NODE_LINE &&
320 (p->flags & TERMP_NONEWLINE) == 0)
322 p->flags |= TERMP_BRNEVER;
324 p->flags &= ~TERMP_BRNEVER;
326 if (n->type == ROFFT_COMMENT || n->flags & NODE_NOPRT)
330 offset = p->tcol->offset;
331 rmargin = p->tcol->rmargin;
332 n->flags &= ~NODE_ENDED;
333 n->prev_font = p->fonti;
335 memset(&npair, 0, sizeof(struct termpair));
338 if (n->flags & NODE_ID && n->tok != MDOC_Pp &&
339 (n->tok != MDOC_It || n->type != ROFFT_BLOCK))
340 term_tag_write(n, p->line);
343 * Keeps only work until the end of a line. If a keep was
344 * invoked in a prior line, revert it to PREKEEP.
347 if (p->flags & TERMP_KEEP && n->flags & NODE_LINE) {
348 p->flags &= ~TERMP_KEEP;
349 p->flags |= TERMP_PREKEEP;
353 * After the keep flags have been set up, we may now
354 * produce output. Note that some pre-handlers do so.
360 if (n->flags & NODE_LINE) {
361 switch (*n->string) {
363 if (p->flags & TERMP_NONEWLINE)
369 if ((p->flags & TERMP_NONEWLINE) == 0)
376 if (NODE_DELIMC & n->flags)
377 p->flags |= TERMP_NOSPACE;
378 term_word(p, n->string);
379 if (NODE_DELIMO & n->flags)
380 p->flags |= TERMP_NOSPACE;
383 if ( ! (n->flags & NODE_LINE))
384 p->flags |= TERMP_NOSPACE;
386 if (n->next != NULL && ! (n->next->flags & NODE_LINE))
387 p->flags |= TERMP_NOSPACE;
390 if (p->tbl.cols == NULL)
392 term_tbl(p, n->span);
395 if (n->tok < ROFF_MAX) {
399 assert(n->tok >= MDOC_Dd && n->tok < MDOC_MAX);
400 act = mdoc_term_acts + (n->tok - MDOC_Dd);
401 if (act->pre != NULL &&
402 (n->end == ENDBODY_NOT || n->child != NULL))
403 chld = (*act->pre)(p, &npair, meta, n);
407 if (chld && n->child)
408 print_mdoc_nodelist(p, &npair, meta, n->child);
411 (ENDBODY_NOT == n->end ? n : n->body)->prev_font);
421 if (act->post == NULL || n->flags & NODE_ENDED)
423 (void)(*act->post)(p, &npair, meta, n);
426 * Explicit end tokens not only call the post
427 * handler, but also tell the respective block
428 * that it must not call the post handler again.
430 if (ENDBODY_NOT != n->end)
431 n->body->flags |= NODE_ENDED;
435 if (NODE_EOS & n->flags)
436 p->flags |= TERMP_SENTENCE;
438 if (n->type != ROFFT_TEXT)
439 p->tcol->offset = offset;
440 p->tcol->rmargin = rmargin;
444 print_mdoc_foot(struct termp *p, const struct roff_meta *meta)
448 term_fontrepl(p, TERMFONT_NONE);
451 * Output the footer in new-groff style, that is, three columns
452 * with the middle being the manual date and flanking columns
453 * being the operating system:
461 sz = term_strlen(p, meta->date);
462 p->tcol->rmargin = p->maxrmargin > sz ?
463 (p->maxrmargin + term_len(p, 1) - sz) / 2 : 0;
465 p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
467 term_word(p, meta->os);
470 p->tcol->offset = p->tcol->rmargin;
471 sz = term_strlen(p, meta->os);
472 p->tcol->rmargin = p->maxrmargin > sz ? p->maxrmargin - sz : 0;
473 p->flags |= TERMP_NOSPACE;
475 term_word(p, meta->date);
478 p->tcol->offset = p->tcol->rmargin;
479 p->tcol->rmargin = p->maxrmargin;
481 p->flags &= ~TERMP_NOBREAK;
482 p->flags |= TERMP_NOSPACE;
484 term_word(p, meta->os);
488 p->tcol->rmargin = p->maxrmargin;
493 print_mdoc_head(struct termp *p, const struct roff_meta *meta)
495 char *volume, *title;
496 size_t vollen, titlen;
499 * The header is strange. It has three components, which are
500 * really two with the first duplicated. It goes like this:
502 * IDENTIFIER TITLE IDENTIFIER
504 * The IDENTIFIER is NAME(SECTION), which is the command-name
505 * (if given, or "unknown" if not) followed by the manual page
506 * section. These are given in `Dt'. The TITLE is a free-form
507 * string depending on the manual volume. If not specified, it
508 * switches on the manual section.
512 if (NULL == meta->arch)
513 volume = mandoc_strdup(meta->vol);
515 mandoc_asprintf(&volume, "%s (%s)",
516 meta->vol, meta->arch);
517 vollen = term_strlen(p, volume);
519 if (NULL == meta->msec)
520 title = mandoc_strdup(meta->title);
522 mandoc_asprintf(&title, "%s(%s)",
523 meta->title, meta->msec);
524 titlen = term_strlen(p, title);
526 p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
529 p->tcol->rmargin = 2 * (titlen+1) + vollen < p->maxrmargin ?
530 (p->maxrmargin - vollen + term_len(p, 1)) / 2 :
531 vollen < p->maxrmargin ? p->maxrmargin - vollen : 0;
536 p->flags |= TERMP_NOSPACE;
537 p->tcol->offset = p->tcol->rmargin;
538 p->tcol->rmargin = p->tcol->offset + vollen + titlen <
539 p->maxrmargin ? p->maxrmargin - titlen : p->maxrmargin;
541 term_word(p, volume);
544 p->flags &= ~TERMP_NOBREAK;
546 if (p->tcol->rmargin + titlen <= p->maxrmargin) {
547 p->flags |= TERMP_NOSPACE;
548 p->tcol->offset = p->tcol->rmargin;
549 p->tcol->rmargin = p->maxrmargin;
554 p->flags &= ~TERMP_NOSPACE;
556 p->tcol->rmargin = p->maxrmargin;
562 a2width(const struct termp *p, const char *v)
567 end = a2roffsu(v, &su, SCALE_MAX);
568 if (end == NULL || *end != '\0') {
569 SCALE_HS_INIT(&su, term_strlen(p, v));
570 su.scale /= term_strlen(p, "0");
572 return term_hen(p, &su);
576 * Determine how much space to print out before block elements of `It'
577 * (and thus `Bl') and `Bd'. And then go ahead and print that space,
581 print_bvspace(struct termp *p, struct roff_node *bl, struct roff_node *n)
583 struct roff_node *nn;
587 if ((bl->tok == MDOC_Bd && bl->norm->Bd.comp) ||
588 (bl->tok == MDOC_Bl && bl->norm->Bl.comp))
591 /* Do not vspace directly after Ss/Sh. */
594 while (roff_node_prev(nn) == NULL) {
597 if (nn->type == ROFFT_ROOT)
599 } while (nn->type != ROFFT_BLOCK);
600 if (nn->tok == MDOC_Sh || nn->tok == MDOC_Ss)
602 if (nn->tok == MDOC_It &&
603 nn->parent->parent->norm->Bl.type != LIST_item)
608 * No vertical space after:
609 * items in .Bl -column
610 * items without a body in .Bl -diag
613 if (bl->tok != MDOC_Bl ||
614 n->prev == NULL || n->prev->tok != MDOC_It ||
615 (bl->norm->Bl.type != LIST_column &&
616 (bl->norm->Bl.type != LIST_diag ||
617 n->prev->body->child != NULL)))
623 termp_it_pre(DECL_ARGS)
627 const struct roff_node *bl, *nn;
629 int i, offset, width;
632 if (n->type == ROFFT_BLOCK) {
633 print_bvspace(p, n->parent->parent, n);
634 if (n->flags & NODE_ID)
635 term_tag_write(n, p->line);
639 bl = n->parent->parent->parent;
640 type = bl->norm->Bl.type;
643 * Defaults for specific list types.
651 width = term_len(p, 2);
655 width = term_len(p, 8);
658 width = term_len(p, 10);
667 * First calculate width and offset. This is pretty easy unless
668 * we're a -column list, in which case all prior columns must
672 if (bl->norm->Bl.offs != NULL) {
673 offset = a2width(p, bl->norm->Bl.offs);
674 if (offset < 0 && (size_t)(-offset) > p->tcol->offset)
675 offset = -p->tcol->offset;
676 else if (offset > SHRT_MAX)
682 if (n->type == ROFFT_HEAD)
686 * Imitate groff's column handling:
687 * - For each earlier column, add its width.
688 * - For less than 5 columns, add four more blanks per
690 * - For exactly 5 columns, add three more blank per
692 * - For more than 5 columns, add only one column.
694 ncols = bl->norm->Bl.ncols;
695 dcol = ncols < 5 ? term_len(p, 4) :
696 ncols == 5 ? term_len(p, 3) : term_len(p, 1);
699 * Calculate the offset by applying all prior ROFFT_BODY,
700 * so we stop at the ROFFT_HEAD (nn->prev == NULL).
703 for (i = 0, nn = n->prev;
704 nn->prev && i < (int)ncols;
705 nn = nn->prev, i++) {
707 term_strlen(p, bl->norm->Bl.cols[i]));
708 su.scale /= term_strlen(p, "0");
709 offset += term_hen(p, &su) + dcol;
713 * When exceeding the declared number of columns, leave
714 * the remaining widths at 0. This will later be
715 * adjusted to the default width of 10, or, for the last
716 * column, stretched to the right margin.
722 * Use the declared column widths, extended as explained
723 * in the preceding paragraph.
725 SCALE_HS_INIT(&su, term_strlen(p, bl->norm->Bl.cols[i]));
726 su.scale /= term_strlen(p, "0");
727 width = term_hen(p, &su) + dcol;
730 if (NULL == bl->norm->Bl.width)
734 * Note: buffer the width by 2, which is groff's magic
735 * number for buffering single arguments. See the above
736 * handling for column for how this changes.
738 width = a2width(p, bl->norm->Bl.width) + term_len(p, 2);
739 if (width < 0 && (size_t)(-width) > p->tcol->offset)
740 width = -p->tcol->offset;
741 else if (width > SHRT_MAX)
747 * Whitespace control. Inset bodies need an initial space,
748 * while diagonal bodies need two.
751 p->flags |= TERMP_NOSPACE;
755 if (n->type == ROFFT_BODY)
756 term_word(p, "\\ \\ ");
759 if (n->type == ROFFT_BODY && n->parent->head->child != NULL)
766 p->flags |= TERMP_NOSPACE;
770 if (n->type == ROFFT_HEAD)
771 term_fontpush(p, TERMFONT_BOLD);
778 * Pad and break control. This is the tricky part. These flags
779 * are documented in term_flushln() in term.c. Note that we're
780 * going to unset all of these flags in termp_it_post() when we
789 if (n->type == ROFFT_HEAD) {
790 p->flags |= TERMP_NOBREAK | TERMP_HANG;
792 } else if (width <= (int)term_len(p, 2))
793 p->flags |= TERMP_NOPAD;
796 if (n->type != ROFFT_HEAD)
798 p->flags |= TERMP_NOBREAK | TERMP_BRIND | TERMP_HANG;
802 if (n->type != ROFFT_HEAD)
805 p->flags |= TERMP_NOBREAK | TERMP_BRTRSP | TERMP_BRIND;
808 if (NULL == n->next || NULL == n->next->child)
809 p->flags |= TERMP_HANG;
812 if (n->type == ROFFT_HEAD)
815 if (NULL == n->next) {
816 p->flags &= ~TERMP_NOBREAK;
819 p->flags |= TERMP_NOBREAK;
825 if (n->type != ROFFT_HEAD)
827 p->flags |= TERMP_NOBREAK | TERMP_BRIND;
835 * Margin control. Set-head-width lists have their right
836 * margins shortened. The body for these lists has the offset
837 * necessarily lengthened. Everybody gets the offset.
840 p->tcol->offset += offset;
849 if (n->type == ROFFT_HEAD)
850 p->tcol->rmargin = p->tcol->offset + width;
852 p->tcol->offset += width;
856 p->tcol->rmargin = p->tcol->offset + width;
858 * XXX - this behaviour is not documented: the
859 * right-most column is filled to the right margin.
861 if (n->type == ROFFT_HEAD)
863 if (n->next == NULL && p->tcol->rmargin < p->maxrmargin)
864 p->tcol->rmargin = p->maxrmargin;
871 * The dash, hyphen, bullet and enum lists all have a special
872 * HEAD character (temporarily bold, in some cases).
875 if (n->type == ROFFT_HEAD)
878 term_fontpush(p, TERMFONT_BOLD);
879 term_word(p, "\\[bu]");
884 term_fontpush(p, TERMFONT_BOLD);
889 (pair->ppair->ppair->count)++;
890 (void)snprintf(buf, sizeof(buf), "%d.",
891 pair->ppair->ppair->count);
899 * If we're not going to process our children, indicate so here.
908 if (n->type == ROFFT_HEAD)
912 if (n->type == ROFFT_HEAD)
924 termp_it_post(DECL_ARGS)
928 if (n->type == ROFFT_BLOCK)
931 type = n->parent->parent->parent->norm->Bl.type;
937 if (n->type == ROFFT_BODY)
941 if (n->type == ROFFT_BODY)
950 * Now that our output is flushed, we can reset our tags. Since
951 * only `It' sets these flags, we're free to assume that nobody
952 * has munged them in the meanwhile.
955 p->flags &= ~(TERMP_NOBREAK | TERMP_BRTRSP | TERMP_BRIND | TERMP_HANG);
960 termp_nm_pre(DECL_ARGS)
964 if (n->type == ROFFT_BLOCK) {
965 p->flags |= TERMP_PREKEEP;
969 if (n->type == ROFFT_BODY) {
970 if (n->child == NULL)
972 p->flags |= TERMP_NOSPACE;
974 if (n->prev->child != NULL)
975 cp = n->prev->child->string;
979 p->tcol->offset += term_len(p, 6);
981 p->tcol->offset += term_len(p, 1) +
986 if (n->child == NULL)
989 if (n->type == ROFFT_HEAD)
990 synopsis_pre(p, n->parent);
992 if (n->type == ROFFT_HEAD &&
993 n->next != NULL && n->next->child != NULL) {
994 p->flags |= TERMP_NOSPACE | TERMP_NOBREAK | TERMP_BRIND;
996 p->tcol->rmargin = p->tcol->offset + term_len(p, 1);
997 if (n->child == NULL)
998 p->tcol->rmargin += term_strlen(p, meta->name);
999 else if (n->child->type == ROFFT_TEXT) {
1000 p->tcol->rmargin += term_strlen(p, n->child->string);
1001 if (n->child->next != NULL)
1002 p->flags |= TERMP_HANG;
1004 p->tcol->rmargin += term_len(p, 5);
1005 p->flags |= TERMP_HANG;
1008 return termp_bold_pre(p, pair, meta, n);
1012 termp_nm_post(DECL_ARGS)
1016 p->flags &= ~(TERMP_KEEP | TERMP_PREKEEP);
1019 if (n->next == NULL || n->next->child == NULL)
1022 p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND | TERMP_HANG);
1026 if (n->child != NULL)
1035 termp_fl_pre(DECL_ARGS)
1037 struct roff_node *nn;
1039 term_fontpush(p, TERMFONT_BOLD);
1040 term_word(p, "\\-");
1042 if (n->child != NULL ||
1043 ((nn = roff_node_next(n)) != NULL &&
1044 nn->type != ROFFT_TEXT &&
1045 (nn->flags & NODE_LINE) == 0))
1046 p->flags |= TERMP_NOSPACE;
1052 termp__a_pre(DECL_ARGS)
1054 struct roff_node *nn;
1056 if ((nn = roff_node_prev(n)) != NULL && nn->tok == MDOC__A &&
1057 ((nn = roff_node_next(n)) == NULL || nn->tok != MDOC__A))
1058 term_word(p, "and");
1064 termp_an_pre(DECL_ARGS)
1067 if (n->norm->An.auth == AUTH_split) {
1068 p->flags &= ~TERMP_NOSPLIT;
1069 p->flags |= TERMP_SPLIT;
1072 if (n->norm->An.auth == AUTH_nosplit) {
1073 p->flags &= ~TERMP_SPLIT;
1074 p->flags |= TERMP_NOSPLIT;
1078 if (p->flags & TERMP_SPLIT)
1081 if (n->sec == SEC_AUTHORS && ! (p->flags & TERMP_NOSPLIT))
1082 p->flags |= TERMP_SPLIT;
1088 termp_ns_pre(DECL_ARGS)
1091 if ( ! (NODE_LINE & n->flags))
1092 p->flags |= TERMP_NOSPACE;
1097 termp_rs_pre(DECL_ARGS)
1099 if (SEC_SEE_ALSO != n->sec)
1101 if (n->type == ROFFT_BLOCK && roff_node_prev(n) != NULL)
1107 termp_ex_pre(DECL_ARGS)
1114 termp_nd_pre(DECL_ARGS)
1116 if (n->type == ROFFT_BODY)
1117 term_word(p, "\\(en");
1122 termp_bl_pre(DECL_ARGS)
1136 termp_bl_post(DECL_ARGS)
1138 if (n->type != ROFFT_BLOCK)
1141 if (n->tok != MDOC_Bl || n->norm->Bl.type != LIST_column)
1143 term_tab_set(p, NULL);
1144 term_tab_set(p, "T");
1145 term_tab_set(p, ".5i");
1149 termp_xr_pre(DECL_ARGS)
1151 if (NULL == (n = n->child))
1154 assert(n->type == ROFFT_TEXT);
1155 term_word(p, n->string);
1157 if (NULL == (n = n->next))
1160 p->flags |= TERMP_NOSPACE;
1162 p->flags |= TERMP_NOSPACE;
1164 assert(n->type == ROFFT_TEXT);
1165 term_word(p, n->string);
1167 p->flags |= TERMP_NOSPACE;
1174 * This decides how to assert whitespace before any of the SYNOPSIS set
1175 * of macros (which, as in the case of Ft/Fo and Ft/Fn, may contain
1179 synopsis_pre(struct termp *p, struct roff_node *n)
1181 struct roff_node *np;
1183 if ((n->flags & NODE_SYNPRETTY) == 0 ||
1184 (np = roff_node_prev(n)) == NULL)
1188 * If we're the second in a pair of like elements, emit our
1189 * newline and return. UNLESS we're `Fo', `Fn', `Fn', in which
1190 * case we soldier on.
1192 if (np->tok == n->tok &&
1193 MDOC_Ft != n->tok &&
1194 MDOC_Fo != n->tok &&
1195 MDOC_Fn != n->tok) {
1201 * If we're one of the SYNOPSIS set and non-like pair-wise after
1202 * another (or Fn/Fo, which we've let slip through) then assert
1203 * vertical space, else only newline and move on.
1214 if (n->tok != MDOC_Fn && n->tok != MDOC_Fo) {
1226 termp_vt_pre(DECL_ARGS)
1230 return termp_ft_pre(p, pair, meta, n);
1237 return termp_under_pre(p, pair, meta, n);
1242 termp_bold_pre(DECL_ARGS)
1244 term_fontpush(p, TERMFONT_BOLD);
1249 termp_fd_pre(DECL_ARGS)
1252 return termp_bold_pre(p, pair, meta, n);
1256 termp_fd_post(DECL_ARGS)
1262 termp_sh_pre(DECL_ARGS)
1264 struct roff_node *np;
1269 * Vertical space before sections, except
1270 * when the previous section was empty.
1272 if ((np = roff_node_prev(n)) == NULL ||
1273 np->tok != MDOC_Sh ||
1274 (np->body != NULL && np->body->child != NULL))
1278 return termp_bold_pre(p, pair, meta, n);
1280 p->tcol->offset = term_len(p, p->defindent);
1281 term_tab_set(p, NULL);
1282 term_tab_set(p, "T");
1283 term_tab_set(p, ".5i");
1284 if (n->sec == SEC_AUTHORS)
1285 p->flags &= ~(TERMP_SPLIT|TERMP_NOSPLIT);
1294 termp_sh_post(DECL_ARGS)
1302 p->tcol->offset = 0;
1310 termp_lb_post(DECL_ARGS)
1312 if (n->sec == SEC_LIBRARY && n->flags & NODE_LINE)
1317 termp_d1_pre(DECL_ARGS)
1319 if (n->type != ROFFT_BLOCK)
1322 p->tcol->offset += term_len(p, p->defindent + 1);
1323 term_tab_set(p, NULL);
1324 term_tab_set(p, "T");
1325 term_tab_set(p, ".5i");
1330 termp_ft_pre(DECL_ARGS)
1333 return termp_under_pre(p, pair, meta, n);
1337 termp_fn_pre(DECL_ARGS)
1343 pretty = n->flags & NODE_SYNPRETTY;
1344 if ((n = n->child) == NULL)
1348 rmargin = p->tcol->rmargin;
1349 p->tcol->rmargin = p->tcol->offset + term_len(p, 4);
1350 p->flags |= TERMP_NOBREAK | TERMP_BRIND | TERMP_HANG;
1353 assert(n->type == ROFFT_TEXT);
1354 term_fontpush(p, TERMFONT_BOLD);
1355 term_word(p, n->string);
1360 p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND | TERMP_HANG);
1361 p->flags |= TERMP_NOPAD;
1362 p->tcol->offset = p->tcol->rmargin;
1363 p->tcol->rmargin = rmargin;
1366 p->flags |= TERMP_NOSPACE;
1368 p->flags |= TERMP_NOSPACE;
1370 for (n = n->next; n; n = n->next) {
1371 assert(n->type == ROFFT_TEXT);
1372 term_fontpush(p, TERMFONT_UNDER);
1374 p->flags |= TERMP_NBRWORD;
1375 term_word(p, n->string);
1379 p->flags |= TERMP_NOSPACE;
1384 p->flags |= TERMP_NOSPACE;
1388 p->flags |= TERMP_NOSPACE;
1396 termp_fa_pre(DECL_ARGS)
1398 const struct roff_node *nn;
1400 if (n->parent->tok != MDOC_Fo)
1401 return termp_under_pre(p, pair, meta, n);
1403 for (nn = n->child; nn != NULL; nn = nn->next) {
1404 term_fontpush(p, TERMFONT_UNDER);
1405 p->flags |= TERMP_NBRWORD;
1406 term_word(p, nn->string);
1408 if (nn->next != NULL) {
1409 p->flags |= TERMP_NOSPACE;
1413 if (n->child != NULL &&
1414 (nn = roff_node_next(n)) != NULL &&
1415 nn->tok == MDOC_Fa) {
1416 p->flags |= TERMP_NOSPACE;
1423 termp_bd_pre(DECL_ARGS)
1427 if (n->type == ROFFT_BLOCK) {
1428 print_bvspace(p, n, n);
1430 } else if (n->type == ROFFT_HEAD)
1433 /* Handle the -offset argument. */
1435 if (n->norm->Bd.offs == NULL ||
1436 ! strcmp(n->norm->Bd.offs, "left"))
1438 else if ( ! strcmp(n->norm->Bd.offs, "indent"))
1439 p->tcol->offset += term_len(p, p->defindent + 1);
1440 else if ( ! strcmp(n->norm->Bd.offs, "indent-two"))
1441 p->tcol->offset += term_len(p, (p->defindent + 1) * 2);
1443 offset = a2width(p, n->norm->Bd.offs);
1444 if (offset < 0 && (size_t)(-offset) > p->tcol->offset)
1445 p->tcol->offset = 0;
1446 else if (offset < SHRT_MAX)
1447 p->tcol->offset += offset;
1450 switch (n->norm->Bd.type) {
1452 term_tab_set(p, NULL);
1453 term_tab_set(p, "T");
1454 term_tab_set(p, "8n");
1457 p->flags |= TERMP_CENTER;
1466 termp_bd_post(DECL_ARGS)
1468 if (n->type != ROFFT_BODY)
1470 if (n->norm->Bd.type == DISP_unfilled ||
1471 n->norm->Bd.type == DISP_literal)
1472 p->flags |= TERMP_BRNEVER;
1473 p->flags |= TERMP_NOSPACE;
1475 p->flags &= ~TERMP_BRNEVER;
1476 if (n->norm->Bd.type == DISP_centered)
1477 p->flags &= ~TERMP_CENTER;
1481 termp_xx_pre(DECL_ARGS)
1483 if ((n->aux = p->flags & TERMP_PREKEEP) == 0)
1484 p->flags |= TERMP_PREKEEP;
1489 termp_xx_post(DECL_ARGS)
1492 p->flags &= ~(TERMP_KEEP | TERMP_PREKEEP);
1496 termp_pf_post(DECL_ARGS)
1498 if (n->next != NULL && (n->next->flags & NODE_LINE) == 0)
1499 p->flags |= TERMP_NOSPACE;
1503 termp_ss_pre(DECL_ARGS)
1507 if (roff_node_prev(n) == NULL)
1513 p->tcol->offset = term_len(p, (p->defindent+1)/2);
1514 return termp_bold_pre(p, pair, meta, n);
1516 p->tcol->offset = term_len(p, p->defindent);
1517 term_tab_set(p, NULL);
1518 term_tab_set(p, "T");
1519 term_tab_set(p, ".5i");
1528 termp_ss_post(DECL_ARGS)
1530 if (n->type == ROFFT_HEAD || n->type == ROFFT_BODY)
1535 termp_in_pre(DECL_ARGS)
1538 if (n->flags & NODE_SYNPRETTY && n->flags & NODE_LINE) {
1539 term_fontpush(p, TERMFONT_BOLD);
1540 term_word(p, "#include");
1544 term_fontpush(p, TERMFONT_UNDER);
1546 p->flags |= TERMP_NOSPACE;
1551 termp_in_post(DECL_ARGS)
1553 if (n->flags & NODE_SYNPRETTY)
1554 term_fontpush(p, TERMFONT_BOLD);
1555 p->flags |= TERMP_NOSPACE;
1557 if (n->flags & NODE_SYNPRETTY)
1562 termp_pp_pre(DECL_ARGS)
1565 if (n->flags & NODE_ID)
1566 term_tag_write(n, p->line);
1571 termp_skip_pre(DECL_ARGS)
1577 termp_quote_pre(DECL_ARGS)
1579 if (n->type != ROFFT_BODY && n->type != ROFFT_ELEM)
1585 term_word(p, n->child != NULL && n->child->next == NULL &&
1586 n->child->tok == MDOC_Mt ? "<" : "\\(la");
1602 term_word(p, "\\(lq");
1605 if (NULL == n->norm->Es ||
1606 NULL == n->norm->Es->child)
1608 term_word(p, n->norm->Es->child->string);
1621 term_word(p, "\\(oq");
1627 p->flags |= TERMP_NOSPACE;
1632 termp_quote_post(DECL_ARGS)
1635 if (n->type != ROFFT_BODY && n->type != ROFFT_ELEM)
1638 p->flags |= TERMP_NOSPACE;
1643 term_word(p, n->child != NULL && n->child->next == NULL &&
1644 n->child->tok == MDOC_Mt ? ">" : "\\(ra");
1660 term_word(p, "\\(rq");
1663 if (n->norm->Es == NULL ||
1664 n->norm->Es->child == NULL ||
1665 n->norm->Es->child->next == NULL)
1666 p->flags &= ~TERMP_NOSPACE;
1668 term_word(p, n->norm->Es->child->next->string);
1681 term_word(p, "\\(cq");
1689 termp_eo_pre(DECL_ARGS)
1692 if (n->type != ROFFT_BODY)
1695 if (n->end == ENDBODY_NOT &&
1696 n->parent->head->child == NULL &&
1698 n->child->end != ENDBODY_NOT)
1699 term_word(p, "\\&");
1700 else if (n->end != ENDBODY_NOT ? n->child != NULL :
1701 n->parent->head->child != NULL && (n->child != NULL ||
1702 (n->parent->tail != NULL && n->parent->tail->child != NULL)))
1703 p->flags |= TERMP_NOSPACE;
1709 termp_eo_post(DECL_ARGS)
1713 if (n->type != ROFFT_BODY)
1716 if (n->end != ENDBODY_NOT) {
1717 p->flags &= ~TERMP_NOSPACE;
1721 body = n->child != NULL || n->parent->head->child != NULL;
1722 tail = n->parent->tail != NULL && n->parent->tail->child != NULL;
1725 p->flags |= TERMP_NOSPACE;
1726 else if ( ! (body || tail))
1727 term_word(p, "\\&");
1729 p->flags &= ~TERMP_NOSPACE;
1733 termp_fo_pre(DECL_ARGS)
1742 rmargin = p->tcol->rmargin;
1743 if (n->flags & NODE_SYNPRETTY) {
1744 p->tcol->rmargin = p->tcol->offset + term_len(p, 4);
1745 p->flags |= TERMP_NOBREAK | TERMP_BRIND |
1748 p->flags |= TERMP_NOSPACE;
1750 p->flags |= TERMP_NOSPACE;
1751 if (n->flags & NODE_SYNPRETTY) {
1753 p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND |
1755 p->flags |= TERMP_NOPAD;
1756 p->tcol->offset = p->tcol->rmargin;
1757 p->tcol->rmargin = rmargin;
1761 return termp_bold_pre(p, pair, meta, n);
1766 termp_fo_post(DECL_ARGS)
1768 if (n->type != ROFFT_BODY)
1771 p->flags |= TERMP_NOSPACE;
1774 if (n->flags & NODE_SYNPRETTY) {
1775 p->flags |= TERMP_NOSPACE;
1782 termp_bf_pre(DECL_ARGS)
1792 switch (n->norm->Bf.font) {
1794 return termp_under_pre(p, pair, meta, n);
1796 return termp_bold_pre(p, pair, meta, n);
1798 return termp_li_pre(p, pair, meta, n);
1803 termp_sm_pre(DECL_ARGS)
1805 if (n->child == NULL)
1806 p->flags ^= TERMP_NONOSPACE;
1807 else if (strcmp(n->child->string, "on") == 0)
1808 p->flags &= ~TERMP_NONOSPACE;
1810 p->flags |= TERMP_NONOSPACE;
1812 if (p->col && ! (TERMP_NONOSPACE & p->flags))
1813 p->flags &= ~TERMP_NOSPACE;
1819 termp_ap_pre(DECL_ARGS)
1821 p->flags |= TERMP_NOSPACE;
1823 p->flags |= TERMP_NOSPACE;
1828 termp____post(DECL_ARGS)
1830 struct roff_node *nn;
1833 * Handle lists of authors. In general, print each followed by
1834 * a comma. Don't print the comma if there are only two
1837 if (n->tok == MDOC__A &&
1838 (nn = roff_node_next(n)) != NULL && nn->tok == MDOC__A &&
1839 ((nn = roff_node_next(nn)) == NULL || nn->tok != MDOC__A) &&
1840 ((nn = roff_node_prev(n)) == NULL || nn->tok != MDOC__A))
1845 if (n->parent == NULL || n->parent->tok != MDOC_Rs)
1848 p->flags |= TERMP_NOSPACE;
1849 if (roff_node_next(n) == NULL) {
1851 p->flags |= TERMP_SENTENCE;
1857 termp_li_pre(DECL_ARGS)
1859 term_fontpush(p, TERMFONT_NONE);
1864 termp_lk_pre(DECL_ARGS)
1866 const struct roff_node *link, *descr, *punct;
1868 if ((link = n->child) == NULL)
1871 /* Find beginning of trailing punctuation. */
1873 while (punct != link && punct->flags & NODE_DELIMC)
1874 punct = punct->prev;
1875 punct = punct->next;
1878 if ((descr = link->next) != NULL && descr != punct) {
1879 term_fontpush(p, TERMFONT_UNDER);
1880 while (descr != punct) {
1881 if (descr->flags & (NODE_DELIMC | NODE_DELIMO))
1882 p->flags |= TERMP_NOSPACE;
1883 term_word(p, descr->string);
1884 descr = descr->next;
1887 p->flags |= TERMP_NOSPACE;
1892 term_fontpush(p, TERMFONT_BOLD);
1893 term_word(p, link->string);
1896 /* Trailing punctuation. */
1897 while (punct != NULL) {
1898 p->flags |= TERMP_NOSPACE;
1899 term_word(p, punct->string);
1900 punct = punct->next;
1906 termp_bk_pre(DECL_ARGS)
1914 if (n->parent->args != NULL || n->prev->child == NULL)
1915 p->flags |= TERMP_PREKEEP;
1924 termp_bk_post(DECL_ARGS)
1926 if (n->type == ROFFT_BODY)
1927 p->flags &= ~(TERMP_KEEP | TERMP_PREKEEP);
1931 * If we are in an `Rs' and there is a journal present,
1932 * then quote us instead of underlining us (for disambiguation).
1935 termp__t_post(DECL_ARGS)
1937 if (n->parent != NULL && n->parent->tok == MDOC_Rs &&
1938 n->parent->norm->Rs.quote_T)
1939 termp_quote_post(p, pair, meta, n);
1940 termp____post(p, pair, meta, n);
1944 termp__t_pre(DECL_ARGS)
1946 if (n->parent != NULL && n->parent->tok == MDOC_Rs &&
1947 n->parent->norm->Rs.quote_T)
1948 return termp_quote_pre(p, pair, meta, n);
1950 return termp_under_pre(p, pair, meta, n);
1954 termp_under_pre(DECL_ARGS)
1956 term_fontpush(p, TERMFONT_UNDER);
1961 termp_abort_pre(DECL_ARGS)