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