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