]> git.cameronkatri.com Git - mandoc.git/blob - term.c
Lint fixes.
[mandoc.git] / term.c
1 /* $Id: term.c,v 1.118 2009/10/30 18:50:11 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 <err.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 fprintf(stderr, "memory exhausted\n");
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 0 : p->rmargin - p->offset - overstep;
151 mmax = (int)(p->maxrmargin - p->offset) - overstep < 0 ?
152 0 : p->maxrmargin - p->offset - overstep;
153
154 bp = TERMP_NOBREAK & p->flags ? mmax : maxvis;
155
156 /*
157 * FIXME: if bp is zero, we still output the first word before
158 * breaking the line.
159 */
160
161 vis = 0;
162
163 /*
164 * If in the standard case (left-justified), then begin with our
165 * indentation, otherwise (columns, etc.) just start spitting
166 * out text.
167 */
168
169 if ( ! (p->flags & TERMP_NOLPAD))
170 /* LINTED */
171 for (j = 0; j < (int)p->offset; j++)
172 putchar(' ');
173
174 for (i = 0; i < (int)p->col; i++) {
175 /*
176 * Count up visible word characters. Control sequences
177 * (starting with the CSI) aren't counted. A space
178 * generates a non-printing word, which is valid (the
179 * space is printed according to regular spacing rules).
180 */
181
182 /* LINTED */
183 for (j = i, vsz = 0; j < (int)p->col; j++) {
184 if (j && ' ' == p->buf[j])
185 break;
186 else if (8 == p->buf[j])
187 vsz--;
188 else
189 vsz++;
190 }
191
192 /*
193 * Choose the number of blanks to prepend: no blank at the
194 * beginning of a line, one between words -- but do not
195 * actually write them yet.
196 */
197 vbl = (size_t)(0 == vis ? 0 : 1);
198
199 /*
200 * Find out whether we would exceed the right margin.
201 * If so, break to the next line. (TODO: hyphenate)
202 * Otherwise, write the chosen number of blanks now.
203 */
204 if (vis && vis + vbl + vsz > bp) {
205 putchar('\n');
206 if (TERMP_NOBREAK & p->flags) {
207 for (j = 0; j < (int)p->rmargin; j++)
208 putchar(' ');
209 vis = p->rmargin - p->offset;
210 } else {
211 for (j = 0; j < (int)p->offset; j++)
212 putchar(' ');
213 vis = 0;
214 }
215 /* Remove the overstep width. */
216 bp += (int)/* LINTED */
217 overstep;
218 overstep = 0;
219 } else {
220 for (j = 0; j < (int)vbl; j++)
221 putchar(' ');
222 vis += vbl;
223 }
224
225 /*
226 * Finally, write out the word.
227 */
228 for ( ; i < (int)p->col; i++) {
229 if (' ' == p->buf[i])
230 break;
231 putchar(p->buf[i]);
232 }
233 vis += vsz;
234 }
235
236 p->col = 0;
237 overstep = 0;
238
239 if ( ! (TERMP_NOBREAK & p->flags)) {
240 putchar('\n');
241 return;
242 }
243
244 if (TERMP_HANG & p->flags) {
245 /* We need one blank after the tag. */
246 overstep = /* LINTED */
247 vis - maxvis + 1;
248
249 /*
250 * Behave exactly the same way as groff:
251 * If we have overstepped the margin, temporarily move
252 * it to the right and flag the rest of the line to be
253 * shorter.
254 * If we landed right at the margin, be happy.
255 * If we are one step before the margin, temporarily
256 * move it one step LEFT and flag the rest of the line
257 * to be longer.
258 */
259 if (overstep >= -1) {
260 assert((int)maxvis + overstep >= 0);
261 /* LINTED */
262 maxvis += overstep;
263 } else
264 overstep = 0;
265
266 } else if (TERMP_DANGLE & p->flags)
267 return;
268
269 /* Right-pad. */
270 if (maxvis > vis + /* LINTED */
271 ((TERMP_TWOSPACE & p->flags) ? 1 : 0))
272 for ( ; vis < maxvis; vis++)
273 putchar(' ');
274 else { /* ...or newline break. */
275 putchar('\n');
276 for (i = 0; i < (int)p->rmargin; i++)
277 putchar(' ');
278 }
279 }
280
281
282 /*
283 * A newline only breaks an existing line; it won't assert vertical
284 * space. All data in the output buffer is flushed prior to the newline
285 * assertion.
286 */
287 void
288 term_newln(struct termp *p)
289 {
290
291 p->flags |= TERMP_NOSPACE;
292 if (0 == p->col) {
293 p->flags &= ~TERMP_NOLPAD;
294 return;
295 }
296 term_flushln(p);
297 p->flags &= ~TERMP_NOLPAD;
298 }
299
300
301 /*
302 * Asserts a vertical space (a full, empty line-break between lines).
303 * Note that if used twice, this will cause two blank spaces and so on.
304 * All data in the output buffer is flushed prior to the newline
305 * assertion.
306 */
307 void
308 term_vspace(struct termp *p)
309 {
310
311 term_newln(p);
312 putchar('\n');
313 }
314
315
316 static void
317 do_special(struct termp *p, const char *word, size_t len)
318 {
319 const char *rhs;
320 size_t sz;
321 int i;
322
323 rhs = chars_a2ascii(p->symtab, word, len, &sz);
324
325 if (NULL == rhs) {
326 #if 0
327 fputs("Unknown special character: ", stderr);
328 for (i = 0; i < (int)len; i++)
329 fputc(word[i], stderr);
330 fputc('\n', stderr);
331 #endif
332 return;
333 }
334 for (i = 0; i < (int)sz; i++)
335 encode(p, rhs[i]);
336 }
337
338
339 static void
340 do_reserved(struct termp *p, const char *word, size_t len)
341 {
342 const char *rhs;
343 size_t sz;
344 int i;
345
346 rhs = chars_a2res(p->symtab, word, len, &sz);
347
348 if (NULL == rhs) {
349 #if 0
350 fputs("Unknown reserved word: ", stderr);
351 for (i = 0; i < (int)len; i++)
352 fputc(word[i], stderr);
353 fputc('\n', stderr);
354 #endif
355 return;
356 }
357 for (i = 0; i < (int)sz; i++)
358 encode(p, rhs[i]);
359 }
360
361
362 /*
363 * Handle an escape sequence: determine its length and pass it to the
364 * escape-symbol look table. Note that we assume mdoc(3) has validated
365 * the escape sequence (we assert upon badly-formed escape sequences).
366 */
367 static void
368 do_escaped(struct termp *p, const char **word)
369 {
370 int j, type;
371 const char *wp;
372
373 wp = *word;
374 type = 1;
375
376 if (0 == *(++wp)) {
377 *word = wp;
378 return;
379 }
380
381 if ('(' == *wp) {
382 wp++;
383 if (0 == *wp || 0 == *(wp + 1)) {
384 *word = 0 == *wp ? wp : wp + 1;
385 return;
386 }
387
388 do_special(p, wp, 2);
389 *word = ++wp;
390 return;
391
392 } else if ('*' == *wp) {
393 if (0 == *(++wp)) {
394 *word = wp;
395 return;
396 }
397
398 switch (*wp) {
399 case ('('):
400 wp++;
401 if (0 == *wp || 0 == *(wp + 1)) {
402 *word = 0 == *wp ? wp : wp + 1;
403 return;
404 }
405
406 do_reserved(p, wp, 2);
407 *word = ++wp;
408 return;
409 case ('['):
410 type = 0;
411 break;
412 default:
413 do_reserved(p, wp, 1);
414 *word = wp;
415 return;
416 }
417
418 } else if ('f' == *wp) {
419 if (0 == *(++wp)) {
420 *word = wp;
421 return;
422 }
423
424 switch (*wp) {
425 case ('B'):
426 p->bold++;
427 break;
428 case ('I'):
429 p->under++;
430 break;
431 case ('P'):
432 /* FALLTHROUGH */
433 case ('R'):
434 p->bold = p->under = 0;
435 break;
436 default:
437 break;
438 }
439
440 *word = wp;
441 return;
442
443 } else if ('[' != *wp) {
444 do_special(p, wp, 1);
445 *word = wp;
446 return;
447 }
448
449 wp++;
450 for (j = 0; *wp && ']' != *wp; wp++, j++)
451 /* Loop... */ ;
452
453 if (0 == *wp) {
454 *word = wp;
455 return;
456 }
457
458 if (type)
459 do_special(p, wp - j, (size_t)j);
460 else
461 do_reserved(p, wp - j, (size_t)j);
462 *word = wp;
463 }
464
465
466 /*
467 * Handle pwords, partial words, which may be either a single word or a
468 * phrase that cannot be broken down (such as a literal string). This
469 * handles word styling.
470 */
471 void
472 term_word(struct termp *p, const char *word)
473 {
474 const char *sv;
475
476 sv = word;
477
478 if (word[0] && 0 == word[1])
479 switch (word[0]) {
480 case('.'):
481 /* FALLTHROUGH */
482 case(','):
483 /* FALLTHROUGH */
484 case(';'):
485 /* FALLTHROUGH */
486 case(':'):
487 /* FALLTHROUGH */
488 case('?'):
489 /* FALLTHROUGH */
490 case('!'):
491 /* FALLTHROUGH */
492 case(')'):
493 /* FALLTHROUGH */
494 case(']'):
495 /* FALLTHROUGH */
496 case('}'):
497 if ( ! (TERMP_IGNDELIM & p->flags))
498 p->flags |= TERMP_NOSPACE;
499 break;
500 default:
501 break;
502 }
503
504 if ( ! (TERMP_NOSPACE & p->flags))
505 buffer(p, ' ');
506
507 if ( ! (p->flags & TERMP_NONOSPACE))
508 p->flags &= ~TERMP_NOSPACE;
509
510 for ( ; *word; word++)
511 if ('\\' != *word)
512 encode(p, *word);
513 else
514 do_escaped(p, &word);
515
516 if (sv[0] && 0 == sv[1])
517 switch (sv[0]) {
518 case('('):
519 /* FALLTHROUGH */
520 case('['):
521 /* FALLTHROUGH */
522 case('{'):
523 p->flags |= TERMP_NOSPACE;
524 break;
525 default:
526 break;
527 }
528 }
529
530
531 /*
532 * Insert a single character into the line-buffer. If the buffer's
533 * space is exceeded, then allocate more space by doubling the buffer
534 * size.
535 */
536 static void
537 buffer(struct termp *p, char c)
538 {
539 size_t s;
540
541 if (p->col + 1 >= p->maxcols) {
542 if (0 == p->maxcols)
543 p->maxcols = 256;
544 s = p->maxcols * 2;
545 p->buf = realloc(p->buf, s);
546 if (NULL == p->buf) {
547 fprintf(stderr, "memory exhausted\n");
548 exit(EXIT_FAILURE);
549 }
550 p->maxcols = s;
551 }
552 p->buf[(int)(p->col)++] = c;
553 }
554
555
556 static void
557 encode(struct termp *p, char c)
558 {
559
560 if (' ' != c) {
561 if (p->under) {
562 buffer(p, '_');
563 buffer(p, 8);
564 }
565 if (p->bold) {
566 buffer(p, c);
567 buffer(p, 8);
568 }
569 }
570 buffer(p, c);
571 }
572
573
574 size_t
575 term_vspan(const struct roffsu *su)
576 {
577 double r;
578
579 switch (su->unit) {
580 case (SCALE_CM):
581 r = su->scale * 2;
582 break;
583 case (SCALE_IN):
584 r = su->scale * 6;
585 break;
586 case (SCALE_PC):
587 r = su->scale;
588 break;
589 case (SCALE_PT):
590 r = su->scale / 8;
591 break;
592 case (SCALE_MM):
593 r = su->scale / 1000;
594 break;
595 case (SCALE_VS):
596 r = su->scale;
597 break;
598 default:
599 r = su->scale - 1;
600 break;
601 }
602
603 if (r < 0.0)
604 r = 0.0;
605 return(/* LINTED */(size_t)
606 r);
607 }
608
609
610 size_t
611 term_hspan(const struct roffsu *su)
612 {
613 double r;
614
615 /* XXX: CM, IN, and PT are approximations. */
616
617 switch (su->unit) {
618 case (SCALE_CM):
619 r = 4 * su->scale;
620 break;
621 case (SCALE_IN):
622 /* XXX: this is an approximation. */
623 r = 10 * su->scale;
624 break;
625 case (SCALE_PC):
626 r = (10 * su->scale) / 6;
627 break;
628 case (SCALE_PT):
629 r = (10 * su->scale) / 72;
630 break;
631 case (SCALE_MM):
632 r = su->scale / 1000; /* FIXME: double-check. */
633 break;
634 case (SCALE_VS):
635 r = su->scale * 2 - 1; /* FIXME: double-check. */
636 break;
637 default:
638 r = su->scale;
639 break;
640 }
641
642 if (r < 0.0)
643 r = 0.0;
644 return((size_t)/* LINTED */
645 r);
646 }
647
648