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