Annotation of mandoc/man_term.c, Revision 1.232
1.232 ! schwarze 1: /* $Id: man_term.c,v 1.231 2019/07/22 03:21:49 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;
1.232 ! schwarze 623:
! 624: if (nn == NULL)
! 625: return 0;
! 626:
! 627: if (nn->type == ROFFT_TEXT)
! 628: tag_man(p, nn);
! 629: else if (nn->child != NULL &&
! 630: nn->child->type == ROFFT_TEXT &&
! 631: (nn->tok == MAN_B || nn->tok == MAN_BI ||
! 632: nn->tok == MAN_BR || nn->tok == MAN_I ||
! 633: nn->tok == MAN_IB || nn->tok == MAN_IR))
! 634: tag_man(p, nn->child);
1.141 schwarze 635:
1.227 schwarze 636: while (nn != NULL) {
1.141 schwarze 637: print_man_node(p, mt, nn, meta);
638: nn = nn->next;
639: }
1.185 schwarze 640: return 0;
1.171 schwarze 641: case ROFFT_BODY:
1.203 schwarze 642: p->tcol->offset = mt->offset + len;
643: p->tcol->rmargin = p->maxrmargin;
1.139 schwarze 644: p->trailspace = 0;
1.184 schwarze 645: p->flags &= ~(TERMP_NOBREAK | TERMP_BRTRSP);
1.21 kristaps 646: break;
647: default:
1.227 schwarze 648: abort();
1.21 kristaps 649: }
1.185 schwarze 650: return 1;
1.21 kristaps 651: }
1.1 kristaps 652:
1.21 kristaps 653: static void
654: post_TP(DECL_ARGS)
655: {
656: switch (n->type) {
1.227 schwarze 657: case ROFFT_BLOCK:
658: break;
1.171 schwarze 659: case ROFFT_HEAD:
1.21 kristaps 660: term_flushln(p);
661: break;
1.171 schwarze 662: case ROFFT_BODY:
1.94 schwarze 663: term_newln(p);
1.203 schwarze 664: p->tcol->offset = mt->offset;
1.21 kristaps 665: break;
666: default:
1.227 schwarze 667: abort();
1.21 kristaps 668: }
1.1 kristaps 669: }
670:
671: static int
672: pre_SS(DECL_ARGS)
673: {
1.134 schwarze 674: int i;
1.1 kristaps 675:
1.19 kristaps 676: switch (n->type) {
1.171 schwarze 677: case ROFFT_BLOCK:
1.122 schwarze 678: mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
679: mt->offset = term_len(p, p->defindent);
1.158 schwarze 680:
681: /*
682: * No vertical space before the first subsection
683: * and after an empty subsection.
684: */
685:
686: do {
687: n = n->prev;
1.209 schwarze 688: } while (n != NULL && n->tok >= MAN_TH &&
1.214 schwarze 689: man_term_act(n->tok)->flags & MAN_NOTEXT);
1.210 schwarze 690: if (n == NULL || n->type == ROFFT_COMMENT ||
691: (n->tok == MAN_SS && n->body->child == NULL))
1.24 kristaps 692: break;
1.158 schwarze 693:
1.134 schwarze 694: for (i = 0; i < mt->pardist; i++)
695: term_vspace(p);
1.19 kristaps 696: break;
1.171 schwarze 697: case ROFFT_HEAD:
1.52 kristaps 698: term_fontrepl(p, TERMFONT_BOLD);
1.203 schwarze 699: p->tcol->offset = term_len(p, 3);
700: p->tcol->rmargin = mt->offset;
1.176 schwarze 701: p->trailspace = mt->offset;
702: p->flags |= TERMP_NOBREAK | TERMP_BRIND;
1.19 kristaps 703: break;
1.171 schwarze 704: case ROFFT_BODY:
1.203 schwarze 705: p->tcol->offset = mt->offset;
706: p->tcol->rmargin = p->maxrmargin;
1.176 schwarze 707: p->trailspace = 0;
708: p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
1.24 kristaps 709: break;
1.19 kristaps 710: default:
711: break;
712: }
1.185 schwarze 713: return 1;
1.1 kristaps 714: }
715:
716: static int
717: pre_SH(DECL_ARGS)
718: {
1.134 schwarze 719: int i;
1.22 kristaps 720:
1.19 kristaps 721: switch (n->type) {
1.171 schwarze 722: case ROFFT_BLOCK:
1.122 schwarze 723: mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
724: mt->offset = term_len(p, p->defindent);
1.158 schwarze 725:
726: /*
727: * No vertical space before the first section
728: * and after an empty section.
729: */
730:
731: do {
732: n = n->prev;
1.209 schwarze 733: } while (n != NULL && n->tok >= MAN_TH &&
1.214 schwarze 734: man_term_act(n->tok)->flags & MAN_NOTEXT);
1.210 schwarze 735: if (n == NULL || n->type == ROFFT_COMMENT ||
736: (n->tok == MAN_SH && n->body->child == NULL))
1.61 kristaps 737: break;
1.158 schwarze 738:
1.134 schwarze 739: for (i = 0; i < mt->pardist; i++)
740: term_vspace(p);
1.19 kristaps 741: break;
1.171 schwarze 742: case ROFFT_HEAD:
1.52 kristaps 743: term_fontrepl(p, TERMFONT_BOLD);
1.203 schwarze 744: p->tcol->offset = 0;
745: p->tcol->rmargin = mt->offset;
1.176 schwarze 746: p->trailspace = mt->offset;
747: p->flags |= TERMP_NOBREAK | TERMP_BRIND;
1.19 kristaps 748: break;
1.171 schwarze 749: case ROFFT_BODY:
1.203 schwarze 750: p->tcol->offset = mt->offset;
751: p->tcol->rmargin = p->maxrmargin;
1.176 schwarze 752: p->trailspace = 0;
753: p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
1.19 kristaps 754: break;
755: default:
1.227 schwarze 756: abort();
1.19 kristaps 757: }
1.185 schwarze 758: return 1;
1.1 kristaps 759: }
760:
761: static void
762: post_SH(DECL_ARGS)
763: {
1.19 kristaps 764: switch (n->type) {
1.227 schwarze 765: case ROFFT_BLOCK:
766: break;
1.171 schwarze 767: case ROFFT_HEAD:
768: case ROFFT_BODY:
1.19 kristaps 769: term_newln(p);
770: break;
771: default:
1.227 schwarze 772: abort();
1.19 kristaps 773: }
1.1 kristaps 774: }
775:
1.26 kristaps 776: static int
777: pre_RS(DECL_ARGS)
778: {
1.163 schwarze 779: struct roffsu su;
1.26 kristaps 780:
781: switch (n->type) {
1.171 schwarze 782: case ROFFT_BLOCK:
1.26 kristaps 783: term_newln(p);
1.185 schwarze 784: return 1;
1.171 schwarze 785: case ROFFT_HEAD:
1.185 schwarze 786: return 0;
1.227 schwarze 787: case ROFFT_BODY:
788: break;
1.26 kristaps 789: default:
1.227 schwarze 790: abort();
1.26 kristaps 791: }
792:
1.165 schwarze 793: n = n->parent->head;
794: n->aux = SHRT_MAX + 1;
1.177 schwarze 795: if (n->child == NULL)
796: n->aux = mt->lmargin[mt->lmargincur];
1.204 schwarze 797: else if (a2roffsu(n->child->string, &su, SCALE_EN) != NULL)
1.205 schwarze 798: n->aux = term_hen(p, &su);
1.165 schwarze 799: if (n->aux < 0 && (size_t)(-n->aux) > mt->offset)
800: n->aux = -mt->offset;
801: else if (n->aux > SHRT_MAX)
802: n->aux = term_len(p, p->defindent);
1.26 kristaps 803:
1.165 schwarze 804: mt->offset += n->aux;
1.203 schwarze 805: p->tcol->offset = mt->offset;
806: p->tcol->rmargin = p->maxrmargin;
1.26 kristaps 807:
1.116 schwarze 808: if (++mt->lmarginsz < MAXMARGINS)
809: mt->lmargincur = mt->lmarginsz;
810:
1.178 schwarze 811: mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
1.185 schwarze 812: return 1;
1.26 kristaps 813: }
814:
815: static void
816: post_RS(DECL_ARGS)
817: {
818: switch (n->type) {
1.171 schwarze 819: case ROFFT_BLOCK:
820: case ROFFT_HEAD:
1.110 kristaps 821: return;
1.227 schwarze 822: case ROFFT_BODY:
823: break;
1.26 kristaps 824: default:
1.227 schwarze 825: abort();
1.26 kristaps 826: }
1.227 schwarze 827: term_newln(p);
1.165 schwarze 828: mt->offset -= n->parent->head->aux;
1.203 schwarze 829: p->tcol->offset = mt->offset;
1.116 schwarze 830: if (--mt->lmarginsz < MAXMARGINS)
831: mt->lmargincur = mt->lmarginsz;
1.215 schwarze 832: }
833:
834: static int
835: pre_SY(DECL_ARGS)
836: {
837: const struct roff_node *nn;
838: int len;
839:
840: switch (n->type) {
841: case ROFFT_BLOCK:
1.217 schwarze 842: if (n->prev == NULL || n->prev->tok != MAN_SY)
843: print_bvspace(p, n, mt->pardist);
1.215 schwarze 844: return 1;
845: case ROFFT_HEAD:
846: case ROFFT_BODY:
847: break;
848: default:
849: abort();
850: }
851:
852: nn = n->parent->head->child;
1.219 schwarze 853: len = nn == NULL ? 1 : term_strlen(p, nn->string) + 1;
1.215 schwarze 854:
855: switch (n->type) {
856: case ROFFT_HEAD:
857: p->tcol->offset = mt->offset;
858: p->tcol->rmargin = mt->offset + len;
1.228 schwarze 859: if (n->next->child == NULL ||
860: (n->next->child->flags & NODE_NOFILL) == 0)
861: p->flags |= TERMP_NOBREAK;
1.215 schwarze 862: term_fontrepl(p, TERMFONT_BOLD);
863: break;
864: case ROFFT_BODY:
865: mt->lmargin[mt->lmargincur] = len;
866: p->tcol->offset = mt->offset + len;
867: p->tcol->rmargin = p->maxrmargin;
868: p->flags |= TERMP_NOSPACE;
869: break;
870: default:
871: abort();
872: }
873: return 1;
874: }
875:
876: static void
877: post_SY(DECL_ARGS)
878: {
879: switch (n->type) {
1.227 schwarze 880: case ROFFT_BLOCK:
881: break;
1.215 schwarze 882: case ROFFT_HEAD:
883: term_flushln(p);
884: p->flags &= ~TERMP_NOBREAK;
885: break;
886: case ROFFT_BODY:
887: term_newln(p);
888: p->tcol->offset = mt->offset;
889: break;
890: default:
1.227 schwarze 891: abort();
1.215 schwarze 892: }
1.137 schwarze 893: }
894:
895: static int
896: pre_UR(DECL_ARGS)
897: {
1.185 schwarze 898: return n->type != ROFFT_HEAD;
1.137 schwarze 899: }
900:
901: static void
902: post_UR(DECL_ARGS)
903: {
1.171 schwarze 904: if (n->type != ROFFT_BLOCK)
1.137 schwarze 905: return;
906:
907: term_word(p, "<");
908: p->flags |= TERMP_NOSPACE;
909:
1.227 schwarze 910: if (n->child->child != NULL)
1.137 schwarze 911: print_man_node(p, mt, n->child->child, meta);
912:
913: p->flags |= TERMP_NOSPACE;
914: term_word(p, ">");
1.26 kristaps 915: }
916:
1.1 kristaps 917: static void
1.45 kristaps 918: print_man_node(DECL_ARGS)
1.1 kristaps 919: {
1.214 schwarze 920: const struct man_term_act *act;
921: int c;
1.1 kristaps 922:
923: switch (n->type) {
1.171 schwarze 924: case ROFFT_TEXT:
1.97 kristaps 925: /*
926: * If we have a blank line, output a vertical space.
927: * If we have a space as the first character, break
928: * before printing the line's data.
929: */
1.200 schwarze 930: if (*n->string == '\0') {
1.207 schwarze 931: if (p->flags & TERMP_NONEWLINE)
932: term_newln(p);
933: else
934: term_vspace(p);
1.98 schwarze 935: return;
1.200 schwarze 936: } else if (*n->string == ' ' && n->flags & NODE_LINE &&
937: (p->flags & TERMP_NONEWLINE) == 0)
1.96 kristaps 938: term_newln(p);
1.212 schwarze 939: else if (n->flags & NODE_DELIMC)
940: p->flags |= TERMP_NOSPACE;
1.54 kristaps 941:
1.4 kristaps 942: term_word(p, n->string);
1.130 schwarze 943: goto out;
1.210 schwarze 944: case ROFFT_COMMENT:
945: return;
1.171 schwarze 946: case ROFFT_EQN:
1.188 schwarze 947: if ( ! (n->flags & NODE_LINE))
1.152 schwarze 948: p->flags |= TERMP_NOSPACE;
1.115 kristaps 949: term_eqn(p, n->eqn);
1.188 schwarze 950: if (n->next != NULL && ! (n->next->flags & NODE_LINE))
1.153 schwarze 951: p->flags |= TERMP_NOSPACE;
1.97 kristaps 952: return;
1.171 schwarze 953: case ROFFT_TBL:
1.169 schwarze 954: if (p->tbl.cols == NULL)
955: term_vspace(p);
1.92 kristaps 956: term_tbl(p, n->span);
1.97 kristaps 957: return;
1.1 kristaps 958: default:
959: break;
960: }
1.224 schwarze 961:
1.193 schwarze 962: if (n->tok < ROFF_MAX) {
1.194 schwarze 963: roff_term_pre(p, n);
1.193 schwarze 964: return;
965: }
966:
1.214 schwarze 967: act = man_term_act(n->tok);
1.220 schwarze 968: if ((act->flags & MAN_NOTEXT) == 0 && n->tok != MAN_SM)
1.97 kristaps 969: term_fontrepl(p, TERMFONT_NONE);
970:
971: c = 1;
1.214 schwarze 972: if (act->pre != NULL)
973: c = (*act->pre)(p, mt, n, meta);
1.97 kristaps 974:
1.227 schwarze 975: if (c && n->child != NULL)
1.135 schwarze 976: print_man_nodelist(p, mt, n->child, meta);
1.1 kristaps 977:
1.214 schwarze 978: if (act->post != NULL)
979: (*act->post)(p, mt, n, meta);
1.220 schwarze 980: if ((act->flags & MAN_NOTEXT) == 0 && n->tok != MAN_SM)
1.97 kristaps 981: term_fontrepl(p, TERMFONT_NONE);
1.63 kristaps 982:
1.130 schwarze 983: out:
984: /*
985: * If we're in a literal context, make sure that words
986: * together on the same line stay together. This is a
987: * POST-printing call, so we check the NEXT word. Since
988: * -man doesn't have nested macros, we don't need to be
989: * more specific than this.
990: */
1.225 schwarze 991: if (n->flags & NODE_NOFILL &&
1.157 schwarze 992: ! (p->flags & (TERMP_NOBREAK | TERMP_NONEWLINE)) &&
1.188 schwarze 993: (n->next == NULL || n->next->flags & NODE_LINE)) {
1.202 schwarze 994: p->flags |= TERMP_BRNEVER | TERMP_NOSPACE;
1.157 schwarze 995: if (n->string != NULL && *n->string != '\0')
1.130 schwarze 996: term_flushln(p);
997: else
998: term_newln(p);
1.202 schwarze 999: p->flags &= ~TERMP_BRNEVER;
1.203 schwarze 1000: if (p->tcol->rmargin < p->maxrmargin &&
1001: n->parent->tok == MAN_HP) {
1002: p->tcol->offset = p->tcol->rmargin;
1003: p->tcol->rmargin = p->maxrmargin;
1.202 schwarze 1004: }
1.130 schwarze 1005: }
1.227 schwarze 1006: if (n->flags & NODE_EOS)
1.63 kristaps 1007: p->flags |= TERMP_SENTENCE;
1.1 kristaps 1008: }
1009:
1010: static void
1.52 kristaps 1011: print_man_nodelist(DECL_ARGS)
1.1 kristaps 1012: {
1.168 schwarze 1013: while (n != NULL) {
1014: print_man_node(p, mt, n, meta);
1015: n = n->next;
1016: }
1.1 kristaps 1017: }
1018:
1.31 kristaps 1019: static void
1.173 schwarze 1020: print_man_foot(struct termp *p, const struct roff_meta *meta)
1.1 kristaps 1021: {
1.147 schwarze 1022: char *title;
1.156 schwarze 1023: size_t datelen, titlen;
1.73 kristaps 1024:
1.125 schwarze 1025: assert(meta->title);
1026: assert(meta->msec);
1027: assert(meta->date);
1.1 kristaps 1028:
1.52 kristaps 1029: term_fontrepl(p, TERMFONT_NONE);
1.50 kristaps 1030:
1.149 schwarze 1031: if (meta->hasbody)
1032: term_vspace(p);
1.126 schwarze 1033:
1034: /*
1035: * Temporary, undocumented option to imitate mdoc(7) output.
1.173 schwarze 1036: * In the bottom right corner, use the operating system
1037: * instead of the title.
1.126 schwarze 1038: */
1039:
1040: if ( ! p->mdocstyle) {
1.149 schwarze 1041: if (meta->hasbody) {
1042: term_vspace(p);
1043: term_vspace(p);
1044: }
1.147 schwarze 1045: mandoc_asprintf(&title, "%s(%s)",
1046: meta->title, meta->msec);
1.227 schwarze 1047: } else if (meta->os != NULL) {
1.173 schwarze 1048: title = mandoc_strdup(meta->os);
1.126 schwarze 1049: } else {
1.147 schwarze 1050: title = mandoc_strdup("");
1.126 schwarze 1051: }
1.125 schwarze 1052: datelen = term_strlen(p, meta->date);
1.1 kristaps 1053:
1.173 schwarze 1054: /* Bottom left corner: operating system. */
1.126 schwarze 1055:
1.1 kristaps 1056: p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
1.139 schwarze 1057: p->trailspace = 1;
1.203 schwarze 1058: p->tcol->offset = 0;
1059: p->tcol->rmargin = p->maxrmargin > datelen ?
1.156 schwarze 1060: (p->maxrmargin + term_len(p, 1) - datelen) / 2 : 0;
1.1 kristaps 1061:
1.173 schwarze 1062: if (meta->os)
1063: term_word(p, meta->os);
1.1 kristaps 1064: term_flushln(p);
1065:
1.126 schwarze 1066: /* At the bottom in the middle: manual date. */
1067:
1.203 schwarze 1068: p->tcol->offset = p->tcol->rmargin;
1.156 schwarze 1069: titlen = term_strlen(p, title);
1.203 schwarze 1070: p->tcol->rmargin = p->maxrmargin > titlen ?
1071: p->maxrmargin - titlen : 0;
1.117 schwarze 1072: p->flags |= TERMP_NOSPACE;
1.123 schwarze 1073:
1.125 schwarze 1074: term_word(p, meta->date);
1.123 schwarze 1075: term_flushln(p);
1076:
1.126 schwarze 1077: /* Bottom right corner: manual title and section. */
1078:
1.123 schwarze 1079: p->flags &= ~TERMP_NOBREAK;
1080: p->flags |= TERMP_NOSPACE;
1.139 schwarze 1081: p->trailspace = 0;
1.203 schwarze 1082: p->tcol->offset = p->tcol->rmargin;
1083: p->tcol->rmargin = p->maxrmargin;
1.1 kristaps 1084:
1.123 schwarze 1085: term_word(p, title);
1.1 kristaps 1086: term_flushln(p);
1.211 schwarze 1087:
1088: /*
1089: * Reset the terminal state for more output after the footer:
1090: * Some output modes, in particular PostScript and PDF, print
1091: * the header and the footer into a buffer such that it can be
1092: * reused for multiple output pages, then go on to format the
1093: * main text.
1094: */
1095:
1096: p->tcol->offset = 0;
1097: p->flags = 0;
1098:
1.147 schwarze 1099: free(title);
1.1 kristaps 1100: }
1101:
1.31 kristaps 1102: static void
1.173 schwarze 1103: print_man_head(struct termp *p, const struct roff_meta *meta)
1.1 kristaps 1104: {
1.148 schwarze 1105: const char *volume;
1.147 schwarze 1106: char *title;
1.148 schwarze 1107: size_t vollen, titlen;
1.73 kristaps 1108:
1.135 schwarze 1109: assert(meta->title);
1110: assert(meta->msec);
1.1 kristaps 1111:
1.148 schwarze 1112: volume = NULL == meta->vol ? "" : meta->vol;
1113: vollen = term_strlen(p, volume);
1.1 kristaps 1114:
1.126 schwarze 1115: /* Top left corner: manual title and section. */
1116:
1.147 schwarze 1117: mandoc_asprintf(&title, "%s(%s)", meta->title, meta->msec);
1.77 kristaps 1118: titlen = term_strlen(p, title);
1.1 kristaps 1119:
1.118 schwarze 1120: p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
1.139 schwarze 1121: p->trailspace = 1;
1.203 schwarze 1122: p->tcol->offset = 0;
1123: p->tcol->rmargin = 2 * (titlen+1) + vollen < p->maxrmargin ?
1.148 schwarze 1124: (p->maxrmargin - vollen + term_len(p, 1)) / 2 :
1.156 schwarze 1125: vollen < p->maxrmargin ? p->maxrmargin - vollen : 0;
1.1 kristaps 1126:
1127: term_word(p, title);
1128: term_flushln(p);
1129:
1.126 schwarze 1130: /* At the top in the middle: manual volume. */
1131:
1.117 schwarze 1132: p->flags |= TERMP_NOSPACE;
1.203 schwarze 1133: p->tcol->offset = p->tcol->rmargin;
1134: p->tcol->rmargin = p->tcol->offset + vollen + titlen <
1135: p->maxrmargin ? p->maxrmargin - titlen : p->maxrmargin;
1.1 kristaps 1136:
1.148 schwarze 1137: term_word(p, volume);
1.1 kristaps 1138: term_flushln(p);
1139:
1.126 schwarze 1140: /* Top right corner: title and section, again. */
1141:
1.1 kristaps 1142: p->flags &= ~TERMP_NOBREAK;
1.139 schwarze 1143: p->trailspace = 0;
1.203 schwarze 1144: if (p->tcol->rmargin + titlen <= p->maxrmargin) {
1.117 schwarze 1145: p->flags |= TERMP_NOSPACE;
1.203 schwarze 1146: p->tcol->offset = p->tcol->rmargin;
1147: p->tcol->rmargin = p->maxrmargin;
1.57 kristaps 1148: term_word(p, title);
1149: term_flushln(p);
1150: }
1.1 kristaps 1151:
1.118 schwarze 1152: p->flags &= ~TERMP_NOSPACE;
1.203 schwarze 1153: p->tcol->offset = 0;
1154: p->tcol->rmargin = p->maxrmargin;
1.61 kristaps 1155:
1.146 schwarze 1156: /*
1.126 schwarze 1157: * Groff prints three blank lines before the content.
1158: * Do the same, except in the temporary, undocumented
1159: * mode imitating mdoc(7) output.
1.62 kristaps 1160: */
1161:
1.61 kristaps 1162: term_vspace(p);
1.126 schwarze 1163: if ( ! p->mdocstyle) {
1164: term_vspace(p);
1165: term_vspace(p);
1166: }
1.147 schwarze 1167: free(title);
1.231 schwarze 1168: }
1169:
1170: /*
1171: * Skip leading whitespace, dashes, backslashes, and font escapes,
1172: * then create a tag if the first following byte is a letter.
1173: * Priority is high unless whitespace is present.
1174: */
1175: static void
1176: tag_man(struct termp *p, struct roff_node *n)
1177: {
1178: const char *cp, *arg;
1179: int prio, sz;
1180:
1181: assert(n->type == ROFFT_TEXT);
1182: cp = n->string;
1183: prio = 1;
1184: for (;;) {
1185: switch (*cp) {
1186: case ' ':
1187: case '\t':
1188: prio = INT_MAX;
1189: /* FALLTHROUGH */
1190: case '-':
1191: cp++;
1192: break;
1193: case '\\':
1194: cp++;
1195: switch (mandoc_escape(&cp, &arg, &sz)) {
1196: case ESCAPE_FONT:
1197: case ESCAPE_FONTROMAN:
1198: case ESCAPE_FONTITALIC:
1199: case ESCAPE_FONTBOLD:
1200: case ESCAPE_FONTPREV:
1201: case ESCAPE_FONTBI:
1202: break;
1203: case ESCAPE_SPECIAL:
1204: if (sz != 1)
1205: return;
1206: switch (*arg) {
1207: case '&':
1208: case '-':
1209: case 'e':
1210: break;
1211: default:
1212: return;
1213: }
1214: break;
1215: default:
1216: return;
1217: }
1218: break;
1219: default:
1220: if (isalpha((unsigned char)*cp))
1221: tag_put(cp, prio, p->line);
1222: return;
1223: }
1224: }
1.1 kristaps 1225: }
CVSweb