]>
git.cameronkatri.com Git - mandoc.git/blob - term.c
1 /* $Id: term.c,v 1.111 2009/10/26 07:18:23 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.
30 /* FIXME: accomodate non-breaking, non-collapsing white-space. */
31 /* FIXME: accomodate non-breaking, collapsing white-space. */
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_free(void *arg
)
57 term_free((struct termp
*)arg
);
62 term_free(struct termp
*p
)
68 chars_free(p
->symtab
);
75 term_alloc(enum termenc enc
)
79 if (NULL
== (p
= malloc(sizeof(struct termp
))))
81 bzero(p
, sizeof(struct termp
));
89 * Flush a line of text. A "line" is loosely defined as being something
90 * that should be followed by a newline, regardless of whether it's
91 * broken apart by newlines getting there. A line can also be a
92 * fragment of a columnar list.
94 * Specifically, a line is whatever's in p->buf of length p->col, which
95 * is zeroed after this function returns.
97 * The usage of termp:flags is as follows:
99 * - TERMP_NOLPAD: when beginning to write the line, don't left-pad the
100 * offset value. This is useful when doing columnar lists where the
101 * prior column has right-padded.
103 * - TERMP_NOBREAK: this is the most important and is used when making
104 * columns. In short: don't print a newline and instead pad to the
105 * right margin. Used in conjunction with TERMP_NOLPAD.
107 * - TERMP_TWOSPACE: when padding, make sure there are at least two
108 * space characters of padding. Otherwise, rather break the line.
110 * - TERMP_DANGLE: don't newline when TERMP_NOBREAK is specified and
111 * the line is overrun, and don't pad-right if it's underrun.
113 * - TERMP_HANG: like TERMP_DANGLE, but doesn't newline when
114 * overruning, instead save the position and continue at that point
115 * when the next invocation.
117 * In-line line breaking:
119 * If TERMP_NOBREAK is specified and the line overruns the right
120 * margin, it will break and pad-right to the right margin after
121 * writing. If maxrmargin is violated, it will break and continue
122 * writing from the right-margin, which will lead to the above
123 * scenario upon exit.
125 * Otherwise, the line will break at the right margin. Extremely long
126 * lines will cause the system to emit a warning (TODO: hyphenate, if
130 term_flushln(struct termp
*p
)
133 size_t vbl
, vsz
, vis
, maxvis
, mmax
, bp
;
134 static int overstep
= 0;
137 * First, establish the maximum columns of "visible" content.
138 * This is usually the difference between the right-margin and
139 * an indentation, but can be, for tagged lists or columns, a
140 * small set of values.
143 assert(p
->offset
< p
->rmargin
);
144 assert((int)(p
->rmargin
- p
->offset
) - overstep
> 0);
146 maxvis
= /* LINTED */
147 p
->rmargin
- p
->offset
- overstep
;
149 p
->maxrmargin
- p
->offset
- overstep
;
151 bp
= TERMP_NOBREAK
& p
->flags
? mmax
: maxvis
;
155 * If in the standard case (left-justified), then begin with our
156 * indentation, otherwise (columns, etc.) just start spitting
160 if ( ! (p
->flags
& TERMP_NOLPAD
))
162 for (j
= 0; j
< (int)p
->offset
; j
++)
165 for (i
= 0; i
< (int)p
->col
; i
++) {
167 * Count up visible word characters. Control sequences
168 * (starting with the CSI) aren't counted. A space
169 * generates a non-printing word, which is valid (the
170 * space is printed according to regular spacing rules).
174 for (j
= i
, vsz
= 0; j
< (int)p
->col
; j
++) {
175 if (j
&& ' ' == p
->buf
[j
])
177 else if (8 == p
->buf
[j
])
184 * Choose the number of blanks to prepend: no blank at the
185 * beginning of a line, one between words -- but do not
186 * actually write them yet.
188 vbl
= (size_t)(0 == vis
? 0 : 1);
191 * Find out whether we would exceed the right margin.
192 * If so, break to the next line. (TODO: hyphenate)
193 * Otherwise, write the chosen number of blanks now.
195 if (vis
&& vis
+ vbl
+ vsz
> bp
) {
197 if (TERMP_NOBREAK
& p
->flags
) {
198 for (j
= 0; j
< (int)p
->rmargin
; j
++)
200 vis
= p
->rmargin
- p
->offset
;
202 for (j
= 0; j
< (int)p
->offset
; j
++)
206 /* Remove the overstep width. */
210 for (j
= 0; j
< (int)vbl
; j
++)
216 * Finally, write out the word.
218 for ( ; i
< (int)p
->col
; i
++) {
219 if (' ' == p
->buf
[i
])
229 if ( ! (TERMP_NOBREAK
& p
->flags
)) {
234 if (TERMP_HANG
& p
->flags
) {
235 /* We need one blank after the tag. */
236 overstep
= /* LINTED */
240 * Behave exactly the same way as groff:
241 * If we have overstepped the margin, temporarily move
242 * it to the right and flag the rest of the line to be
244 * If we landed right at the margin, be happy.
245 * If we are one step before the margin, temporarily
246 * move it one step LEFT and flag the rest of the line
249 if (overstep
>= -1) {
250 assert((int)maxvis
+ overstep
>= 0);
256 } else if (TERMP_DANGLE
& p
->flags
)
260 if (maxvis
> vis
+ /* LINTED */
261 ((TERMP_TWOSPACE
& p
->flags
) ? 1 : 0))
262 for ( ; vis
< maxvis
; vis
++)
264 else { /* ...or newline break. */
266 for (i
= 0; i
< (int)p
->rmargin
; i
++)
273 * A newline only breaks an existing line; it won't assert vertical
274 * space. All data in the output buffer is flushed prior to the newline
278 term_newln(struct termp
*p
)
281 p
->flags
|= TERMP_NOSPACE
;
283 p
->flags
&= ~TERMP_NOLPAD
;
287 p
->flags
&= ~TERMP_NOLPAD
;
292 * Asserts a vertical space (a full, empty line-break between lines).
293 * Note that if used twice, this will cause two blank spaces and so on.
294 * All data in the output buffer is flushed prior to the newline
298 term_vspace(struct termp
*p
)
307 do_special(struct termp
*p
, const char *word
, size_t len
)
313 rhs
= chars_a2ascii(p
->symtab
, word
, len
, &sz
);
317 fputs("Unknown special character: ", stderr
);
318 for (i
= 0; i
< (int)len
; i
++)
319 fputc(word
[i
], stderr
);
324 for (i
= 0; i
< (int)sz
; i
++)
330 do_reserved(struct termp
*p
, const char *word
, size_t len
)
336 rhs
= chars_a2res(p
->symtab
, word
, len
, &sz
);
340 fputs("Unknown reserved word: ", stderr
);
341 for (i
= 0; i
< (int)len
; i
++)
342 fputc(word
[i
], stderr
);
347 for (i
= 0; i
< (int)sz
; i
++)
353 * Handle an escape sequence: determine its length and pass it to the
354 * escape-symbol look table. Note that we assume mdoc(3) has validated
355 * the escape sequence (we assert upon badly-formed escape sequences).
358 do_escaped(struct termp
*p
, const char **word
)
373 if (0 == *wp
|| 0 == *(wp
+ 1)) {
374 *word
= 0 == *wp
? wp
: wp
+ 1;
378 do_special(p
, wp
, 2);
382 } else if ('*' == *wp
) {
391 if (0 == *wp
|| 0 == *(wp
+ 1)) {
392 *word
= 0 == *wp
? wp
: wp
+ 1;
396 do_reserved(p
, wp
, 2);
403 do_reserved(p
, wp
, 1);
408 } else if ('f' == *wp
) {
424 p
->bold
= p
->under
= 0;
433 } else if ('[' != *wp
) {
434 do_special(p
, wp
, 1);
440 for (j
= 0; *wp
&& ']' != *wp
; wp
++, j
++)
449 do_special(p
, wp
- j
, (size_t)j
);
451 do_reserved(p
, wp
- j
, (size_t)j
);
457 * Handle pwords, partial words, which may be either a single word or a
458 * phrase that cannot be broken down (such as a literal string). This
459 * handles word styling.
462 term_word(struct termp
*p
, const char *word
)
468 if (word
[0] && 0 == word
[1])
487 if ( ! (TERMP_IGNDELIM
& p
->flags
))
488 p
->flags
|= TERMP_NOSPACE
;
494 if ( ! (TERMP_NOSPACE
& p
->flags
))
497 if ( ! (p
->flags
& TERMP_NONOSPACE
))
498 p
->flags
&= ~TERMP_NOSPACE
;
500 for ( ; *word
; word
++)
504 do_escaped(p
, &word
);
506 if (sv
[0] && 0 == sv
[1])
513 p
->flags
|= TERMP_NOSPACE
;
522 * Insert a single character into the line-buffer. If the buffer's
523 * space is exceeded, then allocate more space by doubling the buffer
527 buffer(struct termp
*p
, char c
)
531 if (p
->col
+ 1 >= p
->maxcols
) {
535 p
->buf
= realloc(p
->buf
, s
);
537 err(1, "realloc"); /* FIXME: shouldn't be here! */
540 p
->buf
[(int)(p
->col
)++] = c
;
545 encode(struct termp
*p
, char c
)
563 term_vspan(const struct roffsu
*su
)
581 r
= su
->scale
/ 1000;
593 return(/* LINTED */(size_t)
599 term_hspan(const struct roffsu
*su
)
603 /* XXX: CM, IN, and PT are approximations. */
610 /* XXX: this is an approximation. */
614 r
= (10 * su
->scale
) / 6;
617 r
= (10 * su
->scale
) / 72;
620 r
= su
->scale
/ 1000; /* FIXME: double-check. */
623 r
= su
->scale
* 2 - 1; /* FIXME: double-check. */
632 return((size_t)/* LINTED */