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