]>
git.cameronkatri.com Git - mandoc.git/blob - term.c
1 /* $Id: term.c,v 1.105 2009/10/13 10:57:25 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.
29 /* FIXME: accomodate non-breaking, non-collapsing white-space. */
30 /* FIXME: accomodate non-breaking, collapsing white-space. */
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_free(void *arg
)
56 term_free((struct termp
*)arg
);
61 term_free(struct termp
*p
)
67 chars_free(p
->symtab
);
74 term_alloc(enum termenc enc
)
78 if (NULL
== (p
= malloc(sizeof(struct termp
))))
80 bzero(p
, sizeof(struct termp
));
88 * Flush a line of text. A "line" is loosely defined as being something
89 * that should be followed by a newline, regardless of whether it's
90 * broken apart by newlines getting there. A line can also be a
91 * fragment of a columnar list.
93 * Specifically, a line is whatever's in p->buf of length p->col, which
94 * is zeroed after this function returns.
96 * The usage of termp:flags is as follows:
98 * - TERMP_NOLPAD: when beginning to write the line, don't left-pad the
99 * offset value. This is useful when doing columnar lists where the
100 * prior column has right-padded.
102 * - TERMP_NOBREAK: this is the most important and is used when making
103 * columns. In short: don't print a newline and instead pad to the
104 * right margin. Used in conjunction with TERMP_NOLPAD.
106 * - TERMP_TWOSPACE: when padding, make sure there are at least two
107 * space characters of padding. Otherwise, rather break the line.
109 * - TERMP_DANGLE: don't newline when TERMP_NOBREAK is specified and
110 * the line is overrun, and don't pad-right if it's underrun.
112 * - TERMP_HANG: like TERMP_DANGLE, but doesn't newline when
113 * overruning, instead save the position and continue at that point
114 * when the next invocation.
116 * In-line line breaking:
118 * If TERMP_NOBREAK is specified and the line overruns the right
119 * margin, it will break and pad-right to the right margin after
120 * writing. If maxrmargin is violated, it will break and continue
121 * writing from the right-margin, which will lead to the above
122 * scenario upon exit.
124 * Otherwise, the line will break at the right margin. Extremely long
125 * lines will cause the system to emit a warning (TODO: hyphenate, if
129 term_flushln(struct termp
*p
)
132 size_t vbl
, vsz
, vis
, maxvis
, mmax
, bp
, os
;
133 static int overstep
= 0;
136 * First, establish the maximum columns of "visible" content.
137 * This is usually the difference between the right-margin and
138 * an indentation, but can be, for tagged lists or columns, a
139 * small set of values.
142 assert(p
->offset
< p
->rmargin
);
143 assert((int)(p
->rmargin
- p
->offset
) - overstep
> 0);
145 /* Save the overstep. */
146 os
= (size_t)overstep
;
148 maxvis
= /* LINTED */
149 p
->rmargin
- p
->offset
- overstep
;
151 p
->maxrmargin
- p
->offset
- overstep
;
153 bp
= TERMP_NOBREAK
& p
->flags
? mmax
: maxvis
;
158 * If in the standard case (left-justified), then begin with our
159 * indentation, otherwise (columns, etc.) just start spitting
163 if ( ! (p
->flags
& TERMP_NOLPAD
))
165 for (j
= 0; j
< (int)p
->offset
; j
++)
168 for (i
= 0; i
< (int)p
->col
; i
++) {
170 * Count up visible word characters. Control sequences
171 * (starting with the CSI) aren't counted. A space
172 * generates a non-printing word, which is valid (the
173 * space is printed according to regular spacing rules).
177 for (j
= i
, vsz
= 0; j
< (int)p
->col
; j
++) {
178 if (j
&& ' ' == p
->buf
[j
])
180 else if (8 == p
->buf
[j
])
187 * Choose the number of blanks to prepend: no blank at the
188 * beginning of a line, one between words -- but do not
189 * actually write them yet.
191 vbl
= (size_t)(0 == vis
? 0 : 1);
194 * Find out whether we would exceed the right margin.
195 * If so, break to the next line. (TODO: hyphenate)
196 * Otherwise, write the chosen number of blanks now.
198 if (vis
&& vis
+ vbl
+ vsz
> bp
) {
200 if (TERMP_NOBREAK
& p
->flags
) {
201 for (j
= 0; j
< (int)p
->rmargin
; j
++)
203 vis
= p
->rmargin
- p
->offset
;
205 for (j
= 0; j
< (int)p
->offset
; j
++)
209 /* Remove the overstep width. */
213 for (j
= 0; j
< (int)vbl
; j
++)
219 * Finally, write out the word.
221 for ( ; i
< (int)p
->col
; i
++) {
222 if (' ' == p
->buf
[i
])
230 if ( ! (TERMP_NOBREAK
& p
->flags
)) {
235 if (TERMP_HANG
& p
->flags
) {
236 /* We need one blank after the tag. */
237 overstep
= /* LINTED */
241 * Behave exactly the same way as groff:
242 * If we have overstepped the margin, temporarily move
243 * it to the right and flag the rest of the line to be
245 * If we landed right at the margin, be happy.
246 * If we are one step before the margin, temporarily
247 * move it one step LEFT and flag the rest of the line
250 if (overstep
>= -1) {
251 assert((int)maxvis
+ overstep
>= 0);
257 } else if (TERMP_DANGLE
& p
->flags
)
261 if (maxvis
> vis
+ /* LINTED */
262 ((TERMP_TWOSPACE
& p
->flags
) ? 1 : 0))
263 for ( ; vis
< maxvis
; vis
++)
265 else { /* ...or newline break. */
267 for (i
= 0; i
< (int)p
->rmargin
; i
++)
274 * A newline only breaks an existing line; it won't assert vertical
275 * space. All data in the output buffer is flushed prior to the newline
279 term_newln(struct termp
*p
)
282 p
->flags
|= TERMP_NOSPACE
;
284 p
->flags
&= ~TERMP_NOLPAD
;
288 p
->flags
&= ~TERMP_NOLPAD
;
293 * Asserts a vertical space (a full, empty line-break between lines).
294 * Note that if used twice, this will cause two blank spaces and so on.
295 * All data in the output buffer is flushed prior to the newline
299 term_vspace(struct termp
*p
)
308 do_special(struct termp
*p
, const char *word
, size_t len
)
314 rhs
= chars_a2ascii(p
->symtab
, word
, len
, &sz
);
318 fputs("Unknown special character: ", stderr
);
319 for (i
= 0; i
< (int)len
; i
++)
320 fputc(word
[i
], stderr
);
325 for (i
= 0; i
< (int)sz
; i
++)
331 do_reserved(struct termp
*p
, const char *word
, size_t len
)
337 rhs
= chars_a2res(p
->symtab
, word
, len
, &sz
);
341 fputs("Unknown reserved word: ", stderr
);
342 for (i
= 0; i
< (int)len
; i
++)
343 fputc(word
[i
], stderr
);
348 for (i
= 0; i
< (int)sz
; i
++)
354 * Handle an escape sequence: determine its length and pass it to the
355 * escape-symbol look table. Note that we assume mdoc(3) has validated
356 * the escape sequence (we assert upon badly-formed escape sequences).
359 do_escaped(struct termp
*p
, const char **word
)
374 if (0 == *wp
|| 0 == *(wp
+ 1)) {
375 *word
= 0 == *wp
? wp
: wp
+ 1;
379 do_special(p
, wp
, 2);
383 } else if ('*' == *wp
) {
392 if (0 == *wp
|| 0 == *(wp
+ 1)) {
393 *word
= 0 == *wp
? wp
: wp
+ 1;
397 do_reserved(p
, wp
, 2);
404 do_reserved(p
, wp
, 1);
409 } else if ('f' == *wp
) {
425 p
->bold
= p
->under
= 0;
434 } else if ('[' != *wp
) {
435 do_special(p
, wp
, 1);
441 for (j
= 0; *wp
&& ']' != *wp
; wp
++, j
++)
450 do_special(p
, wp
- j
, (size_t)j
);
452 do_reserved(p
, wp
- j
, (size_t)j
);
458 * Handle pwords, partial words, which may be either a single word or a
459 * phrase that cannot be broken down (such as a literal string). This
460 * handles word styling.
463 term_word(struct termp
*p
, const char *word
)
469 if (word
[0] && 0 == word
[1])
488 if ( ! (TERMP_IGNDELIM
& p
->flags
))
489 p
->flags
|= TERMP_NOSPACE
;
495 if ( ! (TERMP_NOSPACE
& p
->flags
))
498 if ( ! (p
->flags
& TERMP_NONOSPACE
))
499 p
->flags
&= ~TERMP_NOSPACE
;
501 for ( ; *word
; word
++)
505 do_escaped(p
, &word
);
507 if (sv
[0] && 0 == sv
[1])
514 p
->flags
|= TERMP_NOSPACE
;
523 * Insert a single character into the line-buffer. If the buffer's
524 * space is exceeded, then allocate more space by doubling the buffer
528 buffer(struct termp
*p
, char c
)
532 if (p
->col
+ 1 >= p
->maxcols
) {
536 p
->buf
= realloc(p
->buf
, s
);
538 err(1, "realloc"); /* FIXME: shouldn't be here! */
541 p
->buf
[(int)(p
->col
)++] = c
;
546 encode(struct termp
*p
, char c
)