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