]> git.cameronkatri.com Git - mandoc.git/blob - term.c
Initial abstraction of front-end decoration events (special characters, text decorati...
[mandoc.git] / term.c
1 /* $Id: term.c,v 1.123 2009/11/06 10:31:32 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
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.
16 */
17 #include <assert.h>
18 #include <ctype.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <time.h>
23
24 #include "chars.h"
25 #include "out.h"
26 #include "term.h"
27 #include "man.h"
28 #include "mdoc.h"
29 #include "main.h"
30
31 /* FIXME: accomodate non-breaking, non-collapsing white-space. */
32 /* FIXME: accomodate non-breaking, collapsing white-space. */
33
34 static struct termp *term_alloc(enum termenc);
35 static void term_free(struct termp *);
36
37 static void do_escaped(struct termp *, const char **);
38 static void do_special(struct termp *,
39 const char *, size_t);
40 static void do_reserved(struct termp *,
41 const char *, size_t);
42 static void buffer(struct termp *, char);
43 static void encode(struct termp *, char);
44
45
46 void *
47 ascii_alloc(void)
48 {
49
50 return(term_alloc(TERMENC_ASCII));
51 }
52
53
54 void
55 terminal_free(void *arg)
56 {
57
58 term_free((struct termp *)arg);
59 }
60
61
62 static void
63 term_free(struct termp *p)
64 {
65
66 if (p->buf)
67 free(p->buf);
68 if (p->symtab)
69 chars_free(p->symtab);
70
71 free(p);
72 }
73
74
75 static struct termp *
76 term_alloc(enum termenc enc)
77 {
78 struct termp *p;
79
80 p = calloc(1, sizeof(struct termp));
81 if (NULL == p) {
82 perror(NULL);
83 exit(EXIT_FAILURE);
84 }
85 p->maxrmargin = 78;
86 p->enc = enc;
87 return(p);
88 }
89
90
91 /*
92 * Flush a line of text. A "line" is loosely defined as being something
93 * that should be followed by a newline, regardless of whether it's
94 * broken apart by newlines getting there. A line can also be a
95 * fragment of a columnar list.
96 *
97 * Specifically, a line is whatever's in p->buf of length p->col, which
98 * is zeroed after this function returns.
99 *
100 * The usage of termp:flags is as follows:
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 *
110 * - TERMP_TWOSPACE: when padding, make sure there are at least two
111 * space characters of padding. Otherwise, rather break the line.
112 *
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.
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
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.
127 */
128 void
129 term_flushln(struct termp *p)
130 {
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 */
134 size_t vsz; /* visual characters to write to output */
135 size_t bp; /* visual right border position */
136 int j; /* temporary loop index */
137 size_t maxvis, mmax;
138 static int overstep = 0;
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
144 * small set of values.
145 */
146
147 assert(p->offset < p->rmargin);
148
149 maxvis = (int)(p->rmargin - p->offset) - overstep < 0 ?
150 /* LINTED */
151 0 : p->rmargin - p->offset - overstep;
152 mmax = (int)(p->maxrmargin - p->offset) - overstep < 0 ?
153 /* LINTED */
154 0 : p->maxrmargin - p->offset - overstep;
155
156 bp = TERMP_NOBREAK & p->flags ? mmax : maxvis;
157
158 /*
159 * FIXME: if bp is zero, we still output the first word before
160 * breaking the line.
161 */
162
163 vis = 0;
164
165 /*
166 * If in the standard case (left-justified), then begin with our
167 * indentation, otherwise (columns, etc.) just start spitting
168 * out text.
169 */
170
171 if ( ! (p->flags & TERMP_NOLPAD))
172 /* LINTED */
173 for (j = 0; j < (int)p->offset; j++)
174 putchar(' ');
175
176 for (i = 0; i < (int)p->col; i++) {
177 /*
178 * Count up visible word characters. Control sequences
179 * (starting with the CSI) aren't counted. A space
180 * generates a non-printing word, which is valid (the
181 * space is printed according to regular spacing rules).
182 */
183
184 /* LINTED */
185 for (j = i, vsz = 0; j < (int)p->col; j++) {
186 if (j && ' ' == p->buf[j])
187 break;
188 else if (8 == p->buf[j])
189 vsz--;
190 else
191 vsz++;
192 }
193
194 /*
195 * Choose the number of blanks to prepend: no blank at the
196 * beginning of a line, one between words -- but do not
197 * actually write them yet.
198 */
199 vbl = (size_t)(0 == vis ? 0 : 1);
200
201 /*
202 * Find out whether we would exceed the right margin.
203 * If so, break to the next line. (TODO: hyphenate)
204 * Otherwise, write the chosen number of blanks now.
205 */
206 if (vis && vis + vbl + vsz > bp) {
207 putchar('\n');
208 if (TERMP_NOBREAK & p->flags) {
209 for (j = 0; j < (int)p->rmargin; j++)
210 putchar(' ');
211 vis = p->rmargin - p->offset;
212 } else {
213 for (j = 0; j < (int)p->offset; j++)
214 putchar(' ');
215 vis = 0;
216 }
217 /* Remove the overstep width. */
218 bp += (int)/* LINTED */
219 overstep;
220 overstep = 0;
221 } else {
222 for (j = 0; j < (int)vbl; j++)
223 putchar(' ');
224 vis += vbl;
225 }
226
227 /*
228 * Finally, write out the word.
229 */
230 for ( ; i < (int)p->col; i++) {
231 if (' ' == p->buf[i])
232 break;
233
234 /* The unit sep. is a non-breaking space. */
235 if (31 == p->buf[i])
236 putchar(' ');
237 else
238 putchar(p->buf[i]);
239 }
240 vis += vsz;
241 }
242
243 p->col = 0;
244 overstep = 0;
245
246 if ( ! (TERMP_NOBREAK & p->flags)) {
247 putchar('\n');
248 return;
249 }
250
251 if (TERMP_HANG & p->flags) {
252 /* We need one blank after the tag. */
253 overstep = /* LINTED */
254 vis - maxvis + 1;
255
256 /*
257 * Behave exactly the same way as groff:
258 * If we have overstepped the margin, temporarily move
259 * it to the right and flag the rest of the line to be
260 * shorter.
261 * If we landed right at the margin, be happy.
262 * If we are one step before the margin, temporarily
263 * move it one step LEFT and flag the rest of the line
264 * to be longer.
265 */
266 if (overstep >= -1) {
267 assert((int)maxvis + overstep >= 0);
268 /* LINTED */
269 maxvis += overstep;
270 } else
271 overstep = 0;
272
273 } else if (TERMP_DANGLE & p->flags)
274 return;
275
276 /* Right-pad. */
277 if (maxvis > vis + /* LINTED */
278 ((TERMP_TWOSPACE & p->flags) ? 1 : 0))
279 for ( ; vis < maxvis; vis++)
280 putchar(' ');
281 else { /* ...or newline break. */
282 putchar('\n');
283 for (i = 0; i < (int)p->rmargin; i++)
284 putchar(' ');
285 }
286 }
287
288
289 /*
290 * A newline only breaks an existing line; it won't assert vertical
291 * space. All data in the output buffer is flushed prior to the newline
292 * assertion.
293 */
294 void
295 term_newln(struct termp *p)
296 {
297
298 p->flags |= TERMP_NOSPACE;
299 if (0 == p->col) {
300 p->flags &= ~TERMP_NOLPAD;
301 return;
302 }
303 term_flushln(p);
304 p->flags &= ~TERMP_NOLPAD;
305 }
306
307
308 /*
309 * Asserts a vertical space (a full, empty line-break between lines).
310 * Note that if used twice, this will cause two blank spaces and so on.
311 * All data in the output buffer is flushed prior to the newline
312 * assertion.
313 */
314 void
315 term_vspace(struct termp *p)
316 {
317
318 term_newln(p);
319 putchar('\n');
320 }
321
322
323 static void
324 do_special(struct termp *p, const char *word, size_t len)
325 {
326 const char *rhs;
327 size_t sz;
328 int i;
329
330 rhs = chars_a2ascii(p->symtab, word, len, &sz);
331
332 if (NULL == rhs) {
333 #if 0
334 fputs("Unknown special character: ", stderr);
335 for (i = 0; i < (int)len; i++)
336 fputc(word[i], stderr);
337 fputc('\n', stderr);
338 #endif
339 return;
340 }
341 for (i = 0; i < (int)sz; i++)
342 encode(p, rhs[i]);
343 }
344
345
346 static void
347 do_reserved(struct termp *p, const char *word, size_t len)
348 {
349 const char *rhs;
350 size_t sz;
351 int i;
352
353 rhs = chars_a2res(p->symtab, word, len, &sz);
354
355 if (NULL == rhs) {
356 #if 0
357 fputs("Unknown reserved word: ", stderr);
358 for (i = 0; i < (int)len; i++)
359 fputc(word[i], stderr);
360 fputc('\n', stderr);
361 #endif
362 return;
363 }
364 for (i = 0; i < (int)sz; i++)
365 encode(p, rhs[i]);
366 }
367
368
369 /*
370 * Handle an escape sequence: determine its length and pass it to the
371 * escape-symbol look table. Note that we assume mdoc(3) has validated
372 * the escape sequence (we assert upon badly-formed escape sequences).
373 */
374 static void
375 do_escaped(struct termp *p, const char **word)
376 {
377 int j, type, sv, t, lim;
378 const char *wp;
379
380 wp = *word;
381 type = 1;
382
383 if ('\0' == *(++wp)) {
384 *word = wp;
385 return;
386 }
387
388 if ('(' == *wp) {
389 wp++;
390 if ('\0' == *wp || '\0' == *(wp + 1)) {
391 *word = '\0' == *wp ? wp : wp + 1;
392 return;
393 }
394
395 do_special(p, wp, 2);
396 *word = ++wp;
397 return;
398
399 } else if ('*' == *wp) {
400 if ('\0' == *(++wp)) {
401 *word = wp;
402 return;
403 }
404
405 switch (*wp) {
406 case ('('):
407 wp++;
408 if ('\0' == *wp || '\0' == *(wp + 1)) {
409 *word = '\0' == *wp ? wp : wp + 1;
410 return;
411 }
412
413 do_reserved(p, wp, 2);
414 *word = ++wp;
415 return;
416 case ('['):
417 type = 0;
418 break;
419 default:
420 do_reserved(p, wp, 1);
421 *word = wp;
422 return;
423 }
424
425 } else if ('s' == *wp) {
426 /* This closely follows mandoc_special(). */
427 if ('\0' == *(++wp)) {
428 *word = wp;
429 return;
430 }
431
432 t = 0;
433 lim = 1;
434
435 if (*wp == '\'') {
436 lim = 0;
437 t = 1;
438 ++wp;
439 } else if (*wp == '[') {
440 lim = 0;
441 t = 2;
442 ++wp;
443 } else if (*wp == '(') {
444 lim = 2;
445 t = 3;
446 ++wp;
447 }
448
449 if (*wp == '+' || *wp == '-')
450 ++wp;
451
452 if (*wp == '\'') {
453 if (t) {
454 *word = wp;
455 return;
456 }
457 lim = 0;
458 t = 1;
459 ++wp;
460 } else if (*wp == '[') {
461 if (t) {
462 *word = wp;
463 return;
464 }
465 lim = 0;
466 t = 2;
467 ++wp;
468 } else if (*wp == '(') {
469 if (t) {
470 *word = wp;
471 return;
472 }
473 lim = 2;
474 t = 3;
475 ++wp;
476 }
477
478 if ( ! isdigit((u_char)*wp)) {
479 *word = --wp;
480 return;
481 }
482
483 for (j = 0; isdigit((u_char)*wp); j++) {
484 if (lim && j >= lim)
485 break;
486 ++wp;
487 }
488
489 if (t && t < 3) {
490 if (1 == t && *wp != '\'') {
491 *word = --wp;
492 return;
493 }
494 if (2 == t && *wp != ']') {
495 *word = --wp;
496 return;
497 }
498 ++wp;
499 }
500 *word = --wp;
501 return;
502
503 } else if ('f' == *wp) {
504 if ('\0' == *(++wp)) {
505 *word = wp;
506 return;
507 }
508
509 switch (*wp) {
510 case ('3'):
511 /* FALLTHROUGH */
512 case ('B'):
513 p->metamask = p->metafont;
514 p->metafont |= METAF_BOLD;
515 break;
516 case ('2'):
517 /* FALLTHROUGH */
518 case ('I'):
519 p->metamask = p->metafont;
520 p->metafont |= METAF_UNDER;
521 break;
522 case ('P'):
523 sv = p->metamask;
524 p->metamask = p->metafont;
525 p->metafont = sv;
526 break;
527 case ('1'):
528 /* FALLTHROUGH */
529 case ('R'):
530 p->metamask = p->metafont;
531 p->metafont &= ~METAF_UNDER;
532 p->metafont &= ~METAF_BOLD;
533 break;
534 default:
535 break;
536 }
537
538 *word = wp;
539 return;
540
541 } else if ('[' != *wp) {
542 do_special(p, wp, 1);
543 *word = wp;
544 return;
545 }
546
547 wp++;
548 for (j = 0; *wp && ']' != *wp; wp++, j++)
549 /* Loop... */ ;
550
551 if ('\0' == *wp) {
552 *word = wp;
553 return;
554 }
555
556 if (type)
557 do_special(p, wp - j, (size_t)j);
558 else
559 do_reserved(p, wp - j, (size_t)j);
560 *word = wp;
561 }
562
563
564 /*
565 * Handle pwords, partial words, which may be either a single word or a
566 * phrase that cannot be broken down (such as a literal string). This
567 * handles word styling.
568 */
569 void
570 term_word(struct termp *p, const char *word)
571 {
572 const char *sv;
573
574 sv = word;
575
576 if (word[0] && '\0' == word[1])
577 switch (word[0]) {
578 case('.'):
579 /* FALLTHROUGH */
580 case(','):
581 /* FALLTHROUGH */
582 case(';'):
583 /* FALLTHROUGH */
584 case(':'):
585 /* FALLTHROUGH */
586 case('?'):
587 /* FALLTHROUGH */
588 case('!'):
589 /* FALLTHROUGH */
590 case(')'):
591 /* FALLTHROUGH */
592 case(']'):
593 /* FALLTHROUGH */
594 case('}'):
595 if ( ! (TERMP_IGNDELIM & p->flags))
596 p->flags |= TERMP_NOSPACE;
597 break;
598 default:
599 break;
600 }
601
602 if ( ! (TERMP_NOSPACE & p->flags))
603 buffer(p, ' ');
604
605 if ( ! (p->flags & TERMP_NONOSPACE))
606 p->flags &= ~TERMP_NOSPACE;
607
608 for ( ; *word; word++)
609 if ('\\' != *word)
610 encode(p, *word);
611 else
612 do_escaped(p, &word);
613
614 if (sv[0] && 0 == sv[1])
615 switch (sv[0]) {
616 case('('):
617 /* FALLTHROUGH */
618 case('['):
619 /* FALLTHROUGH */
620 case('{'):
621 p->flags |= TERMP_NOSPACE;
622 break;
623 default:
624 break;
625 }
626 }
627
628
629 /*
630 * Insert a single character into the line-buffer. If the buffer's
631 * space is exceeded, then allocate more space by doubling the buffer
632 * size.
633 */
634 static void
635 buffer(struct termp *p, char c)
636 {
637 size_t s;
638
639 if (p->col + 1 >= p->maxcols) {
640 if (0 == p->maxcols)
641 p->maxcols = 256;
642 s = p->maxcols * 2;
643 p->buf = realloc(p->buf, s);
644 if (NULL == p->buf) {
645 perror(NULL);
646 exit(EXIT_FAILURE);
647 }
648 p->maxcols = s;
649 }
650 p->buf[(int)(p->col)++] = c;
651 }
652
653
654 static void
655 encode(struct termp *p, char c)
656 {
657
658 if (isgraph((u_char)c)) {
659 if (p->under || METAF_UNDER & p->metafont) {
660 buffer(p, '_');
661 buffer(p, 8);
662 }
663 if (p->bold || METAF_BOLD & p->metafont) {
664 buffer(p, c);
665 buffer(p, 8);
666 }
667 }
668 buffer(p, c);
669 }
670
671
672 size_t
673 term_vspan(const struct roffsu *su)
674 {
675 double r;
676
677 switch (su->unit) {
678 case (SCALE_CM):
679 r = su->scale * 2;
680 break;
681 case (SCALE_IN):
682 r = su->scale * 6;
683 break;
684 case (SCALE_PC):
685 r = su->scale;
686 break;
687 case (SCALE_PT):
688 r = su->scale / 8;
689 break;
690 case (SCALE_MM):
691 r = su->scale / 1000;
692 break;
693 case (SCALE_VS):
694 r = su->scale;
695 break;
696 default:
697 r = su->scale - 1;
698 break;
699 }
700
701 if (r < 0.0)
702 r = 0.0;
703 return(/* LINTED */(size_t)
704 r);
705 }
706
707
708 size_t
709 term_hspan(const struct roffsu *su)
710 {
711 double r;
712
713 /* XXX: CM, IN, and PT are approximations. */
714
715 switch (su->unit) {
716 case (SCALE_CM):
717 r = 4 * su->scale;
718 break;
719 case (SCALE_IN):
720 /* XXX: this is an approximation. */
721 r = 10 * su->scale;
722 break;
723 case (SCALE_PC):
724 r = (10 * su->scale) / 6;
725 break;
726 case (SCALE_PT):
727 r = (10 * su->scale) / 72;
728 break;
729 case (SCALE_MM):
730 r = su->scale / 1000; /* FIXME: double-check. */
731 break;
732 case (SCALE_VS):
733 r = su->scale * 2 - 1; /* FIXME: double-check. */
734 break;
735 default:
736 r = su->scale;
737 break;
738 }
739
740 if (r < 0.0)
741 r = 0.0;
742 return((size_t)/* LINTED */
743 r);
744 }
745
746