Annotation of mandoc/man_term.c, Revision 1.148
1.148 ! schwarze 1: /* $Id: man_term.c,v 1.147 2014/04/20 20:18:12 schwarze Exp $ */
1.1 kristaps 2: /*
1.128 schwarze 3: * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
1.140 schwarze 4: * Copyright (c) 2010-2014 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.8 kristaps 10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
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: #ifdef HAVE_CONFIG_H
19: #include "config.h"
20: #endif
21:
1.28 kristaps 22: #include <sys/types.h>
23:
1.1 kristaps 24: #include <assert.h>
1.18 kristaps 25: #include <ctype.h>
1.1 kristaps 26: #include <stdio.h>
27: #include <stdlib.h>
28: #include <string.h>
29:
1.71 kristaps 30: #include "mandoc.h"
1.147 schwarze 31: #include "mandoc_aux.h"
1.40 kristaps 32: #include "out.h"
1.36 kristaps 33: #include "man.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.116 schwarze 42: size_t lmargin[MAXMARGINS]; /* margins (incl. visible page) */
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.1 kristaps 51: const struct man_node *n, \
1.135 schwarze 52: const struct man_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.77 kristaps 61: static int a2width(const struct termp *, const char *);
62: static size_t a2height(const struct termp *, const char *);
1.39 kristaps 63:
1.52 kristaps 64: static void print_man_nodelist(DECL_ARGS);
1.45 kristaps 65: static void print_man_node(DECL_ARGS);
1.74 kristaps 66: static void print_man_head(struct termp *, const void *);
1.73 kristaps 67: static void print_man_foot(struct termp *, const void *);
1.146 schwarze 68: static void print_bvspace(struct termp *,
1.134 schwarze 69: const struct man_node *, int);
1.39 kristaps 70:
1.1 kristaps 71: static int pre_B(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);
84: static int pre_ft(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.143 schwarze 88: static int pre_ll(DECL_ARGS);
1.19 kristaps 89: static int pre_sp(DECL_ARGS);
1.1 kristaps 90:
1.22 kristaps 91: static void post_IP(DECL_ARGS);
1.20 kristaps 92: static void post_HP(DECL_ARGS);
1.26 kristaps 93: static void post_RS(DECL_ARGS);
1.1 kristaps 94: static void post_SH(DECL_ARGS);
95: static void post_SS(DECL_ARGS);
1.21 kristaps 96: static void post_TP(DECL_ARGS);
1.137 schwarze 97: static void post_UR(DECL_ARGS);
1.1 kristaps 98:
1.32 kristaps 99: static const struct termact termacts[MAN_MAX] = {
1.82 kristaps 100: { pre_sp, NULL, MAN_NOTEXT }, /* br */
1.56 kristaps 101: { NULL, NULL, 0 }, /* TH */
102: { pre_SH, post_SH, 0 }, /* SH */
103: { pre_SS, post_SS, 0 }, /* SS */
104: { pre_TP, post_TP, 0 }, /* TP */
105: { pre_PP, NULL, 0 }, /* LP */
106: { pre_PP, NULL, 0 }, /* PP */
107: { pre_PP, NULL, 0 }, /* P */
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.99 schwarze 121: { pre_ign, NULL, MAN_NOTEXT }, /* na */
1.56 kristaps 122: { pre_sp, NULL, MAN_NOTEXT }, /* sp */
1.84 kristaps 123: { pre_literal, NULL, 0 }, /* nf */
124: { pre_literal, NULL, 0 }, /* fi */
1.56 kristaps 125: { NULL, NULL, 0 }, /* RE */
126: { pre_RS, post_RS, 0 }, /* RS */
127: { pre_ign, NULL, 0 }, /* DT */
128: { pre_ign, NULL, 0 }, /* UC */
1.134 schwarze 129: { pre_PD, NULL, MAN_NOTEXT }, /* PD */
1.70 joerg 130: { pre_ign, NULL, 0 }, /* AT */
1.83 kristaps 131: { pre_in, NULL, MAN_NOTEXT }, /* in */
1.89 kristaps 132: { pre_ft, NULL, MAN_NOTEXT }, /* ft */
1.127 kristaps 133: { pre_OP, NULL, 0 }, /* OP */
1.129 schwarze 134: { pre_literal, NULL, 0 }, /* EX */
135: { pre_literal, NULL, 0 }, /* EE */
1.137 schwarze 136: { pre_UR, post_UR, 0 }, /* UR */
137: { NULL, NULL, 0 }, /* UE */
1.143 schwarze 138: { pre_ll, NULL, MAN_NOTEXT }, /* ll */
1.1 kristaps 139: };
140:
141:
1.31 kristaps 142: void
1.36 kristaps 143: terminal_man(void *arg, const struct man *man)
1.1 kristaps 144: {
1.36 kristaps 145: struct termp *p;
146: const struct man_node *n;
1.135 schwarze 147: const struct man_meta *meta;
1.36 kristaps 148: struct mtermp mt;
149:
150: p = (struct termp *)arg;
151:
1.122 schwarze 152: if (0 == p->defindent)
153: p->defindent = 7;
154:
1.58 kristaps 155: p->overstep = 0;
1.65 joerg 156: p->maxrmargin = p->defrmargin;
1.77 kristaps 157: p->tabwidth = term_len(p, 5);
1.73 kristaps 158:
1.36 kristaps 159: if (NULL == p->symtab)
1.109 kristaps 160: p->symtab = mchars_alloc();
1.36 kristaps 161:
162: n = man_node(man);
1.135 schwarze 163: meta = man_meta(man);
1.1 kristaps 164:
1.135 schwarze 165: term_begin(p, print_man_head, print_man_foot, meta);
1.1 kristaps 166: p->flags |= TERMP_NOSPACE;
1.19 kristaps 167:
1.116 schwarze 168: memset(&mt, 0, sizeof(struct mtermp));
169:
1.122 schwarze 170: mt.lmargin[mt.lmargincur] = term_len(p, p->defindent);
171: mt.offset = term_len(p, p->defindent);
1.134 schwarze 172: mt.pardist = 1;
1.24 kristaps 173:
1.36 kristaps 174: if (n->child)
1.135 schwarze 175: print_man_nodelist(p, &mt, n->child, meta);
1.73 kristaps 176:
177: term_end(p);
1.1 kristaps 178: }
179:
180:
1.77 kristaps 181: static size_t
182: a2height(const struct termp *p, const char *cp)
1.18 kristaps 183: {
1.40 kristaps 184: struct roffsu su;
1.18 kristaps 185:
1.77 kristaps 186: if ( ! a2roffsu(cp, &su, SCALE_VS))
1.112 kristaps 187: SCALE_VS_INIT(&su, atoi(cp));
1.18 kristaps 188:
1.77 kristaps 189: return(term_vspan(p, &su));
1.18 kristaps 190: }
191:
192: static int
1.77 kristaps 193: a2width(const struct termp *p, const char *cp)
1.38 kristaps 194: {
1.40 kristaps 195: struct roffsu su;
1.38 kristaps 196:
1.77 kristaps 197: if ( ! a2roffsu(cp, &su, SCALE_BU))
1.41 kristaps 198: return(-1);
1.40 kristaps 199:
1.77 kristaps 200: return((int)term_hspan(p, &su));
1.38 kristaps 201: }
202:
1.111 kristaps 203: /*
204: * Printing leading vertical space before a block.
205: * This is used for the paragraph macros.
206: * The rules are pretty simple, since there's very little nesting going
207: * on here. Basically, if we're the first within another block (SS/SH),
208: * then don't emit vertical space. If we are (RS), then do. If not the
209: * first, print it.
210: */
1.39 kristaps 211: static void
1.134 schwarze 212: print_bvspace(struct termp *p, const struct man_node *n, int pardist)
1.18 kristaps 213: {
1.134 schwarze 214: int i;
1.111 kristaps 215:
1.39 kristaps 216: term_newln(p);
1.101 schwarze 217:
1.111 kristaps 218: if (n->body && n->body->child)
219: if (MAN_TBL == n->body->child->type)
220: return;
221:
222: if (MAN_ROOT == n->parent->type || MAN_RS != n->parent->tok)
223: if (NULL == n->prev)
224: return;
1.18 kristaps 225:
1.134 schwarze 226: for (i = 0; i < pardist; i++)
227: term_vspace(p);
1.18 kristaps 228: }
229:
1.146 schwarze 230:
1.1 kristaps 231: static int
1.29 kristaps 232: pre_ign(DECL_ARGS)
233: {
234:
1.143 schwarze 235: return(0);
236: }
237:
238: static int
239: pre_ll(DECL_ARGS)
240: {
241:
1.144 schwarze 242: term_setwidth(p, n->nchild ? n->child->string : NULL);
1.29 kristaps 243: return(0);
244: }
245:
246: static int
1.1 kristaps 247: pre_I(DECL_ARGS)
248: {
249:
1.52 kristaps 250: term_fontrepl(p, TERMFONT_UNDER);
1.19 kristaps 251: return(1);
252: }
253:
1.3 kristaps 254: static int
1.84 kristaps 255: pre_literal(DECL_ARGS)
1.19 kristaps 256: {
257:
1.81 kristaps 258: term_newln(p);
1.88 kristaps 259:
1.129 schwarze 260: if (MAN_nf == n->tok || MAN_EX == n->tok)
1.84 kristaps 261: mt->fl |= MANT_LITERAL;
1.88 kristaps 262: else
1.84 kristaps 263: mt->fl &= ~MANT_LITERAL;
264:
1.117 schwarze 265: /*
266: * Unlike .IP and .TP, .HP does not have a HEAD.
267: * So in case a second call to term_flushln() is needed,
268: * indentation has to be set up explicitly.
269: */
270: if (MAN_HP == n->parent->tok && p->rmargin < p->maxrmargin) {
1.121 schwarze 271: p->offset = p->rmargin;
1.117 schwarze 272: p->rmargin = p->maxrmargin;
1.139 schwarze 273: p->trailspace = 0;
1.145 schwarze 274: p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
1.117 schwarze 275: p->flags |= TERMP_NOSPACE;
276: }
277:
1.99 schwarze 278: return(0);
1.19 kristaps 279: }
280:
281: static int
1.134 schwarze 282: pre_PD(DECL_ARGS)
283: {
284:
285: n = n->child;
286: if (0 == n) {
287: mt->pardist = 1;
288: return(0);
289: }
290: assert(MAN_TEXT == n->type);
291: mt->pardist = atoi(n->string);
292: return(0);
293: }
294:
295: static int
1.88 kristaps 296: pre_alternate(DECL_ARGS)
1.3 kristaps 297: {
1.88 kristaps 298: enum termfont font[2];
299: const struct man_node *nn;
300: int savelit, i;
1.3 kristaps 301:
1.88 kristaps 302: switch (n->tok) {
1.146 schwarze 303: case MAN_RB:
1.88 kristaps 304: font[0] = TERMFONT_NONE;
305: font[1] = TERMFONT_BOLD;
306: break;
1.146 schwarze 307: case MAN_RI:
1.88 kristaps 308: font[0] = TERMFONT_NONE;
309: font[1] = TERMFONT_UNDER;
310: break;
1.146 schwarze 311: case MAN_BR:
1.88 kristaps 312: font[0] = TERMFONT_BOLD;
313: font[1] = TERMFONT_NONE;
314: break;
1.146 schwarze 315: case MAN_BI:
1.88 kristaps 316: font[0] = TERMFONT_BOLD;
317: font[1] = TERMFONT_UNDER;
318: break;
1.146 schwarze 319: case MAN_IR:
1.88 kristaps 320: font[0] = TERMFONT_UNDER;
321: font[1] = TERMFONT_NONE;
322: break;
1.146 schwarze 323: case MAN_IB:
1.88 kristaps 324: font[0] = TERMFONT_UNDER;
325: font[1] = TERMFONT_BOLD;
326: break;
327: default:
328: abort();
329: }
1.35 kristaps 330:
1.88 kristaps 331: savelit = MANT_LITERAL & mt->fl;
332: mt->fl &= ~MANT_LITERAL;
1.35 kristaps 333:
1.88 kristaps 334: for (i = 0, nn = n->child; nn; nn = nn->next, i = 1 - i) {
335: term_fontrepl(p, font[i]);
336: if (savelit && NULL == nn->next)
337: mt->fl |= MANT_LITERAL;
1.135 schwarze 338: print_man_node(p, mt, nn, meta);
1.88 kristaps 339: if (nn->next)
1.4 kristaps 340: p->flags |= TERMP_NOSPACE;
1.3 kristaps 341: }
342:
343: return(0);
344: }
345:
1.1 kristaps 346: static int
347: pre_B(DECL_ARGS)
348: {
349:
1.52 kristaps 350: term_fontrepl(p, TERMFONT_BOLD);
1.1 kristaps 351: return(1);
1.127 kristaps 352: }
353:
354: static int
355: pre_OP(DECL_ARGS)
356: {
357:
358: term_word(p, "[");
359: p->flags |= TERMP_NOSPACE;
360:
361: if (NULL != (n = n->child)) {
362: term_fontrepl(p, TERMFONT_BOLD);
363: term_word(p, n->string);
364: }
365: if (NULL != n && NULL != n->next) {
366: term_fontrepl(p, TERMFONT_UNDER);
367: term_word(p, n->next->string);
368: }
369:
370: term_fontrepl(p, TERMFONT_NONE);
371: p->flags |= TERMP_NOSPACE;
372: term_word(p, "]");
373: return(0);
1.89 kristaps 374: }
375:
376: static int
377: pre_ft(DECL_ARGS)
378: {
379: const char *cp;
380:
381: if (NULL == n->child) {
382: term_fontlast(p);
383: return(0);
384: }
385:
386: cp = n->child->string;
387: switch (*cp) {
1.146 schwarze 388: case '4':
1.89 kristaps 389: /* FALLTHROUGH */
1.146 schwarze 390: case '3':
1.89 kristaps 391: /* FALLTHROUGH */
1.146 schwarze 392: case 'B':
1.89 kristaps 393: term_fontrepl(p, TERMFONT_BOLD);
394: break;
1.146 schwarze 395: case '2':
1.89 kristaps 396: /* FALLTHROUGH */
1.146 schwarze 397: case 'I':
1.89 kristaps 398: term_fontrepl(p, TERMFONT_UNDER);
399: break;
1.146 schwarze 400: case 'P':
1.89 kristaps 401: term_fontlast(p);
402: break;
1.146 schwarze 403: case '1':
1.89 kristaps 404: /* FALLTHROUGH */
1.146 schwarze 405: case 'C':
1.89 kristaps 406: /* FALLTHROUGH */
1.146 schwarze 407: case 'R':
1.89 kristaps 408: term_fontrepl(p, TERMFONT_NONE);
409: break;
410: default:
411: break;
412: }
413: return(0);
1.83 kristaps 414: }
415:
416: static int
417: pre_in(DECL_ARGS)
418: {
419: int len, less;
420: size_t v;
421: const char *cp;
422:
423: term_newln(p);
424:
425: if (NULL == n->child) {
426: p->offset = mt->offset;
427: return(0);
428: }
429:
430: cp = n->child->string;
431: less = 0;
432:
433: if ('-' == *cp)
434: less = -1;
435: else if ('+' == *cp)
436: less = 1;
437: else
438: cp--;
439:
440: if ((len = a2width(p, ++cp)) < 0)
441: return(0);
442:
443: v = (size_t)len;
444:
445: if (less < 0)
446: p->offset -= p->offset > v ? v : p->offset;
447: else if (less > 0)
448: p->offset += v;
1.146 schwarze 449: else
1.83 kristaps 450: p->offset = v;
1.95 kristaps 451:
452: /* Don't let this creep beyond the right margin. */
453:
454: if (p->offset > p->rmargin)
455: p->offset = p->rmargin;
1.83 kristaps 456:
457: return(0);
1.1 kristaps 458: }
459:
460: static int
1.19 kristaps 461: pre_sp(DECL_ARGS)
462: {
1.131 schwarze 463: char *s;
1.77 kristaps 464: size_t i, len;
1.131 schwarze 465: int neg;
1.112 kristaps 466:
467: if ((NULL == n->prev && n->parent)) {
1.132 schwarze 468: switch (n->parent->tok) {
1.146 schwarze 469: case MAN_SH:
1.132 schwarze 470: /* FALLTHROUGH */
1.146 schwarze 471: case MAN_SS:
1.132 schwarze 472: /* FALLTHROUGH */
1.146 schwarze 473: case MAN_PP:
1.132 schwarze 474: /* FALLTHROUGH */
1.146 schwarze 475: case MAN_LP:
1.132 schwarze 476: /* FALLTHROUGH */
1.146 schwarze 477: case MAN_P:
1.132 schwarze 478: /* FALLTHROUGH */
1.112 kristaps 479: return(0);
1.132 schwarze 480: default:
481: break;
482: }
1.112 kristaps 483: }
1.19 kristaps 484:
1.131 schwarze 485: neg = 0;
1.82 kristaps 486: switch (n->tok) {
1.146 schwarze 487: case MAN_br:
1.82 kristaps 488: len = 0;
489: break;
490: default:
1.131 schwarze 491: if (NULL == n->child) {
492: len = 1;
493: break;
494: }
495: s = n->child->string;
496: if ('-' == *s) {
497: neg = 1;
498: s++;
499: }
500: len = a2height(p, s);
1.82 kristaps 501: break;
502: }
1.38 kristaps 503:
1.19 kristaps 504: if (0 == len)
505: term_newln(p);
1.131 schwarze 506: else if (neg)
507: p->skipvsp += len;
508: else
509: for (i = 0; i < len; i++)
510: term_vspace(p);
1.19 kristaps 511:
1.15 kristaps 512: return(0);
513: }
514:
515: static int
1.19 kristaps 516: pre_HP(DECL_ARGS)
517: {
1.117 schwarze 518: size_t len, one;
1.24 kristaps 519: int ival;
520: const struct man_node *nn;
1.19 kristaps 521:
1.20 kristaps 522: switch (n->type) {
1.146 schwarze 523: case MAN_BLOCK:
1.134 schwarze 524: print_bvspace(p, n, mt->pardist);
1.24 kristaps 525: return(1);
1.146 schwarze 526: case MAN_BODY:
1.20 kristaps 527: break;
528: default:
529: return(0);
530: }
531:
1.130 schwarze 532: if ( ! (MANT_LITERAL & mt->fl)) {
1.145 schwarze 533: p->flags |= TERMP_NOBREAK | TERMP_BRIND;
1.139 schwarze 534: p->trailspace = 2;
1.130 schwarze 535: }
536:
1.116 schwarze 537: len = mt->lmargin[mt->lmargincur];
1.24 kristaps 538: ival = -1;
539:
540: /* Calculate offset. */
541:
1.39 kristaps 542: if (NULL != (nn = n->parent->head->child))
1.77 kristaps 543: if ((ival = a2width(p, nn->string)) >= 0)
1.24 kristaps 544: len = (size_t)ival;
545:
1.117 schwarze 546: one = term_len(p, 1);
1.121 schwarze 547: if (len < one)
1.117 schwarze 548: len = one;
1.24 kristaps 549:
1.26 kristaps 550: p->offset = mt->offset;
551: p->rmargin = mt->offset + len;
1.24 kristaps 552:
553: if (ival >= 0)
1.116 schwarze 554: mt->lmargin[mt->lmargincur] = (size_t)ival;
1.24 kristaps 555:
1.19 kristaps 556: return(1);
557: }
558:
1.20 kristaps 559: static void
560: post_HP(DECL_ARGS)
561: {
562:
563: switch (n->type) {
1.146 schwarze 564: case MAN_BODY:
1.136 schwarze 565: term_newln(p);
1.145 schwarze 566: p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
1.139 schwarze 567: p->trailspace = 0;
1.26 kristaps 568: p->offset = mt->offset;
1.20 kristaps 569: p->rmargin = p->maxrmargin;
570: break;
571: default:
572: break;
573: }
574: }
575:
1.19 kristaps 576: static int
1.1 kristaps 577: pre_PP(DECL_ARGS)
578: {
579:
1.19 kristaps 580: switch (n->type) {
1.146 schwarze 581: case MAN_BLOCK:
1.122 schwarze 582: mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
1.134 schwarze 583: print_bvspace(p, n, mt->pardist);
1.19 kristaps 584: break;
585: default:
1.26 kristaps 586: p->offset = mt->offset;
1.19 kristaps 587: break;
588: }
589:
1.87 kristaps 590: return(MAN_HEAD != n->type);
1.1 kristaps 591: }
592:
593: static int
1.4 kristaps 594: pre_IP(DECL_ARGS)
595: {
1.22 kristaps 596: const struct man_node *nn;
597: size_t len;
1.94 schwarze 598: int savelit, ival;
1.18 kristaps 599:
1.22 kristaps 600: switch (n->type) {
1.146 schwarze 601: case MAN_BODY:
1.22 kristaps 602: p->flags |= TERMP_NOSPACE;
603: break;
1.146 schwarze 604: case MAN_HEAD:
1.22 kristaps 605: p->flags |= TERMP_NOBREAK;
1.139 schwarze 606: p->trailspace = 1;
1.22 kristaps 607: break;
1.146 schwarze 608: case MAN_BLOCK:
1.134 schwarze 609: print_bvspace(p, n, mt->pardist);
1.23 kristaps 610: /* FALLTHROUGH */
1.22 kristaps 611: default:
612: return(1);
613: }
1.18 kristaps 614:
1.116 schwarze 615: len = mt->lmargin[mt->lmargincur];
1.22 kristaps 616: ival = -1;
1.4 kristaps 617:
1.94 schwarze 618: /* Calculate the offset from the optional second argument. */
1.22 kristaps 619: if (NULL != (nn = n->parent->head->child))
1.94 schwarze 620: if (NULL != (nn = nn->next))
1.77 kristaps 621: if ((ival = a2width(p, nn->string)) >= 0)
1.22 kristaps 622: len = (size_t)ival;
1.4 kristaps 623:
1.22 kristaps 624: switch (n->type) {
1.146 schwarze 625: case MAN_HEAD:
1.23 kristaps 626: /* Handle zero-width lengths. */
627: if (0 == len)
1.77 kristaps 628: len = term_len(p, 1);
1.23 kristaps 629:
1.26 kristaps 630: p->offset = mt->offset;
631: p->rmargin = mt->offset + len;
1.22 kristaps 632: if (ival < 0)
633: break;
1.18 kristaps 634:
1.24 kristaps 635: /* Set the saved left-margin. */
1.116 schwarze 636: mt->lmargin[mt->lmargincur] = (size_t)ival;
1.24 kristaps 637:
1.94 schwarze 638: savelit = MANT_LITERAL & mt->fl;
639: mt->fl &= ~MANT_LITERAL;
640:
641: if (n->child)
1.135 schwarze 642: print_man_node(p, mt, n->child, meta);
1.94 schwarze 643:
644: if (savelit)
645: mt->fl |= MANT_LITERAL;
646:
1.22 kristaps 647: return(0);
1.146 schwarze 648: case MAN_BODY:
1.26 kristaps 649: p->offset = mt->offset + len;
1.140 schwarze 650: p->rmargin = p->maxrmargin > p->offset ?
1.146 schwarze 651: p->maxrmargin : p->offset;
1.23 kristaps 652: break;
1.22 kristaps 653: default:
654: break;
1.18 kristaps 655: }
656:
1.22 kristaps 657: return(1);
658: }
1.18 kristaps 659:
1.22 kristaps 660: static void
661: post_IP(DECL_ARGS)
662: {
1.4 kristaps 663:
1.22 kristaps 664: switch (n->type) {
1.146 schwarze 665: case MAN_HEAD:
1.22 kristaps 666: term_flushln(p);
667: p->flags &= ~TERMP_NOBREAK;
1.139 schwarze 668: p->trailspace = 0;
1.22 kristaps 669: p->rmargin = p->maxrmargin;
670: break;
1.146 schwarze 671: case MAN_BODY:
1.94 schwarze 672: term_newln(p);
1.138 schwarze 673: p->offset = mt->offset;
1.22 kristaps 674: break;
675: default:
676: break;
677: }
1.4 kristaps 678: }
679:
680: static int
1.1 kristaps 681: pre_TP(DECL_ARGS)
682: {
1.23 kristaps 683: const struct man_node *nn;
684: size_t len;
1.94 schwarze 685: int savelit, ival;
1.1 kristaps 686:
1.21 kristaps 687: switch (n->type) {
1.146 schwarze 688: case MAN_HEAD:
1.21 kristaps 689: p->flags |= TERMP_NOBREAK;
1.139 schwarze 690: p->trailspace = 1;
1.21 kristaps 691: break;
1.146 schwarze 692: case MAN_BODY:
1.21 kristaps 693: p->flags |= TERMP_NOSPACE;
1.23 kristaps 694: break;
1.146 schwarze 695: case MAN_BLOCK:
1.134 schwarze 696: print_bvspace(p, n, mt->pardist);
1.23 kristaps 697: /* FALLTHROUGH */
698: default:
699: return(1);
700: }
701:
1.116 schwarze 702: len = (size_t)mt->lmargin[mt->lmargincur];
1.23 kristaps 703: ival = -1;
704:
705: /* Calculate offset. */
706:
1.116 schwarze 707: if (NULL != (nn = n->parent->head->child))
1.141 schwarze 708: if (nn->string && 0 == (MAN_LINE & nn->flags))
1.77 kristaps 709: if ((ival = a2width(p, nn->string)) >= 0)
1.23 kristaps 710: len = (size_t)ival;
711:
712: switch (n->type) {
1.146 schwarze 713: case MAN_HEAD:
1.23 kristaps 714: /* Handle zero-length properly. */
715: if (0 == len)
1.77 kristaps 716: len = term_len(p, 1);
1.23 kristaps 717:
1.26 kristaps 718: p->offset = mt->offset;
719: p->rmargin = mt->offset + len;
1.23 kristaps 720:
1.94 schwarze 721: savelit = MANT_LITERAL & mt->fl;
722: mt->fl &= ~MANT_LITERAL;
723:
1.23 kristaps 724: /* Don't print same-line elements. */
1.141 schwarze 725: nn = n->child;
726: while (NULL != nn && 0 == (MAN_LINE & nn->flags))
727: nn = nn->next;
728:
729: while (NULL != nn) {
730: print_man_node(p, mt, nn, meta);
731: nn = nn->next;
732: }
1.24 kristaps 733:
1.94 schwarze 734: if (savelit)
735: mt->fl |= MANT_LITERAL;
1.24 kristaps 736: if (ival >= 0)
1.116 schwarze 737: mt->lmargin[mt->lmargincur] = (size_t)ival;
1.24 kristaps 738:
1.23 kristaps 739: return(0);
1.146 schwarze 740: case MAN_BODY:
1.26 kristaps 741: p->offset = mt->offset + len;
1.140 schwarze 742: p->rmargin = p->maxrmargin > p->offset ?
1.146 schwarze 743: p->maxrmargin : p->offset;
1.139 schwarze 744: p->trailspace = 0;
1.130 schwarze 745: p->flags &= ~TERMP_NOBREAK;
1.21 kristaps 746: break;
747: default:
748: break;
749: }
1.16 kristaps 750:
1.21 kristaps 751: return(1);
752: }
1.1 kristaps 753:
1.21 kristaps 754: static void
755: post_TP(DECL_ARGS)
756: {
1.1 kristaps 757:
1.21 kristaps 758: switch (n->type) {
1.146 schwarze 759: case MAN_HEAD:
1.21 kristaps 760: term_flushln(p);
761: break;
1.146 schwarze 762: case MAN_BODY:
1.94 schwarze 763: term_newln(p);
1.138 schwarze 764: p->offset = mt->offset;
1.21 kristaps 765: break;
766: default:
767: break;
768: }
1.1 kristaps 769: }
770:
771: static int
772: pre_SS(DECL_ARGS)
773: {
1.134 schwarze 774: int i;
1.1 kristaps 775:
1.19 kristaps 776: switch (n->type) {
1.146 schwarze 777: case MAN_BLOCK:
1.113 kristaps 778: mt->fl &= ~MANT_LITERAL;
1.122 schwarze 779: mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
780: mt->offset = term_len(p, p->defindent);
1.24 kristaps 781: /* If following a prior empty `SS', no vspace. */
782: if (n->prev && MAN_SS == n->prev->tok)
783: if (NULL == n->prev->body->child)
784: break;
785: if (NULL == n->prev)
786: break;
1.134 schwarze 787: for (i = 0; i < mt->pardist; i++)
788: term_vspace(p);
1.19 kristaps 789: break;
1.146 schwarze 790: case MAN_HEAD:
1.52 kristaps 791: term_fontrepl(p, TERMFONT_BOLD);
1.133 schwarze 792: p->offset = term_len(p, 3);
1.19 kristaps 793: break;
1.146 schwarze 794: case MAN_BODY:
1.26 kristaps 795: p->offset = mt->offset;
1.24 kristaps 796: break;
1.19 kristaps 797: default:
798: break;
799: }
800:
1.1 kristaps 801: return(1);
802: }
803:
804: static void
805: post_SS(DECL_ARGS)
806: {
1.146 schwarze 807:
1.19 kristaps 808: switch (n->type) {
1.146 schwarze 809: case MAN_HEAD:
1.19 kristaps 810: term_newln(p);
811: break;
1.146 schwarze 812: case MAN_BODY:
1.24 kristaps 813: term_newln(p);
814: break;
1.19 kristaps 815: default:
816: break;
817: }
1.1 kristaps 818: }
819:
820: static int
821: pre_SH(DECL_ARGS)
822: {
1.134 schwarze 823: int i;
1.22 kristaps 824:
1.19 kristaps 825: switch (n->type) {
1.146 schwarze 826: case MAN_BLOCK:
1.113 kristaps 827: mt->fl &= ~MANT_LITERAL;
1.122 schwarze 828: mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
829: mt->offset = term_len(p, p->defindent);
1.22 kristaps 830: /* If following a prior empty `SH', no vspace. */
1.19 kristaps 831: if (n->prev && MAN_SH == n->prev->tok)
832: if (NULL == n->prev->body->child)
833: break;
1.61 kristaps 834: /* If the first macro, no vspae. */
835: if (NULL == n->prev)
836: break;
1.134 schwarze 837: for (i = 0; i < mt->pardist; i++)
838: term_vspace(p);
1.19 kristaps 839: break;
1.146 schwarze 840: case MAN_HEAD:
1.52 kristaps 841: term_fontrepl(p, TERMFONT_BOLD);
1.19 kristaps 842: p->offset = 0;
843: break;
1.146 schwarze 844: case MAN_BODY:
1.26 kristaps 845: p->offset = mt->offset;
1.19 kristaps 846: break;
847: default:
848: break;
849: }
1.1 kristaps 850:
851: return(1);
852: }
853:
854: static void
855: post_SH(DECL_ARGS)
856: {
1.146 schwarze 857:
1.19 kristaps 858: switch (n->type) {
1.146 schwarze 859: case MAN_HEAD:
1.19 kristaps 860: term_newln(p);
861: break;
1.146 schwarze 862: case MAN_BODY:
1.19 kristaps 863: term_newln(p);
864: break;
865: default:
866: break;
867: }
1.1 kristaps 868: }
869:
1.26 kristaps 870: static int
871: pre_RS(DECL_ARGS)
872: {
1.110 kristaps 873: int ival;
874: size_t sz;
1.26 kristaps 875:
876: switch (n->type) {
1.146 schwarze 877: case MAN_BLOCK:
1.26 kristaps 878: term_newln(p);
879: return(1);
1.146 schwarze 880: case MAN_HEAD:
1.26 kristaps 881: return(0);
882: default:
883: break;
884: }
885:
1.122 schwarze 886: sz = term_len(p, p->defindent);
1.26 kristaps 887:
1.110 kristaps 888: if (NULL != (n = n->parent->head->child))
1.146 schwarze 889: if ((ival = a2width(p, n->string)) >= 0)
1.110 kristaps 890: sz = (size_t)ival;
1.26 kristaps 891:
1.110 kristaps 892: mt->offset += sz;
1.140 schwarze 893: p->offset = mt->offset;
894: p->rmargin = p->maxrmargin > p->offset ?
1.146 schwarze 895: p->maxrmargin : p->offset;
1.26 kristaps 896:
1.116 schwarze 897: if (++mt->lmarginsz < MAXMARGINS)
898: mt->lmargincur = mt->lmarginsz;
899:
900: mt->lmargin[mt->lmargincur] = mt->lmargin[mt->lmargincur - 1];
1.26 kristaps 901: return(1);
902: }
903:
904: static void
905: post_RS(DECL_ARGS)
906: {
1.110 kristaps 907: int ival;
908: size_t sz;
1.26 kristaps 909:
910: switch (n->type) {
1.146 schwarze 911: case MAN_BLOCK:
1.110 kristaps 912: return;
1.146 schwarze 913: case MAN_HEAD:
1.110 kristaps 914: return;
1.26 kristaps 915: default:
916: term_newln(p);
917: break;
918: }
1.110 kristaps 919:
1.122 schwarze 920: sz = term_len(p, p->defindent);
1.110 kristaps 921:
1.146 schwarze 922: if (NULL != (n = n->parent->head->child))
923: if ((ival = a2width(p, n->string)) >= 0)
1.110 kristaps 924: sz = (size_t)ival;
925:
926: mt->offset = mt->offset < sz ? 0 : mt->offset - sz;
927: p->offset = mt->offset;
1.116 schwarze 928:
929: if (--mt->lmarginsz < MAXMARGINS)
930: mt->lmargincur = mt->lmarginsz;
1.137 schwarze 931: }
932:
933: static int
934: pre_UR(DECL_ARGS)
935: {
936:
937: return (MAN_HEAD != n->type);
938: }
939:
940: static void
941: post_UR(DECL_ARGS)
942: {
943:
944: if (MAN_BLOCK != n->type)
945: return;
946:
947: term_word(p, "<");
948: p->flags |= TERMP_NOSPACE;
949:
950: if (NULL != n->child->child)
951: print_man_node(p, mt, n->child->child, meta);
952:
953: p->flags |= TERMP_NOSPACE;
954: term_word(p, ">");
1.26 kristaps 955: }
956:
1.1 kristaps 957: static void
1.45 kristaps 958: print_man_node(DECL_ARGS)
1.1 kristaps 959: {
1.65 joerg 960: size_t rm, rmax;
1.54 kristaps 961: int c;
1.1 kristaps 962:
963: switch (n->type) {
1.146 schwarze 964: case MAN_TEXT:
1.97 kristaps 965: /*
966: * If we have a blank line, output a vertical space.
967: * If we have a space as the first character, break
968: * before printing the line's data.
969: */
1.96 kristaps 970: if ('\0' == *n->string) {
1.4 kristaps 971: term_vspace(p);
1.98 schwarze 972: return;
1.97 kristaps 973: } else if (' ' == *n->string && MAN_LINE & n->flags)
1.96 kristaps 974: term_newln(p);
1.54 kristaps 975:
1.4 kristaps 976: term_word(p, n->string);
1.130 schwarze 977: goto out;
1.52 kristaps 978:
1.146 schwarze 979: case MAN_EQN:
1.115 kristaps 980: term_eqn(p, n->eqn);
1.97 kristaps 981: return;
1.146 schwarze 982: case MAN_TBL:
1.97 kristaps 983: /*
984: * Tables are preceded by a newline. Then process a
985: * table line, which will cause line termination,
986: */
1.146 schwarze 987: if (TBL_SPAN_FIRST & n->span->flags)
1.93 kristaps 988: term_newln(p);
1.92 kristaps 989: term_tbl(p, n->span);
1.97 kristaps 990: return;
1.1 kristaps 991: default:
992: break;
993: }
994:
1.97 kristaps 995: if ( ! (MAN_NOTEXT & termacts[n->tok].flags))
996: term_fontrepl(p, TERMFONT_NONE);
997:
998: c = 1;
999: if (termacts[n->tok].pre)
1.135 schwarze 1000: c = (*termacts[n->tok].pre)(p, mt, n, meta);
1.97 kristaps 1001:
1.1 kristaps 1002: if (c && n->child)
1.135 schwarze 1003: print_man_nodelist(p, mt, n->child, meta);
1.1 kristaps 1004:
1.97 kristaps 1005: if (termacts[n->tok].post)
1.135 schwarze 1006: (*termacts[n->tok].post)(p, mt, n, meta);
1.97 kristaps 1007: if ( ! (MAN_NOTEXT & termacts[n->tok].flags))
1008: term_fontrepl(p, TERMFONT_NONE);
1.63 kristaps 1009:
1.130 schwarze 1010: out:
1011: /*
1012: * If we're in a literal context, make sure that words
1013: * together on the same line stay together. This is a
1014: * POST-printing call, so we check the NEXT word. Since
1015: * -man doesn't have nested macros, we don't need to be
1016: * more specific than this.
1017: */
1018: if (MANT_LITERAL & mt->fl && ! (TERMP_NOBREAK & p->flags) &&
1.142 schwarze 1019: (NULL == n->next || MAN_LINE & n->next->flags)) {
1.130 schwarze 1020: rm = p->rmargin;
1021: rmax = p->maxrmargin;
1022: p->rmargin = p->maxrmargin = TERM_MAXMARGIN;
1023: p->flags |= TERMP_NOSPACE;
1024: if (NULL != n->string && '\0' != *n->string)
1025: term_flushln(p);
1026: else
1027: term_newln(p);
1028: if (rm < rmax && n->parent->tok == MAN_HP) {
1029: p->offset = rm;
1030: p->rmargin = rmax;
1031: } else
1032: p->rmargin = rm;
1033: p->maxrmargin = rmax;
1034: }
1.63 kristaps 1035: if (MAN_EOS & n->flags)
1036: p->flags |= TERMP_SENTENCE;
1.1 kristaps 1037: }
1038:
1039:
1040: static void
1.52 kristaps 1041: print_man_nodelist(DECL_ARGS)
1.1 kristaps 1042: {
1.19 kristaps 1043:
1.135 schwarze 1044: print_man_node(p, mt, n, meta);
1.1 kristaps 1045: if ( ! n->next)
1046: return;
1.135 schwarze 1047: print_man_nodelist(p, mt, n->next, meta);
1.1 kristaps 1048: }
1049:
1.31 kristaps 1050: static void
1.73 kristaps 1051: print_man_foot(struct termp *p, const void *arg)
1.1 kristaps 1052: {
1.147 schwarze 1053: const struct man_meta *meta;
1054: char *title;
1055: size_t datelen;
1.73 kristaps 1056:
1057: meta = (const struct man_meta *)arg;
1.125 schwarze 1058: assert(meta->title);
1059: assert(meta->msec);
1060: assert(meta->date);
1.1 kristaps 1061:
1.52 kristaps 1062: term_fontrepl(p, TERMFONT_NONE);
1.50 kristaps 1063:
1.69 joerg 1064: term_vspace(p);
1.126 schwarze 1065:
1066: /*
1067: * Temporary, undocumented option to imitate mdoc(7) output.
1068: * In the bottom right corner, use the source instead of
1069: * the title.
1070: */
1071:
1072: if ( ! p->mdocstyle) {
1073: term_vspace(p);
1074: term_vspace(p);
1.147 schwarze 1075: mandoc_asprintf(&title, "%s(%s)",
1076: meta->title, meta->msec);
1.126 schwarze 1077: } else if (meta->source) {
1.147 schwarze 1078: title = mandoc_strdup(meta->source);
1.126 schwarze 1079: } else {
1.147 schwarze 1080: title = mandoc_strdup("");
1.126 schwarze 1081: }
1.125 schwarze 1082: datelen = term_strlen(p, meta->date);
1.1 kristaps 1083:
1.126 schwarze 1084: /* Bottom left corner: manual source. */
1085:
1.1 kristaps 1086: p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
1.139 schwarze 1087: p->trailspace = 1;
1.1 kristaps 1088: p->offset = 0;
1.123 schwarze 1089: p->rmargin = (p->maxrmargin - datelen + term_len(p, 1)) / 2;
1.1 kristaps 1090:
1091: if (meta->source)
1092: term_word(p, meta->source);
1093: term_flushln(p);
1094:
1.126 schwarze 1095: /* At the bottom in the middle: manual date. */
1096:
1.117 schwarze 1097: p->flags |= TERMP_NOSPACE;
1.1 kristaps 1098: p->offset = p->rmargin;
1.123 schwarze 1099: p->rmargin = p->maxrmargin - term_strlen(p, title);
1100: if (p->offset + datelen >= p->rmargin)
1101: p->rmargin = p->offset + datelen;
1102:
1.125 schwarze 1103: term_word(p, meta->date);
1.123 schwarze 1104: term_flushln(p);
1105:
1.126 schwarze 1106: /* Bottom right corner: manual title and section. */
1107:
1.123 schwarze 1108: p->flags &= ~TERMP_NOBREAK;
1109: p->flags |= TERMP_NOSPACE;
1.139 schwarze 1110: p->trailspace = 0;
1.123 schwarze 1111: p->offset = p->rmargin;
1.1 kristaps 1112: p->rmargin = p->maxrmargin;
1113:
1.123 schwarze 1114: term_word(p, title);
1.1 kristaps 1115: term_flushln(p);
1.147 schwarze 1116: free(title);
1.1 kristaps 1117: }
1118:
1.31 kristaps 1119: static void
1.73 kristaps 1120: print_man_head(struct termp *p, const void *arg)
1.1 kristaps 1121: {
1.147 schwarze 1122: const struct man_meta *meta;
1.148 ! schwarze 1123: const char *volume;
1.147 schwarze 1124: char *title;
1.148 ! schwarze 1125: size_t vollen, titlen;
1.73 kristaps 1126:
1.135 schwarze 1127: meta = (const struct man_meta *)arg;
1128: assert(meta->title);
1129: assert(meta->msec);
1.1 kristaps 1130:
1.148 ! schwarze 1131: volume = NULL == meta->vol ? "" : meta->vol;
! 1132: vollen = term_strlen(p, volume);
1.1 kristaps 1133:
1.126 schwarze 1134: /* Top left corner: manual title and section. */
1135:
1.147 schwarze 1136: mandoc_asprintf(&title, "%s(%s)", meta->title, meta->msec);
1.77 kristaps 1137: titlen = term_strlen(p, title);
1.1 kristaps 1138:
1.118 schwarze 1139: p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
1.139 schwarze 1140: p->trailspace = 1;
1.1 kristaps 1141: p->offset = 0;
1.148 ! schwarze 1142: p->rmargin = 2 * (titlen+1) + vollen < p->maxrmargin ?
! 1143: (p->maxrmargin - vollen + term_len(p, 1)) / 2 :
! 1144: p->maxrmargin - vollen;
1.1 kristaps 1145:
1146: term_word(p, title);
1147: term_flushln(p);
1148:
1.126 schwarze 1149: /* At the top in the middle: manual volume. */
1150:
1.117 schwarze 1151: p->flags |= TERMP_NOSPACE;
1.1 kristaps 1152: p->offset = p->rmargin;
1.148 ! schwarze 1153: p->rmargin = p->offset + vollen + titlen < p->maxrmargin ?
1.57 kristaps 1154: p->maxrmargin - titlen : p->maxrmargin;
1.1 kristaps 1155:
1.148 ! schwarze 1156: term_word(p, volume);
1.1 kristaps 1157: term_flushln(p);
1158:
1.126 schwarze 1159: /* Top right corner: title and section, again. */
1160:
1.1 kristaps 1161: p->flags &= ~TERMP_NOBREAK;
1.139 schwarze 1162: p->trailspace = 0;
1.57 kristaps 1163: if (p->rmargin + titlen <= p->maxrmargin) {
1.117 schwarze 1164: p->flags |= TERMP_NOSPACE;
1.57 kristaps 1165: p->offset = p->rmargin;
1166: p->rmargin = p->maxrmargin;
1167: term_word(p, title);
1168: term_flushln(p);
1169: }
1.1 kristaps 1170:
1.118 schwarze 1171: p->flags &= ~TERMP_NOSPACE;
1172: p->offset = 0;
1.1 kristaps 1173: p->rmargin = p->maxrmargin;
1.61 kristaps 1174:
1.146 schwarze 1175: /*
1.126 schwarze 1176: * Groff prints three blank lines before the content.
1177: * Do the same, except in the temporary, undocumented
1178: * mode imitating mdoc(7) output.
1.62 kristaps 1179: */
1180:
1.61 kristaps 1181: term_vspace(p);
1.126 schwarze 1182: if ( ! p->mdocstyle) {
1183: term_vspace(p);
1184: term_vspace(p);
1185: }
1.147 schwarze 1186: free(title);
1.1 kristaps 1187: }
CVSweb