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