Removing warning about OSNAME.
[minix.git] / external / bsd / mdocml / dist / term.c
blobc46b4c41345afbc6fc0fb26255ffdee41e9ee761
1 /* $Vendor-Id: term.c,v 1.176 2011/01/04 13:14:26 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010 Ingo Schwarze <schwarze@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
22 #include <sys/types.h>
24 #include <assert.h>
25 #include <ctype.h>
26 #include <stdint.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
31 #include "mandoc.h"
32 #include "chars.h"
33 #include "out.h"
34 #include "term.h"
35 #include "main.h"
37 static void spec(struct termp *, enum roffdeco,
38 const char *, size_t);
39 static void res(struct termp *, const char *, size_t);
40 static void bufferc(struct termp *, char);
41 static void adjbuf(struct termp *p, size_t);
42 static void encode(struct termp *, const char *, size_t);
45 void
46 term_free(struct termp *p)
49 if (p->buf)
50 free(p->buf);
51 if (p->symtab)
52 chars_free(p->symtab);
54 free(p);
58 void
59 term_begin(struct termp *p, term_margin head,
60 term_margin foot, const void *arg)
63 p->headf = head;
64 p->footf = foot;
65 p->argf = arg;
66 (*p->begin)(p);
70 void
71 term_end(struct termp *p)
74 (*p->end)(p);
78 struct termp *
79 term_alloc(enum termenc enc)
81 struct termp *p;
83 p = calloc(1, sizeof(struct termp));
84 if (NULL == p) {
85 perror(NULL);
86 exit((int)MANDOCLEVEL_SYSERR);
89 p->enc = enc;
90 return(p);
95 * Flush a line of text. A "line" is loosely defined as being something
96 * that should be followed by a newline, regardless of whether it's
97 * broken apart by newlines getting there. A line can also be a
98 * fragment of a columnar list (`Bl -tag' or `Bl -column'), which does
99 * not have a trailing newline.
101 * The following flags may be specified:
103 * - TERMP_NOLPAD: when beginning to write the line, don't left-pad the
104 * offset value. This is useful when doing columnar lists where the
105 * prior column has right-padded.
107 * - TERMP_NOBREAK: this is the most important and is used when making
108 * columns. In short: don't print a newline and instead pad to the
109 * right margin. Used in conjunction with TERMP_NOLPAD.
111 * - TERMP_TWOSPACE: when padding, make sure there are at least two
112 * space characters of padding. Otherwise, rather break the line.
114 * - TERMP_DANGLE: don't newline when TERMP_NOBREAK is specified and
115 * the line is overrun, and don't pad-right if it's underrun.
117 * - TERMP_HANG: like TERMP_DANGLE, but doesn't newline when
118 * overruning, instead save the position and continue at that point
119 * when the next invocation.
121 * In-line line breaking:
123 * If TERMP_NOBREAK is specified and the line overruns the right
124 * margin, it will break and pad-right to the right margin after
125 * writing. If maxrmargin is violated, it will break and continue
126 * writing from the right-margin, which will lead to the above scenario
127 * upon exit. Otherwise, the line will break at the right margin.
129 void
130 term_flushln(struct termp *p)
132 int i; /* current input position in p->buf */
133 size_t vis; /* current visual position on output */
134 size_t vbl; /* number of blanks to prepend to output */
135 size_t vend; /* end of word visual position on output */
136 size_t bp; /* visual right border position */
137 size_t dv; /* temporary for visual pos calculations */
138 int j; /* temporary loop index for p->buf */
139 int jhy; /* last hyph before overflow w/r/t j */
140 size_t maxvis; /* output position of visible boundary */
141 size_t mmax; /* used in calculating bp */
144 * First, establish the maximum columns of "visible" content.
145 * This is usually the difference between the right-margin and
146 * an indentation, but can be, for tagged lists or columns, a
147 * small set of values.
149 assert (p->rmargin >= p->offset);
150 dv = p->rmargin - p->offset;
151 maxvis = (int)dv > p->overstep ? dv - (size_t)p->overstep : 0;
152 dv = p->maxrmargin - p->offset;
153 mmax = (int)dv > p->overstep ? dv - (size_t)p->overstep : 0;
155 bp = TERMP_NOBREAK & p->flags ? mmax : maxvis;
158 * Indent the first line of a paragraph.
160 vbl = p->flags & TERMP_NOLPAD ? (size_t)0 : p->offset;
162 vis = vend = 0;
163 i = 0;
165 while (i < (int)p->col) {
167 * Handle literal tab characters: collapse all
168 * subsequent tabs into a single huge set of spaces.
170 while (i < (int)p->col && '\t' == p->buf[i]) {
171 vend = (vis / p->tabwidth + 1) * p->tabwidth;
172 vbl += vend - vis;
173 vis = vend;
174 i++;
178 * Count up visible word characters. Control sequences
179 * (starting with the CSI) aren't counted. A space
180 * generates a non-printing word, which is valid (the
181 * space is printed according to regular spacing rules).
184 for (j = i, jhy = 0; j < (int)p->col; j++) {
185 if ((j && ' ' == p->buf[j]) || '\t' == p->buf[j])
186 break;
188 /* Back over the the last printed character. */
189 if (8 == p->buf[j]) {
190 assert(j);
191 vend -= (*p->width)(p, p->buf[j - 1]);
192 continue;
195 /* Regular word. */
196 /* Break at the hyphen point if we overrun. */
197 if (vend > vis && vend < bp &&
198 ASCII_HYPH == p->buf[j])
199 jhy = j;
201 vend += (*p->width)(p, p->buf[j]);
205 * Find out whether we would exceed the right margin.
206 * If so, break to the next line.
208 if (vend > bp && 0 == jhy && vis > 0) {
209 vend -= vis;
210 (*p->endline)(p);
211 if (TERMP_NOBREAK & p->flags) {
212 p->viscol = p->rmargin;
213 (*p->advance)(p, p->rmargin);
214 vend += p->rmargin - p->offset;
215 } else {
216 p->viscol = 0;
217 vbl = p->offset;
220 /* Remove the p->overstep width. */
222 bp += (size_t)p->overstep;
223 p->overstep = 0;
226 /* Write out the [remaining] word. */
227 for ( ; i < (int)p->col; i++) {
228 if (vend > bp && jhy > 0 && i > jhy)
229 break;
230 if ('\t' == p->buf[i])
231 break;
232 if (' ' == p->buf[i]) {
233 j = i;
234 while (' ' == p->buf[i])
235 i++;
236 dv = (size_t)(i - j) * (*p->width)(p, ' ');
237 vbl += dv;
238 vend += dv;
239 break;
241 if (ASCII_NBRSP == p->buf[i]) {
242 vbl += (*p->width)(p, ' ');
243 continue;
247 * Now we definitely know there will be
248 * printable characters to output,
249 * so write preceding white space now.
251 if (vbl) {
252 (*p->advance)(p, vbl);
253 p->viscol += vbl;
254 vbl = 0;
257 if (ASCII_HYPH == p->buf[i]) {
258 (*p->letter)(p, '-');
259 p->viscol += (*p->width)(p, '-');
260 } else {
261 (*p->letter)(p, p->buf[i]);
262 p->viscol += (*p->width)(p, p->buf[i]);
265 vis = vend;
269 * If there was trailing white space, it was not printed;
270 * so reset the cursor position accordingly.
272 vis -= vbl;
274 p->col = 0;
275 p->overstep = 0;
277 if ( ! (TERMP_NOBREAK & p->flags)) {
278 p->viscol = 0;
279 (*p->endline)(p);
280 return;
283 if (TERMP_HANG & p->flags) {
284 /* We need one blank after the tag. */
285 p->overstep = (int)(vis - maxvis + (*p->width)(p, ' '));
288 * Behave exactly the same way as groff:
289 * If we have overstepped the margin, temporarily move
290 * it to the right and flag the rest of the line to be
291 * shorter.
292 * If we landed right at the margin, be happy.
293 * If we are one step before the margin, temporarily
294 * move it one step LEFT and flag the rest of the line
295 * to be longer.
297 if (p->overstep >= -1) {
298 assert((int)maxvis + p->overstep >= 0);
299 maxvis += (size_t)p->overstep;
300 } else
301 p->overstep = 0;
303 } else if (TERMP_DANGLE & p->flags)
304 return;
306 /* Right-pad. */
307 if (maxvis > vis +
308 ((TERMP_TWOSPACE & p->flags) ? (*p->width)(p, ' ') : 0)) {
309 p->viscol += maxvis - vis;
310 (*p->advance)(p, maxvis - vis);
311 vis += (maxvis - vis);
312 } else { /* ...or newline break. */
313 (*p->endline)(p);
314 p->viscol = p->rmargin;
315 (*p->advance)(p, p->rmargin);
321 * A newline only breaks an existing line; it won't assert vertical
322 * space. All data in the output buffer is flushed prior to the newline
323 * assertion.
325 void
326 term_newln(struct termp *p)
329 p->flags |= TERMP_NOSPACE;
330 if (0 == p->col && 0 == p->viscol) {
331 p->flags &= ~TERMP_NOLPAD;
332 return;
334 term_flushln(p);
335 p->flags &= ~TERMP_NOLPAD;
340 * Asserts a vertical space (a full, empty line-break between lines).
341 * Note that if used twice, this will cause two blank spaces and so on.
342 * All data in the output buffer is flushed prior to the newline
343 * assertion.
345 void
346 term_vspace(struct termp *p)
349 term_newln(p);
350 p->viscol = 0;
351 (*p->endline)(p);
355 static void
356 spec(struct termp *p, enum roffdeco d, const char *word, size_t len)
358 const char *rhs;
359 size_t sz;
361 rhs = chars_spec2str(p->symtab, word, len, &sz);
362 if (rhs)
363 encode(p, rhs, sz);
364 else if (DECO_SSPECIAL == d)
365 encode(p, word, len);
369 static void
370 res(struct termp *p, const char *word, size_t len)
372 const char *rhs;
373 size_t sz;
375 rhs = chars_res2str(p->symtab, word, len, &sz);
376 if (rhs)
377 encode(p, rhs, sz);
381 void
382 term_fontlast(struct termp *p)
384 enum termfont f;
386 f = p->fontl;
387 p->fontl = p->fontq[p->fonti];
388 p->fontq[p->fonti] = f;
392 void
393 term_fontrepl(struct termp *p, enum termfont f)
396 p->fontl = p->fontq[p->fonti];
397 p->fontq[p->fonti] = f;
401 void
402 term_fontpush(struct termp *p, enum termfont f)
405 assert(p->fonti + 1 < 10);
406 p->fontl = p->fontq[p->fonti];
407 p->fontq[++p->fonti] = f;
411 const void *
412 term_fontq(struct termp *p)
415 return(&p->fontq[p->fonti]);
419 enum termfont
420 term_fonttop(struct termp *p)
423 return(p->fontq[p->fonti]);
427 void
428 term_fontpopq(struct termp *p, const void *key)
431 while (p->fonti >= 0 && key != &p->fontq[p->fonti])
432 p->fonti--;
433 assert(p->fonti >= 0);
437 void
438 term_fontpop(struct termp *p)
441 assert(p->fonti);
442 p->fonti--;
447 * Handle pwords, partial words, which may be either a single word or a
448 * phrase that cannot be broken down (such as a literal string). This
449 * handles word styling.
451 void
452 term_word(struct termp *p, const char *word)
454 const char *sv, *seq;
455 size_t ssz;
456 enum roffdeco deco;
458 sv = word;
460 if (word[0] && '\0' == word[1])
461 switch (word[0]) {
462 case('.'):
463 /* FALLTHROUGH */
464 case(','):
465 /* FALLTHROUGH */
466 case(';'):
467 /* FALLTHROUGH */
468 case(':'):
469 /* FALLTHROUGH */
470 case('?'):
471 /* FALLTHROUGH */
472 case('!'):
473 /* FALLTHROUGH */
474 case(')'):
475 /* FALLTHROUGH */
476 case(']'):
477 if ( ! (TERMP_IGNDELIM & p->flags))
478 p->flags |= TERMP_NOSPACE;
479 break;
480 default:
481 break;
484 if ( ! (TERMP_NOSPACE & p->flags)) {
485 if ( ! (TERMP_KEEP & p->flags)) {
486 if (TERMP_PREKEEP & p->flags)
487 p->flags |= TERMP_KEEP;
488 bufferc(p, ' ');
489 if (TERMP_SENTENCE & p->flags)
490 bufferc(p, ' ');
491 } else
492 bufferc(p, ASCII_NBRSP);
495 if ( ! (p->flags & TERMP_NONOSPACE))
496 p->flags &= ~TERMP_NOSPACE;
497 else
498 p->flags |= TERMP_NOSPACE;
500 p->flags &= ~(TERMP_SENTENCE | TERMP_IGNDELIM);
502 while (*word) {
503 if ((ssz = strcspn(word, "\\")) > 0)
504 encode(p, word, ssz);
506 word += ssz;
507 if ('\\' != *word)
508 continue;
510 seq = ++word;
511 word += a2roffdeco(&deco, &seq, &ssz);
513 switch (deco) {
514 case (DECO_RESERVED):
515 res(p, seq, ssz);
516 break;
517 case (DECO_SPECIAL):
518 /* FALLTHROUGH */
519 case (DECO_SSPECIAL):
520 spec(p, deco, seq, ssz);
521 break;
522 case (DECO_BOLD):
523 term_fontrepl(p, TERMFONT_BOLD);
524 break;
525 case (DECO_ITALIC):
526 term_fontrepl(p, TERMFONT_UNDER);
527 break;
528 case (DECO_ROMAN):
529 term_fontrepl(p, TERMFONT_NONE);
530 break;
531 case (DECO_PREVIOUS):
532 term_fontlast(p);
533 break;
534 default:
535 break;
538 if (DECO_NOSPACE == deco && '\0' == *word)
539 p->flags |= TERMP_NOSPACE;
543 * Note that we don't process the pipe: the parser sees it as
544 * punctuation, but we don't in terms of typography.
546 if (sv[0] && '\0' == sv[1])
547 switch (sv[0]) {
548 case('('):
549 /* FALLTHROUGH */
550 case('['):
551 p->flags |= TERMP_NOSPACE;
552 break;
553 default:
554 break;
559 static void
560 adjbuf(struct termp *p, size_t sz)
563 if (0 == p->maxcols)
564 p->maxcols = 1024;
565 while (sz >= p->maxcols)
566 p->maxcols <<= 2;
568 p->buf = realloc(p->buf, p->maxcols);
569 if (NULL == p->buf) {
570 perror(NULL);
571 exit((int)MANDOCLEVEL_SYSERR);
576 static void
577 bufferc(struct termp *p, char c)
580 if (p->col + 1 >= p->maxcols)
581 adjbuf(p, p->col + 1);
583 p->buf[(int)p->col++] = c;
587 static void
588 encode(struct termp *p, const char *word, size_t sz)
590 enum termfont f;
591 int i;
594 * Encode and buffer a string of characters. If the current
595 * font mode is unset, buffer directly, else encode then buffer
596 * character by character.
599 if (TERMFONT_NONE == (f = term_fonttop(p))) {
600 if (p->col + sz >= p->maxcols)
601 adjbuf(p, p->col + sz);
602 memcpy(&p->buf[(int)p->col], word, sz);
603 p->col += sz;
604 return;
607 /* Pre-buffer, assuming worst-case. */
609 if (p->col + 1 + (sz * 3) >= p->maxcols)
610 adjbuf(p, p->col + 1 + (sz * 3));
612 for (i = 0; i < (int)sz; i++) {
613 if ( ! isgraph((u_char)word[i])) {
614 p->buf[(int)p->col++] = word[i];
615 continue;
618 if (TERMFONT_UNDER == f)
619 p->buf[(int)p->col++] = '_';
620 else
621 p->buf[(int)p->col++] = word[i];
623 p->buf[(int)p->col++] = 8;
624 p->buf[(int)p->col++] = word[i];
629 size_t
630 term_len(const struct termp *p, size_t sz)
633 return((*p->width)(p, ' ') * sz);
637 size_t
638 term_strlen(const struct termp *p, const char *cp)
640 size_t sz, ssz, rsz, i;
641 enum roffdeco d;
642 const char *seq, *rhs;
644 for (sz = 0; '\0' != *cp; )
646 * Account for escaped sequences within string length
647 * calculations. This follows the logic in term_word()
648 * as we must calculate the width of produced strings.
650 if ('\\' == *cp) {
651 seq = ++cp;
652 cp += a2roffdeco(&d, &seq, &ssz);
654 switch (d) {
655 case (DECO_RESERVED):
656 rhs = chars_res2str
657 (p->symtab, seq, ssz, &rsz);
658 break;
659 case (DECO_SPECIAL):
660 /* FALLTHROUGH */
661 case (DECO_SSPECIAL):
662 rhs = chars_spec2str
663 (p->symtab, seq, ssz, &rsz);
665 /* Allow for one-char escapes. */
666 if (DECO_SSPECIAL != d || rhs)
667 break;
669 rhs = seq;
670 rsz = ssz;
671 break;
672 default:
673 rhs = NULL;
674 break;
677 if (rhs)
678 for (i = 0; i < rsz; i++)
679 sz += (*p->width)(p, *rhs++);
680 } else if (ASCII_NBRSP == *cp) {
681 sz += (*p->width)(p, ' ');
682 cp++;
683 } else if (ASCII_HYPH == *cp) {
684 sz += (*p->width)(p, '-');
685 cp++;
686 } else
687 sz += (*p->width)(p, *cp++);
689 return(sz);
693 /* ARGSUSED */
694 size_t
695 term_vspan(const struct termp *p, const struct roffsu *su)
697 double r;
699 switch (su->unit) {
700 case (SCALE_CM):
701 r = su->scale * 2;
702 break;
703 case (SCALE_IN):
704 r = su->scale * 6;
705 break;
706 case (SCALE_PC):
707 r = su->scale;
708 break;
709 case (SCALE_PT):
710 r = su->scale / 8;
711 break;
712 case (SCALE_MM):
713 r = su->scale / 1000;
714 break;
715 case (SCALE_VS):
716 r = su->scale;
717 break;
718 default:
719 r = su->scale - 1;
720 break;
723 if (r < 0.0)
724 r = 0.0;
725 return(/* LINTED */(size_t)
730 size_t
731 term_hspan(const struct termp *p, const struct roffsu *su)
733 double v;
735 v = ((*p->hspan)(p, su));
736 if (v < 0.0)
737 v = 0.0;
738 return((size_t) /* LINTED */