]>
git.cameronkatri.com Git - mandoc.git/blob - mdocterm.c
d4df8a39be082a958a4cd244e55d3d123fd17358
1 /* $Id: mdocterm.c,v 1.22 2009/03/01 13:06:49 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 we're breaking normally...
195 * If a word is too long and we're within a line, put it
196 * on the next line. Puke if we're being asked to write
197 * something that will exceed the right margin (i.e.,
198 * from a fresh line).
200 * If we're not breaking...
202 * Don't let the visible size exceed the full margin.
205 if ( ! (TERMP_NOBREAK
& p
->flags
)) {
206 if (vis
&& vis
+ vsz
> maxvis
) {
208 for (j
= 0; j
< p
->offset
; j
++)
211 } else if (vis
+ vsz
> maxvis
)
212 errx(1, "word breaks right margin");
213 } else if (vis
+ vsz
> p
->maxrmargin
)
214 errx(1, "word breaks right margin");
217 * Write out the word and a trailing space. Omit the
218 * space if we're the last word in the line.
221 for ( ; i
< p
->col
; i
++) {
222 if (xisspace(p
->buf
[i
]))
233 if ((TERMP_NOBREAK
& p
->flags
) && vis
>= maxvis
) {
235 for (i
= 0; i
< p
->rmargin
; i
++)
242 * If we're not to right-marginalise it (newline), then instead
243 * pad to the right margin and stay off.
246 if (p
->flags
& TERMP_NOBREAK
) {
247 for ( ; vis
< maxvis
; vis
++)
257 newln(struct termp
*p
)
261 * A newline only breaks an existing line; it won't assert
264 p
->flags
|= TERMP_NOSPACE
;
266 p
->flags
&= ~TERMP_NOLPAD
;
270 p
->flags
&= ~TERMP_NOLPAD
;
275 vspace(struct termp
*p
)
279 * Asserts a vertical space (a full, empty line-break between
288 stringa(struct termp
*p
, const char *s
)
291 /* XXX - speed up if not passing to chara. */
298 chara(struct termp
*p
, char c
)
302 * Insert a single character into the line-buffer. If the
303 * buffer's space is exceeded, then allocate more space.
305 if (p
->col
+ 1 >= p
->maxcols
) {
306 p
->buf
= realloc(p
->buf
, p
->maxcols
* 2);
311 p
->buf
[(p
->col
)++] = c
;
316 style(struct termp
*p
, enum termstyle esc
)
319 if (p
->col
+ 4 >= p
->maxcols
)
320 errx(1, "line overrun");
322 p
->buf
[(p
->col
)++] = 27;
323 p
->buf
[(p
->col
)++] = '[';
326 p
->buf
[(p
->col
)++] = '0';
329 p
->buf
[(p
->col
)++] = '1';
331 case (STYLE_UNDERLINE
):
332 p
->buf
[(p
->col
)++] = '4';
338 p
->buf
[(p
->col
)++] = 'm';
343 nescape(struct termp
*p
, const char *word
, size_t len
)
349 stringa(p
, TERMSYM_DQUOTE
);
352 if ('r' == word
[0] && 'B' == word
[1])
353 stringa(p
, TERMSYM_RBRACK
);
354 else if ('l' == word
[0] && 'B' == word
[1])
355 stringa(p
, TERMSYM_LBRACK
);
356 else if ('l' == word
[0] && 'q' == word
[1])
357 stringa(p
, TERMSYM_LDQUOTE
);
358 else if ('r' == word
[0] && 'q' == word
[1])
359 stringa(p
, TERMSYM_RDQUOTE
);
360 else if ('o' == word
[0] && 'q' == word
[1])
361 stringa(p
, TERMSYM_LSQUOTE
);
362 else if ('a' == word
[0] && 'q' == word
[1])
363 stringa(p
, TERMSYM_RSQUOTE
);
364 else if ('<' == word
[0] && '-' == word
[1])
365 stringa(p
, TERMSYM_LARROW
);
366 else if ('-' == word
[0] && '>' == word
[1])
367 stringa(p
, TERMSYM_RARROW
);
368 else if ('b' == word
[0] && 'u' == word
[1])
369 stringa(p
, TERMSYM_BULLET
);
370 else if ('<' == word
[0] && '=' == word
[1])
371 stringa(p
, TERMSYM_LE
);
372 else if ('>' == word
[0] && '=' == word
[1])
373 stringa(p
, TERMSYM_GE
);
374 else if ('=' == word
[0] && '=' == word
[1])
375 stringa(p
, TERMSYM_EQ
);
376 else if ('+' == word
[0] && '-' == word
[1])
377 stringa(p
, TERMSYM_PLUSMINUS
);
378 else if ('u' == word
[0] && 'a' == word
[1])
379 stringa(p
, TERMSYM_UARROW
);
380 else if ('d' == word
[0] && 'a' == word
[1])
381 stringa(p
, TERMSYM_DARROW
);
382 else if ('a' == word
[0] && 'a' == word
[1])
383 stringa(p
, TERMSYM_ACUTE
);
384 else if ('g' == word
[0] && 'a' == word
[1])
385 stringa(p
, TERMSYM_GRAVE
);
386 else if ('!' == word
[0] && '=' == word
[1])
387 stringa(p
, TERMSYM_NEQ
);
388 else if ('i' == word
[0] && 'f' == word
[1])
389 stringa(p
, TERMSYM_INF
);
390 else if ('n' == word
[0] && 'a' == word
[1])
391 stringa(p
, TERMSYM_NAN
);
392 else if ('b' == word
[0] && 'a' == word
[1])
393 stringa(p
, TERMSYM_BAR
);
395 /* Deprecated forms. */
396 else if ('B' == word
[0] && 'a' == word
[1])
397 stringa(p
, TERMSYM_BAR
);
398 else if ('I' == word
[0] && 'f' == word
[1])
399 stringa(p
, TERMSYM_INF2
);
400 else if ('G' == word
[0] && 'e' == word
[1])
401 stringa(p
, TERMSYM_GE
);
402 else if ('G' == word
[0] && 't' == word
[1])
403 stringa(p
, TERMSYM_GT
);
404 else if ('L' == word
[0] && 'e' == word
[1])
405 stringa(p
, TERMSYM_LE
);
406 else if ('L' == word
[0] && 'q' == word
[1])
407 stringa(p
, TERMSYM_LDQUOTE
);
408 else if ('L' == word
[0] && 't' == word
[1])
409 stringa(p
, TERMSYM_LT
);
410 else if ('N' == word
[0] && 'a' == word
[1])
411 stringa(p
, TERMSYM_NAN
);
412 else if ('N' == word
[0] && 'e' == word
[1])
413 stringa(p
, TERMSYM_NEQ
);
414 else if ('P' == word
[0] && 'i' == word
[1])
415 stringa(p
, TERMSYM_PI
);
416 else if ('P' == word
[0] && 'm' == word
[1])
417 stringa(p
, TERMSYM_PLUSMINUS
);
418 else if ('R' == word
[0] && 'q' == word
[1])
419 stringa(p
, TERMSYM_RDQUOTE
);
428 pescape(struct termp
*p
, const char *word
, size_t *i
, size_t len
)
436 * Handle an escape sequence. This must manage both groff-style
437 * escapes and mdoc-style escapes.
440 if ('(' == word
[*i
]) {
441 /* Two-character escapes. */
443 assert(*i
+ 1 < len
);
444 nescape(p
, &word
[*i
], 2);
448 } else if ('*' == word
[*i
]) {
454 assert(*i
+ 1 < len
);
455 nescape(p
, &word
[*i
], 2);
461 nescape(p
, &word
[*i
], 1);
464 } else if ('[' != word
[*i
]) {
465 /* One-character escapes. */
490 for (j
= 0; word
[*i
] && ']' != word
[*i
]; (*i
)++, j
++)
493 nescape(p
, &word
[*i
- j
], j
);
498 pword(struct termp
*p
, const char *word
, size_t len
)
502 /*assert(len > 0);*/ /* Can be, if literal. */
505 * Handle pwords, partial words, which may be either a single
506 * word or a phrase that cannot be broken down (such as a
507 * literal string). This handles word styling.
510 if ( ! (p
->flags
& TERMP_NOSPACE
) &&
511 ! (p
->flags
& TERMP_LITERAL
))
514 if ( ! (p
->flags
& TERMP_NONOSPACE
))
515 p
->flags
&= ~TERMP_NOSPACE
;
518 * XXX - if literal and underlining, this will underline the
519 * spaces between literal words.
522 if (p
->flags
& TERMP_BOLD
)
523 style(p
, STYLE_BOLD
);
524 if (p
->flags
& TERMP_UNDERLINE
)
525 style(p
, STYLE_UNDERLINE
);
527 for (i
= 0; i
< len
; i
++) {
528 if ('\\' == word
[i
]) {
529 pescape(p
, word
, &i
, len
);
535 if (p
->flags
& TERMP_BOLD
||
536 p
->flags
& TERMP_UNDERLINE
)
537 style(p
, STYLE_CLEAR
);
542 word(struct termp
*p
, const char *word
)
547 * Break apart a word into tokens. If we're a literal word,
548 * then don't. This doesn't handle zero-length words (there
549 * should be none) and makes sure that pword doesn't get spaces
550 * or nil words unless literal.
553 if (p
->flags
& TERMP_LITERAL
) {
554 pword(p
, word
, strlen(word
));
561 if (mdoc_isdelim(word
)) {
562 if ( ! (p
->flags
& TERMP_IGNDELIM
))
563 p
->flags
|= TERMP_NOSPACE
;
564 p
->flags
&= ~TERMP_IGNDELIM
;
568 for (j
= i
= 0; i
< len
; i
++) {
569 if ( ! xisspace(word
[i
])) {
574 /* Escaped spaces don't delimit... */
575 if (i
> 0 && xisspace(word
[i
]) && '\\' == word
[i
- 1]) {
583 pword(p
, &word
[i
- j
], j
);
588 pword(p
, &word
[i
- j
], j
);
594 body(struct termp
*p
, struct termpair
*ppair
,
595 const struct mdoc_meta
*meta
,
596 const struct mdoc_node
*node
)
599 struct termpair pair
;
602 * This is the main function for printing out nodes. It's
603 * constituted of PRE and POST functions, which correspond to
604 * prefix and infix processing.
607 /* Pre-processing. */
612 pair
.offset
= pair
.rmargin
= 0;
616 if (MDOC_TEXT
!= node
->type
) {
617 if (termacts
[node
->tok
].pre
)
618 if ( ! (*termacts
[node
->tok
].pre
)(p
, &pair
, meta
, node
))
620 } else /* MDOC_TEXT == node->type */
621 word(p
, node
->data
.text
.string
);
625 if (TERMPAIR_FLAG
& pair
.type
)
626 p
->flags
|= pair
.flag
;
628 if (dochild
&& node
->child
)
629 body(p
, &pair
, meta
, node
->child
);
631 if (TERMPAIR_FLAG
& pair
.type
)
632 p
->flags
&= ~pair
.flag
;
634 /* Post-processing. */
636 if (MDOC_TEXT
!= node
->type
)
637 if (termacts
[node
->tok
].post
)
638 (*termacts
[node
->tok
].post
)(p
, &pair
, meta
, node
);
643 body(p
, ppair
, meta
, node
->next
);
648 footer(struct termp
*p
, const struct mdoc_meta
*meta
)
653 if (NULL
== (buf
= malloc(p
->rmargin
)))
655 if (NULL
== (os
= malloc(p
->rmargin
)))
658 tm
= localtime(&meta
->date
);
661 if (NULL
== strftime(buf
, p
->rmargin
, "%B %d, %Y", tm
))
663 if (0 == strftime(buf
, p
->rmargin
, "%B %d, %Y", tm
))
667 (void)strlcpy(os
, meta
->os
, p
->rmargin
);
670 * This is /slightly/ different from regular groff output
671 * because we don't have page numbers. Print the following:
678 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
679 p
->rmargin
= p
->maxrmargin
- strlen(buf
);
685 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
686 p
->offset
= p
->rmargin
;
687 p
->rmargin
= p
->maxrmargin
;
688 p
->flags
&= ~TERMP_NOBREAK
;
699 header(struct termp
*p
, const struct mdoc_meta
*meta
)
701 char *buf
, *title
, *bufp
, *vbuf
;
705 p
->rmargin
= p
->maxrmargin
;
708 if (NULL
== (buf
= malloc(p
->rmargin
)))
710 if (NULL
== (title
= malloc(p
->rmargin
)))
712 if (NULL
== (vbuf
= malloc(p
->rmargin
)))
715 if (NULL
== (pp
= mdoc_vol2a(meta
->vol
))) {
716 switch (meta
->msec
) {
722 pp
= mdoc_vol2a(VOL_URM
);
725 pp
= mdoc_vol2a(VOL_SMM
);
734 pp
= mdoc_vol2a(VOL_PRM
);
737 pp
= mdoc_vol2a(VOL_KM
);
746 if (-1 == uname(&uts
))
748 (void)strlcat(vbuf
, uts
.sysname
, p
->rmargin
);
749 (void)strlcat(vbuf
, " ", p
->rmargin
);
750 } else if (NULL
== (pp
= mdoc_msec2a(meta
->msec
)))
751 pp
= mdoc_msec2a(MSEC_local
);
753 (void)strlcat(vbuf
, pp
, p
->rmargin
);
756 * The header is strange. It has three components, which are
757 * really two with the first duplicated. It goes like this:
759 * IDENTIFIER TITLE IDENTIFIER
761 * The IDENTIFIER is NAME(SECTION), which is the command-name
762 * (if given, or "unknown" if not) followed by the manual page
763 * section. These are given in `Dt'. The TITLE is a free-form
764 * string depending on the manual volume. If not specified, it
765 * switches on the manual section.
768 if (mdoc_arch2a(meta
->arch
))
769 (void)snprintf(buf
, p
->rmargin
, "%s (%s)",
770 vbuf
, mdoc_arch2a(meta
->arch
));
772 (void)strlcpy(buf
, vbuf
, p
->rmargin
);
774 pp
= mdoc_msec2a(meta
->msec
);
776 (void)snprintf(title
, p
->rmargin
, "%s(%s)",
777 meta
->title
, pp
? pp
: "");
779 for (bufp
= title
; *bufp
; bufp
++)
780 *bufp
= toupper(*bufp
);
783 p
->rmargin
= (p
->maxrmargin
- strlen(buf
)) / 2;
784 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
789 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
790 p
->offset
= p
->rmargin
;
791 p
->rmargin
= p
->maxrmargin
- strlen(title
);
796 p
->offset
= p
->rmargin
;
797 p
->rmargin
= p
->maxrmargin
;
798 p
->flags
&= ~TERMP_NOBREAK
;
799 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
804 p
->rmargin
= p
->maxrmargin
;
806 p
->flags
&= ~TERMP_NOSPACE
;