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