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