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