Annotation of src/usr.bin/mandoc/term.c, Revision 1.16
1.16 ! schwarze 1: /* $Id: term.c,v 1.15 2009/10/19 09:16:58 schwarze Exp $ */
1.1 kristaps 2: /*
1.2 schwarze 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.2 schwarze 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.2 schwarze 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>
18: #include <err.h>
19: #include <stdio.h>
20: #include <stdlib.h>
21: #include <string.h>
22:
1.15 schwarze 23: #include "chars.h"
1.16 ! schwarze 24: #include "out.h"
1.1 kristaps 25: #include "term.h"
26: #include "man.h"
27: #include "mdoc.h"
1.16 ! schwarze 28: #include "main.h"
1.1 kristaps 29:
1.16 ! schwarze 30: /* FIXME: accomodate non-breaking, non-collapsing white-space. */
! 31: /* FIXME: accomodate non-breaking, collapsing white-space. */
1.1 kristaps 32:
33: static struct termp *term_alloc(enum termenc);
34: static void term_free(struct termp *);
1.11 schwarze 35:
36: static void do_escaped(struct termp *, const char **);
37: static void do_special(struct termp *,
38: const char *, size_t);
39: static void do_reserved(struct termp *,
1.1 kristaps 40: const char *, size_t);
1.11 schwarze 41: static void buffer(struct termp *, char);
42: static void encode(struct termp *, char);
1.1 kristaps 43:
44:
45: void *
46: ascii_alloc(void)
47: {
48:
49: return(term_alloc(TERMENC_ASCII));
50: }
51:
52:
1.13 schwarze 53: void
1.1 kristaps 54: terminal_free(void *arg)
55: {
56:
57: term_free((struct termp *)arg);
58: }
59:
60:
61: static void
62: term_free(struct termp *p)
63: {
64:
65: if (p->buf)
66: free(p->buf);
1.15 schwarze 67: if (p->symtab)
68: chars_free(p->symtab);
1.1 kristaps 69:
70: free(p);
71: }
72:
73:
74: static struct termp *
75: term_alloc(enum termenc enc)
76: {
77: struct termp *p;
78:
79: if (NULL == (p = malloc(sizeof(struct termp))))
1.15 schwarze 80: return(NULL);
1.1 kristaps 81: bzero(p, sizeof(struct termp));
82: p->maxrmargin = 78;
83: p->enc = enc;
84: return(p);
85: }
86:
87:
88: /*
89: * Flush a line of text. A "line" is loosely defined as being something
90: * that should be followed by a newline, regardless of whether it's
91: * broken apart by newlines getting there. A line can also be a
92: * fragment of a columnar list.
93: *
94: * Specifically, a line is whatever's in p->buf of length p->col, which
95: * is zeroed after this function returns.
96: *
1.6 schwarze 97: * The usage of termp:flags is as follows:
1.1 kristaps 98: *
99: * - TERMP_NOLPAD: when beginning to write the line, don't left-pad the
100: * offset value. This is useful when doing columnar lists where the
101: * prior column has right-padded.
102: *
103: * - TERMP_NOBREAK: this is the most important and is used when making
104: * columns. In short: don't print a newline and instead pad to the
105: * right margin. Used in conjunction with TERMP_NOLPAD.
106: *
1.9 schwarze 107: * - TERMP_TWOSPACE: when padding, make sure there are at least two
108: * space characters of padding. Otherwise, rather break the line.
109: *
1.6 schwarze 110: * - TERMP_DANGLE: don't newline when TERMP_NOBREAK is specified and
111: * the line is overrun, and don't pad-right if it's underrun.
112: *
113: * - TERMP_HANG: like TERMP_DANGLE, but doesn't newline when
114: * overruning, instead save the position and continue at that point
115: * when the next invocation.
1.1 kristaps 116: *
117: * In-line line breaking:
118: *
119: * If TERMP_NOBREAK is specified and the line overruns the right
120: * margin, it will break and pad-right to the right margin after
121: * writing. If maxrmargin is violated, it will break and continue
122: * writing from the right-margin, which will lead to the above
123: * scenario upon exit.
124: *
125: * Otherwise, the line will break at the right margin. Extremely long
126: * lines will cause the system to emit a warning (TODO: hyphenate, if
127: * possible).
128: */
129: void
130: term_flushln(struct termp *p)
131: {
132: int i, j;
1.16 ! schwarze 133: size_t vbl, vsz, vis, maxvis, mmax, bp, os;
1.9 schwarze 134: static int overstep = 0;
1.1 kristaps 135:
136: /*
137: * First, establish the maximum columns of "visible" content.
138: * This is usually the difference between the right-margin and
139: * an indentation, but can be, for tagged lists or columns, a
140: * small set of values.
141: */
142:
143: assert(p->offset < p->rmargin);
1.9 schwarze 144: assert((int)(p->rmargin - p->offset) - overstep > 0);
145:
1.16 ! schwarze 146: /* Save the overstep. */
! 147: os = (size_t)overstep;
! 148:
1.9 schwarze 149: maxvis = /* LINTED */
150: p->rmargin - p->offset - overstep;
151: mmax = /* LINTED */
152: p->maxrmargin - p->offset - overstep;
153:
1.1 kristaps 154: bp = TERMP_NOBREAK & p->flags ? mmax : maxvis;
155: vis = 0;
1.9 schwarze 156: overstep = 0;
1.6 schwarze 157:
1.1 kristaps 158: /*
159: * If in the standard case (left-justified), then begin with our
160: * indentation, otherwise (columns, etc.) just start spitting
161: * out text.
162: */
163:
164: if ( ! (p->flags & TERMP_NOLPAD))
165: /* LINTED */
166: for (j = 0; j < (int)p->offset; j++)
167: putchar(' ');
168:
169: for (i = 0; i < (int)p->col; i++) {
170: /*
171: * Count up visible word characters. Control sequences
172: * (starting with the CSI) aren't counted. A space
173: * generates a non-printing word, which is valid (the
174: * space is printed according to regular spacing rules).
175: */
176:
177: /* LINTED */
178: for (j = i, vsz = 0; j < (int)p->col; j++) {
1.10 schwarze 179: if (j && ' ' == p->buf[j])
1.1 kristaps 180: break;
181: else if (8 == p->buf[j])
1.8 schwarze 182: vsz--;
1.1 kristaps 183: else
184: vsz++;
185: }
186:
187: /*
1.5 schwarze 188: * Choose the number of blanks to prepend: no blank at the
189: * beginning of a line, one between words -- but do not
190: * actually write them yet.
1.1 kristaps 191: */
1.5 schwarze 192: vbl = (size_t)(0 == vis ? 0 : 1);
1.1 kristaps 193:
1.5 schwarze 194: /*
195: * Find out whether we would exceed the right margin.
196: * If so, break to the next line. (TODO: hyphenate)
197: * Otherwise, write the chosen number of blanks now.
198: */
199: if (vis && vis + vbl + vsz > bp) {
200: putchar('\n');
201: if (TERMP_NOBREAK & p->flags) {
202: for (j = 0; j < (int)p->rmargin; j++)
203: putchar(' ');
204: vis = p->rmargin - p->offset;
205: } else {
1.1 kristaps 206: for (j = 0; j < (int)p->offset; j++)
207: putchar(' ');
208: vis = 0;
1.5 schwarze 209: }
1.16 ! schwarze 210: /* Remove the overstep width. */
! 211: bp += os;
! 212: os = 0;
1.5 schwarze 213: } else {
214: for (j = 0; j < (int)vbl; j++)
1.1 kristaps 215: putchar(' ');
1.5 schwarze 216: vis += vbl;
1.1 kristaps 217: }
218:
1.3 schwarze 219: /*
1.5 schwarze 220: * Finally, write out the word.
1.1 kristaps 221: */
222: for ( ; i < (int)p->col; i++) {
223: if (' ' == p->buf[i])
224: break;
225: putchar(p->buf[i]);
226: }
227: vis += vsz;
228: }
1.9 schwarze 229: p->col = 0;
1.1 kristaps 230:
1.9 schwarze 231: if ( ! (TERMP_NOBREAK & p->flags)) {
232: putchar('\n');
1.1 kristaps 233: return;
234: }
235:
1.9 schwarze 236: if (TERMP_HANG & p->flags) {
237: /* We need one blank after the tag. */
238: overstep = /* LINTED */
239: vis - maxvis + 1;
240:
241: /*
242: * Behave exactly the same way as groff:
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.
246: * If we landed right at the margin, be happy.
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.
250: */
251: if (overstep >= -1) {
252: assert((int)maxvis + overstep >= 0);
253: /* LINTED */
254: maxvis += overstep;
255: } else
256: overstep = 0;
257:
258: } else if (TERMP_DANGLE & p->flags)
259: return;
1.1 kristaps 260:
1.9 schwarze 261: /* Right-pad. */
262: if (maxvis > vis + /* LINTED */
263: ((TERMP_TWOSPACE & p->flags) ? 1 : 0))
264: for ( ; vis < maxvis; vis++)
265: putchar(' ');
266: else { /* ...or newline break. */
1.1 kristaps 267: putchar('\n');
1.9 schwarze 268: for (i = 0; i < (int)p->rmargin; i++)
269: putchar(' ');
270: }
1.1 kristaps 271: }
272:
273:
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)
281: {
282:
283: p->flags |= TERMP_NOSPACE;
284: if (0 == p->col) {
285: p->flags &= ~TERMP_NOLPAD;
286: return;
287: }
288: term_flushln(p);
289: p->flags &= ~TERMP_NOLPAD;
290: }
291:
292:
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)
301: {
302:
303: term_newln(p);
304: putchar('\n');
305: }
306:
307:
308: static void
1.11 schwarze 309: do_special(struct termp *p, const char *word, size_t len)
1.1 kristaps 310: {
311: const char *rhs;
312: size_t sz;
1.4 schwarze 313: int i;
1.1 kristaps 314:
1.15 schwarze 315: rhs = chars_a2ascii(p->symtab, word, len, &sz);
1.7 schwarze 316:
1.11 schwarze 317: if (NULL == rhs) {
318: #if 0
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
324: return;
325: }
326: for (i = 0; i < (int)sz; i++)
327: encode(p, rhs[i]);
328: }
329:
330:
331: static void
332: do_reserved(struct termp *p, const char *word, size_t len)
333: {
334: const char *rhs;
335: size_t sz;
336: int i;
337:
1.15 schwarze 338: rhs = chars_a2res(p->symtab, word, len, &sz);
1.11 schwarze 339:
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
347: return;
348: }
349: for (i = 0; i < (int)sz; i++)
350: encode(p, rhs[i]);
1.1 kristaps 351: }
352:
353:
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.11 schwarze 360: do_escaped(struct termp *p, const char **word)
1.1 kristaps 361: {
1.11 schwarze 362: int j, type;
1.7 schwarze 363: const char *wp;
364:
365: wp = *word;
1.11 schwarze 366: type = 1;
1.1 kristaps 367:
1.7 schwarze 368: if (0 == *(++wp)) {
369: *word = wp;
1.1 kristaps 370: return;
1.7 schwarze 371: }
1.1 kristaps 372:
1.7 schwarze 373: if ('(' == *wp) {
374: wp++;
375: if (0 == *wp || 0 == *(wp + 1)) {
376: *word = 0 == *wp ? wp : wp + 1;
1.1 kristaps 377: return;
1.7 schwarze 378: }
1.1 kristaps 379:
1.11 schwarze 380: do_special(p, wp, 2);
1.7 schwarze 381: *word = ++wp;
1.1 kristaps 382: return;
383:
1.7 schwarze 384: } else if ('*' == *wp) {
385: if (0 == *(++wp)) {
386: *word = wp;
1.1 kristaps 387: return;
1.7 schwarze 388: }
1.1 kristaps 389:
1.7 schwarze 390: switch (*wp) {
1.1 kristaps 391: case ('('):
1.7 schwarze 392: wp++;
393: if (0 == *wp || 0 == *(wp + 1)) {
394: *word = 0 == *wp ? wp : wp + 1;
1.1 kristaps 395: return;
1.7 schwarze 396: }
1.1 kristaps 397:
1.11 schwarze 398: do_reserved(p, wp, 2);
1.7 schwarze 399: *word = ++wp;
1.1 kristaps 400: return;
401: case ('['):
1.11 schwarze 402: type = 0;
1.1 kristaps 403: break;
404: default:
1.11 schwarze 405: do_reserved(p, wp, 1);
1.7 schwarze 406: *word = wp;
1.1 kristaps 407: return;
408: }
409:
1.7 schwarze 410: } else if ('f' == *wp) {
411: if (0 == *(++wp)) {
412: *word = wp;
1.1 kristaps 413: return;
1.7 schwarze 414: }
415:
416: switch (*wp) {
1.1 kristaps 417: case ('B'):
1.12 schwarze 418: p->bold++;
1.1 kristaps 419: break;
420: case ('I'):
1.12 schwarze 421: p->under++;
1.1 kristaps 422: break;
423: case ('P'):
424: /* FALLTHROUGH */
425: case ('R'):
1.12 schwarze 426: p->bold = p->under = 0;
1.1 kristaps 427: break;
428: default:
429: break;
430: }
1.7 schwarze 431:
432: *word = wp;
1.1 kristaps 433: return;
434:
1.7 schwarze 435: } else if ('[' != *wp) {
1.11 schwarze 436: do_special(p, wp, 1);
1.7 schwarze 437: *word = wp;
1.1 kristaps 438: return;
439: }
440:
1.7 schwarze 441: wp++;
442: for (j = 0; *wp && ']' != *wp; wp++, j++)
1.1 kristaps 443: /* Loop... */ ;
444:
1.7 schwarze 445: if (0 == *wp) {
446: *word = wp;
1.1 kristaps 447: return;
1.7 schwarze 448: }
1.1 kristaps 449:
1.11 schwarze 450: if (type)
451: do_special(p, wp - j, (size_t)j);
452: else
453: do_reserved(p, wp - j, (size_t)j);
1.7 schwarze 454: *word = wp;
1.1 kristaps 455: }
456:
457:
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.7 schwarze 463: void
464: term_word(struct termp *p, const char *word)
1.1 kristaps 465: {
1.7 schwarze 466: const char *sv;
1.1 kristaps 467:
1.14 schwarze 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.1 kristaps 495:
496: if ( ! (TERMP_NOSPACE & p->flags))
1.11 schwarze 497: buffer(p, ' ');
1.1 kristaps 498:
499: if ( ! (p->flags & TERMP_NONOSPACE))
500: p->flags &= ~TERMP_NOSPACE;
501:
1.14 schwarze 502: for ( ; *word; word++)
1.7 schwarze 503: if ('\\' != *word)
1.11 schwarze 504: encode(p, *word);
1.4 schwarze 505: else
1.11 schwarze 506: do_escaped(p, &word);
1.1 kristaps 507:
1.14 schwarze 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.1 kristaps 520: }
521:
522:
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.11 schwarze 529: buffer(struct termp *p, char c)
1.1 kristaps 530: {
531: size_t s;
532:
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.15 schwarze 539: err(1, "realloc"); /* FIXME: shouldn't be here! */
1.1 kristaps 540: p->maxcols = s;
541: }
542: p->buf[(int)(p->col)++] = c;
543: }
544:
1.4 schwarze 545:
546: static void
1.11 schwarze 547: encode(struct termp *p, char c)
1.4 schwarze 548: {
1.7 schwarze 549:
1.12 schwarze 550: if (' ' != c) {
551: if (p->bold) {
1.11 schwarze 552: buffer(p, c);
553: buffer(p, 8);
1.4 schwarze 554: }
1.12 schwarze 555: if (p->under) {
1.11 schwarze 556: buffer(p, '_');
557: buffer(p, 8);
1.4 schwarze 558: }
559: }
1.11 schwarze 560: buffer(p, c);
1.4 schwarze 561: }
1.16 ! schwarze 562:
! 563:
! 564: size_t
! 565: term_vspan(const struct roffsu *su)
! 566: {
! 567: double r;
! 568:
! 569: switch (su->unit) {
! 570: case (SCALE_CM):
! 571: r = su->scale * 2;
! 572: break;
! 573: case (SCALE_IN):
! 574: r = su->scale * 6;
! 575: break;
! 576: case (SCALE_PC):
! 577: r = su->scale;
! 578: break;
! 579: case (SCALE_PT):
! 580: r = su->scale / 8;
! 581: break;
! 582: case (SCALE_MM):
! 583: r = su->scale / 1000;
! 584: break;
! 585: case (SCALE_VS):
! 586: r = su->scale;
! 587: break;
! 588: default:
! 589: r = su->scale - 1;
! 590: break;
! 591: }
! 592:
! 593: if (r < 0.0)
! 594: r = 0.0;
! 595: return(/* LINTED */(size_t)
! 596: r);
! 597: }
! 598:
! 599:
! 600: size_t
! 601: term_hspan(const struct roffsu *su)
! 602: {
! 603: double r;
! 604:
! 605: /* XXX: CM, IN, and PT are approximations. */
! 606:
! 607: switch (su->unit) {
! 608: case (SCALE_CM):
! 609: r = 4 * su->scale;
! 610: break;
! 611: case (SCALE_IN):
! 612: /* XXX: this is an approximation. */
! 613: r = 10 * su->scale;
! 614: break;
! 615: case (SCALE_PC):
! 616: r = (10 * su->scale) / 6;
! 617: break;
! 618: case (SCALE_PT):
! 619: r = (10 * su->scale) / 72;
! 620: break;
! 621: case (SCALE_MM):
! 622: r = su->scale / 1000; /* FIXME: double-check. */
! 623: break;
! 624: case (SCALE_VS):
! 625: r = su->scale * 2 - 1; /* FIXME: double-check. */
! 626: break;
! 627: default:
! 628: r = su->scale;
! 629: break;
! 630: }
! 631:
! 632: if (r < 0.0)
! 633: r = 0.0;
! 634: return((size_t)/* LINTED */
! 635: r);
! 636: }
! 637:
! 638: