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