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