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