]> git.cameronkatri.com Git - mandoc.git/blob - term.c
The `am', `ami', `de', and holy `dei' are all being properly ignored.
[mandoc.git] / term.c
1 /* $Id: term.c,v 1.134 2010/05/15 16:18:23 joerg 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, size_t);
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(size_t width)
49 {
50
51 return(term_alloc(TERMENC_ASCII, width));
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, size_t width)
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 /* Enforce some lower boundary. */
88 if (width < 60)
89 width = 60;
90 p->defrmargin = width - 2;
91 return(p);
92 }
93
94
95 /*
96 * Flush a line of text. A "line" is loosely defined as being something
97 * that should be followed by a newline, regardless of whether it's
98 * broken apart by newlines getting there. A line can also be a
99 * fragment of a columnar list (`Bl -tag' or `Bl -column'), which does
100 * not have a trailing newline.
101 *
102 * The following flags may be specified:
103 *
104 * - TERMP_NOLPAD: when beginning to write the line, don't left-pad the
105 * offset value. This is useful when doing columnar lists where the
106 * prior column has right-padded.
107 *
108 * - TERMP_NOBREAK: this is the most important and is used when making
109 * columns. In short: don't print a newline and instead pad to the
110 * right margin. Used in conjunction with TERMP_NOLPAD.
111 *
112 * - TERMP_TWOSPACE: when padding, make sure there are at least two
113 * space characters of padding. Otherwise, rather break the line.
114 *
115 * - TERMP_DANGLE: don't newline when TERMP_NOBREAK is specified and
116 * the line is overrun, and don't pad-right if it's underrun.
117 *
118 * - TERMP_HANG: like TERMP_DANGLE, but doesn't newline when
119 * overruning, instead save the position and continue at that point
120 * when the next invocation.
121 *
122 * In-line line breaking:
123 *
124 * If TERMP_NOBREAK is specified and the line overruns the right
125 * margin, it will break and pad-right to the right margin after
126 * writing. If maxrmargin is violated, it will break and continue
127 * writing from the right-margin, which will lead to the above scenario
128 * upon exit. Otherwise, the line will break at the right margin.
129 */
130 void
131 term_flushln(struct termp *p)
132 {
133 int i; /* current input position in p->buf */
134 size_t vis; /* current visual position on output */
135 size_t vbl; /* number of blanks to prepend to output */
136 size_t vsz; /* visual characters to write to output */
137 size_t bp; /* visual right border position */
138 int j; /* temporary loop index */
139 size_t maxvis, mmax;
140
141 /*
142 * First, establish the maximum columns of "visible" content.
143 * This is usually the difference between the right-margin and
144 * an indentation, but can be, for tagged lists or columns, a
145 * small set of values.
146 */
147
148 assert(p->offset < p->rmargin);
149
150 maxvis = (int)(p->rmargin - p->offset) - p->overstep < 0 ?
151 /* LINTED */
152 0 : p->rmargin - p->offset - p->overstep;
153 mmax = (int)(p->maxrmargin - p->offset) - p->overstep < 0 ?
154 /* LINTED */
155 0 : p->maxrmargin - p->offset - p->overstep;
156
157 bp = TERMP_NOBREAK & p->flags ? mmax : maxvis;
158
159 /*
160 * FIXME: if bp is zero, we still output the first word before
161 * breaking the line.
162 */
163
164 vis = 0;
165
166 /*
167 * If in the standard case (left-justified), then begin with our
168 * indentation, otherwise (columns, etc.) just start spitting
169 * out text.
170 */
171
172 if ( ! (p->flags & TERMP_NOLPAD))
173 /* LINTED */
174 for (j = 0; j < (int)p->offset; j++)
175 putchar(' ');
176
177 for (i = 0; i < (int)p->col; i++) {
178 /*
179 * Count up visible word characters. Control sequences
180 * (starting with the CSI) aren't counted. A space
181 * generates a non-printing word, which is valid (the
182 * space is printed according to regular spacing rules).
183 */
184
185 /* LINTED */
186 for (j = i, vsz = 0; j < (int)p->col; j++) {
187 if (j && ' ' == p->buf[j])
188 break;
189 if (8 == p->buf[j])
190 vsz--;
191 else
192 vsz++;
193 }
194
195 /*
196 * Choose the number of blanks to prepend: no blank at the
197 * beginning of a line, one between words -- but do not
198 * actually write them yet.
199 */
200
201 vbl = (size_t)(0 == vis ? 0 : 1);
202
203 /*
204 * Find out whether we would exceed the right margin.
205 * If so, break to the next line. Otherwise, write the chosen
206 * number of blanks.
207 */
208
209 if (vis && vis + vbl + vsz > bp) {
210 putchar('\n');
211 if (TERMP_NOBREAK & p->flags) {
212 for (j = 0; j < (int)p->rmargin; j++)
213 putchar(' ');
214 vis = p->rmargin - p->offset;
215 } else {
216 for (j = 0; j < (int)p->offset; j++)
217 putchar(' ');
218 vis = 0;
219 }
220
221 /* Remove the p->overstep width. */
222
223 bp += (int)/* LINTED */
224 p->overstep;
225 p->overstep = 0;
226 } else {
227 for (j = 0; j < (int)vbl; j++)
228 putchar(' ');
229 vis += vbl;
230 }
231
232 /* Write out the [remaining] word. */
233 for ( ; i < (int)p->col; i++)
234 if (' ' == p->buf[i])
235 break;
236 else if (31 == p->buf[i])
237 putchar(' ');
238 else
239 putchar(p->buf[i]);
240
241 vis += vsz;
242 }
243
244 p->col = 0;
245 p->overstep = 0;
246
247 if ( ! (TERMP_NOBREAK & p->flags)) {
248 putchar('\n');
249 return;
250 }
251
252 if (TERMP_HANG & p->flags) {
253 /* We need one blank after the tag. */
254 p->overstep = /* LINTED */
255 vis - maxvis + 1;
256
257 /*
258 * Behave exactly the same way as groff:
259 * If we have overstepped the margin, temporarily move
260 * it to the right and flag the rest of the line to be
261 * shorter.
262 * If we landed right at the margin, be happy.
263 * If we are one step before the margin, temporarily
264 * move it one step LEFT and flag the rest of the line
265 * to be longer.
266 */
267 if (p->overstep >= -1) {
268 assert((int)maxvis + p->overstep >= 0);
269 /* LINTED */
270 maxvis += p->overstep;
271 } else
272 p->overstep = 0;
273
274 } else if (TERMP_DANGLE & p->flags)
275 return;
276
277 /* Right-pad. */
278 if (maxvis > vis + /* LINTED */
279 ((TERMP_TWOSPACE & p->flags) ? 1 : 0))
280 for ( ; vis < maxvis; vis++)
281 putchar(' ');
282 else { /* ...or newline break. */
283 putchar('\n');
284 for (i = 0; i < (int)p->rmargin; i++)
285 putchar(' ');
286 }
287 }
288
289
290 /*
291 * A newline only breaks an existing line; it won't assert vertical
292 * space. All data in the output buffer is flushed prior to the newline
293 * assertion.
294 */
295 void
296 term_newln(struct termp *p)
297 {
298
299 p->flags |= TERMP_NOSPACE;
300 if (0 == p->col) {
301 p->flags &= ~TERMP_NOLPAD;
302 return;
303 }
304 term_flushln(p);
305 p->flags &= ~TERMP_NOLPAD;
306 }
307
308
309 /*
310 * Asserts a vertical space (a full, empty line-break between lines).
311 * Note that if used twice, this will cause two blank spaces and so on.
312 * All data in the output buffer is flushed prior to the newline
313 * assertion.
314 */
315 void
316 term_vspace(struct termp *p)
317 {
318
319 term_newln(p);
320 putchar('\n');
321 }
322
323
324 static void
325 spec(struct termp *p, const char *word, size_t len)
326 {
327 const char *rhs;
328 size_t sz;
329
330 rhs = chars_a2ascii(p->symtab, word, len, &sz);
331 if (rhs)
332 encode(p, rhs, sz);
333 }
334
335
336 static void
337 res(struct termp *p, const char *word, size_t len)
338 {
339 const char *rhs;
340 size_t sz;
341
342 rhs = chars_a2res(p->symtab, word, len, &sz);
343 if (rhs)
344 encode(p, rhs, sz);
345 }
346
347
348 void
349 term_fontlast(struct termp *p)
350 {
351 enum termfont f;
352
353 f = p->fontl;
354 p->fontl = p->fontq[p->fonti];
355 p->fontq[p->fonti] = f;
356 }
357
358
359 void
360 term_fontrepl(struct termp *p, enum termfont f)
361 {
362
363 p->fontl = p->fontq[p->fonti];
364 p->fontq[p->fonti] = f;
365 }
366
367
368 void
369 term_fontpush(struct termp *p, enum termfont f)
370 {
371
372 assert(p->fonti + 1 < 10);
373 p->fontl = p->fontq[p->fonti];
374 p->fontq[++p->fonti] = f;
375 }
376
377
378 const void *
379 term_fontq(struct termp *p)
380 {
381
382 return(&p->fontq[p->fonti]);
383 }
384
385
386 enum termfont
387 term_fonttop(struct termp *p)
388 {
389
390 return(p->fontq[p->fonti]);
391 }
392
393
394 void
395 term_fontpopq(struct termp *p, const void *key)
396 {
397
398 while (p->fonti >= 0 && key != &p->fontq[p->fonti])
399 p->fonti--;
400 assert(p->fonti >= 0);
401 }
402
403
404 void
405 term_fontpop(struct termp *p)
406 {
407
408 assert(p->fonti);
409 p->fonti--;
410 }
411
412
413 /*
414 * Handle pwords, partial words, which may be either a single word or a
415 * phrase that cannot be broken down (such as a literal string). This
416 * handles word styling.
417 */
418 void
419 term_word(struct termp *p, const char *word)
420 {
421 const char *sv, *seq;
422 int sz;
423 size_t ssz;
424 enum roffdeco deco;
425
426 sv = word;
427
428 if (word[0] && '\0' == word[1])
429 switch (word[0]) {
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 if (TERMP_SENTENCE & p->flags)
455 bufferc(p, ' ');
456 }
457
458 if ( ! (p->flags & TERMP_NONOSPACE))
459 p->flags &= ~TERMP_NOSPACE;
460
461 p->flags &= ~TERMP_SENTENCE;
462
463 /* FIXME: use strcspn. */
464
465 while (*word) {
466 if ('\\' != *word) {
467 encode(p, word, 1);
468 word++;
469 continue;
470 }
471
472 seq = ++word;
473 sz = a2roffdeco(&deco, &seq, &ssz);
474
475 switch (deco) {
476 case (DECO_RESERVED):
477 res(p, seq, ssz);
478 break;
479 case (DECO_SPECIAL):
480 spec(p, seq, ssz);
481 break;
482 case (DECO_BOLD):
483 term_fontrepl(p, TERMFONT_BOLD);
484 break;
485 case (DECO_ITALIC):
486 term_fontrepl(p, TERMFONT_UNDER);
487 break;
488 case (DECO_ROMAN):
489 term_fontrepl(p, TERMFONT_NONE);
490 break;
491 case (DECO_PREVIOUS):
492 term_fontlast(p);
493 break;
494 default:
495 break;
496 }
497
498 word += sz;
499 if (DECO_NOSPACE == deco && '\0' == *word)
500 p->flags |= TERMP_NOSPACE;
501 }
502
503 /*
504 * Note that we don't process the pipe: the parser sees it as
505 * punctuation, but we don't in terms of typography.
506 */
507 if (sv[0] && 0 == sv[1])
508 switch (sv[0]) {
509 case('('):
510 /* FALLTHROUGH */
511 case('['):
512 p->flags |= TERMP_NOSPACE;
513 break;
514 default:
515 break;
516 }
517 }
518
519
520 static void
521 adjbuf(struct termp *p, size_t sz)
522 {
523
524 if (0 == p->maxcols)
525 p->maxcols = 1024;
526 while (sz >= p->maxcols)
527 p->maxcols <<= 2;
528
529 p->buf = realloc(p->buf, p->maxcols);
530 if (NULL == p->buf) {
531 perror(NULL);
532 exit(EXIT_FAILURE);
533 }
534 }
535
536
537 static void
538 buffera(struct termp *p, const char *word, size_t sz)
539 {
540
541 if (p->col + sz >= p->maxcols)
542 adjbuf(p, p->col + sz);
543
544 memcpy(&p->buf[(int)p->col], word, sz);
545 p->col += sz;
546 }
547
548
549 static void
550 bufferc(struct termp *p, char c)
551 {
552
553 if (p->col + 1 >= p->maxcols)
554 adjbuf(p, p->col + 1);
555
556 p->buf[(int)p->col++] = c;
557 }
558
559
560 static void
561 encode(struct termp *p, const char *word, size_t sz)
562 {
563 enum termfont f;
564 int i;
565
566 /*
567 * Encode and buffer a string of characters. If the current
568 * font mode is unset, buffer directly, else encode then buffer
569 * character by character.
570 */
571
572 if (TERMFONT_NONE == (f = term_fonttop(p))) {
573 buffera(p, word, sz);
574 return;
575 }
576
577 for (i = 0; i < (int)sz; i++) {
578 if ( ! isgraph((u_char)word[i])) {
579 bufferc(p, word[i]);
580 continue;
581 }
582
583 if (TERMFONT_UNDER == f)
584 bufferc(p, '_');
585 else
586 bufferc(p, word[i]);
587
588 bufferc(p, 8);
589 bufferc(p, word[i]);
590 }
591 }
592
593
594 size_t
595 term_vspan(const struct roffsu *su)
596 {
597 double r;
598
599 switch (su->unit) {
600 case (SCALE_CM):
601 r = su->scale * 2;
602 break;
603 case (SCALE_IN):
604 r = su->scale * 6;
605 break;
606 case (SCALE_PC):
607 r = su->scale;
608 break;
609 case (SCALE_PT):
610 r = su->scale / 8;
611 break;
612 case (SCALE_MM):
613 r = su->scale / 1000;
614 break;
615 case (SCALE_VS):
616 r = su->scale;
617 break;
618 default:
619 r = su->scale - 1;
620 break;
621 }
622
623 if (r < 0.0)
624 r = 0.0;
625 return(/* LINTED */(size_t)
626 r);
627 }
628
629
630 size_t
631 term_hspan(const struct roffsu *su)
632 {
633 double r;
634
635 /* XXX: CM, IN, and PT are approximations. */
636
637 switch (su->unit) {
638 case (SCALE_CM):
639 r = 4 * su->scale;
640 break;
641 case (SCALE_IN):
642 /* XXX: this is an approximation. */
643 r = 10 * su->scale;
644 break;
645 case (SCALE_PC):
646 r = (10 * su->scale) / 6;
647 break;
648 case (SCALE_PT):
649 r = (10 * su->scale) / 72;
650 break;
651 case (SCALE_MM):
652 r = su->scale / 1000; /* FIXME: double-check. */
653 break;
654 case (SCALE_VS):
655 r = su->scale * 2 - 1; /* FIXME: double-check. */
656 break;
657 default:
658 r = su->scale;
659 break;
660 }
661
662 if (r < 0.0)
663 r = 0.0;
664 return((size_t)/* LINTED */
665 r);
666 }
667
668