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