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