]>
git.cameronkatri.com Git - mandoc.git/blob - term.c
1 /* $Id: term.c,v 1.101 2009/09/17 07:41:28 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
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.
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.
28 extern void man_run(struct termp
*,
30 extern void mdoc_run(struct termp
*,
33 static struct termp
*term_alloc(enum termenc
);
34 static void term_free(struct termp
*);
36 static void do_escaped(struct termp
*, const char **);
37 static void do_special(struct termp
*,
38 const char *, size_t);
39 static void do_reserved(struct termp
*,
40 const char *, size_t);
41 static void buffer(struct termp
*, char);
42 static void encode(struct termp
*, char);
49 return(term_alloc(TERMENC_ASCII
));
54 terminal_man(void *arg
, const struct man
*man
)
58 p
= (struct termp
*)arg
;
59 if (NULL
== p
->symtab
)
60 p
->symtab
= chars_init(CHARS_ASCII
);
67 terminal_mdoc(void *arg
, const struct mdoc
*mdoc
)
71 p
= (struct termp
*)arg
;
72 if (NULL
== p
->symtab
)
73 p
->symtab
= chars_init(CHARS_ASCII
);
80 terminal_free(void *arg
)
83 term_free((struct termp
*)arg
);
88 term_free(struct termp
*p
)
93 if (TERMENC_ASCII
== p
->enc
&& p
->symtab
)
94 chars_free(p
->symtab
);
100 static struct termp
*
101 term_alloc(enum termenc enc
)
105 if (NULL
== (p
= malloc(sizeof(struct termp
))))
107 bzero(p
, sizeof(struct termp
));
115 * Flush a line of text. A "line" is loosely defined as being something
116 * that should be followed by a newline, regardless of whether it's
117 * broken apart by newlines getting there. A line can also be a
118 * fragment of a columnar list.
120 * Specifically, a line is whatever's in p->buf of length p->col, which
121 * is zeroed after this function returns.
123 * The usage of termp:flags is as follows:
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.
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.
133 * - TERMP_TWOSPACE: when padding, make sure there are at least two
134 * space characters of padding. Otherwise, rather break the line.
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.
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.
143 * In-line line breaking:
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
149 * scenario upon exit.
151 * Otherwise, the line will break at the right margin. Extremely long
152 * lines will cause the system to emit a warning (TODO: hyphenate, if
156 term_flushln(struct termp
*p
)
159 size_t vbl
, vsz
, vis
, maxvis
, mmax
, bp
;
160 static int overstep
= 0;
163 * First, establish the maximum columns of "visible" content.
164 * This is usually the difference between the right-margin and
165 * an indentation, but can be, for tagged lists or columns, a
166 * small set of values.
169 assert(p
->offset
< p
->rmargin
);
170 assert((int)(p
->rmargin
- p
->offset
) - overstep
> 0);
172 maxvis
= /* LINTED */
173 p
->rmargin
- p
->offset
- overstep
;
175 p
->maxrmargin
- p
->offset
- overstep
;
177 bp
= TERMP_NOBREAK
& p
->flags
? mmax
: maxvis
;
182 * If in the standard case (left-justified), then begin with our
183 * indentation, otherwise (columns, etc.) just start spitting
187 if ( ! (p
->flags
& TERMP_NOLPAD
))
189 for (j
= 0; j
< (int)p
->offset
; j
++)
192 for (i
= 0; i
< (int)p
->col
; i
++) {
194 * Count up visible word characters. Control sequences
195 * (starting with the CSI) aren't counted. A space
196 * generates a non-printing word, which is valid (the
197 * space is printed according to regular spacing rules).
201 for (j
= i
, vsz
= 0; j
< (int)p
->col
; j
++) {
202 if (j
&& ' ' == p
->buf
[j
])
204 else if (8 == p
->buf
[j
])
211 * Choose the number of blanks to prepend: no blank at the
212 * beginning of a line, one between words -- but do not
213 * actually write them yet.
215 vbl
= (size_t)(0 == vis
? 0 : 1);
218 * Find out whether we would exceed the right margin.
219 * If so, break to the next line. (TODO: hyphenate)
220 * Otherwise, write the chosen number of blanks now.
222 if (vis
&& vis
+ vbl
+ vsz
> bp
) {
224 if (TERMP_NOBREAK
& p
->flags
) {
225 for (j
= 0; j
< (int)p
->rmargin
; j
++)
227 vis
= p
->rmargin
- p
->offset
;
229 for (j
= 0; j
< (int)p
->offset
; j
++)
234 for (j
= 0; j
< (int)vbl
; j
++)
240 * Finally, write out the word.
242 for ( ; i
< (int)p
->col
; i
++) {
243 if (' ' == p
->buf
[i
])
251 if ( ! (TERMP_NOBREAK
& p
->flags
)) {
256 if (TERMP_HANG
& p
->flags
) {
257 /* We need one blank after the tag. */
258 overstep
= /* LINTED */
262 * Behave exactly the same way as groff:
263 * If we have overstepped the margin, temporarily move
264 * it to the right and flag the rest of the line to be
266 * If we landed right at the margin, be happy.
267 * If we are one step before the margin, temporarily
268 * move it one step LEFT and flag the rest of the line
271 if (overstep
>= -1) {
272 assert((int)maxvis
+ overstep
>= 0);
278 } else if (TERMP_DANGLE
& p
->flags
)
282 if (maxvis
> vis
+ /* LINTED */
283 ((TERMP_TWOSPACE
& p
->flags
) ? 1 : 0))
284 for ( ; vis
< maxvis
; vis
++)
286 else { /* ...or newline break. */
288 for (i
= 0; i
< (int)p
->rmargin
; i
++)
295 * A newline only breaks an existing line; it won't assert vertical
296 * space. All data in the output buffer is flushed prior to the newline
300 term_newln(struct termp
*p
)
303 p
->flags
|= TERMP_NOSPACE
;
305 p
->flags
&= ~TERMP_NOLPAD
;
309 p
->flags
&= ~TERMP_NOLPAD
;
314 * Asserts a vertical space (a full, empty line-break between lines).
315 * Note that if used twice, this will cause two blank spaces and so on.
316 * All data in the output buffer is flushed prior to the newline
320 term_vspace(struct termp
*p
)
329 do_special(struct termp
*p
, const char *word
, size_t len
)
335 rhs
= chars_a2ascii(p
->symtab
, word
, len
, &sz
);
339 fputs("Unknown special character: ", stderr
);
340 for (i
= 0; i
< (int)len
; i
++)
341 fputc(word
[i
], stderr
);
346 for (i
= 0; i
< (int)sz
; i
++)
352 do_reserved(struct termp
*p
, const char *word
, size_t len
)
358 rhs
= chars_a2res(p
->symtab
, word
, len
, &sz
);
362 fputs("Unknown reserved word: ", stderr
);
363 for (i
= 0; i
< (int)len
; i
++)
364 fputc(word
[i
], stderr
);
369 for (i
= 0; i
< (int)sz
; i
++)
375 * Handle an escape sequence: determine its length and pass it to the
376 * escape-symbol look table. Note that we assume mdoc(3) has validated
377 * the escape sequence (we assert upon badly-formed escape sequences).
380 do_escaped(struct termp
*p
, const char **word
)
395 if (0 == *wp
|| 0 == *(wp
+ 1)) {
396 *word
= 0 == *wp
? wp
: wp
+ 1;
400 do_special(p
, wp
, 2);
404 } else if ('*' == *wp
) {
413 if (0 == *wp
|| 0 == *(wp
+ 1)) {
414 *word
= 0 == *wp
? wp
: wp
+ 1;
418 do_reserved(p
, wp
, 2);
425 do_reserved(p
, wp
, 1);
430 } else if ('f' == *wp
) {
446 p
->bold
= p
->under
= 0;
455 } else if ('[' != *wp
) {
456 do_special(p
, wp
, 1);
462 for (j
= 0; *wp
&& ']' != *wp
; wp
++, j
++)
471 do_special(p
, wp
- j
, (size_t)j
);
473 do_reserved(p
, wp
- j
, (size_t)j
);
479 * Handle pwords, partial words, which may be either a single word or a
480 * phrase that cannot be broken down (such as a literal string). This
481 * handles word styling.
484 term_word(struct termp
*p
, const char *word
)
490 if (word
[0] && 0 == word
[1])
509 if ( ! (TERMP_IGNDELIM
& p
->flags
))
510 p
->flags
|= TERMP_NOSPACE
;
516 if ( ! (TERMP_NOSPACE
& p
->flags
))
519 if ( ! (p
->flags
& TERMP_NONOSPACE
))
520 p
->flags
&= ~TERMP_NOSPACE
;
522 for ( ; *word
; word
++)
526 do_escaped(p
, &word
);
528 if (sv
[0] && 0 == sv
[1])
535 p
->flags
|= TERMP_NOSPACE
;
544 * Insert a single character into the line-buffer. If the buffer's
545 * space is exceeded, then allocate more space by doubling the buffer
549 buffer(struct termp
*p
, char c
)
553 if (p
->col
+ 1 >= p
->maxcols
) {
557 p
->buf
= realloc(p
->buf
, s
);
559 err(1, "realloc"); /* FIXME: shouldn't be here! */
562 p
->buf
[(int)(p
->col
)++] = c
;
567 encode(struct termp
*p
, char c
)