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