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