Annotation of mandoc/mdoc_term.c, Revision 1.290
1.290 ! schwarze 1: /* $Id: mdoc_term.c,v 1.289 2014/11/10 21:56:43 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);
1.268 schwarze 94: static int termp_es_pre(DECL_ARGS);
1.31 kristaps 95: static int termp_ex_pre(DECL_ARGS);
96: static int termp_fa_pre(DECL_ARGS);
1.188 kristaps 97: static int termp_fd_pre(DECL_ARGS);
1.31 kristaps 98: static int termp_fl_pre(DECL_ARGS);
99: static int termp_fn_pre(DECL_ARGS);
100: static int termp_fo_pre(DECL_ARGS);
101: static int termp_ft_pre(DECL_ARGS);
102: static int termp_in_pre(DECL_ARGS);
103: static int termp_it_pre(DECL_ARGS);
1.102 kristaps 104: static int termp_li_pre(DECL_ARGS);
1.260 schwarze 105: static int termp_ll_pre(DECL_ARGS);
1.31 kristaps 106: static int termp_lk_pre(DECL_ARGS);
107: static int termp_nd_pre(DECL_ARGS);
108: static int termp_nm_pre(DECL_ARGS);
109: static int termp_ns_pre(DECL_ARGS);
1.188 kristaps 110: static int termp_quote_pre(DECL_ARGS);
1.31 kristaps 111: static int termp_rs_pre(DECL_ARGS);
112: static int termp_rv_pre(DECL_ARGS);
113: static int termp_sh_pre(DECL_ARGS);
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.1 kristaps 186: { NULL, NULL }, /* Db */
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.268 schwarze 238: { termp_es_pre, NULL }, /* Es */
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:
353: if (termacts[n->tok].pre && ENDBODY_NOT == n->end)
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.144 kristaps 408:
1.245 schwarze 409: meta = (const struct mdoc_meta *)arg;
1.1 kristaps 410:
1.102 kristaps 411: term_fontrepl(p, TERMFONT_NONE);
1.101 kristaps 412:
1.264 schwarze 413: /*
1.9 kristaps 414: * Output the footer in new-groff style, that is, three columns
415: * with the middle being the manual date and flanking columns
416: * being the operating system:
417: *
418: * SYSTEM DATE SYSTEM
419: */
420:
1.1 kristaps 421: term_vspace(p);
422:
1.9 kristaps 423: p->offset = 0;
1.264 schwarze 424: p->rmargin = (p->maxrmargin -
425: term_strlen(p, meta->date) + term_len(p, 1)) / 2;
1.250 schwarze 426: p->trailspace = 1;
1.1 kristaps 427: p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
428:
1.245 schwarze 429: term_word(p, meta->os);
1.1 kristaps 430: term_flushln(p);
431:
1.9 kristaps 432: p->offset = p->rmargin;
1.245 schwarze 433: p->rmargin = p->maxrmargin - term_strlen(p, meta->os);
1.234 schwarze 434: p->flags |= TERMP_NOSPACE;
1.9 kristaps 435:
1.245 schwarze 436: term_word(p, meta->date);
1.9 kristaps 437: term_flushln(p);
438:
1.1 kristaps 439: p->offset = p->rmargin;
440: p->rmargin = p->maxrmargin;
1.250 schwarze 441: p->trailspace = 0;
1.1 kristaps 442: p->flags &= ~TERMP_NOBREAK;
1.234 schwarze 443: p->flags |= TERMP_NOSPACE;
1.1 kristaps 444:
1.245 schwarze 445: term_word(p, meta->os);
1.1 kristaps 446: term_flushln(p);
447:
1.9 kristaps 448: p->offset = 0;
449: p->rmargin = p->maxrmargin;
450: p->flags = 0;
1.1 kristaps 451: }
452:
1.3 kristaps 453: static void
1.144 kristaps 454: print_mdoc_head(struct termp *p, const void *arg)
1.1 kristaps 455: {
1.266 schwarze 456: const struct mdoc_meta *meta;
1.267 schwarze 457: char *volume, *title;
458: size_t vollen, titlen;
1.144 kristaps 459:
1.245 schwarze 460: meta = (const struct mdoc_meta *)arg;
1.1 kristaps 461:
462: /*
463: * The header is strange. It has three components, which are
464: * really two with the first duplicated. It goes like this:
465: *
466: * IDENTIFIER TITLE IDENTIFIER
467: *
468: * The IDENTIFIER is NAME(SECTION), which is the command-name
469: * (if given, or "unknown" if not) followed by the manual page
470: * section. These are given in `Dt'. The TITLE is a free-form
471: * string depending on the manual volume. If not specified, it
472: * switches on the manual section.
473: */
1.235 schwarze 474:
1.245 schwarze 475: assert(meta->vol);
1.267 schwarze 476: if (NULL == meta->arch)
477: volume = mandoc_strdup(meta->vol);
478: else
479: mandoc_asprintf(&volume, "%s (%s)",
480: meta->vol, meta->arch);
481: vollen = term_strlen(p, volume);
1.1 kristaps 482:
1.275 schwarze 483: if (NULL == meta->msec)
484: title = mandoc_strdup(meta->title);
485: else
486: mandoc_asprintf(&title, "%s(%s)",
487: meta->title, meta->msec);
1.235 schwarze 488: titlen = term_strlen(p, title);
1.1 kristaps 489:
1.235 schwarze 490: p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
1.250 schwarze 491: p->trailspace = 1;
1.1 kristaps 492: p->offset = 0;
1.267 schwarze 493: p->rmargin = 2 * (titlen+1) + vollen < p->maxrmargin ?
494: (p->maxrmargin - vollen + term_len(p, 1)) / 2 :
495: p->maxrmargin - vollen;
1.1 kristaps 496:
497: term_word(p, title);
498: term_flushln(p);
499:
1.235 schwarze 500: p->flags |= TERMP_NOSPACE;
1.1 kristaps 501: p->offset = p->rmargin;
1.267 schwarze 502: p->rmargin = p->offset + vollen + titlen < p->maxrmargin ?
1.235 schwarze 503: p->maxrmargin - titlen : p->maxrmargin;
1.1 kristaps 504:
1.267 schwarze 505: term_word(p, volume);
1.1 kristaps 506: term_flushln(p);
507:
508: p->flags &= ~TERMP_NOBREAK;
1.250 schwarze 509: p->trailspace = 0;
1.235 schwarze 510: if (p->rmargin + titlen <= p->maxrmargin) {
511: p->flags |= TERMP_NOSPACE;
512: p->offset = p->rmargin;
513: p->rmargin = p->maxrmargin;
514: term_word(p, title);
515: term_flushln(p);
516: }
1.1 kristaps 517:
1.235 schwarze 518: p->flags &= ~TERMP_NOSPACE;
1.9 kristaps 519: p->offset = 0;
1.1 kristaps 520: p->rmargin = p->maxrmargin;
1.266 schwarze 521: free(title);
1.267 schwarze 522: free(volume);
1.1 kristaps 523: }
524:
525: static size_t
1.157 kristaps 526: a2height(const struct termp *p, const char *v)
1.1 kristaps 527: {
1.92 kristaps 528: struct roffsu su;
1.91 kristaps 529:
1.231 kristaps 530:
1.157 kristaps 531: assert(v);
532: if ( ! a2roffsu(v, &su, SCALE_VS))
1.231 kristaps 533: SCALE_VS_INIT(&su, atoi(v));
1.91 kristaps 534:
1.157 kristaps 535: return(term_vspan(p, &su));
1.91 kristaps 536: }
1.1 kristaps 537:
1.91 kristaps 538: static size_t
1.157 kristaps 539: a2width(const struct termp *p, const char *v)
1.91 kristaps 540: {
1.92 kristaps 541: struct roffsu su;
1.1 kristaps 542:
1.154 kristaps 543: assert(v);
1.283 schwarze 544: if ( ! a2roffsu(v, &su, SCALE_MAX)) {
1.157 kristaps 545: SCALE_HS_INIT(&su, term_strlen(p, v));
1.283 schwarze 546: su.scale /= term_strlen(p, "0");
547: }
1.1 kristaps 548:
1.157 kristaps 549: return(term_hspan(p, &su));
1.1 kristaps 550: }
551:
1.108 kristaps 552: /*
553: * Determine how much space to print out before block elements of `It'
554: * (and thus `Bl') and `Bd'. And then go ahead and print that space,
555: * too.
556: */
1.54 kristaps 557: static void
1.264 schwarze 558: print_bvspace(struct termp *p,
559: const struct mdoc_node *bl,
560: const struct mdoc_node *n)
1.1 kristaps 561: {
1.86 kristaps 562: const struct mdoc_node *nn;
1.236 schwarze 563:
564: assert(n);
1.1 kristaps 565:
566: term_newln(p);
1.150 kristaps 567:
1.202 kristaps 568: if (MDOC_Bd == bl->tok && bl->norm->Bd.comp)
1.150 kristaps 569: return;
1.202 kristaps 570: if (MDOC_Bl == bl->tok && bl->norm->Bl.comp)
1.54 kristaps 571: return;
572:
1.85 kristaps 573: /* Do not vspace directly after Ss/Sh. */
1.1 kristaps 574:
1.289 schwarze 575: nn = n;
576: while (nn->prev == NULL) {
577: do {
578: nn = nn->parent;
579: if (nn->type == MDOC_ROOT)
580: return;
581: } while (nn->type != MDOC_BLOCK);
582: if (nn->tok == MDOC_Sh || nn->tok == MDOC_Ss)
1.54 kristaps 583: return;
1.289 schwarze 584: if (nn->tok == MDOC_It &&
585: nn->parent->parent->norm->Bl.type != LIST_item)
586: break;
1.1 kristaps 587: }
588:
1.85 kristaps 589: /* A `-column' does not assert vspace within the list. */
1.54 kristaps 590:
1.202 kristaps 591: if (MDOC_Bl == bl->tok && LIST_column == bl->norm->Bl.type)
1.86 kristaps 592: if (n->prev && MDOC_It == n->prev->tok)
1.54 kristaps 593: return;
594:
1.85 kristaps 595: /* A `-diag' without body does not vspace. */
596:
1.202 kristaps 597: if (MDOC_Bl == bl->tok && LIST_diag == bl->norm->Bl.type)
1.86 kristaps 598: if (n->prev && MDOC_It == n->prev->tok) {
599: assert(n->prev->body);
600: if (NULL == n->prev->body->child)
1.55 kristaps 601: return;
602: }
603:
1.54 kristaps 604: term_vspace(p);
1.260 schwarze 605: }
606:
607:
608: static int
609: termp_ll_pre(DECL_ARGS)
610: {
611:
1.261 schwarze 612: term_setwidth(p, n->nchild ? n->child->string : NULL);
1.260 schwarze 613: return(0);
1.1 kristaps 614: }
615:
616: static int
617: termp_it_pre(DECL_ARGS)
618: {
1.86 kristaps 619: const struct mdoc_node *bl, *nn;
1.265 schwarze 620: char buf[24];
1.264 schwarze 621: int i;
622: size_t width, offset, ncols, dcol;
1.126 kristaps 623: enum mdoc_list type;
1.1 kristaps 624:
1.86 kristaps 625: if (MDOC_BLOCK == n->type) {
1.151 kristaps 626: print_bvspace(p, n->parent->parent, n);
1.54 kristaps 627: return(1);
628: }
1.1 kristaps 629:
1.86 kristaps 630: bl = n->parent->parent->parent;
1.202 kristaps 631: type = bl->norm->Bl.type;
1.1 kristaps 632:
1.264 schwarze 633: /*
1.108 kristaps 634: * First calculate width and offset. This is pretty easy unless
635: * we're a -column list, in which case all prior columns must
636: * be accounted for.
637: */
638:
639: width = offset = 0;
640:
1.202 kristaps 641: if (bl->norm->Bl.offs)
1.288 schwarze 642: offset = a2width(p, bl->norm->Bl.offs);
1.152 kristaps 643:
1.1 kristaps 644: switch (type) {
1.264 schwarze 645: case LIST_column:
1.134 kristaps 646: if (MDOC_HEAD == n->type)
1.1 kristaps 647: break;
1.155 kristaps 648:
1.105 kristaps 649: /*
1.108 kristaps 650: * Imitate groff's column handling:
651: * - For each earlier column, add its width.
652: * - For less than 5 columns, add four more blanks per
653: * column.
654: * - For exactly 5 columns, add three more blank per
655: * column.
656: * - For more than 5 columns, add only one column.
1.105 kristaps 657: */
1.202 kristaps 658: ncols = bl->norm->Bl.ncols;
1.264 schwarze 659: dcol = ncols < 5 ? term_len(p, 4) :
660: ncols == 5 ? term_len(p, 3) : term_len(p, 1);
1.108 kristaps 661:
1.134 kristaps 662: /*
663: * Calculate the offset by applying all prior MDOC_BODY,
664: * so we stop at the MDOC_HEAD (NULL == nn->prev).
665: */
666:
1.264 schwarze 667: for (i = 0, nn = n->prev;
668: nn->prev && i < (int)ncols;
669: nn = nn->prev, i++)
670: offset += dcol + a2width(p,
671: bl->norm->Bl.cols[i]);
1.105 kristaps 672:
673: /*
1.108 kristaps 674: * When exceeding the declared number of columns, leave
675: * the remaining widths at 0. This will later be
676: * adjusted to the default width of 10, or, for the last
677: * column, stretched to the right margin.
1.58 kristaps 678: */
1.108 kristaps 679: if (i >= (int)ncols)
680: break;
1.58 kristaps 681:
1.105 kristaps 682: /*
1.108 kristaps 683: * Use the declared column widths, extended as explained
684: * in the preceding paragraph.
1.105 kristaps 685: */
1.202 kristaps 686: width = a2width(p, bl->norm->Bl.cols[i]) + dcol;
1.1 kristaps 687: break;
688: default:
1.202 kristaps 689: if (NULL == bl->norm->Bl.width)
1.108 kristaps 690: break;
691:
1.264 schwarze 692: /*
1.108 kristaps 693: * Note: buffer the width by 2, which is groff's magic
694: * number for buffering single arguments. See the above
695: * handling for column for how this changes.
696: */
1.202 kristaps 697: assert(bl->norm->Bl.width);
698: width = a2width(p, bl->norm->Bl.width) + term_len(p, 2);
1.1 kristaps 699: break;
700: }
701:
1.264 schwarze 702: /*
1.1 kristaps 703: * List-type can override the width in the case of fixed-head
704: * values (bullet, dash/hyphen, enum). Tags need a non-zero
1.67 kristaps 705: * offset.
1.1 kristaps 706: */
707:
708: switch (type) {
1.264 schwarze 709: case LIST_bullet:
1.1 kristaps 710: /* FALLTHROUGH */
1.264 schwarze 711: case LIST_dash:
1.1 kristaps 712: /* FALLTHROUGH */
1.264 schwarze 713: case LIST_hyphen:
1.242 schwarze 714: /* FALLTHROUGH */
1.264 schwarze 715: case LIST_enum:
1.242 schwarze 716: if (width < term_len(p, 2))
717: width = term_len(p, 2);
1.17 kristaps 718: break;
1.264 schwarze 719: case LIST_hang:
1.39 kristaps 720: if (0 == width)
1.157 kristaps 721: width = term_len(p, 8);
1.39 kristaps 722: break;
1.264 schwarze 723: case LIST_column:
1.49 kristaps 724: /* FALLTHROUGH */
1.264 schwarze 725: case LIST_tag:
1.1 kristaps 726: if (0 == width)
1.157 kristaps 727: width = term_len(p, 10);
1.1 kristaps 728: break;
729: default:
730: break;
731: }
732:
1.264 schwarze 733: /*
1.17 kristaps 734: * Whitespace control. Inset bodies need an initial space,
735: * while diagonal bodies need two.
1.1 kristaps 736: */
737:
1.51 kristaps 738: p->flags |= TERMP_NOSPACE;
739:
1.1 kristaps 740: switch (type) {
1.264 schwarze 741: case LIST_diag:
1.86 kristaps 742: if (MDOC_BODY == n->type)
1.62 kristaps 743: term_word(p, "\\ \\ ");
1.51 kristaps 744: break;
1.264 schwarze 745: case LIST_inset:
1.270 schwarze 746: if (MDOC_BODY == n->type && n->parent->head->nchild)
1.51 kristaps 747: term_word(p, "\\ ");
1.1 kristaps 748: break;
749: default:
750: break;
751: }
752:
1.51 kristaps 753: p->flags |= TERMP_NOSPACE;
754:
1.1 kristaps 755: switch (type) {
1.264 schwarze 756: case LIST_diag:
1.86 kristaps 757: if (MDOC_HEAD == n->type)
1.102 kristaps 758: term_fontpush(p, TERMFONT_BOLD);
1.1 kristaps 759: break;
760: default:
761: break;
762: }
763:
764: /*
1.109 kristaps 765: * Pad and break control. This is the tricky part. These flags
766: * are documented in term_flushln() in term.c. Note that we're
767: * going to unset all of these flags in termp_it_post() when we
768: * exit.
1.1 kristaps 769: */
770:
771: switch (type) {
1.264 schwarze 772: case LIST_enum:
1.242 schwarze 773: /*
774: * Weird special case.
775: * Very narrow enum lists actually hang.
776: */
777: if (width == term_len(p, 2))
778: p->flags |= TERMP_HANG;
779: /* FALLTHROUGH */
1.264 schwarze 780: case LIST_bullet:
1.1 kristaps 781: /* FALLTHROUGH */
1.264 schwarze 782: case LIST_dash:
1.1 kristaps 783: /* FALLTHROUGH */
1.264 schwarze 784: case LIST_hyphen:
1.250 schwarze 785: if (MDOC_HEAD != n->type)
786: break;
787: p->flags |= TERMP_NOBREAK;
788: p->trailspace = 1;
1.39 kristaps 789: break;
1.264 schwarze 790: case LIST_hang:
1.250 schwarze 791: if (MDOC_HEAD != n->type)
1.59 kristaps 792: break;
793:
794: /*
795: * This is ugly. If `-hang' is specified and the body
796: * is a `Bl' or `Bd', then we want basically to nullify
797: * the "overstep" effect in term_flushln() and treat
798: * this as a `-ohang' list instead.
799: */
1.279 schwarze 800: if (NULL != n->next &&
1.278 schwarze 801: NULL != n->next->child &&
802: (MDOC_Bl == n->next->child->tok ||
803: MDOC_Bd == n->next->child->tok))
1.250 schwarze 804: break;
805:
1.263 schwarze 806: p->flags |= TERMP_NOBREAK | TERMP_BRIND | TERMP_HANG;
1.250 schwarze 807: p->trailspace = 1;
1.39 kristaps 808: break;
1.264 schwarze 809: case LIST_tag:
1.86 kristaps 810: if (MDOC_HEAD != n->type)
1.39 kristaps 811: break;
1.250 schwarze 812:
1.263 schwarze 813: p->flags |= TERMP_NOBREAK | TERMP_BRIND;
1.250 schwarze 814: p->trailspace = 2;
815:
1.86 kristaps 816: if (NULL == n->next || NULL == n->next->child)
1.39 kristaps 817: p->flags |= TERMP_DANGLE;
1.1 kristaps 818: break;
1.264 schwarze 819: case LIST_column:
1.134 kristaps 820: if (MDOC_HEAD == n->type)
821: break;
822:
1.250 schwarze 823: if (NULL == n->next) {
1.134 kristaps 824: p->flags &= ~TERMP_NOBREAK;
1.250 schwarze 825: p->trailspace = 0;
826: } else {
1.134 kristaps 827: p->flags |= TERMP_NOBREAK;
1.250 schwarze 828: p->trailspace = 1;
829: }
1.134 kristaps 830:
1.1 kristaps 831: break;
1.264 schwarze 832: case LIST_diag:
1.250 schwarze 833: if (MDOC_HEAD != n->type)
834: break;
1.263 schwarze 835: p->flags |= TERMP_NOBREAK | TERMP_BRIND;
1.250 schwarze 836: p->trailspace = 1;
1.1 kristaps 837: break;
838: default:
839: break;
840: }
841:
1.264 schwarze 842: /*
1.1 kristaps 843: * Margin control. Set-head-width lists have their right
844: * margins shortened. The body for these lists has the offset
845: * necessarily lengthened. Everybody gets the offset.
846: */
847:
848: p->offset += offset;
849:
850: switch (type) {
1.264 schwarze 851: case LIST_hang:
1.59 kristaps 852: /*
853: * Same stipulation as above, regarding `-hang'. We
854: * don't want to recalculate rmargin and offsets when
855: * using `Bd' or `Bl' within `-hang' overstep lists.
856: */
1.279 schwarze 857: if (MDOC_HEAD == n->type &&
858: NULL != n->next &&
1.278 schwarze 859: NULL != n->next->child &&
860: (MDOC_Bl == n->next->child->tok ||
861: MDOC_Bd == n->next->child->tok))
1.59 kristaps 862: break;
863: /* FALLTHROUGH */
1.264 schwarze 864: case LIST_bullet:
1.1 kristaps 865: /* FALLTHROUGH */
1.264 schwarze 866: case LIST_dash:
1.1 kristaps 867: /* FALLTHROUGH */
1.264 schwarze 868: case LIST_enum:
1.1 kristaps 869: /* FALLTHROUGH */
1.264 schwarze 870: case LIST_hyphen:
1.1 kristaps 871: /* FALLTHROUGH */
1.264 schwarze 872: case LIST_tag:
1.49 kristaps 873: assert(width);
1.86 kristaps 874: if (MDOC_HEAD == n->type)
1.1 kristaps 875: p->rmargin = p->offset + width;
1.259 schwarze 876: else {
1.1 kristaps 877: p->offset += width;
1.259 schwarze 878: if (p->rmargin < p->offset)
879: p->rmargin = p->offset;
880: }
1.1 kristaps 881: break;
1.264 schwarze 882: case LIST_column:
1.49 kristaps 883: assert(width);
1.1 kristaps 884: p->rmargin = p->offset + width;
1.264 schwarze 885: /*
1.50 kristaps 886: * XXX - this behaviour is not documented: the
887: * right-most column is filled to the right margin.
888: */
1.134 kristaps 889: if (MDOC_HEAD == n->type)
890: break;
891: if (NULL == n->next && p->rmargin < p->maxrmargin)
1.50 kristaps 892: p->rmargin = p->maxrmargin;
1.1 kristaps 893: break;
894: default:
895: break;
896: }
897:
1.264 schwarze 898: /*
1.1 kristaps 899: * The dash, hyphen, bullet and enum lists all have a special
1.264 schwarze 900: * HEAD character (temporarily bold, in some cases).
1.1 kristaps 901: */
902:
1.86 kristaps 903: if (MDOC_HEAD == n->type)
1.1 kristaps 904: switch (type) {
1.264 schwarze 905: case LIST_bullet:
1.102 kristaps 906: term_fontpush(p, TERMFONT_BOLD);
1.1 kristaps 907: term_word(p, "\\[bu]");
1.102 kristaps 908: term_fontpop(p);
1.1 kristaps 909: break;
1.264 schwarze 910: case LIST_dash:
1.1 kristaps 911: /* FALLTHROUGH */
1.264 schwarze 912: case LIST_hyphen:
1.102 kristaps 913: term_fontpush(p, TERMFONT_BOLD);
1.27 kristaps 914: term_word(p, "\\(hy");
1.102 kristaps 915: term_fontpop(p);
1.1 kristaps 916: break;
1.264 schwarze 917: case LIST_enum:
1.1 kristaps 918: (pair->ppair->ppair->count)++;
1.265 schwarze 919: (void)snprintf(buf, sizeof(buf), "%d.",
1.264 schwarze 920: pair->ppair->ppair->count);
1.1 kristaps 921: term_word(p, buf);
922: break;
923: default:
924: break;
925: }
926:
1.264 schwarze 927: /*
1.1 kristaps 928: * If we're not going to process our children, indicate so here.
929: */
930:
931: switch (type) {
1.264 schwarze 932: case LIST_bullet:
1.1 kristaps 933: /* FALLTHROUGH */
1.264 schwarze 934: case LIST_item:
1.1 kristaps 935: /* FALLTHROUGH */
1.264 schwarze 936: case LIST_dash:
1.1 kristaps 937: /* FALLTHROUGH */
1.264 schwarze 938: case LIST_hyphen:
1.1 kristaps 939: /* FALLTHROUGH */
1.264 schwarze 940: case LIST_enum:
1.86 kristaps 941: if (MDOC_HEAD == n->type)
1.1 kristaps 942: return(0);
943: break;
1.264 schwarze 944: case LIST_column:
1.134 kristaps 945: if (MDOC_HEAD == n->type)
1.1 kristaps 946: return(0);
947: break;
948: default:
949: break;
950: }
951:
952: return(1);
953: }
954:
955: static void
956: termp_it_post(DECL_ARGS)
957: {
1.126 kristaps 958: enum mdoc_list type;
1.1 kristaps 959:
1.109 kristaps 960: if (MDOC_BLOCK == n->type)
1.1 kristaps 961: return;
962:
1.202 kristaps 963: type = n->parent->parent->parent->norm->Bl.type;
1.1 kristaps 964:
965: switch (type) {
1.264 schwarze 966: case LIST_item:
1.1 kristaps 967: /* FALLTHROUGH */
1.264 schwarze 968: case LIST_diag:
1.53 kristaps 969: /* FALLTHROUGH */
1.264 schwarze 970: case LIST_inset:
1.86 kristaps 971: if (MDOC_BODY == n->type)
1.130 schwarze 972: term_newln(p);
1.1 kristaps 973: break;
1.264 schwarze 974: case LIST_column:
1.134 kristaps 975: if (MDOC_BODY == n->type)
1.1 kristaps 976: term_flushln(p);
977: break;
978: default:
1.130 schwarze 979: term_newln(p);
1.1 kristaps 980: break;
981: }
982:
1.264 schwarze 983: /*
1.109 kristaps 984: * Now that our output is flushed, we can reset our tags. Since
985: * only `It' sets these flags, we're free to assume that nobody
986: * has munged them in the meanwhile.
987: */
988:
1.263 schwarze 989: p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND |
990: TERMP_DANGLE | TERMP_HANG);
1.250 schwarze 991: p->trailspace = 0;
1.1 kristaps 992: }
993:
994: static int
995: termp_nm_pre(DECL_ARGS)
996: {
997:
1.253 schwarze 998: if (MDOC_BLOCK == n->type) {
999: p->flags |= TERMP_PREKEEP;
1.164 schwarze 1000: return(1);
1.253 schwarze 1001: }
1.164 schwarze 1002:
1003: if (MDOC_BODY == n->type) {
1004: if (NULL == n->child)
1005: return(0);
1.234 schwarze 1006: p->flags |= TERMP_NOSPACE;
1.164 schwarze 1007: p->offset += term_len(p, 1) +
1.245 schwarze 1008: (NULL == n->prev->child ?
1009: term_strlen(p, meta->name) :
1.164 schwarze 1010: MDOC_TEXT == n->prev->child->type ?
1.245 schwarze 1011: term_strlen(p, n->prev->child->string) :
1.164 schwarze 1012: term_len(p, 5));
1.262 schwarze 1013: if (p->rmargin < p->offset)
1014: p->rmargin = p->offset;
1.164 schwarze 1015: return(1);
1016: }
1017:
1.245 schwarze 1018: if (NULL == n->child && NULL == meta->name)
1.164 schwarze 1019: return(0);
1.125 kristaps 1020:
1.177 schwarze 1021: if (MDOC_HEAD == n->type)
1022: synopsis_pre(p, n->parent);
1.102 kristaps 1023:
1.279 schwarze 1024: if (MDOC_HEAD == n->type &&
1.278 schwarze 1025: NULL != n->next && NULL != n->next->child) {
1.263 schwarze 1026: p->flags |= TERMP_NOSPACE | TERMP_NOBREAK | TERMP_BRIND;
1.250 schwarze 1027: p->trailspace = 1;
1.185 schwarze 1028: p->rmargin = p->offset + term_len(p, 1);
1029: if (NULL == n->child) {
1.245 schwarze 1030: p->rmargin += term_strlen(p, meta->name);
1.185 schwarze 1031: } else if (MDOC_TEXT == n->child->type) {
1032: p->rmargin += term_strlen(p, n->child->string);
1033: if (n->child->next)
1034: p->flags |= TERMP_HANG;
1035: } else {
1036: p->rmargin += term_len(p, 5);
1037: p->flags |= TERMP_HANG;
1038: }
1.164 schwarze 1039: }
1040:
1.102 kristaps 1041: term_fontpush(p, TERMFONT_BOLD);
1.86 kristaps 1042: if (NULL == n->child)
1.245 schwarze 1043: term_word(p, meta->name);
1.1 kristaps 1044: return(1);
1045: }
1046:
1.164 schwarze 1047: static void
1048: termp_nm_post(DECL_ARGS)
1049: {
1050:
1.253 schwarze 1051: if (MDOC_BLOCK == n->type) {
1052: p->flags &= ~(TERMP_KEEP | TERMP_PREKEEP);
1.279 schwarze 1053: } else if (MDOC_HEAD == n->type &&
1.278 schwarze 1054: NULL != n->next && NULL != n->next->child) {
1.164 schwarze 1055: term_flushln(p);
1.263 schwarze 1056: p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND | TERMP_HANG);
1.250 schwarze 1057: p->trailspace = 0;
1.234 schwarze 1058: } else if (MDOC_BODY == n->type && n->child)
1.164 schwarze 1059: term_flushln(p);
1060: }
1061:
1.1 kristaps 1062: static int
1063: termp_fl_pre(DECL_ARGS)
1064: {
1065:
1.102 kristaps 1066: term_fontpush(p, TERMFONT_BOLD);
1.1 kristaps 1067: term_word(p, "\\-");
1.103 kristaps 1068:
1.280 schwarze 1069: if ( ! (n->nchild == 0 &&
1070: (n->next == NULL ||
1071: n->next->type == MDOC_TEXT ||
1072: n->next->flags & MDOC_LINE)))
1.103 kristaps 1073: p->flags |= TERMP_NOSPACE;
1074:
1.1 kristaps 1075: return(1);
1076: }
1077:
1078: static int
1.184 kristaps 1079: termp__a_pre(DECL_ARGS)
1080: {
1081:
1082: if (n->prev && MDOC__A == n->prev->tok)
1083: if (NULL == n->next || MDOC__A != n->next->tok)
1084: term_word(p, "and");
1085:
1086: return(1);
1087: }
1088:
1089: static int
1.61 kristaps 1090: termp_an_pre(DECL_ARGS)
1091: {
1092:
1.282 schwarze 1093: if (n->norm->An.auth == AUTH_split) {
1094: p->flags &= ~TERMP_NOSPLIT;
1095: p->flags |= TERMP_SPLIT;
1096: return(0);
1097: }
1098: if (n->norm->An.auth == AUTH_nosplit) {
1099: p->flags &= ~TERMP_SPLIT;
1100: p->flags |= TERMP_NOSPLIT;
1101: return(0);
1102: }
1.61 kristaps 1103:
1.282 schwarze 1104: if (n->child == NULL)
1105: return(0);
1.264 schwarze 1106:
1.282 schwarze 1107: if (p->flags & TERMP_SPLIT)
1.61 kristaps 1108: term_newln(p);
1109:
1.282 schwarze 1110: if (n->sec == SEC_AUTHORS && ! (p->flags & TERMP_NOSPLIT))
1111: p->flags |= TERMP_SPLIT;
1.61 kristaps 1112:
1113: return(1);
1114: }
1115:
1116: static int
1.1 kristaps 1117: termp_ns_pre(DECL_ARGS)
1118: {
1119:
1.215 kristaps 1120: if ( ! (MDOC_LINE & n->flags))
1121: p->flags |= TERMP_NOSPACE;
1.1 kristaps 1122: return(1);
1123: }
1124:
1125: static int
1126: termp_rs_pre(DECL_ARGS)
1127: {
1128:
1.86 kristaps 1129: if (SEC_SEE_ALSO != n->sec)
1.80 kristaps 1130: return(1);
1.86 kristaps 1131: if (MDOC_BLOCK == n->type && n->prev)
1.1 kristaps 1132: term_vspace(p);
1133: return(1);
1134: }
1135:
1136: static int
1137: termp_rv_pre(DECL_ARGS)
1138: {
1.223 kristaps 1139: int nchild;
1.1 kristaps 1140:
1141: term_newln(p);
1142:
1.223 kristaps 1143: nchild = n->nchild;
1.273 schwarze 1144: if (nchild > 0) {
1145: term_word(p, "The");
1.224 kristaps 1146:
1.273 schwarze 1147: for (n = n->child; n; n = n->next) {
1148: term_fontpush(p, TERMFONT_BOLD);
1149: term_word(p, n->string);
1150: term_fontpop(p);
1.223 kristaps 1151:
1152: p->flags |= TERMP_NOSPACE;
1.273 schwarze 1153: term_word(p, "()");
1154:
1155: if (n->next == NULL)
1156: continue;
1157:
1158: if (nchild > 2) {
1159: p->flags |= TERMP_NOSPACE;
1160: term_word(p, ",");
1161: }
1162: if (n->next->next == NULL)
1163: term_word(p, "and");
1.223 kristaps 1164: }
1165:
1.273 schwarze 1166: if (nchild > 1)
1167: term_word(p, "functions return");
1168: else
1169: term_word(p, "function returns");
1170:
1171: term_word(p, "the value\\~0 if successful;");
1172: } else
1173: term_word(p, "Upon successful completion,"
1174: " the value\\~0 is returned;");
1.1 kristaps 1175:
1.273 schwarze 1176: term_word(p, "otherwise the value\\~\\-1 is returned"
1177: " and the global variable");
1.1 kristaps 1178:
1.102 kristaps 1179: term_fontpush(p, TERMFONT_UNDER);
1.1 kristaps 1180: term_word(p, "errno");
1.102 kristaps 1181: term_fontpop(p);
1.1 kristaps 1182:
1.264 schwarze 1183: term_word(p, "is set to indicate the error.");
1.131 kristaps 1184: p->flags |= TERMP_SENTENCE;
1.1 kristaps 1185:
1.68 kristaps 1186: return(0);
1.1 kristaps 1187: }
1188:
1189: static int
1190: termp_ex_pre(DECL_ARGS)
1191: {
1.224 kristaps 1192: int nchild;
1.1 kristaps 1193:
1.224 kristaps 1194: term_newln(p);
1.68 kristaps 1195: term_word(p, "The");
1.1 kristaps 1196:
1.224 kristaps 1197: nchild = n->nchild;
1198: for (n = n->child; n; n = n->next) {
1.102 kristaps 1199: term_fontpush(p, TERMFONT_BOLD);
1.224 kristaps 1200: term_word(p, n->string);
1.102 kristaps 1201: term_fontpop(p);
1.224 kristaps 1202:
1203: if (nchild > 2 && n->next) {
1204: p->flags |= TERMP_NOSPACE;
1.68 kristaps 1205: term_word(p, ",");
1.224 kristaps 1206: }
1207:
1208: if (n->next && NULL == n->next->next)
1209: term_word(p, "and");
1.68 kristaps 1210: }
1211:
1.224 kristaps 1212: if (nchild > 1)
1.273 schwarze 1213: term_word(p, "utilities exit\\~0");
1.68 kristaps 1214: else
1.273 schwarze 1215: term_word(p, "utility exits\\~0");
1.68 kristaps 1216:
1.273 schwarze 1217: term_word(p, "on success, and\\~>0 if an error occurs.");
1.224 kristaps 1218:
1.131 kristaps 1219: p->flags |= TERMP_SENTENCE;
1.68 kristaps 1220: return(0);
1.1 kristaps 1221: }
1222:
1223: static int
1224: termp_nd_pre(DECL_ARGS)
1225: {
1.25 kristaps 1226:
1.86 kristaps 1227: if (MDOC_BODY != n->type)
1.25 kristaps 1228: return(1);
1.30 kristaps 1229:
1.27 kristaps 1230: #if defined(__OpenBSD__) || defined(__linux__)
1.41 kristaps 1231: term_word(p, "\\(en");
1.23 kristaps 1232: #else
1233: term_word(p, "\\(em");
1234: #endif
1.1 kristaps 1235: return(1);
1.115 kristaps 1236: }
1237:
1238: static int
1239: termp_bl_pre(DECL_ARGS)
1240: {
1241:
1242: return(MDOC_HEAD != n->type);
1.1 kristaps 1243: }
1244:
1245: static void
1246: termp_bl_post(DECL_ARGS)
1247: {
1248:
1.86 kristaps 1249: if (MDOC_BLOCK == n->type)
1.1 kristaps 1250: term_newln(p);
1251: }
1252:
1253: static int
1254: termp_xr_pre(DECL_ARGS)
1255: {
1256:
1.225 kristaps 1257: if (NULL == (n = n->child))
1.113 kristaps 1258: return(0);
1259:
1.225 kristaps 1260: assert(MDOC_TEXT == n->type);
1261: term_word(p, n->string);
1.11 kristaps 1262:
1.264 schwarze 1263: if (NULL == (n = n->next))
1.1 kristaps 1264: return(0);
1.225 kristaps 1265:
1.1 kristaps 1266: p->flags |= TERMP_NOSPACE;
1267: term_word(p, "(");
1.222 kristaps 1268: p->flags |= TERMP_NOSPACE;
1.225 kristaps 1269:
1270: assert(MDOC_TEXT == n->type);
1271: term_word(p, n->string);
1272:
1.222 kristaps 1273: p->flags |= TERMP_NOSPACE;
1.1 kristaps 1274: term_word(p, ")");
1.86 kristaps 1275:
1.1 kristaps 1276: return(0);
1277: }
1278:
1.143 kristaps 1279: /*
1280: * This decides how to assert whitespace before any of the SYNOPSIS set
1281: * of macros (which, as in the case of Ft/Fo and Ft/Fn, may contain
1282: * macro combos).
1283: */
1284: static void
1285: synopsis_pre(struct termp *p, const struct mdoc_node *n)
1286: {
1.264 schwarze 1287: /*
1.143 kristaps 1288: * Obviously, if we're not in a SYNOPSIS or no prior macros
1289: * exist, do nothing.
1290: */
1.160 kristaps 1291: if (NULL == n->prev || ! (MDOC_SYNPRETTY & n->flags))
1.143 kristaps 1292: return;
1293:
1294: /*
1295: * If we're the second in a pair of like elements, emit our
1296: * newline and return. UNLESS we're `Fo', `Fn', `Fn', in which
1297: * case we soldier on.
1298: */
1.264 schwarze 1299: if (n->prev->tok == n->tok &&
1300: MDOC_Ft != n->tok &&
1301: MDOC_Fo != n->tok &&
1302: MDOC_Fn != n->tok) {
1.143 kristaps 1303: term_newln(p);
1304: return;
1305: }
1306:
1307: /*
1308: * If we're one of the SYNOPSIS set and non-like pair-wise after
1309: * another (or Fn/Fo, which we've let slip through) then assert
1310: * vertical space, else only newline and move on.
1311: */
1312: switch (n->prev->tok) {
1.264 schwarze 1313: case MDOC_Fd:
1.143 kristaps 1314: /* FALLTHROUGH */
1.264 schwarze 1315: case MDOC_Fn:
1.143 kristaps 1316: /* FALLTHROUGH */
1.264 schwarze 1317: case MDOC_Fo:
1.143 kristaps 1318: /* FALLTHROUGH */
1.264 schwarze 1319: case MDOC_In:
1.143 kristaps 1320: /* FALLTHROUGH */
1.264 schwarze 1321: case MDOC_Vt:
1.143 kristaps 1322: term_vspace(p);
1323: break;
1.264 schwarze 1324: case MDOC_Ft:
1.143 kristaps 1325: if (MDOC_Fn != n->tok && MDOC_Fo != n->tok) {
1326: term_vspace(p);
1327: break;
1328: }
1329: /* FALLTHROUGH */
1330: default:
1331: term_newln(p);
1332: break;
1333: }
1334: }
1335:
1.110 kristaps 1336: static int
1337: termp_vt_pre(DECL_ARGS)
1338: {
1339:
1.143 kristaps 1340: if (MDOC_ELEM == n->type) {
1341: synopsis_pre(p, n);
1.245 schwarze 1342: return(termp_under_pre(p, pair, meta, n));
1.143 kristaps 1343: } else if (MDOC_BLOCK == n->type) {
1344: synopsis_pre(p, n);
1345: return(1);
1346: } else if (MDOC_HEAD == n->type)
1.110 kristaps 1347: return(0);
1348:
1.245 schwarze 1349: return(termp_under_pre(p, pair, meta, n));
1.110 kristaps 1350: }
1351:
1.1 kristaps 1352: static int
1.69 kristaps 1353: termp_bold_pre(DECL_ARGS)
1.1 kristaps 1354: {
1355:
1.102 kristaps 1356: term_fontpush(p, TERMFONT_BOLD);
1.1 kristaps 1357: return(1);
1358: }
1359:
1.143 kristaps 1360: static int
1361: termp_fd_pre(DECL_ARGS)
1.1 kristaps 1362: {
1363:
1.143 kristaps 1364: synopsis_pre(p, n);
1.245 schwarze 1365: return(termp_bold_pre(p, pair, meta, n));
1.1 kristaps 1366: }
1367:
1.241 schwarze 1368: static void
1369: termp_fd_post(DECL_ARGS)
1370: {
1371:
1372: term_newln(p);
1373: }
1374:
1.1 kristaps 1375: static int
1376: termp_sh_pre(DECL_ARGS)
1377: {
1.85 kristaps 1378:
1379: /* No vspace between consecutive `Sh' calls. */
1380:
1.86 kristaps 1381: switch (n->type) {
1.264 schwarze 1382: case MDOC_BLOCK:
1.86 kristaps 1383: if (n->prev && MDOC_Sh == n->prev->tok)
1384: if (NULL == n->prev->body->child)
1.65 kristaps 1385: break;
1386: term_vspace(p);
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.268 schwarze 1840: termp_es_pre(DECL_ARGS)
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.188 kristaps 1857: term_word(p, "<");
1858: break;
1.264 schwarze 1859: case MDOC_Bro:
1.188 kristaps 1860: /* FALLTHROUGH */
1.264 schwarze 1861: case MDOC_Brq:
1.188 kristaps 1862: term_word(p, "{");
1863: break;
1.264 schwarze 1864: case MDOC_Oo:
1.188 kristaps 1865: /* FALLTHROUGH */
1.264 schwarze 1866: case MDOC_Op:
1.188 kristaps 1867: /* FALLTHROUGH */
1.264 schwarze 1868: case MDOC_Bo:
1.188 kristaps 1869: /* FALLTHROUGH */
1.264 schwarze 1870: case MDOC_Bq:
1.188 kristaps 1871: term_word(p, "[");
1872: break;
1.264 schwarze 1873: case MDOC_Do:
1.188 kristaps 1874: /* FALLTHROUGH */
1.264 schwarze 1875: case MDOC_Dq:
1.249 schwarze 1876: term_word(p, "\\(lq");
1.188 kristaps 1877: break;
1.268 schwarze 1878: case MDOC_En:
1879: if (NULL == n->norm->Es ||
1880: NULL == n->norm->Es->child)
1881: return(1);
1882: term_word(p, n->norm->Es->child->string);
1883: break;
1.264 schwarze 1884: case MDOC_Eo:
1.237 schwarze 1885: break;
1.264 schwarze 1886: case MDOC_Po:
1.188 kristaps 1887: /* FALLTHROUGH */
1.264 schwarze 1888: case MDOC_Pq:
1.188 kristaps 1889: term_word(p, "(");
1890: break;
1.264 schwarze 1891: case MDOC__T:
1.205 kristaps 1892: /* FALLTHROUGH */
1.264 schwarze 1893: case MDOC_Qo:
1.188 kristaps 1894: /* FALLTHROUGH */
1.264 schwarze 1895: case MDOC_Qq:
1.188 kristaps 1896: term_word(p, "\"");
1897: break;
1.264 schwarze 1898: case MDOC_Ql:
1.188 kristaps 1899: /* FALLTHROUGH */
1.264 schwarze 1900: case MDOC_So:
1.188 kristaps 1901: /* FALLTHROUGH */
1.264 schwarze 1902: case MDOC_Sq:
1.249 schwarze 1903: term_word(p, "\\(oq");
1.188 kristaps 1904: break;
1905: default:
1906: abort();
1907: /* NOTREACHED */
1908: }
1.1 kristaps 1909:
1910: p->flags |= TERMP_NOSPACE;
1911: return(1);
1912: }
1913:
1914: static void
1.188 kristaps 1915: termp_quote_post(DECL_ARGS)
1.1 kristaps 1916: {
1917:
1.203 kristaps 1918: if (MDOC_BODY != n->type && MDOC_ELEM != n->type)
1.1 kristaps 1919: return;
1920:
1.268 schwarze 1921: if (MDOC_En != n->tok)
1922: p->flags |= TERMP_NOSPACE;
1.1 kristaps 1923:
1.188 kristaps 1924: switch (n->tok) {
1.264 schwarze 1925: case MDOC_Ao:
1.188 kristaps 1926: /* FALLTHROUGH */
1.264 schwarze 1927: case MDOC_Aq:
1.188 kristaps 1928: term_word(p, ">");
1929: break;
1.264 schwarze 1930: case MDOC_Bro:
1.188 kristaps 1931: /* FALLTHROUGH */
1.264 schwarze 1932: case MDOC_Brq:
1.188 kristaps 1933: term_word(p, "}");
1934: break;
1.264 schwarze 1935: case MDOC_Oo:
1.188 kristaps 1936: /* FALLTHROUGH */
1.264 schwarze 1937: case MDOC_Op:
1.188 kristaps 1938: /* FALLTHROUGH */
1.264 schwarze 1939: case MDOC_Bo:
1.188 kristaps 1940: /* FALLTHROUGH */
1.264 schwarze 1941: case MDOC_Bq:
1.188 kristaps 1942: term_word(p, "]");
1943: break;
1.264 schwarze 1944: case MDOC_Do:
1.188 kristaps 1945: /* FALLTHROUGH */
1.264 schwarze 1946: case MDOC_Dq:
1.249 schwarze 1947: term_word(p, "\\(rq");
1.268 schwarze 1948: break;
1949: case MDOC_En:
1950: if (NULL != n->norm->Es &&
1951: NULL != n->norm->Es->child &&
1952: NULL != n->norm->Es->child->next) {
1953: p->flags |= TERMP_NOSPACE;
1954: term_word(p, n->norm->Es->child->next->string);
1955: }
1.237 schwarze 1956: break;
1.264 schwarze 1957: case MDOC_Eo:
1.188 kristaps 1958: break;
1.264 schwarze 1959: case MDOC_Po:
1.188 kristaps 1960: /* FALLTHROUGH */
1.264 schwarze 1961: case MDOC_Pq:
1.188 kristaps 1962: term_word(p, ")");
1963: break;
1.264 schwarze 1964: case MDOC__T:
1.205 kristaps 1965: /* FALLTHROUGH */
1.264 schwarze 1966: case MDOC_Qo:
1.188 kristaps 1967: /* FALLTHROUGH */
1.264 schwarze 1968: case MDOC_Qq:
1.188 kristaps 1969: term_word(p, "\"");
1970: break;
1.264 schwarze 1971: case MDOC_Ql:
1.188 kristaps 1972: /* FALLTHROUGH */
1.264 schwarze 1973: case MDOC_So:
1.188 kristaps 1974: /* FALLTHROUGH */
1.264 schwarze 1975: case MDOC_Sq:
1.249 schwarze 1976: term_word(p, "\\(cq");
1.188 kristaps 1977: break;
1978: default:
1979: abort();
1980: /* NOTREACHED */
1981: }
1.1 kristaps 1982: }
1983:
1984: static int
1985: termp_fo_pre(DECL_ARGS)
1986: {
1.257 schwarze 1987: size_t rmargin = 0;
1.256 schwarze 1988: int pretty;
1989:
1990: pretty = MDOC_SYNPRETTY & n->flags;
1.1 kristaps 1991:
1.141 kristaps 1992: if (MDOC_BLOCK == n->type) {
1.143 kristaps 1993: synopsis_pre(p, n);
1.141 kristaps 1994: return(1);
1995: } else if (MDOC_BODY == n->type) {
1.256 schwarze 1996: if (pretty) {
1997: rmargin = p->rmargin;
1.257 schwarze 1998: p->rmargin = p->offset + term_len(p, 4);
1.263 schwarze 1999: p->flags |= TERMP_NOBREAK | TERMP_BRIND |
2000: TERMP_HANG;
1.256 schwarze 2001: }
1.33 kristaps 2002: p->flags |= TERMP_NOSPACE;
1.1 kristaps 2003: term_word(p, "(");
1.222 kristaps 2004: p->flags |= TERMP_NOSPACE;
1.256 schwarze 2005: if (pretty) {
2006: term_flushln(p);
1.263 schwarze 2007: p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND |
2008: TERMP_HANG);
1.256 schwarze 2009: p->offset = p->rmargin;
2010: p->rmargin = rmargin;
2011: }
1.1 kristaps 2012: return(1);
1.256 schwarze 2013: }
1.141 kristaps 2014:
1.169 kristaps 2015: if (NULL == n->child)
2016: return(0);
2017:
1.141 kristaps 2018: /* XXX: we drop non-initial arguments as per groff. */
1.1 kristaps 2019:
1.141 kristaps 2020: assert(n->child->string);
1.102 kristaps 2021: term_fontpush(p, TERMFONT_BOLD);
1.141 kristaps 2022: term_word(p, n->child->string);
1.1 kristaps 2023: return(0);
2024: }
2025:
2026: static void
2027: termp_fo_post(DECL_ARGS)
2028: {
2029:
1.264 schwarze 2030: if (MDOC_BODY != n->type)
1.143 kristaps 2031: return;
2032:
1.222 kristaps 2033: p->flags |= TERMP_NOSPACE;
1.143 kristaps 2034: term_word(p, ")");
2035:
1.222 kristaps 2036: if (MDOC_SYNPRETTY & n->flags) {
2037: p->flags |= TERMP_NOSPACE;
1.143 kristaps 2038: term_word(p, ";");
1.256 schwarze 2039: term_flushln(p);
1.222 kristaps 2040: }
1.1 kristaps 2041: }
2042:
2043: static int
2044: termp_bf_pre(DECL_ARGS)
2045: {
2046:
1.86 kristaps 2047: if (MDOC_HEAD == n->type)
1.1 kristaps 2048: return(0);
1.244 schwarze 2049: else if (MDOC_BODY != n->type)
1.1 kristaps 2050: return(1);
2051:
1.264 schwarze 2052: if (FONT_Em == n->norm->Bf.font)
1.102 kristaps 2053: term_fontpush(p, TERMFONT_UNDER);
1.264 schwarze 2054: else if (FONT_Sy == n->norm->Bf.font)
1.102 kristaps 2055: term_fontpush(p, TERMFONT_BOLD);
1.264 schwarze 2056: else
1.102 kristaps 2057: term_fontpush(p, TERMFONT_NONE);
1.1 kristaps 2058:
2059: return(1);
2060: }
2061:
2062: static int
2063: termp_sm_pre(DECL_ARGS)
2064: {
2065:
1.269 schwarze 2066: if (NULL == n->child)
2067: p->flags ^= TERMP_NONOSPACE;
2068: else if (0 == strcmp("on", n->child->string))
1.1 kristaps 2069: p->flags &= ~TERMP_NONOSPACE;
1.269 schwarze 2070: else
1.1 kristaps 2071: p->flags |= TERMP_NONOSPACE;
1.269 schwarze 2072:
2073: if (p->col && ! (TERMP_NONOSPACE & p->flags))
2074: p->flags &= ~TERMP_NOSPACE;
1.1 kristaps 2075:
2076: return(0);
2077: }
2078:
2079: static int
2080: termp_ap_pre(DECL_ARGS)
2081: {
2082:
2083: p->flags |= TERMP_NOSPACE;
1.188 kristaps 2084: term_word(p, "'");
1.1 kristaps 2085: p->flags |= TERMP_NOSPACE;
2086: return(1);
2087: }
2088:
2089: static void
2090: termp____post(DECL_ARGS)
2091: {
1.184 kristaps 2092:
2093: /*
2094: * Handle lists of authors. In general, print each followed by
2095: * a comma. Don't print the comma if there are only two
2096: * authors.
2097: */
2098: if (MDOC__A == n->tok && n->next && MDOC__A == n->next->tok)
2099: if (NULL == n->next->next || MDOC__A != n->next->next->tok)
2100: if (NULL == n->prev || MDOC__A != n->prev->tok)
2101: return;
1.1 kristaps 2102:
1.95 kristaps 2103: /* TODO: %U. */
2104:
1.187 kristaps 2105: if (NULL == n->parent || MDOC_Rs != n->parent->tok)
2106: return;
2107:
1.222 kristaps 2108: p->flags |= TERMP_NOSPACE;
1.186 kristaps 2109: if (NULL == n->next) {
2110: term_word(p, ".");
2111: p->flags |= TERMP_SENTENCE;
2112: } else
2113: term_word(p, ",");
1.1 kristaps 2114: }
2115:
2116: static int
1.102 kristaps 2117: termp_li_pre(DECL_ARGS)
2118: {
2119:
2120: term_fontpush(p, TERMFONT_NONE);
2121: return(1);
2122: }
2123:
2124: static int
1.1 kristaps 2125: termp_lk_pre(DECL_ARGS)
2126: {
1.240 schwarze 2127: const struct mdoc_node *link, *descr;
1.1 kristaps 2128:
1.240 schwarze 2129: if (NULL == (link = n->child))
2130: return(0);
1.181 kristaps 2131:
1.240 schwarze 2132: if (NULL != (descr = link->next)) {
2133: term_fontpush(p, TERMFONT_UNDER);
2134: while (NULL != descr) {
2135: term_word(p, descr->string);
2136: descr = descr->next;
2137: }
2138: p->flags |= TERMP_NOSPACE;
2139: term_word(p, ":");
2140: term_fontpop(p);
2141: }
1.1 kristaps 2142:
1.102 kristaps 2143: term_fontpush(p, TERMFONT_BOLD);
1.240 schwarze 2144: term_word(p, link->string);
1.102 kristaps 2145: term_fontpop(p);
1.12 kristaps 2146:
1.1 kristaps 2147: return(0);
2148: }
2149:
1.159 schwarze 2150: static int
2151: termp_bk_pre(DECL_ARGS)
2152: {
2153:
1.161 schwarze 2154: switch (n->type) {
1.264 schwarze 2155: case MDOC_BLOCK:
1.166 kristaps 2156: break;
1.264 schwarze 2157: case MDOC_HEAD:
1.161 schwarze 2158: return(0);
1.264 schwarze 2159: case MDOC_BODY:
1.200 schwarze 2160: if (n->parent->args || 0 == n->prev->nchild)
2161: p->flags |= TERMP_PREKEEP;
1.166 kristaps 2162: break;
1.161 schwarze 2163: default:
2164: abort();
1.166 kristaps 2165: /* NOTREACHED */
1.161 schwarze 2166: }
1.166 kristaps 2167:
2168: return(1);
1.159 schwarze 2169: }
2170:
2171: static void
2172: termp_bk_post(DECL_ARGS)
2173: {
2174:
1.253 schwarze 2175: if (MDOC_BODY == n->type)
1.161 schwarze 2176: p->flags &= ~(TERMP_KEEP | TERMP_PREKEEP);
1.203 kristaps 2177: }
2178:
2179: static void
2180: termp__t_post(DECL_ARGS)
2181: {
2182:
2183: /*
2184: * If we're in an `Rs' and there's a journal present, then quote
2185: * us instead of underlining us (for disambiguation).
2186: */
1.217 schwarze 2187: if (n->parent && MDOC_Rs == n->parent->tok &&
1.264 schwarze 2188: n->parent->norm->Rs.quote_T)
1.245 schwarze 2189: termp_quote_post(p, pair, meta, n);
1.203 kristaps 2190:
1.245 schwarze 2191: termp____post(p, pair, meta, n);
1.203 kristaps 2192: }
2193:
2194: static int
2195: termp__t_pre(DECL_ARGS)
2196: {
2197:
2198: /*
2199: * If we're in an `Rs' and there's a journal present, then quote
2200: * us instead of underlining us (for disambiguation).
2201: */
2202: if (n->parent && MDOC_Rs == n->parent->tok &&
1.264 schwarze 2203: n->parent->norm->Rs.quote_T)
1.245 schwarze 2204: return(termp_quote_pre(p, pair, meta, n));
1.203 kristaps 2205:
2206: term_fontpush(p, TERMFONT_UNDER);
2207: return(1);
1.159 schwarze 2208: }
1.1 kristaps 2209:
2210: static int
1.69 kristaps 2211: termp_under_pre(DECL_ARGS)
1.1 kristaps 2212: {
2213:
1.102 kristaps 2214: term_fontpush(p, TERMFONT_UNDER);
1.84 kristaps 2215: return(1);
2216: }
CVSweb