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