1 /* $Id: mdoc_term.c,v 1.341 2017/01/11 17:39:53 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2012-2017 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 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.
21 #include <sys/types.h>
31 #include "mandoc_aux.h"
41 struct termpair *ppair;
45 #define DECL_ARGS struct termp *p, \
46 struct termpair *pair, \
47 const struct roff_meta *meta, \
51 int (*pre)(DECL_ARGS);
52 void (*post)(DECL_ARGS);
55 static int a2width(const struct termp *, const char *);
57 static void print_bvspace(struct termp *,
58 const struct roff_node *,
59 const struct roff_node *);
60 static void print_mdoc_node(DECL_ARGS);
61 static void print_mdoc_nodelist(DECL_ARGS);
62 static void print_mdoc_head(struct termp *, const struct roff_meta *);
63 static void print_mdoc_foot(struct termp *, const struct roff_meta *);
64 static void synopsis_pre(struct termp *,
65 const 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_an_pre(DECL_ARGS);
88 static int termp_ap_pre(DECL_ARGS);
89 static int termp_bd_pre(DECL_ARGS);
90 static int termp_bf_pre(DECL_ARGS);
91 static int termp_bk_pre(DECL_ARGS);
92 static int termp_bl_pre(DECL_ARGS);
93 static int termp_bold_pre(DECL_ARGS);
94 static int termp_cd_pre(DECL_ARGS);
95 static int termp_d1_pre(DECL_ARGS);
96 static int termp_eo_pre(DECL_ARGS);
97 static int termp_em_pre(DECL_ARGS);
98 static int termp_er_pre(DECL_ARGS);
99 static int termp_ex_pre(DECL_ARGS);
100 static int termp_fa_pre(DECL_ARGS);
101 static int termp_fd_pre(DECL_ARGS);
102 static int termp_fl_pre(DECL_ARGS);
103 static int termp_fn_pre(DECL_ARGS);
104 static int termp_fo_pre(DECL_ARGS);
105 static int termp_ft_pre(DECL_ARGS);
106 static int termp_in_pre(DECL_ARGS);
107 static int termp_it_pre(DECL_ARGS);
108 static int termp_li_pre(DECL_ARGS);
109 static int termp_ll_pre(DECL_ARGS);
110 static int termp_lk_pre(DECL_ARGS);
111 static int termp_nd_pre(DECL_ARGS);
112 static int termp_nm_pre(DECL_ARGS);
113 static int termp_ns_pre(DECL_ARGS);
114 static int termp_quote_pre(DECL_ARGS);
115 static int termp_rs_pre(DECL_ARGS);
116 static int termp_sh_pre(DECL_ARGS);
117 static int termp_skip_pre(DECL_ARGS);
118 static int termp_sm_pre(DECL_ARGS);
119 static int termp_sp_pre(DECL_ARGS);
120 static int termp_ss_pre(DECL_ARGS);
121 static int termp_sy_pre(DECL_ARGS);
122 static int termp_tag_pre(DECL_ARGS);
123 static int termp_under_pre(DECL_ARGS);
124 static int termp_vt_pre(DECL_ARGS);
125 static int termp_xr_pre(DECL_ARGS);
126 static int termp_xx_pre(DECL_ARGS);
128 static const struct termact termacts[MDOC_MAX] = {
129 { termp_ap_pre, NULL }, /* Ap */
130 { NULL, NULL }, /* Dd */
131 { NULL, NULL }, /* Dt */
132 { NULL, NULL }, /* Os */
133 { termp_sh_pre, termp_sh_post }, /* Sh */
134 { termp_ss_pre, termp_ss_post }, /* Ss */
135 { termp_sp_pre, NULL }, /* Pp */
136 { termp_d1_pre, termp_bl_post }, /* D1 */
137 { termp_d1_pre, termp_bl_post }, /* Dl */
138 { termp_bd_pre, termp_bd_post }, /* Bd */
139 { NULL, NULL }, /* Ed */
140 { termp_bl_pre, termp_bl_post }, /* Bl */
141 { NULL, NULL }, /* El */
142 { termp_it_pre, termp_it_post }, /* It */
143 { termp_under_pre, NULL }, /* Ad */
144 { termp_an_pre, NULL }, /* An */
145 { termp_under_pre, NULL }, /* Ar */
146 { termp_cd_pre, NULL }, /* Cd */
147 { termp_bold_pre, NULL }, /* Cm */
148 { termp_li_pre, NULL }, /* Dv */
149 { termp_er_pre, NULL }, /* Er */
150 { termp_tag_pre, NULL }, /* Ev */
151 { termp_ex_pre, NULL }, /* Ex */
152 { termp_fa_pre, NULL }, /* Fa */
153 { termp_fd_pre, termp_fd_post }, /* Fd */
154 { termp_fl_pre, NULL }, /* Fl */
155 { termp_fn_pre, NULL }, /* Fn */
156 { termp_ft_pre, NULL }, /* Ft */
157 { termp_bold_pre, NULL }, /* Ic */
158 { termp_in_pre, termp_in_post }, /* In */
159 { termp_li_pre, NULL }, /* Li */
160 { termp_nd_pre, NULL }, /* Nd */
161 { termp_nm_pre, termp_nm_post }, /* Nm */
162 { termp_quote_pre, termp_quote_post }, /* Op */
163 { termp_ft_pre, NULL }, /* Ot */
164 { termp_under_pre, NULL }, /* Pa */
165 { termp_ex_pre, NULL }, /* Rv */
166 { NULL, NULL }, /* St */
167 { termp_under_pre, NULL }, /* Va */
168 { termp_vt_pre, NULL }, /* Vt */
169 { termp_xr_pre, NULL }, /* Xr */
170 { termp__a_pre, termp____post }, /* %A */
171 { termp_under_pre, termp____post }, /* %B */
172 { NULL, termp____post }, /* %D */
173 { termp_under_pre, termp____post }, /* %I */
174 { termp_under_pre, termp____post }, /* %J */
175 { NULL, termp____post }, /* %N */
176 { NULL, termp____post }, /* %O */
177 { NULL, termp____post }, /* %P */
178 { NULL, termp____post }, /* %R */
179 { termp__t_pre, termp__t_post }, /* %T */
180 { NULL, termp____post }, /* %V */
181 { NULL, NULL }, /* Ac */
182 { termp_quote_pre, termp_quote_post }, /* Ao */
183 { termp_quote_pre, termp_quote_post }, /* Aq */
184 { NULL, NULL }, /* At */
185 { NULL, NULL }, /* Bc */
186 { termp_bf_pre, NULL }, /* Bf */
187 { termp_quote_pre, termp_quote_post }, /* Bo */
188 { termp_quote_pre, termp_quote_post }, /* Bq */
189 { termp_xx_pre, termp_xx_post }, /* Bsx */
190 { NULL, NULL }, /* Bx */
191 { termp_skip_pre, NULL }, /* Db */
192 { NULL, NULL }, /* Dc */
193 { termp_quote_pre, termp_quote_post }, /* Do */
194 { termp_quote_pre, termp_quote_post }, /* Dq */
195 { NULL, NULL }, /* Ec */ /* FIXME: no space */
196 { NULL, NULL }, /* Ef */
197 { termp_em_pre, NULL }, /* Em */
198 { termp_eo_pre, termp_eo_post }, /* Eo */
199 { termp_xx_pre, termp_xx_post }, /* Fx */
200 { termp_bold_pre, NULL }, /* Ms */
201 { termp_li_pre, NULL }, /* No */
202 { termp_ns_pre, NULL }, /* Ns */
203 { termp_xx_pre, termp_xx_post }, /* Nx */
204 { termp_xx_pre, termp_xx_post }, /* Ox */
205 { NULL, NULL }, /* Pc */
206 { NULL, termp_pf_post }, /* Pf */
207 { termp_quote_pre, termp_quote_post }, /* Po */
208 { termp_quote_pre, termp_quote_post }, /* Pq */
209 { NULL, NULL }, /* Qc */
210 { termp_quote_pre, termp_quote_post }, /* Ql */
211 { termp_quote_pre, termp_quote_post }, /* Qo */
212 { termp_quote_pre, termp_quote_post }, /* Qq */
213 { NULL, NULL }, /* Re */
214 { termp_rs_pre, NULL }, /* Rs */
215 { NULL, NULL }, /* Sc */
216 { termp_quote_pre, termp_quote_post }, /* So */
217 { termp_quote_pre, termp_quote_post }, /* Sq */
218 { termp_sm_pre, NULL }, /* Sm */
219 { termp_under_pre, NULL }, /* Sx */
220 { termp_sy_pre, NULL }, /* Sy */
221 { NULL, NULL }, /* Tn */
222 { termp_xx_pre, termp_xx_post }, /* Ux */
223 { NULL, NULL }, /* Xc */
224 { NULL, NULL }, /* Xo */
225 { termp_fo_pre, termp_fo_post }, /* Fo */
226 { NULL, NULL }, /* Fc */
227 { termp_quote_pre, termp_quote_post }, /* Oo */
228 { NULL, NULL }, /* Oc */
229 { termp_bk_pre, termp_bk_post }, /* Bk */
230 { NULL, NULL }, /* Ek */
231 { NULL, NULL }, /* Bt */
232 { NULL, NULL }, /* Hf */
233 { termp_under_pre, NULL }, /* Fr */
234 { NULL, NULL }, /* Ud */
235 { NULL, termp_lb_post }, /* Lb */
236 { termp_sp_pre, NULL }, /* Lp */
237 { termp_lk_pre, NULL }, /* Lk */
238 { termp_under_pre, NULL }, /* Mt */
239 { termp_quote_pre, termp_quote_post }, /* Brq */
240 { termp_quote_pre, termp_quote_post }, /* Bro */
241 { NULL, NULL }, /* Brc */
242 { NULL, termp____post }, /* %C */
243 { termp_skip_pre, NULL }, /* Es */
244 { termp_quote_pre, termp_quote_post }, /* En */
245 { termp_xx_pre, termp_xx_post }, /* Dx */
246 { NULL, termp____post }, /* %Q */
247 { termp_sp_pre, NULL }, /* br */
248 { termp_sp_pre, NULL }, /* sp */
249 { NULL, termp____post }, /* %U */
250 { NULL, NULL }, /* Ta */
251 { termp_ll_pre, NULL }, /* ll */
257 terminal_mdoc(void *arg, const struct roff_man *mdoc)
262 p = (struct termp *)arg;
264 p->rmargin = p->maxrmargin = p->defrmargin;
265 p->tabwidth = term_len(p, 5);
267 n = mdoc->first->child;
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,
274 n->child->next->child);
281 if (p->defindent == 0)
283 term_begin(p, print_mdoc_head, print_mdoc_foot,
285 while (n != NULL && n->flags & NODE_NOPRT)
288 if (n->tok != MDOC_Sh)
290 print_mdoc_nodelist(p, NULL, &mdoc->meta, n);
297 print_mdoc_nodelist(DECL_ARGS)
301 print_mdoc_node(p, pair, meta, n);
307 print_mdoc_node(DECL_ARGS)
310 struct termpair npair;
311 size_t offset, rmargin;
313 if (n->flags & NODE_NOPRT)
318 rmargin = p->rmargin;
319 n->flags &= ~NODE_ENDED;
320 n->prev_font = p->fonti;
322 memset(&npair, 0, sizeof(struct termpair));
326 * Keeps only work until the end of a line. If a keep was
327 * invoked in a prior line, revert it to PREKEEP.
330 if (p->flags & TERMP_KEEP && n->flags & NODE_LINE) {
331 p->flags &= ~TERMP_KEEP;
332 p->flags |= TERMP_PREKEEP;
336 * After the keep flags have been set up, we may now
337 * produce output. Note that some pre-handlers do so.
342 if (' ' == *n->string && NODE_LINE & n->flags)
344 if (NODE_DELIMC & n->flags)
345 p->flags |= TERMP_NOSPACE;
346 term_word(p, n->string);
347 if (NODE_DELIMO & n->flags)
348 p->flags |= TERMP_NOSPACE;
351 if ( ! (n->flags & NODE_LINE))
352 p->flags |= TERMP_NOSPACE;
354 if (n->next != NULL && ! (n->next->flags & NODE_LINE))
355 p->flags |= TERMP_NOSPACE;
358 if (p->tbl.cols == NULL)
360 term_tbl(p, n->span);
363 if (termacts[n->tok].pre &&
364 (n->end == ENDBODY_NOT || n->child != NULL))
365 chld = (*termacts[n->tok].pre)
366 (p, &npair, meta, n);
370 if (chld && n->child)
371 print_mdoc_nodelist(p, &npair, meta, n->child);
374 (ENDBODY_NOT == n->end ? n : n->body)->prev_font);
384 if ( ! termacts[n->tok].post || NODE_ENDED & n->flags)
386 (void)(*termacts[n->tok].post)(p, &npair, meta, n);
389 * Explicit end tokens not only call the post
390 * handler, but also tell the respective block
391 * that it must not call the post handler again.
393 if (ENDBODY_NOT != n->end)
394 n->body->flags |= NODE_ENDED;
397 * End of line terminating an implicit block
398 * while an explicit block is still open.
399 * Continue the explicit block without spacing.
401 if (ENDBODY_NOSPACE == n->end)
402 p->flags |= TERMP_NOSPACE;
406 if (NODE_EOS & n->flags)
407 p->flags |= TERMP_SENTENCE;
409 if (MDOC_ll != n->tok) {
411 p->rmargin = rmargin;
416 print_mdoc_foot(struct termp *p, const struct roff_meta *meta)
420 term_fontrepl(p, TERMFONT_NONE);
423 * Output the footer in new-groff style, that is, three columns
424 * with the middle being the manual date and flanking columns
425 * being the operating system:
433 sz = term_strlen(p, meta->date);
434 p->rmargin = p->maxrmargin > sz ?
435 (p->maxrmargin + term_len(p, 1) - sz) / 2 : 0;
437 p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
439 term_word(p, meta->os);
442 p->offset = p->rmargin;
443 sz = term_strlen(p, meta->os);
444 p->rmargin = p->maxrmargin > sz ? p->maxrmargin - sz : 0;
445 p->flags |= TERMP_NOSPACE;
447 term_word(p, meta->date);
450 p->offset = p->rmargin;
451 p->rmargin = p->maxrmargin;
453 p->flags &= ~TERMP_NOBREAK;
454 p->flags |= TERMP_NOSPACE;
456 term_word(p, meta->os);
460 p->rmargin = p->maxrmargin;
465 print_mdoc_head(struct termp *p, const struct roff_meta *meta)
467 char *volume, *title;
468 size_t vollen, titlen;
471 * The header is strange. It has three components, which are
472 * really two with the first duplicated. It goes like this:
474 * IDENTIFIER TITLE IDENTIFIER
476 * The IDENTIFIER is NAME(SECTION), which is the command-name
477 * (if given, or "unknown" if not) followed by the manual page
478 * section. These are given in `Dt'. The TITLE is a free-form
479 * string depending on the manual volume. If not specified, it
480 * switches on the manual section.
484 if (NULL == meta->arch)
485 volume = mandoc_strdup(meta->vol);
487 mandoc_asprintf(&volume, "%s (%s)",
488 meta->vol, meta->arch);
489 vollen = term_strlen(p, volume);
491 if (NULL == meta->msec)
492 title = mandoc_strdup(meta->title);
494 mandoc_asprintf(&title, "%s(%s)",
495 meta->title, meta->msec);
496 titlen = term_strlen(p, title);
498 p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
501 p->rmargin = 2 * (titlen+1) + vollen < p->maxrmargin ?
502 (p->maxrmargin - vollen + term_len(p, 1)) / 2 :
503 vollen < p->maxrmargin ? p->maxrmargin - vollen : 0;
508 p->flags |= TERMP_NOSPACE;
509 p->offset = p->rmargin;
510 p->rmargin = p->offset + vollen + titlen < p->maxrmargin ?
511 p->maxrmargin - titlen : p->maxrmargin;
513 term_word(p, volume);
516 p->flags &= ~TERMP_NOBREAK;
518 if (p->rmargin + titlen <= p->maxrmargin) {
519 p->flags |= TERMP_NOSPACE;
520 p->offset = p->rmargin;
521 p->rmargin = p->maxrmargin;
526 p->flags &= ~TERMP_NOSPACE;
528 p->rmargin = p->maxrmargin;
534 a2width(const struct termp *p, const char *v)
538 if (a2roffsu(v, &su, SCALE_MAX) < 2) {
539 SCALE_HS_INIT(&su, term_strlen(p, v));
540 su.scale /= term_strlen(p, "0");
542 return term_hspan(p, &su) / 24;
546 * Determine how much space to print out before block elements of `It'
547 * (and thus `Bl') and `Bd'. And then go ahead and print that space,
551 print_bvspace(struct termp *p,
552 const struct roff_node *bl,
553 const struct roff_node *n)
555 const struct roff_node *nn;
561 if (MDOC_Bd == bl->tok && bl->norm->Bd.comp)
563 if (MDOC_Bl == bl->tok && bl->norm->Bl.comp)
566 /* Do not vspace directly after Ss/Sh. */
569 while (nn->prev != NULL && nn->prev->flags & NODE_NOPRT)
571 while (nn->prev == NULL) {
574 if (nn->type == ROFFT_ROOT)
576 } while (nn->type != ROFFT_BLOCK);
577 if (nn->tok == MDOC_Sh || nn->tok == MDOC_Ss)
579 if (nn->tok == MDOC_It &&
580 nn->parent->parent->norm->Bl.type != LIST_item)
584 /* A `-column' does not assert vspace within the list. */
586 if (MDOC_Bl == bl->tok && LIST_column == bl->norm->Bl.type)
587 if (n->prev && MDOC_It == n->prev->tok)
590 /* A `-diag' without body does not vspace. */
592 if (MDOC_Bl == bl->tok && LIST_diag == bl->norm->Bl.type)
593 if (n->prev && MDOC_It == n->prev->tok) {
594 assert(n->prev->body);
595 if (NULL == n->prev->body->child)
604 termp_ll_pre(DECL_ARGS)
607 term_setwidth(p, n->child != NULL ? n->child->string : NULL);
612 termp_it_pre(DECL_ARGS)
615 const struct roff_node *bl, *nn;
617 int i, offset, width;
620 if (n->type == ROFFT_BLOCK) {
621 print_bvspace(p, n->parent->parent, n);
625 bl = n->parent->parent->parent;
626 type = bl->norm->Bl.type;
629 * Defaults for specific list types.
637 width = term_len(p, 2);
641 width = term_len(p, 8);
644 width = term_len(p, 10);
653 * First calculate width and offset. This is pretty easy unless
654 * we're a -column list, in which case all prior columns must
658 if (bl->norm->Bl.offs != NULL) {
659 offset = a2width(p, bl->norm->Bl.offs);
660 if (offset < 0 && (size_t)(-offset) > p->offset)
662 else if (offset > SHRT_MAX)
668 if (n->type == ROFFT_HEAD)
672 * Imitate groff's column handling:
673 * - For each earlier column, add its width.
674 * - For less than 5 columns, add four more blanks per
676 * - For exactly 5 columns, add three more blank per
678 * - For more than 5 columns, add only one column.
680 ncols = bl->norm->Bl.ncols;
681 dcol = ncols < 5 ? term_len(p, 4) :
682 ncols == 5 ? term_len(p, 3) : term_len(p, 1);
685 * Calculate the offset by applying all prior ROFFT_BODY,
686 * so we stop at the ROFFT_HEAD (nn->prev == NULL).
689 for (i = 0, nn = n->prev;
690 nn->prev && i < (int)ncols;
692 offset += dcol + a2width(p,
693 bl->norm->Bl.cols[i]);
696 * When exceeding the declared number of columns, leave
697 * the remaining widths at 0. This will later be
698 * adjusted to the default width of 10, or, for the last
699 * column, stretched to the right margin.
705 * Use the declared column widths, extended as explained
706 * in the preceding paragraph.
708 width = a2width(p, bl->norm->Bl.cols[i]) + dcol;
711 if (NULL == bl->norm->Bl.width)
715 * Note: buffer the width by 2, which is groff's magic
716 * number for buffering single arguments. See the above
717 * handling for column for how this changes.
719 width = a2width(p, bl->norm->Bl.width) + term_len(p, 2);
720 if (width < 0 && (size_t)(-width) > p->offset)
722 else if (width > SHRT_MAX)
728 * Whitespace control. Inset bodies need an initial space,
729 * while diagonal bodies need two.
732 p->flags |= TERMP_NOSPACE;
736 if (n->type == ROFFT_BODY)
737 term_word(p, "\\ \\ ");
740 if (n->type == ROFFT_BODY && n->parent->head->child != NULL)
747 p->flags |= TERMP_NOSPACE;
751 if (n->type == ROFFT_HEAD)
752 term_fontpush(p, TERMFONT_BOLD);
759 * Pad and break control. This is the tricky part. These flags
760 * are documented in term_flushln() in term.c. Note that we're
761 * going to unset all of these flags in termp_it_post() when we
771 * Weird special case.
772 * Some very narrow lists actually hang.
774 if (width <= (int)term_len(p, 2))
775 p->flags |= TERMP_HANG;
776 if (n->type != ROFFT_HEAD)
778 p->flags |= TERMP_NOBREAK;
782 if (n->type != ROFFT_HEAD)
786 * This is ugly. If `-hang' is specified and the body
787 * is a `Bl' or `Bd', then we want basically to nullify
788 * the "overstep" effect in term_flushln() and treat
789 * this as a `-ohang' list instead.
791 if (NULL != n->next &&
792 NULL != n->next->child &&
793 (MDOC_Bl == n->next->child->tok ||
794 MDOC_Bd == n->next->child->tok))
797 p->flags |= TERMP_NOBREAK | TERMP_BRIND | TERMP_HANG;
801 if (n->type != ROFFT_HEAD)
804 p->flags |= TERMP_NOBREAK | TERMP_BRTRSP | TERMP_BRIND;
807 if (NULL == n->next || NULL == n->next->child)
808 p->flags |= TERMP_DANGLE;
811 if (n->type == ROFFT_HEAD)
814 if (NULL == n->next) {
815 p->flags &= ~TERMP_NOBREAK;
818 p->flags |= TERMP_NOBREAK;
824 if (n->type != ROFFT_HEAD)
826 p->flags |= TERMP_NOBREAK | TERMP_BRIND;
834 * Margin control. Set-head-width lists have their right
835 * margins shortened. The body for these lists has the offset
836 * necessarily lengthened. Everybody gets the offset.
844 * Same stipulation as above, regarding `-hang'. We
845 * don't want to recalculate rmargin and offsets when
846 * using `Bd' or `Bl' within `-hang' overstep lists.
848 if (n->type == ROFFT_HEAD &&
850 NULL != n->next->child &&
851 (MDOC_Bl == n->next->child->tok ||
852 MDOC_Bd == n->next->child->tok))
860 if (n->type == ROFFT_HEAD)
861 p->rmargin = p->offset + width;
867 p->rmargin = p->offset + width;
869 * XXX - this behaviour is not documented: the
870 * right-most column is filled to the right margin.
872 if (n->type == ROFFT_HEAD)
874 if (NULL == n->next && p->rmargin < p->maxrmargin)
875 p->rmargin = p->maxrmargin;
882 * The dash, hyphen, bullet and enum lists all have a special
883 * HEAD character (temporarily bold, in some cases).
886 if (n->type == ROFFT_HEAD)
889 term_fontpush(p, TERMFONT_BOLD);
890 term_word(p, "\\[bu]");
895 term_fontpush(p, TERMFONT_BOLD);
900 (pair->ppair->ppair->count)++;
901 (void)snprintf(buf, sizeof(buf), "%d.",
902 pair->ppair->ppair->count);
910 * If we're not going to process our children, indicate so here.
919 if (n->type == ROFFT_HEAD)
923 if (n->type == ROFFT_HEAD)
934 termp_it_post(DECL_ARGS)
938 if (n->type == ROFFT_BLOCK)
941 type = n->parent->parent->parent->norm->Bl.type;
947 if (n->type == ROFFT_BODY)
951 if (n->type == ROFFT_BODY)
960 * Now that our output is flushed, we can reset our tags. Since
961 * only `It' sets these flags, we're free to assume that nobody
962 * has munged them in the meanwhile.
965 p->flags &= ~(TERMP_NOBREAK | TERMP_BRTRSP | TERMP_BRIND |
966 TERMP_DANGLE | TERMP_HANG);
971 termp_nm_pre(DECL_ARGS)
975 if (n->type == ROFFT_BLOCK) {
976 p->flags |= TERMP_PREKEEP;
980 if (n->type == ROFFT_BODY) {
981 if (NULL == n->child)
983 p->flags |= TERMP_NOSPACE;
985 if (n->prev->child != NULL)
986 cp = n->prev->child->string;
990 p->offset += term_len(p, 6);
992 p->offset += term_len(p, 1) + term_strlen(p, cp);
996 if (NULL == n->child && NULL == meta->name)
999 if (n->type == ROFFT_HEAD)
1000 synopsis_pre(p, n->parent);
1002 if (n->type == ROFFT_HEAD &&
1003 NULL != n->next && NULL != n->next->child) {
1004 p->flags |= TERMP_NOSPACE | TERMP_NOBREAK | TERMP_BRIND;
1006 p->rmargin = p->offset + term_len(p, 1);
1007 if (NULL == n->child) {
1008 p->rmargin += term_strlen(p, meta->name);
1009 } else if (n->child->type == ROFFT_TEXT) {
1010 p->rmargin += term_strlen(p, n->child->string);
1012 p->flags |= TERMP_HANG;
1014 p->rmargin += term_len(p, 5);
1015 p->flags |= TERMP_HANG;
1019 term_fontpush(p, TERMFONT_BOLD);
1020 if (NULL == n->child)
1021 term_word(p, meta->name);
1026 termp_nm_post(DECL_ARGS)
1029 if (n->type == ROFFT_BLOCK) {
1030 p->flags &= ~(TERMP_KEEP | TERMP_PREKEEP);
1031 } else if (n->type == ROFFT_HEAD &&
1032 NULL != n->next && NULL != n->next->child) {
1034 p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND | TERMP_HANG);
1036 } else if (n->type == ROFFT_BODY && n->child != NULL)
1041 termp_fl_pre(DECL_ARGS)
1044 termp_tag_pre(p, pair, meta, n);
1045 term_fontpush(p, TERMFONT_BOLD);
1046 term_word(p, "\\-");
1048 if (!(n->child == NULL &&
1050 n->next->type == ROFFT_TEXT ||
1051 n->next->flags & NODE_LINE)))
1052 p->flags |= TERMP_NOSPACE;
1058 termp__a_pre(DECL_ARGS)
1061 if (n->prev && MDOC__A == n->prev->tok)
1062 if (NULL == n->next || MDOC__A != n->next->tok)
1063 term_word(p, "and");
1069 termp_an_pre(DECL_ARGS)
1072 if (n->norm->An.auth == AUTH_split) {
1073 p->flags &= ~TERMP_NOSPLIT;
1074 p->flags |= TERMP_SPLIT;
1077 if (n->norm->An.auth == AUTH_nosplit) {
1078 p->flags &= ~TERMP_SPLIT;
1079 p->flags |= TERMP_NOSPLIT;
1083 if (p->flags & TERMP_SPLIT)
1086 if (n->sec == SEC_AUTHORS && ! (p->flags & TERMP_NOSPLIT))
1087 p->flags |= TERMP_SPLIT;
1093 termp_ns_pre(DECL_ARGS)
1096 if ( ! (NODE_LINE & n->flags))
1097 p->flags |= TERMP_NOSPACE;
1102 termp_rs_pre(DECL_ARGS)
1105 if (SEC_SEE_ALSO != n->sec)
1107 if (n->type == ROFFT_BLOCK && n->prev != NULL)
1113 termp_ex_pre(DECL_ARGS)
1120 termp_nd_pre(DECL_ARGS)
1123 if (n->type == ROFFT_BODY)
1124 term_word(p, "\\(en");
1129 termp_bl_pre(DECL_ARGS)
1132 return n->type != ROFFT_HEAD;
1136 termp_bl_post(DECL_ARGS)
1139 if (n->type == ROFFT_BLOCK)
1144 termp_xr_pre(DECL_ARGS)
1147 if (NULL == (n = n->child))
1150 assert(n->type == ROFFT_TEXT);
1151 term_word(p, n->string);
1153 if (NULL == (n = n->next))
1156 p->flags |= TERMP_NOSPACE;
1158 p->flags |= TERMP_NOSPACE;
1160 assert(n->type == ROFFT_TEXT);
1161 term_word(p, n->string);
1163 p->flags |= TERMP_NOSPACE;
1170 * This decides how to assert whitespace before any of the SYNOPSIS set
1171 * of macros (which, as in the case of Ft/Fo and Ft/Fn, may contain
1175 synopsis_pre(struct termp *p, const struct roff_node *n)
1178 * Obviously, if we're not in a SYNOPSIS or no prior macros
1179 * exist, do nothing.
1181 if (NULL == n->prev || ! (NODE_SYNPRETTY & n->flags))
1185 * If we're the second in a pair of like elements, emit our
1186 * newline and return. UNLESS we're `Fo', `Fn', `Fn', in which
1187 * case we soldier on.
1189 if (n->prev->tok == n->tok &&
1190 MDOC_Ft != n->tok &&
1191 MDOC_Fo != n->tok &&
1192 MDOC_Fn != n->tok) {
1198 * If we're one of the SYNOPSIS set and non-like pair-wise after
1199 * another (or Fn/Fo, which we've let slip through) then assert
1200 * vertical space, else only newline and move on.
1202 switch (n->prev->tok) {
1211 if (MDOC_Fn != n->tok && MDOC_Fo != n->tok) {
1223 termp_vt_pre(DECL_ARGS)
1226 if (n->type == ROFFT_ELEM) {
1228 return termp_under_pre(p, pair, meta, n);
1229 } else if (n->type == ROFFT_BLOCK) {
1232 } else if (n->type == ROFFT_HEAD)
1235 return termp_under_pre(p, pair, meta, n);
1239 termp_bold_pre(DECL_ARGS)
1242 termp_tag_pre(p, pair, meta, n);
1243 term_fontpush(p, TERMFONT_BOLD);
1248 termp_fd_pre(DECL_ARGS)
1252 return termp_bold_pre(p, pair, meta, n);
1256 termp_fd_post(DECL_ARGS)
1263 termp_sh_pre(DECL_ARGS)
1269 * Vertical space before sections, except
1270 * when the previous section was empty.
1272 if (n->prev == NULL ||
1273 n->prev->tok != MDOC_Sh ||
1274 (n->prev->body != NULL &&
1275 n->prev->body->child != NULL))
1279 term_fontpush(p, TERMFONT_BOLD);
1282 p->offset = term_len(p, p->defindent);
1284 case SEC_DESCRIPTION:
1288 p->flags &= ~(TERMP_SPLIT|TERMP_NOSPLIT);
1301 termp_sh_post(DECL_ARGS)
1318 termp_lb_post(DECL_ARGS)
1321 if (SEC_LIBRARY == n->sec && NODE_LINE & n->flags)
1326 termp_d1_pre(DECL_ARGS)
1329 if (n->type != ROFFT_BLOCK)
1332 p->offset += term_len(p, p->defindent + 1);
1337 termp_ft_pre(DECL_ARGS)
1340 /* NB: NODE_LINE does not effect this! */
1342 term_fontpush(p, TERMFONT_UNDER);
1347 termp_fn_pre(DECL_ARGS)
1352 pretty = NODE_SYNPRETTY & n->flags;
1356 if (NULL == (n = n->child))
1360 rmargin = p->rmargin;
1361 p->rmargin = p->offset + term_len(p, 4);
1362 p->flags |= TERMP_NOBREAK | TERMP_BRIND | TERMP_HANG;
1365 assert(n->type == ROFFT_TEXT);
1366 term_fontpush(p, TERMFONT_BOLD);
1367 term_word(p, n->string);
1370 if (n->sec == SEC_DESCRIPTION || n->sec == SEC_CUSTOM)
1371 tag_put(n->string, ++fn_prio, p->line);
1375 p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND | TERMP_HANG);
1376 p->offset = p->rmargin;
1377 p->rmargin = rmargin;
1380 p->flags |= TERMP_NOSPACE;
1382 p->flags |= TERMP_NOSPACE;
1384 for (n = n->next; n; n = n->next) {
1385 assert(n->type == ROFFT_TEXT);
1386 term_fontpush(p, TERMFONT_UNDER);
1388 p->flags |= TERMP_NBRWORD;
1389 term_word(p, n->string);
1393 p->flags |= TERMP_NOSPACE;
1398 p->flags |= TERMP_NOSPACE;
1402 p->flags |= TERMP_NOSPACE;
1411 termp_fa_pre(DECL_ARGS)
1413 const struct roff_node *nn;
1415 if (n->parent->tok != MDOC_Fo) {
1416 term_fontpush(p, TERMFONT_UNDER);
1420 for (nn = n->child; nn; nn = nn->next) {
1421 term_fontpush(p, TERMFONT_UNDER);
1422 p->flags |= TERMP_NBRWORD;
1423 term_word(p, nn->string);
1426 if (nn->next || (n->next && n->next->tok == MDOC_Fa)) {
1427 p->flags |= TERMP_NOSPACE;
1436 termp_bd_pre(DECL_ARGS)
1438 size_t tabwidth, lm, len, rm, rmax;
1439 struct roff_node *nn;
1442 if (n->type == ROFFT_BLOCK) {
1443 print_bvspace(p, n, n);
1445 } else if (n->type == ROFFT_HEAD)
1448 /* Handle the -offset argument. */
1450 if (n->norm->Bd.offs == NULL ||
1451 ! strcmp(n->norm->Bd.offs, "left"))
1453 else if ( ! strcmp(n->norm->Bd.offs, "indent"))
1454 p->offset += term_len(p, p->defindent + 1);
1455 else if ( ! strcmp(n->norm->Bd.offs, "indent-two"))
1456 p->offset += term_len(p, (p->defindent + 1) * 2);
1458 offset = a2width(p, n->norm->Bd.offs);
1459 if (offset < 0 && (size_t)(-offset) > p->offset)
1461 else if (offset < SHRT_MAX)
1462 p->offset += offset;
1466 * If -ragged or -filled are specified, the block does nothing
1467 * but change the indentation. If -unfilled or -literal are
1468 * specified, text is printed exactly as entered in the display:
1469 * for macro lines, a newline is appended to the line. Blank
1470 * lines are allowed.
1473 if (DISP_literal != n->norm->Bd.type &&
1474 DISP_unfilled != n->norm->Bd.type &&
1475 DISP_centered != n->norm->Bd.type)
1478 tabwidth = p->tabwidth;
1479 if (DISP_literal == n->norm->Bd.type)
1480 p->tabwidth = term_len(p, 8);
1484 rmax = p->maxrmargin;
1485 p->rmargin = p->maxrmargin = TERM_MAXMARGIN;
1487 for (nn = n->child; nn; nn = nn->next) {
1488 if (DISP_centered == n->norm->Bd.type) {
1489 if (nn->type == ROFFT_TEXT) {
1490 len = term_strlen(p, nn->string);
1491 p->offset = len >= rm ? 0 :
1492 lm + len >= rm ? rm - len :
1493 (lm + rm - len) / 2;
1497 print_mdoc_node(p, pair, meta, nn);
1499 * If the printed node flushes its own line, then we
1500 * needn't do it here as well. This is hacky, but the
1501 * notion of selective eoln whitespace is pretty dumb
1502 * anyway, so don't sweat it.
1517 if (p->flags & TERMP_NONEWLINE ||
1518 (nn->next && ! (nn->next->flags & NODE_LINE)))
1521 p->flags |= TERMP_NOSPACE;
1524 p->tabwidth = tabwidth;
1526 p->maxrmargin = rmax;
1531 termp_bd_post(DECL_ARGS)
1535 if (n->type != ROFFT_BODY)
1539 rmax = p->maxrmargin;
1541 if (DISP_literal == n->norm->Bd.type ||
1542 DISP_unfilled == n->norm->Bd.type)
1543 p->rmargin = p->maxrmargin = TERM_MAXMARGIN;
1545 p->flags |= TERMP_NOSPACE;
1549 p->maxrmargin = rmax;
1553 termp_xx_pre(DECL_ARGS)
1555 if ((n->aux = p->flags & TERMP_PREKEEP) == 0)
1556 p->flags |= TERMP_PREKEEP;
1561 termp_xx_post(DECL_ARGS)
1564 p->flags &= ~(TERMP_KEEP | TERMP_PREKEEP);
1568 termp_pf_post(DECL_ARGS)
1571 if ( ! (n->next == NULL || n->next->flags & NODE_LINE))
1572 p->flags |= TERMP_NOSPACE;
1576 termp_ss_pre(DECL_ARGS)
1578 struct roff_node *nn;
1583 for (nn = n->prev; nn != NULL; nn = nn->prev)
1584 if ((nn->flags & NODE_NOPRT) == 0)
1590 term_fontpush(p, TERMFONT_BOLD);
1591 p->offset = term_len(p, (p->defindent+1)/2);
1594 p->offset = term_len(p, p->defindent);
1604 termp_ss_post(DECL_ARGS)
1607 if (n->type == ROFFT_HEAD || n->type == ROFFT_BODY)
1612 termp_cd_pre(DECL_ARGS)
1616 term_fontpush(p, TERMFONT_BOLD);
1621 termp_in_pre(DECL_ARGS)
1626 if (NODE_SYNPRETTY & n->flags && NODE_LINE & n->flags) {
1627 term_fontpush(p, TERMFONT_BOLD);
1628 term_word(p, "#include");
1632 term_fontpush(p, TERMFONT_UNDER);
1635 p->flags |= TERMP_NOSPACE;
1640 termp_in_post(DECL_ARGS)
1643 if (NODE_SYNPRETTY & n->flags)
1644 term_fontpush(p, TERMFONT_BOLD);
1646 p->flags |= TERMP_NOSPACE;
1649 if (NODE_SYNPRETTY & n->flags)
1654 termp_sp_pre(DECL_ARGS)
1662 if ( ! a2roffsu(n->child->string, &su, SCALE_VS))
1664 len = term_vspan(p, &su);
1682 for (i = 0; i < len; i++)
1689 termp_skip_pre(DECL_ARGS)
1696 termp_quote_pre(DECL_ARGS)
1699 if (n->type != ROFFT_BODY && n->type != ROFFT_ELEM)
1705 term_word(p, n->child != NULL && n->child->next == NULL &&
1706 n->child->tok == MDOC_Mt ? "<" : "\\(la");
1720 term_word(p, "\\(Lq");
1723 if (NULL == n->norm->Es ||
1724 NULL == n->norm->Es->child)
1726 term_word(p, n->norm->Es->child->string);
1740 term_word(p, "\\(oq");
1746 p->flags |= TERMP_NOSPACE;
1751 termp_quote_post(DECL_ARGS)
1754 if (n->type != ROFFT_BODY && n->type != ROFFT_ELEM)
1757 p->flags |= TERMP_NOSPACE;
1762 term_word(p, n->child != NULL && n->child->next == NULL &&
1763 n->child->tok == MDOC_Mt ? ">" : "\\(ra");
1777 term_word(p, "\\(Rq");
1780 if (n->norm->Es == NULL ||
1781 n->norm->Es->child == NULL ||
1782 n->norm->Es->child->next == NULL)
1783 p->flags &= ~TERMP_NOSPACE;
1785 term_word(p, n->norm->Es->child->next->string);
1799 term_word(p, "\\(cq");
1807 termp_eo_pre(DECL_ARGS)
1810 if (n->type != ROFFT_BODY)
1813 if (n->end == ENDBODY_NOT &&
1814 n->parent->head->child == NULL &&
1816 n->child->end != ENDBODY_NOT)
1817 term_word(p, "\\&");
1818 else if (n->end != ENDBODY_NOT ? n->child != NULL :
1819 n->parent->head->child != NULL && (n->child != NULL ||
1820 (n->parent->tail != NULL && n->parent->tail->child != NULL)))
1821 p->flags |= TERMP_NOSPACE;
1827 termp_eo_post(DECL_ARGS)
1831 if (n->type != ROFFT_BODY)
1834 if (n->end != ENDBODY_NOT) {
1835 p->flags &= ~TERMP_NOSPACE;
1839 body = n->child != NULL || n->parent->head->child != NULL;
1840 tail = n->parent->tail != NULL && n->parent->tail->child != NULL;
1843 p->flags |= TERMP_NOSPACE;
1844 else if ( ! (body || tail))
1845 term_word(p, "\\&");
1847 p->flags &= ~TERMP_NOSPACE;
1851 termp_fo_pre(DECL_ARGS)
1856 pretty = NODE_SYNPRETTY & n->flags;
1858 if (n->type == ROFFT_BLOCK) {
1861 } else if (n->type == ROFFT_BODY) {
1863 rmargin = p->rmargin;
1864 p->rmargin = p->offset + term_len(p, 4);
1865 p->flags |= TERMP_NOBREAK | TERMP_BRIND |
1868 p->flags |= TERMP_NOSPACE;
1870 p->flags |= TERMP_NOSPACE;
1873 p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND |
1875 p->offset = p->rmargin;
1876 p->rmargin = rmargin;
1881 if (NULL == n->child)
1884 /* XXX: we drop non-initial arguments as per groff. */
1886 assert(n->child->string);
1887 term_fontpush(p, TERMFONT_BOLD);
1888 term_word(p, n->child->string);
1893 termp_fo_post(DECL_ARGS)
1896 if (n->type != ROFFT_BODY)
1899 p->flags |= TERMP_NOSPACE;
1902 if (NODE_SYNPRETTY & n->flags) {
1903 p->flags |= TERMP_NOSPACE;
1910 termp_bf_pre(DECL_ARGS)
1913 if (n->type == ROFFT_HEAD)
1915 else if (n->type != ROFFT_BODY)
1918 if (FONT_Em == n->norm->Bf.font)
1919 term_fontpush(p, TERMFONT_UNDER);
1920 else if (FONT_Sy == n->norm->Bf.font)
1921 term_fontpush(p, TERMFONT_BOLD);
1923 term_fontpush(p, TERMFONT_NONE);
1929 termp_sm_pre(DECL_ARGS)
1932 if (NULL == n->child)
1933 p->flags ^= TERMP_NONOSPACE;
1934 else if (0 == strcmp("on", n->child->string))
1935 p->flags &= ~TERMP_NONOSPACE;
1937 p->flags |= TERMP_NONOSPACE;
1939 if (p->col && ! (TERMP_NONOSPACE & p->flags))
1940 p->flags &= ~TERMP_NOSPACE;
1946 termp_ap_pre(DECL_ARGS)
1949 p->flags |= TERMP_NOSPACE;
1951 p->flags |= TERMP_NOSPACE;
1956 termp____post(DECL_ARGS)
1960 * Handle lists of authors. In general, print each followed by
1961 * a comma. Don't print the comma if there are only two
1964 if (MDOC__A == n->tok && n->next && MDOC__A == n->next->tok)
1965 if (NULL == n->next->next || MDOC__A != n->next->next->tok)
1966 if (NULL == n->prev || MDOC__A != n->prev->tok)
1971 if (NULL == n->parent || MDOC_Rs != n->parent->tok)
1974 p->flags |= TERMP_NOSPACE;
1975 if (NULL == n->next) {
1977 p->flags |= TERMP_SENTENCE;
1983 termp_li_pre(DECL_ARGS)
1986 termp_tag_pre(p, pair, meta, n);
1987 term_fontpush(p, TERMFONT_NONE);
1992 termp_lk_pre(DECL_ARGS)
1994 const struct roff_node *link, *descr;
1996 if (NULL == (link = n->child))
1999 if (NULL != (descr = link->next)) {
2000 term_fontpush(p, TERMFONT_UNDER);
2001 while (NULL != descr) {
2002 term_word(p, descr->string);
2003 descr = descr->next;
2005 p->flags |= TERMP_NOSPACE;
2010 term_fontpush(p, TERMFONT_BOLD);
2011 term_word(p, link->string);
2018 termp_bk_pre(DECL_ARGS)
2027 if (n->parent->args != NULL || n->prev->child == NULL)
2028 p->flags |= TERMP_PREKEEP;
2038 termp_bk_post(DECL_ARGS)
2041 if (n->type == ROFFT_BODY)
2042 p->flags &= ~(TERMP_KEEP | TERMP_PREKEEP);
2046 termp__t_post(DECL_ARGS)
2050 * If we're in an `Rs' and there's a journal present, then quote
2051 * us instead of underlining us (for disambiguation).
2053 if (n->parent && MDOC_Rs == n->parent->tok &&
2054 n->parent->norm->Rs.quote_T)
2055 termp_quote_post(p, pair, meta, n);
2057 termp____post(p, pair, meta, n);
2061 termp__t_pre(DECL_ARGS)
2065 * If we're in an `Rs' and there's a journal present, then quote
2066 * us instead of underlining us (for disambiguation).
2068 if (n->parent && MDOC_Rs == n->parent->tok &&
2069 n->parent->norm->Rs.quote_T)
2070 return termp_quote_pre(p, pair, meta, n);
2072 term_fontpush(p, TERMFONT_UNDER);
2077 termp_under_pre(DECL_ARGS)
2080 term_fontpush(p, TERMFONT_UNDER);
2085 termp_em_pre(DECL_ARGS)
2087 if (n->child != NULL &&
2088 n->child->type == ROFFT_TEXT)
2089 tag_put(n->child->string, 0, p->line);
2090 term_fontpush(p, TERMFONT_UNDER);
2095 termp_sy_pre(DECL_ARGS)
2097 if (n->child != NULL &&
2098 n->child->type == ROFFT_TEXT)
2099 tag_put(n->child->string, 0, p->line);
2100 term_fontpush(p, TERMFONT_BOLD);
2105 termp_er_pre(DECL_ARGS)
2108 if (n->sec == SEC_ERRORS &&
2109 (n->parent->tok == MDOC_It ||
2110 (n->parent->tok == MDOC_Bq &&
2111 n->parent->parent->parent->tok == MDOC_It)))
2112 tag_put(n->child->string, 1, p->line);
2117 termp_tag_pre(DECL_ARGS)
2120 if (n->child != NULL &&
2121 n->child->type == ROFFT_TEXT &&
2123 (n->prev->type == ROFFT_TEXT &&
2124 strcmp(n->prev->string, "|") == 0)) &&
2125 (n->parent->tok == MDOC_It ||
2126 (n->parent->tok == MDOC_Xo &&
2127 n->parent->parent->prev == NULL &&
2128 n->parent->parent->parent->tok == MDOC_It)))
2129 tag_put(n->child->string, 1, p->line);