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