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