]>
git.cameronkatri.com Git - mandoc.git/blob - mdocterm.c
1 /* $Id: mdocterm.c,v 1.34 2009/03/06 14:13:47 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.
38 static void body(struct termp
*,
40 const struct mdoc_meta
*,
41 const struct mdoc_node
*);
42 static void header(struct termp
*,
43 const struct mdoc_meta
*);
44 static void footer(struct termp
*,
45 const struct mdoc_meta
*);
47 static void pword(struct termp
*, const char *, size_t);
48 static void pescape(struct termp
*, const char *,
50 static void nescape(struct termp
*,
51 const char *, size_t);
52 static void chara(struct termp
*, char);
53 static void stringa(struct termp
*,
54 const char *, size_t);
55 static void symbola(struct termp
*, enum tsym
);
56 static void stylea(struct termp
*, enum tstyle
);
59 extern size_t strlcat(char *, const char *, size_t);
60 extern size_t strlcpy(char *, const char *, size_t);
63 static struct termenc termenc1
[] = {
64 { "\\", TERMSYM_SLASH
},
65 { "\'", TERMSYM_RSQUOTE
},
66 { "`", TERMSYM_LSQUOTE
},
67 { "-", TERMSYM_HYPHEN
},
68 { " ", TERMSYM_SPACE
},
69 { ".", TERMSYM_PERIOD
},
70 { "&", TERMSYM_BREAK
},
71 { "e", TERMSYM_SLASH
},
72 { "q", TERMSYM_DQUOTE
},
76 static struct termenc termenc2
[] = {
77 { "rB", TERMSYM_RBRACK
},
78 { "lB", TERMSYM_LBRACK
},
79 { "ra", TERMSYM_RANGLE
},
80 { "la", TERMSYM_LANGLE
},
81 { "Lq", TERMSYM_LDQUOTE
},
82 { "lq", TERMSYM_LDQUOTE
},
83 { "Rq", TERMSYM_RDQUOTE
},
84 { "rq", TERMSYM_RDQUOTE
},
85 { "oq", TERMSYM_LSQUOTE
},
86 { "aq", TERMSYM_RSQUOTE
},
88 { "<-", TERMSYM_LARROW
},
89 { "->", TERMSYM_RARROW
},
90 { "ua", TERMSYM_UARROW
},
91 { "da", TERMSYM_DARROW
},
93 { "bu", TERMSYM_BULLET
},
94 { "Ba", TERMSYM_BAR
},
95 { "ba", TERMSYM_BAR
},
96 { "co", TERMSYM_COPY
},
97 { "Am", TERMSYM_AMP
},
100 { "<=", TERMSYM_LE
},
101 { "Ge", TERMSYM_GE
},
102 { ">=", TERMSYM_GE
},
103 { "==", TERMSYM_EQ
},
104 { "Ne", TERMSYM_NEQ
},
105 { "!=", TERMSYM_NEQ
},
106 { "Pm", TERMSYM_PLUSMINUS
},
107 { "+-", TERMSYM_PLUSMINUS
},
108 { "If", TERMSYM_INF2
},
109 { "if", TERMSYM_INF
},
110 { "Na", TERMSYM_NAN
},
111 { "na", TERMSYM_NAN
},
112 { "**", TERMSYM_ASTERISK
},
113 { "Gt", TERMSYM_GT
},
114 { "Lt", TERMSYM_LT
},
116 { "aa", TERMSYM_ACUTE
},
117 { "ga", TERMSYM_GRAVE
},
119 { "en", TERMSYM_EN
},
120 { "em", TERMSYM_EM
},
122 { "Pi", TERMSYM_PI
},
126 static struct termsym termsym_ansi
[] = {
127 { "]", 1 }, /* TERMSYM_RBRACK */
128 { "[", 1 }, /* TERMSYM_LBRACK */
129 { "<-", 2 }, /* TERMSYM_LARROW */
130 { "->", 2 }, /* TERMSYM_RARROW */
131 { "^", 1 }, /* TERMSYM_UARROW */
132 { "v", 1 }, /* TERMSYM_DARROW */
133 { "`", 1 }, /* TERMSYM_LSQUOTE */
134 { "\'", 1 }, /* TERMSYM_RSQUOTE */
135 { "\'", 1 }, /* TERMSYM_SQUOTE */
136 { "``", 2 }, /* TERMSYM_LDQUOTE */
137 { "\'\'", 2 }, /* TERMSYM_RDQUOTE */
138 { "\"", 1 }, /* TERMSYM_DQUOTE */
139 { "<", 1 }, /* TERMSYM_LT */
140 { ">", 1 }, /* TERMSYM_GT */
141 { "<=", 2 }, /* TERMSYM_LE */
142 { ">=", 2 }, /* TERMSYM_GE */
143 { "==", 2 }, /* TERMSYM_EQ */
144 { "!=", 2 }, /* TERMSYM_NEQ */
145 { "\'", 1 }, /* TERMSYM_ACUTE */
146 { "`", 1 }, /* TERMSYM_GRAVE */
147 { "pi", 2 }, /* TERMSYM_PI */
148 { "+=", 2 }, /* TERMSYM_PLUSMINUS */
149 { "oo", 2 }, /* TERMSYM_INF */
150 { "infinity", 8 }, /* TERMSYM_INF2 */
151 { "NaN", 3 }, /* TERMSYM_NAN */
152 { "|", 1 }, /* TERMSYM_BAR */
153 { "o", 1 }, /* TERMSYM_BULLET */
154 { "&", 1 }, /* TERMSYM_AMP */
155 { "--", 2 }, /* TERMSYM_EM */
156 { "-", 1 }, /* TERMSYM_EN */
157 { "(C)", 3 }, /* TERMSYM_COPY */
158 { "*", 1 }, /* TERMSYM_ASTERISK */
159 { "\\", 1 }, /* TERMSYM_SLASH */
160 { "-", 1 }, /* TERMSYM_HYPHEN */
161 { " ", 1 }, /* TERMSYM_SPACE */
162 { ".", 1 }, /* TERMSYM_PERIOD */
163 { "", 0 }, /* TERMSYM_BREAK */
164 { "<", 1 }, /* TERMSYM_LANGLE */
165 { ">", 1 }, /* TERMSYM_RANGLE */
168 static const char ansi_clear
[] = { 27, '[', '0', 'm' };
169 static const char ansi_bold
[] = { 27, '[', '1', 'm' };
170 static const char ansi_under
[] = { 27, '[', '4', 'm' };
172 static struct termsym termstyle_ansi
[] = {
180 main(int argc
, char *argv
[])
184 const struct mdoc
*mdoc
;
189 c
= mmain_getopt(p
, argc
, argv
, NULL
, NULL
, NULL
, NULL
);
191 mmain_exit(p
, -1 == c
? 1 : 0);
193 if (NULL
== (mdoc
= mmain_mdoc(p
)))
196 termp
.maxrmargin
= termp
.rmargin
= 78; /* XXX */
197 termp
.maxcols
= 1024;
198 termp
.offset
= termp
.col
= 0;
199 termp
.flags
= TERMP_NOSPACE
;
200 termp
.symtab
= termsym_ansi
;
201 termp
.styletab
= termstyle_ansi
;
203 if (NULL
== (termp
.buf
= malloc(termp
.maxcols
)))
206 header(&termp
, mdoc_meta(mdoc
));
207 body(&termp
, NULL
, mdoc_meta(mdoc
), mdoc_node(mdoc
));
208 footer(&termp
, mdoc_meta(mdoc
));
218 * Flush a line of text. A "line" is loosely defined as being something
219 * that should be followed by a newline, regardless of whether it's
220 * broken apart by newlines getting there. A line can also be a
221 * fragment of a columnar list.
223 * Specifically, a line is whatever's in p->buf of length p->col, which
224 * is zeroed after this function returns.
226 * The variables TERMP_NOLPAD, TERMP_LITERAL and TERMP_NOBREAK are of
227 * critical importance here. Their behaviour follows:
229 * - TERMP_NOLPAD: when beginning to write the line, don't left-pad the
230 * offset value. This is useful when doing columnar lists where the
231 * prior column has right-padded.
233 * - TERMP_NOBREAK: this is the most important and is used when making
234 * columns. In short: don't print a newline and instead pad to the
235 * right margin. Used in conjunction with TERMP_NOLPAD.
237 * In-line line breaking:
239 * If TERMP_NOBREAK is specified and the line overruns the right
240 * margin, it will break and pad-right to the right margin after
241 * writing. If maxrmargin is violated, it will break and continue
242 * writing from the right-margin, which will lead to the above
243 * scenario upon exit.
245 * Otherwise, the line will break at the right margin. Extremely long
246 * lines will cause the system to emit a warning (TODO: hyphenate, if
250 flushln(struct termp
*p
)
252 size_t i
, j
, vsz
, vis
, maxvis
, mmax
, bp
;
255 * First, establish the maximum columns of "visible" content.
256 * This is usually the difference between the right-margin and
257 * an indentation, but can be, for tagged lists or columns, a
258 * small set of values.
261 assert(p
->offset
< p
->rmargin
);
262 maxvis
= p
->rmargin
- p
->offset
;
263 mmax
= p
->maxrmargin
- p
->offset
;
264 bp
= TERMP_NOBREAK
& p
->flags
? mmax
: maxvis
;
268 * If in the standard case (left-justified), then begin with our
269 * indentation, otherwise (columns, etc.) just start spitting
273 if ( ! (p
->flags
& TERMP_NOLPAD
))
275 for (j
= 0; j
< p
->offset
; j
++)
278 for (i
= 0; i
< p
->col
; i
++) {
280 * Count up visible word characters. Control sequences
281 * (starting with the CSI) aren't counted. A space
282 * generates a non-printing word, which is valid (the
283 * space is printed according to regular spacing rules).
286 /* FIXME: make non-ANSI friendly. */
289 for (j
= i
, vsz
= 0; j
< p
->col
; j
++) {
290 if (isspace((u_char
)p
->buf
[j
]))
292 else if (27 == p
->buf
[j
]) {
293 assert(j
+ 4 <= p
->col
);
300 * Do line-breaking. If we're greater than our
301 * break-point and already in-line, break to the next
302 * line and start writing. If we're at the line start,
303 * then write out the word (TODO: hyphenate) and break
304 * in a subsequent loop invocation.
307 if ( ! (TERMP_NOBREAK
& p
->flags
)) {
308 if (vis
&& vis
+ vsz
> bp
) {
310 for (j
= 0; j
< p
->offset
; j
++)
313 } else if (vis
+ vsz
> bp
)
314 warnx("word breaks right margin");
316 /* TODO: hyphenate. */
319 if (vis
&& vis
+ vsz
> bp
) {
321 for (j
= 0; j
< p
->rmargin
; j
++)
323 vis
= p
->rmargin
- p
->offset
;
324 } else if (vis
+ vsz
> bp
)
325 warnx("word breaks right margin");
327 /* TODO: hyphenate. */
331 * Write out the word and a trailing space. Omit the
332 * space if we're the last word in the line or beyond
336 for ( ; i
< p
->col
; i
++) {
337 if (isspace((u_char
)p
->buf
[i
]))
342 if (i
< p
->col
&& vis
<= bp
) {
349 * If we've overstepped our maximum visible no-break space, then
350 * cause a newline and offset at the right margin.
353 if ((TERMP_NOBREAK
& p
->flags
) && vis
>= maxvis
) {
354 if ( ! (TERMP_NONOBREAK
& p
->flags
)) {
356 for (i
= 0; i
< p
->rmargin
; i
++)
364 * If we're not to right-marginalise it (newline), then instead
365 * pad to the right margin and stay off.
368 if (p
->flags
& TERMP_NOBREAK
) {
369 if ( ! (TERMP_NONOBREAK
& p
->flags
))
370 for ( ; vis
< maxvis
; vis
++)
380 * A newline only breaks an existing line; it won't assert vertical
381 * space. All data in the output buffer is flushed prior to the newline
385 newln(struct termp
*p
)
388 p
->flags
|= TERMP_NOSPACE
;
390 p
->flags
&= ~TERMP_NOLPAD
;
394 p
->flags
&= ~TERMP_NOLPAD
;
399 * Asserts a vertical space (a full, empty line-break between lines).
400 * Note that if used twice, this will cause two blank spaces and so on.
401 * All data in the output buffer is flushed prior to the newline
405 vspace(struct termp
*p
)
414 * Break apart a word into "pwords" (partial-words, usually from
415 * breaking up a phrase into individual words) and, eventually, put them
416 * into the output buffer. If we're a literal word, then don't break up
417 * the word and put it verbatim into the output buffer.
420 word(struct termp
*p
, const char *word
)
424 if (p
->flags
& TERMP_LITERAL
) {
425 pword(p
, word
, strlen(word
));
432 if (mdoc_isdelim(word
)) {
433 if ( ! (p
->flags
& TERMP_IGNDELIM
))
434 p
->flags
|= TERMP_NOSPACE
;
435 p
->flags
&= ~TERMP_IGNDELIM
;
439 for (j
= i
= 0; i
< len
; i
++) {
440 if ( ! isspace((u_char
)word
[i
])) {
445 /* Escaped spaces don't delimit... */
446 if (i
> 0 && isspace((u_char
)word
[i
]) &&
447 '\\' == word
[i
- 1]) {
455 pword(p
, &word
[i
- j
], j
);
460 pword(p
, &word
[i
- j
], j
);
466 * This is the main function for printing out nodes. It's constituted
467 * of PRE and POST functions, which correspond to prefix and infix
468 * processing. The termpair structure allows data to persist between
469 * prefix and postfix invocations.
472 body(struct termp
*p
, struct termpair
*ppair
,
473 const struct mdoc_meta
*meta
,
474 const struct mdoc_node
*node
)
477 struct termpair pair
;
479 /* Pre-processing. */
484 pair
.offset
= pair
.rmargin
= 0;
488 if (MDOC_TEXT
!= node
->type
) {
489 if (termacts
[node
->tok
].pre
)
490 if ( ! (*termacts
[node
->tok
].pre
)(p
, &pair
, meta
, node
))
492 } else /* MDOC_TEXT == node->type */
493 word(p
, node
->data
.text
.string
);
497 if (TERMPAIR_FLAG
& pair
.type
)
498 p
->flags
|= pair
.flag
;
500 if (dochild
&& node
->child
)
501 body(p
, &pair
, meta
, node
->child
);
503 if (TERMPAIR_FLAG
& pair
.type
)
504 p
->flags
&= ~pair
.flag
;
506 /* Post-processing. */
508 if (MDOC_TEXT
!= node
->type
)
509 if (termacts
[node
->tok
].post
)
510 (*termacts
[node
->tok
].post
)(p
, &pair
, meta
, node
);
515 body(p
, ppair
, meta
, node
->next
);
520 footer(struct termp
*p
, const struct mdoc_meta
*meta
)
525 if (NULL
== (buf
= malloc(p
->rmargin
)))
527 if (NULL
== (os
= malloc(p
->rmargin
)))
530 tm
= localtime(&meta
->date
);
533 if (NULL
== strftime(buf
, p
->rmargin
, "%B %d, %Y", tm
))
535 if (0 == strftime(buf
, p
->rmargin
, "%B %d, %Y", tm
))
539 (void)strlcpy(os
, meta
->os
, p
->rmargin
);
542 * This is /slightly/ different from regular groff output
543 * because we don't have page numbers. Print the following:
550 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
551 p
->rmargin
= p
->maxrmargin
- strlen(buf
);
557 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
558 p
->offset
= p
->rmargin
;
559 p
->rmargin
= p
->maxrmargin
;
560 p
->flags
&= ~TERMP_NOBREAK
;
571 header(struct termp
*p
, const struct mdoc_meta
*meta
)
573 char *buf
, *title
, *bufp
;
575 p
->rmargin
= p
->maxrmargin
;
578 if (NULL
== (buf
= malloc(p
->rmargin
)))
580 if (NULL
== (title
= malloc(p
->rmargin
)))
584 * The header is strange. It has three components, which are
585 * really two with the first duplicated. It goes like this:
587 * IDENTIFIER TITLE IDENTIFIER
589 * The IDENTIFIER is NAME(SECTION), which is the command-name
590 * (if given, or "unknown" if not) followed by the manual page
591 * section. These are given in `Dt'. The TITLE is a free-form
592 * string depending on the manual volume. If not specified, it
593 * switches on the manual section.
597 (void)strlcpy(buf
, meta
->vol
, p
->rmargin
);
600 (void)strlcat(buf
, " (", p
->rmargin
);
601 (void)strlcat(buf
, meta
->arch
, p
->rmargin
);
602 (void)strlcat(buf
, ")", p
->rmargin
);
605 (void)snprintf(title
, p
->rmargin
, "%s(%d)",
606 meta
->title
, meta
->msec
);
608 for (bufp
= title
; *bufp
; bufp
++)
609 *bufp
= toupper((u_char
)*bufp
);
612 p
->rmargin
= (p
->maxrmargin
- strlen(buf
)) / 2;
613 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
618 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
619 p
->offset
= p
->rmargin
;
620 p
->rmargin
= p
->maxrmargin
- strlen(title
);
625 p
->offset
= p
->rmargin
;
626 p
->rmargin
= p
->maxrmargin
;
627 p
->flags
&= ~TERMP_NOBREAK
;
628 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
633 p
->rmargin
= p
->maxrmargin
;
635 p
->flags
&= ~TERMP_NOSPACE
;
643 * Determine the symbol indicated by an escape sequences, that is, one
644 * starting with a backslash. Once done, we pass this value into the
645 * output buffer by way of the symbol table.
648 nescape(struct termp
*p
, const char *word
, size_t len
)
660 warnx("unsupported %zu-byte escape sequence", len
);
664 for ( ; enc
->enc
; enc
++)
665 if (0 == memcmp(enc
->enc
, word
, len
)) {
666 symbola(p
, enc
->sym
);
670 warnx("unsupported %zu-byte escape sequence", len
);
675 * Handle an escape sequence: determine its length and pass it to the
676 * escape-symbol look table. Note that we assume mdoc(3) has validated
677 * the escape sequence (we assert upon badly-formed escape sequences).
680 pescape(struct termp
*p
, const char *word
, size_t *i
, size_t len
)
687 if ('(' == word
[*i
]) {
689 assert(*i
+ 1 < len
);
690 nescape(p
, &word
[*i
], 2);
694 } else if ('*' == word
[*i
]) {
695 /* XXX - deprecated! */
701 assert(*i
+ 1 < len
);
702 nescape(p
, &word
[*i
], 2);
708 nescape(p
, &word
[*i
], 1);
712 } else if ('[' != word
[*i
]) {
713 nescape(p
, &word
[*i
], 1);
718 for (j
= 0; word
[*i
] && ']' != word
[*i
]; (*i
)++, j
++)
722 nescape(p
, &word
[*i
- j
], j
);
727 * Handle pwords, partial words, which may be either a single word or a
728 * phrase that cannot be broken down (such as a literal string). This
729 * handles word styling.
732 pword(struct termp
*p
, const char *word
, size_t len
)
736 if ( ! (TERMP_NOSPACE
& p
->flags
) &&
737 ! (TERMP_LITERAL
& p
->flags
))
740 if ( ! (p
->flags
& TERMP_NONOSPACE
))
741 p
->flags
&= ~TERMP_NOSPACE
;
744 * XXX - if literal and underlining, this will underline the
745 * spaces between literal words.
748 if (p
->flags
& TERMP_BOLD
)
749 stylea(p
, TERMSTYLE_BOLD
);
750 if (p
->flags
& TERMP_UNDERLINE
)
751 stylea(p
, TERMSTYLE_UNDER
);
753 for (i
= 0; i
< len
; i
++) {
754 if ('\\' == word
[i
]) {
755 pescape(p
, word
, &i
, len
);
761 if (p
->flags
& TERMP_BOLD
||
762 p
->flags
& TERMP_UNDERLINE
)
763 stylea(p
, TERMSTYLE_CLEAR
);
768 * Add a symbol to the output line buffer.
771 symbola(struct termp
*p
, enum tsym sym
)
774 assert(p
->symtab
[sym
].sym
);
775 stringa(p
, p
->symtab
[sym
].sym
, p
->symtab
[sym
].sz
);
780 * Add a style to the output line buffer.
783 stylea(struct termp
*p
, enum tstyle style
)
786 assert(p
->styletab
[style
].sym
);
787 stringa(p
, p
->styletab
[style
].sym
, p
->styletab
[style
].sz
);
792 * Like chara() but for arbitrary-length buffers. Resize the buffer by
793 * a factor of two (if the buffer is less than that) or the buffer's
797 stringa(struct termp
*p
, const char *c
, size_t sz
)
804 s
= sz
> p
->maxcols
* 2 ? sz
: p
->maxcols
* 2;
807 if (p
->col
+ sz
>= p
->maxcols
) {
808 p
->buf
= realloc(p
->buf
, s
);
814 (void)memcpy(&p
->buf
[p
->col
], c
, sz
);
820 * Insert a single character into the line-buffer. If the buffer's
821 * space is exceeded, then allocate more space by doubling the buffer
825 chara(struct termp
*p
, char c
)
828 if (p
->col
+ 1 >= p
->maxcols
) {
829 p
->buf
= realloc(p
->buf
, p
->maxcols
* 2);
834 p
->buf
[(p
->col
)++] = c
;