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