Annotation of mandoc/term.c, Revision 1.102
1.102 ! kristaps 1: /* $Id: term.c,v 1.101 2009/09/17 07:41:28 kristaps Exp $ */
1.1 kristaps 2: /*
1.75 kristaps 3: * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
1.1 kristaps 4: *
5: * Permission to use, copy, modify, and distribute this software for any
1.74 kristaps 6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 8: *
1.74 kristaps 9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 kristaps 16: */
17: #include <assert.h>
1.23 kristaps 18: #include <err.h>
1.22 kristaps 19: #include <stdio.h>
1.1 kristaps 20: #include <stdlib.h>
21: #include <string.h>
22:
1.101 kristaps 23: #include "chars.h"
1.71 kristaps 24: #include "term.h"
25: #include "man.h"
26: #include "mdoc.h"
1.1 kristaps 27:
1.99 kristaps 28: extern void man_run(struct termp *,
1.71 kristaps 29: const struct man *);
1.99 kristaps 30: extern void mdoc_run(struct termp *,
1.71 kristaps 31: const struct mdoc *);
1.1 kristaps 32:
1.71 kristaps 33: static struct termp *term_alloc(enum termenc);
34: static void term_free(struct termp *);
1.95 kristaps 35:
36: static void do_escaped(struct termp *, const char **);
37: static void do_special(struct termp *,
1.71 kristaps 38: const char *, size_t);
1.95 kristaps 39: static void do_reserved(struct termp *,
1.94 kristaps 40: const char *, size_t);
1.95 kristaps 41: static void buffer(struct termp *, char);
42: static void encode(struct termp *, char);
1.1 kristaps 43:
44:
1.71 kristaps 45: void *
46: ascii_alloc(void)
1.10 kristaps 47: {
1.1 kristaps 48:
1.71 kristaps 49: return(term_alloc(TERMENC_ASCII));
1.1 kristaps 50: }
51:
52:
1.99 kristaps 53: void
1.72 kristaps 54: terminal_man(void *arg, const struct man *man)
1.1 kristaps 55: {
1.71 kristaps 56: struct termp *p;
1.1 kristaps 57:
1.71 kristaps 58: p = (struct termp *)arg;
59: if (NULL == p->symtab)
1.101 kristaps 60: p->symtab = chars_init(CHARS_ASCII);
1.2 kristaps 61:
1.99 kristaps 62: man_run(p, man);
1.72 kristaps 63: }
64:
65:
1.99 kristaps 66: void
1.72 kristaps 67: terminal_mdoc(void *arg, const struct mdoc *mdoc)
68: {
69: struct termp *p;
70:
71: p = (struct termp *)arg;
72: if (NULL == p->symtab)
1.101 kristaps 73: p->symtab = chars_init(CHARS_ASCII);
1.2 kristaps 74:
1.99 kristaps 75: mdoc_run(p, mdoc);
1.1 kristaps 76: }
77:
78:
1.71 kristaps 79: void
80: terminal_free(void *arg)
1.11 kristaps 81: {
82:
1.71 kristaps 83: term_free((struct termp *)arg);
1.11 kristaps 84: }
85:
86:
1.71 kristaps 87: static void
88: term_free(struct termp *p)
1.14 kristaps 89: {
90:
1.71 kristaps 91: if (p->buf)
92: free(p->buf);
1.102 ! kristaps 93: if (p->symtab)
1.101 kristaps 94: chars_free(p->symtab);
1.14 kristaps 95:
1.71 kristaps 96: free(p);
1.14 kristaps 97: }
98:
99:
1.71 kristaps 100: static struct termp *
101: term_alloc(enum termenc enc)
1.14 kristaps 102: {
1.71 kristaps 103: struct termp *p;
1.14 kristaps 104:
1.71 kristaps 105: if (NULL == (p = malloc(sizeof(struct termp))))
1.98 kristaps 106: return(NULL);
1.71 kristaps 107: bzero(p, sizeof(struct termp));
1.80 kristaps 108: p->maxrmargin = 78;
1.71 kristaps 109: p->enc = enc;
110: return(p);
1.14 kristaps 111: }
112:
113:
1.71 kristaps 114: /*
115: * Flush a line of text. A "line" is loosely defined as being something
116: * that should be followed by a newline, regardless of whether it's
117: * broken apart by newlines getting there. A line can also be a
118: * fragment of a columnar list.
119: *
120: * Specifically, a line is whatever's in p->buf of length p->col, which
121: * is zeroed after this function returns.
122: *
1.84 kristaps 123: * The usage of termp:flags is as follows:
1.71 kristaps 124: *
125: * - TERMP_NOLPAD: when beginning to write the line, don't left-pad the
126: * offset value. This is useful when doing columnar lists where the
127: * prior column has right-padded.
128: *
129: * - TERMP_NOBREAK: this is the most important and is used when making
130: * columns. In short: don't print a newline and instead pad to the
131: * right margin. Used in conjunction with TERMP_NOLPAD.
132: *
1.91 kristaps 133: * - TERMP_TWOSPACE: when padding, make sure there are at least two
134: * space characters of padding. Otherwise, rather break the line.
135: *
1.84 kristaps 136: * - TERMP_DANGLE: don't newline when TERMP_NOBREAK is specified and
137: * the line is overrun, and don't pad-right if it's underrun.
138: *
139: * - TERMP_HANG: like TERMP_DANGLE, but doesn't newline when
140: * overruning, instead save the position and continue at that point
141: * when the next invocation.
1.71 kristaps 142: *
143: * In-line line breaking:
144: *
145: * If TERMP_NOBREAK is specified and the line overruns the right
146: * margin, it will break and pad-right to the right margin after
147: * writing. If maxrmargin is violated, it will break and continue
148: * writing from the right-margin, which will lead to the above
149: * scenario upon exit.
150: *
151: * Otherwise, the line will break at the right margin. Extremely long
152: * lines will cause the system to emit a warning (TODO: hyphenate, if
153: * possible).
154: */
155: void
156: term_flushln(struct termp *p)
1.53 kristaps 157: {
1.71 kristaps 158: int i, j;
1.81 kristaps 159: size_t vbl, vsz, vis, maxvis, mmax, bp;
1.91 kristaps 160: static int overstep = 0;
1.53 kristaps 161:
1.71 kristaps 162: /*
163: * First, establish the maximum columns of "visible" content.
164: * This is usually the difference between the right-margin and
165: * an indentation, but can be, for tagged lists or columns, a
166: * small set of values.
167: */
1.53 kristaps 168:
1.71 kristaps 169: assert(p->offset < p->rmargin);
1.92 kristaps 170: assert((int)(p->rmargin - p->offset) - overstep > 0);
171:
172: maxvis = /* LINTED */
173: p->rmargin - p->offset - overstep;
174: mmax = /* LINTED */
175: p->maxrmargin - p->offset - overstep;
176:
1.71 kristaps 177: bp = TERMP_NOBREAK & p->flags ? mmax : maxvis;
178: vis = 0;
1.91 kristaps 179: overstep = 0;
1.84 kristaps 180:
1.71 kristaps 181: /*
182: * If in the standard case (left-justified), then begin with our
183: * indentation, otherwise (columns, etc.) just start spitting
184: * out text.
185: */
1.53 kristaps 186:
1.71 kristaps 187: if ( ! (p->flags & TERMP_NOLPAD))
188: /* LINTED */
189: for (j = 0; j < (int)p->offset; j++)
190: putchar(' ');
191:
192: for (i = 0; i < (int)p->col; i++) {
193: /*
194: * Count up visible word characters. Control sequences
195: * (starting with the CSI) aren't counted. A space
196: * generates a non-printing word, which is valid (the
197: * space is printed according to regular spacing rules).
198: */
199:
200: /* LINTED */
201: for (j = i, vsz = 0; j < (int)p->col; j++) {
1.93 kristaps 202: if (j && ' ' == p->buf[j])
1.71 kristaps 203: break;
204: else if (8 == p->buf[j])
1.89 kristaps 205: vsz--;
1.71 kristaps 206: else
207: vsz++;
208: }
1.53 kristaps 209:
1.71 kristaps 210: /*
1.81 kristaps 211: * Choose the number of blanks to prepend: no blank at the
212: * beginning of a line, one between words -- but do not
213: * actually write them yet.
1.71 kristaps 214: */
1.81 kristaps 215: vbl = (size_t)(0 == vis ? 0 : 1);
1.71 kristaps 216:
1.81 kristaps 217: /*
218: * Find out whether we would exceed the right margin.
219: * If so, break to the next line. (TODO: hyphenate)
220: * Otherwise, write the chosen number of blanks now.
221: */
222: if (vis && vis + vbl + vsz > bp) {
223: putchar('\n');
224: if (TERMP_NOBREAK & p->flags) {
225: for (j = 0; j < (int)p->rmargin; j++)
226: putchar(' ');
227: vis = p->rmargin - p->offset;
228: } else {
1.71 kristaps 229: for (j = 0; j < (int)p->offset; j++)
230: putchar(' ');
231: vis = 0;
1.81 kristaps 232: }
233: } else {
234: for (j = 0; j < (int)vbl; j++)
1.71 kristaps 235: putchar(' ');
1.81 kristaps 236: vis += vbl;
1.71 kristaps 237: }
1.53 kristaps 238:
1.78 kristaps 239: /*
1.81 kristaps 240: * Finally, write out the word.
1.71 kristaps 241: */
242: for ( ; i < (int)p->col; i++) {
243: if (' ' == p->buf[i])
244: break;
245: putchar(p->buf[i]);
246: }
247: vis += vsz;
248: }
1.91 kristaps 249: p->col = 0;
1.15 kristaps 250:
1.91 kristaps 251: if ( ! (TERMP_NOBREAK & p->flags)) {
252: putchar('\n');
1.15 kristaps 253: return;
1.71 kristaps 254: }
1.15 kristaps 255:
1.91 kristaps 256: if (TERMP_HANG & p->flags) {
257: /* We need one blank after the tag. */
1.92 kristaps 258: overstep = /* LINTED */
259: vis - maxvis + 1;
1.91 kristaps 260:
261: /*
262: * Behave exactly the same way as groff:
1.92 kristaps 263: * If we have overstepped the margin, temporarily move
264: * it to the right and flag the rest of the line to be
265: * shorter.
1.91 kristaps 266: * If we landed right at the margin, be happy.
1.92 kristaps 267: * If we are one step before the margin, temporarily
268: * move it one step LEFT and flag the rest of the line
269: * to be longer.
1.91 kristaps 270: */
1.92 kristaps 271: if (overstep >= -1) {
272: assert((int)maxvis + overstep >= 0);
273: /* LINTED */
1.91 kristaps 274: maxvis += overstep;
1.92 kristaps 275: } else
1.91 kristaps 276: overstep = 0;
277:
278: } else if (TERMP_DANGLE & p->flags)
279: return;
1.15 kristaps 280:
1.92 kristaps 281: /* Right-pad. */
282: if (maxvis > vis + /* LINTED */
283: ((TERMP_TWOSPACE & p->flags) ? 1 : 0))
1.91 kristaps 284: for ( ; vis < maxvis; vis++)
285: putchar(' ');
1.92 kristaps 286: else { /* ...or newline break. */
1.71 kristaps 287: putchar('\n');
1.91 kristaps 288: for (i = 0; i < (int)p->rmargin; i++)
289: putchar(' ');
290: }
1.15 kristaps 291: }
292:
293:
1.71 kristaps 294: /*
295: * A newline only breaks an existing line; it won't assert vertical
296: * space. All data in the output buffer is flushed prior to the newline
297: * assertion.
298: */
299: void
300: term_newln(struct termp *p)
1.15 kristaps 301: {
302:
1.71 kristaps 303: p->flags |= TERMP_NOSPACE;
304: if (0 == p->col) {
305: p->flags &= ~TERMP_NOLPAD;
1.15 kristaps 306: return;
1.16 kristaps 307: }
1.71 kristaps 308: term_flushln(p);
309: p->flags &= ~TERMP_NOLPAD;
1.16 kristaps 310: }
311:
312:
1.71 kristaps 313: /*
314: * Asserts a vertical space (a full, empty line-break between lines).
315: * Note that if used twice, this will cause two blank spaces and so on.
316: * All data in the output buffer is flushed prior to the newline
317: * assertion.
318: */
319: void
320: term_vspace(struct termp *p)
1.16 kristaps 321: {
322:
1.62 kristaps 323: term_newln(p);
1.71 kristaps 324: putchar('\n');
1.16 kristaps 325: }
326:
327:
1.71 kristaps 328: static void
1.95 kristaps 329: do_special(struct termp *p, const char *word, size_t len)
1.17 kristaps 330: {
1.71 kristaps 331: const char *rhs;
332: size_t sz;
1.79 kristaps 333: int i;
1.17 kristaps 334:
1.101 kristaps 335: rhs = chars_a2ascii(p->symtab, word, len, &sz);
1.86 kristaps 336:
1.96 kristaps 337: if (NULL == rhs) {
1.97 kristaps 338: #if 0
1.96 kristaps 339: fputs("Unknown special character: ", stderr);
340: for (i = 0; i < (int)len; i++)
341: fputc(word[i], stderr);
342: fputc('\n', stderr);
343: #endif
1.94 kristaps 344: return;
1.96 kristaps 345: }
1.94 kristaps 346: for (i = 0; i < (int)sz; i++)
1.95 kristaps 347: encode(p, rhs[i]);
1.94 kristaps 348: }
349:
350:
351: static void
1.95 kristaps 352: do_reserved(struct termp *p, const char *word, size_t len)
1.94 kristaps 353: {
354: const char *rhs;
355: size_t sz;
356: int i;
357:
1.101 kristaps 358: rhs = chars_a2res(p->symtab, word, len, &sz);
1.94 kristaps 359:
1.96 kristaps 360: if (NULL == rhs) {
361: #if 0
362: fputs("Unknown reserved word: ", stderr);
363: for (i = 0; i < (int)len; i++)
364: fputc(word[i], stderr);
365: fputc('\n', stderr);
366: #endif
1.94 kristaps 367: return;
1.96 kristaps 368: }
1.94 kristaps 369: for (i = 0; i < (int)sz; i++)
1.95 kristaps 370: encode(p, rhs[i]);
1.17 kristaps 371: }
372:
373:
1.71 kristaps 374: /*
375: * Handle an escape sequence: determine its length and pass it to the
376: * escape-symbol look table. Note that we assume mdoc(3) has validated
377: * the escape sequence (we assert upon badly-formed escape sequences).
378: */
379: static void
1.95 kristaps 380: do_escaped(struct termp *p, const char **word)
1.17 kristaps 381: {
1.97 kristaps 382: int j, type;
1.86 kristaps 383: const char *wp;
384:
385: wp = *word;
1.97 kristaps 386: type = 1;
1.17 kristaps 387:
1.86 kristaps 388: if (0 == *(++wp)) {
389: *word = wp;
1.71 kristaps 390: return;
1.86 kristaps 391: }
1.17 kristaps 392:
1.86 kristaps 393: if ('(' == *wp) {
394: wp++;
395: if (0 == *wp || 0 == *(wp + 1)) {
396: *word = 0 == *wp ? wp : wp + 1;
1.71 kristaps 397: return;
1.86 kristaps 398: }
1.22 kristaps 399:
1.95 kristaps 400: do_special(p, wp, 2);
1.86 kristaps 401: *word = ++wp;
1.71 kristaps 402: return;
1.22 kristaps 403:
1.86 kristaps 404: } else if ('*' == *wp) {
405: if (0 == *(++wp)) {
406: *word = wp;
1.71 kristaps 407: return;
1.86 kristaps 408: }
1.22 kristaps 409:
1.86 kristaps 410: switch (*wp) {
1.71 kristaps 411: case ('('):
1.86 kristaps 412: wp++;
413: if (0 == *wp || 0 == *(wp + 1)) {
414: *word = 0 == *wp ? wp : wp + 1;
1.71 kristaps 415: return;
1.86 kristaps 416: }
1.65 kristaps 417:
1.95 kristaps 418: do_reserved(p, wp, 2);
1.86 kristaps 419: *word = ++wp;
1.71 kristaps 420: return;
421: case ('['):
1.97 kristaps 422: type = 0;
1.71 kristaps 423: break;
424: default:
1.95 kristaps 425: do_reserved(p, wp, 1);
1.86 kristaps 426: *word = wp;
1.71 kristaps 427: return;
428: }
429:
1.86 kristaps 430: } else if ('f' == *wp) {
431: if (0 == *(++wp)) {
432: *word = wp;
1.71 kristaps 433: return;
1.86 kristaps 434: }
435:
436: switch (*wp) {
1.71 kristaps 437: case ('B'):
1.98 kristaps 438: p->bold++;
1.71 kristaps 439: break;
440: case ('I'):
1.98 kristaps 441: p->under++;
1.71 kristaps 442: break;
443: case ('P'):
444: /* FALLTHROUGH */
445: case ('R'):
1.98 kristaps 446: p->bold = p->under = 0;
1.71 kristaps 447: break;
448: default:
449: break;
450: }
1.86 kristaps 451:
452: *word = wp;
1.71 kristaps 453: return;
1.22 kristaps 454:
1.86 kristaps 455: } else if ('[' != *wp) {
1.95 kristaps 456: do_special(p, wp, 1);
1.86 kristaps 457: *word = wp;
1.71 kristaps 458: return;
459: }
1.28 kristaps 460:
1.86 kristaps 461: wp++;
462: for (j = 0; *wp && ']' != *wp; wp++, j++)
1.71 kristaps 463: /* Loop... */ ;
1.28 kristaps 464:
1.86 kristaps 465: if (0 == *wp) {
466: *word = wp;
1.71 kristaps 467: return;
1.86 kristaps 468: }
1.48 kristaps 469:
1.97 kristaps 470: if (type)
471: do_special(p, wp - j, (size_t)j);
472: else
473: do_reserved(p, wp - j, (size_t)j);
1.86 kristaps 474: *word = wp;
1.48 kristaps 475: }
476:
477:
1.71 kristaps 478: /*
479: * Handle pwords, partial words, which may be either a single word or a
480: * phrase that cannot be broken down (such as a literal string). This
481: * handles word styling.
482: */
1.86 kristaps 483: void
484: term_word(struct termp *p, const char *word)
1.65 kristaps 485: {
1.88 kristaps 486: const char *sv;
1.71 kristaps 487:
1.100 kristaps 488: sv = word;
489:
490: if (word[0] && 0 == word[1])
491: switch (word[0]) {
492: case('.'):
493: /* FALLTHROUGH */
494: case(','):
495: /* FALLTHROUGH */
496: case(';'):
497: /* FALLTHROUGH */
498: case(':'):
499: /* FALLTHROUGH */
500: case('?'):
501: /* FALLTHROUGH */
502: case('!'):
503: /* FALLTHROUGH */
504: case(')'):
505: /* FALLTHROUGH */
506: case(']'):
507: /* FALLTHROUGH */
508: case('}'):
509: if ( ! (TERMP_IGNDELIM & p->flags))
510: p->flags |= TERMP_NOSPACE;
511: break;
512: default:
513: break;
514: }
1.65 kristaps 515:
1.71 kristaps 516: if ( ! (TERMP_NOSPACE & p->flags))
1.95 kristaps 517: buffer(p, ' ');
1.65 kristaps 518:
1.71 kristaps 519: if ( ! (p->flags & TERMP_NONOSPACE))
520: p->flags &= ~TERMP_NOSPACE;
1.65 kristaps 521:
1.100 kristaps 522: for ( ; *word; word++)
1.86 kristaps 523: if ('\\' != *word)
1.95 kristaps 524: encode(p, *word);
1.79 kristaps 525: else
1.95 kristaps 526: do_escaped(p, &word);
1.65 kristaps 527:
1.100 kristaps 528: if (sv[0] && 0 == sv[1])
529: switch (sv[0]) {
530: case('('):
531: /* FALLTHROUGH */
532: case('['):
533: /* FALLTHROUGH */
534: case('{'):
535: p->flags |= TERMP_NOSPACE;
536: break;
537: default:
538: break;
539: }
1.65 kristaps 540: }
541:
542:
1.71 kristaps 543: /*
544: * Insert a single character into the line-buffer. If the buffer's
545: * space is exceeded, then allocate more space by doubling the buffer
546: * size.
547: */
548: static void
1.95 kristaps 549: buffer(struct termp *p, char c)
1.51 kristaps 550: {
1.71 kristaps 551: size_t s;
1.51 kristaps 552:
1.71 kristaps 553: if (p->col + 1 >= p->maxcols) {
554: if (0 == p->maxcols)
555: p->maxcols = 256;
556: s = p->maxcols * 2;
557: p->buf = realloc(p->buf, s);
558: if (NULL == p->buf)
1.98 kristaps 559: err(1, "realloc"); /* FIXME: shouldn't be here! */
1.71 kristaps 560: p->maxcols = s;
561: }
562: p->buf[(int)(p->col)++] = c;
1.51 kristaps 563: }
564:
1.79 kristaps 565:
566: static void
1.95 kristaps 567: encode(struct termp *p, char c)
1.79 kristaps 568: {
1.89 kristaps 569:
1.98 kristaps 570: if (' ' != c) {
571: if (p->bold) {
1.95 kristaps 572: buffer(p, c);
573: buffer(p, 8);
1.79 kristaps 574: }
1.98 kristaps 575: if (p->under) {
1.95 kristaps 576: buffer(p, '_');
577: buffer(p, 8);
1.79 kristaps 578: }
579: }
1.95 kristaps 580: buffer(p, c);
1.79 kristaps 581: }
CVSweb