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