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