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