Annotation of mandoc/term.c, Revision 1.74
1.74 ! kristaps 1: /* $Id: term.c,v 1.73 2009/04/03 12:27:18 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
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));
109: p->maxrmargin = 78;
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.71 kristaps 275: /*
276: * Write out the word and a trailing space. Omit the
277: * space if we're the last word in the line or beyond
278: * our breakpoint.
279: */
280:
281: for ( ; i < (int)p->col; i++) {
282: if (' ' == p->buf[i])
283: break;
284: putchar(p->buf[i]);
285: }
286: vis += vsz;
287: if (i < (int)p->col && vis <= bp) {
288: putchar(' ');
289: vis++;
290: }
291: }
1.15 kristaps 292:
1.71 kristaps 293: /*
294: * If we've overstepped our maximum visible no-break space, then
295: * cause a newline and offset at the right margin.
296: */
1.15 kristaps 297:
1.71 kristaps 298: if ((TERMP_NOBREAK & p->flags) && vis >= maxvis) {
299: if ( ! (TERMP_NONOBREAK & p->flags)) {
300: putchar('\n');
301: for (i = 0; i < (int)p->rmargin; i++)
302: putchar(' ');
303: }
304: p->col = 0;
1.15 kristaps 305: return;
1.71 kristaps 306: }
1.15 kristaps 307:
1.71 kristaps 308: /*
309: * If we're not to right-marginalise it (newline), then instead
310: * pad to the right margin and stay off.
311: */
1.15 kristaps 312:
1.71 kristaps 313: if (p->flags & TERMP_NOBREAK) {
314: if ( ! (TERMP_NONOBREAK & p->flags))
315: for ( ; vis < maxvis; vis++)
316: putchar(' ');
317: } else
318: putchar('\n');
1.15 kristaps 319:
1.71 kristaps 320: p->col = 0;
1.15 kristaps 321: }
322:
323:
1.71 kristaps 324: /*
325: * A newline only breaks an existing line; it won't assert vertical
326: * space. All data in the output buffer is flushed prior to the newline
327: * assertion.
328: */
329: void
330: term_newln(struct termp *p)
1.15 kristaps 331: {
332:
1.71 kristaps 333: p->flags |= TERMP_NOSPACE;
334: if (0 == p->col) {
335: p->flags &= ~TERMP_NOLPAD;
1.15 kristaps 336: return;
1.16 kristaps 337: }
1.71 kristaps 338: term_flushln(p);
339: p->flags &= ~TERMP_NOLPAD;
1.16 kristaps 340: }
341:
342:
1.71 kristaps 343: /*
344: * Asserts a vertical space (a full, empty line-break between lines).
345: * Note that if used twice, this will cause two blank spaces and so on.
346: * All data in the output buffer is flushed prior to the newline
347: * assertion.
348: */
349: void
350: term_vspace(struct termp *p)
1.16 kristaps 351: {
352:
1.62 kristaps 353: term_newln(p);
1.71 kristaps 354: putchar('\n');
1.16 kristaps 355: }
356:
357:
1.71 kristaps 358: /*
359: * Break apart a word into "pwords" (partial-words, usually from
360: * breaking up a phrase into individual words) and, eventually, put them
361: * into the output buffer. If we're a literal word, then don't break up
362: * the word and put it verbatim into the output buffer.
363: */
364: void
365: term_word(struct termp *p, const char *word)
1.17 kristaps 366: {
1.71 kristaps 367: int i, j, len;
1.17 kristaps 368:
1.71 kristaps 369: len = (int)strlen(word);
1.17 kristaps 370:
1.71 kristaps 371: if (p->flags & TERMP_LITERAL) {
372: term_pword(p, word, len);
373: return;
374: }
1.17 kristaps 375:
1.71 kristaps 376: /* LINTED */
377: for (j = i = 0; i < len; i++) {
378: if (' ' != word[i]) {
379: j++;
380: continue;
381: }
382:
383: /* Escaped spaces don't delimit... */
384: if (i && ' ' == word[i] && '\\' == word[i - 1]) {
385: j++;
386: continue;
387: }
1.17 kristaps 388:
1.71 kristaps 389: if (0 == j)
390: continue;
391: assert(i >= j);
392: term_pword(p, &word[i - j], j);
393: j = 0;
394: }
395: if (j > 0) {
396: assert(i >= j);
397: term_pword(p, &word[i - j], j);
398: }
1.17 kristaps 399: }
400:
401:
1.71 kristaps 402: /*
403: * Determine the symbol indicated by an escape sequences, that is, one
404: * starting with a backslash. Once done, we pass this value into the
405: * output buffer by way of the symbol table.
406: */
407: static void
408: term_nescape(struct termp *p, const char *word, size_t len)
1.17 kristaps 409: {
1.71 kristaps 410: const char *rhs;
411: size_t sz;
1.17 kristaps 412:
1.71 kristaps 413: if (NULL == (rhs = term_a2ascii(p->symtab, word, len, &sz)))
414: return;
415: term_stringa(p, rhs, sz);
1.17 kristaps 416: }
417:
418:
1.71 kristaps 419: /*
420: * Handle an escape sequence: determine its length and pass it to the
421: * escape-symbol look table. Note that we assume mdoc(3) has validated
422: * the escape sequence (we assert upon badly-formed escape sequences).
423: */
424: static void
425: term_pescape(struct termp *p, const char *word, int *i, int len)
1.17 kristaps 426: {
1.71 kristaps 427: int j;
1.17 kristaps 428:
1.71 kristaps 429: if (++(*i) >= len)
430: return;
1.17 kristaps 431:
1.71 kristaps 432: if ('(' == word[*i]) {
433: (*i)++;
434: if (*i + 1 >= len)
435: return;
1.22 kristaps 436:
1.71 kristaps 437: term_nescape(p, &word[*i], 2);
438: (*i)++;
439: return;
1.22 kristaps 440:
1.71 kristaps 441: } else if ('*' == word[*i]) {
442: (*i)++;
443: if (*i >= len)
444: return;
1.22 kristaps 445:
1.71 kristaps 446: switch (word[*i]) {
447: case ('('):
448: (*i)++;
449: if (*i + 1 >= len)
450: return;
1.65 kristaps 451:
1.71 kristaps 452: term_nescape(p, &word[*i], 2);
453: (*i)++;
454: return;
455: case ('['):
456: break;
457: default:
458: term_nescape(p, &word[*i], 1);
459: return;
460: }
461:
462: } else if ('f' == word[*i]) {
463: (*i)++;
464: if (*i >= len)
465: return;
466: switch (word[*i]) {
467: case ('B'):
468: p->flags |= TERMP_BOLD;
469: break;
470: case ('I'):
471: p->flags |= TERMP_UNDER;
472: break;
473: case ('P'):
474: /* FALLTHROUGH */
475: case ('R'):
476: p->flags &= ~TERMP_STYLE;
477: break;
478: default:
479: break;
480: }
481: return;
1.22 kristaps 482:
1.71 kristaps 483: } else if ('[' != word[*i]) {
484: term_nescape(p, &word[*i], 1);
485: return;
486: }
1.28 kristaps 487:
1.71 kristaps 488: (*i)++;
489: for (j = 0; word[*i] && ']' != word[*i]; (*i)++, j++)
490: /* Loop... */ ;
1.28 kristaps 491:
1.71 kristaps 492: if (0 == word[*i])
493: return;
1.48 kristaps 494:
1.71 kristaps 495: term_nescape(p, &word[*i - j], (size_t)j);
1.48 kristaps 496: }
497:
498:
1.71 kristaps 499: /*
500: * Handle pwords, partial words, which may be either a single word or a
501: * phrase that cannot be broken down (such as a literal string). This
502: * handles word styling.
503: */
504: static void
505: term_pword(struct termp *p, const char *word, int len)
1.65 kristaps 506: {
1.71 kristaps 507: int i;
508:
509: if (term_isclosedelim(word, len))
510: if ( ! (TERMP_IGNDELIM & p->flags))
511: p->flags |= TERMP_NOSPACE;
1.65 kristaps 512:
1.71 kristaps 513: if ( ! (TERMP_NOSPACE & p->flags))
514: term_chara(p, ' ');
1.65 kristaps 515:
1.71 kristaps 516: if ( ! (p->flags & TERMP_NONOSPACE))
517: p->flags &= ~TERMP_NOSPACE;
1.65 kristaps 518:
1.71 kristaps 519: /*
520: * If ANSI (word-length styling), then apply our style now,
521: * before the word.
522: */
1.28 kristaps 523:
1.71 kristaps 524: for (i = 0; i < len; i++) {
525: if ('\\' == word[i]) {
526: term_pescape(p, word, &i, len);
527: continue;
528: }
1.28 kristaps 529:
1.71 kristaps 530: if (TERMP_STYLE & p->flags) {
531: if (TERMP_BOLD & p->flags) {
532: term_chara(p, word[i]);
533: term_chara(p, 8);
534: }
535: if (TERMP_UNDER & p->flags) {
536: term_chara(p, '_');
537: term_chara(p, 8);
538: }
539: }
1.28 kristaps 540:
1.71 kristaps 541: term_chara(p, word[i]);
542: }
1.65 kristaps 543:
1.71 kristaps 544: if (term_isopendelim(word, len))
545: p->flags |= TERMP_NOSPACE;
1.65 kristaps 546: }
547:
548:
1.71 kristaps 549: /*
550: * Like term_chara() but for arbitrary-length buffers. Resize the
551: * buffer by a factor of two (if the buffer is less than that) or the
552: * buffer's size.
553: */
1.65 kristaps 554: static void
1.71 kristaps 555: term_stringa(struct termp *p, const char *c, size_t sz)
1.28 kristaps 556: {
1.71 kristaps 557: size_t s;
1.28 kristaps 558:
1.71 kristaps 559: if (0 == sz)
560: return;
1.51 kristaps 561:
1.71 kristaps 562: assert(c);
563: if (p->col + sz >= p->maxcols) {
564: if (0 == p->maxcols)
565: p->maxcols = 256;
566: s = sz > p->maxcols * 2 ? sz : p->maxcols * 2;
567: p->buf = realloc(p->buf, s);
568: if (NULL == p->buf)
569: err(1, "realloc");
570: p->maxcols = s;
1.51 kristaps 571: }
572:
1.71 kristaps 573: (void)memcpy(&p->buf[(int)p->col], c, sz);
574: p->col += sz;
1.51 kristaps 575: }
576:
577:
1.71 kristaps 578: /*
579: * Insert a single character into the line-buffer. If the buffer's
580: * space is exceeded, then allocate more space by doubling the buffer
581: * size.
582: */
583: static void
584: term_chara(struct termp *p, char c)
1.51 kristaps 585: {
1.71 kristaps 586: size_t s;
1.51 kristaps 587:
1.71 kristaps 588: if (p->col + 1 >= p->maxcols) {
589: if (0 == p->maxcols)
590: p->maxcols = 256;
591: s = p->maxcols * 2;
592: p->buf = realloc(p->buf, s);
593: if (NULL == p->buf)
594: err(1, "realloc");
595: p->maxcols = s;
596: }
597: p->buf[(int)(p->col)++] = c;
1.51 kristaps 598: }
599:
CVSweb