]>
git.cameronkatri.com Git - mandoc.git/blob - term.c
1 /* $Id: term.c,v 1.7 2009/02/22 14:31:08 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
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.
39 static void termprint_r(struct termp
*,
40 const struct mdoc_meta
*,
41 const struct mdoc_node
*);
42 static void termprint_header(struct termp
*,
43 const struct mdoc_meta
*);
44 static void termprint_footer(struct termp
*,
45 const struct mdoc_meta
*);
47 static void pword(struct termp
*, const char *, size_t);
48 static void pescape(struct termp
*,
49 const char *, size_t *, size_t);
50 static void chara(struct termp
*, char);
51 static void style(struct termp
*, enum termstyle
);
54 extern size_t strlcat(char *, const char *, size_t);
55 extern size_t strlcpy(char *, const char *, size_t);
59 flushln(struct termp
*p
)
61 size_t i
, j
, vsz
, vis
, maxvis
;
64 * First, establish the maximum columns of "visible" content.
65 * This is usually the difference between the right-margin and
66 * an indentation, but can be, for tagged lists or columns, a
67 * small set of values.
70 assert(p
->offset
< p
->rmargin
);
71 maxvis
= p
->rmargin
- p
->offset
;
75 * If in the standard case (left-justified), then begin with our
76 * indentation, otherwise (columns, etc.) just start spitting
80 if ( ! (p
->flags
& TERMP_NOLPAD
))
82 for (j
= 0; j
< p
->offset
; j
++)
85 for (i
= 0; i
< p
->col
; i
++) {
87 * Count up visible word characters. Control sequences
88 * (starting with the CSI) aren't counted.
90 assert( ! isspace(p
->buf
[i
]));
93 for (j
= i
, vsz
= 0; j
< p
->col
; j
++) {
94 if (isspace(p
->buf
[j
]))
96 else if (27 == p
->buf
[j
]) {
97 assert(j
+ 4 <= p
->col
);
105 * If a word is too long and we're within a line, put it
106 * on the next line. Puke if we're being asked to write
107 * something that will exceed the right margin (i.e.,
108 * from a fresh line or when we're not allowed to break
109 * the line with TERMP_NOBREAK).
112 if (vis
&& vis
+ vsz
>= maxvis
) {
114 if (p
->flags
& TERMP_NOBREAK
)
115 errx(1, "word breaks right margin");
117 for (j
= 0; j
< p
->offset
; j
++)
120 } else if (vis
+ vsz
>= maxvis
) {
122 errx(1, "word breaks right margin");
126 * Write out the word and a trailing space. Omit the
127 * space if we're the last word in the line.
130 for ( ; i
< p
->col
; i
++) {
131 if (isspace(p
->buf
[i
]))
143 * If we're not to right-marginalise it (newline), then instead
144 * pad to the right margin and stay off.
147 if (p
->flags
& TERMP_NOBREAK
) {
148 for ( ; vis
<= maxvis
; vis
++)
158 newln(struct termp
*p
)
162 * A newline only breaks an existing line; it won't assert
165 p
->flags
|= TERMP_NOSPACE
;
173 vspace(struct termp
*p
)
177 * Asserts a vertical space (a full, empty line-break between
186 chara(struct termp
*p
, char c
)
189 /* TODO: dynamically expand the buffer. */
190 if (p
->col
+ 1 >= p
->maxcols
)
191 errx(1, "line overrun");
192 p
->buf
[(p
->col
)++] = c
;
197 style(struct termp
*p
, enum termstyle esc
)
200 if (p
->col
+ 4 >= p
->maxcols
)
201 errx(1, "line overrun");
203 p
->buf
[(p
->col
)++] = 27;
204 p
->buf
[(p
->col
)++] = '[';
207 p
->buf
[(p
->col
)++] = '0';
210 p
->buf
[(p
->col
)++] = '1';
212 case (STYLE_UNDERLINE
):
213 p
->buf
[(p
->col
)++] = '4';
219 p
->buf
[(p
->col
)++] = 'm';
224 pescape(struct termp
*p
, const char *word
, size_t *i
, size_t len
)
230 if ('(' == word
[*i
]) {
231 /* Two-character escapes. */
233 assert(*i
+ 1 < len
);
235 if ('r' == word
[*i
] && 'B' == word
[*i
+ 1])
237 else if ('l' == word
[*i
] && 'B' == word
[*i
+ 1])
243 } else if ('[' != word
[*i
]) {
244 /* One-character escapes. */
261 /* n-character escapes. */
266 pword(struct termp
*p
, const char *word
, size_t len
)
272 if ( ! (p
->flags
& TERMP_NOSPACE
))
275 p
->flags
&= ~TERMP_NOSPACE
;
277 if (p
->flags
& TERMP_BOLD
)
278 style(p
, STYLE_BOLD
);
279 if (p
->flags
& TERMP_UNDERLINE
)
280 style(p
, STYLE_UNDERLINE
);
282 for (i
= 0; i
< len
; i
++) {
283 if ('\\' == word
[i
]) {
284 pescape(p
, word
, &i
, len
);
290 if (p
->flags
& TERMP_BOLD
||
291 p
->flags
& TERMP_UNDERLINE
)
292 style(p
, STYLE_CLEAR
);
297 word(struct termp
*p
, const char *word
)
301 if (mdoc_isdelim(word
))
302 p
->flags
|= TERMP_NOSPACE
;
308 for (j
= i
= 0; i
< len
; i
++) {
309 if ( ! isspace(word
[i
])) {
316 pword(p
, &word
[i
- j
], j
);
321 pword(p
, &word
[i
- j
], j
);
327 termprint_r(struct termp
*p
, const struct mdoc_meta
*meta
,
328 const struct mdoc_node
*node
)
332 /* Pre-processing. */
336 if (MDOC_TEXT
!= node
->type
) {
337 if (termacts
[node
->tok
].pre
)
338 if ( ! (*termacts
[node
->tok
].pre
)(p
, meta
, node
))
340 } else /* MDOC_TEXT == node->type */
341 word(p
, node
->data
.text
.string
);
345 if (dochild
&& node
->child
)
346 termprint_r(p
, meta
, node
->child
);
348 /* Post-processing. */
350 if (MDOC_TEXT
!= node
->type
)
351 if (termacts
[node
->tok
].post
)
352 (*termacts
[node
->tok
].post
)(p
, meta
, node
);
357 termprint_r(p
, meta
, node
->next
);
362 termprint_footer(struct termp
*p
, const struct mdoc_meta
*meta
)
366 size_t sz
, osz
, ssz
, i
;
368 if (NULL
== (buf
= malloc(p
->rmargin
)))
370 if (NULL
== (os
= malloc(p
->rmargin
)))
373 tm
= localtime(&meta
->date
);
376 if (0 == strftime(buf
, p
->rmargin
, "%B %d, %Y", tm
))
378 if (NULL
== strftime(buf
, p
->rmargin
, "%B %d, %Y", tm
))
382 osz
= strlcpy(os
, meta
->os
, p
->rmargin
);
387 if (ssz
> p
->rmargin
) {
393 ssz
= p
->rmargin
- ssz
+ 1;
397 for (i
= 0; i
< ssz
; i
++)
409 termprint_header(struct termp
*p
, const struct mdoc_meta
*meta
)
412 const char *pp
, *msec
;
413 size_t ssz
, tsz
, ttsz
, i
;;
415 if (NULL
== (buf
= malloc(p
->rmargin
)))
417 if (NULL
== (title
= malloc(p
->rmargin
)))
420 if (NULL
== (pp
= mdoc_vol2a(meta
->vol
)))
421 switch (meta
->msec
) {
427 pp
= mdoc_vol2a(VOL_URM
);
430 pp
= mdoc_vol2a(VOL_SMM
);
439 pp
= mdoc_vol2a(VOL_PRM
);
442 pp
= mdoc_vol2a(VOL_KM
);
445 /* FIXME: capitalise. */
446 if (NULL
== (pp
= mdoc_msec2a(meta
->msec
)))
447 pp
= mdoc_msec2a(MSEC_local
);
452 tsz
= strlcpy(buf
, pp
, p
->rmargin
);
453 assert(tsz
< p
->rmargin
);
455 if ((pp
= mdoc_arch2a(meta
->arch
))) {
456 tsz
= strlcat(buf
, " (", p
->rmargin
);
457 assert(tsz
< p
->rmargin
);
458 tsz
= strlcat(buf
, pp
, p
->rmargin
);
459 assert(tsz
< p
->rmargin
);
460 tsz
= strlcat(buf
, ")", p
->rmargin
);
461 assert(tsz
< p
->rmargin
);
464 ttsz
= strlcpy(title
, meta
->title
, p
->rmargin
);
466 if (NULL
== (msec
= mdoc_msec2a(meta
->msec
)))
469 ssz
= (2 * (ttsz
+ 2 + strlen(msec
))) + tsz
+ 2;
471 if (ssz
> p
->rmargin
) {
472 if ((ssz
-= p
->rmargin
) % 2)
477 title
[ttsz
- ssz
] = 0;
480 ssz
= ((p
->rmargin
- ssz
) / 2) + 1;
482 printf("%s(%s)", title
, msec
);
484 for (i
= 0; i
< ssz
; i
++)
489 for (i
= 0; i
< ssz
; i
++)
492 printf("%s(%s)\n", title
, msec
);
501 termprint(const struct mdoc_node
*node
,
502 const struct mdoc_meta
*meta
)
506 p
.maxrmargin
= 80; /* XXX */
507 p
.rmargin
= p
.maxrmargin
;
509 p
.offset
= p
.col
= 0;
510 p
.flags
= TERMP_NOSPACE
;
512 if (NULL
== (p
.buf
= malloc(p
.maxcols
)))
515 termprint_header(&p
, meta
);
516 termprint_r(&p
, meta
, node
);
517 termprint_footer(&p
, meta
);