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