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