]> git.cameronkatri.com Git - mandoc.git/blob - term.c
Using man_node_delete() instead of man_node_free()/man_node_freelist() and friends...
[mandoc.git] / term.c
1 /* $Id: term.c,v 1.129 2010/03/23 12:42:22 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 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #include <sys/types.h>
22
23 #include <assert.h>
24 #include <ctype.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <time.h>
29
30 #include "chars.h"
31 #include "out.h"
32 #include "term.h"
33 #include "man.h"
34 #include "mdoc.h"
35 #include "main.h"
36
37 static struct termp *term_alloc(enum termenc);
38 static void term_free(struct termp *);
39 static void spec(struct termp *, const char *, size_t);
40 static void res(struct termp *, const char *, size_t);
41 static void buffera(struct termp *, const char *, size_t);
42 static void bufferc(struct termp *, char);
43 static void adjbuf(struct termp *p, size_t);
44 static void encode(struct termp *, const char *, size_t);
45
46
47 void *
48 ascii_alloc(void)
49 {
50
51 return(term_alloc(TERMENC_ASCII));
52 }
53
54
55 void
56 terminal_free(void *arg)
57 {
58
59 term_free((struct termp *)arg);
60 }
61
62
63 static void
64 term_free(struct termp *p)
65 {
66
67 if (p->buf)
68 free(p->buf);
69 if (p->symtab)
70 chars_free(p->symtab);
71
72 free(p);
73 }
74
75
76 static struct termp *
77 term_alloc(enum termenc enc)
78 {
79 struct termp *p;
80
81 p = calloc(1, sizeof(struct termp));
82 if (NULL == p) {
83 perror(NULL);
84 exit(EXIT_FAILURE);
85 }
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
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
143 * small set of values.
144 */
145
146 assert(p->offset < p->rmargin);
147
148 maxvis = (int)(p->rmargin - p->offset) - p->overstep < 0 ?
149 /* LINTED */
150 0 : p->rmargin - p->offset - p->overstep;
151 mmax = (int)(p->maxrmargin - p->offset) - p->overstep < 0 ?
152 /* LINTED */
153 0 : p->maxrmargin - p->offset - p->overstep;
154
155 bp = TERMP_NOBREAK & p->flags ? mmax : maxvis;
156
157 /*
158 * FIXME: if bp is zero, we still output the first word before
159 * breaking the line.
160 */
161
162 vis = 0;
163
164 /*
165 * If in the standard case (left-justified), then begin with our
166 * indentation, otherwise (columns, etc.) just start spitting
167 * out text.
168 */
169
170 if ( ! (p->flags & TERMP_NOLPAD))
171 /* LINTED */
172 for (j = 0; j < (int)p->offset; j++)
173 putchar(' ');
174
175 for (i = 0; i < (int)p->col; i++) {
176 /*
177 * Count up visible word characters. Control sequences
178 * (starting with the CSI) aren't counted. A space
179 * generates a non-printing word, which is valid (the
180 * space is printed according to regular spacing rules).
181 */
182
183 /* LINTED */
184 for (j = i, vsz = 0; j < (int)p->col; j++) {
185 if (j && ' ' == p->buf[j])
186 break;
187 else if (8 == p->buf[j])
188 vsz--;
189 else
190 vsz++;
191 }
192
193 /*
194 * Choose the number of blanks to prepend: no blank at the
195 * beginning of a line, one between words -- but do not
196 * actually write them yet.
197 */
198 vbl = (size_t)(0 == vis ? 0 : 1);
199
200 /*
201 * Find out whether we would exceed the right margin.
202 * If so, break to the next line. (TODO: hyphenate)
203 * Otherwise, write the chosen number of blanks now.
204 */
205 if (vis && vis + vbl + vsz > bp) {
206 putchar('\n');
207 if (TERMP_NOBREAK & p->flags) {
208 for (j = 0; j < (int)p->rmargin; j++)
209 putchar(' ');
210 vis = p->rmargin - p->offset;
211 } else {
212 for (j = 0; j < (int)p->offset; j++)
213 putchar(' ');
214 vis = 0;
215 }
216 /* Remove the p->overstep width. */
217 bp += (int)/* LINTED */
218 p->overstep;
219 p->overstep = 0;
220 } else {
221 for (j = 0; j < (int)vbl; j++)
222 putchar(' ');
223 vis += vbl;
224 }
225
226 /*
227 * Finally, write out the word.
228 */
229 for ( ; i < (int)p->col; i++) {
230 if (' ' == p->buf[i])
231 break;
232
233 /* The unit sep. is a non-breaking space. */
234 if (31 == p->buf[i])
235 putchar(' ');
236 else
237 putchar(p->buf[i]);
238 }
239 vis += vsz;
240 }
241
242 p->col = 0;
243 p->overstep = 0;
244
245 if ( ! (TERMP_NOBREAK & p->flags)) {
246 putchar('\n');
247 return;
248 }
249
250 if (TERMP_HANG & p->flags) {
251 /* We need one blank after the tag. */
252 p->overstep = /* LINTED */
253 vis - maxvis + 1;
254
255 /*
256 * Behave exactly the same way as groff:
257 * If we have overstepped the margin, temporarily move
258 * it to the right and flag the rest of the line to be
259 * shorter.
260 * If we landed right at the margin, be happy.
261 * If we are one step before the margin, temporarily
262 * move it one step LEFT and flag the rest of the line
263 * to be longer.
264 */
265 if (p->overstep >= -1) {
266 assert((int)maxvis + p->overstep >= 0);
267 /* LINTED */
268 maxvis += p->overstep;
269 } else
270 p->overstep = 0;
271
272 } else if (TERMP_DANGLE & p->flags)
273 return;
274
275 /* Right-pad. */
276 if (maxvis > vis + /* LINTED */
277 ((TERMP_TWOSPACE & p->flags) ? 1 : 0))
278 for ( ; vis < maxvis; vis++)
279 putchar(' ');
280 else { /* ...or newline break. */
281 putchar('\n');
282 for (i = 0; i < (int)p->rmargin; i++)
283 putchar(' ');
284 }
285 }
286
287
288 /*
289 * A newline only breaks an existing line; it won't assert vertical
290 * space. All data in the output buffer is flushed prior to the newline
291 * assertion.
292 */
293 void
294 term_newln(struct termp *p)
295 {
296
297 p->flags |= TERMP_NOSPACE;
298 if (0 == p->col) {
299 p->flags &= ~TERMP_NOLPAD;
300 return;
301 }
302 term_flushln(p);
303 p->flags &= ~TERMP_NOLPAD;
304 }
305
306
307 /*
308 * Asserts a vertical space (a full, empty line-break between lines).
309 * Note that if used twice, this will cause two blank spaces and so on.
310 * All data in the output buffer is flushed prior to the newline
311 * assertion.
312 */
313 void
314 term_vspace(struct termp *p)
315 {
316
317 term_newln(p);
318 putchar('\n');
319 }
320
321
322 static void
323 spec(struct termp *p, const char *word, size_t len)
324 {
325 const char *rhs;
326 size_t sz;
327
328 rhs = chars_a2ascii(p->symtab, word, len, &sz);
329 if (rhs)
330 encode(p, rhs, sz);
331 }
332
333
334 static void
335 res(struct termp *p, const char *word, size_t len)
336 {
337 const char *rhs;
338 size_t sz;
339
340 rhs = chars_a2res(p->symtab, word, len, &sz);
341 if (rhs)
342 encode(p, rhs, sz);
343 }
344
345
346 void
347 term_fontlast(struct termp *p)
348 {
349 enum termfont f;
350
351 f = p->fontl;
352 p->fontl = p->fontq[p->fonti];
353 p->fontq[p->fonti] = f;
354 }
355
356
357 void
358 term_fontrepl(struct termp *p, enum termfont f)
359 {
360
361 p->fontl = p->fontq[p->fonti];
362 p->fontq[p->fonti] = f;
363 }
364
365
366 void
367 term_fontpush(struct termp *p, enum termfont f)
368 {
369
370 assert(p->fonti + 1 < 10);
371 p->fontl = p->fontq[p->fonti];
372 p->fontq[++p->fonti] = f;
373 }
374
375
376 const void *
377 term_fontq(struct termp *p)
378 {
379
380 return(&p->fontq[p->fonti]);
381 }
382
383
384 enum termfont
385 term_fonttop(struct termp *p)
386 {
387
388 return(p->fontq[p->fonti]);
389 }
390
391
392 void
393 term_fontpopq(struct termp *p, const void *key)
394 {
395
396 while (p->fonti >= 0 && key != &p->fontq[p->fonti])
397 p->fonti--;
398 assert(p->fonti >= 0);
399 }
400
401
402 void
403 term_fontpop(struct termp *p)
404 {
405
406 assert(p->fonti);
407 p->fonti--;
408 }
409
410
411 /*
412 * Handle pwords, partial words, which may be either a single word or a
413 * phrase that cannot be broken down (such as a literal string). This
414 * handles word styling.
415 */
416 void
417 term_word(struct termp *p, const char *word)
418 {
419 const char *sv, *seq;
420 int sz;
421 size_t ssz;
422 enum roffdeco deco;
423
424 sv = word;
425
426 if (word[0] && '\0' == word[1])
427 switch (word[0]) {
428 case('.'):
429 /* FALLTHROUGH */
430 case(','):
431 /* FALLTHROUGH */
432 case(';'):
433 /* FALLTHROUGH */
434 case(':'):
435 /* FALLTHROUGH */
436 case('?'):
437 /* FALLTHROUGH */
438 case('!'):
439 /* FALLTHROUGH */
440 case(')'):
441 /* FALLTHROUGH */
442 case(']'):
443 /* FALLTHROUGH */
444 case('}'):
445 if ( ! (TERMP_IGNDELIM & p->flags))
446 p->flags |= TERMP_NOSPACE;
447 break;
448 default:
449 break;
450 }
451
452 if ( ! (TERMP_NOSPACE & p->flags))
453 bufferc(p, ' ');
454
455 if ( ! (p->flags & TERMP_NONOSPACE))
456 p->flags &= ~TERMP_NOSPACE;
457
458 /* FIXME: use strcspn. */
459
460 while (*word) {
461 if ('\\' != *word) {
462 encode(p, word, 1);
463 word++;
464 continue;
465 }
466
467 seq = ++word;
468 sz = a2roffdeco(&deco, &seq, &ssz);
469
470 switch (deco) {
471 case (DECO_RESERVED):
472 res(p, seq, ssz);
473 break;
474 case (DECO_SPECIAL):
475 spec(p, seq, ssz);
476 break;
477 case (DECO_BOLD):
478 term_fontrepl(p, TERMFONT_BOLD);
479 break;
480 case (DECO_ITALIC):
481 term_fontrepl(p, TERMFONT_UNDER);
482 break;
483 case (DECO_ROMAN):
484 term_fontrepl(p, TERMFONT_NONE);
485 break;
486 case (DECO_PREVIOUS):
487 term_fontlast(p);
488 break;
489 default:
490 break;
491 }
492
493 word += sz;
494 if (DECO_NOSPACE == deco && '\0' == *word)
495 p->flags |= TERMP_NOSPACE;
496 }
497
498 if (sv[0] && 0 == sv[1])
499 switch (sv[0]) {
500 case('('):
501 /* FALLTHROUGH */
502 case('['):
503 /* FALLTHROUGH */
504 case('{'):
505 p->flags |= TERMP_NOSPACE;
506 break;
507 default:
508 break;
509 }
510 }
511
512
513 static void
514 adjbuf(struct termp *p, size_t sz)
515 {
516
517 if (0 == p->maxcols)
518 p->maxcols = 1024;
519 while (sz >= p->maxcols)
520 p->maxcols <<= 2;
521
522 p->buf = realloc(p->buf, p->maxcols);
523 if (NULL == p->buf) {
524 perror(NULL);
525 exit(EXIT_FAILURE);
526 }
527 }
528
529
530 static void
531 buffera(struct termp *p, const char *word, size_t sz)
532 {
533
534 if (p->col + sz >= p->maxcols)
535 adjbuf(p, p->col + sz);
536
537 memcpy(&p->buf[(int)p->col], word, sz);
538 p->col += sz;
539 }
540
541
542 static void
543 bufferc(struct termp *p, char c)
544 {
545
546 if (p->col + 1 >= p->maxcols)
547 adjbuf(p, p->col + 1);
548
549 p->buf[(int)p->col++] = c;
550 }
551
552
553 static void
554 encode(struct termp *p, const char *word, size_t sz)
555 {
556 enum termfont f;
557 int i;
558
559 /*
560 * Encode and buffer a string of characters. If the current
561 * font mode is unset, buffer directly, else encode then buffer
562 * character by character.
563 */
564
565 if (TERMFONT_NONE == (f = term_fonttop(p))) {
566 buffera(p, word, sz);
567 return;
568 }
569
570 for (i = 0; i < (int)sz; i++) {
571 if ( ! isgraph((u_char)word[i])) {
572 bufferc(p, word[i]);
573 continue;
574 }
575
576 if (TERMFONT_UNDER == f)
577 bufferc(p, '_');
578 else
579 bufferc(p, word[i]);
580
581 bufferc(p, 8);
582 bufferc(p, word[i]);
583 }
584 }
585
586
587 size_t
588 term_vspan(const struct roffsu *su)
589 {
590 double r;
591
592 switch (su->unit) {
593 case (SCALE_CM):
594 r = su->scale * 2;
595 break;
596 case (SCALE_IN):
597 r = su->scale * 6;
598 break;
599 case (SCALE_PC):
600 r = su->scale;
601 break;
602 case (SCALE_PT):
603 r = su->scale / 8;
604 break;
605 case (SCALE_MM):
606 r = su->scale / 1000;
607 break;
608 case (SCALE_VS):
609 r = su->scale;
610 break;
611 default:
612 r = su->scale - 1;
613 break;
614 }
615
616 if (r < 0.0)
617 r = 0.0;
618 return(/* LINTED */(size_t)
619 r);
620 }
621
622
623 size_t
624 term_hspan(const struct roffsu *su)
625 {
626 double r;
627
628 /* XXX: CM, IN, and PT are approximations. */
629
630 switch (su->unit) {
631 case (SCALE_CM):
632 r = 4 * su->scale;
633 break;
634 case (SCALE_IN):
635 /* XXX: this is an approximation. */
636 r = 10 * su->scale;
637 break;
638 case (SCALE_PC):
639 r = (10 * su->scale) / 6;
640 break;
641 case (SCALE_PT):
642 r = (10 * su->scale) / 72;
643 break;
644 case (SCALE_MM):
645 r = su->scale / 1000; /* FIXME: double-check. */
646 break;
647 case (SCALE_VS):
648 r = su->scale * 2 - 1; /* FIXME: double-check. */
649 break;
650 default:
651 r = su->scale;
652 break;
653 }
654
655 if (r < 0.0)
656 r = 0.0;
657 return((size_t)/* LINTED */
658 r);
659 }
660
661