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