Annotation of mandoc/term.c, Revision 1.103
1.103 ! kristaps 1: /* $Id: term.c,v 1.102 2009/09/20 13:43:31 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.81 kristaps 162: size_t vbl, vsz, vis, maxvis, mmax, bp;
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:
175: maxvis = /* LINTED */
176: p->rmargin - p->offset - overstep;
177: mmax = /* LINTED */
178: p->maxrmargin - p->offset - overstep;
179:
1.71 kristaps 180: bp = TERMP_NOBREAK & p->flags ? mmax : maxvis;
181: vis = 0;
1.91 kristaps 182: overstep = 0;
1.84 kristaps 183:
1.71 kristaps 184: /*
185: * If in the standard case (left-justified), then begin with our
186: * indentation, otherwise (columns, etc.) just start spitting
187: * out text.
188: */
1.53 kristaps 189:
1.71 kristaps 190: if ( ! (p->flags & TERMP_NOLPAD))
191: /* LINTED */
192: for (j = 0; j < (int)p->offset; j++)
193: putchar(' ');
194:
195: for (i = 0; i < (int)p->col; i++) {
196: /*
197: * Count up visible word characters. Control sequences
198: * (starting with the CSI) aren't counted. A space
199: * generates a non-printing word, which is valid (the
200: * space is printed according to regular spacing rules).
201: */
202:
203: /* LINTED */
204: for (j = i, vsz = 0; j < (int)p->col; j++) {
1.93 kristaps 205: if (j && ' ' == p->buf[j])
1.71 kristaps 206: break;
207: else if (8 == p->buf[j])
1.89 kristaps 208: vsz--;
1.71 kristaps 209: else
210: vsz++;
211: }
1.53 kristaps 212:
1.71 kristaps 213: /*
1.81 kristaps 214: * Choose the number of blanks to prepend: no blank at the
215: * beginning of a line, one between words -- but do not
216: * actually write them yet.
1.71 kristaps 217: */
1.81 kristaps 218: vbl = (size_t)(0 == vis ? 0 : 1);
1.71 kristaps 219:
1.81 kristaps 220: /*
221: * Find out whether we would exceed the right margin.
222: * If so, break to the next line. (TODO: hyphenate)
223: * Otherwise, write the chosen number of blanks now.
224: */
225: if (vis && vis + vbl + vsz > bp) {
226: putchar('\n');
227: if (TERMP_NOBREAK & p->flags) {
228: for (j = 0; j < (int)p->rmargin; j++)
229: putchar(' ');
230: vis = p->rmargin - p->offset;
231: } else {
1.71 kristaps 232: for (j = 0; j < (int)p->offset; j++)
233: putchar(' ');
234: vis = 0;
1.81 kristaps 235: }
236: } else {
237: for (j = 0; j < (int)vbl; j++)
1.71 kristaps 238: putchar(' ');
1.81 kristaps 239: vis += vbl;
1.71 kristaps 240: }
1.53 kristaps 241:
1.78 kristaps 242: /*
1.81 kristaps 243: * Finally, write out the word.
1.71 kristaps 244: */
245: for ( ; i < (int)p->col; i++) {
246: if (' ' == p->buf[i])
247: break;
248: putchar(p->buf[i]);
249: }
250: vis += vsz;
251: }
1.91 kristaps 252: p->col = 0;
1.15 kristaps 253:
1.91 kristaps 254: if ( ! (TERMP_NOBREAK & p->flags)) {
255: putchar('\n');
1.15 kristaps 256: return;
1.71 kristaps 257: }
1.15 kristaps 258:
1.91 kristaps 259: if (TERMP_HANG & p->flags) {
260: /* We need one blank after the tag. */
1.92 kristaps 261: overstep = /* LINTED */
262: vis - maxvis + 1;
1.91 kristaps 263:
264: /*
265: * Behave exactly the same way as groff:
1.92 kristaps 266: * If we have overstepped the margin, temporarily move
267: * it to the right and flag the rest of the line to be
268: * shorter.
1.91 kristaps 269: * If we landed right at the margin, be happy.
1.92 kristaps 270: * If we are one step before the margin, temporarily
271: * move it one step LEFT and flag the rest of the line
272: * to be longer.
1.91 kristaps 273: */
1.92 kristaps 274: if (overstep >= -1) {
275: assert((int)maxvis + overstep >= 0);
276: /* LINTED */
1.91 kristaps 277: maxvis += overstep;
1.92 kristaps 278: } else
1.91 kristaps 279: overstep = 0;
280:
281: } else if (TERMP_DANGLE & p->flags)
282: return;
1.15 kristaps 283:
1.92 kristaps 284: /* Right-pad. */
285: if (maxvis > vis + /* LINTED */
286: ((TERMP_TWOSPACE & p->flags) ? 1 : 0))
1.91 kristaps 287: for ( ; vis < maxvis; vis++)
288: putchar(' ');
1.92 kristaps 289: else { /* ...or newline break. */
1.71 kristaps 290: putchar('\n');
1.91 kristaps 291: for (i = 0; i < (int)p->rmargin; i++)
292: putchar(' ');
293: }
1.15 kristaps 294: }
295:
296:
1.71 kristaps 297: /*
298: * A newline only breaks an existing line; it won't assert vertical
299: * space. All data in the output buffer is flushed prior to the newline
300: * assertion.
301: */
302: void
303: term_newln(struct termp *p)
1.15 kristaps 304: {
305:
1.71 kristaps 306: p->flags |= TERMP_NOSPACE;
307: if (0 == p->col) {
308: p->flags &= ~TERMP_NOLPAD;
1.15 kristaps 309: return;
1.16 kristaps 310: }
1.71 kristaps 311: term_flushln(p);
312: p->flags &= ~TERMP_NOLPAD;
1.16 kristaps 313: }
314:
315:
1.71 kristaps 316: /*
317: * Asserts a vertical space (a full, empty line-break between lines).
318: * Note that if used twice, this will cause two blank spaces and so on.
319: * All data in the output buffer is flushed prior to the newline
320: * assertion.
321: */
322: void
323: term_vspace(struct termp *p)
1.16 kristaps 324: {
325:
1.62 kristaps 326: term_newln(p);
1.71 kristaps 327: putchar('\n');
1.16 kristaps 328: }
329:
330:
1.71 kristaps 331: static void
1.95 kristaps 332: do_special(struct termp *p, const char *word, size_t len)
1.17 kristaps 333: {
1.71 kristaps 334: const char *rhs;
335: size_t sz;
1.79 kristaps 336: int i;
1.17 kristaps 337:
1.101 kristaps 338: rhs = chars_a2ascii(p->symtab, word, len, &sz);
1.86 kristaps 339:
1.96 kristaps 340: if (NULL == rhs) {
1.97 kristaps 341: #if 0
1.96 kristaps 342: fputs("Unknown special character: ", stderr);
343: for (i = 0; i < (int)len; i++)
344: fputc(word[i], stderr);
345: fputc('\n', stderr);
346: #endif
1.94 kristaps 347: return;
1.96 kristaps 348: }
1.94 kristaps 349: for (i = 0; i < (int)sz; i++)
1.95 kristaps 350: encode(p, rhs[i]);
1.94 kristaps 351: }
352:
353:
354: static void
1.95 kristaps 355: do_reserved(struct termp *p, const char *word, size_t len)
1.94 kristaps 356: {
357: const char *rhs;
358: size_t sz;
359: int i;
360:
1.101 kristaps 361: rhs = chars_a2res(p->symtab, word, len, &sz);
1.94 kristaps 362:
1.96 kristaps 363: if (NULL == rhs) {
364: #if 0
365: fputs("Unknown reserved word: ", stderr);
366: for (i = 0; i < (int)len; i++)
367: fputc(word[i], stderr);
368: fputc('\n', stderr);
369: #endif
1.94 kristaps 370: return;
1.96 kristaps 371: }
1.94 kristaps 372: for (i = 0; i < (int)sz; i++)
1.95 kristaps 373: encode(p, rhs[i]);
1.17 kristaps 374: }
375:
376:
1.71 kristaps 377: /*
378: * Handle an escape sequence: determine its length and pass it to the
379: * escape-symbol look table. Note that we assume mdoc(3) has validated
380: * the escape sequence (we assert upon badly-formed escape sequences).
381: */
382: static void
1.95 kristaps 383: do_escaped(struct termp *p, const char **word)
1.17 kristaps 384: {
1.97 kristaps 385: int j, type;
1.86 kristaps 386: const char *wp;
387:
388: wp = *word;
1.97 kristaps 389: type = 1;
1.17 kristaps 390:
1.86 kristaps 391: if (0 == *(++wp)) {
392: *word = wp;
1.71 kristaps 393: return;
1.86 kristaps 394: }
1.17 kristaps 395:
1.86 kristaps 396: if ('(' == *wp) {
397: wp++;
398: if (0 == *wp || 0 == *(wp + 1)) {
399: *word = 0 == *wp ? wp : wp + 1;
1.71 kristaps 400: return;
1.86 kristaps 401: }
1.22 kristaps 402:
1.95 kristaps 403: do_special(p, wp, 2);
1.86 kristaps 404: *word = ++wp;
1.71 kristaps 405: return;
1.22 kristaps 406:
1.86 kristaps 407: } else if ('*' == *wp) {
408: if (0 == *(++wp)) {
409: *word = wp;
1.71 kristaps 410: return;
1.86 kristaps 411: }
1.22 kristaps 412:
1.86 kristaps 413: switch (*wp) {
1.71 kristaps 414: case ('('):
1.86 kristaps 415: wp++;
416: if (0 == *wp || 0 == *(wp + 1)) {
417: *word = 0 == *wp ? wp : wp + 1;
1.71 kristaps 418: return;
1.86 kristaps 419: }
1.65 kristaps 420:
1.95 kristaps 421: do_reserved(p, wp, 2);
1.86 kristaps 422: *word = ++wp;
1.71 kristaps 423: return;
424: case ('['):
1.97 kristaps 425: type = 0;
1.71 kristaps 426: break;
427: default:
1.95 kristaps 428: do_reserved(p, wp, 1);
1.86 kristaps 429: *word = wp;
1.71 kristaps 430: return;
431: }
432:
1.86 kristaps 433: } else if ('f' == *wp) {
434: if (0 == *(++wp)) {
435: *word = wp;
1.71 kristaps 436: return;
1.86 kristaps 437: }
438:
439: switch (*wp) {
1.71 kristaps 440: case ('B'):
1.98 kristaps 441: p->bold++;
1.71 kristaps 442: break;
443: case ('I'):
1.98 kristaps 444: p->under++;
1.71 kristaps 445: break;
446: case ('P'):
447: /* FALLTHROUGH */
448: case ('R'):
1.98 kristaps 449: p->bold = p->under = 0;
1.71 kristaps 450: break;
451: default:
452: break;
453: }
1.86 kristaps 454:
455: *word = wp;
1.71 kristaps 456: return;
1.22 kristaps 457:
1.86 kristaps 458: } else if ('[' != *wp) {
1.95 kristaps 459: do_special(p, wp, 1);
1.86 kristaps 460: *word = wp;
1.71 kristaps 461: return;
462: }
1.28 kristaps 463:
1.86 kristaps 464: wp++;
465: for (j = 0; *wp && ']' != *wp; wp++, j++)
1.71 kristaps 466: /* Loop... */ ;
1.28 kristaps 467:
1.86 kristaps 468: if (0 == *wp) {
469: *word = wp;
1.71 kristaps 470: return;
1.86 kristaps 471: }
1.48 kristaps 472:
1.97 kristaps 473: if (type)
474: do_special(p, wp - j, (size_t)j);
475: else
476: do_reserved(p, wp - j, (size_t)j);
1.86 kristaps 477: *word = wp;
1.48 kristaps 478: }
479:
480:
1.71 kristaps 481: /*
482: * Handle pwords, partial words, which may be either a single word or a
483: * phrase that cannot be broken down (such as a literal string). This
484: * handles word styling.
485: */
1.86 kristaps 486: void
487: term_word(struct termp *p, const char *word)
1.65 kristaps 488: {
1.88 kristaps 489: const char *sv;
1.71 kristaps 490:
1.100 kristaps 491: sv = word;
492:
493: if (word[0] && 0 == word[1])
494: switch (word[0]) {
495: case('.'):
496: /* FALLTHROUGH */
497: case(','):
498: /* FALLTHROUGH */
499: case(';'):
500: /* FALLTHROUGH */
501: case(':'):
502: /* FALLTHROUGH */
503: case('?'):
504: /* FALLTHROUGH */
505: case('!'):
506: /* FALLTHROUGH */
507: case(')'):
508: /* FALLTHROUGH */
509: case(']'):
510: /* FALLTHROUGH */
511: case('}'):
512: if ( ! (TERMP_IGNDELIM & p->flags))
513: p->flags |= TERMP_NOSPACE;
514: break;
515: default:
516: break;
517: }
1.65 kristaps 518:
1.71 kristaps 519: if ( ! (TERMP_NOSPACE & p->flags))
1.95 kristaps 520: buffer(p, ' ');
1.65 kristaps 521:
1.71 kristaps 522: if ( ! (p->flags & TERMP_NONOSPACE))
523: p->flags &= ~TERMP_NOSPACE;
1.65 kristaps 524:
1.100 kristaps 525: for ( ; *word; word++)
1.86 kristaps 526: if ('\\' != *word)
1.95 kristaps 527: encode(p, *word);
1.79 kristaps 528: else
1.95 kristaps 529: do_escaped(p, &word);
1.65 kristaps 530:
1.100 kristaps 531: if (sv[0] && 0 == sv[1])
532: switch (sv[0]) {
533: case('('):
534: /* FALLTHROUGH */
535: case('['):
536: /* FALLTHROUGH */
537: case('{'):
538: p->flags |= TERMP_NOSPACE;
539: break;
540: default:
541: break;
542: }
1.65 kristaps 543: }
544:
545:
1.71 kristaps 546: /*
547: * Insert a single character into the line-buffer. If the buffer's
548: * space is exceeded, then allocate more space by doubling the buffer
549: * size.
550: */
551: static void
1.95 kristaps 552: buffer(struct termp *p, char c)
1.51 kristaps 553: {
1.71 kristaps 554: size_t s;
1.51 kristaps 555:
1.71 kristaps 556: if (p->col + 1 >= p->maxcols) {
557: if (0 == p->maxcols)
558: p->maxcols = 256;
559: s = p->maxcols * 2;
560: p->buf = realloc(p->buf, s);
561: if (NULL == p->buf)
1.98 kristaps 562: err(1, "realloc"); /* FIXME: shouldn't be here! */
1.71 kristaps 563: p->maxcols = s;
564: }
565: p->buf[(int)(p->col)++] = c;
1.51 kristaps 566: }
567:
1.79 kristaps 568:
569: static void
1.95 kristaps 570: encode(struct termp *p, char c)
1.79 kristaps 571: {
1.89 kristaps 572:
1.98 kristaps 573: if (' ' != c) {
574: if (p->bold) {
1.95 kristaps 575: buffer(p, c);
576: buffer(p, 8);
1.79 kristaps 577: }
1.98 kristaps 578: if (p->under) {
1.95 kristaps 579: buffer(p, '_');
580: buffer(p, 8);
1.79 kristaps 581: }
582: }
1.95 kristaps 583: buffer(p, c);
1.79 kristaps 584: }
CVSweb