]>
git.cameronkatri.com Git - mandoc.git/blob - mdocterm.c
1 /* $Id: mdocterm.c,v 1.4 2009/02/23 09:33:34 kristaps Exp $ */
3 * Copyright (c) 2008 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.
36 static void body(struct termp
*,
37 const struct mdoc_meta
*,
38 const struct mdoc_node
*);
39 static void header(struct termp
*,
40 const struct mdoc_meta
*);
41 static void footer(struct termp
*,
42 const struct mdoc_meta
*);
44 static void pword(struct termp
*, const char *, size_t);
45 static void pescape(struct termp
*,
46 const char *, size_t *, size_t);
47 static void chara(struct termp
*, char);
48 static void stringa(struct termp
*, const char *);
49 static void style(struct termp
*, enum termstyle
);
52 extern size_t strlcat(char *, const char *, size_t);
53 extern size_t strlcpy(char *, const char *, size_t);
58 main(int argc
, char *argv
[])
61 const struct mdoc
*mdoc
;
69 if ( ! mmain_getopt(p
, argc
, argv
, NULL
, NULL
, NULL
, NULL
))
72 if (NULL
== (mdoc
= mmain_mdoc(p
)))
75 termp
.maxrmargin
= 80; /* XXX */
76 termp
.rmargin
= termp
.maxrmargin
;
78 termp
.offset
= termp
.col
= 0;
79 termp
.flags
= TERMP_NOSPACE
;
81 if (NULL
== (termp
.buf
= malloc(termp
.maxcols
)))
84 header(&termp
, mdoc_meta(mdoc
));
85 body(&termp
, mdoc_meta(mdoc
), mdoc_node(mdoc
));
86 footer(&termp
, mdoc_meta(mdoc
));
96 flushln(struct termp
*p
)
98 size_t i
, j
, vsz
, vis
, maxvis
;
101 * First, establish the maximum columns of "visible" content.
102 * This is usually the difference between the right-margin and
103 * an indentation, but can be, for tagged lists or columns, a
104 * small set of values.
107 assert(p
->offset
< p
->rmargin
);
108 maxvis
= p
->rmargin
- p
->offset
;
112 * If in the standard case (left-justified), then begin with our
113 * indentation, otherwise (columns, etc.) just start spitting
117 if ( ! (p
->flags
& TERMP_NOLPAD
))
119 for (j
= 0; j
< p
->offset
; j
++)
123 * If we're literal, print out verbatim.
125 if (p
->flags
& TERMP_LITERAL
) {
126 /* FIXME: count non-printing chars. */
127 for (i
= 0; i
< p
->col
; i
++)
134 for (i
= 0; i
< p
->col
; i
++) {
136 * Count up visible word characters. Control sequences
137 * (starting with the CSI) aren't counted.
139 assert( ! isspace(p
->buf
[i
]));
142 for (j
= i
, vsz
= 0; j
< p
->col
; j
++) {
143 if (isspace(p
->buf
[j
]))
145 else if (27 == p
->buf
[j
]) {
146 assert(j
+ 4 <= p
->col
);
154 * If a word is too long and we're within a line, put it
155 * on the next line. Puke if we're being asked to write
156 * something that will exceed the right margin (i.e.,
157 * from a fresh line or when we're not allowed to break
158 * the line with TERMP_NOBREAK).
161 if (vis
&& vis
+ vsz
>= maxvis
) {
163 if (p
->flags
& TERMP_NOBREAK
)
164 errx(1, "word breaks right margin");
166 for (j
= 0; j
< p
->offset
; j
++)
169 } else if (vis
+ vsz
>= maxvis
) {
171 errx(1, "word breaks right margin");
175 * Write out the word and a trailing space. Omit the
176 * space if we're the last word in the line.
179 for ( ; i
< p
->col
; i
++) {
180 if (isspace(p
->buf
[i
]))
192 * If we're not to right-marginalise it (newline), then instead
193 * pad to the right margin and stay off.
196 if (p
->flags
& TERMP_NOBREAK
) {
197 for ( ; vis
<= maxvis
; vis
++)
207 newln(struct termp
*p
)
211 * A newline only breaks an existing line; it won't assert
214 p
->flags
|= TERMP_NOSPACE
;
222 vspace(struct termp
*p
)
226 * Asserts a vertical space (a full, empty line-break between
235 stringa(struct termp
*p
, const char *s
)
238 /* XXX - speed up if not passing to chara. */
245 chara(struct termp
*p
, char c
)
248 /* TODO: dynamically expand the buffer. */
249 if (p
->col
+ 1 >= p
->maxcols
)
250 errx(1, "line overrun");
251 p
->buf
[(p
->col
)++] = c
;
256 style(struct termp
*p
, enum termstyle esc
)
259 if (p
->col
+ 4 >= p
->maxcols
)
260 errx(1, "line overrun");
262 p
->buf
[(p
->col
)++] = 27;
263 p
->buf
[(p
->col
)++] = '[';
266 p
->buf
[(p
->col
)++] = '0';
269 p
->buf
[(p
->col
)++] = '1';
271 case (STYLE_UNDERLINE
):
272 p
->buf
[(p
->col
)++] = '4';
278 p
->buf
[(p
->col
)++] = 'm';
283 pescape(struct termp
*p
, const char *word
, size_t *i
, size_t len
)
289 if ('(' == word
[*i
]) {
290 /* Two-character escapes. */
292 assert(*i
+ 1 < len
);
294 if ('r' == word
[*i
] && 'B' == word
[*i
+ 1])
296 else if ('l' == word
[*i
] && 'B' == word
[*i
+ 1])
298 else if ('<' == word
[*i
] && '-' == word
[*i
+ 1])
300 else if ('-' == word
[*i
] && '>' == word
[*i
+ 1])
306 } else if ('[' != word
[*i
]) {
307 /* One-character escapes. */
324 /* n-character escapes. */
329 pword(struct termp
*p
, const char *word
, size_t len
)
333 /*assert(len > 0);*/ /* Can be, if literal. */
335 if ( ! (p
->flags
& TERMP_NOSPACE
) &&
336 ! (p
->flags
& TERMP_LITERAL
))
339 p
->flags
&= ~TERMP_NOSPACE
;
341 if (p
->flags
& TERMP_BOLD
)
342 style(p
, STYLE_BOLD
);
343 if (p
->flags
& TERMP_UNDERLINE
)
344 style(p
, STYLE_UNDERLINE
);
346 for (i
= 0; i
< len
; i
++) {
347 if ('\\' == word
[i
]) {
348 pescape(p
, word
, &i
, len
);
354 if (p
->flags
& TERMP_BOLD
||
355 p
->flags
& TERMP_UNDERLINE
)
356 style(p
, STYLE_CLEAR
);
361 word(struct termp
*p
, const char *word
)
365 if (p
->flags
& TERMP_LITERAL
) {
366 pword(p
, word
, strlen(word
));
373 if (mdoc_isdelim(word
)) {
374 if ( ! (p
->flags
& TERMP_IGNDELIM
))
375 p
->flags
|= TERMP_NOSPACE
;
376 p
->flags
&= ~TERMP_IGNDELIM
;
380 for (j
= i
= 0; i
< len
; i
++) {
381 if ( ! isspace(word
[i
])) {
388 pword(p
, &word
[i
- j
], j
);
393 pword(p
, &word
[i
- j
], j
);
399 body(struct termp
*p
, const struct mdoc_meta
*meta
,
400 const struct mdoc_node
*node
)
404 /* Pre-processing. */
408 if (MDOC_TEXT
!= node
->type
) {
409 if (termacts
[node
->tok
].pre
)
410 if ( ! (*termacts
[node
->tok
].pre
)(p
, meta
, node
))
412 } else /* MDOC_TEXT == node->type */
413 word(p
, node
->data
.text
.string
);
417 if (dochild
&& node
->child
)
418 body(p
, meta
, node
->child
);
420 /* Post-processing. */
422 if (MDOC_TEXT
!= node
->type
)
423 if (termacts
[node
->tok
].post
)
424 (*termacts
[node
->tok
].post
)(p
, meta
, node
);
429 body(p
, meta
, node
->next
);
434 footer(struct termp
*p
, const struct mdoc_meta
*meta
)
438 size_t sz
, osz
, ssz
, i
;
440 if (NULL
== (buf
= malloc(p
->rmargin
)))
442 if (NULL
== (os
= malloc(p
->rmargin
)))
445 tm
= localtime(&meta
->date
);
448 if (0 == strftime(buf
, p
->rmargin
, "%B %d, %Y", tm
))
450 if (NULL
== strftime(buf
, p
->rmargin
, "%B %d, %Y", tm
))
454 osz
= strlcpy(os
, meta
->os
, p
->rmargin
);
459 if (ssz
> p
->rmargin
) {
465 ssz
= p
->rmargin
- ssz
+ 1;
469 for (i
= 0; i
< ssz
; i
++)
481 header(struct termp
*p
, const struct mdoc_meta
*meta
)
484 const char *pp
, *msec
;
485 size_t ssz
, tsz
, ttsz
, i
;;
487 if (NULL
== (buf
= malloc(p
->rmargin
)))
489 if (NULL
== (title
= malloc(p
->rmargin
)))
492 if (NULL
== (pp
= mdoc_vol2a(meta
->vol
)))
493 switch (meta
->msec
) {
499 pp
= mdoc_vol2a(VOL_URM
);
502 pp
= mdoc_vol2a(VOL_SMM
);
511 pp
= mdoc_vol2a(VOL_PRM
);
514 pp
= mdoc_vol2a(VOL_KM
);
517 /* FIXME: capitalise. */
518 if (NULL
== (pp
= mdoc_msec2a(meta
->msec
)))
519 pp
= mdoc_msec2a(MSEC_local
);
524 tsz
= strlcpy(buf
, pp
, p
->rmargin
);
525 assert(tsz
< p
->rmargin
);
527 if ((pp
= mdoc_arch2a(meta
->arch
))) {
528 tsz
= strlcat(buf
, " (", p
->rmargin
);
529 assert(tsz
< p
->rmargin
);
530 tsz
= strlcat(buf
, pp
, p
->rmargin
);
531 assert(tsz
< p
->rmargin
);
532 tsz
= strlcat(buf
, ")", p
->rmargin
);
533 assert(tsz
< p
->rmargin
);
536 ttsz
= strlcpy(title
, meta
->title
, p
->rmargin
);
538 if (NULL
== (msec
= mdoc_msec2a(meta
->msec
)))
541 ssz
= (2 * (ttsz
+ 2 + strlen(msec
))) + tsz
+ 2;
543 if (ssz
> p
->rmargin
) {
544 if ((ssz
-= p
->rmargin
) % 2)
549 title
[ttsz
- ssz
] = 0;
552 ssz
= ((p
->rmargin
- ssz
) / 2) + 1;
554 printf("%s(%s)", title
, msec
);
556 for (i
= 0; i
< ssz
; i
++)
561 for (i
= 0; i
< ssz
; i
++)
564 printf("%s(%s)\n", title
, msec
);