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