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