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