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