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