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