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