]> git.cameronkatri.com Git - mandoc.git/blob - mdoc_term.c
Simplify by deleting the "lastline" member of struct mdoc_node.
[mandoc.git] / mdoc_term.c
1 /* $Id: mdoc_term.c,v 1.307 2015/02/05 00:14:13 schwarze Exp $ */
2 /*
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>
6 *
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.
10 *
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.
18 */
19 #include "config.h"
20
21 #include <sys/types.h>
22
23 #include <assert.h>
24 #include <ctype.h>
25 #include <limits.h>
26 #include <stdint.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "mandoc.h"
32 #include "mandoc_aux.h"
33 #include "out.h"
34 #include "term.h"
35 #include "mdoc.h"
36 #include "main.h"
37
38 struct termpair {
39 struct termpair *ppair;
40 int count;
41 };
42
43 #define DECL_ARGS struct termp *p, \
44 struct termpair *pair, \
45 const struct mdoc_meta *meta, \
46 struct mdoc_node *n
47
48 struct termact {
49 int (*pre)(DECL_ARGS);
50 void (*post)(DECL_ARGS);
51 };
52
53 static int a2width(const struct termp *, const char *);
54
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 *);
64
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);
81
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);
124
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 */
249 };
250
251
252 void
253 terminal_mdoc(void *arg, const struct mdoc *mdoc)
254 {
255 const struct mdoc_meta *meta;
256 struct mdoc_node *n;
257 struct termp *p;
258
259 p = (struct termp *)arg;
260
261 p->overstep = 0;
262 p->rmargin = p->maxrmargin = p->defrmargin;
263 p->tabwidth = term_len(p, 5);
264
265 n = mdoc_node(mdoc)->child;
266 meta = mdoc_meta(mdoc);
267
268 if (p->synopsisonly) {
269 while (n != NULL) {
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);
274 term_newln(p);
275 break;
276 }
277 n = n->next;
278 }
279 } else {
280 if (p->defindent == 0)
281 p->defindent = 5;
282 term_begin(p, print_mdoc_head, print_mdoc_foot, meta);
283 if (n != NULL) {
284 if (n->tok != MDOC_Sh)
285 term_vspace(p);
286 print_mdoc_nodelist(p, NULL, meta, n);
287 }
288 term_end(p);
289 }
290 }
291
292 static void
293 print_mdoc_nodelist(DECL_ARGS)
294 {
295
296 while (n != NULL) {
297 print_mdoc_node(p, pair, meta, n);
298 n = n->next;
299 }
300 }
301
302 static void
303 print_mdoc_node(DECL_ARGS)
304 {
305 int chld;
306 struct termpair npair;
307 size_t offset, rmargin;
308
309 chld = 1;
310 offset = p->offset;
311 rmargin = p->rmargin;
312 n->prev_font = p->fonti;
313
314 memset(&npair, 0, sizeof(struct termpair));
315 npair.ppair = pair;
316
317 /*
318 * Keeps only work until the end of a line. If a keep was
319 * invoked in a prior line, revert it to PREKEEP.
320 */
321
322 if (p->flags & TERMP_KEEP && n->flags & MDOC_LINE) {
323 p->flags &= ~TERMP_KEEP;
324 p->flags |= TERMP_PREKEEP;
325 }
326
327 /*
328 * After the keep flags have been set up, we may now
329 * produce output. Note that some pre-handlers do so.
330 */
331
332 switch (n->type) {
333 case MDOC_TEXT:
334 if (' ' == *n->string && MDOC_LINE & n->flags)
335 term_newln(p);
336 if (MDOC_DELIMC & n->flags)
337 p->flags |= TERMP_NOSPACE;
338 term_word(p, n->string);
339 if (MDOC_DELIMO & n->flags)
340 p->flags |= TERMP_NOSPACE;
341 break;
342 case MDOC_EQN:
343 if ( ! (n->flags & MDOC_LINE))
344 p->flags |= TERMP_NOSPACE;
345 term_eqn(p, n->eqn);
346 if (n->next != NULL && ! (n->next->flags & MDOC_LINE))
347 p->flags |= TERMP_NOSPACE;
348 break;
349 case MDOC_TBL:
350 term_tbl(p, n->span);
351 break;
352 default:
353 if (termacts[n->tok].pre &&
354 (n->end == ENDBODY_NOT || n->nchild))
355 chld = (*termacts[n->tok].pre)
356 (p, &npair, meta, n);
357 break;
358 }
359
360 if (chld && n->child)
361 print_mdoc_nodelist(p, &npair, meta, n->child);
362
363 term_fontpopq(p,
364 (ENDBODY_NOT == n->end ? n : n->pending)->prev_font);
365
366 switch (n->type) {
367 case MDOC_TEXT:
368 break;
369 case MDOC_TBL:
370 break;
371 case MDOC_EQN:
372 break;
373 default:
374 if ( ! termacts[n->tok].post || MDOC_ENDED & n->flags)
375 break;
376 (void)(*termacts[n->tok].post)(p, &npair, meta, n);
377
378 /*
379 * Explicit end tokens not only call the post
380 * handler, but also tell the respective block
381 * that it must not call the post handler again.
382 */
383 if (ENDBODY_NOT != n->end)
384 n->pending->flags |= MDOC_ENDED;
385
386 /*
387 * End of line terminating an implicit block
388 * while an explicit block is still open.
389 * Continue the explicit block without spacing.
390 */
391 if (ENDBODY_NOSPACE == n->end)
392 p->flags |= TERMP_NOSPACE;
393 break;
394 }
395
396 if (MDOC_EOS & n->flags)
397 p->flags |= TERMP_SENTENCE;
398
399 if (MDOC_ll != n->tok) {
400 p->offset = offset;
401 p->rmargin = rmargin;
402 }
403 }
404
405 static void
406 print_mdoc_foot(struct termp *p, const void *arg)
407 {
408 const struct mdoc_meta *meta;
409 size_t sz;
410
411 meta = (const struct mdoc_meta *)arg;
412
413 term_fontrepl(p, TERMFONT_NONE);
414
415 /*
416 * Output the footer in new-groff style, that is, three columns
417 * with the middle being the manual date and flanking columns
418 * being the operating system:
419 *
420 * SYSTEM DATE SYSTEM
421 */
422
423 term_vspace(p);
424
425 p->offset = 0;
426 sz = term_strlen(p, meta->date);
427 p->rmargin = p->maxrmargin > sz ?
428 (p->maxrmargin + term_len(p, 1) - sz) / 2 : 0;
429 p->trailspace = 1;
430 p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
431
432 term_word(p, meta->os);
433 term_flushln(p);
434
435 p->offset = p->rmargin;
436 sz = term_strlen(p, meta->os);
437 p->rmargin = p->maxrmargin > sz ? p->maxrmargin - sz : 0;
438 p->flags |= TERMP_NOSPACE;
439
440 term_word(p, meta->date);
441 term_flushln(p);
442
443 p->offset = p->rmargin;
444 p->rmargin = p->maxrmargin;
445 p->trailspace = 0;
446 p->flags &= ~TERMP_NOBREAK;
447 p->flags |= TERMP_NOSPACE;
448
449 term_word(p, meta->os);
450 term_flushln(p);
451
452 p->offset = 0;
453 p->rmargin = p->maxrmargin;
454 p->flags = 0;
455 }
456
457 static void
458 print_mdoc_head(struct termp *p, const void *arg)
459 {
460 const struct mdoc_meta *meta;
461 char *volume, *title;
462 size_t vollen, titlen;
463
464 meta = (const struct mdoc_meta *)arg;
465
466 /*
467 * The header is strange. It has three components, which are
468 * really two with the first duplicated. It goes like this:
469 *
470 * IDENTIFIER TITLE IDENTIFIER
471 *
472 * The IDENTIFIER is NAME(SECTION), which is the command-name
473 * (if given, or "unknown" if not) followed by the manual page
474 * section. These are given in `Dt'. The TITLE is a free-form
475 * string depending on the manual volume. If not specified, it
476 * switches on the manual section.
477 */
478
479 assert(meta->vol);
480 if (NULL == meta->arch)
481 volume = mandoc_strdup(meta->vol);
482 else
483 mandoc_asprintf(&volume, "%s (%s)",
484 meta->vol, meta->arch);
485 vollen = term_strlen(p, volume);
486
487 if (NULL == meta->msec)
488 title = mandoc_strdup(meta->title);
489 else
490 mandoc_asprintf(&title, "%s(%s)",
491 meta->title, meta->msec);
492 titlen = term_strlen(p, title);
493
494 p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
495 p->trailspace = 1;
496 p->offset = 0;
497 p->rmargin = 2 * (titlen+1) + vollen < p->maxrmargin ?
498 (p->maxrmargin - vollen + term_len(p, 1)) / 2 :
499 vollen < p->maxrmargin ? p->maxrmargin - vollen : 0;
500
501 term_word(p, title);
502 term_flushln(p);
503
504 p->flags |= TERMP_NOSPACE;
505 p->offset = p->rmargin;
506 p->rmargin = p->offset + vollen + titlen < p->maxrmargin ?
507 p->maxrmargin - titlen : p->maxrmargin;
508
509 term_word(p, volume);
510 term_flushln(p);
511
512 p->flags &= ~TERMP_NOBREAK;
513 p->trailspace = 0;
514 if (p->rmargin + titlen <= p->maxrmargin) {
515 p->flags |= TERMP_NOSPACE;
516 p->offset = p->rmargin;
517 p->rmargin = p->maxrmargin;
518 term_word(p, title);
519 term_flushln(p);
520 }
521
522 p->flags &= ~TERMP_NOSPACE;
523 p->offset = 0;
524 p->rmargin = p->maxrmargin;
525 free(title);
526 free(volume);
527 }
528
529 static int
530 a2width(const struct termp *p, const char *v)
531 {
532 struct roffsu su;
533
534 if (a2roffsu(v, &su, SCALE_MAX) < 2) {
535 SCALE_HS_INIT(&su, term_strlen(p, v));
536 su.scale /= term_strlen(p, "0");
537 }
538 return(term_hspan(p, &su));
539 }
540
541 /*
542 * Determine how much space to print out before block elements of `It'
543 * (and thus `Bl') and `Bd'. And then go ahead and print that space,
544 * too.
545 */
546 static void
547 print_bvspace(struct termp *p,
548 const struct mdoc_node *bl,
549 const struct mdoc_node *n)
550 {
551 const struct mdoc_node *nn;
552
553 assert(n);
554
555 term_newln(p);
556
557 if (MDOC_Bd == bl->tok && bl->norm->Bd.comp)
558 return;
559 if (MDOC_Bl == bl->tok && bl->norm->Bl.comp)
560 return;
561
562 /* Do not vspace directly after Ss/Sh. */
563
564 nn = n;
565 while (nn->prev == NULL) {
566 do {
567 nn = nn->parent;
568 if (nn->type == MDOC_ROOT)
569 return;
570 } while (nn->type != MDOC_BLOCK);
571 if (nn->tok == MDOC_Sh || nn->tok == MDOC_Ss)
572 return;
573 if (nn->tok == MDOC_It &&
574 nn->parent->parent->norm->Bl.type != LIST_item)
575 break;
576 }
577
578 /* A `-column' does not assert vspace within the list. */
579
580 if (MDOC_Bl == bl->tok && LIST_column == bl->norm->Bl.type)
581 if (n->prev && MDOC_It == n->prev->tok)
582 return;
583
584 /* A `-diag' without body does not vspace. */
585
586 if (MDOC_Bl == bl->tok && LIST_diag == bl->norm->Bl.type)
587 if (n->prev && MDOC_It == n->prev->tok) {
588 assert(n->prev->body);
589 if (NULL == n->prev->body->child)
590 return;
591 }
592
593 term_vspace(p);
594 }
595
596
597 static int
598 termp_ll_pre(DECL_ARGS)
599 {
600
601 term_setwidth(p, n->nchild ? n->child->string : NULL);
602 return(0);
603 }
604
605 static int
606 termp_it_pre(DECL_ARGS)
607 {
608 char buf[24];
609 const struct mdoc_node *bl, *nn;
610 size_t ncols, dcol;
611 int i, offset, width;
612 enum mdoc_list type;
613
614 if (MDOC_BLOCK == n->type) {
615 print_bvspace(p, n->parent->parent, n);
616 return(1);
617 }
618
619 bl = n->parent->parent->parent;
620 type = bl->norm->Bl.type;
621
622 /*
623 * Defaults for specific list types.
624 */
625
626 switch (type) {
627 case LIST_bullet:
628 /* FALLTHROUGH */
629 case LIST_dash:
630 /* FALLTHROUGH */
631 case LIST_hyphen:
632 /* FALLTHROUGH */
633 case LIST_enum:
634 width = term_len(p, 2);
635 break;
636 case LIST_hang:
637 width = term_len(p, 8);
638 break;
639 case LIST_column:
640 /* FALLTHROUGH */
641 case LIST_tag:
642 width = term_len(p, 10);
643 break;
644 default:
645 width = 0;
646 break;
647 }
648 offset = 0;
649
650 /*
651 * First calculate width and offset. This is pretty easy unless
652 * we're a -column list, in which case all prior columns must
653 * be accounted for.
654 */
655
656 if (bl->norm->Bl.offs != NULL) {
657 offset = a2width(p, bl->norm->Bl.offs);
658 if (offset < 0 && (size_t)(-offset) > p->offset)
659 offset = -p->offset;
660 else if (offset > SHRT_MAX)
661 offset = 0;
662 }
663
664 switch (type) {
665 case LIST_column:
666 if (MDOC_HEAD == n->type)
667 break;
668
669 /*
670 * Imitate groff's column handling:
671 * - For each earlier column, add its width.
672 * - For less than 5 columns, add four more blanks per
673 * column.
674 * - For exactly 5 columns, add three more blank per
675 * column.
676 * - For more than 5 columns, add only one column.
677 */
678 ncols = bl->norm->Bl.ncols;
679 dcol = ncols < 5 ? term_len(p, 4) :
680 ncols == 5 ? term_len(p, 3) : term_len(p, 1);
681
682 /*
683 * Calculate the offset by applying all prior MDOC_BODY,
684 * so we stop at the MDOC_HEAD (NULL == nn->prev).
685 */
686
687 for (i = 0, nn = n->prev;
688 nn->prev && i < (int)ncols;
689 nn = nn->prev, i++)
690 offset += dcol + a2width(p,
691 bl->norm->Bl.cols[i]);
692
693 /*
694 * When exceeding the declared number of columns, leave
695 * the remaining widths at 0. This will later be
696 * adjusted to the default width of 10, or, for the last
697 * column, stretched to the right margin.
698 */
699 if (i >= (int)ncols)
700 break;
701
702 /*
703 * Use the declared column widths, extended as explained
704 * in the preceding paragraph.
705 */
706 width = a2width(p, bl->norm->Bl.cols[i]) + dcol;
707 break;
708 default:
709 if (NULL == bl->norm->Bl.width)
710 break;
711
712 /*
713 * Note: buffer the width by 2, which is groff's magic
714 * number for buffering single arguments. See the above
715 * handling for column for how this changes.
716 */
717 width = a2width(p, bl->norm->Bl.width) + term_len(p, 2);
718 if (width < 0 && (size_t)(-width) > p->offset)
719 width = -p->offset;
720 else if (width > SHRT_MAX)
721 width = 0;
722 break;
723 }
724
725 /*
726 * Whitespace control. Inset bodies need an initial space,
727 * while diagonal bodies need two.
728 */
729
730 p->flags |= TERMP_NOSPACE;
731
732 switch (type) {
733 case LIST_diag:
734 if (MDOC_BODY == n->type)
735 term_word(p, "\\ \\ ");
736 break;
737 case LIST_inset:
738 if (MDOC_BODY == n->type && n->parent->head->nchild)
739 term_word(p, "\\ ");
740 break;
741 default:
742 break;
743 }
744
745 p->flags |= TERMP_NOSPACE;
746
747 switch (type) {
748 case LIST_diag:
749 if (MDOC_HEAD == n->type)
750 term_fontpush(p, TERMFONT_BOLD);
751 break;
752 default:
753 break;
754 }
755
756 /*
757 * Pad and break control. This is the tricky part. These flags
758 * are documented in term_flushln() in term.c. Note that we're
759 * going to unset all of these flags in termp_it_post() when we
760 * exit.
761 */
762
763 switch (type) {
764 case LIST_enum:
765 /*
766 * Weird special case.
767 * Some very narrow lists actually hang.
768 */
769 /* FALLTHROUGH */
770 case LIST_bullet:
771 /* FALLTHROUGH */
772 case LIST_dash:
773 /* FALLTHROUGH */
774 case LIST_hyphen:
775 if (width <= (int)term_len(p, 2))
776 p->flags |= TERMP_HANG;
777 if (MDOC_HEAD != n->type)
778 break;
779 p->flags |= TERMP_NOBREAK;
780 p->trailspace = 1;
781 break;
782 case LIST_hang:
783 if (MDOC_HEAD != n->type)
784 break;
785
786 /*
787 * This is ugly. If `-hang' is specified and the body
788 * is a `Bl' or `Bd', then we want basically to nullify
789 * the "overstep" effect in term_flushln() and treat
790 * this as a `-ohang' list instead.
791 */
792 if (NULL != n->next &&
793 NULL != n->next->child &&
794 (MDOC_Bl == n->next->child->tok ||
795 MDOC_Bd == n->next->child->tok))
796 break;
797
798 p->flags |= TERMP_NOBREAK | TERMP_BRIND | TERMP_HANG;
799 p->trailspace = 1;
800 break;
801 case LIST_tag:
802 if (MDOC_HEAD != n->type)
803 break;
804
805 p->flags |= TERMP_NOBREAK | TERMP_BRIND;
806 p->trailspace = 2;
807
808 if (NULL == n->next || NULL == n->next->child)
809 p->flags |= TERMP_DANGLE;
810 break;
811 case LIST_column:
812 if (MDOC_HEAD == n->type)
813 break;
814
815 if (NULL == n->next) {
816 p->flags &= ~TERMP_NOBREAK;
817 p->trailspace = 0;
818 } else {
819 p->flags |= TERMP_NOBREAK;
820 p->trailspace = 1;
821 }
822
823 break;
824 case LIST_diag:
825 if (MDOC_HEAD != n->type)
826 break;
827 p->flags |= TERMP_NOBREAK | TERMP_BRIND;
828 p->trailspace = 1;
829 break;
830 default:
831 break;
832 }
833
834 /*
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.
838 */
839
840 p->offset += offset;
841
842 switch (type) {
843 case LIST_hang:
844 /*
845 * Same stipulation as above, regarding `-hang'. We
846 * don't want to recalculate rmargin and offsets when
847 * using `Bd' or `Bl' within `-hang' overstep lists.
848 */
849 if (MDOC_HEAD == n->type &&
850 NULL != n->next &&
851 NULL != n->next->child &&
852 (MDOC_Bl == n->next->child->tok ||
853 MDOC_Bd == n->next->child->tok))
854 break;
855 /* FALLTHROUGH */
856 case LIST_bullet:
857 /* FALLTHROUGH */
858 case LIST_dash:
859 /* FALLTHROUGH */
860 case LIST_enum:
861 /* FALLTHROUGH */
862 case LIST_hyphen:
863 /* FALLTHROUGH */
864 case LIST_tag:
865 if (MDOC_HEAD == n->type)
866 p->rmargin = p->offset + width;
867 else
868 p->offset += width;
869 break;
870 case LIST_column:
871 assert(width);
872 p->rmargin = p->offset + width;
873 /*
874 * XXX - this behaviour is not documented: the
875 * right-most column is filled to the right margin.
876 */
877 if (MDOC_HEAD == n->type)
878 break;
879 if (NULL == n->next && p->rmargin < p->maxrmargin)
880 p->rmargin = p->maxrmargin;
881 break;
882 default:
883 break;
884 }
885
886 /*
887 * The dash, hyphen, bullet and enum lists all have a special
888 * HEAD character (temporarily bold, in some cases).
889 */
890
891 if (MDOC_HEAD == n->type)
892 switch (type) {
893 case LIST_bullet:
894 term_fontpush(p, TERMFONT_BOLD);
895 term_word(p, "\\[bu]");
896 term_fontpop(p);
897 break;
898 case LIST_dash:
899 /* FALLTHROUGH */
900 case LIST_hyphen:
901 term_fontpush(p, TERMFONT_BOLD);
902 term_word(p, "\\(hy");
903 term_fontpop(p);
904 break;
905 case LIST_enum:
906 (pair->ppair->ppair->count)++;
907 (void)snprintf(buf, sizeof(buf), "%d.",
908 pair->ppair->ppair->count);
909 term_word(p, buf);
910 break;
911 default:
912 break;
913 }
914
915 /*
916 * If we're not going to process our children, indicate so here.
917 */
918
919 switch (type) {
920 case LIST_bullet:
921 /* FALLTHROUGH */
922 case LIST_item:
923 /* FALLTHROUGH */
924 case LIST_dash:
925 /* FALLTHROUGH */
926 case LIST_hyphen:
927 /* FALLTHROUGH */
928 case LIST_enum:
929 if (MDOC_HEAD == n->type)
930 return(0);
931 break;
932 case LIST_column:
933 if (MDOC_HEAD == n->type)
934 return(0);
935 break;
936 default:
937 break;
938 }
939
940 return(1);
941 }
942
943 static void
944 termp_it_post(DECL_ARGS)
945 {
946 enum mdoc_list type;
947
948 if (MDOC_BLOCK == n->type)
949 return;
950
951 type = n->parent->parent->parent->norm->Bl.type;
952
953 switch (type) {
954 case LIST_item:
955 /* FALLTHROUGH */
956 case LIST_diag:
957 /* FALLTHROUGH */
958 case LIST_inset:
959 if (MDOC_BODY == n->type)
960 term_newln(p);
961 break;
962 case LIST_column:
963 if (MDOC_BODY == n->type)
964 term_flushln(p);
965 break;
966 default:
967 term_newln(p);
968 break;
969 }
970
971 /*
972 * Now that our output is flushed, we can reset our tags. Since
973 * only `It' sets these flags, we're free to assume that nobody
974 * has munged them in the meanwhile.
975 */
976
977 p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND |
978 TERMP_DANGLE | TERMP_HANG);
979 p->trailspace = 0;
980 }
981
982 static int
983 termp_nm_pre(DECL_ARGS)
984 {
985 const char *cp;
986
987 if (MDOC_BLOCK == n->type) {
988 p->flags |= TERMP_PREKEEP;
989 return(1);
990 }
991
992 if (MDOC_BODY == n->type) {
993 if (NULL == n->child)
994 return(0);
995 p->flags |= TERMP_NOSPACE;
996 cp = NULL;
997 if (n->prev->child != NULL)
998 cp = n->prev->child->string;
999 if (cp == NULL)
1000 cp = meta->name;
1001 if (cp == NULL)
1002 p->offset += term_len(p, 6);
1003 else
1004 p->offset += term_len(p, 1) + term_strlen(p, cp);
1005 return(1);
1006 }
1007
1008 if (NULL == n->child && NULL == meta->name)
1009 return(0);
1010
1011 if (MDOC_HEAD == n->type)
1012 synopsis_pre(p, n->parent);
1013
1014 if (MDOC_HEAD == n->type &&
1015 NULL != n->next && NULL != n->next->child) {
1016 p->flags |= TERMP_NOSPACE | TERMP_NOBREAK | TERMP_BRIND;
1017 p->trailspace = 1;
1018 p->rmargin = p->offset + term_len(p, 1);
1019 if (NULL == n->child) {
1020 p->rmargin += term_strlen(p, meta->name);
1021 } else if (MDOC_TEXT == n->child->type) {
1022 p->rmargin += term_strlen(p, n->child->string);
1023 if (n->child->next)
1024 p->flags |= TERMP_HANG;
1025 } else {
1026 p->rmargin += term_len(p, 5);
1027 p->flags |= TERMP_HANG;
1028 }
1029 }
1030
1031 term_fontpush(p, TERMFONT_BOLD);
1032 if (NULL == n->child)
1033 term_word(p, meta->name);
1034 return(1);
1035 }
1036
1037 static void
1038 termp_nm_post(DECL_ARGS)
1039 {
1040
1041 if (MDOC_BLOCK == n->type) {
1042 p->flags &= ~(TERMP_KEEP | TERMP_PREKEEP);
1043 } else if (MDOC_HEAD == n->type &&
1044 NULL != n->next && NULL != n->next->child) {
1045 term_flushln(p);
1046 p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND | TERMP_HANG);
1047 p->trailspace = 0;
1048 } else if (MDOC_BODY == n->type && n->child)
1049 term_flushln(p);
1050 }
1051
1052 static int
1053 termp_fl_pre(DECL_ARGS)
1054 {
1055
1056 term_fontpush(p, TERMFONT_BOLD);
1057 term_word(p, "\\-");
1058
1059 if ( ! (n->nchild == 0 &&
1060 (n->next == NULL ||
1061 n->next->type == MDOC_TEXT ||
1062 n->next->flags & MDOC_LINE)))
1063 p->flags |= TERMP_NOSPACE;
1064
1065 return(1);
1066 }
1067
1068 static int
1069 termp__a_pre(DECL_ARGS)
1070 {
1071
1072 if (n->prev && MDOC__A == n->prev->tok)
1073 if (NULL == n->next || MDOC__A != n->next->tok)
1074 term_word(p, "and");
1075
1076 return(1);
1077 }
1078
1079 static int
1080 termp_an_pre(DECL_ARGS)
1081 {
1082
1083 if (n->norm->An.auth == AUTH_split) {
1084 p->flags &= ~TERMP_NOSPLIT;
1085 p->flags |= TERMP_SPLIT;
1086 return(0);
1087 }
1088 if (n->norm->An.auth == AUTH_nosplit) {
1089 p->flags &= ~TERMP_SPLIT;
1090 p->flags |= TERMP_NOSPLIT;
1091 return(0);
1092 }
1093
1094 if (n->child == NULL)
1095 return(0);
1096
1097 if (p->flags & TERMP_SPLIT)
1098 term_newln(p);
1099
1100 if (n->sec == SEC_AUTHORS && ! (p->flags & TERMP_NOSPLIT))
1101 p->flags |= TERMP_SPLIT;
1102
1103 return(1);
1104 }
1105
1106 static int
1107 termp_ns_pre(DECL_ARGS)
1108 {
1109
1110 if ( ! (MDOC_LINE & n->flags))
1111 p->flags |= TERMP_NOSPACE;
1112 return(1);
1113 }
1114
1115 static int
1116 termp_rs_pre(DECL_ARGS)
1117 {
1118
1119 if (SEC_SEE_ALSO != n->sec)
1120 return(1);
1121 if (MDOC_BLOCK == n->type && n->prev)
1122 term_vspace(p);
1123 return(1);
1124 }
1125
1126 static int
1127 termp_rv_pre(DECL_ARGS)
1128 {
1129 int nchild;
1130
1131 term_newln(p);
1132
1133 nchild = n->nchild;
1134 if (nchild > 0) {
1135 term_word(p, "The");
1136
1137 for (n = n->child; n; n = n->next) {
1138 term_fontpush(p, TERMFONT_BOLD);
1139 term_word(p, n->string);
1140 term_fontpop(p);
1141
1142 p->flags |= TERMP_NOSPACE;
1143 term_word(p, "()");
1144
1145 if (n->next == NULL)
1146 continue;
1147
1148 if (nchild > 2) {
1149 p->flags |= TERMP_NOSPACE;
1150 term_word(p, ",");
1151 }
1152 if (n->next->next == NULL)
1153 term_word(p, "and");
1154 }
1155
1156 if (nchild > 1)
1157 term_word(p, "functions return");
1158 else
1159 term_word(p, "function returns");
1160
1161 term_word(p, "the value\\~0 if successful;");
1162 } else
1163 term_word(p, "Upon successful completion,"
1164 " the value\\~0 is returned;");
1165
1166 term_word(p, "otherwise the value\\~\\-1 is returned"
1167 " and the global variable");
1168
1169 term_fontpush(p, TERMFONT_UNDER);
1170 term_word(p, "errno");
1171 term_fontpop(p);
1172
1173 term_word(p, "is set to indicate the error.");
1174 p->flags |= TERMP_SENTENCE;
1175
1176 return(0);
1177 }
1178
1179 static int
1180 termp_ex_pre(DECL_ARGS)
1181 {
1182 int nchild;
1183
1184 term_newln(p);
1185 term_word(p, "The");
1186
1187 nchild = n->nchild;
1188 for (n = n->child; n; n = n->next) {
1189 term_fontpush(p, TERMFONT_BOLD);
1190 term_word(p, n->string);
1191 term_fontpop(p);
1192
1193 if (nchild > 2 && n->next) {
1194 p->flags |= TERMP_NOSPACE;
1195 term_word(p, ",");
1196 }
1197
1198 if (n->next && NULL == n->next->next)
1199 term_word(p, "and");
1200 }
1201
1202 if (nchild > 1)
1203 term_word(p, "utilities exit\\~0");
1204 else
1205 term_word(p, "utility exits\\~0");
1206
1207 term_word(p, "on success, and\\~>0 if an error occurs.");
1208
1209 p->flags |= TERMP_SENTENCE;
1210 return(0);
1211 }
1212
1213 static int
1214 termp_nd_pre(DECL_ARGS)
1215 {
1216
1217 if (n->type == MDOC_BODY)
1218 term_word(p, "\\(en");
1219 return(1);
1220 }
1221
1222 static int
1223 termp_bl_pre(DECL_ARGS)
1224 {
1225
1226 return(MDOC_HEAD != n->type);
1227 }
1228
1229 static void
1230 termp_bl_post(DECL_ARGS)
1231 {
1232
1233 if (MDOC_BLOCK == n->type)
1234 term_newln(p);
1235 }
1236
1237 static int
1238 termp_xr_pre(DECL_ARGS)
1239 {
1240
1241 if (NULL == (n = n->child))
1242 return(0);
1243
1244 assert(MDOC_TEXT == n->type);
1245 term_word(p, n->string);
1246
1247 if (NULL == (n = n->next))
1248 return(0);
1249
1250 p->flags |= TERMP_NOSPACE;
1251 term_word(p, "(");
1252 p->flags |= TERMP_NOSPACE;
1253
1254 assert(MDOC_TEXT == n->type);
1255 term_word(p, n->string);
1256
1257 p->flags |= TERMP_NOSPACE;
1258 term_word(p, ")");
1259
1260 return(0);
1261 }
1262
1263 /*
1264 * This decides how to assert whitespace before any of the SYNOPSIS set
1265 * of macros (which, as in the case of Ft/Fo and Ft/Fn, may contain
1266 * macro combos).
1267 */
1268 static void
1269 synopsis_pre(struct termp *p, const struct mdoc_node *n)
1270 {
1271 /*
1272 * Obviously, if we're not in a SYNOPSIS or no prior macros
1273 * exist, do nothing.
1274 */
1275 if (NULL == n->prev || ! (MDOC_SYNPRETTY & n->flags))
1276 return;
1277
1278 /*
1279 * If we're the second in a pair of like elements, emit our
1280 * newline and return. UNLESS we're `Fo', `Fn', `Fn', in which
1281 * case we soldier on.
1282 */
1283 if (n->prev->tok == n->tok &&
1284 MDOC_Ft != n->tok &&
1285 MDOC_Fo != n->tok &&
1286 MDOC_Fn != n->tok) {
1287 term_newln(p);
1288 return;
1289 }
1290
1291 /*
1292 * If we're one of the SYNOPSIS set and non-like pair-wise after
1293 * another (or Fn/Fo, which we've let slip through) then assert
1294 * vertical space, else only newline and move on.
1295 */
1296 switch (n->prev->tok) {
1297 case MDOC_Fd:
1298 /* FALLTHROUGH */
1299 case MDOC_Fn:
1300 /* FALLTHROUGH */
1301 case MDOC_Fo:
1302 /* FALLTHROUGH */
1303 case MDOC_In:
1304 /* FALLTHROUGH */
1305 case MDOC_Vt:
1306 term_vspace(p);
1307 break;
1308 case MDOC_Ft:
1309 if (MDOC_Fn != n->tok && MDOC_Fo != n->tok) {
1310 term_vspace(p);
1311 break;
1312 }
1313 /* FALLTHROUGH */
1314 default:
1315 term_newln(p);
1316 break;
1317 }
1318 }
1319
1320 static int
1321 termp_vt_pre(DECL_ARGS)
1322 {
1323
1324 if (MDOC_ELEM == n->type) {
1325 synopsis_pre(p, n);
1326 return(termp_under_pre(p, pair, meta, n));
1327 } else if (MDOC_BLOCK == n->type) {
1328 synopsis_pre(p, n);
1329 return(1);
1330 } else if (MDOC_HEAD == n->type)
1331 return(0);
1332
1333 return(termp_under_pre(p, pair, meta, n));
1334 }
1335
1336 static int
1337 termp_bold_pre(DECL_ARGS)
1338 {
1339
1340 term_fontpush(p, TERMFONT_BOLD);
1341 return(1);
1342 }
1343
1344 static int
1345 termp_fd_pre(DECL_ARGS)
1346 {
1347
1348 synopsis_pre(p, n);
1349 return(termp_bold_pre(p, pair, meta, n));
1350 }
1351
1352 static void
1353 termp_fd_post(DECL_ARGS)
1354 {
1355
1356 term_newln(p);
1357 }
1358
1359 static int
1360 termp_sh_pre(DECL_ARGS)
1361 {
1362
1363 switch (n->type) {
1364 case MDOC_BLOCK:
1365 /*
1366 * Vertical space before sections, except
1367 * when the previous section was empty.
1368 */
1369 if (n->prev == NULL ||
1370 MDOC_Sh != n->prev->tok ||
1371 (n->prev->body != NULL &&
1372 n->prev->body->child != NULL))
1373 term_vspace(p);
1374 break;
1375 case MDOC_HEAD:
1376 term_fontpush(p, TERMFONT_BOLD);
1377 break;
1378 case MDOC_BODY:
1379 p->offset = term_len(p, p->defindent);
1380 if (SEC_AUTHORS == n->sec)
1381 p->flags &= ~(TERMP_SPLIT|TERMP_NOSPLIT);
1382 break;
1383 default:
1384 break;
1385 }
1386 return(1);
1387 }
1388
1389 static void
1390 termp_sh_post(DECL_ARGS)
1391 {
1392
1393 switch (n->type) {
1394 case MDOC_HEAD:
1395 term_newln(p);
1396 break;
1397 case MDOC_BODY:
1398 term_newln(p);
1399 p->offset = 0;
1400 break;
1401 default:
1402 break;
1403 }
1404 }
1405
1406 static int
1407 termp_bt_pre(DECL_ARGS)
1408 {
1409
1410 term_word(p, "is currently in beta test.");
1411 p->flags |= TERMP_SENTENCE;
1412 return(0);
1413 }
1414
1415 static void
1416 termp_lb_post(DECL_ARGS)
1417 {
1418
1419 if (SEC_LIBRARY == n->sec && MDOC_LINE & n->flags)
1420 term_newln(p);
1421 }
1422
1423 static int
1424 termp_ud_pre(DECL_ARGS)
1425 {
1426
1427 term_word(p, "currently under development.");
1428 p->flags |= TERMP_SENTENCE;
1429 return(0);
1430 }
1431
1432 static int
1433 termp_d1_pre(DECL_ARGS)
1434 {
1435
1436 if (MDOC_BLOCK != n->type)
1437 return(1);
1438 term_newln(p);
1439 p->offset += term_len(p, p->defindent + 1);
1440 return(1);
1441 }
1442
1443 static int
1444 termp_ft_pre(DECL_ARGS)
1445 {
1446
1447 /* NB: MDOC_LINE does not effect this! */
1448 synopsis_pre(p, n);
1449 term_fontpush(p, TERMFONT_UNDER);
1450 return(1);
1451 }
1452
1453 static int
1454 termp_fn_pre(DECL_ARGS)
1455 {
1456 size_t rmargin = 0;
1457 int pretty;
1458
1459 pretty = MDOC_SYNPRETTY & n->flags;
1460
1461 synopsis_pre(p, n);
1462
1463 if (NULL == (n = n->child))
1464 return(0);
1465
1466 if (pretty) {
1467 rmargin = p->rmargin;
1468 p->rmargin = p->offset + term_len(p, 4);
1469 p->flags |= TERMP_NOBREAK | TERMP_BRIND | TERMP_HANG;
1470 }
1471
1472 assert(MDOC_TEXT == n->type);
1473 term_fontpush(p, TERMFONT_BOLD);
1474 term_word(p, n->string);
1475 term_fontpop(p);
1476
1477 if (pretty) {
1478 term_flushln(p);
1479 p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND | TERMP_HANG);
1480 p->offset = p->rmargin;
1481 p->rmargin = rmargin;
1482 }
1483
1484 p->flags |= TERMP_NOSPACE;
1485 term_word(p, "(");
1486 p->flags |= TERMP_NOSPACE;
1487
1488 for (n = n->next; n; n = n->next) {
1489 assert(MDOC_TEXT == n->type);
1490 term_fontpush(p, TERMFONT_UNDER);
1491 if (pretty)
1492 p->flags |= TERMP_NBRWORD;
1493 term_word(p, n->string);
1494 term_fontpop(p);
1495
1496 if (n->next) {
1497 p->flags |= TERMP_NOSPACE;
1498 term_word(p, ",");
1499 }
1500 }
1501
1502 p->flags |= TERMP_NOSPACE;
1503 term_word(p, ")");
1504
1505 if (pretty) {
1506 p->flags |= TERMP_NOSPACE;
1507 term_word(p, ";");
1508 term_flushln(p);
1509 }
1510
1511 return(0);
1512 }
1513
1514 static int
1515 termp_fa_pre(DECL_ARGS)
1516 {
1517 const struct mdoc_node *nn;
1518
1519 if (n->parent->tok != MDOC_Fo) {
1520 term_fontpush(p, TERMFONT_UNDER);
1521 return(1);
1522 }
1523
1524 for (nn = n->child; nn; nn = nn->next) {
1525 term_fontpush(p, TERMFONT_UNDER);
1526 p->flags |= TERMP_NBRWORD;
1527 term_word(p, nn->string);
1528 term_fontpop(p);
1529
1530 if (nn->next || (n->next && n->next->tok == MDOC_Fa)) {
1531 p->flags |= TERMP_NOSPACE;
1532 term_word(p, ",");
1533 }
1534 }
1535
1536 return(0);
1537 }
1538
1539 static int
1540 termp_bd_pre(DECL_ARGS)
1541 {
1542 size_t tabwidth, lm, len, rm, rmax;
1543 struct mdoc_node *nn;
1544 int offset;
1545
1546 if (MDOC_BLOCK == n->type) {
1547 print_bvspace(p, n, n);
1548 return(1);
1549 } else if (MDOC_HEAD == n->type)
1550 return(0);
1551
1552 /* Handle the -offset argument. */
1553
1554 if (n->norm->Bd.offs == NULL ||
1555 ! strcmp(n->norm->Bd.offs, "left"))
1556 /* nothing */;
1557 else if ( ! strcmp(n->norm->Bd.offs, "indent"))
1558 p->offset += term_len(p, p->defindent + 1);
1559 else if ( ! strcmp(n->norm->Bd.offs, "indent-two"))
1560 p->offset += term_len(p, (p->defindent + 1) * 2);
1561 else {
1562 offset = a2width(p, n->norm->Bd.offs);
1563 if (offset < 0 && (size_t)(-offset) > p->offset)
1564 p->offset = 0;
1565 else if (offset < SHRT_MAX)
1566 p->offset += offset;
1567 }
1568
1569 /*
1570 * If -ragged or -filled are specified, the block does nothing
1571 * but change the indentation. If -unfilled or -literal are
1572 * specified, text is printed exactly as entered in the display:
1573 * for macro lines, a newline is appended to the line. Blank
1574 * lines are allowed.
1575 */
1576
1577 if (DISP_literal != n->norm->Bd.type &&
1578 DISP_unfilled != n->norm->Bd.type &&
1579 DISP_centered != n->norm->Bd.type)
1580 return(1);
1581
1582 tabwidth = p->tabwidth;
1583 if (DISP_literal == n->norm->Bd.type)
1584 p->tabwidth = term_len(p, 8);
1585
1586 lm = p->offset;
1587 rm = p->rmargin;
1588 rmax = p->maxrmargin;
1589 p->rmargin = p->maxrmargin = TERM_MAXMARGIN;
1590
1591 for (nn = n->child; nn; nn = nn->next) {
1592 if (DISP_centered == n->norm->Bd.type) {
1593 if (MDOC_TEXT == nn->type) {
1594 len = term_strlen(p, nn->string);
1595 p->offset = len >= rm ? 0 :
1596 lm + len >= rm ? rm - len :
1597 (lm + rm - len) / 2;
1598 } else
1599 p->offset = lm;
1600 }
1601 print_mdoc_node(p, pair, meta, nn);
1602 /*
1603 * If the printed node flushes its own line, then we
1604 * needn't do it here as well. This is hacky, but the
1605 * notion of selective eoln whitespace is pretty dumb
1606 * anyway, so don't sweat it.
1607 */
1608 switch (nn->tok) {
1609 case MDOC_Sm:
1610 /* FALLTHROUGH */
1611 case MDOC_br:
1612 /* FALLTHROUGH */
1613 case MDOC_sp:
1614 /* FALLTHROUGH */
1615 case MDOC_Bl:
1616 /* FALLTHROUGH */
1617 case MDOC_D1:
1618 /* FALLTHROUGH */
1619 case MDOC_Dl:
1620 /* FALLTHROUGH */
1621 case MDOC_Lp:
1622 /* FALLTHROUGH */
1623 case MDOC_Pp:
1624 continue;
1625 default:
1626 break;
1627 }
1628 if (p->flags & TERMP_NONEWLINE ||
1629 (nn->next && ! (nn->next->flags & MDOC_LINE)))
1630 continue;
1631 term_flushln(p);
1632 p->flags |= TERMP_NOSPACE;
1633 }
1634
1635 p->tabwidth = tabwidth;
1636 p->rmargin = rm;
1637 p->maxrmargin = rmax;
1638 return(0);
1639 }
1640
1641 static void
1642 termp_bd_post(DECL_ARGS)
1643 {
1644 size_t rm, rmax;
1645
1646 if (MDOC_BODY != n->type)
1647 return;
1648
1649 rm = p->rmargin;
1650 rmax = p->maxrmargin;
1651
1652 if (DISP_literal == n->norm->Bd.type ||
1653 DISP_unfilled == n->norm->Bd.type)
1654 p->rmargin = p->maxrmargin = TERM_MAXMARGIN;
1655
1656 p->flags |= TERMP_NOSPACE;
1657 term_newln(p);
1658
1659 p->rmargin = rm;
1660 p->maxrmargin = rmax;
1661 }
1662
1663 static int
1664 termp_bx_pre(DECL_ARGS)
1665 {
1666
1667 if (NULL != (n = n->child)) {
1668 term_word(p, n->string);
1669 p->flags |= TERMP_NOSPACE;
1670 term_word(p, "BSD");
1671 } else {
1672 term_word(p, "BSD");
1673 return(0);
1674 }
1675
1676 if (NULL != (n = n->next)) {
1677 p->flags |= TERMP_NOSPACE;
1678 term_word(p, "-");
1679 p->flags |= TERMP_NOSPACE;
1680 term_word(p, n->string);
1681 }
1682
1683 return(0);
1684 }
1685
1686 static int
1687 termp_xx_pre(DECL_ARGS)
1688 {
1689 const char *pp;
1690 int flags;
1691
1692 pp = NULL;
1693 switch (n->tok) {
1694 case MDOC_Bsx:
1695 pp = "BSD/OS";
1696 break;
1697 case MDOC_Dx:
1698 pp = "DragonFly";
1699 break;
1700 case MDOC_Fx:
1701 pp = "FreeBSD";
1702 break;
1703 case MDOC_Nx:
1704 pp = "NetBSD";
1705 break;
1706 case MDOC_Ox:
1707 pp = "OpenBSD";
1708 break;
1709 case MDOC_Ux:
1710 pp = "UNIX";
1711 break;
1712 default:
1713 abort();
1714 /* NOTREACHED */
1715 }
1716
1717 term_word(p, pp);
1718 if (n->child) {
1719 flags = p->flags;
1720 p->flags |= TERMP_KEEP;
1721 term_word(p, n->child->string);
1722 p->flags = flags;
1723 }
1724 return(0);
1725 }
1726
1727 static void
1728 termp_pf_post(DECL_ARGS)
1729 {
1730
1731 if ( ! (n->next == NULL || n->next->flags & MDOC_LINE))
1732 p->flags |= TERMP_NOSPACE;
1733 }
1734
1735 static int
1736 termp_ss_pre(DECL_ARGS)
1737 {
1738
1739 switch (n->type) {
1740 case MDOC_BLOCK:
1741 term_newln(p);
1742 if (n->prev)
1743 term_vspace(p);
1744 break;
1745 case MDOC_HEAD:
1746 term_fontpush(p, TERMFONT_BOLD);
1747 p->offset = term_len(p, (p->defindent+1)/2);
1748 break;
1749 case MDOC_BODY:
1750 p->offset = term_len(p, p->defindent);
1751 break;
1752 default:
1753 break;
1754 }
1755
1756 return(1);
1757 }
1758
1759 static void
1760 termp_ss_post(DECL_ARGS)
1761 {
1762
1763 if (n->type == MDOC_HEAD || n->type == MDOC_BODY)
1764 term_newln(p);
1765 }
1766
1767 static int
1768 termp_cd_pre(DECL_ARGS)
1769 {
1770
1771 synopsis_pre(p, n);
1772 term_fontpush(p, TERMFONT_BOLD);
1773 return(1);
1774 }
1775
1776 static int
1777 termp_in_pre(DECL_ARGS)
1778 {
1779
1780 synopsis_pre(p, n);
1781
1782 if (MDOC_SYNPRETTY & n->flags && MDOC_LINE & n->flags) {
1783 term_fontpush(p, TERMFONT_BOLD);
1784 term_word(p, "#include");
1785 term_word(p, "<");
1786 } else {
1787 term_word(p, "<");
1788 term_fontpush(p, TERMFONT_UNDER);
1789 }
1790
1791 p->flags |= TERMP_NOSPACE;
1792 return(1);
1793 }
1794
1795 static void
1796 termp_in_post(DECL_ARGS)
1797 {
1798
1799 if (MDOC_SYNPRETTY & n->flags)
1800 term_fontpush(p, TERMFONT_BOLD);
1801
1802 p->flags |= TERMP_NOSPACE;
1803 term_word(p, ">");
1804
1805 if (MDOC_SYNPRETTY & n->flags)
1806 term_fontpop(p);
1807 }
1808
1809 static int
1810 termp_sp_pre(DECL_ARGS)
1811 {
1812 struct roffsu su;
1813 size_t i, len;
1814
1815 switch (n->tok) {
1816 case MDOC_sp:
1817 if (n->child) {
1818 if ( ! a2roffsu(n->child->string, &su, SCALE_VS))
1819 su.scale = 1.0;
1820 len = term_vspan(p, &su);
1821 } else
1822 len = 1;
1823 break;
1824 case MDOC_br:
1825 len = 0;
1826 break;
1827 default:
1828 len = 1;
1829 break;
1830 }
1831
1832 if (0 == len)
1833 term_newln(p);
1834 for (i = 0; i < len; i++)
1835 term_vspace(p);
1836
1837 return(0);
1838 }
1839
1840 static int
1841 termp_skip_pre(DECL_ARGS)
1842 {
1843
1844 return(0);
1845 }
1846
1847 static int
1848 termp_quote_pre(DECL_ARGS)
1849 {
1850
1851 if (MDOC_BODY != n->type && MDOC_ELEM != n->type)
1852 return(1);
1853
1854 switch (n->tok) {
1855 case MDOC_Ao:
1856 /* FALLTHROUGH */
1857 case MDOC_Aq:
1858 term_word(p, n->nchild == 1 &&
1859 n->child->tok == MDOC_Mt ? "<" : "\\(la");
1860 break;
1861 case MDOC_Bro:
1862 /* FALLTHROUGH */
1863 case MDOC_Brq:
1864 term_word(p, "{");
1865 break;
1866 case MDOC_Oo:
1867 /* FALLTHROUGH */
1868 case MDOC_Op:
1869 /* FALLTHROUGH */
1870 case MDOC_Bo:
1871 /* FALLTHROUGH */
1872 case MDOC_Bq:
1873 term_word(p, "[");
1874 break;
1875 case MDOC_Do:
1876 /* FALLTHROUGH */
1877 case MDOC_Dq:
1878 term_word(p, "\\(lq");
1879 break;
1880 case MDOC_En:
1881 if (NULL == n->norm->Es ||
1882 NULL == n->norm->Es->child)
1883 return(1);
1884 term_word(p, n->norm->Es->child->string);
1885 break;
1886 case MDOC_Po:
1887 /* FALLTHROUGH */
1888 case MDOC_Pq:
1889 term_word(p, "(");
1890 break;
1891 case MDOC__T:
1892 /* FALLTHROUGH */
1893 case MDOC_Qo:
1894 /* FALLTHROUGH */
1895 case MDOC_Qq:
1896 term_word(p, "\"");
1897 break;
1898 case MDOC_Ql:
1899 /* FALLTHROUGH */
1900 case MDOC_So:
1901 /* FALLTHROUGH */
1902 case MDOC_Sq:
1903 term_word(p, "\\(oq");
1904 break;
1905 default:
1906 abort();
1907 /* NOTREACHED */
1908 }
1909
1910 p->flags |= TERMP_NOSPACE;
1911 return(1);
1912 }
1913
1914 static void
1915 termp_quote_post(DECL_ARGS)
1916 {
1917
1918 if (n->type != MDOC_BODY && n->type != MDOC_ELEM)
1919 return;
1920
1921 p->flags |= TERMP_NOSPACE;
1922
1923 switch (n->tok) {
1924 case MDOC_Ao:
1925 /* FALLTHROUGH */
1926 case MDOC_Aq:
1927 term_word(p, n->nchild == 1 &&
1928 n->child->tok == MDOC_Mt ? ">" : "\\(ra");
1929 break;
1930 case MDOC_Bro:
1931 /* FALLTHROUGH */
1932 case MDOC_Brq:
1933 term_word(p, "}");
1934 break;
1935 case MDOC_Oo:
1936 /* FALLTHROUGH */
1937 case MDOC_Op:
1938 /* FALLTHROUGH */
1939 case MDOC_Bo:
1940 /* FALLTHROUGH */
1941 case MDOC_Bq:
1942 term_word(p, "]");
1943 break;
1944 case MDOC_Do:
1945 /* FALLTHROUGH */
1946 case MDOC_Dq:
1947 term_word(p, "\\(rq");
1948 break;
1949 case MDOC_En:
1950 if (n->norm->Es == NULL ||
1951 n->norm->Es->child == NULL ||
1952 n->norm->Es->child->next == NULL)
1953 p->flags &= ~TERMP_NOSPACE;
1954 else
1955 term_word(p, n->norm->Es->child->next->string);
1956 break;
1957 case MDOC_Po:
1958 /* FALLTHROUGH */
1959 case MDOC_Pq:
1960 term_word(p, ")");
1961 break;
1962 case MDOC__T:
1963 /* FALLTHROUGH */
1964 case MDOC_Qo:
1965 /* FALLTHROUGH */
1966 case MDOC_Qq:
1967 term_word(p, "\"");
1968 break;
1969 case MDOC_Ql:
1970 /* FALLTHROUGH */
1971 case MDOC_So:
1972 /* FALLTHROUGH */
1973 case MDOC_Sq:
1974 term_word(p, "\\(cq");
1975 break;
1976 default:
1977 abort();
1978 /* NOTREACHED */
1979 }
1980 }
1981
1982 static int
1983 termp_eo_pre(DECL_ARGS)
1984 {
1985
1986 if (n->type != MDOC_BODY)
1987 return(1);
1988
1989 if (n->end == ENDBODY_NOT &&
1990 n->parent->head->child == NULL &&
1991 n->child != NULL &&
1992 n->child->end != ENDBODY_NOT)
1993 term_word(p, "\\&");
1994 else if (n->end != ENDBODY_NOT ? n->child != NULL :
1995 n->parent->head->child != NULL &&
1996 (n->parent->body->child != NULL ||
1997 n->parent->tail->child != NULL))
1998 p->flags |= TERMP_NOSPACE;
1999
2000 return(1);
2001 }
2002
2003 static void
2004 termp_eo_post(DECL_ARGS)
2005 {
2006 int body, tail;
2007
2008 if (n->type != MDOC_BODY)
2009 return;
2010
2011 if (n->end != ENDBODY_NOT) {
2012 p->flags &= ~TERMP_NOSPACE;
2013 return;
2014 }
2015
2016 body = n->child != NULL || n->parent->head->child != NULL;
2017 tail = n->parent->tail != NULL && n->parent->tail->child != NULL;
2018
2019 if (body && tail)
2020 p->flags |= TERMP_NOSPACE;
2021 else if ( ! (body || tail))
2022 term_word(p, "\\&");
2023 else if ( ! tail)
2024 p->flags &= ~TERMP_NOSPACE;
2025 }
2026
2027 static int
2028 termp_fo_pre(DECL_ARGS)
2029 {
2030 size_t rmargin = 0;
2031 int pretty;
2032
2033 pretty = MDOC_SYNPRETTY & n->flags;
2034
2035 if (MDOC_BLOCK == n->type) {
2036 synopsis_pre(p, n);
2037 return(1);
2038 } else if (MDOC_BODY == n->type) {
2039 if (pretty) {
2040 rmargin = p->rmargin;
2041 p->rmargin = p->offset + term_len(p, 4);
2042 p->flags |= TERMP_NOBREAK | TERMP_BRIND |
2043 TERMP_HANG;
2044 }
2045 p->flags |= TERMP_NOSPACE;
2046 term_word(p, "(");
2047 p->flags |= TERMP_NOSPACE;
2048 if (pretty) {
2049 term_flushln(p);
2050 p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND |
2051 TERMP_HANG);
2052 p->offset = p->rmargin;
2053 p->rmargin = rmargin;
2054 }
2055 return(1);
2056 }
2057
2058 if (NULL == n->child)
2059 return(0);
2060
2061 /* XXX: we drop non-initial arguments as per groff. */
2062
2063 assert(n->child->string);
2064 term_fontpush(p, TERMFONT_BOLD);
2065 term_word(p, n->child->string);
2066 return(0);
2067 }
2068
2069 static void
2070 termp_fo_post(DECL_ARGS)
2071 {
2072
2073 if (MDOC_BODY != n->type)
2074 return;
2075
2076 p->flags |= TERMP_NOSPACE;
2077 term_word(p, ")");
2078
2079 if (MDOC_SYNPRETTY & n->flags) {
2080 p->flags |= TERMP_NOSPACE;
2081 term_word(p, ";");
2082 term_flushln(p);
2083 }
2084 }
2085
2086 static int
2087 termp_bf_pre(DECL_ARGS)
2088 {
2089
2090 if (MDOC_HEAD == n->type)
2091 return(0);
2092 else if (MDOC_BODY != n->type)
2093 return(1);
2094
2095 if (FONT_Em == n->norm->Bf.font)
2096 term_fontpush(p, TERMFONT_UNDER);
2097 else if (FONT_Sy == n->norm->Bf.font)
2098 term_fontpush(p, TERMFONT_BOLD);
2099 else
2100 term_fontpush(p, TERMFONT_NONE);
2101
2102 return(1);
2103 }
2104
2105 static int
2106 termp_sm_pre(DECL_ARGS)
2107 {
2108
2109 if (NULL == n->child)
2110 p->flags ^= TERMP_NONOSPACE;
2111 else if (0 == strcmp("on", n->child->string))
2112 p->flags &= ~TERMP_NONOSPACE;
2113 else
2114 p->flags |= TERMP_NONOSPACE;
2115
2116 if (p->col && ! (TERMP_NONOSPACE & p->flags))
2117 p->flags &= ~TERMP_NOSPACE;
2118
2119 return(0);
2120 }
2121
2122 static int
2123 termp_ap_pre(DECL_ARGS)
2124 {
2125
2126 p->flags |= TERMP_NOSPACE;
2127 term_word(p, "'");
2128 p->flags |= TERMP_NOSPACE;
2129 return(1);
2130 }
2131
2132 static void
2133 termp____post(DECL_ARGS)
2134 {
2135
2136 /*
2137 * Handle lists of authors. In general, print each followed by
2138 * a comma. Don't print the comma if there are only two
2139 * authors.
2140 */
2141 if (MDOC__A == n->tok && n->next && MDOC__A == n->next->tok)
2142 if (NULL == n->next->next || MDOC__A != n->next->next->tok)
2143 if (NULL == n->prev || MDOC__A != n->prev->tok)
2144 return;
2145
2146 /* TODO: %U. */
2147
2148 if (NULL == n->parent || MDOC_Rs != n->parent->tok)
2149 return;
2150
2151 p->flags |= TERMP_NOSPACE;
2152 if (NULL == n->next) {
2153 term_word(p, ".");
2154 p->flags |= TERMP_SENTENCE;
2155 } else
2156 term_word(p, ",");
2157 }
2158
2159 static int
2160 termp_li_pre(DECL_ARGS)
2161 {
2162
2163 term_fontpush(p, TERMFONT_NONE);
2164 return(1);
2165 }
2166
2167 static int
2168 termp_lk_pre(DECL_ARGS)
2169 {
2170 const struct mdoc_node *link, *descr;
2171
2172 if (NULL == (link = n->child))
2173 return(0);
2174
2175 if (NULL != (descr = link->next)) {
2176 term_fontpush(p, TERMFONT_UNDER);
2177 while (NULL != descr) {
2178 term_word(p, descr->string);
2179 descr = descr->next;
2180 }
2181 p->flags |= TERMP_NOSPACE;
2182 term_word(p, ":");
2183 term_fontpop(p);
2184 }
2185
2186 term_fontpush(p, TERMFONT_BOLD);
2187 term_word(p, link->string);
2188 term_fontpop(p);
2189
2190 return(0);
2191 }
2192
2193 static int
2194 termp_bk_pre(DECL_ARGS)
2195 {
2196
2197 switch (n->type) {
2198 case MDOC_BLOCK:
2199 break;
2200 case MDOC_HEAD:
2201 return(0);
2202 case MDOC_BODY:
2203 if (n->parent->args || 0 == n->prev->nchild)
2204 p->flags |= TERMP_PREKEEP;
2205 break;
2206 default:
2207 abort();
2208 /* NOTREACHED */
2209 }
2210
2211 return(1);
2212 }
2213
2214 static void
2215 termp_bk_post(DECL_ARGS)
2216 {
2217
2218 if (MDOC_BODY == n->type)
2219 p->flags &= ~(TERMP_KEEP | TERMP_PREKEEP);
2220 }
2221
2222 static void
2223 termp__t_post(DECL_ARGS)
2224 {
2225
2226 /*
2227 * If we're in an `Rs' and there's a journal present, then quote
2228 * us instead of underlining us (for disambiguation).
2229 */
2230 if (n->parent && MDOC_Rs == n->parent->tok &&
2231 n->parent->norm->Rs.quote_T)
2232 termp_quote_post(p, pair, meta, n);
2233
2234 termp____post(p, pair, meta, n);
2235 }
2236
2237 static int
2238 termp__t_pre(DECL_ARGS)
2239 {
2240
2241 /*
2242 * If we're in an `Rs' and there's a journal present, then quote
2243 * us instead of underlining us (for disambiguation).
2244 */
2245 if (n->parent && MDOC_Rs == n->parent->tok &&
2246 n->parent->norm->Rs.quote_T)
2247 return(termp_quote_pre(p, pair, meta, n));
2248
2249 term_fontpush(p, TERMFONT_UNDER);
2250 return(1);
2251 }
2252
2253 static int
2254 termp_under_pre(DECL_ARGS)
2255 {
2256
2257 term_fontpush(p, TERMFONT_UNDER);
2258 return(1);
2259 }