Annotation of mandoc/man_term.c, Revision 1.200
1.200 ! schwarze 1: /* $Id: man_term.c,v 1.199 2017/05/07 21:44:49 schwarze Exp $ */
1.1 kristaps 2: /*
1.128 schwarze 3: * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
1.189 schwarze 4: * Copyright (c) 2010-2015, 2017 Ingo Schwarze <schwarze@openbsd.org>
1.1 kristaps 5: *
6: * Permission to use, copy, modify, and distribute this software for any
1.8 kristaps 7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 9: *
1.171 schwarze 10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.8 kristaps 11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.171 schwarze 12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.8 kristaps 13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 kristaps 17: */
1.55 kristaps 18: #include "config.h"
19:
1.28 kristaps 20: #include <sys/types.h>
21:
1.1 kristaps 22: #include <assert.h>
1.18 kristaps 23: #include <ctype.h>
1.164 schwarze 24: #include <limits.h>
1.1 kristaps 25: #include <stdio.h>
26: #include <stdlib.h>
27: #include <string.h>
28:
1.171 schwarze 29: #include "mandoc_aux.h"
1.71 kristaps 30: #include "mandoc.h"
1.171 schwarze 31: #include "roff.h"
32: #include "man.h"
1.40 kristaps 33: #include "out.h"
1.1 kristaps 34: #include "term.h"
1.36 kristaps 35: #include "main.h"
1.1 kristaps 36:
1.116 schwarze 37: #define MAXMARGINS 64 /* maximum number of indented scopes */
1.18 kristaps 38:
1.24 kristaps 39: struct mtermp {
40: int fl;
1.19 kristaps 41: #define MANT_LITERAL (1 << 0)
1.163 schwarze 42: int lmargin[MAXMARGINS]; /* margins (incl. vis. page) */
1.116 schwarze 43: int lmargincur; /* index of current margin */
44: int lmarginsz; /* actual number of nested margins */
45: size_t offset; /* default offset to visible page */
1.134 schwarze 46: int pardist; /* vert. space before par., unit: [v] */
1.24 kristaps 47: };
1.19 kristaps 48:
1.146 schwarze 49: #define DECL_ARGS struct termp *p, \
1.24 kristaps 50: struct mtermp *mt, \
1.172 schwarze 51: struct roff_node *n, \
1.173 schwarze 52: const struct roff_meta *meta
1.1 kristaps 53:
54: struct termact {
55: int (*pre)(DECL_ARGS);
56: void (*post)(DECL_ARGS);
1.56 kristaps 57: int flags;
58: #define MAN_NOTEXT (1 << 0) /* Never has text children. */
1.1 kristaps 59: };
60:
1.52 kristaps 61: static void print_man_nodelist(DECL_ARGS);
1.45 kristaps 62: static void print_man_node(DECL_ARGS);
1.173 schwarze 63: static void print_man_head(struct termp *,
64: const struct roff_meta *);
65: static void print_man_foot(struct termp *,
66: const struct roff_meta *);
1.146 schwarze 67: static void print_bvspace(struct termp *,
1.172 schwarze 68: const struct roff_node *, int);
1.39 kristaps 69:
1.1 kristaps 70: static int pre_B(DECL_ARGS);
1.199 schwarze 71: static int pre_DT(DECL_ARGS);
1.19 kristaps 72: static int pre_HP(DECL_ARGS);
1.1 kristaps 73: static int pre_I(DECL_ARGS);
1.4 kristaps 74: static int pre_IP(DECL_ARGS);
1.127 kristaps 75: static int pre_OP(DECL_ARGS);
1.134 schwarze 76: static int pre_PD(DECL_ARGS);
1.1 kristaps 77: static int pre_PP(DECL_ARGS);
1.26 kristaps 78: static int pre_RS(DECL_ARGS);
1.1 kristaps 79: static int pre_SH(DECL_ARGS);
80: static int pre_SS(DECL_ARGS);
81: static int pre_TP(DECL_ARGS);
1.137 schwarze 82: static int pre_UR(DECL_ARGS);
1.127 kristaps 83: static int pre_alternate(DECL_ARGS);
1.29 kristaps 84: static int pre_ign(DECL_ARGS);
1.83 kristaps 85: static int pre_in(DECL_ARGS);
1.84 kristaps 86: static int pre_literal(DECL_ARGS);
1.1 kristaps 87:
1.22 kristaps 88: static void post_IP(DECL_ARGS);
1.20 kristaps 89: static void post_HP(DECL_ARGS);
1.26 kristaps 90: static void post_RS(DECL_ARGS);
1.1 kristaps 91: static void post_SH(DECL_ARGS);
92: static void post_SS(DECL_ARGS);
1.21 kristaps 93: static void post_TP(DECL_ARGS);
1.137 schwarze 94: static void post_UR(DECL_ARGS);
1.1 kristaps 95:
1.192 schwarze 96: static const struct termact __termacts[MAN_MAX - MAN_TH] = {
1.56 kristaps 97: { NULL, NULL, 0 }, /* TH */
98: { pre_SH, post_SH, 0 }, /* SH */
99: { pre_SS, post_SS, 0 }, /* SS */
100: { pre_TP, post_TP, 0 }, /* TP */
101: { pre_PP, NULL, 0 }, /* LP */
102: { pre_PP, NULL, 0 }, /* PP */
103: { pre_PP, NULL, 0 }, /* P */
104: { pre_IP, post_IP, 0 }, /* IP */
1.146 schwarze 105: { pre_HP, post_HP, 0 }, /* HP */
1.56 kristaps 106: { NULL, NULL, 0 }, /* SM */
107: { pre_B, NULL, 0 }, /* SB */
1.88 kristaps 108: { pre_alternate, NULL, 0 }, /* BI */
109: { pre_alternate, NULL, 0 }, /* IB */
110: { pre_alternate, NULL, 0 }, /* BR */
111: { pre_alternate, NULL, 0 }, /* RB */
1.56 kristaps 112: { NULL, NULL, 0 }, /* R */
113: { pre_B, NULL, 0 }, /* B */
114: { pre_I, NULL, 0 }, /* I */
1.88 kristaps 115: { pre_alternate, NULL, 0 }, /* IR */
116: { pre_alternate, NULL, 0 }, /* RI */
1.84 kristaps 117: { pre_literal, NULL, 0 }, /* nf */
118: { pre_literal, NULL, 0 }, /* fi */
1.56 kristaps 119: { NULL, NULL, 0 }, /* RE */
120: { pre_RS, post_RS, 0 }, /* RS */
1.199 schwarze 121: { pre_DT, NULL, 0 }, /* DT */
1.158 schwarze 122: { pre_ign, NULL, MAN_NOTEXT }, /* UC */
1.134 schwarze 123: { pre_PD, NULL, MAN_NOTEXT }, /* PD */
1.70 joerg 124: { pre_ign, NULL, 0 }, /* AT */
1.83 kristaps 125: { pre_in, NULL, MAN_NOTEXT }, /* in */
1.127 kristaps 126: { pre_OP, NULL, 0 }, /* OP */
1.129 schwarze 127: { pre_literal, NULL, 0 }, /* EX */
128: { pre_literal, NULL, 0 }, /* EE */
1.137 schwarze 129: { pre_UR, post_UR, 0 }, /* UR */
130: { NULL, NULL, 0 }, /* UE */
1.1 kristaps 131: };
1.192 schwarze 132: static const struct termact *termacts = __termacts - MAN_TH;
1.1 kristaps 133:
134:
1.31 kristaps 135: void
1.180 schwarze 136: terminal_man(void *arg, const struct roff_man *man)
1.1 kristaps 137: {
1.36 kristaps 138: struct termp *p;
1.172 schwarze 139: struct roff_node *n;
1.36 kristaps 140: struct mtermp mt;
1.189 schwarze 141: size_t save_defindent;
1.36 kristaps 142:
143: p = (struct termp *)arg;
1.58 kristaps 144: p->overstep = 0;
1.151 schwarze 145: p->rmargin = p->maxrmargin = p->defrmargin;
1.198 schwarze 146: term_tab_set(p, NULL);
147: term_tab_set(p, "T");
148: term_tab_set(p, ".5i");
1.36 kristaps 149:
1.116 schwarze 150: memset(&mt, 0, sizeof(struct mtermp));
1.122 schwarze 151: mt.lmargin[mt.lmargincur] = term_len(p, p->defindent);
152: mt.offset = term_len(p, p->defindent);
1.134 schwarze 153: mt.pardist = 1;
1.24 kristaps 154:
1.181 schwarze 155: n = man->first->child;
1.151 schwarze 156: if (p->synopsisonly) {
157: while (n != NULL) {
158: if (n->tok == MAN_SH &&
1.171 schwarze 159: n->child->child->type == ROFFT_TEXT &&
1.151 schwarze 160: !strcmp(n->child->child->string, "SYNOPSIS")) {
161: if (n->child->next->child != NULL)
162: print_man_nodelist(p, &mt,
1.181 schwarze 163: n->child->next->child,
164: &man->meta);
1.151 schwarze 165: term_newln(p);
166: break;
167: }
168: n = n->next;
169: }
170: } else {
1.189 schwarze 171: save_defindent = p->defindent;
1.151 schwarze 172: if (p->defindent == 0)
173: p->defindent = 7;
1.181 schwarze 174: term_begin(p, print_man_head, print_man_foot, &man->meta);
1.151 schwarze 175: p->flags |= TERMP_NOSPACE;
176: if (n != NULL)
1.181 schwarze 177: print_man_nodelist(p, &mt, n, &man->meta);
1.151 schwarze 178: term_end(p);
1.189 schwarze 179: p->defindent = save_defindent;
1.151 schwarze 180: }
1.1 kristaps 181: }
182:
1.111 kristaps 183: /*
184: * Printing leading vertical space before a block.
185: * This is used for the paragraph macros.
186: * The rules are pretty simple, since there's very little nesting going
187: * on here. Basically, if we're the first within another block (SS/SH),
188: * then don't emit vertical space. If we are (RS), then do. If not the
189: * first, print it.
190: */
1.39 kristaps 191: static void
1.172 schwarze 192: print_bvspace(struct termp *p, const struct roff_node *n, int pardist)
1.18 kristaps 193: {
1.134 schwarze 194: int i;
1.111 kristaps 195:
1.39 kristaps 196: term_newln(p);
1.101 schwarze 197:
1.111 kristaps 198: if (n->body && n->body->child)
1.171 schwarze 199: if (n->body->child->type == ROFFT_TBL)
1.111 kristaps 200: return;
201:
1.171 schwarze 202: if (n->parent->type == ROFFT_ROOT || n->parent->tok != MAN_RS)
1.111 kristaps 203: if (NULL == n->prev)
204: return;
1.18 kristaps 205:
1.134 schwarze 206: for (i = 0; i < pardist; i++)
207: term_vspace(p);
1.18 kristaps 208: }
209:
1.146 schwarze 210:
1.1 kristaps 211: static int
1.29 kristaps 212: pre_ign(DECL_ARGS)
213: {
214:
1.185 schwarze 215: return 0;
1.29 kristaps 216: }
217:
218: static int
1.1 kristaps 219: pre_I(DECL_ARGS)
220: {
221:
1.52 kristaps 222: term_fontrepl(p, TERMFONT_UNDER);
1.185 schwarze 223: return 1;
1.19 kristaps 224: }
225:
1.3 kristaps 226: static int
1.84 kristaps 227: pre_literal(DECL_ARGS)
1.19 kristaps 228: {
229:
1.81 kristaps 230: term_newln(p);
1.88 kristaps 231:
1.129 schwarze 232: if (MAN_nf == n->tok || MAN_EX == n->tok)
1.84 kristaps 233: mt->fl |= MANT_LITERAL;
1.88 kristaps 234: else
1.84 kristaps 235: mt->fl &= ~MANT_LITERAL;
236:
1.117 schwarze 237: /*
238: * Unlike .IP and .TP, .HP does not have a HEAD.
239: * So in case a second call to term_flushln() is needed,
240: * indentation has to be set up explicitly.
241: */
242: if (MAN_HP == n->parent->tok && p->rmargin < p->maxrmargin) {
1.121 schwarze 243: p->offset = p->rmargin;
1.117 schwarze 244: p->rmargin = p->maxrmargin;
1.139 schwarze 245: p->trailspace = 0;
1.145 schwarze 246: p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
1.117 schwarze 247: p->flags |= TERMP_NOSPACE;
248: }
249:
1.185 schwarze 250: return 0;
1.19 kristaps 251: }
252:
253: static int
1.134 schwarze 254: pre_PD(DECL_ARGS)
255: {
1.160 schwarze 256: struct roffsu su;
1.134 schwarze 257:
258: n = n->child;
1.160 schwarze 259: if (n == NULL) {
1.134 schwarze 260: mt->pardist = 1;
1.185 schwarze 261: return 0;
1.134 schwarze 262: }
1.171 schwarze 263: assert(n->type == ROFFT_TEXT);
1.160 schwarze 264: if (a2roffsu(n->string, &su, SCALE_VS))
265: mt->pardist = term_vspan(p, &su);
1.185 schwarze 266: return 0;
1.134 schwarze 267: }
268:
269: static int
1.88 kristaps 270: pre_alternate(DECL_ARGS)
1.3 kristaps 271: {
1.88 kristaps 272: enum termfont font[2];
1.172 schwarze 273: struct roff_node *nn;
1.88 kristaps 274: int savelit, i;
1.3 kristaps 275:
1.88 kristaps 276: switch (n->tok) {
1.146 schwarze 277: case MAN_RB:
1.88 kristaps 278: font[0] = TERMFONT_NONE;
279: font[1] = TERMFONT_BOLD;
280: break;
1.146 schwarze 281: case MAN_RI:
1.88 kristaps 282: font[0] = TERMFONT_NONE;
283: font[1] = TERMFONT_UNDER;
284: break;
1.146 schwarze 285: case MAN_BR:
1.88 kristaps 286: font[0] = TERMFONT_BOLD;
287: font[1] = TERMFONT_NONE;
288: break;
1.146 schwarze 289: case MAN_BI:
1.88 kristaps 290: font[0] = TERMFONT_BOLD;
291: font[1] = TERMFONT_UNDER;
292: break;
1.146 schwarze 293: case MAN_IR:
1.88 kristaps 294: font[0] = TERMFONT_UNDER;
295: font[1] = TERMFONT_NONE;
296: break;
1.146 schwarze 297: case MAN_IB:
1.88 kristaps 298: font[0] = TERMFONT_UNDER;
299: font[1] = TERMFONT_BOLD;
300: break;
301: default:
302: abort();
303: }
1.35 kristaps 304:
1.88 kristaps 305: savelit = MANT_LITERAL & mt->fl;
306: mt->fl &= ~MANT_LITERAL;
1.35 kristaps 307:
1.88 kristaps 308: for (i = 0, nn = n->child; nn; nn = nn->next, i = 1 - i) {
309: term_fontrepl(p, font[i]);
310: if (savelit && NULL == nn->next)
311: mt->fl |= MANT_LITERAL;
1.179 schwarze 312: assert(nn->type == ROFFT_TEXT);
313: term_word(p, nn->string);
1.188 schwarze 314: if (nn->flags & NODE_EOS)
1.179 schwarze 315: p->flags |= TERMP_SENTENCE;
1.88 kristaps 316: if (nn->next)
1.4 kristaps 317: p->flags |= TERMP_NOSPACE;
1.3 kristaps 318: }
319:
1.185 schwarze 320: return 0;
1.3 kristaps 321: }
322:
1.1 kristaps 323: static int
324: pre_B(DECL_ARGS)
325: {
326:
1.52 kristaps 327: term_fontrepl(p, TERMFONT_BOLD);
1.185 schwarze 328: return 1;
1.127 kristaps 329: }
330:
331: static int
332: pre_OP(DECL_ARGS)
333: {
334:
335: term_word(p, "[");
336: p->flags |= TERMP_NOSPACE;
337:
338: if (NULL != (n = n->child)) {
339: term_fontrepl(p, TERMFONT_BOLD);
340: term_word(p, n->string);
341: }
342: if (NULL != n && NULL != n->next) {
343: term_fontrepl(p, TERMFONT_UNDER);
344: term_word(p, n->next->string);
345: }
346:
347: term_fontrepl(p, TERMFONT_NONE);
348: p->flags |= TERMP_NOSPACE;
349: term_word(p, "]");
1.185 schwarze 350: return 0;
1.83 kristaps 351: }
352:
353: static int
354: pre_in(DECL_ARGS)
355: {
1.163 schwarze 356: struct roffsu su;
357: const char *cp;
1.83 kristaps 358: size_t v;
1.163 schwarze 359: int less;
1.83 kristaps 360:
361: term_newln(p);
362:
363: if (NULL == n->child) {
364: p->offset = mt->offset;
1.185 schwarze 365: return 0;
1.83 kristaps 366: }
367:
368: cp = n->child->string;
369: less = 0;
370:
371: if ('-' == *cp)
372: less = -1;
373: else if ('+' == *cp)
374: less = 1;
375: else
376: cp--;
377:
1.163 schwarze 378: if ( ! a2roffsu(++cp, &su, SCALE_EN))
1.185 schwarze 379: return 0;
1.83 kristaps 380:
1.175 schwarze 381: v = (term_hspan(p, &su) + 11) / 24;
1.83 kristaps 382:
383: if (less < 0)
384: p->offset -= p->offset > v ? v : p->offset;
385: else if (less > 0)
386: p->offset += v;
1.146 schwarze 387: else
1.83 kristaps 388: p->offset = v;
1.164 schwarze 389: if (p->offset > SHRT_MAX)
390: p->offset = term_len(p, p->defindent);
1.19 kristaps 391:
1.199 schwarze 392: return 0;
393: }
394:
395: static int
396: pre_DT(DECL_ARGS)
397: {
398: term_tab_set(p, NULL);
399: term_tab_set(p, "T");
400: term_tab_set(p, ".5i");
1.185 schwarze 401: return 0;
1.15 kristaps 402: }
403:
404: static int
1.19 kristaps 405: pre_HP(DECL_ARGS)
406: {
1.163 schwarze 407: struct roffsu su;
1.172 schwarze 408: const struct roff_node *nn;
1.163 schwarze 409: int len;
1.19 kristaps 410:
1.20 kristaps 411: switch (n->type) {
1.171 schwarze 412: case ROFFT_BLOCK:
1.134 schwarze 413: print_bvspace(p, n, mt->pardist);
1.185 schwarze 414: return 1;
1.171 schwarze 415: case ROFFT_BODY:
1.20 kristaps 416: break;
417: default:
1.185 schwarze 418: return 0;
1.20 kristaps 419: }
420:
1.130 schwarze 421: if ( ! (MANT_LITERAL & mt->fl)) {
1.145 schwarze 422: p->flags |= TERMP_NOBREAK | TERMP_BRIND;
1.139 schwarze 423: p->trailspace = 2;
1.130 schwarze 424: }
425:
1.24 kristaps 426: /* Calculate offset. */
427:
1.163 schwarze 428: if ((nn = n->parent->head->child) != NULL &&
429: a2roffsu(nn->string, &su, SCALE_EN)) {
1.175 schwarze 430: len = term_hspan(p, &su) / 24;
1.164 schwarze 431: if (len < 0 && (size_t)(-len) > mt->offset)
432: len = -mt->offset;
433: else if (len > SHRT_MAX)
434: len = term_len(p, p->defindent);
1.163 schwarze 435: mt->lmargin[mt->lmargincur] = len;
436: } else
437: len = mt->lmargin[mt->lmargincur];
1.24 kristaps 438:
1.26 kristaps 439: p->offset = mt->offset;
1.164 schwarze 440: p->rmargin = mt->offset + len;
1.185 schwarze 441: return 1;
1.19 kristaps 442: }
443:
1.20 kristaps 444: static void
445: post_HP(DECL_ARGS)
446: {
447:
448: switch (n->type) {
1.171 schwarze 449: case ROFFT_BODY:
1.136 schwarze 450: term_newln(p);
1.174 schwarze 451:
452: /*
453: * Compatibility with a groff bug.
454: * The .HP macro uses the undocumented .tag request
455: * which causes a line break and cancels no-space
456: * mode even if there isn't any output.
457: */
458:
459: if (n->child == NULL)
460: term_vspace(p);
461:
1.145 schwarze 462: p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
1.139 schwarze 463: p->trailspace = 0;
1.26 kristaps 464: p->offset = mt->offset;
1.20 kristaps 465: p->rmargin = p->maxrmargin;
466: break;
467: default:
468: break;
469: }
470: }
471:
1.19 kristaps 472: static int
1.1 kristaps 473: pre_PP(DECL_ARGS)
474: {
475:
1.19 kristaps 476: switch (n->type) {
1.171 schwarze 477: case ROFFT_BLOCK:
1.122 schwarze 478: mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
1.134 schwarze 479: print_bvspace(p, n, mt->pardist);
1.19 kristaps 480: break;
481: default:
1.26 kristaps 482: p->offset = mt->offset;
1.19 kristaps 483: break;
484: }
485:
1.185 schwarze 486: return n->type != ROFFT_HEAD;
1.1 kristaps 487: }
488:
489: static int
1.4 kristaps 490: pre_IP(DECL_ARGS)
491: {
1.163 schwarze 492: struct roffsu su;
1.172 schwarze 493: const struct roff_node *nn;
1.163 schwarze 494: int len, savelit;
1.18 kristaps 495:
1.22 kristaps 496: switch (n->type) {
1.171 schwarze 497: case ROFFT_BODY:
1.22 kristaps 498: p->flags |= TERMP_NOSPACE;
499: break;
1.171 schwarze 500: case ROFFT_HEAD:
1.22 kristaps 501: p->flags |= TERMP_NOBREAK;
1.139 schwarze 502: p->trailspace = 1;
1.22 kristaps 503: break;
1.171 schwarze 504: case ROFFT_BLOCK:
1.134 schwarze 505: print_bvspace(p, n, mt->pardist);
1.23 kristaps 506: /* FALLTHROUGH */
1.22 kristaps 507: default:
1.185 schwarze 508: return 1;
1.22 kristaps 509: }
1.18 kristaps 510:
1.94 schwarze 511: /* Calculate the offset from the optional second argument. */
1.163 schwarze 512: if ((nn = n->parent->head->child) != NULL &&
513: (nn = nn->next) != NULL &&
514: a2roffsu(nn->string, &su, SCALE_EN)) {
1.175 schwarze 515: len = term_hspan(p, &su) / 24;
1.163 schwarze 516: if (len < 0 && (size_t)(-len) > mt->offset)
517: len = -mt->offset;
1.164 schwarze 518: else if (len > SHRT_MAX)
519: len = term_len(p, p->defindent);
520: mt->lmargin[mt->lmargincur] = len;
1.163 schwarze 521: } else
522: len = mt->lmargin[mt->lmargincur];
1.4 kristaps 523:
1.22 kristaps 524: switch (n->type) {
1.171 schwarze 525: case ROFFT_HEAD:
1.26 kristaps 526: p->offset = mt->offset;
527: p->rmargin = mt->offset + len;
1.18 kristaps 528:
1.94 schwarze 529: savelit = MANT_LITERAL & mt->fl;
530: mt->fl &= ~MANT_LITERAL;
531:
532: if (n->child)
1.135 schwarze 533: print_man_node(p, mt, n->child, meta);
1.94 schwarze 534:
535: if (savelit)
536: mt->fl |= MANT_LITERAL;
537:
1.185 schwarze 538: return 0;
1.171 schwarze 539: case ROFFT_BODY:
1.26 kristaps 540: p->offset = mt->offset + len;
1.156 schwarze 541: p->rmargin = p->maxrmargin;
1.23 kristaps 542: break;
1.22 kristaps 543: default:
544: break;
1.18 kristaps 545: }
546:
1.185 schwarze 547: return 1;
1.22 kristaps 548: }
1.18 kristaps 549:
1.22 kristaps 550: static void
551: post_IP(DECL_ARGS)
552: {
1.4 kristaps 553:
1.22 kristaps 554: switch (n->type) {
1.171 schwarze 555: case ROFFT_HEAD:
1.22 kristaps 556: term_flushln(p);
557: p->flags &= ~TERMP_NOBREAK;
1.139 schwarze 558: p->trailspace = 0;
1.22 kristaps 559: p->rmargin = p->maxrmargin;
560: break;
1.171 schwarze 561: case ROFFT_BODY:
1.94 schwarze 562: term_newln(p);
1.138 schwarze 563: p->offset = mt->offset;
1.22 kristaps 564: break;
565: default:
566: break;
567: }
1.4 kristaps 568: }
569:
570: static int
1.1 kristaps 571: pre_TP(DECL_ARGS)
572: {
1.163 schwarze 573: struct roffsu su;
1.172 schwarze 574: struct roff_node *nn;
1.163 schwarze 575: int len, savelit;
1.1 kristaps 576:
1.21 kristaps 577: switch (n->type) {
1.171 schwarze 578: case ROFFT_HEAD:
1.184 schwarze 579: p->flags |= TERMP_NOBREAK | TERMP_BRTRSP;
1.139 schwarze 580: p->trailspace = 1;
1.21 kristaps 581: break;
1.171 schwarze 582: case ROFFT_BODY:
1.21 kristaps 583: p->flags |= TERMP_NOSPACE;
1.23 kristaps 584: break;
1.171 schwarze 585: case ROFFT_BLOCK:
1.134 schwarze 586: print_bvspace(p, n, mt->pardist);
1.23 kristaps 587: /* FALLTHROUGH */
588: default:
1.185 schwarze 589: return 1;
1.23 kristaps 590: }
591:
592: /* Calculate offset. */
593:
1.163 schwarze 594: if ((nn = n->parent->head->child) != NULL &&
1.188 schwarze 595: nn->string != NULL && ! (NODE_LINE & nn->flags) &&
1.163 schwarze 596: a2roffsu(nn->string, &su, SCALE_EN)) {
1.175 schwarze 597: len = term_hspan(p, &su) / 24;
1.163 schwarze 598: if (len < 0 && (size_t)(-len) > mt->offset)
599: len = -mt->offset;
1.164 schwarze 600: else if (len > SHRT_MAX)
601: len = term_len(p, p->defindent);
602: mt->lmargin[mt->lmargincur] = len;
1.163 schwarze 603: } else
604: len = mt->lmargin[mt->lmargincur];
1.23 kristaps 605:
606: switch (n->type) {
1.171 schwarze 607: case ROFFT_HEAD:
1.26 kristaps 608: p->offset = mt->offset;
609: p->rmargin = mt->offset + len;
1.23 kristaps 610:
1.94 schwarze 611: savelit = MANT_LITERAL & mt->fl;
612: mt->fl &= ~MANT_LITERAL;
613:
1.23 kristaps 614: /* Don't print same-line elements. */
1.141 schwarze 615: nn = n->child;
1.188 schwarze 616: while (NULL != nn && 0 == (NODE_LINE & nn->flags))
1.141 schwarze 617: nn = nn->next;
618:
619: while (NULL != nn) {
620: print_man_node(p, mt, nn, meta);
621: nn = nn->next;
622: }
1.24 kristaps 623:
1.94 schwarze 624: if (savelit)
625: mt->fl |= MANT_LITERAL;
1.185 schwarze 626: return 0;
1.171 schwarze 627: case ROFFT_BODY:
1.26 kristaps 628: p->offset = mt->offset + len;
1.156 schwarze 629: p->rmargin = p->maxrmargin;
1.139 schwarze 630: p->trailspace = 0;
1.184 schwarze 631: p->flags &= ~(TERMP_NOBREAK | TERMP_BRTRSP);
1.21 kristaps 632: break;
633: default:
634: break;
635: }
1.16 kristaps 636:
1.185 schwarze 637: return 1;
1.21 kristaps 638: }
1.1 kristaps 639:
1.21 kristaps 640: static void
641: post_TP(DECL_ARGS)
642: {
1.1 kristaps 643:
1.21 kristaps 644: switch (n->type) {
1.171 schwarze 645: case ROFFT_HEAD:
1.21 kristaps 646: term_flushln(p);
647: break;
1.171 schwarze 648: case ROFFT_BODY:
1.94 schwarze 649: term_newln(p);
1.138 schwarze 650: p->offset = mt->offset;
1.21 kristaps 651: break;
652: default:
653: break;
654: }
1.1 kristaps 655: }
656:
657: static int
658: pre_SS(DECL_ARGS)
659: {
1.134 schwarze 660: int i;
1.1 kristaps 661:
1.19 kristaps 662: switch (n->type) {
1.171 schwarze 663: case ROFFT_BLOCK:
1.113 kristaps 664: mt->fl &= ~MANT_LITERAL;
1.122 schwarze 665: mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
666: mt->offset = term_len(p, p->defindent);
1.158 schwarze 667:
668: /*
669: * No vertical space before the first subsection
670: * and after an empty subsection.
671: */
672:
673: do {
674: n = n->prev;
1.182 schwarze 675: } while (n != NULL && n->tok != TOKEN_NONE &&
1.170 schwarze 676: termacts[n->tok].flags & MAN_NOTEXT);
1.158 schwarze 677: if (n == NULL || (n->tok == MAN_SS && n->body->child == NULL))
1.24 kristaps 678: break;
1.158 schwarze 679:
1.134 schwarze 680: for (i = 0; i < mt->pardist; i++)
681: term_vspace(p);
1.19 kristaps 682: break;
1.171 schwarze 683: case ROFFT_HEAD:
1.52 kristaps 684: term_fontrepl(p, TERMFONT_BOLD);
1.133 schwarze 685: p->offset = term_len(p, 3);
1.176 schwarze 686: p->rmargin = mt->offset;
687: p->trailspace = mt->offset;
688: p->flags |= TERMP_NOBREAK | TERMP_BRIND;
1.19 kristaps 689: break;
1.171 schwarze 690: case ROFFT_BODY:
1.26 kristaps 691: p->offset = mt->offset;
1.176 schwarze 692: p->rmargin = p->maxrmargin;
693: p->trailspace = 0;
694: p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
1.24 kristaps 695: break;
1.19 kristaps 696: default:
697: break;
698: }
699:
1.185 schwarze 700: return 1;
1.1 kristaps 701: }
702:
703: static void
704: post_SS(DECL_ARGS)
705: {
1.146 schwarze 706:
1.19 kristaps 707: switch (n->type) {
1.171 schwarze 708: case ROFFT_HEAD:
1.19 kristaps 709: term_newln(p);
710: break;
1.171 schwarze 711: case ROFFT_BODY:
1.24 kristaps 712: term_newln(p);
713: break;
1.19 kristaps 714: default:
715: break;
716: }
1.1 kristaps 717: }
718:
719: static int
720: pre_SH(DECL_ARGS)
721: {
1.134 schwarze 722: int i;
1.22 kristaps 723:
1.19 kristaps 724: switch (n->type) {
1.171 schwarze 725: case ROFFT_BLOCK:
1.113 kristaps 726: mt->fl &= ~MANT_LITERAL;
1.122 schwarze 727: mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
728: mt->offset = term_len(p, p->defindent);
1.158 schwarze 729:
730: /*
731: * No vertical space before the first section
732: * and after an empty section.
733: */
734:
735: do {
736: n = n->prev;
1.191 schwarze 737: } while (n != NULL && n->tok != TOKEN_NONE &&
1.190 schwarze 738: termacts[n->tok].flags & MAN_NOTEXT);
1.158 schwarze 739: if (n == NULL || (n->tok == MAN_SH && n->body->child == NULL))
1.61 kristaps 740: break;
1.158 schwarze 741:
1.134 schwarze 742: for (i = 0; i < mt->pardist; i++)
743: term_vspace(p);
1.19 kristaps 744: break;
1.171 schwarze 745: case ROFFT_HEAD:
1.52 kristaps 746: term_fontrepl(p, TERMFONT_BOLD);
1.19 kristaps 747: p->offset = 0;
1.176 schwarze 748: p->rmargin = mt->offset;
749: p->trailspace = mt->offset;
750: p->flags |= TERMP_NOBREAK | TERMP_BRIND;
1.19 kristaps 751: break;
1.171 schwarze 752: case ROFFT_BODY:
1.26 kristaps 753: p->offset = mt->offset;
1.176 schwarze 754: p->rmargin = p->maxrmargin;
755: p->trailspace = 0;
756: p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
1.19 kristaps 757: break;
758: default:
759: break;
760: }
1.1 kristaps 761:
1.185 schwarze 762: return 1;
1.1 kristaps 763: }
764:
765: static void
766: post_SH(DECL_ARGS)
767: {
1.146 schwarze 768:
1.19 kristaps 769: switch (n->type) {
1.171 schwarze 770: case ROFFT_HEAD:
1.19 kristaps 771: term_newln(p);
772: break;
1.171 schwarze 773: case ROFFT_BODY:
1.19 kristaps 774: term_newln(p);
775: break;
776: default:
777: break;
778: }
1.1 kristaps 779: }
780:
1.26 kristaps 781: static int
782: pre_RS(DECL_ARGS)
783: {
1.163 schwarze 784: struct roffsu su;
1.26 kristaps 785:
786: switch (n->type) {
1.171 schwarze 787: case ROFFT_BLOCK:
1.26 kristaps 788: term_newln(p);
1.185 schwarze 789: return 1;
1.171 schwarze 790: case ROFFT_HEAD:
1.185 schwarze 791: return 0;
1.26 kristaps 792: default:
793: break;
794: }
795:
1.165 schwarze 796: n = n->parent->head;
797: n->aux = SHRT_MAX + 1;
1.177 schwarze 798: if (n->child == NULL)
799: n->aux = mt->lmargin[mt->lmargincur];
800: else if (a2roffsu(n->child->string, &su, SCALE_EN))
1.175 schwarze 801: n->aux = term_hspan(p, &su) / 24;
1.165 schwarze 802: if (n->aux < 0 && (size_t)(-n->aux) > mt->offset)
803: n->aux = -mt->offset;
804: else if (n->aux > SHRT_MAX)
805: n->aux = term_len(p, p->defindent);
1.26 kristaps 806:
1.165 schwarze 807: mt->offset += n->aux;
1.140 schwarze 808: p->offset = mt->offset;
1.156 schwarze 809: p->rmargin = p->maxrmargin;
1.26 kristaps 810:
1.116 schwarze 811: if (++mt->lmarginsz < MAXMARGINS)
812: mt->lmargincur = mt->lmarginsz;
813:
1.178 schwarze 814: mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
1.185 schwarze 815: return 1;
1.26 kristaps 816: }
817:
818: static void
819: post_RS(DECL_ARGS)
820: {
821:
822: switch (n->type) {
1.171 schwarze 823: case ROFFT_BLOCK:
1.110 kristaps 824: return;
1.171 schwarze 825: case ROFFT_HEAD:
1.110 kristaps 826: return;
1.26 kristaps 827: default:
828: term_newln(p);
829: break;
830: }
1.110 kristaps 831:
1.165 schwarze 832: mt->offset -= n->parent->head->aux;
1.110 kristaps 833: p->offset = mt->offset;
1.116 schwarze 834:
835: if (--mt->lmarginsz < MAXMARGINS)
836: mt->lmargincur = mt->lmarginsz;
1.137 schwarze 837: }
838:
839: static int
840: pre_UR(DECL_ARGS)
841: {
842:
1.185 schwarze 843: return n->type != ROFFT_HEAD;
1.137 schwarze 844: }
845:
846: static void
847: post_UR(DECL_ARGS)
848: {
849:
1.171 schwarze 850: if (n->type != ROFFT_BLOCK)
1.137 schwarze 851: return;
852:
853: term_word(p, "<");
854: p->flags |= TERMP_NOSPACE;
855:
856: if (NULL != n->child->child)
857: print_man_node(p, mt, n->child->child, meta);
858:
859: p->flags |= TERMP_NOSPACE;
860: term_word(p, ">");
1.26 kristaps 861: }
862:
1.1 kristaps 863: static void
1.45 kristaps 864: print_man_node(DECL_ARGS)
1.1 kristaps 865: {
1.65 joerg 866: size_t rm, rmax;
1.54 kristaps 867: int c;
1.1 kristaps 868:
869: switch (n->type) {
1.171 schwarze 870: case ROFFT_TEXT:
1.97 kristaps 871: /*
872: * If we have a blank line, output a vertical space.
873: * If we have a space as the first character, break
874: * before printing the line's data.
875: */
1.200 ! schwarze 876: if (*n->string == '\0') {
1.4 kristaps 877: term_vspace(p);
1.98 schwarze 878: return;
1.200 ! schwarze 879: } else if (*n->string == ' ' && n->flags & NODE_LINE &&
! 880: (p->flags & TERMP_NONEWLINE) == 0)
1.96 kristaps 881: term_newln(p);
1.54 kristaps 882:
1.4 kristaps 883: term_word(p, n->string);
1.130 schwarze 884: goto out;
1.52 kristaps 885:
1.171 schwarze 886: case ROFFT_EQN:
1.188 schwarze 887: if ( ! (n->flags & NODE_LINE))
1.152 schwarze 888: p->flags |= TERMP_NOSPACE;
1.115 kristaps 889: term_eqn(p, n->eqn);
1.188 schwarze 890: if (n->next != NULL && ! (n->next->flags & NODE_LINE))
1.153 schwarze 891: p->flags |= TERMP_NOSPACE;
1.97 kristaps 892: return;
1.171 schwarze 893: case ROFFT_TBL:
1.169 schwarze 894: if (p->tbl.cols == NULL)
895: term_vspace(p);
1.92 kristaps 896: term_tbl(p, n->span);
1.97 kristaps 897: return;
1.1 kristaps 898: default:
899: break;
900: }
901:
1.193 schwarze 902: if (n->tok < ROFF_MAX) {
1.194 schwarze 903: roff_term_pre(p, n);
1.193 schwarze 904: return;
905: }
906:
907: assert(n->tok >= MAN_TH && n->tok <= MAN_MAX);
1.97 kristaps 908: if ( ! (MAN_NOTEXT & termacts[n->tok].flags))
909: term_fontrepl(p, TERMFONT_NONE);
910:
911: c = 1;
912: if (termacts[n->tok].pre)
1.135 schwarze 913: c = (*termacts[n->tok].pre)(p, mt, n, meta);
1.97 kristaps 914:
1.1 kristaps 915: if (c && n->child)
1.135 schwarze 916: print_man_nodelist(p, mt, n->child, meta);
1.1 kristaps 917:
1.97 kristaps 918: if (termacts[n->tok].post)
1.135 schwarze 919: (*termacts[n->tok].post)(p, mt, n, meta);
1.97 kristaps 920: if ( ! (MAN_NOTEXT & termacts[n->tok].flags))
921: term_fontrepl(p, TERMFONT_NONE);
1.63 kristaps 922:
1.130 schwarze 923: out:
924: /*
925: * If we're in a literal context, make sure that words
926: * together on the same line stay together. This is a
927: * POST-printing call, so we check the NEXT word. Since
928: * -man doesn't have nested macros, we don't need to be
929: * more specific than this.
930: */
1.157 schwarze 931: if (mt->fl & MANT_LITERAL &&
932: ! (p->flags & (TERMP_NOBREAK | TERMP_NONEWLINE)) &&
1.188 schwarze 933: (n->next == NULL || n->next->flags & NODE_LINE)) {
1.130 schwarze 934: rm = p->rmargin;
935: rmax = p->maxrmargin;
936: p->rmargin = p->maxrmargin = TERM_MAXMARGIN;
937: p->flags |= TERMP_NOSPACE;
1.157 schwarze 938: if (n->string != NULL && *n->string != '\0')
1.130 schwarze 939: term_flushln(p);
940: else
941: term_newln(p);
942: if (rm < rmax && n->parent->tok == MAN_HP) {
943: p->offset = rm;
944: p->rmargin = rmax;
945: } else
946: p->rmargin = rm;
947: p->maxrmargin = rmax;
948: }
1.188 schwarze 949: if (NODE_EOS & n->flags)
1.63 kristaps 950: p->flags |= TERMP_SENTENCE;
1.1 kristaps 951: }
952:
953:
954: static void
1.52 kristaps 955: print_man_nodelist(DECL_ARGS)
1.1 kristaps 956: {
1.19 kristaps 957:
1.168 schwarze 958: while (n != NULL) {
959: print_man_node(p, mt, n, meta);
960: n = n->next;
961: }
1.1 kristaps 962: }
963:
1.31 kristaps 964: static void
1.173 schwarze 965: print_man_foot(struct termp *p, const struct roff_meta *meta)
1.1 kristaps 966: {
1.147 schwarze 967: char *title;
1.156 schwarze 968: size_t datelen, titlen;
1.73 kristaps 969:
1.125 schwarze 970: assert(meta->title);
971: assert(meta->msec);
972: assert(meta->date);
1.1 kristaps 973:
1.52 kristaps 974: term_fontrepl(p, TERMFONT_NONE);
1.50 kristaps 975:
1.149 schwarze 976: if (meta->hasbody)
977: term_vspace(p);
1.126 schwarze 978:
979: /*
980: * Temporary, undocumented option to imitate mdoc(7) output.
1.173 schwarze 981: * In the bottom right corner, use the operating system
982: * instead of the title.
1.126 schwarze 983: */
984:
985: if ( ! p->mdocstyle) {
1.149 schwarze 986: if (meta->hasbody) {
987: term_vspace(p);
988: term_vspace(p);
989: }
1.147 schwarze 990: mandoc_asprintf(&title, "%s(%s)",
991: meta->title, meta->msec);
1.173 schwarze 992: } else if (meta->os) {
993: title = mandoc_strdup(meta->os);
1.126 schwarze 994: } else {
1.147 schwarze 995: title = mandoc_strdup("");
1.126 schwarze 996: }
1.125 schwarze 997: datelen = term_strlen(p, meta->date);
1.1 kristaps 998:
1.173 schwarze 999: /* Bottom left corner: operating system. */
1.126 schwarze 1000:
1.1 kristaps 1001: p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
1.139 schwarze 1002: p->trailspace = 1;
1.1 kristaps 1003: p->offset = 0;
1.156 schwarze 1004: p->rmargin = p->maxrmargin > datelen ?
1005: (p->maxrmargin + term_len(p, 1) - datelen) / 2 : 0;
1.1 kristaps 1006:
1.173 schwarze 1007: if (meta->os)
1008: term_word(p, meta->os);
1.1 kristaps 1009: term_flushln(p);
1010:
1.126 schwarze 1011: /* At the bottom in the middle: manual date. */
1012:
1.156 schwarze 1013: p->offset = p->rmargin;
1014: titlen = term_strlen(p, title);
1015: p->rmargin = p->maxrmargin > titlen ? p->maxrmargin - titlen : 0;
1.117 schwarze 1016: p->flags |= TERMP_NOSPACE;
1.123 schwarze 1017:
1.125 schwarze 1018: term_word(p, meta->date);
1.123 schwarze 1019: term_flushln(p);
1020:
1.126 schwarze 1021: /* Bottom right corner: manual title and section. */
1022:
1.123 schwarze 1023: p->flags &= ~TERMP_NOBREAK;
1024: p->flags |= TERMP_NOSPACE;
1.139 schwarze 1025: p->trailspace = 0;
1.123 schwarze 1026: p->offset = p->rmargin;
1.1 kristaps 1027: p->rmargin = p->maxrmargin;
1028:
1.123 schwarze 1029: term_word(p, title);
1.1 kristaps 1030: term_flushln(p);
1.147 schwarze 1031: free(title);
1.1 kristaps 1032: }
1033:
1.31 kristaps 1034: static void
1.173 schwarze 1035: print_man_head(struct termp *p, const struct roff_meta *meta)
1.1 kristaps 1036: {
1.148 schwarze 1037: const char *volume;
1.147 schwarze 1038: char *title;
1.148 schwarze 1039: size_t vollen, titlen;
1.73 kristaps 1040:
1.135 schwarze 1041: assert(meta->title);
1042: assert(meta->msec);
1.1 kristaps 1043:
1.148 schwarze 1044: volume = NULL == meta->vol ? "" : meta->vol;
1045: vollen = term_strlen(p, volume);
1.1 kristaps 1046:
1.126 schwarze 1047: /* Top left corner: manual title and section. */
1048:
1.147 schwarze 1049: mandoc_asprintf(&title, "%s(%s)", meta->title, meta->msec);
1.77 kristaps 1050: titlen = term_strlen(p, title);
1.1 kristaps 1051:
1.118 schwarze 1052: p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
1.139 schwarze 1053: p->trailspace = 1;
1.1 kristaps 1054: p->offset = 0;
1.148 schwarze 1055: p->rmargin = 2 * (titlen+1) + vollen < p->maxrmargin ?
1056: (p->maxrmargin - vollen + term_len(p, 1)) / 2 :
1.156 schwarze 1057: vollen < p->maxrmargin ? p->maxrmargin - vollen : 0;
1.1 kristaps 1058:
1059: term_word(p, title);
1060: term_flushln(p);
1061:
1.126 schwarze 1062: /* At the top in the middle: manual volume. */
1063:
1.117 schwarze 1064: p->flags |= TERMP_NOSPACE;
1.1 kristaps 1065: p->offset = p->rmargin;
1.148 schwarze 1066: p->rmargin = p->offset + vollen + titlen < p->maxrmargin ?
1.57 kristaps 1067: p->maxrmargin - titlen : p->maxrmargin;
1.1 kristaps 1068:
1.148 schwarze 1069: term_word(p, volume);
1.1 kristaps 1070: term_flushln(p);
1071:
1.126 schwarze 1072: /* Top right corner: title and section, again. */
1073:
1.1 kristaps 1074: p->flags &= ~TERMP_NOBREAK;
1.139 schwarze 1075: p->trailspace = 0;
1.57 kristaps 1076: if (p->rmargin + titlen <= p->maxrmargin) {
1.117 schwarze 1077: p->flags |= TERMP_NOSPACE;
1.57 kristaps 1078: p->offset = p->rmargin;
1079: p->rmargin = p->maxrmargin;
1080: term_word(p, title);
1081: term_flushln(p);
1082: }
1.1 kristaps 1083:
1.118 schwarze 1084: p->flags &= ~TERMP_NOSPACE;
1085: p->offset = 0;
1.1 kristaps 1086: p->rmargin = p->maxrmargin;
1.61 kristaps 1087:
1.146 schwarze 1088: /*
1.126 schwarze 1089: * Groff prints three blank lines before the content.
1090: * Do the same, except in the temporary, undocumented
1091: * mode imitating mdoc(7) output.
1.62 kristaps 1092: */
1093:
1.61 kristaps 1094: term_vspace(p);
1.126 schwarze 1095: if ( ! p->mdocstyle) {
1096: term_vspace(p);
1097: term_vspace(p);
1098: }
1.147 schwarze 1099: free(title);
1.1 kristaps 1100: }
CVSweb