]> git.cameronkatri.com Git - mandoc.git/blob - mdoc_term.c
De Morgan's law not being applied properly. Noted by Ulrich Spoerlein.
[mandoc.git] / mdoc_term.c
1 /* $Id: mdoc_term.c,v 1.136 2010/06/01 14:51:09 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 if (n->sec != SEC_SYNOPSIS || ! (MDOC_LINE & n->flags))
1362 return;
1363
1364 term_newln(p);
1365 if (n->next && MDOC_Fd != n->next->tok)
1366 term_vspace(p);
1367 }
1368
1369
1370 /* ARGSUSED */
1371 static int
1372 termp_sh_pre(DECL_ARGS)
1373 {
1374
1375 /* No vspace between consecutive `Sh' calls. */
1376
1377 switch (n->type) {
1378 case (MDOC_BLOCK):
1379 if (n->prev && MDOC_Sh == n->prev->tok)
1380 if (NULL == n->prev->body->child)
1381 break;
1382 term_vspace(p);
1383 break;
1384 case (MDOC_HEAD):
1385 term_fontpush(p, TERMFONT_BOLD);
1386 break;
1387 case (MDOC_BODY):
1388 p->offset = INDENT;
1389 break;
1390 default:
1391 break;
1392 }
1393 return(1);
1394 }
1395
1396
1397 /* ARGSUSED */
1398 static void
1399 termp_sh_post(DECL_ARGS)
1400 {
1401
1402 switch (n->type) {
1403 case (MDOC_HEAD):
1404 term_newln(p);
1405 break;
1406 case (MDOC_BODY):
1407 term_newln(p);
1408 p->offset = 0;
1409 break;
1410 default:
1411 break;
1412 }
1413 }
1414
1415
1416 /* ARGSUSED */
1417 static int
1418 termp_op_pre(DECL_ARGS)
1419 {
1420
1421 switch (n->type) {
1422 case (MDOC_BODY):
1423 term_word(p, "\\(lB");
1424 p->flags |= TERMP_NOSPACE;
1425 break;
1426 default:
1427 break;
1428 }
1429 return(1);
1430 }
1431
1432
1433 /* ARGSUSED */
1434 static int
1435 termp_bt_pre(DECL_ARGS)
1436 {
1437
1438 term_word(p, "is currently in beta test.");
1439 p->flags |= TERMP_SENTENCE;
1440 return(0);
1441 }
1442
1443
1444 /* ARGSUSED */
1445 static void
1446 termp_lb_post(DECL_ARGS)
1447 {
1448
1449 if (SEC_LIBRARY == n->sec && MDOC_LINE & n->flags)
1450 term_newln(p);
1451 }
1452
1453
1454 /* ARGSUSED */
1455 static int
1456 termp_ud_pre(DECL_ARGS)
1457 {
1458
1459 term_word(p, "currently under development.");
1460 p->flags |= TERMP_SENTENCE;
1461 return(0);
1462 }
1463
1464
1465 /* ARGSUSED */
1466 static int
1467 termp_d1_pre(DECL_ARGS)
1468 {
1469
1470 if (MDOC_BLOCK != n->type)
1471 return(1);
1472 term_newln(p);
1473 p->offset += (INDENT + 1);
1474 return(1);
1475 }
1476
1477
1478 /* ARGSUSED */
1479 static void
1480 termp_d1_post(DECL_ARGS)
1481 {
1482
1483 if (MDOC_BLOCK != n->type)
1484 return;
1485 term_newln(p);
1486 }
1487
1488
1489 /* ARGSUSED */
1490 static int
1491 termp_aq_pre(DECL_ARGS)
1492 {
1493
1494 if (MDOC_BODY != n->type)
1495 return(1);
1496 term_word(p, "\\(la");
1497 p->flags |= TERMP_NOSPACE;
1498 return(1);
1499 }
1500
1501
1502 /* ARGSUSED */
1503 static void
1504 termp_aq_post(DECL_ARGS)
1505 {
1506
1507 if (MDOC_BODY != n->type)
1508 return;
1509 p->flags |= TERMP_NOSPACE;
1510 term_word(p, "\\(ra");
1511 }
1512
1513
1514 /* ARGSUSED */
1515 static int
1516 termp_ft_pre(DECL_ARGS)
1517 {
1518
1519 if (SEC_SYNOPSIS == n->sec && MDOC_LINE & n->flags)
1520 if (n->prev && MDOC_Fo == n->prev->tok)
1521 term_vspace(p);
1522
1523 term_fontpush(p, TERMFONT_UNDER);
1524 return(1);
1525 }
1526
1527
1528 /* ARGSUSED */
1529 static void
1530 termp_ft_post(DECL_ARGS)
1531 {
1532
1533 if (SEC_SYNOPSIS == n->sec && MDOC_LINE & n->flags)
1534 term_newln(p);
1535 }
1536
1537
1538 /* ARGSUSED */
1539 static int
1540 termp_fn_pre(DECL_ARGS)
1541 {
1542 const struct mdoc_node *nn;
1543
1544 term_fontpush(p, TERMFONT_BOLD);
1545 term_word(p, n->child->string);
1546 term_fontpop(p);
1547
1548 p->flags |= TERMP_NOSPACE;
1549 term_word(p, "(");
1550
1551 for (nn = n->child->next; nn; nn = nn->next) {
1552 term_fontpush(p, TERMFONT_UNDER);
1553 term_word(p, nn->string);
1554 term_fontpop(p);
1555
1556 if (nn->next)
1557 term_word(p, ",");
1558 }
1559
1560 term_word(p, ")");
1561
1562 if (SEC_SYNOPSIS == n->sec)
1563 term_word(p, ";");
1564
1565 return(0);
1566 }
1567
1568
1569 /* ARGSUSED */
1570 static void
1571 termp_fn_post(DECL_ARGS)
1572 {
1573
1574 if (n->sec == SEC_SYNOPSIS && n->next && MDOC_LINE & n->flags)
1575 term_vspace(p);
1576 }
1577
1578
1579 /* ARGSUSED */
1580 static int
1581 termp_fa_pre(DECL_ARGS)
1582 {
1583 const struct mdoc_node *nn;
1584
1585 if (n->parent->tok != MDOC_Fo) {
1586 term_fontpush(p, TERMFONT_UNDER);
1587 return(1);
1588 }
1589
1590 for (nn = n->child; nn; nn = nn->next) {
1591 term_fontpush(p, TERMFONT_UNDER);
1592 term_word(p, nn->string);
1593 term_fontpop(p);
1594
1595 if (nn->next)
1596 term_word(p, ",");
1597 }
1598
1599 if (n->child && n->next && n->next->tok == MDOC_Fa)
1600 term_word(p, ",");
1601
1602 return(0);
1603 }
1604
1605
1606 /* ARGSUSED */
1607 static int
1608 termp_bd_pre(DECL_ARGS)
1609 {
1610 size_t tabwidth;
1611 int i, type;
1612 size_t rm, rmax;
1613 const struct mdoc_node *nn;
1614
1615 if (MDOC_BLOCK == n->type) {
1616 print_bvspace(p, n, n);
1617 return(1);
1618 } else if (MDOC_HEAD == n->type)
1619 return(0);
1620
1621 nn = n->parent;
1622
1623 type = arg_disptype(nn);
1624 assert(-1 != type);
1625
1626 if (-1 != (i = arg_getattr(MDOC_Offset, nn)))
1627 p->offset += a2offs(&nn->args->argv[i]);
1628
1629 /*
1630 * If -ragged or -filled are specified, the block does nothing
1631 * but change the indentation. If -unfilled or -literal are
1632 * specified, text is printed exactly as entered in the display:
1633 * for macro lines, a newline is appended to the line. Blank
1634 * lines are allowed.
1635 */
1636
1637 if (MDOC_Literal != type && MDOC_Unfilled != type)
1638 return(1);
1639
1640 tabwidth = p->tabwidth;
1641 p->tabwidth = 8;
1642 rm = p->rmargin;
1643 rmax = p->maxrmargin;
1644 p->rmargin = p->maxrmargin = TERM_MAXMARGIN;
1645
1646 for (nn = n->child; nn; nn = nn->next) {
1647 p->flags |= TERMP_NOSPACE;
1648 print_mdoc_node(p, pair, m, nn);
1649 if (NULL == nn->prev ||
1650 nn->prev->line < nn->line ||
1651 NULL == nn->next)
1652 term_flushln(p);
1653 }
1654 p->tabwidth = tabwidth;
1655
1656 p->rmargin = rm;
1657 p->maxrmargin = rmax;
1658 return(0);
1659 }
1660
1661
1662 /* ARGSUSED */
1663 static void
1664 termp_bd_post(DECL_ARGS)
1665 {
1666 int type;
1667 size_t rm, rmax;
1668
1669 if (MDOC_BODY != n->type)
1670 return;
1671
1672 type = arg_disptype(n->parent);
1673 assert(-1 != type);
1674
1675 rm = p->rmargin;
1676 rmax = p->maxrmargin;
1677
1678 if (MDOC_Literal == type || MDOC_Unfilled == type)
1679 p->rmargin = p->maxrmargin = TERM_MAXMARGIN;
1680
1681 p->flags |= TERMP_NOSPACE;
1682 term_newln(p);
1683
1684 p->rmargin = rm;
1685 p->maxrmargin = rmax;
1686 }
1687
1688
1689 /* ARGSUSED */
1690 static int
1691 termp_qq_pre(DECL_ARGS)
1692 {
1693
1694 if (MDOC_BODY != n->type)
1695 return(1);
1696 term_word(p, "\"");
1697 p->flags |= TERMP_NOSPACE;
1698 return(1);
1699 }
1700
1701
1702 /* ARGSUSED */
1703 static void
1704 termp_qq_post(DECL_ARGS)
1705 {
1706
1707 if (MDOC_BODY != n->type)
1708 return;
1709 p->flags |= TERMP_NOSPACE;
1710 term_word(p, "\"");
1711 }
1712
1713
1714 /* ARGSUSED */
1715 static void
1716 termp_bx_post(DECL_ARGS)
1717 {
1718
1719 if (n->child)
1720 p->flags |= TERMP_NOSPACE;
1721 term_word(p, "BSD");
1722 }
1723
1724
1725 /* ARGSUSED */
1726 static int
1727 termp_xx_pre(DECL_ARGS)
1728 {
1729 const char *pp;
1730
1731 pp = NULL;
1732 switch (n->tok) {
1733 case (MDOC_Bsx):
1734 pp = "BSDI BSD/OS";
1735 break;
1736 case (MDOC_Dx):
1737 pp = "DragonFly";
1738 break;
1739 case (MDOC_Fx):
1740 pp = "FreeBSD";
1741 break;
1742 case (MDOC_Nx):
1743 pp = "NetBSD";
1744 break;
1745 case (MDOC_Ox):
1746 pp = "OpenBSD";
1747 break;
1748 case (MDOC_Ux):
1749 pp = "UNIX";
1750 break;
1751 default:
1752 break;
1753 }
1754
1755 assert(pp);
1756 term_word(p, pp);
1757 return(1);
1758 }
1759
1760
1761 /* ARGSUSED */
1762 static int
1763 termp_sq_pre(DECL_ARGS)
1764 {
1765
1766 if (MDOC_BODY != n->type)
1767 return(1);
1768 term_word(p, "\\(oq");
1769 p->flags |= TERMP_NOSPACE;
1770 return(1);
1771 }
1772
1773
1774 /* ARGSUSED */
1775 static void
1776 termp_sq_post(DECL_ARGS)
1777 {
1778
1779 if (MDOC_BODY != n->type)
1780 return;
1781 p->flags |= TERMP_NOSPACE;
1782 term_word(p, "\\(aq");
1783 }
1784
1785
1786 /* ARGSUSED */
1787 static int
1788 termp_pf_pre(DECL_ARGS)
1789 {
1790
1791 p->flags |= TERMP_IGNDELIM;
1792 return(1);
1793 }
1794
1795
1796 /* ARGSUSED */
1797 static void
1798 termp_pf_post(DECL_ARGS)
1799 {
1800
1801 p->flags &= ~TERMP_IGNDELIM;
1802 p->flags |= TERMP_NOSPACE;
1803 }
1804
1805
1806 /* ARGSUSED */
1807 static int
1808 termp_ss_pre(DECL_ARGS)
1809 {
1810
1811 switch (n->type) {
1812 case (MDOC_BLOCK):
1813 term_newln(p);
1814 if (n->prev)
1815 term_vspace(p);
1816 break;
1817 case (MDOC_HEAD):
1818 term_fontpush(p, TERMFONT_BOLD);
1819 p->offset = HALFINDENT;
1820 break;
1821 default:
1822 break;
1823 }
1824
1825 return(1);
1826 }
1827
1828
1829 /* ARGSUSED */
1830 static void
1831 termp_ss_post(DECL_ARGS)
1832 {
1833
1834 if (MDOC_HEAD == n->type)
1835 term_newln(p);
1836 }
1837
1838
1839 /* ARGSUSED */
1840 static int
1841 termp_cd_pre(DECL_ARGS)
1842 {
1843
1844 term_fontpush(p, TERMFONT_BOLD);
1845 term_newln(p);
1846 return(1);
1847 }
1848
1849
1850 /* ARGSUSED */
1851 static int
1852 termp_in_pre(DECL_ARGS)
1853 {
1854
1855 term_fontpush(p, TERMFONT_BOLD);
1856 if (SEC_SYNOPSIS == n->sec)
1857 term_word(p, "#include");
1858
1859 term_word(p, "<");
1860 p->flags |= TERMP_NOSPACE;
1861 return(1);
1862 }
1863
1864
1865 /* ARGSUSED */
1866 static void
1867 termp_in_post(DECL_ARGS)
1868 {
1869
1870 term_fontpush(p, TERMFONT_BOLD);
1871 p->flags |= TERMP_NOSPACE;
1872 term_word(p, ">");
1873 term_fontpop(p);
1874
1875 if (SEC_SYNOPSIS != n->sec || ! (MDOC_LINE & n->flags))
1876 return;
1877
1878 term_newln(p);
1879 /*
1880 * XXX Not entirely correct. If `.In foo bar' is specified in
1881 * the SYNOPSIS section, then it produces a single break after
1882 * the <foo>; mandoc asserts a vertical space. Since this
1883 * construction is rarely used, I think it's fine.
1884 */
1885 if (n->next && MDOC_In != n->next->tok)
1886 term_vspace(p);
1887 }
1888
1889
1890 /* ARGSUSED */
1891 static int
1892 termp_sp_pre(DECL_ARGS)
1893 {
1894 size_t i, len;
1895
1896 switch (n->tok) {
1897 case (MDOC_sp):
1898 len = n->child ? a2height(n->child) : 1;
1899 break;
1900 case (MDOC_br):
1901 len = 0;
1902 break;
1903 default:
1904 len = 1;
1905 break;
1906 }
1907
1908 if (0 == len)
1909 term_newln(p);
1910 for (i = 0; i < len; i++)
1911 term_vspace(p);
1912
1913 return(0);
1914 }
1915
1916
1917 /* ARGSUSED */
1918 static int
1919 termp_brq_pre(DECL_ARGS)
1920 {
1921
1922 if (MDOC_BODY != n->type)
1923 return(1);
1924 term_word(p, "\\(lC");
1925 p->flags |= TERMP_NOSPACE;
1926 return(1);
1927 }
1928
1929
1930 /* ARGSUSED */
1931 static void
1932 termp_brq_post(DECL_ARGS)
1933 {
1934
1935 if (MDOC_BODY != n->type)
1936 return;
1937 p->flags |= TERMP_NOSPACE;
1938 term_word(p, "\\(rC");
1939 }
1940
1941
1942 /* ARGSUSED */
1943 static int
1944 termp_bq_pre(DECL_ARGS)
1945 {
1946
1947 if (MDOC_BODY != n->type)
1948 return(1);
1949 term_word(p, "\\(lB");
1950 p->flags |= TERMP_NOSPACE;
1951 return(1);
1952 }
1953
1954
1955 /* ARGSUSED */
1956 static void
1957 termp_bq_post(DECL_ARGS)
1958 {
1959
1960 if (MDOC_BODY != n->type)
1961 return;
1962 p->flags |= TERMP_NOSPACE;
1963 term_word(p, "\\(rB");
1964 }
1965
1966
1967 /* ARGSUSED */
1968 static int
1969 termp_pq_pre(DECL_ARGS)
1970 {
1971
1972 if (MDOC_BODY != n->type)
1973 return(1);
1974 term_word(p, "\\&(");
1975 p->flags |= TERMP_NOSPACE;
1976 return(1);
1977 }
1978
1979
1980 /* ARGSUSED */
1981 static void
1982 termp_pq_post(DECL_ARGS)
1983 {
1984
1985 if (MDOC_BODY != n->type)
1986 return;
1987 term_word(p, ")");
1988 }
1989
1990
1991 /* ARGSUSED */
1992 static int
1993 termp_fo_pre(DECL_ARGS)
1994 {
1995 const struct mdoc_node *nn;
1996
1997 if (MDOC_BODY == n->type) {
1998 p->flags |= TERMP_NOSPACE;
1999 term_word(p, "(");
2000 p->flags |= TERMP_NOSPACE;
2001 return(1);
2002 } else if (MDOC_HEAD != n->type)
2003 return(1);
2004
2005 term_fontpush(p, TERMFONT_BOLD);
2006 for (nn = n->child; nn; nn = nn->next) {
2007 assert(MDOC_TEXT == nn->type);
2008 term_word(p, nn->string);
2009 }
2010 term_fontpop(p);
2011
2012 return(0);
2013 }
2014
2015
2016 /* ARGSUSED */
2017 static void
2018 termp_fo_post(DECL_ARGS)
2019 {
2020
2021 if (MDOC_BODY != n->type)
2022 return;
2023 p->flags |= TERMP_NOSPACE;
2024 term_word(p, ")");
2025 p->flags |= TERMP_NOSPACE;
2026 term_word(p, ";");
2027 term_newln(p);
2028 }
2029
2030
2031 /* ARGSUSED */
2032 static int
2033 termp_bf_pre(DECL_ARGS)
2034 {
2035 const struct mdoc_node *nn;
2036
2037 if (MDOC_HEAD == n->type)
2038 return(0);
2039 else if (MDOC_BLOCK != n->type)
2040 return(1);
2041
2042 if (NULL == (nn = n->head->child)) {
2043 if (arg_hasattr(MDOC_Emphasis, n))
2044 term_fontpush(p, TERMFONT_UNDER);
2045 else if (arg_hasattr(MDOC_Symbolic, n))
2046 term_fontpush(p, TERMFONT_BOLD);
2047 else
2048 term_fontpush(p, TERMFONT_NONE);
2049
2050 return(1);
2051 }
2052
2053 assert(MDOC_TEXT == nn->type);
2054 if (0 == strcmp("Em", nn->string))
2055 term_fontpush(p, TERMFONT_UNDER);
2056 else if (0 == strcmp("Sy", nn->string))
2057 term_fontpush(p, TERMFONT_BOLD);
2058 else
2059 term_fontpush(p, TERMFONT_NONE);
2060
2061 return(1);
2062 }
2063
2064
2065 /* ARGSUSED */
2066 static int
2067 termp_sm_pre(DECL_ARGS)
2068 {
2069
2070 assert(n->child && MDOC_TEXT == n->child->type);
2071 if (0 == strcmp("on", n->child->string))
2072 p->flags &= ~TERMP_NONOSPACE;
2073 else
2074 p->flags |= TERMP_NONOSPACE;
2075
2076 return(0);
2077 }
2078
2079
2080 /* ARGSUSED */
2081 static int
2082 termp_ap_pre(DECL_ARGS)
2083 {
2084
2085 p->flags |= TERMP_NOSPACE;
2086 term_word(p, "\\(aq");
2087 p->flags |= TERMP_NOSPACE;
2088 return(1);
2089 }
2090
2091
2092 /* ARGSUSED */
2093 static void
2094 termp____post(DECL_ARGS)
2095 {
2096
2097 /* TODO: %U. */
2098
2099 p->flags |= TERMP_NOSPACE;
2100 term_word(p, n->next ? "," : ".");
2101 }
2102
2103
2104 /* ARGSUSED */
2105 static int
2106 termp_li_pre(DECL_ARGS)
2107 {
2108
2109 term_fontpush(p, TERMFONT_NONE);
2110 return(1);
2111 }
2112
2113
2114 /* ARGSUSED */
2115 static int
2116 termp_lk_pre(DECL_ARGS)
2117 {
2118 const struct mdoc_node *nn;
2119
2120 term_fontpush(p, TERMFONT_UNDER);
2121 nn = n->child;
2122
2123 if (NULL == nn->next)
2124 return(1);
2125
2126 term_word(p, nn->string);
2127 term_fontpop(p);
2128
2129 p->flags |= TERMP_NOSPACE;
2130 term_word(p, ":");
2131
2132 term_fontpush(p, TERMFONT_BOLD);
2133 for (nn = nn->next; nn; nn = nn->next)
2134 term_word(p, nn->string);
2135 term_fontpop(p);
2136
2137 return(0);
2138 }
2139
2140
2141 /* ARGSUSED */
2142 static int
2143 termp_under_pre(DECL_ARGS)
2144 {
2145
2146 term_fontpush(p, TERMFONT_UNDER);
2147 return(1);
2148 }