Annotation of src/usr.bin/mandoc/man_term.c, Revision 1.111
1.111 ! schwarze 1: /* $OpenBSD: man_term.c,v 1.110 2014/12/02 10:07:17 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 */
1.111 ! schwarze 124: { pre_ign, NULL, MAN_NOTEXT }, /* 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.18 schwarze 151:
1.104 schwarze 152: n = man_node(man)->child;
1.89 schwarze 153: meta = man_meta(man);
1.1 kristaps 154:
1.70 schwarze 155: memset(&mt, 0, sizeof(struct mtermp));
156:
1.77 schwarze 157: mt.lmargin[mt.lmargincur] = term_len(p, p->defindent);
158: mt.offset = term_len(p, p->defindent);
1.88 schwarze 159: mt.pardist = 1;
1.11 schwarze 160:
1.104 schwarze 161: if (p->synopsisonly) {
162: while (n != NULL) {
163: if (n->tok == MAN_SH &&
164: n->child->child->type == MAN_TEXT &&
165: !strcmp(n->child->child->string, "SYNOPSIS")) {
166: if (n->child->next->child != NULL)
167: print_man_nodelist(p, &mt,
168: n->child->next->child, meta);
169: term_newln(p);
170: break;
171: }
172: n = n->next;
173: }
174: } else {
175: if (p->defindent == 0)
176: p->defindent = 7;
177: term_begin(p, print_man_head, print_man_foot, meta);
178: p->flags |= TERMP_NOSPACE;
179: if (n != NULL)
180: print_man_nodelist(p, &mt, n, meta);
181: term_end(p);
182: }
1.1 kristaps 183: }
184:
185:
1.42 schwarze 186: static size_t
187: a2height(const struct termp *p, const char *cp)
1.11 schwarze 188: {
1.18 schwarze 189: struct roffsu su;
1.11 schwarze 190:
1.42 schwarze 191: if ( ! a2roffsu(cp, &su, SCALE_VS))
1.69 schwarze 192: SCALE_VS_INIT(&su, atoi(cp));
1.11 schwarze 193:
1.42 schwarze 194: return(term_vspan(p, &su));
1.11 schwarze 195: }
196:
197: static int
1.42 schwarze 198: a2width(const struct termp *p, const char *cp)
1.11 schwarze 199: {
1.18 schwarze 200: struct roffsu su;
1.11 schwarze 201:
1.42 schwarze 202: if ( ! a2roffsu(cp, &su, SCALE_BU))
1.18 schwarze 203: return(-1);
204:
1.42 schwarze 205: return((int)term_hspan(p, &su));
1.18 schwarze 206: }
1.11 schwarze 207:
1.69 schwarze 208: /*
209: * Printing leading vertical space before a block.
210: * This is used for the paragraph macros.
211: * The rules are pretty simple, since there's very little nesting going
212: * on here. Basically, if we're the first within another block (SS/SH),
213: * then don't emit vertical space. If we are (RS), then do. If not the
214: * first, print it.
215: */
1.18 schwarze 216: static void
1.88 schwarze 217: print_bvspace(struct termp *p, const struct man_node *n, int pardist)
1.18 schwarze 218: {
1.88 schwarze 219: int i;
1.69 schwarze 220:
1.18 schwarze 221: term_newln(p);
1.64 schwarze 222:
1.69 schwarze 223: if (n->body && n->body->child)
224: if (MAN_TBL == n->body->child->type)
225: return;
1.11 schwarze 226:
1.69 schwarze 227: if (MAN_ROOT == n->parent->type || MAN_RS != n->parent->tok)
228: if (NULL == n->prev)
229: return;
1.11 schwarze 230:
1.88 schwarze 231: for (i = 0; i < pardist; i++)
232: term_vspace(p);
1.14 schwarze 233: }
234:
1.100 schwarze 235:
1.14 schwarze 236: static int
237: pre_ign(DECL_ARGS)
238: {
239:
1.97 schwarze 240: return(0);
241: }
242:
243: static int
244: pre_ll(DECL_ARGS)
245: {
246:
1.98 schwarze 247: term_setwidth(p, n->nchild ? n->child->string : NULL);
1.14 schwarze 248: return(0);
1.11 schwarze 249: }
250:
1.1 kristaps 251: static int
252: pre_I(DECL_ARGS)
253: {
254:
1.21 schwarze 255: term_fontrepl(p, TERMFONT_UNDER);
1.1 kristaps 256: return(1);
257: }
258:
1.11 schwarze 259: static int
1.45 schwarze 260: pre_literal(DECL_ARGS)
1.11 schwarze 261: {
262:
1.45 schwarze 263: term_newln(p);
1.53 schwarze 264:
1.83 schwarze 265: if (MAN_nf == n->tok || MAN_EX == n->tok)
1.45 schwarze 266: mt->fl |= MANT_LITERAL;
1.53 schwarze 267: else
1.45 schwarze 268: mt->fl &= ~MANT_LITERAL;
269:
1.72 schwarze 270: /*
271: * Unlike .IP and .TP, .HP does not have a HEAD.
272: * So in case a second call to term_flushln() is needed,
273: * indentation has to be set up explicitly.
274: */
275: if (MAN_HP == n->parent->tok && p->rmargin < p->maxrmargin) {
1.76 schwarze 276: p->offset = p->rmargin;
1.72 schwarze 277: p->rmargin = p->maxrmargin;
1.93 schwarze 278: p->trailspace = 0;
1.99 schwarze 279: p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
1.72 schwarze 280: p->flags |= TERMP_NOSPACE;
281: }
282:
1.62 schwarze 283: return(0);
1.11 schwarze 284: }
285:
286: static int
1.88 schwarze 287: pre_PD(DECL_ARGS)
288: {
289:
290: n = n->child;
291: if (0 == n) {
292: mt->pardist = 1;
293: return(0);
294: }
295: assert(MAN_TEXT == n->type);
296: mt->pardist = atoi(n->string);
297: return(0);
298: }
299:
300: static int
1.51 schwarze 301: pre_alternate(DECL_ARGS)
1.1 kristaps 302: {
1.51 schwarze 303: enum termfont font[2];
304: const struct man_node *nn;
305: int savelit, i;
1.1 kristaps 306:
1.51 schwarze 307: switch (n->tok) {
1.100 schwarze 308: case MAN_RB:
1.51 schwarze 309: font[0] = TERMFONT_NONE;
310: font[1] = TERMFONT_BOLD;
311: break;
1.100 schwarze 312: case MAN_RI:
1.51 schwarze 313: font[0] = TERMFONT_NONE;
314: font[1] = TERMFONT_UNDER;
315: break;
1.100 schwarze 316: case MAN_BR:
1.51 schwarze 317: font[0] = TERMFONT_BOLD;
318: font[1] = TERMFONT_NONE;
319: break;
1.100 schwarze 320: case MAN_BI:
1.51 schwarze 321: font[0] = TERMFONT_BOLD;
322: font[1] = TERMFONT_UNDER;
323: break;
1.100 schwarze 324: case MAN_IR:
1.51 schwarze 325: font[0] = TERMFONT_UNDER;
326: font[1] = TERMFONT_NONE;
327: break;
1.100 schwarze 328: case MAN_IB:
1.51 schwarze 329: font[0] = TERMFONT_UNDER;
330: font[1] = TERMFONT_BOLD;
331: break;
332: default:
333: abort();
334: }
1.17 schwarze 335:
1.51 schwarze 336: savelit = MANT_LITERAL & mt->fl;
337: mt->fl &= ~MANT_LITERAL;
1.17 schwarze 338:
1.51 schwarze 339: for (i = 0, nn = n->child; nn; nn = nn->next, i = 1 - i) {
340: term_fontrepl(p, font[i]);
341: if (savelit && NULL == nn->next)
342: mt->fl |= MANT_LITERAL;
1.89 schwarze 343: print_man_node(p, mt, nn, meta);
1.51 schwarze 344: if (nn->next)
1.1 kristaps 345: p->flags |= TERMP_NOSPACE;
346: }
1.17 schwarze 347:
1.1 kristaps 348: return(0);
349: }
350:
351: static int
352: pre_B(DECL_ARGS)
353: {
354:
1.21 schwarze 355: term_fontrepl(p, TERMFONT_BOLD);
1.1 kristaps 356: return(1);
1.82 schwarze 357: }
358:
359: static int
360: pre_OP(DECL_ARGS)
361: {
362:
363: term_word(p, "[");
364: p->flags |= TERMP_NOSPACE;
365:
366: if (NULL != (n = n->child)) {
367: term_fontrepl(p, TERMFONT_BOLD);
368: term_word(p, n->string);
369: }
370: if (NULL != n && NULL != n->next) {
371: term_fontrepl(p, TERMFONT_UNDER);
372: term_word(p, n->next->string);
373: }
374:
375: term_fontrepl(p, TERMFONT_NONE);
376: p->flags |= TERMP_NOSPACE;
377: term_word(p, "]");
378: return(0);
1.52 schwarze 379: }
380:
381: static int
382: pre_ft(DECL_ARGS)
383: {
384: const char *cp;
385:
386: if (NULL == n->child) {
387: term_fontlast(p);
388: return(0);
389: }
390:
391: cp = n->child->string;
392: switch (*cp) {
1.100 schwarze 393: case '4':
1.52 schwarze 394: /* FALLTHROUGH */
1.100 schwarze 395: case '3':
1.52 schwarze 396: /* FALLTHROUGH */
1.100 schwarze 397: case 'B':
1.52 schwarze 398: term_fontrepl(p, TERMFONT_BOLD);
399: break;
1.100 schwarze 400: case '2':
1.52 schwarze 401: /* FALLTHROUGH */
1.100 schwarze 402: case 'I':
1.52 schwarze 403: term_fontrepl(p, TERMFONT_UNDER);
404: break;
1.100 schwarze 405: case 'P':
1.52 schwarze 406: term_fontlast(p);
407: break;
1.100 schwarze 408: case '1':
1.52 schwarze 409: /* FALLTHROUGH */
1.100 schwarze 410: case 'C':
1.52 schwarze 411: /* FALLTHROUGH */
1.100 schwarze 412: case 'R':
1.52 schwarze 413: term_fontrepl(p, TERMFONT_NONE);
414: break;
415: default:
416: break;
417: }
418: return(0);
1.1 kristaps 419: }
420:
421: static int
1.45 schwarze 422: pre_in(DECL_ARGS)
1.11 schwarze 423: {
1.45 schwarze 424: int len, less;
425: size_t v;
426: const char *cp;
427:
428: term_newln(p);
429:
430: if (NULL == n->child) {
431: p->offset = mt->offset;
432: return(0);
433: }
1.11 schwarze 434:
1.45 schwarze 435: cp = n->child->string;
436: less = 0;
1.11 schwarze 437:
1.45 schwarze 438: if ('-' == *cp)
439: less = -1;
440: else if ('+' == *cp)
441: less = 1;
442: else
443: cp--;
444:
445: if ((len = a2width(p, ++cp)) < 0)
446: return(0);
447:
448: v = (size_t)len;
449:
450: if (less < 0)
451: p->offset -= p->offset > v ? v : p->offset;
452: else if (less > 0)
453: p->offset += v;
1.100 schwarze 454: else
1.45 schwarze 455: p->offset = v;
1.59 schwarze 456:
1.11 schwarze 457: return(0);
458: }
459:
460: static int
1.45 schwarze 461: pre_sp(DECL_ARGS)
1.8 schwarze 462: {
1.85 schwarze 463: char *s;
1.45 schwarze 464: size_t i, len;
1.85 schwarze 465: int neg;
1.45 schwarze 466:
1.69 schwarze 467: if ((NULL == n->prev && n->parent)) {
1.86 schwarze 468: switch (n->parent->tok) {
1.100 schwarze 469: case MAN_SH:
1.86 schwarze 470: /* FALLTHROUGH */
1.100 schwarze 471: case MAN_SS:
1.86 schwarze 472: /* FALLTHROUGH */
1.100 schwarze 473: case MAN_PP:
1.86 schwarze 474: /* FALLTHROUGH */
1.100 schwarze 475: case MAN_LP:
1.86 schwarze 476: /* FALLTHROUGH */
1.100 schwarze 477: case MAN_P:
1.86 schwarze 478: /* FALLTHROUGH */
1.69 schwarze 479: return(0);
1.86 schwarze 480: default:
481: break;
482: }
1.69 schwarze 483: }
484:
1.85 schwarze 485: neg = 0;
1.45 schwarze 486: switch (n->tok) {
1.100 schwarze 487: case MAN_br:
1.45 schwarze 488: len = 0;
489: break;
490: default:
1.85 schwarze 491: if (NULL == n->child) {
492: len = 1;
493: break;
494: }
495: s = n->child->string;
496: if ('-' == *s) {
497: neg = 1;
498: s++;
499: }
500: len = a2height(p, s);
1.45 schwarze 501: break;
502: }
503:
504: if (0 == len)
505: term_newln(p);
1.85 schwarze 506: else if (neg)
507: p->skipvsp += len;
508: else
509: for (i = 0; i < len; i++)
510: term_vspace(p);
1.8 schwarze 511:
512: return(0);
513: }
514:
515: static int
1.11 schwarze 516: pre_HP(DECL_ARGS)
517: {
1.72 schwarze 518: size_t len, one;
1.11 schwarze 519: int ival;
520: const struct man_node *nn;
521:
522: switch (n->type) {
1.100 schwarze 523: case MAN_BLOCK:
1.88 schwarze 524: print_bvspace(p, n, mt->pardist);
1.11 schwarze 525: return(1);
1.100 schwarze 526: case MAN_BODY:
1.11 schwarze 527: break;
528: default:
529: return(0);
530: }
531:
1.84 schwarze 532: if ( ! (MANT_LITERAL & mt->fl)) {
1.99 schwarze 533: p->flags |= TERMP_NOBREAK | TERMP_BRIND;
1.93 schwarze 534: p->trailspace = 2;
1.84 schwarze 535: }
536:
1.70 schwarze 537: len = mt->lmargin[mt->lmargincur];
1.11 schwarze 538: ival = -1;
539:
540: /* Calculate offset. */
541:
542: if (NULL != (nn = n->parent->head->child))
1.42 schwarze 543: if ((ival = a2width(p, nn->string)) >= 0)
1.11 schwarze 544: len = (size_t)ival;
545:
1.72 schwarze 546: one = term_len(p, 1);
1.76 schwarze 547: if (len < one)
1.72 schwarze 548: len = one;
1.11 schwarze 549:
1.13 schwarze 550: p->offset = mt->offset;
551: p->rmargin = mt->offset + len;
1.11 schwarze 552:
553: if (ival >= 0)
1.70 schwarze 554: mt->lmargin[mt->lmargincur] = (size_t)ival;
1.11 schwarze 555:
556: return(1);
557: }
558:
559: static void
560: post_HP(DECL_ARGS)
561: {
562:
563: switch (n->type) {
1.100 schwarze 564: case MAN_BODY:
1.90 schwarze 565: term_newln(p);
1.99 schwarze 566: p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
1.93 schwarze 567: p->trailspace = 0;
1.13 schwarze 568: p->offset = mt->offset;
1.11 schwarze 569: p->rmargin = p->maxrmargin;
570: break;
571: default:
572: break;
573: }
574: }
575:
576: static int
1.1 kristaps 577: pre_PP(DECL_ARGS)
578: {
579:
1.11 schwarze 580: switch (n->type) {
1.100 schwarze 581: case MAN_BLOCK:
1.77 schwarze 582: mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
1.88 schwarze 583: print_bvspace(p, n, mt->pardist);
1.11 schwarze 584: break;
585: default:
1.13 schwarze 586: p->offset = mt->offset;
1.11 schwarze 587: break;
588: }
589:
1.54 schwarze 590: return(MAN_HEAD != n->type);
1.1 kristaps 591: }
592:
593: static int
594: pre_IP(DECL_ARGS)
595: {
1.11 schwarze 596: const struct man_node *nn;
597: size_t len;
1.57 schwarze 598: int savelit, ival;
1.11 schwarze 599:
600: switch (n->type) {
1.100 schwarze 601: case MAN_BODY:
1.11 schwarze 602: p->flags |= TERMP_NOSPACE;
603: break;
1.100 schwarze 604: case MAN_HEAD:
1.11 schwarze 605: p->flags |= TERMP_NOBREAK;
1.93 schwarze 606: p->trailspace = 1;
1.11 schwarze 607: break;
1.100 schwarze 608: case MAN_BLOCK:
1.88 schwarze 609: print_bvspace(p, n, mt->pardist);
1.11 schwarze 610: /* FALLTHROUGH */
611: default:
612: return(1);
613: }
614:
1.70 schwarze 615: len = mt->lmargin[mt->lmargincur];
1.11 schwarze 616: ival = -1;
617:
1.57 schwarze 618: /* Calculate the offset from the optional second argument. */
1.11 schwarze 619: if (NULL != (nn = n->parent->head->child))
1.57 schwarze 620: if (NULL != (nn = nn->next))
1.42 schwarze 621: if ((ival = a2width(p, nn->string)) >= 0)
1.11 schwarze 622: len = (size_t)ival;
623:
624: switch (n->type) {
1.100 schwarze 625: case MAN_HEAD:
1.11 schwarze 626: /* Handle zero-width lengths. */
627: if (0 == len)
1.42 schwarze 628: len = term_len(p, 1);
1.11 schwarze 629:
1.13 schwarze 630: p->offset = mt->offset;
631: p->rmargin = mt->offset + len;
1.11 schwarze 632: if (ival < 0)
633: break;
634:
635: /* Set the saved left-margin. */
1.70 schwarze 636: mt->lmargin[mt->lmargincur] = (size_t)ival;
1.1 kristaps 637:
1.57 schwarze 638: savelit = MANT_LITERAL & mt->fl;
639: mt->fl &= ~MANT_LITERAL;
640:
641: if (n->child)
1.89 schwarze 642: print_man_node(p, mt, n->child, meta);
1.57 schwarze 643:
644: if (savelit)
645: mt->fl |= MANT_LITERAL;
646:
1.11 schwarze 647: return(0);
1.100 schwarze 648: case MAN_BODY:
1.13 schwarze 649: p->offset = mt->offset + len;
1.109 schwarze 650: p->rmargin = p->maxrmargin;
1.11 schwarze 651: break;
652: default:
653: break;
654: }
1.1 kristaps 655:
1.11 schwarze 656: return(1);
657: }
1.1 kristaps 658:
1.11 schwarze 659: static void
660: post_IP(DECL_ARGS)
661: {
1.4 schwarze 662:
1.11 schwarze 663: switch (n->type) {
1.100 schwarze 664: case MAN_HEAD:
1.11 schwarze 665: term_flushln(p);
666: p->flags &= ~TERMP_NOBREAK;
1.93 schwarze 667: p->trailspace = 0;
1.11 schwarze 668: p->rmargin = p->maxrmargin;
669: break;
1.100 schwarze 670: case MAN_BODY:
1.57 schwarze 671: term_newln(p);
1.92 schwarze 672: p->offset = mt->offset;
1.11 schwarze 673: break;
674: default:
675: break;
676: }
1.1 kristaps 677: }
678:
679: static int
680: pre_TP(DECL_ARGS)
681: {
1.11 schwarze 682: const struct man_node *nn;
683: size_t len;
1.57 schwarze 684: int savelit, ival;
1.11 schwarze 685:
686: switch (n->type) {
1.100 schwarze 687: case MAN_HEAD:
1.11 schwarze 688: p->flags |= TERMP_NOBREAK;
1.93 schwarze 689: p->trailspace = 1;
1.11 schwarze 690: break;
1.100 schwarze 691: case MAN_BODY:
1.11 schwarze 692: p->flags |= TERMP_NOSPACE;
693: break;
1.100 schwarze 694: case MAN_BLOCK:
1.88 schwarze 695: print_bvspace(p, n, mt->pardist);
1.11 schwarze 696: /* FALLTHROUGH */
697: default:
698: return(1);
699: }
700:
1.70 schwarze 701: len = (size_t)mt->lmargin[mt->lmargincur];
1.11 schwarze 702: ival = -1;
703:
704: /* Calculate offset. */
1.1 kristaps 705:
1.70 schwarze 706: if (NULL != (nn = n->parent->head->child))
1.95 schwarze 707: if (nn->string && 0 == (MAN_LINE & nn->flags))
1.42 schwarze 708: if ((ival = a2width(p, nn->string)) >= 0)
1.11 schwarze 709: len = (size_t)ival;
1.8 schwarze 710:
1.11 schwarze 711: switch (n->type) {
1.100 schwarze 712: case MAN_HEAD:
1.11 schwarze 713: /* Handle zero-length properly. */
714: if (0 == len)
1.42 schwarze 715: len = term_len(p, 1);
1.11 schwarze 716:
1.13 schwarze 717: p->offset = mt->offset;
718: p->rmargin = mt->offset + len;
1.11 schwarze 719:
1.57 schwarze 720: savelit = MANT_LITERAL & mt->fl;
721: mt->fl &= ~MANT_LITERAL;
722:
1.11 schwarze 723: /* Don't print same-line elements. */
1.95 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.11 schwarze 732:
1.57 schwarze 733: if (savelit)
734: mt->fl |= MANT_LITERAL;
1.11 schwarze 735: if (ival >= 0)
1.70 schwarze 736: mt->lmargin[mt->lmargincur] = (size_t)ival;
1.11 schwarze 737:
738: return(0);
1.100 schwarze 739: case MAN_BODY:
1.13 schwarze 740: p->offset = mt->offset + len;
1.109 schwarze 741: p->rmargin = p->maxrmargin;
1.93 schwarze 742: p->trailspace = 0;
1.84 schwarze 743: p->flags &= ~TERMP_NOBREAK;
1.11 schwarze 744: break;
745: default:
746: break;
747: }
1.1 kristaps 748:
1.11 schwarze 749: return(1);
750: }
1.1 kristaps 751:
1.11 schwarze 752: static void
753: post_TP(DECL_ARGS)
754: {
1.1 kristaps 755:
1.11 schwarze 756: switch (n->type) {
1.100 schwarze 757: case MAN_HEAD:
1.11 schwarze 758: term_flushln(p);
759: break;
1.100 schwarze 760: case MAN_BODY:
1.57 schwarze 761: term_newln(p);
1.92 schwarze 762: p->offset = mt->offset;
1.11 schwarze 763: break;
764: default:
765: break;
766: }
1.1 kristaps 767: }
768:
769: static int
770: pre_SS(DECL_ARGS)
771: {
1.88 schwarze 772: int i;
1.1 kristaps 773:
1.11 schwarze 774: switch (n->type) {
1.100 schwarze 775: case MAN_BLOCK:
1.69 schwarze 776: mt->fl &= ~MANT_LITERAL;
1.77 schwarze 777: mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
778: mt->offset = term_len(p, p->defindent);
1.111 ! schwarze 779:
! 780: /*
! 781: * No vertical space before the first subsection
! 782: * and after an empty subsection.
! 783: */
! 784:
! 785: do {
! 786: n = n->prev;
! 787: } while (n != NULL && termacts[n->tok].flags & MAN_NOTEXT);
! 788: if (n == NULL || (n->tok == MAN_SS && n->body->child == NULL))
1.11 schwarze 789: break;
1.111 ! schwarze 790:
1.88 schwarze 791: for (i = 0; i < mt->pardist; i++)
792: term_vspace(p);
1.11 schwarze 793: break;
1.100 schwarze 794: case MAN_HEAD:
1.21 schwarze 795: term_fontrepl(p, TERMFONT_BOLD);
1.87 schwarze 796: p->offset = term_len(p, 3);
1.11 schwarze 797: break;
1.100 schwarze 798: case MAN_BODY:
1.13 schwarze 799: p->offset = mt->offset;
1.11 schwarze 800: break;
801: default:
802: break;
803: }
804:
1.1 kristaps 805: return(1);
806: }
807:
808: static void
809: post_SS(DECL_ARGS)
810: {
1.100 schwarze 811:
1.11 schwarze 812: switch (n->type) {
1.100 schwarze 813: case MAN_HEAD:
1.11 schwarze 814: term_newln(p);
815: break;
1.100 schwarze 816: case MAN_BODY:
1.11 schwarze 817: term_newln(p);
818: break;
819: default:
820: break;
821: }
1.1 kristaps 822: }
823:
824: static int
825: pre_SH(DECL_ARGS)
826: {
1.88 schwarze 827: int i;
1.1 kristaps 828:
1.11 schwarze 829: switch (n->type) {
1.100 schwarze 830: case MAN_BLOCK:
1.69 schwarze 831: mt->fl &= ~MANT_LITERAL;
1.77 schwarze 832: mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
833: mt->offset = term_len(p, p->defindent);
1.111 ! schwarze 834:
! 835: /*
! 836: * No vertical space before the first section
! 837: * and after an empty section.
! 838: */
! 839:
! 840: do {
! 841: n = n->prev;
! 842: } while (n != NULL && termacts[n->tok].flags & MAN_NOTEXT);
! 843: if (n == NULL || (n->tok == MAN_SH && n->body->child == NULL))
1.29 schwarze 844: break;
1.111 ! schwarze 845:
1.88 schwarze 846: for (i = 0; i < mt->pardist; i++)
847: term_vspace(p);
1.11 schwarze 848: break;
1.100 schwarze 849: case MAN_HEAD:
1.21 schwarze 850: term_fontrepl(p, TERMFONT_BOLD);
1.11 schwarze 851: p->offset = 0;
852: break;
1.100 schwarze 853: case MAN_BODY:
1.13 schwarze 854: p->offset = mt->offset;
1.11 schwarze 855: break;
856: default:
857: break;
858: }
859:
1.1 kristaps 860: return(1);
861: }
862:
863: static void
864: post_SH(DECL_ARGS)
865: {
1.100 schwarze 866:
1.11 schwarze 867: switch (n->type) {
1.100 schwarze 868: case MAN_HEAD:
1.11 schwarze 869: term_newln(p);
870: break;
1.100 schwarze 871: case MAN_BODY:
1.11 schwarze 872: term_newln(p);
873: break;
874: default:
1.13 schwarze 875: break;
876: }
877: }
878:
879: static int
880: pre_RS(DECL_ARGS)
881: {
1.69 schwarze 882: int ival;
883: size_t sz;
1.13 schwarze 884:
885: switch (n->type) {
1.100 schwarze 886: case MAN_BLOCK:
1.13 schwarze 887: term_newln(p);
888: return(1);
1.100 schwarze 889: case MAN_HEAD:
1.13 schwarze 890: return(0);
891: default:
892: break;
893: }
894:
1.77 schwarze 895: sz = term_len(p, p->defindent);
1.13 schwarze 896:
1.69 schwarze 897: if (NULL != (n = n->parent->head->child))
1.100 schwarze 898: if ((ival = a2width(p, n->string)) >= 0)
1.69 schwarze 899: sz = (size_t)ival;
1.13 schwarze 900:
1.69 schwarze 901: mt->offset += sz;
1.94 schwarze 902: p->offset = mt->offset;
1.109 schwarze 903: p->rmargin = p->maxrmargin;
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: */
1.110 schwarze 1030: if (mt->fl & MANT_LITERAL &&
1031: ! (p->flags & (TERMP_NOBREAK | TERMP_NONEWLINE)) &&
1032: (n->next == NULL || n->next->flags & MAN_LINE)) {
1.84 schwarze 1033: rm = p->rmargin;
1034: rmax = p->maxrmargin;
1035: p->rmargin = p->maxrmargin = TERM_MAXMARGIN;
1036: p->flags |= TERMP_NOSPACE;
1.110 schwarze 1037: if (n->string != NULL && *n->string != '\0')
1.84 schwarze 1038: term_flushln(p);
1039: else
1040: term_newln(p);
1041: if (rm < rmax && n->parent->tok == MAN_HP) {
1042: p->offset = rm;
1043: p->rmargin = rmax;
1044: } else
1045: p->rmargin = rm;
1046: p->maxrmargin = rmax;
1047: }
1.30 schwarze 1048: if (MAN_EOS & n->flags)
1049: p->flags |= TERMP_SENTENCE;
1.1 kristaps 1050: }
1051:
1052:
1053: static void
1.21 schwarze 1054: print_man_nodelist(DECL_ARGS)
1.1 kristaps 1055: {
1.11 schwarze 1056:
1.89 schwarze 1057: print_man_node(p, mt, n, meta);
1.1 kristaps 1058: if ( ! n->next)
1059: return;
1.89 schwarze 1060: print_man_nodelist(p, mt, n->next, meta);
1.1 kristaps 1061: }
1062:
1063: static void
1.41 schwarze 1064: print_man_foot(struct termp *p, const void *arg)
1.1 kristaps 1065: {
1.101 schwarze 1066: const struct man_meta *meta;
1067: char *title;
1.109 schwarze 1068: size_t datelen, titlen;
1.41 schwarze 1069:
1070: meta = (const struct man_meta *)arg;
1.80 schwarze 1071: assert(meta->title);
1072: assert(meta->msec);
1073: assert(meta->date);
1.21 schwarze 1074:
1075: term_fontrepl(p, TERMFONT_NONE);
1.1 kristaps 1076:
1.103 schwarze 1077: if (meta->hasbody)
1078: term_vspace(p);
1.81 schwarze 1079:
1080: /*
1081: * Temporary, undocumented option to imitate mdoc(7) output.
1082: * In the bottom right corner, use the source instead of
1083: * the title.
1084: */
1085:
1.79 schwarze 1086: if ( ! p->mdocstyle) {
1.103 schwarze 1087: if (meta->hasbody) {
1088: term_vspace(p);
1089: term_vspace(p);
1090: }
1.101 schwarze 1091: mandoc_asprintf(&title, "%s(%s)",
1092: meta->title, meta->msec);
1.79 schwarze 1093: } else if (meta->source) {
1.101 schwarze 1094: title = mandoc_strdup(meta->source);
1.79 schwarze 1095: } else {
1.101 schwarze 1096: title = mandoc_strdup("");
1.79 schwarze 1097: }
1.78 schwarze 1098: datelen = term_strlen(p, meta->date);
1.1 kristaps 1099:
1.81 schwarze 1100: /* Bottom left corner: manual source. */
1101:
1.1 kristaps 1102: p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
1.93 schwarze 1103: p->trailspace = 1;
1.1 kristaps 1104: p->offset = 0;
1.109 schwarze 1105: p->rmargin = p->maxrmargin > datelen ?
1106: (p->maxrmargin + term_len(p, 1) - datelen) / 2 : 0;
1.1 kristaps 1107:
1108: if (meta->source)
1109: term_word(p, meta->source);
1110: term_flushln(p);
1111:
1.81 schwarze 1112: /* At the bottom in the middle: manual date. */
1113:
1.109 schwarze 1114: p->offset = p->rmargin;
1115: titlen = term_strlen(p, title);
1116: p->rmargin = p->maxrmargin > titlen ? p->maxrmargin - titlen : 0;
1.72 schwarze 1117: p->flags |= TERMP_NOSPACE;
1.78 schwarze 1118:
1119: term_word(p, meta->date);
1120: term_flushln(p);
1121:
1.81 schwarze 1122: /* Bottom right corner: manual title and section. */
1123:
1.78 schwarze 1124: p->flags &= ~TERMP_NOBREAK;
1125: p->flags |= TERMP_NOSPACE;
1.93 schwarze 1126: p->trailspace = 0;
1.78 schwarze 1127: p->offset = p->rmargin;
1.1 kristaps 1128: p->rmargin = p->maxrmargin;
1129:
1.78 schwarze 1130: term_word(p, title);
1.1 kristaps 1131: term_flushln(p);
1.101 schwarze 1132: free(title);
1.1 kristaps 1133: }
1134:
1135: static void
1.41 schwarze 1136: print_man_head(struct termp *p, const void *arg)
1.1 kristaps 1137: {
1.101 schwarze 1138: const struct man_meta *meta;
1.102 schwarze 1139: const char *volume;
1.101 schwarze 1140: char *title;
1.102 schwarze 1141: size_t vollen, titlen;
1.41 schwarze 1142:
1.89 schwarze 1143: meta = (const struct man_meta *)arg;
1144: assert(meta->title);
1145: assert(meta->msec);
1.1 kristaps 1146:
1.102 schwarze 1147: volume = NULL == meta->vol ? "" : meta->vol;
1148: vollen = term_strlen(p, volume);
1.1 kristaps 1149:
1.81 schwarze 1150: /* Top left corner: manual title and section. */
1151:
1.101 schwarze 1152: mandoc_asprintf(&title, "%s(%s)", meta->title, meta->msec);
1.42 schwarze 1153: titlen = term_strlen(p, title);
1.1 kristaps 1154:
1.73 schwarze 1155: p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
1.93 schwarze 1156: p->trailspace = 1;
1.1 kristaps 1157: p->offset = 0;
1.102 schwarze 1158: p->rmargin = 2 * (titlen+1) + vollen < p->maxrmargin ?
1159: (p->maxrmargin - vollen + term_len(p, 1)) / 2 :
1.109 schwarze 1160: vollen < p->maxrmargin ? p->maxrmargin - vollen : 0;
1.1 kristaps 1161:
1162: term_word(p, title);
1163: term_flushln(p);
1164:
1.81 schwarze 1165: /* At the top in the middle: manual volume. */
1166:
1.72 schwarze 1167: p->flags |= TERMP_NOSPACE;
1.1 kristaps 1168: p->offset = p->rmargin;
1.102 schwarze 1169: p->rmargin = p->offset + vollen + titlen < p->maxrmargin ?
1.25 schwarze 1170: p->maxrmargin - titlen : p->maxrmargin;
1.1 kristaps 1171:
1.102 schwarze 1172: term_word(p, volume);
1.1 kristaps 1173: term_flushln(p);
1174:
1.81 schwarze 1175: /* Top right corner: title and section, again. */
1176:
1.1 kristaps 1177: p->flags &= ~TERMP_NOBREAK;
1.93 schwarze 1178: p->trailspace = 0;
1.25 schwarze 1179: if (p->rmargin + titlen <= p->maxrmargin) {
1.72 schwarze 1180: p->flags |= TERMP_NOSPACE;
1.25 schwarze 1181: p->offset = p->rmargin;
1182: p->rmargin = p->maxrmargin;
1183: term_word(p, title);
1184: term_flushln(p);
1185: }
1.1 kristaps 1186:
1.73 schwarze 1187: p->flags &= ~TERMP_NOSPACE;
1188: p->offset = 0;
1.1 kristaps 1189: p->rmargin = p->maxrmargin;
1.29 schwarze 1190:
1.100 schwarze 1191: /*
1.81 schwarze 1192: * Groff prints three blank lines before the content.
1193: * Do the same, except in the temporary, undocumented
1194: * mode imitating mdoc(7) output.
1.29 schwarze 1195: */
1196:
1197: term_vspace(p);
1.79 schwarze 1198: if ( ! p->mdocstyle) {
1199: term_vspace(p);
1200: term_vspace(p);
1201: }
1.101 schwarze 1202: free(title);
1.1 kristaps 1203: }