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