Annotation of src/usr.bin/mandoc/term.c, Revision 1.29
1.29 ! schwarze 1: /* $Id: term.c,v 1.28 2010/04/12 22:52:19 schwarze 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: */
1.20 schwarze 17: #include <sys/types.h>
18:
1.1 kristaps 19: #include <assert.h>
1.20 schwarze 20: #include <ctype.h>
1.1 kristaps 21: #include <stdio.h>
22: #include <stdlib.h>
23: #include <string.h>
1.19 schwarze 24: #include <time.h>
1.1 kristaps 25:
1.15 schwarze 26: #include "chars.h"
1.16 schwarze 27: #include "out.h"
1.1 kristaps 28: #include "term.h"
29: #include "man.h"
30: #include "mdoc.h"
1.16 schwarze 31: #include "main.h"
1.1 kristaps 32:
33: static struct termp *term_alloc(enum termenc);
34: static void term_free(struct termp *);
1.20 schwarze 35: static void spec(struct termp *, const char *, size_t);
36: static void res(struct termp *, const char *, size_t);
37: static void buffera(struct termp *, const char *, size_t);
38: static void bufferc(struct termp *, char);
39: static void adjbuf(struct termp *p, size_t);
40: static void encode(struct termp *, const char *, size_t);
1.1 kristaps 41:
42:
43: void *
44: ascii_alloc(void)
45: {
46:
47: return(term_alloc(TERMENC_ASCII));
48: }
49:
50:
1.13 schwarze 51: void
1.1 kristaps 52: terminal_free(void *arg)
53: {
54:
55: term_free((struct termp *)arg);
56: }
57:
58:
59: static void
60: term_free(struct termp *p)
61: {
62:
63: if (p->buf)
64: free(p->buf);
1.15 schwarze 65: if (p->symtab)
66: chars_free(p->symtab);
1.1 kristaps 67:
68: free(p);
69: }
70:
71:
72: static struct termp *
73: term_alloc(enum termenc enc)
74: {
75: struct termp *p;
76:
1.19 schwarze 77: p = calloc(1, sizeof(struct termp));
78: if (NULL == p) {
79: perror(NULL);
80: exit(EXIT_FAILURE);
81: }
1.1 kristaps 82: p->enc = enc;
83: return(p);
84: }
85:
86:
87: /*
88: * Flush a line of text. A "line" is loosely defined as being something
89: * that should be followed by a newline, regardless of whether it's
90: * broken apart by newlines getting there. A line can also be a
1.27 schwarze 91: * fragment of a columnar list (`Bl -tag' or `Bl -column'), which does
92: * not have a trailing newline.
1.1 kristaps 93: *
1.27 schwarze 94: * The following flags may be specified:
1.1 kristaps 95: *
96: * - TERMP_NOLPAD: when beginning to write the line, don't left-pad the
97: * offset value. This is useful when doing columnar lists where the
98: * prior column has right-padded.
99: *
100: * - TERMP_NOBREAK: this is the most important and is used when making
101: * columns. In short: don't print a newline and instead pad to the
102: * right margin. Used in conjunction with TERMP_NOLPAD.
103: *
1.9 schwarze 104: * - TERMP_TWOSPACE: when padding, make sure there are at least two
105: * space characters of padding. Otherwise, rather break the line.
106: *
1.6 schwarze 107: * - TERMP_DANGLE: don't newline when TERMP_NOBREAK is specified and
108: * the line is overrun, and don't pad-right if it's underrun.
109: *
110: * - TERMP_HANG: like TERMP_DANGLE, but doesn't newline when
111: * overruning, instead save the position and continue at that point
112: * when the next invocation.
1.1 kristaps 113: *
114: * In-line line breaking:
115: *
116: * If TERMP_NOBREAK is specified and the line overruns the right
117: * margin, it will break and pad-right to the right margin after
118: * writing. If maxrmargin is violated, it will break and continue
1.19 schwarze 119: * writing from the right-margin, which will lead to the above scenario
120: * upon exit. Otherwise, the line will break at the right margin.
1.1 kristaps 121: */
122: void
123: term_flushln(struct termp *p)
124: {
1.19 schwarze 125: int i; /* current input position in p->buf */
126: size_t vis; /* current visual position on output */
127: size_t vbl; /* number of blanks to prepend to output */
1.22 schwarze 128: size_t vend; /* end of word visual position on output */
1.19 schwarze 129: size_t bp; /* visual right border position */
130: int j; /* temporary loop index */
1.22 schwarze 131: int jhy; /* last hyphen before line overflow */
1.19 schwarze 132: size_t maxvis, mmax;
1.1 kristaps 133:
134: /*
135: * First, establish the maximum columns of "visible" content.
136: * This is usually the difference between the right-margin and
137: * an indentation, but can be, for tagged lists or columns, a
1.19 schwarze 138: * small set of values.
1.1 kristaps 139: */
140:
141: assert(p->offset < p->rmargin);
1.9 schwarze 142:
1.26 schwarze 143: maxvis = (int)(p->rmargin - p->offset) - p->overstep < 0 ?
1.19 schwarze 144: /* LINTED */
1.26 schwarze 145: 0 : p->rmargin - p->offset - p->overstep;
146: mmax = (int)(p->maxrmargin - p->offset) - p->overstep < 0 ?
1.19 schwarze 147: /* LINTED */
1.26 schwarze 148: 0 : p->maxrmargin - p->offset - p->overstep;
1.9 schwarze 149:
1.1 kristaps 150: bp = TERMP_NOBREAK & p->flags ? mmax : maxvis;
1.19 schwarze 151:
152: /*
153: * FIXME: if bp is zero, we still output the first word before
154: * breaking the line.
155: */
156:
1.22 schwarze 157: vis = vend = i = 0;
158: while (i < (int)p->col) {
159:
160: /*
161: * Choose the number of blanks to prepend: no blank at the
162: * beginning of a line, one between words -- but do not
163: * actually write them yet.
164: */
165: vbl = (size_t)(ASCII_EOS == p->buf[i] ? 2 :
166: (0 == vis ? 0 : 1));
167: vis += vbl;
168:
1.1 kristaps 169: /*
170: * Count up visible word characters. Control sequences
171: * (starting with the CSI) aren't counted. A space
172: * generates a non-printing word, which is valid (the
173: * space is printed according to regular spacing rules).
174: */
175:
176: /* LINTED */
1.22 schwarze 177: for (j = i, jhy = 0, vend = vis; j < (int)p->col; j++) {
1.10 schwarze 178: if (j && ' ' == p->buf[j])
1.1 kristaps 179: break;
180: else if (8 == p->buf[j])
1.22 schwarze 181: vend--;
182: else if (ASCII_EOS != p->buf[j]) {
183: if (vend > vis && vend < bp &&
184: '-' == p->buf[j])
185: jhy = j;
186: vend++;
187: }
1.1 kristaps 188: }
189:
190: /*
1.21 schwarze 191: * Skip empty words. This happens due to the ASCII_EOS
192: * after the end of the final sentence of a paragraph.
193: */
1.22 schwarze 194: if (vend == vis && j == (int)p->col)
1.21 schwarze 195: break;
196:
197: /*
1.23 schwarze 198: * Usually, indent the first line of each paragraph.
199: */
1.29 ! schwarze 200: if (0 == i && ! (p->flags & TERMP_NOLPAD)) {
! 201: p->viscol += p->offset;
1.23 schwarze 202: /* LINTED */
203: for (j = 0; j < (int)p->offset; j++)
204: putchar(' ');
1.29 ! schwarze 205: }
1.23 schwarze 206:
207: /*
1.5 schwarze 208: * Find out whether we would exceed the right margin.
209: * If so, break to the next line. (TODO: hyphenate)
210: * Otherwise, write the chosen number of blanks now.
211: */
1.24 schwarze 212: if (vend > bp && 0 == jhy && vis > vbl) {
1.22 schwarze 213: vend -= vis;
1.5 schwarze 214: putchar('\n');
215: if (TERMP_NOBREAK & p->flags) {
1.29 ! schwarze 216: p->viscol = p->rmargin;
1.5 schwarze 217: for (j = 0; j < (int)p->rmargin; j++)
218: putchar(' ');
1.22 schwarze 219: vend += p->rmargin - p->offset;
1.5 schwarze 220: } else {
1.29 ! schwarze 221: p->viscol = p->offset;
1.1 kristaps 222: for (j = 0; j < (int)p->offset; j++)
223: putchar(' ');
1.5 schwarze 224: }
1.26 schwarze 225: /* Remove the p->overstep width. */
1.18 schwarze 226: bp += (int)/* LINTED */
1.26 schwarze 227: p->overstep;
228: p->overstep = 0;
1.5 schwarze 229: } else {
1.29 ! schwarze 230: p->viscol += vbl;
1.5 schwarze 231: for (j = 0; j < (int)vbl; j++)
1.1 kristaps 232: putchar(' ');
233: }
234:
1.3 schwarze 235: /*
1.5 schwarze 236: * Finally, write out the word.
1.1 kristaps 237: */
238: for ( ; i < (int)p->col; i++) {
1.25 schwarze 239: if (vend > bp && jhy > 0 && i > jhy)
1.1 kristaps 240: break;
1.22 schwarze 241: if (' ' == p->buf[i]) {
242: i++;
243: break;
244: }
1.21 schwarze 245: if (ASCII_NBRSP == p->buf[i])
1.20 schwarze 246: putchar(' ');
1.21 schwarze 247: else if (ASCII_EOS != p->buf[i])
1.20 schwarze 248: putchar(p->buf[i]);
1.1 kristaps 249: }
1.29 ! schwarze 250: p->viscol += vend - vis;
1.22 schwarze 251: vis = vend;
1.1 kristaps 252: }
1.18 schwarze 253:
1.9 schwarze 254: p->col = 0;
1.26 schwarze 255: p->overstep = 0;
1.1 kristaps 256:
1.9 schwarze 257: if ( ! (TERMP_NOBREAK & p->flags)) {
1.29 ! schwarze 258: p->viscol = 0;
1.28 schwarze 259: putchar('\n');
1.1 kristaps 260: return;
261: }
262:
1.9 schwarze 263: if (TERMP_HANG & p->flags) {
264: /* We need one blank after the tag. */
1.26 schwarze 265: p->overstep = /* LINTED */
1.9 schwarze 266: vis - maxvis + 1;
267:
268: /*
269: * Behave exactly the same way as groff:
270: * If we have overstepped the margin, temporarily move
271: * it to the right and flag the rest of the line to be
272: * shorter.
273: * If we landed right at the margin, be happy.
274: * If we are one step before the margin, temporarily
275: * move it one step LEFT and flag the rest of the line
276: * to be longer.
277: */
1.26 schwarze 278: if (p->overstep >= -1) {
279: assert((int)maxvis + p->overstep >= 0);
1.9 schwarze 280: /* LINTED */
1.26 schwarze 281: maxvis += p->overstep;
1.9 schwarze 282: } else
1.26 schwarze 283: p->overstep = 0;
1.9 schwarze 284:
285: } else if (TERMP_DANGLE & p->flags)
286: return;
1.1 kristaps 287:
1.9 schwarze 288: /* Right-pad. */
289: if (maxvis > vis + /* LINTED */
1.29 ! schwarze 290: ((TERMP_TWOSPACE & p->flags) ? 1 : 0)) {
! 291: p->viscol += maxvis - vis;
1.9 schwarze 292: for ( ; vis < maxvis; vis++)
293: putchar(' ');
1.29 ! schwarze 294: } else { /* ...or newline break. */
1.1 kristaps 295: putchar('\n');
1.29 ! schwarze 296: p->viscol = p->rmargin;
1.9 schwarze 297: for (i = 0; i < (int)p->rmargin; i++)
298: putchar(' ');
299: }
1.1 kristaps 300: }
301:
302:
303: /*
304: * A newline only breaks an existing line; it won't assert vertical
305: * space. All data in the output buffer is flushed prior to the newline
306: * assertion.
307: */
308: void
309: term_newln(struct termp *p)
310: {
311:
312: p->flags |= TERMP_NOSPACE;
1.29 ! schwarze 313: if (0 == p->col && 0 == p->viscol) {
1.1 kristaps 314: p->flags &= ~TERMP_NOLPAD;
315: return;
316: }
317: term_flushln(p);
318: p->flags &= ~TERMP_NOLPAD;
319: }
320:
321:
322: /*
323: * Asserts a vertical space (a full, empty line-break between lines).
324: * Note that if used twice, this will cause two blank spaces and so on.
325: * All data in the output buffer is flushed prior to the newline
326: * assertion.
327: */
328: void
329: term_vspace(struct termp *p)
330: {
331:
332: term_newln(p);
1.29 ! schwarze 333: p->viscol = 0;
1.1 kristaps 334: putchar('\n');
335: }
336:
337:
338: static void
1.20 schwarze 339: spec(struct termp *p, const char *word, size_t len)
1.1 kristaps 340: {
341: const char *rhs;
342: size_t sz;
343:
1.15 schwarze 344: rhs = chars_a2ascii(p->symtab, word, len, &sz);
1.20 schwarze 345: if (rhs)
346: encode(p, rhs, sz);
1.11 schwarze 347: }
348:
349:
350: static void
1.20 schwarze 351: res(struct termp *p, const char *word, size_t len)
1.11 schwarze 352: {
353: const char *rhs;
354: size_t sz;
355:
1.15 schwarze 356: rhs = chars_a2res(p->symtab, word, len, &sz);
1.20 schwarze 357: if (rhs)
358: encode(p, rhs, sz);
359: }
360:
361:
362: void
363: term_fontlast(struct termp *p)
364: {
365: enum termfont f;
1.11 schwarze 366:
1.20 schwarze 367: f = p->fontl;
368: p->fontl = p->fontq[p->fonti];
369: p->fontq[p->fonti] = f;
370: }
371:
372:
373: void
374: term_fontrepl(struct termp *p, enum termfont f)
375: {
376:
377: p->fontl = p->fontq[p->fonti];
378: p->fontq[p->fonti] = f;
1.1 kristaps 379: }
380:
381:
1.20 schwarze 382: void
383: term_fontpush(struct termp *p, enum termfont f)
1.1 kristaps 384: {
1.7 schwarze 385:
1.20 schwarze 386: assert(p->fonti + 1 < 10);
387: p->fontl = p->fontq[p->fonti];
388: p->fontq[++p->fonti] = f;
389: }
1.1 kristaps 390:
391:
1.20 schwarze 392: const void *
393: term_fontq(struct termp *p)
394: {
1.1 kristaps 395:
1.20 schwarze 396: return(&p->fontq[p->fonti]);
397: }
1.1 kristaps 398:
399:
1.20 schwarze 400: enum termfont
401: term_fonttop(struct termp *p)
402: {
1.1 kristaps 403:
1.20 schwarze 404: return(p->fontq[p->fonti]);
405: }
1.7 schwarze 406:
407:
1.20 schwarze 408: void
409: term_fontpopq(struct termp *p, const void *key)
410: {
1.1 kristaps 411:
1.20 schwarze 412: while (p->fonti >= 0 && key != &p->fontq[p->fonti])
413: p->fonti--;
414: assert(p->fonti >= 0);
415: }
1.1 kristaps 416:
417:
1.20 schwarze 418: void
419: term_fontpop(struct termp *p)
420: {
1.1 kristaps 421:
1.20 schwarze 422: assert(p->fonti);
423: p->fonti--;
1.1 kristaps 424: }
425:
426:
427: /*
428: * Handle pwords, partial words, which may be either a single word or a
429: * phrase that cannot be broken down (such as a literal string). This
430: * handles word styling.
431: */
1.7 schwarze 432: void
433: term_word(struct termp *p, const char *word)
1.1 kristaps 434: {
1.20 schwarze 435: const char *sv, *seq;
436: int sz;
437: size_t ssz;
438: enum roffdeco deco;
1.1 kristaps 439:
1.14 schwarze 440: sv = word;
441:
1.20 schwarze 442: if (word[0] && '\0' == word[1])
1.14 schwarze 443: switch (word[0]) {
444: case('.'):
445: /* FALLTHROUGH */
446: case(','):
447: /* FALLTHROUGH */
448: case(';'):
449: /* FALLTHROUGH */
450: case(':'):
451: /* FALLTHROUGH */
452: case('?'):
453: /* FALLTHROUGH */
454: case('!'):
455: /* FALLTHROUGH */
456: case(')'):
457: /* FALLTHROUGH */
458: case(']'):
459: if ( ! (TERMP_IGNDELIM & p->flags))
460: p->flags |= TERMP_NOSPACE;
461: break;
462: default:
463: break;
464: }
1.1 kristaps 465:
466: if ( ! (TERMP_NOSPACE & p->flags))
1.20 schwarze 467: bufferc(p, ' ');
1.1 kristaps 468:
469: if ( ! (p->flags & TERMP_NONOSPACE))
470: p->flags &= ~TERMP_NOSPACE;
471:
1.20 schwarze 472: /* FIXME: use strcspn. */
473:
474: while (*word) {
475: if ('\\' != *word) {
476: encode(p, word, 1);
477: word++;
478: continue;
479: }
480:
481: seq = ++word;
482: sz = a2roffdeco(&deco, &seq, &ssz);
483:
484: switch (deco) {
485: case (DECO_RESERVED):
486: res(p, seq, ssz);
487: break;
488: case (DECO_SPECIAL):
489: spec(p, seq, ssz);
490: break;
491: case (DECO_BOLD):
492: term_fontrepl(p, TERMFONT_BOLD);
493: break;
494: case (DECO_ITALIC):
495: term_fontrepl(p, TERMFONT_UNDER);
496: break;
497: case (DECO_ROMAN):
498: term_fontrepl(p, TERMFONT_NONE);
499: break;
500: case (DECO_PREVIOUS):
501: term_fontlast(p);
502: break;
503: default:
504: break;
505: }
506:
507: word += sz;
508: if (DECO_NOSPACE == deco && '\0' == *word)
509: p->flags |= TERMP_NOSPACE;
510: }
1.1 kristaps 511:
1.14 schwarze 512: if (sv[0] && 0 == sv[1])
513: switch (sv[0]) {
514: case('('):
515: /* FALLTHROUGH */
516: case('['):
517: p->flags |= TERMP_NOSPACE;
518: break;
519: default:
520: break;
521: }
1.1 kristaps 522: }
523:
524:
525: static void
1.20 schwarze 526: adjbuf(struct termp *p, size_t sz)
1.1 kristaps 527: {
528:
1.20 schwarze 529: if (0 == p->maxcols)
530: p->maxcols = 1024;
531: while (sz >= p->maxcols)
532: p->maxcols <<= 2;
533:
534: p->buf = realloc(p->buf, p->maxcols);
535: if (NULL == p->buf) {
536: perror(NULL);
537: exit(EXIT_FAILURE);
1.1 kristaps 538: }
539: }
540:
1.4 schwarze 541:
542: static void
1.20 schwarze 543: buffera(struct termp *p, const char *word, size_t sz)
544: {
545:
546: if (p->col + sz >= p->maxcols)
547: adjbuf(p, p->col + sz);
548:
549: memcpy(&p->buf[(int)p->col], word, sz);
550: p->col += sz;
551: }
552:
553:
554: static void
555: bufferc(struct termp *p, char c)
556: {
557:
558: if (p->col + 1 >= p->maxcols)
559: adjbuf(p, p->col + 1);
560:
561: p->buf[(int)p->col++] = c;
562: }
563:
564:
565: static void
566: encode(struct termp *p, const char *word, size_t sz)
1.4 schwarze 567: {
1.20 schwarze 568: enum termfont f;
569: int i;
570:
571: /*
572: * Encode and buffer a string of characters. If the current
573: * font mode is unset, buffer directly, else encode then buffer
574: * character by character.
575: */
576:
577: if (TERMFONT_NONE == (f = term_fonttop(p))) {
578: buffera(p, word, sz);
579: return;
580: }
581:
582: for (i = 0; i < (int)sz; i++) {
583: if ( ! isgraph((u_char)word[i])) {
584: bufferc(p, word[i]);
585: continue;
1.4 schwarze 586: }
1.20 schwarze 587:
588: if (TERMFONT_UNDER == f)
589: bufferc(p, '_');
590: else
591: bufferc(p, word[i]);
592:
593: bufferc(p, 8);
594: bufferc(p, word[i]);
1.4 schwarze 595: }
596: }
1.16 schwarze 597:
598:
599: size_t
600: term_vspan(const struct roffsu *su)
601: {
602: double r;
603:
604: switch (su->unit) {
605: case (SCALE_CM):
606: r = su->scale * 2;
607: break;
608: case (SCALE_IN):
609: r = su->scale * 6;
610: break;
611: case (SCALE_PC):
612: r = su->scale;
613: break;
614: case (SCALE_PT):
615: r = su->scale / 8;
616: break;
617: case (SCALE_MM):
618: r = su->scale / 1000;
619: break;
620: case (SCALE_VS):
621: r = su->scale;
622: break;
623: default:
624: r = su->scale - 1;
625: break;
626: }
627:
628: if (r < 0.0)
629: r = 0.0;
630: return(/* LINTED */(size_t)
631: r);
632: }
633:
634:
635: size_t
636: term_hspan(const struct roffsu *su)
637: {
638: double r;
639:
640: /* XXX: CM, IN, and PT are approximations. */
641:
642: switch (su->unit) {
643: case (SCALE_CM):
644: r = 4 * su->scale;
645: break;
646: case (SCALE_IN):
647: /* XXX: this is an approximation. */
648: r = 10 * su->scale;
649: break;
650: case (SCALE_PC):
651: r = (10 * su->scale) / 6;
652: break;
653: case (SCALE_PT):
654: r = (10 * su->scale) / 72;
655: break;
656: case (SCALE_MM):
657: r = su->scale / 1000; /* FIXME: double-check. */
658: break;
659: case (SCALE_VS):
660: r = su->scale * 2 - 1; /* FIXME: double-check. */
661: break;
662: default:
663: r = su->scale;
664: break;
665: }
666:
667: if (r < 0.0)
668: r = 0.0;
669: return((size_t)/* LINTED */
670: r);
671: }
672:
673: