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