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