]> git.cameronkatri.com Git - mandoc.git/blob - mdoc_term.c
Fixed `Fn' newline behaviour and added some regression tests to this
[mandoc.git] / mdoc_term.c
1 /* $Id: mdoc_term.c,v 1.139 2010/06/04 22:16:27 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)
1542 term_vspace(p);
1543 }
1544
1545 term_fontpush(p, TERMFONT_BOLD);
1546 term_word(p, n->child->string);
1547 term_fontpop(p);
1548
1549 p->flags |= TERMP_NOSPACE;
1550 term_word(p, "(");
1551
1552 for (nn = n->child->next; nn; nn = nn->next) {
1553 term_fontpush(p, TERMFONT_UNDER);
1554 term_word(p, nn->string);
1555 term_fontpop(p);
1556
1557 if (nn->next)
1558 term_word(p, ",");
1559 }
1560
1561 term_word(p, ")");
1562
1563 if (SEC_SYNOPSIS == n->sec)
1564 term_word(p, ";");
1565
1566 return(0);
1567 }
1568
1569
1570 /* ARGSUSED */
1571 static void
1572 termp_fn_post(DECL_ARGS)
1573 {
1574
1575 /* NB: MDOC_LINE has no effect on this macro! */
1576 if (SEC_SYNOPSIS == n->sec)
1577 term_newln(p);
1578 }
1579
1580
1581 /* ARGSUSED */
1582 static int
1583 termp_fa_pre(DECL_ARGS)
1584 {
1585 const struct mdoc_node *nn;
1586
1587 if (n->parent->tok != MDOC_Fo) {
1588 term_fontpush(p, TERMFONT_UNDER);
1589 return(1);
1590 }
1591
1592 for (nn = n->child; nn; nn = nn->next) {
1593 term_fontpush(p, TERMFONT_UNDER);
1594 term_word(p, nn->string);
1595 term_fontpop(p);
1596
1597 if (nn->next)
1598 term_word(p, ",");
1599 }
1600
1601 if (n->child && n->next && n->next->tok == MDOC_Fa)
1602 term_word(p, ",");
1603
1604 return(0);
1605 }
1606
1607
1608 /* ARGSUSED */
1609 static int
1610 termp_bd_pre(DECL_ARGS)
1611 {
1612 size_t tabwidth;
1613 int i, type;
1614 size_t rm, rmax;
1615 const struct mdoc_node *nn;
1616
1617 if (MDOC_BLOCK == n->type) {
1618 print_bvspace(p, n, n);
1619 return(1);
1620 } else if (MDOC_HEAD == n->type)
1621 return(0);
1622
1623 nn = n->parent;
1624
1625 type = arg_disptype(nn);
1626 assert(-1 != type);
1627
1628 if (-1 != (i = arg_getattr(MDOC_Offset, nn)))
1629 p->offset += a2offs(&nn->args->argv[i]);
1630
1631 /*
1632 * If -ragged or -filled are specified, the block does nothing
1633 * but change the indentation. If -unfilled or -literal are
1634 * specified, text is printed exactly as entered in the display:
1635 * for macro lines, a newline is appended to the line. Blank
1636 * lines are allowed.
1637 */
1638
1639 if (MDOC_Literal != type && MDOC_Unfilled != type)
1640 return(1);
1641
1642 tabwidth = p->tabwidth;
1643 p->tabwidth = 8;
1644 rm = p->rmargin;
1645 rmax = p->maxrmargin;
1646 p->rmargin = p->maxrmargin = TERM_MAXMARGIN;
1647
1648 for (nn = n->child; nn; nn = nn->next) {
1649 p->flags |= TERMP_NOSPACE;
1650 print_mdoc_node(p, pair, m, nn);
1651 if (NULL == nn->prev ||
1652 nn->prev->line < nn->line ||
1653 NULL == nn->next)
1654 term_flushln(p);
1655 }
1656 p->tabwidth = tabwidth;
1657
1658 p->rmargin = rm;
1659 p->maxrmargin = rmax;
1660 return(0);
1661 }
1662
1663
1664 /* ARGSUSED */
1665 static void
1666 termp_bd_post(DECL_ARGS)
1667 {
1668 int type;
1669 size_t rm, rmax;
1670
1671 if (MDOC_BODY != n->type)
1672 return;
1673
1674 type = arg_disptype(n->parent);
1675 assert(-1 != type);
1676
1677 rm = p->rmargin;
1678 rmax = p->maxrmargin;
1679
1680 if (MDOC_Literal == type || MDOC_Unfilled == type)
1681 p->rmargin = p->maxrmargin = TERM_MAXMARGIN;
1682
1683 p->flags |= TERMP_NOSPACE;
1684 term_newln(p);
1685
1686 p->rmargin = rm;
1687 p->maxrmargin = rmax;
1688 }
1689
1690
1691 /* ARGSUSED */
1692 static int
1693 termp_qq_pre(DECL_ARGS)
1694 {
1695
1696 if (MDOC_BODY != n->type)
1697 return(1);
1698 term_word(p, "\"");
1699 p->flags |= TERMP_NOSPACE;
1700 return(1);
1701 }
1702
1703
1704 /* ARGSUSED */
1705 static void
1706 termp_qq_post(DECL_ARGS)
1707 {
1708
1709 if (MDOC_BODY != n->type)
1710 return;
1711 p->flags |= TERMP_NOSPACE;
1712 term_word(p, "\"");
1713 }
1714
1715
1716 /* ARGSUSED */
1717 static void
1718 termp_bx_post(DECL_ARGS)
1719 {
1720
1721 if (n->child)
1722 p->flags |= TERMP_NOSPACE;
1723 term_word(p, "BSD");
1724 }
1725
1726
1727 /* ARGSUSED */
1728 static int
1729 termp_xx_pre(DECL_ARGS)
1730 {
1731 const char *pp;
1732
1733 pp = NULL;
1734 switch (n->tok) {
1735 case (MDOC_Bsx):
1736 pp = "BSDI BSD/OS";
1737 break;
1738 case (MDOC_Dx):
1739 pp = "DragonFly";
1740 break;
1741 case (MDOC_Fx):
1742 pp = "FreeBSD";
1743 break;
1744 case (MDOC_Nx):
1745 pp = "NetBSD";
1746 break;
1747 case (MDOC_Ox):
1748 pp = "OpenBSD";
1749 break;
1750 case (MDOC_Ux):
1751 pp = "UNIX";
1752 break;
1753 default:
1754 break;
1755 }
1756
1757 assert(pp);
1758 term_word(p, pp);
1759 return(1);
1760 }
1761
1762
1763 /* ARGSUSED */
1764 static int
1765 termp_sq_pre(DECL_ARGS)
1766 {
1767
1768 if (MDOC_BODY != n->type)
1769 return(1);
1770 term_word(p, "\\(oq");
1771 p->flags |= TERMP_NOSPACE;
1772 return(1);
1773 }
1774
1775
1776 /* ARGSUSED */
1777 static void
1778 termp_sq_post(DECL_ARGS)
1779 {
1780
1781 if (MDOC_BODY != n->type)
1782 return;
1783 p->flags |= TERMP_NOSPACE;
1784 term_word(p, "\\(aq");
1785 }
1786
1787
1788 /* ARGSUSED */
1789 static int
1790 termp_pf_pre(DECL_ARGS)
1791 {
1792
1793 p->flags |= TERMP_IGNDELIM;
1794 return(1);
1795 }
1796
1797
1798 /* ARGSUSED */
1799 static void
1800 termp_pf_post(DECL_ARGS)
1801 {
1802
1803 p->flags &= ~TERMP_IGNDELIM;
1804 p->flags |= TERMP_NOSPACE;
1805 }
1806
1807
1808 /* ARGSUSED */
1809 static int
1810 termp_ss_pre(DECL_ARGS)
1811 {
1812
1813 switch (n->type) {
1814 case (MDOC_BLOCK):
1815 term_newln(p);
1816 if (n->prev)
1817 term_vspace(p);
1818 break;
1819 case (MDOC_HEAD):
1820 term_fontpush(p, TERMFONT_BOLD);
1821 p->offset = HALFINDENT;
1822 break;
1823 default:
1824 break;
1825 }
1826
1827 return(1);
1828 }
1829
1830
1831 /* ARGSUSED */
1832 static void
1833 termp_ss_post(DECL_ARGS)
1834 {
1835
1836 if (MDOC_HEAD == n->type)
1837 term_newln(p);
1838 }
1839
1840
1841 /* ARGSUSED */
1842 static int
1843 termp_cd_pre(DECL_ARGS)
1844 {
1845
1846 term_fontpush(p, TERMFONT_BOLD);
1847 term_newln(p);
1848 return(1);
1849 }
1850
1851
1852 /* ARGSUSED */
1853 static int
1854 termp_in_pre(DECL_ARGS)
1855 {
1856
1857 if (SEC_SYNOPSIS == n->sec && MDOC_LINE & n->flags) {
1858 term_fontpush(p, TERMFONT_BOLD);
1859 term_word(p, "#include");
1860 term_word(p, "<");
1861 } else {
1862 term_word(p, "<");
1863 term_fontpush(p, TERMFONT_UNDER);
1864 }
1865
1866 p->flags |= TERMP_NOSPACE;
1867 return(1);
1868 }
1869
1870
1871 /* ARGSUSED */
1872 static void
1873 termp_in_post(DECL_ARGS)
1874 {
1875
1876 if (SEC_SYNOPSIS == n->sec && MDOC_LINE & n->flags)
1877 term_fontpush(p, TERMFONT_BOLD);
1878
1879 p->flags |= TERMP_NOSPACE;
1880 term_word(p, ">");
1881
1882 if (SEC_SYNOPSIS == n->sec && MDOC_LINE & n->flags) {
1883 term_fontpop(p);
1884 term_newln(p);
1885 }
1886 }
1887
1888
1889 /* ARGSUSED */
1890 static int
1891 termp_sp_pre(DECL_ARGS)
1892 {
1893 size_t i, len;
1894
1895 switch (n->tok) {
1896 case (MDOC_sp):
1897 len = n->child ? a2height(n->child) : 1;
1898 break;
1899 case (MDOC_br):
1900 len = 0;
1901 break;
1902 default:
1903 len = 1;
1904 break;
1905 }
1906
1907 if (0 == len)
1908 term_newln(p);
1909 for (i = 0; i < len; i++)
1910 term_vspace(p);
1911
1912 return(0);
1913 }
1914
1915
1916 /* ARGSUSED */
1917 static int
1918 termp_brq_pre(DECL_ARGS)
1919 {
1920
1921 if (MDOC_BODY != n->type)
1922 return(1);
1923 term_word(p, "\\(lC");
1924 p->flags |= TERMP_NOSPACE;
1925 return(1);
1926 }
1927
1928
1929 /* ARGSUSED */
1930 static void
1931 termp_brq_post(DECL_ARGS)
1932 {
1933
1934 if (MDOC_BODY != n->type)
1935 return;
1936 p->flags |= TERMP_NOSPACE;
1937 term_word(p, "\\(rC");
1938 }
1939
1940
1941 /* ARGSUSED */
1942 static int
1943 termp_bq_pre(DECL_ARGS)
1944 {
1945
1946 if (MDOC_BODY != n->type)
1947 return(1);
1948 term_word(p, "\\(lB");
1949 p->flags |= TERMP_NOSPACE;
1950 return(1);
1951 }
1952
1953
1954 /* ARGSUSED */
1955 static void
1956 termp_bq_post(DECL_ARGS)
1957 {
1958
1959 if (MDOC_BODY != n->type)
1960 return;
1961 p->flags |= TERMP_NOSPACE;
1962 term_word(p, "\\(rB");
1963 }
1964
1965
1966 /* ARGSUSED */
1967 static int
1968 termp_pq_pre(DECL_ARGS)
1969 {
1970
1971 if (MDOC_BODY != n->type)
1972 return(1);
1973 term_word(p, "\\&(");
1974 p->flags |= TERMP_NOSPACE;
1975 return(1);
1976 }
1977
1978
1979 /* ARGSUSED */
1980 static void
1981 termp_pq_post(DECL_ARGS)
1982 {
1983
1984 if (MDOC_BODY != n->type)
1985 return;
1986 term_word(p, ")");
1987 }
1988
1989
1990 /* ARGSUSED */
1991 static int
1992 termp_fo_pre(DECL_ARGS)
1993 {
1994 const struct mdoc_node *nn;
1995
1996 if (MDOC_BODY == n->type) {
1997 p->flags |= TERMP_NOSPACE;
1998 term_word(p, "(");
1999 p->flags |= TERMP_NOSPACE;
2000 return(1);
2001 } else if (MDOC_HEAD != n->type)
2002 return(1);
2003
2004 term_fontpush(p, TERMFONT_BOLD);
2005 for (nn = n->child; nn; nn = nn->next) {
2006 assert(MDOC_TEXT == nn->type);
2007 term_word(p, nn->string);
2008 }
2009 term_fontpop(p);
2010
2011 return(0);
2012 }
2013
2014
2015 /* ARGSUSED */
2016 static void
2017 termp_fo_post(DECL_ARGS)
2018 {
2019
2020 if (MDOC_BODY != n->type)
2021 return;
2022 p->flags |= TERMP_NOSPACE;
2023 term_word(p, ")");
2024 p->flags |= TERMP_NOSPACE;
2025 term_word(p, ";");
2026 term_newln(p);
2027 }
2028
2029
2030 /* ARGSUSED */
2031 static int
2032 termp_bf_pre(DECL_ARGS)
2033 {
2034 const struct mdoc_node *nn;
2035
2036 if (MDOC_HEAD == n->type)
2037 return(0);
2038 else if (MDOC_BLOCK != n->type)
2039 return(1);
2040
2041 if (NULL == (nn = n->head->child)) {
2042 if (arg_hasattr(MDOC_Emphasis, n))
2043 term_fontpush(p, TERMFONT_UNDER);
2044 else if (arg_hasattr(MDOC_Symbolic, n))
2045 term_fontpush(p, TERMFONT_BOLD);
2046 else
2047 term_fontpush(p, TERMFONT_NONE);
2048
2049 return(1);
2050 }
2051
2052 assert(MDOC_TEXT == nn->type);
2053 if (0 == strcmp("Em", nn->string))
2054 term_fontpush(p, TERMFONT_UNDER);
2055 else if (0 == strcmp("Sy", nn->string))
2056 term_fontpush(p, TERMFONT_BOLD);
2057 else
2058 term_fontpush(p, TERMFONT_NONE);
2059
2060 return(1);
2061 }
2062
2063
2064 /* ARGSUSED */
2065 static int
2066 termp_sm_pre(DECL_ARGS)
2067 {
2068
2069 assert(n->child && MDOC_TEXT == n->child->type);
2070 if (0 == strcmp("on", n->child->string))
2071 p->flags &= ~TERMP_NONOSPACE;
2072 else
2073 p->flags |= TERMP_NONOSPACE;
2074
2075 return(0);
2076 }
2077
2078
2079 /* ARGSUSED */
2080 static int
2081 termp_ap_pre(DECL_ARGS)
2082 {
2083
2084 p->flags |= TERMP_NOSPACE;
2085 term_word(p, "\\(aq");
2086 p->flags |= TERMP_NOSPACE;
2087 return(1);
2088 }
2089
2090
2091 /* ARGSUSED */
2092 static void
2093 termp____post(DECL_ARGS)
2094 {
2095
2096 /* TODO: %U. */
2097
2098 p->flags |= TERMP_NOSPACE;
2099 term_word(p, n->next ? "," : ".");
2100 }
2101
2102
2103 /* ARGSUSED */
2104 static int
2105 termp_li_pre(DECL_ARGS)
2106 {
2107
2108 term_fontpush(p, TERMFONT_NONE);
2109 return(1);
2110 }
2111
2112
2113 /* ARGSUSED */
2114 static int
2115 termp_lk_pre(DECL_ARGS)
2116 {
2117 const struct mdoc_node *nn;
2118
2119 term_fontpush(p, TERMFONT_UNDER);
2120 nn = n->child;
2121
2122 if (NULL == nn->next)
2123 return(1);
2124
2125 term_word(p, nn->string);
2126 term_fontpop(p);
2127
2128 p->flags |= TERMP_NOSPACE;
2129 term_word(p, ":");
2130
2131 term_fontpush(p, TERMFONT_BOLD);
2132 for (nn = nn->next; nn; nn = nn->next)
2133 term_word(p, nn->string);
2134 term_fontpop(p);
2135
2136 return(0);
2137 }
2138
2139
2140 /* ARGSUSED */
2141 static int
2142 termp_under_pre(DECL_ARGS)
2143 {
2144
2145 term_fontpush(p, TERMFONT_UNDER);
2146 return(1);
2147 }