]>
git.cameronkatri.com Git - mandoc.git/blob - term.c
973ad2c2b1c7d3f66081b614fac9048d6e5e41bc
1 /* $Id: term.c,v 1.84 2009/07/14 15:16:41 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 int man_run(struct termp
*,
29 extern int mdoc_run(struct termp
*,
32 static struct termp
*term_alloc(enum termenc
);
33 static void term_free(struct termp
*);
34 static void term_pword(struct termp
*, const char *, int);
35 static void term_pescape(struct termp
*,
36 const char *, int *, int);
37 static void term_nescape(struct termp
*,
38 const char *, size_t);
39 static void term_chara(struct termp
*, char);
40 static void term_encodea(struct termp
*, char);
41 static int term_isopendelim(const char *, int);
42 static int term_isclosedelim(const char *, int);
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
= term_ascii2htab();
62 return(man_run(p
, man
));
67 terminal_mdoc(void *arg
, const struct mdoc
*mdoc
)
71 p
= (struct termp
*)arg
;
72 if (NULL
== p
->symtab
)
73 p
->symtab
= term_ascii2htab();
75 return(mdoc_run(p
, mdoc
));
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 term_asciifree(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 term_isclosedelim(const char *p
, int len
)
149 term_isopendelim(const char *p
, int len
)
171 * Flush a line of text. A "line" is loosely defined as being something
172 * that should be followed by a newline, regardless of whether it's
173 * broken apart by newlines getting there. A line can also be a
174 * fragment of a columnar list.
176 * Specifically, a line is whatever's in p->buf of length p->col, which
177 * is zeroed after this function returns.
179 * The usage of termp:flags is as follows:
181 * - TERMP_NOLPAD: when beginning to write the line, don't left-pad the
182 * offset value. This is useful when doing columnar lists where the
183 * prior column has right-padded.
185 * - TERMP_NOBREAK: this is the most important and is used when making
186 * columns. In short: don't print a newline and instead pad to the
187 * right margin. Used in conjunction with TERMP_NOLPAD.
189 * - TERMP_DANGLE: don't newline when TERMP_NOBREAK is specified and
190 * the line is overrun, and don't pad-right if it's underrun.
192 * - TERMP_HANG: like TERMP_DANGLE, but doesn't newline when
193 * overruning, instead save the position and continue at that point
194 * when the next invocation.
196 * In-line line breaking:
198 * If TERMP_NOBREAK is specified and the line overruns the right
199 * margin, it will break and pad-right to the right margin after
200 * writing. If maxrmargin is violated, it will break and continue
201 * writing from the right-margin, which will lead to the above
202 * scenario upon exit.
204 * Otherwise, the line will break at the right margin. Extremely long
205 * lines will cause the system to emit a warning (TODO: hyphenate, if
208 * FIXME: newline breaks occur (in groff) also occur when a single
209 * space follows a NOBREAK!
212 term_flushln(struct termp
*p
)
215 size_t vbl
, vsz
, vis
, maxvis
, mmax
, bp
;
219 * First, establish the maximum columns of "visible" content.
220 * This is usually the difference between the right-margin and
221 * an indentation, but can be, for tagged lists or columns, a
222 * small set of values.
225 assert(p
->offset
< p
->rmargin
);
226 maxvis
= p
->rmargin
- p
->offset
;
227 mmax
= p
->maxrmargin
- p
->offset
;
228 bp
= TERMP_NOBREAK
& p
->flags
? mmax
: maxvis
;
237 * If in the standard case (left-justified), then begin with our
238 * indentation, otherwise (columns, etc.) just start spitting
242 if ( ! (p
->flags
& TERMP_NOLPAD
))
244 for (j
= 0; j
< (int)p
->offset
; j
++)
247 for (i
= 0; i
< (int)p
->col
; i
++) {
249 * Count up visible word characters. Control sequences
250 * (starting with the CSI) aren't counted. A space
251 * generates a non-printing word, which is valid (the
252 * space is printed according to regular spacing rules).
256 for (j
= i
, vsz
= 0; j
< (int)p
->col
; j
++) {
257 if (' ' == p
->buf
[j
])
259 else if (8 == p
->buf
[j
])
266 * Choose the number of blanks to prepend: no blank at the
267 * beginning of a line, one between words -- but do not
268 * actually write them yet.
270 vbl
= (size_t)(0 == vis
? 0 : 1);
273 * Find out whether we would exceed the right margin.
274 * If so, break to the next line. (TODO: hyphenate)
275 * Otherwise, write the chosen number of blanks now.
277 if (vis
&& vis
+ vbl
+ vsz
> bp
) {
279 if (TERMP_NOBREAK
& p
->flags
) {
280 for (j
= 0; j
< (int)p
->rmargin
; j
++)
282 vis
= p
->rmargin
- p
->offset
;
284 for (j
= 0; j
< (int)p
->offset
; j
++)
289 for (j
= 0; j
< (int)vbl
; j
++)
295 * Finally, write out the word.
297 for ( ; i
< (int)p
->col
; i
++) {
298 if (' ' == p
->buf
[i
])
306 * If we've overstepped our maximum visible no-break space, then
307 * cause a newline and offset at the right margin.
310 if ((TERMP_NOBREAK
& p
->flags
) && vis
>= maxvis
) {
311 if ( ! (TERMP_DANGLE
& p
->flags
) &&
312 ! (TERMP_HANG
& p
->flags
)) {
314 for (i
= 0; i
< (int)p
->rmargin
; i
++)
317 if (TERMP_HANG
& p
->flags
)
324 * If we're not to right-marginalise it (newline), then instead
325 * pad to the right margin and stay off.
328 if (p
->flags
& TERMP_NOBREAK
) {
329 if ( ! (TERMP_DANGLE
& p
->flags
))
330 for ( ; vis
< maxvis
; vis
++)
340 * A newline only breaks an existing line; it won't assert vertical
341 * space. All data in the output buffer is flushed prior to the newline
345 term_newln(struct termp
*p
)
348 p
->flags
|= TERMP_NOSPACE
;
350 p
->flags
&= ~TERMP_NOLPAD
;
354 p
->flags
&= ~TERMP_NOLPAD
;
359 * Asserts a vertical space (a full, empty line-break between lines).
360 * Note that if used twice, this will cause two blank spaces and so on.
361 * All data in the output buffer is flushed prior to the newline
365 term_vspace(struct termp
*p
)
374 * Break apart a word into "pwords" (partial-words, usually from
375 * breaking up a phrase into individual words) and, eventually, put them
376 * into the output buffer. If we're a literal word, then don't break up
377 * the word and put it verbatim into the output buffer.
380 term_word(struct termp
*p
, const char *word
)
384 len
= (int)strlen(word
);
386 if (p
->flags
& TERMP_LITERAL
) {
387 term_pword(p
, word
, len
);
392 for (j
= i
= 0; i
< len
; i
++) {
393 if (' ' != word
[i
]) {
398 /* Escaped spaces don't delimit... */
399 if (i
&& ' ' == word
[i
] && '\\' == word
[i
- 1]) {
407 term_pword(p
, &word
[i
- j
], j
);
412 term_pword(p
, &word
[i
- j
], j
);
418 * Determine the symbol indicated by an escape sequences, that is, one
419 * starting with a backslash. Once done, we pass this value into the
420 * output buffer by way of the symbol table.
423 term_nescape(struct termp
*p
, const char *word
, size_t len
)
429 rhs
= term_a2ascii(p
->symtab
, word
, len
, &sz
);
431 for (i
= 0; i
< (int)sz
; i
++)
432 term_encodea(p
, rhs
[i
]);
437 * Handle an escape sequence: determine its length and pass it to the
438 * escape-symbol look table. Note that we assume mdoc(3) has validated
439 * the escape sequence (we assert upon badly-formed escape sequences).
442 term_pescape(struct termp
*p
, const char *word
, int *i
, int len
)
449 if ('(' == word
[*i
]) {
454 term_nescape(p
, &word
[*i
], 2);
458 } else if ('*' == word
[*i
]) {
469 term_nescape(p
, &word
[*i
], 2);
475 term_nescape(p
, &word
[*i
], 1);
479 } else if ('f' == word
[*i
]) {
485 p
->flags
|= TERMP_BOLD
;
488 p
->flags
|= TERMP_UNDER
;
493 p
->flags
&= ~TERMP_STYLE
;
500 } else if ('[' != word
[*i
]) {
501 term_nescape(p
, &word
[*i
], 1);
506 for (j
= 0; word
[*i
] && ']' != word
[*i
]; (*i
)++, j
++)
512 term_nescape(p
, &word
[*i
- j
], (size_t)j
);
517 * Handle pwords, partial words, which may be either a single word or a
518 * phrase that cannot be broken down (such as a literal string). This
519 * handles word styling.
522 term_pword(struct termp
*p
, const char *word
, int len
)
526 if (term_isclosedelim(word
, len
))
527 if ( ! (TERMP_IGNDELIM
& p
->flags
))
528 p
->flags
|= TERMP_NOSPACE
;
530 if ( ! (TERMP_NOSPACE
& p
->flags
))
533 if ( ! (p
->flags
& TERMP_NONOSPACE
))
534 p
->flags
&= ~TERMP_NOSPACE
;
537 * If ANSI (word-length styling), then apply our style now,
541 for (i
= 0; i
< len
; i
++)
543 term_pescape(p
, word
, &i
, len
);
545 term_encodea(p
, word
[i
]);
547 if (term_isopendelim(word
, len
))
548 p
->flags
|= TERMP_NOSPACE
;
553 * Insert a single character into the line-buffer. If the buffer's
554 * space is exceeded, then allocate more space by doubling the buffer
558 term_chara(struct termp
*p
, char c
)
562 if (p
->col
+ 1 >= p
->maxcols
) {
566 p
->buf
= realloc(p
->buf
, s
);
571 p
->buf
[(int)(p
->col
)++] = c
;
576 term_encodea(struct termp
*p
, char c
)
579 if (TERMP_STYLE
& p
->flags
) {
580 if (TERMP_BOLD
& p
->flags
) {
584 if (TERMP_UNDER
& p
->flags
) {