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