]>
git.cameronkatri.com Git - mandoc.git/blob - mdocterm.c
1 /* $Id: mdocterm.c,v 1.12 2009/02/25 15:12:26 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.
35 #define xisspace(x) isspace((int)(x))
37 #define xisspace(x) isspace((x))
46 static void body(struct termp
*,
48 const struct mdoc_meta
*,
49 const struct mdoc_node
*);
50 static void header(struct termp
*,
51 const struct mdoc_meta
*);
52 static void footer(struct termp
*,
53 const struct mdoc_meta
*);
55 static void pword(struct termp
*, const char *, size_t);
56 static void pescape(struct termp
*,
57 const char *, size_t *, size_t);
58 static void nescape(struct termp
*,
59 const char *, size_t);
60 static void chara(struct termp
*, char);
61 static void stringa(struct termp
*, const char *);
62 static void style(struct termp
*, enum termstyle
);
65 extern size_t strlcat(char *, const char *, size_t);
66 extern size_t strlcpy(char *, const char *, size_t);
71 main(int argc
, char *argv
[])
74 const struct mdoc
*mdoc
;
79 if ( ! mmain_getopt(p
, argc
, argv
, NULL
, NULL
, NULL
, NULL
))
82 if (NULL
== (mdoc
= mmain_mdoc(p
)))
85 termp
.maxrmargin
= 80; /* XXX */
86 termp
.rmargin
= termp
.maxrmargin
;
88 termp
.offset
= termp
.col
= 0;
89 termp
.flags
= TERMP_NOSPACE
;
91 if (NULL
== (termp
.buf
= malloc(termp
.maxcols
)))
94 header(&termp
, mdoc_meta(mdoc
));
95 body(&termp
, NULL
, mdoc_meta(mdoc
), mdoc_node(mdoc
));
96 footer(&termp
, mdoc_meta(mdoc
));
106 flushln(struct termp
*p
)
108 size_t i
, j
, vsz
, vis
, maxvis
;
111 * First, establish the maximum columns of "visible" content.
112 * This is usually the difference between the right-margin and
113 * an indentation, but can be, for tagged lists or columns, a
114 * small set of values.
117 assert(p
->offset
< p
->rmargin
);
118 maxvis
= p
->rmargin
- p
->offset
;
122 * If in the standard case (left-justified), then begin with our
123 * indentation, otherwise (columns, etc.) just start spitting
127 if ( ! (p
->flags
& TERMP_NOLPAD
))
129 for (j
= 0; j
< p
->offset
; j
++)
133 * If we're literal, print out verbatim.
135 if (p
->flags
& TERMP_LITERAL
) {
136 /* FIXME: count non-printing chars. */
137 for (i
= 0; i
< p
->col
; i
++)
144 for (i
= 0; i
< p
->col
; i
++) {
146 * Count up visible word characters. Control sequences
147 * (starting with the CSI) aren't counted.
149 assert( ! xisspace(p
->buf
[i
]));
152 for (j
= i
, vsz
= 0; j
< p
->col
; j
++) {
153 if (xisspace(p
->buf
[j
]))
155 else if (27 == p
->buf
[j
]) {
156 assert(j
+ 4 <= p
->col
);
164 * If a word is too long and we're within a line, put it
165 * on the next line. Puke if we're being asked to write
166 * something that will exceed the right margin (i.e.,
167 * from a fresh line or when we're not allowed to break
168 * the line with TERMP_NOBREAK).
171 if (vis
&& vis
+ vsz
>= maxvis
) {
173 if (p
->flags
& TERMP_NOBREAK
)
174 errx(1, "word breaks right margin");
176 for (j
= 0; j
< p
->offset
; j
++)
179 } else if (vis
+ vsz
>= maxvis
)
181 errx(1, "word breaks right margin");
184 * Write out the word and a trailing space. Omit the
185 * space if we're the last word in the line.
188 for ( ; i
< p
->col
; i
++) {
189 if (xisspace(p
->buf
[i
]))
201 * If we're not to right-marginalise it (newline), then instead
202 * pad to the right margin and stay off.
205 if (p
->flags
& TERMP_NOBREAK
) {
206 for ( ; vis
< maxvis
; vis
++)
216 newln(struct termp
*p
)
220 * A newline only breaks an existing line; it won't assert
223 p
->flags
|= TERMP_NOSPACE
;
225 p
->flags
&= ~TERMP_NOLPAD
;
229 p
->flags
&= ~TERMP_NOLPAD
;
234 vspace(struct termp
*p
)
238 * Asserts a vertical space (a full, empty line-break between
247 stringa(struct termp
*p
, const char *s
)
250 /* XXX - speed up if not passing to chara. */
257 chara(struct termp
*p
, char c
)
260 /* TODO: dynamically expand the buffer. */
261 if (p
->col
+ 1 >= p
->maxcols
)
262 errx(1, "line overrun");
263 p
->buf
[(p
->col
)++] = c
;
268 style(struct termp
*p
, enum termstyle esc
)
271 if (p
->col
+ 4 >= p
->maxcols
)
272 errx(1, "line overrun");
274 p
->buf
[(p
->col
)++] = 27;
275 p
->buf
[(p
->col
)++] = '[';
278 p
->buf
[(p
->col
)++] = '0';
281 p
->buf
[(p
->col
)++] = '1';
283 case (STYLE_UNDERLINE
):
284 p
->buf
[(p
->col
)++] = '4';
290 p
->buf
[(p
->col
)++] = 'm';
295 nescape(struct termp
*p
, const char *word
, size_t len
)
300 if ('r' == word
[0] && 'B' == word
[1])
302 else if ('l' == word
[0] && 'B' == word
[1])
304 else if ('<' == word
[0] && '-' == word
[1])
306 else if ('-' == word
[0] && '>' == word
[1])
308 else if ('l' == word
[0] && 'q' == word
[1])
310 else if ('r' == word
[0] && 'q' == word
[1])
312 else if ('b' == word
[0] && 'u' == word
[1])
322 pescape(struct termp
*p
, const char *word
, size_t *i
, size_t len
)
329 if ('(' == word
[*i
]) {
330 /* Two-character escapes. */
332 assert(*i
+ 1 < len
);
333 nescape(p
, &word
[*i
], 2);
337 } else if ('[' != word
[*i
]) {
338 /* One-character escapes. */
357 for (j
= 0; word
[*i
] && ']' != word
[*i
]; (*i
)++, j
++)
360 nescape(p
, &word
[*i
- j
], j
);
365 pword(struct termp
*p
, const char *word
, size_t len
)
369 /*assert(len > 0);*/ /* Can be, if literal. */
371 if ( ! (p
->flags
& TERMP_NOSPACE
) &&
372 ! (p
->flags
& TERMP_LITERAL
))
375 p
->flags
&= ~TERMP_NOSPACE
;
377 if (p
->flags
& TERMP_BOLD
)
378 style(p
, STYLE_BOLD
);
379 if (p
->flags
& TERMP_UNDERLINE
)
380 style(p
, STYLE_UNDERLINE
);
382 for (i
= 0; i
< len
; i
++) {
383 if ('\\' == word
[i
]) {
384 pescape(p
, word
, &i
, len
);
390 if (p
->flags
& TERMP_BOLD
||
391 p
->flags
& TERMP_UNDERLINE
)
392 style(p
, STYLE_CLEAR
);
397 word(struct termp
*p
, const char *word
)
401 if (p
->flags
& TERMP_LITERAL
) {
402 pword(p
, word
, strlen(word
));
409 if (mdoc_isdelim(word
)) {
410 if ( ! (p
->flags
& TERMP_IGNDELIM
))
411 p
->flags
|= TERMP_NOSPACE
;
412 p
->flags
&= ~TERMP_IGNDELIM
;
416 for (j
= i
= 0; i
< len
; i
++) {
417 if ( ! xisspace(word
[i
])) {
424 pword(p
, &word
[i
- j
], j
);
429 pword(p
, &word
[i
- j
], j
);
435 body(struct termp
*p
, struct termpair
*ppair
,
436 const struct mdoc_meta
*meta
,
437 const struct mdoc_node
*node
)
440 struct termpair pair
;
442 /* Pre-processing. */
447 pair
.offset
= pair
.rmargin
= 0;
451 if (MDOC_TEXT
!= node
->type
) {
452 if (termacts
[node
->tok
].pre
)
453 if ( ! (*termacts
[node
->tok
].pre
)(p
, &pair
, meta
, node
))
455 } else /* MDOC_TEXT == node->type */
456 word(p
, node
->data
.text
.string
);
460 if (TERMPAIR_FLAG
& pair
.type
)
461 p
->flags
|= pair
.flag
;
463 if (dochild
&& node
->child
)
464 body(p
, &pair
, meta
, node
->child
);
466 if (TERMPAIR_FLAG
& pair
.type
)
467 p
->flags
&= ~pair
.flag
;
469 /* Post-processing. */
471 if (MDOC_TEXT
!= node
->type
)
472 if (termacts
[node
->tok
].post
)
473 (*termacts
[node
->tok
].post
)(p
, &pair
, meta
, node
);
478 body(p
, ppair
, meta
, node
->next
);
483 footer(struct termp
*p
, const struct mdoc_meta
*meta
)
487 size_t sz
, osz
, ssz
, i
;
489 if (NULL
== (buf
= malloc(p
->rmargin
)))
491 if (NULL
== (os
= malloc(p
->rmargin
)))
494 tm
= localtime(&meta
->date
);
497 if (NULL
== strftime(buf
, p
->rmargin
, "%B %d, %Y", tm
))
499 if (0 == strftime(buf
, p
->rmargin
, "%B %d, %Y", tm
))
503 osz
= strlcpy(os
, meta
->os
, p
->rmargin
);
508 if (ssz
> p
->rmargin
) {
514 ssz
= p
->rmargin
- ssz
+ 1;
518 for (i
= 0; i
< ssz
; i
++)
530 header(struct termp
*p
, const struct mdoc_meta
*meta
)
533 const char *pp
, *msec
;
534 size_t ssz
, tsz
, ttsz
, i
;;
536 if (NULL
== (buf
= malloc(p
->rmargin
)))
538 if (NULL
== (title
= malloc(p
->rmargin
)))
541 if (NULL
== (pp
= mdoc_vol2a(meta
->vol
)))
542 switch (meta
->msec
) {
548 pp
= mdoc_vol2a(VOL_URM
);
551 pp
= mdoc_vol2a(VOL_SMM
);
560 pp
= mdoc_vol2a(VOL_PRM
);
563 pp
= mdoc_vol2a(VOL_KM
);
566 /* FIXME: capitalise. */
567 if (NULL
== (pp
= mdoc_msec2a(meta
->msec
)))
568 pp
= mdoc_msec2a(MSEC_local
);
573 tsz
= strlcpy(buf
, pp
, p
->rmargin
);
574 assert(tsz
< p
->rmargin
);
576 if ((pp
= mdoc_arch2a(meta
->arch
))) {
577 tsz
= strlcat(buf
, " (", p
->rmargin
);
578 assert(tsz
< p
->rmargin
);
579 tsz
= strlcat(buf
, pp
, p
->rmargin
);
580 assert(tsz
< p
->rmargin
);
581 tsz
= strlcat(buf
, ")", p
->rmargin
);
582 assert(tsz
< p
->rmargin
);
585 ttsz
= strlcpy(title
, meta
->title
, p
->rmargin
);
587 if (NULL
== (msec
= mdoc_msec2a(meta
->msec
)))
590 ssz
= (2 * (ttsz
+ 2 + strlen(msec
))) + tsz
+ 2;
592 if (ssz
> p
->rmargin
) {
593 if ((ssz
-= p
->rmargin
) % 2)
598 title
[ttsz
- ssz
] = 0;
601 ssz
= ((p
->rmargin
- ssz
) / 2) + 1;
603 printf("%s(%s)", title
, msec
);
605 for (i
= 0; i
< ssz
; i
++)
610 for (i
= 0; i
< ssz
; i
++)
613 printf("%s(%s)\n", title
, msec
);