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