]>
git.cameronkatri.com Git - mandoc.git/blob - term.c
6bfd373d64276f57c079287b13e984538a39f09e
1 /* $Id: term.c,v 1.100 2009/09/16 15:08:31 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.
27 extern void man_run(struct termp
*,
29 extern void mdoc_run(struct termp
*,
32 static struct termp
*term_alloc(enum termenc
);
33 static void term_free(struct termp
*);
35 static void do_escaped(struct termp
*, const char **);
36 static void do_special(struct termp
*,
37 const char *, size_t);
38 static void do_reserved(struct termp
*,
39 const char *, size_t);
40 static void buffer(struct termp
*, char);
41 static void encode(struct termp
*, char);
48 return(term_alloc(TERMENC_ASCII
));
53 terminal_man(void *arg
, const struct man
*man
)
57 p
= (struct termp
*)arg
;
58 if (NULL
== p
->symtab
)
59 p
->symtab
= term_ascii2htab();
66 terminal_mdoc(void *arg
, const struct mdoc
*mdoc
)
70 p
= (struct termp
*)arg
;
71 if (NULL
== p
->symtab
)
72 p
->symtab
= term_ascii2htab();
79 terminal_free(void *arg
)
82 term_free((struct termp
*)arg
);
87 term_free(struct termp
*p
)
92 if (TERMENC_ASCII
== p
->enc
&& p
->symtab
)
93 term_asciifree(p
->symtab
);
100 term_alloc(enum termenc enc
)
104 if (NULL
== (p
= malloc(sizeof(struct termp
))))
106 bzero(p
, sizeof(struct termp
));
114 * Flush a line of text. A "line" is loosely defined as being something
115 * that should be followed by a newline, regardless of whether it's
116 * broken apart by newlines getting there. A line can also be a
117 * fragment of a columnar list.
119 * Specifically, a line is whatever's in p->buf of length p->col, which
120 * is zeroed after this function returns.
122 * The usage of termp:flags is as follows:
124 * - TERMP_NOLPAD: when beginning to write the line, don't left-pad the
125 * offset value. This is useful when doing columnar lists where the
126 * prior column has right-padded.
128 * - TERMP_NOBREAK: this is the most important and is used when making
129 * columns. In short: don't print a newline and instead pad to the
130 * right margin. Used in conjunction with TERMP_NOLPAD.
132 * - TERMP_TWOSPACE: when padding, make sure there are at least two
133 * space characters of padding. Otherwise, rather break the line.
135 * - TERMP_DANGLE: don't newline when TERMP_NOBREAK is specified and
136 * the line is overrun, and don't pad-right if it's underrun.
138 * - TERMP_HANG: like TERMP_DANGLE, but doesn't newline when
139 * overruning, instead save the position and continue at that point
140 * when the next invocation.
142 * In-line line breaking:
144 * If TERMP_NOBREAK is specified and the line overruns the right
145 * margin, it will break and pad-right to the right margin after
146 * writing. If maxrmargin is violated, it will break and continue
147 * writing from the right-margin, which will lead to the above
148 * scenario upon exit.
150 * Otherwise, the line will break at the right margin. Extremely long
151 * lines will cause the system to emit a warning (TODO: hyphenate, if
155 term_flushln(struct termp
*p
)
158 size_t vbl
, vsz
, vis
, maxvis
, mmax
, bp
;
159 static int overstep
= 0;
162 * First, establish the maximum columns of "visible" content.
163 * This is usually the difference between the right-margin and
164 * an indentation, but can be, for tagged lists or columns, a
165 * small set of values.
168 assert(p
->offset
< p
->rmargin
);
169 assert((int)(p
->rmargin
- p
->offset
) - overstep
> 0);
171 maxvis
= /* LINTED */
172 p
->rmargin
- p
->offset
- overstep
;
174 p
->maxrmargin
- p
->offset
- overstep
;
176 bp
= TERMP_NOBREAK
& p
->flags
? mmax
: maxvis
;
181 * If in the standard case (left-justified), then begin with our
182 * indentation, otherwise (columns, etc.) just start spitting
186 if ( ! (p
->flags
& TERMP_NOLPAD
))
188 for (j
= 0; j
< (int)p
->offset
; j
++)
191 for (i
= 0; i
< (int)p
->col
; i
++) {
193 * Count up visible word characters. Control sequences
194 * (starting with the CSI) aren't counted. A space
195 * generates a non-printing word, which is valid (the
196 * space is printed according to regular spacing rules).
200 for (j
= i
, vsz
= 0; j
< (int)p
->col
; j
++) {
201 if (j
&& ' ' == p
->buf
[j
])
203 else if (8 == p
->buf
[j
])
210 * Choose the number of blanks to prepend: no blank at the
211 * beginning of a line, one between words -- but do not
212 * actually write them yet.
214 vbl
= (size_t)(0 == vis
? 0 : 1);
217 * Find out whether we would exceed the right margin.
218 * If so, break to the next line. (TODO: hyphenate)
219 * Otherwise, write the chosen number of blanks now.
221 if (vis
&& vis
+ vbl
+ vsz
> bp
) {
223 if (TERMP_NOBREAK
& p
->flags
) {
224 for (j
= 0; j
< (int)p
->rmargin
; j
++)
226 vis
= p
->rmargin
- p
->offset
;
228 for (j
= 0; j
< (int)p
->offset
; j
++)
233 for (j
= 0; j
< (int)vbl
; j
++)
239 * Finally, write out the word.
241 for ( ; i
< (int)p
->col
; i
++) {
242 if (' ' == p
->buf
[i
])
250 if ( ! (TERMP_NOBREAK
& p
->flags
)) {
255 if (TERMP_HANG
& p
->flags
) {
256 /* We need one blank after the tag. */
257 overstep
= /* LINTED */
261 * Behave exactly the same way as groff:
262 * If we have overstepped the margin, temporarily move
263 * it to the right and flag the rest of the line to be
265 * If we landed right at the margin, be happy.
266 * If we are one step before the margin, temporarily
267 * move it one step LEFT and flag the rest of the line
270 if (overstep
>= -1) {
271 assert((int)maxvis
+ overstep
>= 0);
277 } else if (TERMP_DANGLE
& p
->flags
)
281 if (maxvis
> vis
+ /* LINTED */
282 ((TERMP_TWOSPACE
& p
->flags
) ? 1 : 0))
283 for ( ; vis
< maxvis
; vis
++)
285 else { /* ...or newline break. */
287 for (i
= 0; i
< (int)p
->rmargin
; i
++)
294 * A newline only breaks an existing line; it won't assert vertical
295 * space. All data in the output buffer is flushed prior to the newline
299 term_newln(struct termp
*p
)
302 p
->flags
|= TERMP_NOSPACE
;
304 p
->flags
&= ~TERMP_NOLPAD
;
308 p
->flags
&= ~TERMP_NOLPAD
;
313 * Asserts a vertical space (a full, empty line-break between lines).
314 * Note that if used twice, this will cause two blank spaces and so on.
315 * All data in the output buffer is flushed prior to the newline
319 term_vspace(struct termp
*p
)
328 do_special(struct termp
*p
, const char *word
, size_t len
)
334 rhs
= term_a2ascii(p
->symtab
, word
, len
, &sz
);
338 fputs("Unknown special character: ", stderr
);
339 for (i
= 0; i
< (int)len
; i
++)
340 fputc(word
[i
], stderr
);
345 for (i
= 0; i
< (int)sz
; i
++)
351 do_reserved(struct termp
*p
, const char *word
, size_t len
)
357 rhs
= term_a2res(p
->symtab
, word
, len
, &sz
);
361 fputs("Unknown reserved word: ", stderr
);
362 for (i
= 0; i
< (int)len
; i
++)
363 fputc(word
[i
], stderr
);
368 for (i
= 0; i
< (int)sz
; i
++)
374 * Handle an escape sequence: determine its length and pass it to the
375 * escape-symbol look table. Note that we assume mdoc(3) has validated
376 * the escape sequence (we assert upon badly-formed escape sequences).
379 do_escaped(struct termp
*p
, const char **word
)
394 if (0 == *wp
|| 0 == *(wp
+ 1)) {
395 *word
= 0 == *wp
? wp
: wp
+ 1;
399 do_special(p
, wp
, 2);
403 } else if ('*' == *wp
) {
412 if (0 == *wp
|| 0 == *(wp
+ 1)) {
413 *word
= 0 == *wp
? wp
: wp
+ 1;
417 do_reserved(p
, wp
, 2);
424 do_reserved(p
, wp
, 1);
429 } else if ('f' == *wp
) {
445 p
->bold
= p
->under
= 0;
454 } else if ('[' != *wp
) {
455 do_special(p
, wp
, 1);
461 for (j
= 0; *wp
&& ']' != *wp
; wp
++, j
++)
470 do_special(p
, wp
- j
, (size_t)j
);
472 do_reserved(p
, wp
- j
, (size_t)j
);
478 * Handle pwords, partial words, which may be either a single word or a
479 * phrase that cannot be broken down (such as a literal string). This
480 * handles word styling.
483 term_word(struct termp
*p
, const char *word
)
489 if (word
[0] && 0 == word
[1])
508 if ( ! (TERMP_IGNDELIM
& p
->flags
))
509 p
->flags
|= TERMP_NOSPACE
;
515 if ( ! (TERMP_NOSPACE
& p
->flags
))
518 if ( ! (p
->flags
& TERMP_NONOSPACE
))
519 p
->flags
&= ~TERMP_NOSPACE
;
521 for ( ; *word
; word
++)
525 do_escaped(p
, &word
);
527 if (sv
[0] && 0 == sv
[1])
534 p
->flags
|= TERMP_NOSPACE
;
543 * Insert a single character into the line-buffer. If the buffer's
544 * space is exceeded, then allocate more space by doubling the buffer
548 buffer(struct termp
*p
, char c
)
552 if (p
->col
+ 1 >= p
->maxcols
) {
556 p
->buf
= realloc(p
->buf
, s
);
558 err(1, "realloc"); /* FIXME: shouldn't be here! */
561 p
->buf
[(int)(p
->col
)++] = c
;
566 encode(struct termp
*p
, char c
)