]> git.cameronkatri.com Git - mandoc.git/blob - mdoc_term.c
Sync'd example style-sheet to be more like OpenBSD's default (KISS).
[mandoc.git] / mdoc_term.c
1 /* $Id: mdoc_term.c,v 1.81 2009/09/24 11:05:45 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 */ /* FIXME: convert to symbol? */
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 */ /* TODO */
265 { NULL, NULL }, /* En */ /* TODO */
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 (SEC_SEE_ALSO != node->sec)
1168 return(1);
1169 if (MDOC_BLOCK == node->type && node->prev)
1170 term_vspace(p);
1171 return(1);
1172 }
1173
1174
1175 /* ARGSUSED */
1176 static int
1177 termp_rv_pre(DECL_ARGS)
1178 {
1179 const struct mdoc_node *nn;
1180
1181 term_newln(p);
1182 term_word(p, "The");
1183
1184 nn = node->child;
1185 assert(nn);
1186 for ( ; nn; nn = nn->next) {
1187 p->bold++;
1188 term_word(p, nn->string);
1189 p->bold--;
1190 p->flags |= TERMP_NOSPACE;
1191 if (nn->next && NULL == nn->next->next)
1192 term_word(p, "(), and");
1193 else if (nn->next)
1194 term_word(p, "(),");
1195 else
1196 term_word(p, "()");
1197 }
1198
1199 if (node->child->next)
1200 term_word(p, "functions return");
1201 else
1202 term_word(p, "function returns");
1203
1204 term_word(p, "the value 0 if successful; otherwise the value "
1205 "-1 is returned and the global variable");
1206
1207 p->under++;
1208 term_word(p, "errno");
1209 p->under--;
1210
1211 term_word(p, "is set to indicate the error.");
1212
1213 return(0);
1214 }
1215
1216
1217 /* ARGSUSED */
1218 static int
1219 termp_ex_pre(DECL_ARGS)
1220 {
1221 const struct mdoc_node *nn;
1222
1223 term_word(p, "The");
1224
1225 nn = node->child;
1226 assert(nn);
1227 for ( ; nn; nn = nn->next) {
1228 p->bold++;
1229 term_word(p, nn->string);
1230 p->bold--;
1231 p->flags |= TERMP_NOSPACE;
1232 if (nn->next && NULL == nn->next->next)
1233 term_word(p, ", and");
1234 else if (nn->next)
1235 term_word(p, ",");
1236 else
1237 p->flags &= ~TERMP_NOSPACE;
1238 }
1239
1240 if (node->child->next)
1241 term_word(p, "utilities exit");
1242 else
1243 term_word(p, "utility exits");
1244
1245 term_word(p, "0 on success, and >0 if an error occurs.");
1246
1247 return(0);
1248 }
1249
1250
1251 /* ARGSUSED */
1252 static int
1253 termp_nd_pre(DECL_ARGS)
1254 {
1255
1256 if (MDOC_BODY != node->type)
1257 return(1);
1258
1259 #if defined(__OpenBSD__) || defined(__linux__)
1260 term_word(p, "\\(en");
1261 #else
1262 term_word(p, "\\(em");
1263 #endif
1264 return(1);
1265 }
1266
1267
1268 /* ARGSUSED */
1269 static void
1270 termp_bl_post(DECL_ARGS)
1271 {
1272
1273 if (MDOC_BLOCK == node->type)
1274 term_newln(p);
1275 }
1276
1277
1278 /* ARGSUSED */
1279 static void
1280 termp_op_post(DECL_ARGS)
1281 {
1282
1283 if (MDOC_BODY != node->type)
1284 return;
1285 p->flags |= TERMP_NOSPACE;
1286 term_word(p, "\\(rB");
1287 }
1288
1289
1290 /* ARGSUSED */
1291 static int
1292 termp_xr_pre(DECL_ARGS)
1293 {
1294 const struct mdoc_node *n;
1295
1296 assert(node->child && MDOC_TEXT == node->child->type);
1297 n = node->child;
1298
1299 term_word(p, n->string);
1300 if (NULL == (n = n->next))
1301 return(0);
1302 p->flags |= TERMP_NOSPACE;
1303 term_word(p, "(");
1304 p->flags |= TERMP_NOSPACE;
1305 term_word(p, n->string);
1306 p->flags |= TERMP_NOSPACE;
1307 term_word(p, ")");
1308 return(0);
1309 }
1310
1311
1312 /* ARGSUSED */
1313 static void
1314 termp_vt_post(DECL_ARGS)
1315 {
1316
1317 if (node->sec != SEC_SYNOPSIS)
1318 return;
1319 if (node->next && MDOC_Vt == node->next->tok)
1320 term_newln(p);
1321 else if (node->next)
1322 term_vspace(p);
1323 }
1324
1325
1326 /* ARGSUSED */
1327 static int
1328 termp_bold_pre(DECL_ARGS)
1329 {
1330
1331 p->bold++;
1332 return(1);
1333 }
1334
1335
1336 /* ARGSUSED */
1337 static void
1338 termp_fd_post(DECL_ARGS)
1339 {
1340
1341 if (node->sec != SEC_SYNOPSIS)
1342 return;
1343
1344 term_newln(p);
1345 if (node->next && MDOC_Fd != node->next->tok)
1346 term_vspace(p);
1347 }
1348
1349
1350 /* ARGSUSED */
1351 static int
1352 termp_sh_pre(DECL_ARGS)
1353 {
1354 /*
1355 * XXX: undocumented: using two `Sh' macros in sequence has no
1356 * vspace between calls, only a newline.
1357 */
1358 switch (node->type) {
1359 case (MDOC_BLOCK):
1360 if (node->prev && MDOC_Sh == node->prev->tok)
1361 if (NULL == node->prev->body->child)
1362 break;
1363 term_vspace(p);
1364 break;
1365 case (MDOC_HEAD):
1366 p->bold++;
1367 break;
1368 case (MDOC_BODY):
1369 p->offset = INDENT;
1370 break;
1371 default:
1372 break;
1373 }
1374 return(1);
1375 }
1376
1377
1378 /* ARGSUSED */
1379 static void
1380 termp_sh_post(DECL_ARGS)
1381 {
1382
1383 switch (node->type) {
1384 case (MDOC_HEAD):
1385 term_newln(p);
1386 break;
1387 case (MDOC_BODY):
1388 term_newln(p);
1389 p->offset = 0;
1390 break;
1391 default:
1392 break;
1393 }
1394 }
1395
1396
1397 /* ARGSUSED */
1398 static int
1399 termp_op_pre(DECL_ARGS)
1400 {
1401
1402 switch (node->type) {
1403 case (MDOC_BODY):
1404 term_word(p, "\\(lB");
1405 p->flags |= TERMP_NOSPACE;
1406 break;
1407 default:
1408 break;
1409 }
1410 return(1);
1411 }
1412
1413
1414 /* ARGSUSED */
1415 static int
1416 termp_bt_pre(DECL_ARGS)
1417 {
1418
1419 term_word(p, "is currently in beta test.");
1420 return(0);
1421 }
1422
1423
1424 /* ARGSUSED */
1425 static void
1426 termp_lb_post(DECL_ARGS)
1427 {
1428
1429 if (SEC_LIBRARY == node->sec)
1430 term_newln(p);
1431 }
1432
1433
1434 /* ARGSUSED */
1435 static int
1436 termp_ud_pre(DECL_ARGS)
1437 {
1438
1439 term_word(p, "currently under development.");
1440 return(1);
1441 }
1442
1443
1444 /* ARGSUSED */
1445 static int
1446 termp_d1_pre(DECL_ARGS)
1447 {
1448
1449 if (MDOC_BLOCK != node->type)
1450 return(1);
1451 term_newln(p);
1452 p->offset += (INDENT + 1);
1453 return(1);
1454 }
1455
1456
1457 /* ARGSUSED */
1458 static void
1459 termp_d1_post(DECL_ARGS)
1460 {
1461
1462 if (MDOC_BLOCK != node->type)
1463 return;
1464 term_newln(p);
1465 }
1466
1467
1468 /* ARGSUSED */
1469 static int
1470 termp_aq_pre(DECL_ARGS)
1471 {
1472
1473 if (MDOC_BODY != node->type)
1474 return(1);
1475 term_word(p, "\\(la");
1476 p->flags |= TERMP_NOSPACE;
1477 return(1);
1478 }
1479
1480
1481 /* ARGSUSED */
1482 static void
1483 termp_aq_post(DECL_ARGS)
1484 {
1485
1486 if (MDOC_BODY != node->type)
1487 return;
1488 p->flags |= TERMP_NOSPACE;
1489 term_word(p, "\\(ra");
1490 }
1491
1492
1493 /* ARGSUSED */
1494 static int
1495 termp_ft_pre(DECL_ARGS)
1496 {
1497
1498 if (SEC_SYNOPSIS == node->sec)
1499 if (node->prev && MDOC_Fo == node->prev->tok)
1500 term_vspace(p);
1501 p->under++;
1502 return(1);
1503 }
1504
1505
1506 /* ARGSUSED */
1507 static void
1508 termp_ft_post(DECL_ARGS)
1509 {
1510
1511 if (SEC_SYNOPSIS == node->sec)
1512 term_newln(p);
1513 }
1514
1515
1516 /* ARGSUSED */
1517 static int
1518 termp_fn_pre(DECL_ARGS)
1519 {
1520 const struct mdoc_node *n;
1521
1522 assert(node->child && MDOC_TEXT == node->child->type);
1523
1524 /* FIXME: can be "type funcname" "type varname"... */
1525
1526 p->bold++;
1527 term_word(p, node->child->string);
1528 p->bold--;
1529
1530 p->flags |= TERMP_NOSPACE;
1531 term_word(p, "(");
1532
1533 for (n = node->child->next; n; n = n->next) {
1534 p->under++;
1535 term_word(p, n->string);
1536 p->under--;
1537 if (n->next)
1538 term_word(p, ",");
1539 }
1540
1541 term_word(p, ")");
1542
1543 if (SEC_SYNOPSIS == node->sec)
1544 term_word(p, ";");
1545
1546 return(0);
1547 }
1548
1549
1550 /* ARGSUSED */
1551 static void
1552 termp_fn_post(DECL_ARGS)
1553 {
1554
1555 if (node->sec == SEC_SYNOPSIS && node->next)
1556 term_vspace(p);
1557 }
1558
1559
1560 /* ARGSUSED */
1561 static int
1562 termp_fa_pre(DECL_ARGS)
1563 {
1564 struct mdoc_node *n;
1565
1566 if (node->parent->tok != MDOC_Fo) {
1567 p->under++;
1568 return(1);
1569 }
1570
1571 for (n = node->child; n; n = n->next) {
1572 p->under++;
1573 term_word(p, n->string);
1574 p->under--;
1575 if (n->next)
1576 term_word(p, ",");
1577 }
1578
1579 if (node->child && node->next && node->next->tok == MDOC_Fa)
1580 term_word(p, ",");
1581
1582 return(0);
1583 }
1584
1585
1586 /* ARGSUSED */
1587 static int
1588 termp_bd_pre(DECL_ARGS)
1589 {
1590 int i, type;
1591
1592 /*
1593 * This is fairly tricky due primarily to crappy documentation.
1594 * If -ragged or -filled are specified, the block does nothing
1595 * but change the indentation.
1596 *
1597 * If, on the other hand, -unfilled or -literal are specified,
1598 * then the game changes. Text is printed exactly as entered in
1599 * the display: if a macro line, a newline is appended to the
1600 * line. Blank lines are allowed.
1601 */
1602
1603 if (MDOC_BLOCK == node->type) {
1604 fmt_block_vspace(p, node, node);
1605 return(1);
1606 } else if (MDOC_BODY != node->type)
1607 return(1);
1608
1609 assert(node->parent->args);
1610
1611 for (type = -1, i = 0; -1 == type &&
1612 i < (int)node->parent->args->argc; i++) {
1613 switch (node->parent->args->argv[i].arg) {
1614 case (MDOC_Ragged):
1615 /* FALLTHROUGH */
1616 case (MDOC_Filled):
1617 /* FALLTHROUGH */
1618 case (MDOC_Unfilled):
1619 /* FALLTHROUGH */
1620 case (MDOC_Literal):
1621 type = node->parent->args->argv[i].arg;
1622 break;
1623 default:
1624 break;
1625 }
1626 }
1627
1628 assert(type > -1);
1629
1630 i = arg_getattr(MDOC_Offset, node->parent);
1631 if (-1 != i)
1632 p->offset += arg_offset(&node->parent->args->argv[i]);
1633
1634 switch (type) {
1635 case (MDOC_Literal):
1636 /* FALLTHROUGH */
1637 case (MDOC_Unfilled):
1638 break;
1639 default:
1640 return(1);
1641 }
1642
1643 for (node = node->child; node; node = node->next) {
1644 p->flags |= TERMP_NOSPACE;
1645 print_node(p, pair, meta, node);
1646 if (node->next)
1647 term_flushln(p);
1648 }
1649
1650 return(0);
1651 }
1652
1653
1654 /* ARGSUSED */
1655 static void
1656 termp_bd_post(DECL_ARGS)
1657 {
1658
1659 if (MDOC_BODY != node->type)
1660 return;
1661 p->flags |= TERMP_NOSPACE;
1662 term_flushln(p);
1663 }
1664
1665
1666 /* ARGSUSED */
1667 static int
1668 termp_qq_pre(DECL_ARGS)
1669 {
1670
1671 if (MDOC_BODY != node->type)
1672 return(1);
1673 term_word(p, "\"");
1674 p->flags |= TERMP_NOSPACE;
1675 return(1);
1676 }
1677
1678
1679 /* ARGSUSED */
1680 static void
1681 termp_qq_post(DECL_ARGS)
1682 {
1683
1684 if (MDOC_BODY != node->type)
1685 return;
1686 p->flags |= TERMP_NOSPACE;
1687 term_word(p, "\"");
1688 }
1689
1690
1691 /* ARGSUSED */
1692 static void
1693 termp_bx_post(DECL_ARGS)
1694 {
1695
1696 if (node->child)
1697 p->flags |= TERMP_NOSPACE;
1698 term_word(p, "BSD");
1699 }
1700
1701
1702 /* ARGSUSED */
1703 static int
1704 termp_xx_pre(DECL_ARGS)
1705 {
1706 const char *pp;
1707
1708 pp = NULL;
1709 switch (node->tok) {
1710 case (MDOC_Bsx):
1711 pp = "BSDI BSD/OS";
1712 break;
1713 case (MDOC_Dx):
1714 pp = "DragonFlyBSD";
1715 break;
1716 case (MDOC_Fx):
1717 pp = "FreeBSD";
1718 break;
1719 case (MDOC_Nx):
1720 pp = "NetBSD";
1721 break;
1722 case (MDOC_Ox):
1723 pp = "OpenBSD";
1724 break;
1725 case (MDOC_Ux):
1726 pp = "UNIX";
1727 break;
1728 default:
1729 break;
1730 }
1731
1732 assert(pp);
1733 term_word(p, pp);
1734 return(1);
1735 }
1736
1737
1738 /* ARGSUSED */
1739 static int
1740 termp_sq_pre(DECL_ARGS)
1741 {
1742
1743 if (MDOC_BODY != node->type)
1744 return(1);
1745 term_word(p, "\\(oq");
1746 p->flags |= TERMP_NOSPACE;
1747 return(1);
1748 }
1749
1750
1751 /* ARGSUSED */
1752 static void
1753 termp_sq_post(DECL_ARGS)
1754 {
1755
1756 if (MDOC_BODY != node->type)
1757 return;
1758 p->flags |= TERMP_NOSPACE;
1759 term_word(p, "\\(aq");
1760 }
1761
1762
1763 /* ARGSUSED */
1764 static int
1765 termp_pf_pre(DECL_ARGS)
1766 {
1767
1768 p->flags |= TERMP_IGNDELIM;
1769 return(1);
1770 }
1771
1772
1773 /* ARGSUSED */
1774 static void
1775 termp_pf_post(DECL_ARGS)
1776 {
1777
1778 p->flags &= ~TERMP_IGNDELIM;
1779 p->flags |= TERMP_NOSPACE;
1780 }
1781
1782
1783 /* ARGSUSED */
1784 static int
1785 termp_ss_pre(DECL_ARGS)
1786 {
1787
1788 switch (node->type) {
1789 case (MDOC_BLOCK):
1790 term_newln(p);
1791 if (node->prev)
1792 term_vspace(p);
1793 break;
1794 case (MDOC_HEAD):
1795 p->bold++;
1796 p->offset = HALFINDENT;
1797 break;
1798 default:
1799 break;
1800 }
1801
1802 return(1);
1803 }
1804
1805
1806 /* ARGSUSED */
1807 static void
1808 termp_ss_post(DECL_ARGS)
1809 {
1810
1811 if (MDOC_HEAD == node->type)
1812 term_newln(p);
1813 }
1814
1815
1816 /* ARGSUSED */
1817 static int
1818 termp_cd_pre(DECL_ARGS)
1819 {
1820
1821 p->bold++;
1822 term_newln(p);
1823 return(1);
1824 }
1825
1826
1827 /* ARGSUSED */
1828 static int
1829 termp_in_pre(DECL_ARGS)
1830 {
1831
1832 p->bold++;
1833 if (SEC_SYNOPSIS == node->sec)
1834 term_word(p, "#include");
1835
1836 term_word(p, "<");
1837 p->flags |= TERMP_NOSPACE;
1838 return(1);
1839 }
1840
1841
1842 /* ARGSUSED */
1843 static void
1844 termp_in_post(DECL_ARGS)
1845 {
1846
1847 p->bold++;
1848 p->flags |= TERMP_NOSPACE;
1849 term_word(p, ">");
1850 p->bold--;
1851
1852 if (SEC_SYNOPSIS != node->sec)
1853 return;
1854
1855 term_newln(p);
1856 /*
1857 * XXX Not entirely correct. If `.In foo bar' is specified in
1858 * the SYNOPSIS section, then it produces a single break after
1859 * the <foo>; mandoc asserts a vertical space. Since this
1860 * construction is rarely used, I think it's fine.
1861 */
1862 if (node->next && MDOC_In != node->next->tok)
1863 term_vspace(p);
1864 }
1865
1866
1867 /* ARGSUSED */
1868 static int
1869 termp_sp_pre(DECL_ARGS)
1870 {
1871 int i, len;
1872
1873 switch (node->tok) {
1874 case (MDOC_sp):
1875 len = node->child ? atoi(node->child->string) : 1;
1876 break;
1877 case (MDOC_br):
1878 len = 0;
1879 break;
1880 default:
1881 len = 1;
1882 break;
1883 }
1884
1885 if (0 == len)
1886 term_newln(p);
1887 for (i = 0; i < len; i++)
1888 term_vspace(p);
1889
1890 return(0);
1891 }
1892
1893
1894 /* ARGSUSED */
1895 static int
1896 termp_brq_pre(DECL_ARGS)
1897 {
1898
1899 if (MDOC_BODY != node->type)
1900 return(1);
1901 term_word(p, "\\(lC");
1902 p->flags |= TERMP_NOSPACE;
1903 return(1);
1904 }
1905
1906
1907 /* ARGSUSED */
1908 static void
1909 termp_brq_post(DECL_ARGS)
1910 {
1911
1912 if (MDOC_BODY != node->type)
1913 return;
1914 p->flags |= TERMP_NOSPACE;
1915 term_word(p, "\\(rC");
1916 }
1917
1918
1919 /* ARGSUSED */
1920 static int
1921 termp_bq_pre(DECL_ARGS)
1922 {
1923
1924 if (MDOC_BODY != node->type)
1925 return(1);
1926 term_word(p, "\\(lB");
1927 p->flags |= TERMP_NOSPACE;
1928 return(1);
1929 }
1930
1931
1932 /* ARGSUSED */
1933 static void
1934 termp_bq_post(DECL_ARGS)
1935 {
1936
1937 if (MDOC_BODY != node->type)
1938 return;
1939 p->flags |= TERMP_NOSPACE;
1940 term_word(p, "\\(rB");
1941 }
1942
1943
1944 /* ARGSUSED */
1945 static int
1946 termp_pq_pre(DECL_ARGS)
1947 {
1948
1949 if (MDOC_BODY != node->type)
1950 return(1);
1951 term_word(p, "\\&(");
1952 p->flags |= TERMP_NOSPACE;
1953 return(1);
1954 }
1955
1956
1957 /* ARGSUSED */
1958 static void
1959 termp_pq_post(DECL_ARGS)
1960 {
1961
1962 if (MDOC_BODY != node->type)
1963 return;
1964 term_word(p, ")");
1965 }
1966
1967
1968 /* ARGSUSED */
1969 static int
1970 termp_fo_pre(DECL_ARGS)
1971 {
1972 const struct mdoc_node *n;
1973
1974 if (MDOC_BODY == node->type) {
1975 p->flags |= TERMP_NOSPACE;
1976 term_word(p, "(");
1977 p->flags |= TERMP_NOSPACE;
1978 return(1);
1979 } else if (MDOC_HEAD != node->type)
1980 return(1);
1981
1982 p->bold++;
1983 for (n = node->child; n; n = n->next) {
1984 assert(MDOC_TEXT == n->type);
1985 term_word(p, n->string);
1986 }
1987 p->bold--;
1988
1989 return(0);
1990 }
1991
1992
1993 /* ARGSUSED */
1994 static void
1995 termp_fo_post(DECL_ARGS)
1996 {
1997
1998 if (MDOC_BODY != node->type)
1999 return;
2000 p->flags |= TERMP_NOSPACE;
2001 term_word(p, ")");
2002 p->flags |= TERMP_NOSPACE;
2003 term_word(p, ";");
2004 term_newln(p);
2005 }
2006
2007
2008 /* ARGSUSED */
2009 static int
2010 termp_bf_pre(DECL_ARGS)
2011 {
2012 const struct mdoc_node *n;
2013
2014 if (MDOC_HEAD == node->type)
2015 return(0);
2016 else if (MDOC_BLOCK != node->type)
2017 return(1);
2018
2019 if (NULL == (n = node->head->child)) {
2020 if (arg_hasattr(MDOC_Emphasis, node))
2021 p->under++;
2022 else if (arg_hasattr(MDOC_Symbolic, node))
2023 p->bold++;
2024
2025 return(1);
2026 }
2027
2028 assert(MDOC_TEXT == n->type);
2029 if (0 == strcmp("Em", n->string))
2030 p->under++;
2031 else if (0 == strcmp("Sy", n->string))
2032 p->bold++;
2033
2034 return(1);
2035 }
2036
2037
2038 /* ARGSUSED */
2039 static int
2040 termp_sm_pre(DECL_ARGS)
2041 {
2042
2043 assert(node->child && MDOC_TEXT == node->child->type);
2044 if (0 == strcmp("on", node->child->string)) {
2045 p->flags &= ~TERMP_NONOSPACE;
2046 p->flags &= ~TERMP_NOSPACE;
2047 } else
2048 p->flags |= TERMP_NONOSPACE;
2049
2050 return(0);
2051 }
2052
2053
2054 /* ARGSUSED */
2055 static int
2056 termp_ap_pre(DECL_ARGS)
2057 {
2058
2059 p->flags |= TERMP_NOSPACE;
2060 term_word(p, "\\(aq");
2061 p->flags |= TERMP_NOSPACE;
2062 return(1);
2063 }
2064
2065
2066 /* ARGSUSED */
2067 static void
2068 termp____post(DECL_ARGS)
2069 {
2070
2071 p->flags |= TERMP_NOSPACE;
2072 term_word(p, node->next ? "," : ".");
2073 }
2074
2075
2076 /* ARGSUSED */
2077 static int
2078 termp_lk_pre(DECL_ARGS)
2079 {
2080 const struct mdoc_node *n;
2081
2082 assert(node->child);
2083 n = node->child;
2084
2085 if (NULL == n->next) {
2086 p->under++;
2087 return(1);
2088 }
2089
2090 p->under++;
2091 term_word(p, n->string);
2092 p->flags |= TERMP_NOSPACE;
2093 term_word(p, ":");
2094 p->under--;
2095
2096 p->bold++;
2097 for (n = n->next; n; n = n->next)
2098 term_word(p, n->string);
2099 p->bold--;
2100
2101 return(0);
2102 }
2103
2104
2105 /* ARGSUSED */
2106 static int
2107 termp_under_pre(DECL_ARGS)
2108 {
2109
2110 p->under++;
2111 return(1);
2112 }