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