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