]>
git.cameronkatri.com Git - mandoc.git/blob - mdocterm.c
1 /* $Id: mdocterm.c,v 1.21 2009/02/28 21:31:13 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.
19 #include <sys/utsname.h>
36 #define TERMSYM_RBRACK "]"
37 #define TERMSYM_LBRACK "["
38 #define TERMSYM_LARROW "<-"
39 #define TERMSYM_RARROW "->"
40 #define TERMSYM_UARROW "^"
41 #define TERMSYM_DARROW "v"
42 #define TERMSYM_LSQUOTE "`"
43 #define TERMSYM_RSQUOTE "\'"
44 #define TERMSYM_SQUOTE "\'"
45 #define TERMSYM_LDQUOTE "``"
46 #define TERMSYM_RDQUOTE "\'\'"
47 #define TERMSYM_DQUOTE "\""
48 #define TERMSYM_LT "<"
49 #define TERMSYM_GT ">"
50 #define TERMSYM_LE "<="
51 #define TERMSYM_GE ">="
52 #define TERMSYM_EQ "=="
53 #define TERMSYM_NEQ "!="
54 #define TERMSYM_ACUTE "\'"
55 #define TERMSYM_GRAVE "`"
56 #define TERMSYM_PI "pi"
57 #define TERMSYM_PLUSMINUS "+="
58 #define TERMSYM_INF "oo"
59 #define TERMSYM_INF2 "infinity"
60 #define TERMSYM_NAN "NaN"
61 #define TERMSYM_BAR "|"
62 #define TERMSYM_BULLET "o"
65 #define xisspace(x) isspace((int)(x))
67 #define xisspace(x) isspace((x))
76 static void body(struct termp
*,
78 const struct mdoc_meta
*,
79 const struct mdoc_node
*);
80 static void header(struct termp
*,
81 const struct mdoc_meta
*);
82 static void footer(struct termp
*,
83 const struct mdoc_meta
*);
85 static void pword(struct termp
*, const char *, size_t);
86 static void pescape(struct termp
*,
87 const char *, size_t *, size_t);
88 static void nescape(struct termp
*,
89 const char *, size_t);
90 static void chara(struct termp
*, char);
91 static void stringa(struct termp
*, const char *);
92 static void style(struct termp
*, enum termstyle
);
95 extern size_t strlcat(char *, const char *, size_t);
96 extern size_t strlcpy(char *, const char *, size_t);
101 main(int argc
, char *argv
[])
104 const struct mdoc
*mdoc
;
109 if ( ! mmain_getopt(p
, argc
, argv
, NULL
, NULL
, NULL
, NULL
))
112 if (NULL
== (mdoc
= mmain_mdoc(p
)))
115 termp
.maxrmargin
= 78; /* XXX */
116 termp
.rmargin
= termp
.maxrmargin
;
117 termp
.maxcols
= 1024;
118 termp
.offset
= termp
.col
= 0;
119 termp
.flags
= TERMP_NOSPACE
;
121 if (NULL
== (termp
.buf
= malloc(termp
.maxcols
)))
124 header(&termp
, mdoc_meta(mdoc
));
125 body(&termp
, NULL
, mdoc_meta(mdoc
), mdoc_node(mdoc
));
126 footer(&termp
, mdoc_meta(mdoc
));
136 flushln(struct termp
*p
)
138 size_t i
, j
, vsz
, vis
, maxvis
;
141 * First, establish the maximum columns of "visible" content.
142 * This is usually the difference between the right-margin and
143 * an indentation, but can be, for tagged lists or columns, a
144 * small set of values.
147 assert(p
->offset
< p
->rmargin
);
148 maxvis
= p
->rmargin
- p
->offset
;
152 * If in the standard case (left-justified), then begin with our
153 * indentation, otherwise (columns, etc.) just start spitting
157 if ( ! (p
->flags
& TERMP_NOLPAD
))
159 for (j
= 0; j
< p
->offset
; j
++)
163 * If we're literal, print out verbatim.
165 if (p
->flags
& TERMP_LITERAL
) {
166 for (i
= 0; i
< p
->col
; i
++)
173 for (i
= 0; i
< p
->col
; i
++) {
175 * Count up visible word characters. Control sequences
176 * (starting with the CSI) aren't counted.
178 assert( ! xisspace(p
->buf
[i
]));
181 for (j
= i
, vsz
= 0; j
< p
->col
; j
++) {
182 if (xisspace(p
->buf
[j
]))
184 else if (27 == p
->buf
[j
]) {
185 assert(j
+ 4 <= p
->col
);
193 * If a word is too long and we're within a line, put it
194 * on the next line. Puke if we're being asked to write
195 * something that will exceed the right margin (i.e.,
196 * from a fresh line or when we're not allowed to break
197 * the line with TERMP_NOBREAK).
200 /* FIXME: allow selective right-margin breaking. */
202 if (vis
&& vis
+ vsz
> maxvis
) {
203 if (p
->flags
& TERMP_NOBREAK
)
204 errx(1, "word breaks right margin");
206 for (j
= 0; j
< p
->offset
; j
++)
209 } else if (vis
+ vsz
> maxvis
)
210 errx(1, "word breaks right margin");
213 * Write out the word and a trailing space. Omit the
214 * space if we're the last word in the line.
217 for ( ; i
< p
->col
; i
++) {
218 if (xisspace(p
->buf
[i
]))
230 * If we're not to right-marginalise it (newline), then instead
231 * pad to the right margin and stay off.
234 if (p
->flags
& TERMP_NOBREAK
) {
235 if ( ! (p
->flags
& TERMP_NORPAD
))
236 for ( ; vis
< maxvis
; vis
++)
246 newln(struct termp
*p
)
250 * A newline only breaks an existing line; it won't assert
253 p
->flags
|= TERMP_NOSPACE
;
255 p
->flags
&= ~TERMP_NOLPAD
;
259 p
->flags
&= ~TERMP_NOLPAD
;
264 vspace(struct termp
*p
)
268 * Asserts a vertical space (a full, empty line-break between
277 stringa(struct termp
*p
, const char *s
)
280 /* XXX - speed up if not passing to chara. */
287 chara(struct termp
*p
, char c
)
291 * Insert a single character into the line-buffer. If the
292 * buffer's space is exceeded, then allocate more space.
294 if (p
->col
+ 1 >= p
->maxcols
) {
295 p
->buf
= realloc(p
->buf
, p
->maxcols
* 2);
300 p
->buf
[(p
->col
)++] = c
;
305 style(struct termp
*p
, enum termstyle esc
)
308 if (p
->col
+ 4 >= p
->maxcols
)
309 errx(1, "line overrun");
311 p
->buf
[(p
->col
)++] = 27;
312 p
->buf
[(p
->col
)++] = '[';
315 p
->buf
[(p
->col
)++] = '0';
318 p
->buf
[(p
->col
)++] = '1';
320 case (STYLE_UNDERLINE
):
321 p
->buf
[(p
->col
)++] = '4';
327 p
->buf
[(p
->col
)++] = 'm';
332 nescape(struct termp
*p
, const char *word
, size_t len
)
338 stringa(p
, TERMSYM_DQUOTE
);
341 if ('r' == word
[0] && 'B' == word
[1])
342 stringa(p
, TERMSYM_RBRACK
);
343 else if ('l' == word
[0] && 'B' == word
[1])
344 stringa(p
, TERMSYM_LBRACK
);
345 else if ('l' == word
[0] && 'q' == word
[1])
346 stringa(p
, TERMSYM_LDQUOTE
);
347 else if ('r' == word
[0] && 'q' == word
[1])
348 stringa(p
, TERMSYM_RDQUOTE
);
349 else if ('o' == word
[0] && 'q' == word
[1])
350 stringa(p
, TERMSYM_LSQUOTE
);
351 else if ('a' == word
[0] && 'q' == word
[1])
352 stringa(p
, TERMSYM_RSQUOTE
);
353 else if ('<' == word
[0] && '-' == word
[1])
354 stringa(p
, TERMSYM_LARROW
);
355 else if ('-' == word
[0] && '>' == word
[1])
356 stringa(p
, TERMSYM_RARROW
);
357 else if ('b' == word
[0] && 'u' == word
[1])
358 stringa(p
, TERMSYM_BULLET
);
359 else if ('<' == word
[0] && '=' == word
[1])
360 stringa(p
, TERMSYM_LE
);
361 else if ('>' == word
[0] && '=' == word
[1])
362 stringa(p
, TERMSYM_GE
);
363 else if ('=' == word
[0] && '=' == word
[1])
364 stringa(p
, TERMSYM_EQ
);
365 else if ('+' == word
[0] && '-' == word
[1])
366 stringa(p
, TERMSYM_PLUSMINUS
);
367 else if ('u' == word
[0] && 'a' == word
[1])
368 stringa(p
, TERMSYM_UARROW
);
369 else if ('d' == word
[0] && 'a' == word
[1])
370 stringa(p
, TERMSYM_DARROW
);
371 else if ('a' == word
[0] && 'a' == word
[1])
372 stringa(p
, TERMSYM_ACUTE
);
373 else if ('g' == word
[0] && 'a' == word
[1])
374 stringa(p
, TERMSYM_GRAVE
);
375 else if ('!' == word
[0] && '=' == word
[1])
376 stringa(p
, TERMSYM_NEQ
);
377 else if ('i' == word
[0] && 'f' == word
[1])
378 stringa(p
, TERMSYM_INF
);
379 else if ('n' == word
[0] && 'a' == word
[1])
380 stringa(p
, TERMSYM_NAN
);
381 else if ('b' == word
[0] && 'a' == word
[1])
382 stringa(p
, TERMSYM_BAR
);
384 /* Deprecated forms. */
385 else if ('B' == word
[0] && 'a' == word
[1])
386 stringa(p
, TERMSYM_BAR
);
387 else if ('I' == word
[0] && 'f' == word
[1])
388 stringa(p
, TERMSYM_INF2
);
389 else if ('G' == word
[0] && 'e' == word
[1])
390 stringa(p
, TERMSYM_GE
);
391 else if ('G' == word
[0] && 't' == word
[1])
392 stringa(p
, TERMSYM_GT
);
393 else if ('L' == word
[0] && 'e' == word
[1])
394 stringa(p
, TERMSYM_LE
);
395 else if ('L' == word
[0] && 'q' == word
[1])
396 stringa(p
, TERMSYM_LDQUOTE
);
397 else if ('L' == word
[0] && 't' == word
[1])
398 stringa(p
, TERMSYM_LT
);
399 else if ('N' == word
[0] && 'a' == word
[1])
400 stringa(p
, TERMSYM_NAN
);
401 else if ('N' == word
[0] && 'e' == word
[1])
402 stringa(p
, TERMSYM_NEQ
);
403 else if ('P' == word
[0] && 'i' == word
[1])
404 stringa(p
, TERMSYM_PI
);
405 else if ('P' == word
[0] && 'm' == word
[1])
406 stringa(p
, TERMSYM_PLUSMINUS
);
407 else if ('R' == word
[0] && 'q' == word
[1])
408 stringa(p
, TERMSYM_RDQUOTE
);
417 pescape(struct termp
*p
, const char *word
, size_t *i
, size_t len
)
425 * Handle an escape sequence. This must manage both groff-style
426 * escapes and mdoc-style escapes.
429 if ('(' == word
[*i
]) {
430 /* Two-character escapes. */
432 assert(*i
+ 1 < len
);
433 nescape(p
, &word
[*i
], 2);
437 } else if ('*' == word
[*i
]) {
443 assert(*i
+ 1 < len
);
444 nescape(p
, &word
[*i
], 2);
450 nescape(p
, &word
[*i
], 1);
453 } else if ('[' != word
[*i
]) {
454 /* One-character escapes. */
479 for (j
= 0; word
[*i
] && ']' != word
[*i
]; (*i
)++, j
++)
482 nescape(p
, &word
[*i
- j
], j
);
487 pword(struct termp
*p
, const char *word
, size_t len
)
491 /*assert(len > 0);*/ /* Can be, if literal. */
494 * Handle pwords, partial words, which may be either a single
495 * word or a phrase that cannot be broken down (such as a
496 * literal string). This handles word styling.
499 if ( ! (p
->flags
& TERMP_NOSPACE
) &&
500 ! (p
->flags
& TERMP_LITERAL
))
503 if ( ! (p
->flags
& TERMP_NONOSPACE
))
504 p
->flags
&= ~TERMP_NOSPACE
;
507 * XXX - if literal and underlining, this will underline the
508 * spaces between literal words.
511 if (p
->flags
& TERMP_BOLD
)
512 style(p
, STYLE_BOLD
);
513 if (p
->flags
& TERMP_UNDERLINE
)
514 style(p
, STYLE_UNDERLINE
);
516 for (i
= 0; i
< len
; i
++) {
517 if ('\\' == word
[i
]) {
518 pescape(p
, word
, &i
, len
);
524 if (p
->flags
& TERMP_BOLD
||
525 p
->flags
& TERMP_UNDERLINE
)
526 style(p
, STYLE_CLEAR
);
531 word(struct termp
*p
, const char *word
)
536 * Break apart a word into tokens. If we're a literal word,
537 * then don't. This doesn't handle zero-length words (there
538 * should be none) and makes sure that pword doesn't get spaces
539 * or nil words unless literal.
542 if (p
->flags
& TERMP_LITERAL
) {
543 pword(p
, word
, strlen(word
));
550 if (mdoc_isdelim(word
)) {
551 if ( ! (p
->flags
& TERMP_IGNDELIM
))
552 p
->flags
|= TERMP_NOSPACE
;
553 p
->flags
&= ~TERMP_IGNDELIM
;
557 for (j
= i
= 0; i
< len
; i
++) {
558 if ( ! xisspace(word
[i
])) {
563 /* Escaped spaces don't delimit... */
564 if (i
> 0 && xisspace(word
[i
]) && '\\' == word
[i
- 1]) {
572 pword(p
, &word
[i
- j
], j
);
577 pword(p
, &word
[i
- j
], j
);
583 body(struct termp
*p
, struct termpair
*ppair
,
584 const struct mdoc_meta
*meta
,
585 const struct mdoc_node
*node
)
588 struct termpair pair
;
591 * This is the main function for printing out nodes. It's
592 * constituted of PRE and POST functions, which correspond to
593 * prefix and infix processing.
596 /* Pre-processing. */
601 pair
.offset
= pair
.rmargin
= 0;
605 if (MDOC_TEXT
!= node
->type
) {
606 if (termacts
[node
->tok
].pre
)
607 if ( ! (*termacts
[node
->tok
].pre
)(p
, &pair
, meta
, node
))
609 } else /* MDOC_TEXT == node->type */
610 word(p
, node
->data
.text
.string
);
614 if (TERMPAIR_FLAG
& pair
.type
)
615 p
->flags
|= pair
.flag
;
617 if (dochild
&& node
->child
)
618 body(p
, &pair
, meta
, node
->child
);
620 if (TERMPAIR_FLAG
& pair
.type
)
621 p
->flags
&= ~pair
.flag
;
623 /* Post-processing. */
625 if (MDOC_TEXT
!= node
->type
)
626 if (termacts
[node
->tok
].post
)
627 (*termacts
[node
->tok
].post
)(p
, &pair
, meta
, node
);
632 body(p
, ppair
, meta
, node
->next
);
637 footer(struct termp
*p
, const struct mdoc_meta
*meta
)
642 if (NULL
== (buf
= malloc(p
->rmargin
)))
644 if (NULL
== (os
= malloc(p
->rmargin
)))
647 tm
= localtime(&meta
->date
);
650 if (NULL
== strftime(buf
, p
->rmargin
, "%B %d, %Y", tm
))
652 if (0 == strftime(buf
, p
->rmargin
, "%B %d, %Y", tm
))
656 (void)strlcpy(os
, meta
->os
, p
->rmargin
);
659 * This is /slightly/ different from regular groff output
660 * because we don't have page numbers. Print the following:
667 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
668 p
->rmargin
= p
->maxrmargin
- strlen(buf
);
674 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
675 p
->offset
= p
->rmargin
;
676 p
->rmargin
= p
->maxrmargin
;
677 p
->flags
&= ~TERMP_NOBREAK
;
688 header(struct termp
*p
, const struct mdoc_meta
*meta
)
690 char *buf
, *title
, *bufp
, *vbuf
;
694 p
->rmargin
= p
->maxrmargin
;
697 if (NULL
== (buf
= malloc(p
->rmargin
)))
699 if (NULL
== (title
= malloc(p
->rmargin
)))
701 if (NULL
== (vbuf
= malloc(p
->rmargin
)))
704 if (NULL
== (pp
= mdoc_vol2a(meta
->vol
))) {
705 switch (meta
->msec
) {
711 pp
= mdoc_vol2a(VOL_URM
);
714 pp
= mdoc_vol2a(VOL_SMM
);
723 pp
= mdoc_vol2a(VOL_PRM
);
726 pp
= mdoc_vol2a(VOL_KM
);
735 if (-1 == uname(&uts
))
737 (void)strlcat(vbuf
, uts
.sysname
, p
->rmargin
);
738 (void)strlcat(vbuf
, " ", p
->rmargin
);
739 } else if (NULL
== (pp
= mdoc_msec2a(meta
->msec
)))
740 pp
= mdoc_msec2a(MSEC_local
);
742 (void)strlcat(vbuf
, pp
, p
->rmargin
);
745 * The header is strange. It has three components, which are
746 * really two with the first duplicated. It goes like this:
748 * IDENTIFIER TITLE IDENTIFIER
750 * The IDENTIFIER is NAME(SECTION), which is the command-name
751 * (if given, or "unknown" if not) followed by the manual page
752 * section. These are given in `Dt'. The TITLE is a free-form
753 * string depending on the manual volume. If not specified, it
754 * switches on the manual section.
757 if (mdoc_arch2a(meta
->arch
))
758 (void)snprintf(buf
, p
->rmargin
, "%s (%s)",
759 vbuf
, mdoc_arch2a(meta
->arch
));
761 (void)strlcpy(buf
, vbuf
, p
->rmargin
);
763 pp
= mdoc_msec2a(meta
->msec
);
765 (void)snprintf(title
, p
->rmargin
, "%s(%s)",
766 meta
->title
, pp
? pp
: "");
768 for (bufp
= title
; *bufp
; bufp
++)
769 *bufp
= toupper(*bufp
);
772 p
->rmargin
= (p
->maxrmargin
- strlen(buf
)) / 2;
773 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
778 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
779 p
->offset
= p
->rmargin
;
780 p
->rmargin
= p
->maxrmargin
- strlen(title
);
785 p
->offset
= p
->rmargin
;
786 p
->rmargin
= p
->maxrmargin
;
787 p
->flags
&= ~TERMP_NOBREAK
;
788 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
793 p
->rmargin
= p
->maxrmargin
;
795 p
->flags
&= ~TERMP_NOSPACE
;