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