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