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