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