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