]>
git.cameronkatri.com Git - mandoc.git/blob - term.c
1 /* $Id: term.c,v 1.5 2009/02/21 19:05:28 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.
28 #include "private.h" /* XXX */
36 static void termprint_r(struct termp
*,
37 const struct mdoc_meta
*,
38 const struct mdoc_node
*);
39 static void termprint_header(struct termp
*,
40 const struct mdoc_meta
*);
41 static void termprint_footer(struct termp
*,
42 const struct mdoc_meta
*);
44 static void pword(struct termp
*, const char *, size_t);
45 static void chara(struct termp
*, char);
46 static void escape(struct termp
*, enum termstyle
);
50 flushln(struct termp
*p
)
52 size_t i
, j
, vsz
, vis
, maxvis
;
55 * First, establish the maximum columns of "visible" content.
56 * This is usually the difference between the right-margin and
57 * an indentation, but can be, for tagged lists or columns, a
58 * small set of values.
61 assert(p
->offset
< p
->rmargin
);
62 maxvis
= p
->rmargin
- p
->offset
;
66 * If in the standard case (left-justified), then begin with our
67 * indentation, otherwise (columns, etc.) just start spitting
71 if ( ! (p
->flags
& TERMP_NOLPAD
))
73 for (j
= 0; j
< p
->offset
; j
++)
76 for (i
= 0; i
< p
->col
; i
++) {
78 * Count up visible word characters. Control sequences
79 * (starting with the CSI) aren't counted.
81 assert( ! isspace(p
->buf
[i
]));
84 for (j
= i
, vsz
= 0; j
< p
->col
; j
++) {
85 if (isspace(p
->buf
[j
]))
87 else if (27 == p
->buf
[j
]) {
88 assert(j
+ 4 <= p
->col
);
96 * If a word is too long and we're within a line, put it
97 * on the next line. Puke if we're being asked to write
98 * something that will exceed the right margin (i.e.,
99 * from a fresh line or when we're not allowed to break
100 * the line with TERMP_NOBREAK).
103 if (vis
&& vis
+ vsz
>= maxvis
) {
105 if (p
->flags
& TERMP_NOBREAK
)
106 errx(1, "word breaks right margin");
108 for (j
= 0; j
< p
->offset
; j
++)
111 } else if (vis
+ vsz
>= maxvis
) {
113 errx(1, "word breaks right margin");
117 * Write out the word and a trailing space. Omit the
118 * space if we're the last word in the line.
121 for ( ; i
< p
->col
; i
++) {
122 if (isspace(p
->buf
[i
]))
134 * If we're not to right-marginalise it (newline), then instead
135 * pad to the right margin and stay off.
138 if (p
->flags
& TERMP_NOBREAK
) {
139 for ( ; vis
<= maxvis
; vis
++)
149 newln(struct termp
*p
)
153 * A newline only breaks an existing line; it won't assert
156 p
->flags
|= TERMP_NOSPACE
;
164 vspace(struct termp
*p
)
168 * Asserts a vertical space (a full, empty line-break between
177 chara(struct termp
*p
, char c
)
180 /* TODO: dynamically expand the buffer. */
181 if (p
->col
+ 1 >= p
->maxcols
)
182 errx(1, "line overrun");
183 p
->buf
[(p
->col
)++] = c
;
188 escape(struct termp
*p
, enum termstyle esc
)
191 if (p
->col
+ 4 >= p
->maxcols
)
192 errx(1, "line overrun");
194 p
->buf
[(p
->col
)++] = 27;
195 p
->buf
[(p
->col
)++] = '[';
198 p
->buf
[(p
->col
)++] = '0';
201 p
->buf
[(p
->col
)++] = '1';
203 case (STYLE_UNDERLINE
):
204 p
->buf
[(p
->col
)++] = '4';
210 p
->buf
[(p
->col
)++] = 'm';
215 pword(struct termp
*p
, const char *word
, size_t len
)
221 if ( ! (p
->flags
& TERMP_NOSPACE
))
224 p
->flags
&= ~TERMP_NOSPACE
;
226 if (p
->flags
& TERMP_BOLD
)
227 escape(p
, STYLE_BOLD
);
228 if (p
->flags
& TERMP_UNDERLINE
)
229 escape(p
, STYLE_UNDERLINE
);
231 /* TODO: escape patterns. */
233 for (i
= 0; i
< len
; i
++)
236 if (p
->flags
& TERMP_BOLD
||
237 p
->flags
& TERMP_UNDERLINE
)
238 escape(p
, STYLE_CLEAR
);
243 word(struct termp
*p
, const char *word
)
247 if (mdoc_isdelim(word
))
248 p
->flags
|= TERMP_NOSPACE
;
254 for (j
= i
= 0; i
< len
; i
++) {
255 if ( ! isspace(word
[i
])) {
262 pword(p
, &word
[i
- j
], j
);
267 pword(p
, &word
[i
- j
], j
);
273 termprint_r(struct termp
*p
, const struct mdoc_meta
*meta
,
274 const struct mdoc_node
*node
)
278 /* Pre-processing. */
282 if (MDOC_TEXT
!= node
->type
) {
283 if (termacts
[node
->tok
].pre
)
284 if ( ! (*termacts
[node
->tok
].pre
)(p
, meta
, node
))
286 } else /* MDOC_TEXT == node->type */
287 word(p
, node
->data
.text
.string
);
291 if (dochild
&& node
->child
)
292 termprint_r(p
, meta
, node
->child
);
294 /* Post-processing. */
296 if (MDOC_TEXT
!= node
->type
) {
297 if (termacts
[node
->tok
].post
)
298 if ( ! (*termacts
[node
->tok
].post
)(p
, meta
, node
))
305 termprint_r(p
, meta
, node
->next
);
310 termprint_footer(struct termp
*p
, const struct mdoc_meta
*meta
)
314 size_t sz
, osz
, ssz
, i
;
316 if (NULL
== (buf
= malloc(p
->rmargin
)))
318 if (NULL
== (os
= malloc(p
->rmargin
)))
321 tm
= localtime(&meta
->date
);
322 if (NULL
== strftime(buf
, p
->rmargin
, "%B %d, %Y", tm
))
325 osz
= strlcpy(os
, meta
->os
, p
->rmargin
);
330 if (ssz
> p
->rmargin
) {
336 ssz
= p
->rmargin
- ssz
+ 1;
340 for (i
= 0; i
< ssz
; i
++)
352 termprint_header(struct termp
*p
, const struct mdoc_meta
*meta
)
354 char *msec
, *buf
, *title
, *pp
;
355 size_t ssz
, tsz
, ttsz
, i
;;
357 if (NULL
== (buf
= malloc(p
->rmargin
)))
359 if (NULL
== (title
= malloc(p
->rmargin
)))
362 if (NULL
== (pp
= mdoc_vol2a(meta
->vol
)))
363 switch (meta
->msec
) {
369 pp
= mdoc_vol2a(VOL_URM
);
372 pp
= mdoc_vol2a(VOL_SMM
);
381 pp
= mdoc_vol2a(VOL_PRM
);
384 pp
= mdoc_vol2a(VOL_KM
);
387 /* FIXME: capitalise. */
388 if (NULL
== (pp
= mdoc_msec2a(meta
->msec
)))
389 pp
= mdoc_msec2a(MSEC_local
);
394 tsz
= strlcpy(buf
, pp
, p
->rmargin
);
395 assert(tsz
< p
->rmargin
);
397 if ((pp
= mdoc_arch2a(meta
->arch
))) {
398 tsz
= strlcat(buf
, " (", p
->rmargin
);
399 assert(tsz
< p
->rmargin
);
400 tsz
= strlcat(buf
, pp
, p
->rmargin
);
401 assert(tsz
< p
->rmargin
);
402 tsz
= strlcat(buf
, ")", p
->rmargin
);
403 assert(tsz
< p
->rmargin
);
406 ttsz
= strlcpy(title
, meta
->title
, p
->rmargin
);
408 if (NULL
== (msec
= mdoc_msec2a(meta
->msec
)))
411 ssz
= (2 * (ttsz
+ 2 + strlen(msec
))) + tsz
+ 2;
413 if (ssz
> p
->rmargin
) {
414 if ((ssz
-= p
->rmargin
) % 2)
419 title
[ttsz
- ssz
] = 0;
422 ssz
= ((p
->rmargin
- ssz
) / 2) + 1;
424 printf("%s(%s)", title
, msec
);
426 for (i
= 0; i
< ssz
; i
++)
431 for (i
= 0; i
< ssz
; i
++)
434 printf("%s(%s)\n", title
, msec
);
443 termprint(const struct mdoc_node
*node
,
444 const struct mdoc_meta
*meta
)
448 p
.maxrmargin
= 80; /* XXX */
449 p
.rmargin
= p
.maxrmargin
;
451 p
.offset
= p
.col
= 0;
452 p
.flags
= TERMP_NOSPACE
;
454 if (NULL
== (p
.buf
= malloc(p
.maxcols
)))
457 termprint_header(&p
, meta
);
458 termprint_r(&p
, meta
, node
);
459 termprint_footer(&p
, meta
);