Annotation of src/usr.bin/mandoc/term.c, Revision 1.34
1.34 ! schwarze 1: /* $Id: term.c,v 1.33 2010/05/17 02:25:42 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.33 schwarze 193: if (8 == p->buf[j])
1.22 schwarze 194: vend--;
1.31 schwarze 195: else {
1.22 schwarze 196: if (vend > vis && vend < bp &&
197: '-' == p->buf[j])
198: jhy = j;
199: vend++;
200: }
1.1 kristaps 201: }
202:
203: /*
1.5 schwarze 204: * Find out whether we would exceed the right margin.
1.33 schwarze 205: * If so, break to the next line.
1.5 schwarze 206: */
1.33 schwarze 207: if (vend > bp && 0 == jhy && vis > 0) {
1.22 schwarze 208: vend -= vis;
1.5 schwarze 209: putchar('\n');
210: if (TERMP_NOBREAK & p->flags) {
1.29 schwarze 211: p->viscol = p->rmargin;
1.5 schwarze 212: for (j = 0; j < (int)p->rmargin; j++)
213: putchar(' ');
1.22 schwarze 214: vend += p->rmargin - p->offset;
1.5 schwarze 215: } else {
1.33 schwarze 216: p->viscol = 0;
217: vbl = p->offset;
1.5 schwarze 218: }
1.33 schwarze 219:
1.26 schwarze 220: /* Remove the p->overstep width. */
1.33 schwarze 221:
1.18 schwarze 222: bp += (int)/* LINTED */
1.26 schwarze 223: p->overstep;
224: p->overstep = 0;
1.1 kristaps 225: }
226:
1.3 schwarze 227: /*
1.30 schwarze 228: * Skip leading tabs, they were handled above.
229: */
230: while (i < (int)p->col && '\t' == p->buf[i])
231: i++;
232:
1.33 schwarze 233: /* Write out the [remaining] word. */
1.1 kristaps 234: for ( ; i < (int)p->col; i++) {
1.25 schwarze 235: if (vend > bp && jhy > 0 && i > jhy)
1.30 schwarze 236: break;
237: if ('\t' == p->buf[i])
1.1 kristaps 238: break;
1.22 schwarze 239: if (' ' == p->buf[i]) {
1.33 schwarze 240: while (' ' == p->buf[i]) {
241: vbl++;
242: i++;
243: }
1.22 schwarze 244: break;
245: }
1.33 schwarze 246: if (ASCII_NBRSP == p->buf[i]) {
247: vbl++;
248: continue;
249: }
250:
251: /*
252: * Now we definitely know there will be
253: * printable characters to output,
254: * so write preceding white space now.
255: */
256: if (vbl) {
257: for (j = 0; j < (int)vbl; j++)
258: putchar(' ');
259: p->viscol += vbl;
260: vbl = 0;
261: }
262: putchar(p->buf[i]);
263: p->viscol += 1;
1.1 kristaps 264: }
1.33 schwarze 265: vend += vbl;
1.22 schwarze 266: vis = vend;
1.1 kristaps 267: }
1.18 schwarze 268:
1.9 schwarze 269: p->col = 0;
1.26 schwarze 270: p->overstep = 0;
1.1 kristaps 271:
1.9 schwarze 272: if ( ! (TERMP_NOBREAK & p->flags)) {
1.29 schwarze 273: p->viscol = 0;
1.28 schwarze 274: putchar('\n');
1.1 kristaps 275: return;
276: }
277:
1.9 schwarze 278: if (TERMP_HANG & p->flags) {
279: /* We need one blank after the tag. */
1.26 schwarze 280: p->overstep = /* LINTED */
1.9 schwarze 281: vis - maxvis + 1;
282:
283: /*
284: * Behave exactly the same way as groff:
285: * If we have overstepped the margin, temporarily move
286: * it to the right and flag the rest of the line to be
287: * shorter.
288: * If we landed right at the margin, be happy.
289: * If we are one step before the margin, temporarily
290: * move it one step LEFT and flag the rest of the line
291: * to be longer.
292: */
1.26 schwarze 293: if (p->overstep >= -1) {
294: assert((int)maxvis + p->overstep >= 0);
1.9 schwarze 295: /* LINTED */
1.26 schwarze 296: maxvis += p->overstep;
1.9 schwarze 297: } else
1.26 schwarze 298: p->overstep = 0;
1.9 schwarze 299:
300: } else if (TERMP_DANGLE & p->flags)
301: return;
1.1 kristaps 302:
1.9 schwarze 303: /* Right-pad. */
304: if (maxvis > vis + /* LINTED */
1.29 schwarze 305: ((TERMP_TWOSPACE & p->flags) ? 1 : 0)) {
306: p->viscol += maxvis - vis;
1.9 schwarze 307: for ( ; vis < maxvis; vis++)
308: putchar(' ');
1.29 schwarze 309: } else { /* ...or newline break. */
1.1 kristaps 310: putchar('\n');
1.29 schwarze 311: p->viscol = p->rmargin;
1.9 schwarze 312: for (i = 0; i < (int)p->rmargin; i++)
313: putchar(' ');
314: }
1.1 kristaps 315: }
316:
317:
318: /*
319: * A newline only breaks an existing line; it won't assert vertical
320: * space. All data in the output buffer is flushed prior to the newline
321: * assertion.
322: */
323: void
324: term_newln(struct termp *p)
325: {
326:
327: p->flags |= TERMP_NOSPACE;
1.29 schwarze 328: if (0 == p->col && 0 == p->viscol) {
1.1 kristaps 329: p->flags &= ~TERMP_NOLPAD;
330: return;
331: }
332: term_flushln(p);
333: p->flags &= ~TERMP_NOLPAD;
334: }
335:
336:
337: /*
338: * Asserts a vertical space (a full, empty line-break between lines).
339: * Note that if used twice, this will cause two blank spaces and so on.
340: * All data in the output buffer is flushed prior to the newline
341: * assertion.
342: */
343: void
344: term_vspace(struct termp *p)
345: {
346:
347: term_newln(p);
1.29 schwarze 348: p->viscol = 0;
1.1 kristaps 349: putchar('\n');
350: }
351:
352:
353: static void
1.20 schwarze 354: spec(struct termp *p, const char *word, size_t len)
1.1 kristaps 355: {
356: const char *rhs;
357: size_t sz;
358:
1.15 schwarze 359: rhs = chars_a2ascii(p->symtab, word, len, &sz);
1.20 schwarze 360: if (rhs)
361: encode(p, rhs, sz);
1.11 schwarze 362: }
363:
364:
365: static void
1.20 schwarze 366: res(struct termp *p, const char *word, size_t len)
1.11 schwarze 367: {
368: const char *rhs;
369: size_t sz;
370:
1.15 schwarze 371: rhs = chars_a2res(p->symtab, word, len, &sz);
1.20 schwarze 372: if (rhs)
373: encode(p, rhs, sz);
374: }
375:
376:
377: void
378: term_fontlast(struct termp *p)
379: {
380: enum termfont f;
1.11 schwarze 381:
1.20 schwarze 382: f = p->fontl;
383: p->fontl = p->fontq[p->fonti];
384: p->fontq[p->fonti] = f;
385: }
386:
387:
388: void
389: term_fontrepl(struct termp *p, enum termfont f)
390: {
391:
392: p->fontl = p->fontq[p->fonti];
393: p->fontq[p->fonti] = f;
1.1 kristaps 394: }
395:
396:
1.20 schwarze 397: void
398: term_fontpush(struct termp *p, enum termfont f)
1.1 kristaps 399: {
1.7 schwarze 400:
1.20 schwarze 401: assert(p->fonti + 1 < 10);
402: p->fontl = p->fontq[p->fonti];
403: p->fontq[++p->fonti] = f;
404: }
1.1 kristaps 405:
406:
1.20 schwarze 407: const void *
408: term_fontq(struct termp *p)
409: {
1.1 kristaps 410:
1.20 schwarze 411: return(&p->fontq[p->fonti]);
412: }
1.1 kristaps 413:
414:
1.20 schwarze 415: enum termfont
416: term_fonttop(struct termp *p)
417: {
1.1 kristaps 418:
1.20 schwarze 419: return(p->fontq[p->fonti]);
420: }
1.7 schwarze 421:
422:
1.20 schwarze 423: void
424: term_fontpopq(struct termp *p, const void *key)
425: {
1.1 kristaps 426:
1.20 schwarze 427: while (p->fonti >= 0 && key != &p->fontq[p->fonti])
428: p->fonti--;
429: assert(p->fonti >= 0);
430: }
1.1 kristaps 431:
432:
1.20 schwarze 433: void
434: term_fontpop(struct termp *p)
435: {
1.1 kristaps 436:
1.20 schwarze 437: assert(p->fonti);
438: p->fonti--;
1.1 kristaps 439: }
440:
441:
442: /*
443: * Handle pwords, partial words, which may be either a single word or a
444: * phrase that cannot be broken down (such as a literal string). This
445: * handles word styling.
446: */
1.7 schwarze 447: void
448: term_word(struct termp *p, const char *word)
1.1 kristaps 449: {
1.20 schwarze 450: const char *sv, *seq;
451: int sz;
452: size_t ssz;
453: enum roffdeco deco;
1.1 kristaps 454:
1.14 schwarze 455: sv = word;
456:
1.20 schwarze 457: if (word[0] && '\0' == word[1])
1.14 schwarze 458: switch (word[0]) {
459: case('.'):
460: /* FALLTHROUGH */
461: case(','):
462: /* FALLTHROUGH */
463: case(';'):
464: /* FALLTHROUGH */
465: case(':'):
466: /* FALLTHROUGH */
467: case('?'):
468: /* FALLTHROUGH */
469: case('!'):
470: /* FALLTHROUGH */
471: case(')'):
472: /* FALLTHROUGH */
473: case(']'):
474: if ( ! (TERMP_IGNDELIM & p->flags))
475: p->flags |= TERMP_NOSPACE;
476: break;
477: default:
478: break;
479: }
1.1 kristaps 480:
1.31 schwarze 481: if ( ! (TERMP_NOSPACE & p->flags)) {
1.20 schwarze 482: bufferc(p, ' ');
1.31 schwarze 483: if (TERMP_SENTENCE & p->flags)
484: bufferc(p, ' ');
485: }
1.1 kristaps 486:
487: if ( ! (p->flags & TERMP_NONOSPACE))
488: p->flags &= ~TERMP_NOSPACE;
489:
1.31 schwarze 490: p->flags &= ~TERMP_SENTENCE;
491:
1.20 schwarze 492: /* FIXME: use strcspn. */
493:
494: while (*word) {
495: if ('\\' != *word) {
496: encode(p, word, 1);
497: word++;
498: continue;
499: }
500:
501: seq = ++word;
502: sz = a2roffdeco(&deco, &seq, &ssz);
503:
504: switch (deco) {
505: case (DECO_RESERVED):
506: res(p, seq, ssz);
507: break;
508: case (DECO_SPECIAL):
509: spec(p, seq, ssz);
510: break;
511: case (DECO_BOLD):
512: term_fontrepl(p, TERMFONT_BOLD);
513: break;
514: case (DECO_ITALIC):
515: term_fontrepl(p, TERMFONT_UNDER);
516: break;
517: case (DECO_ROMAN):
518: term_fontrepl(p, TERMFONT_NONE);
519: break;
520: case (DECO_PREVIOUS):
521: term_fontlast(p);
522: break;
523: default:
524: break;
525: }
526:
527: word += sz;
528: if (DECO_NOSPACE == deco && '\0' == *word)
529: p->flags |= TERMP_NOSPACE;
530: }
1.1 kristaps 531:
1.31 schwarze 532: /*
533: * Note that we don't process the pipe: the parser sees it as
534: * punctuation, but we don't in terms of typography.
535: */
1.14 schwarze 536: if (sv[0] && 0 == sv[1])
537: switch (sv[0]) {
538: case('('):
539: /* FALLTHROUGH */
540: case('['):
541: p->flags |= TERMP_NOSPACE;
542: break;
543: default:
544: break;
545: }
1.1 kristaps 546: }
547:
548:
549: static void
1.20 schwarze 550: adjbuf(struct termp *p, size_t sz)
1.1 kristaps 551: {
552:
1.20 schwarze 553: if (0 == p->maxcols)
554: p->maxcols = 1024;
555: while (sz >= p->maxcols)
556: p->maxcols <<= 2;
557:
558: p->buf = realloc(p->buf, p->maxcols);
559: if (NULL == p->buf) {
560: perror(NULL);
561: exit(EXIT_FAILURE);
1.1 kristaps 562: }
563: }
564:
1.4 schwarze 565:
566: static void
1.20 schwarze 567: buffera(struct termp *p, const char *word, size_t sz)
568: {
569:
570: if (p->col + sz >= p->maxcols)
571: adjbuf(p, p->col + sz);
572:
573: memcpy(&p->buf[(int)p->col], word, sz);
574: p->col += sz;
575: }
576:
577:
578: static void
579: bufferc(struct termp *p, char c)
580: {
581:
582: if (p->col + 1 >= p->maxcols)
583: adjbuf(p, p->col + 1);
584:
585: p->buf[(int)p->col++] = c;
586: }
587:
588:
589: static void
590: encode(struct termp *p, const char *word, size_t sz)
1.4 schwarze 591: {
1.20 schwarze 592: enum termfont f;
593: int i;
594:
595: /*
596: * Encode and buffer a string of characters. If the current
597: * font mode is unset, buffer directly, else encode then buffer
598: * character by character.
599: */
600:
601: if (TERMFONT_NONE == (f = term_fonttop(p))) {
602: buffera(p, word, sz);
603: return;
604: }
605:
606: for (i = 0; i < (int)sz; i++) {
607: if ( ! isgraph((u_char)word[i])) {
608: bufferc(p, word[i]);
609: continue;
1.4 schwarze 610: }
1.20 schwarze 611:
612: if (TERMFONT_UNDER == f)
613: bufferc(p, '_');
614: else
615: bufferc(p, word[i]);
616:
617: bufferc(p, 8);
618: bufferc(p, word[i]);
1.4 schwarze 619: }
620: }
1.16 schwarze 621:
622:
623: size_t
624: term_vspan(const struct roffsu *su)
625: {
626: double r;
627:
628: switch (su->unit) {
629: case (SCALE_CM):
630: r = su->scale * 2;
631: break;
632: case (SCALE_IN):
633: r = su->scale * 6;
634: break;
635: case (SCALE_PC):
636: r = su->scale;
637: break;
638: case (SCALE_PT):
639: r = su->scale / 8;
640: break;
641: case (SCALE_MM):
642: r = su->scale / 1000;
643: break;
644: case (SCALE_VS):
645: r = su->scale;
646: break;
647: default:
648: r = su->scale - 1;
649: break;
650: }
651:
652: if (r < 0.0)
653: r = 0.0;
654: return(/* LINTED */(size_t)
655: r);
656: }
657:
658:
659: size_t
660: term_hspan(const struct roffsu *su)
661: {
662: double r;
663:
664: /* XXX: CM, IN, and PT are approximations. */
665:
666: switch (su->unit) {
667: case (SCALE_CM):
668: r = 4 * su->scale;
669: break;
670: case (SCALE_IN):
671: /* XXX: this is an approximation. */
672: r = 10 * su->scale;
673: break;
674: case (SCALE_PC):
675: r = (10 * su->scale) / 6;
676: break;
677: case (SCALE_PT):
678: r = (10 * su->scale) / 72;
679: break;
680: case (SCALE_MM):
681: r = su->scale / 1000; /* FIXME: double-check. */
682: break;
683: case (SCALE_VS):
684: r = su->scale * 2 - 1; /* FIXME: double-check. */
685: break;
686: default:
687: r = su->scale;
688: break;
689: }
690:
691: if (r < 0.0)
692: r = 0.0;
693: return((size_t)/* LINTED */
694: r);
695: }
696:
697: