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