]>
git.cameronkatri.com Git - mandoc.git/blob - terminal.c
1 /* $Id: terminal.c,v 1.10 2009/03/26 14:44:41 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the
7 * above copyright notice and this permission notice appear in all
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
29 extern int man_run(struct termp
*,
31 extern int mdoc_run(struct termp
*,
34 static struct termp
*term_alloc(enum termenc
);
35 static void term_free(struct termp
*);
36 static void term_pword(struct termp
*, const char *, int);
37 static void term_pescape(struct termp
*,
38 const char *, int *, int);
39 static void term_nescape(struct termp
*,
40 const char *, size_t);
41 static void term_chara(struct termp
*, char);
42 static void term_stringa(struct termp
*,
43 const char *, size_t);
44 static int term_isopendelim(const char *, int);
45 static int term_isclosedelim(const char *, int);
52 return(term_alloc(TERMENC_ASCII
));
57 terminal_run(void *arg
, const struct man
*man
,
58 const struct mdoc
*mdoc
)
62 p
= (struct termp
*)arg
;
64 if (NULL
== p
->symtab
)
65 p
->symtab
= term_ascii2htab();
68 return(man_run(p
, man
));
70 return(mdoc_run(p
, mdoc
));
77 terminal_free(void *arg
)
80 term_free((struct termp
*)arg
);
85 term_free(struct termp
*p
)
90 if (TERMENC_ASCII
== p
->enc
&& p
->symtab
)
91 term_asciifree(p
->symtab
);
98 term_alloc(enum termenc enc
)
102 if (NULL
== (p
= malloc(sizeof(struct termp
))))
104 bzero(p
, sizeof(struct termp
));
112 term_isclosedelim(const char *p
, int len
)
146 term_isopendelim(const char *p
, int len
)
168 * Flush a line of text. A "line" is loosely defined as being something
169 * that should be followed by a newline, regardless of whether it's
170 * broken apart by newlines getting there. A line can also be a
171 * fragment of a columnar list.
173 * Specifically, a line is whatever's in p->buf of length p->col, which
174 * is zeroed after this function returns.
176 * The variables TERMP_NOLPAD, TERMP_LITERAL and TERMP_NOBREAK are of
177 * critical importance here. Their behaviour follows:
179 * - TERMP_NOLPAD: when beginning to write the line, don't left-pad the
180 * offset value. This is useful when doing columnar lists where the
181 * prior column has right-padded.
183 * - TERMP_NOBREAK: this is the most important and is used when making
184 * columns. In short: don't print a newline and instead pad to the
185 * right margin. Used in conjunction with TERMP_NOLPAD.
187 * - TERMP_NONOBREAK: don't newline when TERMP_NOBREAK is specified.
189 * In-line line breaking:
191 * If TERMP_NOBREAK is specified and the line overruns the right
192 * margin, it will break and pad-right to the right margin after
193 * writing. If maxrmargin is violated, it will break and continue
194 * writing from the right-margin, which will lead to the above
195 * scenario upon exit.
197 * Otherwise, the line will break at the right margin. Extremely long
198 * lines will cause the system to emit a warning (TODO: hyphenate, if
202 term_flushln(struct termp
*p
)
205 size_t vsz
, vis
, maxvis
, mmax
, bp
;
208 * First, establish the maximum columns of "visible" content.
209 * This is usually the difference between the right-margin and
210 * an indentation, but can be, for tagged lists or columns, a
211 * small set of values.
214 assert(p
->offset
< p
->rmargin
);
215 maxvis
= p
->rmargin
- p
->offset
;
216 mmax
= p
->maxrmargin
- p
->offset
;
217 bp
= TERMP_NOBREAK
& p
->flags
? mmax
: maxvis
;
221 * If in the standard case (left-justified), then begin with our
222 * indentation, otherwise (columns, etc.) just start spitting
226 if ( ! (p
->flags
& TERMP_NOLPAD
))
228 for (j
= 0; j
< (int)p
->offset
; j
++)
231 for (i
= 0; i
< (int)p
->col
; i
++) {
233 * Count up visible word characters. Control sequences
234 * (starting with the CSI) aren't counted. A space
235 * generates a non-printing word, which is valid (the
236 * space is printed according to regular spacing rules).
240 for (j
= i
, vsz
= 0; j
< (int)p
->col
; j
++) {
241 if (' ' == p
->buf
[j
])
243 else if (8 == p
->buf
[j
])
250 * Do line-breaking. If we're greater than our
251 * break-point and already in-line, break to the next
252 * line and start writing. If we're at the line start,
253 * then write out the word (TODO: hyphenate) and break
254 * in a subsequent loop invocation.
257 if ( ! (TERMP_NOBREAK
& p
->flags
)) {
258 if (vis
&& vis
+ vsz
> bp
) {
260 for (j
= 0; j
< (int)p
->offset
; j
++)
264 } else if (vis
&& vis
+ vsz
> bp
) {
266 for (j
= 0; j
< (int)p
->rmargin
; j
++)
268 vis
= p
->rmargin
- p
->offset
;
272 * Write out the word and a trailing space. Omit the
273 * space if we're the last word in the line or beyond
277 for ( ; i
< (int)p
->col
; i
++) {
278 if (' ' == p
->buf
[i
])
283 if (i
< (int)p
->col
&& vis
<= bp
) {
290 * If we've overstepped our maximum visible no-break space, then
291 * cause a newline and offset at the right margin.
294 if ((TERMP_NOBREAK
& p
->flags
) && vis
>= maxvis
) {
295 if ( ! (TERMP_NONOBREAK
& p
->flags
)) {
297 for (i
= 0; i
< (int)p
->rmargin
; i
++)
305 * If we're not to right-marginalise it (newline), then instead
306 * pad to the right margin and stay off.
309 if (p
->flags
& TERMP_NOBREAK
) {
310 if ( ! (TERMP_NONOBREAK
& p
->flags
))
311 for ( ; vis
< maxvis
; vis
++)
321 * A newline only breaks an existing line; it won't assert vertical
322 * space. All data in the output buffer is flushed prior to the newline
326 term_newln(struct termp
*p
)
329 p
->flags
|= TERMP_NOSPACE
;
331 p
->flags
&= ~TERMP_NOLPAD
;
335 p
->flags
&= ~TERMP_NOLPAD
;
340 * Asserts a vertical space (a full, empty line-break between lines).
341 * Note that if used twice, this will cause two blank spaces and so on.
342 * All data in the output buffer is flushed prior to the newline
346 term_vspace(struct termp
*p
)
355 * Break apart a word into "pwords" (partial-words, usually from
356 * breaking up a phrase into individual words) and, eventually, put them
357 * into the output buffer. If we're a literal word, then don't break up
358 * the word and put it verbatim into the output buffer.
361 term_word(struct termp
*p
, const char *word
)
365 len
= (int)strlen(word
);
367 if (p
->flags
& TERMP_LITERAL
) {
368 term_pword(p
, word
, len
);
373 for (j
= i
= 0; i
< len
; i
++) {
374 if (' ' != word
[i
]) {
379 /* Escaped spaces don't delimit... */
380 if (i
&& ' ' == word
[i
] && '\\' == word
[i
- 1]) {
388 term_pword(p
, &word
[i
- j
], j
);
393 term_pword(p
, &word
[i
- j
], j
);
399 * Determine the symbol indicated by an escape sequences, that is, one
400 * starting with a backslash. Once done, we pass this value into the
401 * output buffer by way of the symbol table.
404 term_nescape(struct termp
*p
, const char *word
, size_t len
)
409 if (NULL
== (rhs
= term_a2ascii(p
->symtab
, word
, len
, &sz
)))
411 term_stringa(p
, rhs
, sz
);
416 * Handle an escape sequence: determine its length and pass it to the
417 * escape-symbol look table. Note that we assume mdoc(3) has validated
418 * the escape sequence (we assert upon badly-formed escape sequences).
421 term_pescape(struct termp
*p
, const char *word
, int *i
, int len
)
428 if ('(' == word
[*i
]) {
433 term_nescape(p
, &word
[*i
], 2);
437 } else if ('*' == word
[*i
]) {
448 term_nescape(p
, &word
[*i
], 2);
454 term_nescape(p
, &word
[*i
], 1);
458 } else if ('[' != word
[*i
]) {
459 term_nescape(p
, &word
[*i
], 1);
464 for (j
= 0; word
[*i
] && ']' != word
[*i
]; (*i
)++, j
++)
470 term_nescape(p
, &word
[*i
- j
], (size_t)j
);
475 * Handle pwords, partial words, which may be either a single word or a
476 * phrase that cannot be broken down (such as a literal string). This
477 * handles word styling.
480 term_pword(struct termp
*p
, const char *word
, int len
)
484 if (term_isclosedelim(word
, len
))
485 if ( ! (TERMP_IGNDELIM
& p
->flags
))
486 p
->flags
|= TERMP_NOSPACE
;
488 if ( ! (TERMP_NOSPACE
& p
->flags
))
491 if ( ! (p
->flags
& TERMP_NONOSPACE
))
492 p
->flags
&= ~TERMP_NOSPACE
;
495 * If ANSI (word-length styling), then apply our style now,
499 for (i
= 0; i
< len
; i
++) {
500 if ('\\' == word
[i
]) {
501 term_pescape(p
, word
, &i
, len
);
505 if (TERMP_STYLE
& p
->flags
) {
506 if (TERMP_BOLD
& p
->flags
) {
507 term_chara(p
, word
[i
]);
510 if (TERMP_UNDER
& p
->flags
) {
516 term_chara(p
, word
[i
]);
519 if (term_isopendelim(word
, len
))
520 p
->flags
|= TERMP_NOSPACE
;
525 * Like term_chara() but for arbitrary-length buffers. Resize the
526 * buffer by a factor of two (if the buffer is less than that) or the
530 term_stringa(struct termp
*p
, const char *c
, size_t sz
)
538 if (p
->col
+ sz
>= p
->maxcols
) {
541 s
= sz
> p
->maxcols
* 2 ? sz
: p
->maxcols
* 2;
542 p
->buf
= realloc(p
->buf
, s
);
548 (void)memcpy(&p
->buf
[(int)p
->col
], c
, sz
);
554 * Insert a single character into the line-buffer. If the buffer's
555 * space is exceeded, then allocate more space by doubling the buffer
559 term_chara(struct termp
*p
, char c
)
563 if (p
->col
+ 1 >= p
->maxcols
) {
567 p
->buf
= realloc(p
->buf
, s
);
572 p
->buf
[(int)(p
->col
)++] = c
;