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