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