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