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