]>
git.cameronkatri.com Git - mandoc.git/blob - man.c
1 /* $Id: man.c,v 1.58 2010/03/29 04:52:14 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 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 above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/types.h>
31 #include "libmandoc.h"
33 const char *const __man_merrnames
[WERRMAX
] = {
34 "invalid character", /* WNPRINT */
35 "invalid manual section", /* WMSEC */
36 "invalid date format", /* WDATE */
37 "scope of prior line violated", /* WLNSCOPE */
38 "over-zealous prior line scope violation", /* WLNSCOPE2 */
39 "trailing whitespace", /* WTSPACE */
40 "unterminated quoted parameter", /* WTQUOTE */
41 "document has no body", /* WNODATA */
42 "document has no title/section", /* WNOTITLE */
43 "invalid escape sequence", /* WESCAPE */
44 "invalid number format", /* WNUMFMT */
45 "expected block head arguments", /* WHEADARGS */
46 "expected block body arguments", /* WBODYARGS */
47 "expected empty block head", /* WNHEADARGS */
48 "ill-formed macro", /* WMACROFORM */
49 "scope open on exit", /* WEXITSCOPE */
50 "no scope context", /* WNOSCOPE */
51 "literal context already open", /* WOLITERAL */
52 "no literal context open", /* WNLITERAL */
53 "invalid nesting of roff declarations", /* WROFFNEST */
54 "scope in roff instructions broken", /* WROFFSCOPE */
55 "document title should be uppercase", /* WTITLECASE */
58 const char *const __man_macronames
[MAN_MAX
] = {
59 "br", "TH", "SH", "SS",
60 "TP", "LP", "PP", "P",
61 "IP", "HP", "SM", "SB",
62 "BI", "IB", "BR", "RB",
64 "RI", "na", "i", "sp",
65 "nf", "fi", "r", "RE",
66 "RS", "DT", "UC", "PD",
67 "Sp", "Vb", "Ve", "de",
68 "dei", "am", "ami", "ig",
72 const char * const *man_macronames
= __man_macronames
;
74 static struct man_node
*man_node_alloc(int, int,
75 enum man_type
, enum mant
);
76 static int man_node_append(struct man
*,
78 static void man_node_free(struct man_node
*);
79 static void man_node_unlink(struct man
*,
81 static int man_ptext(struct man
*, int, char *);
82 static int man_pmacro(struct man
*, int, char *);
83 static void man_free1(struct man
*);
84 static void man_alloc1(struct man
*);
85 static int pstring(struct man
*, int, int,
86 const char *, size_t);
87 static int macrowarn(struct man
*, int, const char *);
90 const struct man_node
*
91 man_node(const struct man
*m
)
94 return(MAN_HALT
& m
->flags
? NULL
: m
->first
);
98 const struct man_meta
*
99 man_meta(const struct man
*m
)
102 return(MAN_HALT
& m
->flags
? NULL
: &m
->meta
);
107 man_reset(struct man
*man
)
116 man_free(struct man
*man
)
125 man_alloc(void *data
, int pflags
, const struct man_cb
*cb
)
129 p
= mandoc_calloc(1, sizeof(struct man
));
132 memcpy(&p
->cb
, cb
, sizeof(struct man_cb
));
144 man_endparse(struct man
*m
)
147 if (MAN_HALT
& m
->flags
)
149 else if (man_macroend(m
))
151 m
->flags
|= MAN_HALT
;
157 man_parseln(struct man
*m
, int ln
, char *buf
)
160 return('.' == *buf
|| '\'' == *buf
?
161 man_pmacro(m
, ln
, buf
) :
162 man_ptext(m
, ln
, buf
));
167 man_free1(struct man
*man
)
171 man_node_delete(man
, man
->first
);
173 free(man
->meta
.title
);
174 if (man
->meta
.source
)
175 free(man
->meta
.source
);
182 man_alloc1(struct man
*m
)
185 memset(&m
->meta
, 0, sizeof(struct man_meta
));
187 m
->last
= mandoc_calloc(1, sizeof(struct man_node
));
189 m
->last
->type
= MAN_ROOT
;
190 m
->last
->tok
= MAN_MAX
;
191 m
->next
= MAN_NEXT_CHILD
;
196 man_node_append(struct man
*man
, struct man_node
*p
)
201 assert(MAN_ROOT
!= p
->type
);
204 case (MAN_NEXT_SIBLING
):
207 p
->parent
= man
->last
->parent
;
209 case (MAN_NEXT_CHILD
):
210 man
->last
->child
= p
;
211 p
->parent
= man
->last
;
221 if ( ! man_valid_pre(man
, p
))
226 assert(MAN_BLOCK
== p
->parent
->type
);
230 assert(MAN_BLOCK
== p
->parent
->type
);
241 if ( ! man_valid_post(man
))
243 if ( ! man_action_post(man
))
254 static struct man_node
*
255 man_node_alloc(int line
, int pos
, enum man_type type
, enum mant tok
)
259 p
= mandoc_calloc(1, sizeof(struct man_node
));
269 man_elem_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
273 p
= man_node_alloc(line
, pos
, MAN_ELEM
, tok
);
274 if ( ! man_node_append(m
, p
))
276 m
->next
= MAN_NEXT_CHILD
;
282 man_head_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
286 p
= man_node_alloc(line
, pos
, MAN_HEAD
, tok
);
287 if ( ! man_node_append(m
, p
))
289 m
->next
= MAN_NEXT_CHILD
;
295 man_body_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
299 p
= man_node_alloc(line
, pos
, MAN_BODY
, tok
);
300 if ( ! man_node_append(m
, p
))
302 m
->next
= MAN_NEXT_CHILD
;
308 man_block_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
312 p
= man_node_alloc(line
, pos
, MAN_BLOCK
, tok
);
313 if ( ! man_node_append(m
, p
))
315 m
->next
= MAN_NEXT_CHILD
;
321 pstring(struct man
*m
, int line
, int pos
,
322 const char *p
, size_t len
)
327 n
= man_node_alloc(line
, pos
, MAN_TEXT
, MAN_MAX
);
328 n
->string
= mandoc_malloc(len
+ 1);
329 sv
= strlcpy(n
->string
, p
, len
+ 1);
331 /* Prohibit truncation. */
332 assert(sv
< len
+ 1);
334 if ( ! man_node_append(m
, n
))
336 m
->next
= MAN_NEXT_SIBLING
;
342 man_word_alloc(struct man
*m
, int line
, int pos
, const char *word
)
345 return(pstring(m
, line
, pos
, word
, strlen(word
)));
350 * Free all of the resources held by a node. This does NOT unlink a
351 * node from its context; for that, see man_node_unlink().
354 man_node_free(struct man_node
*p
)
364 man_node_delete(struct man
*m
, struct man_node
*p
)
368 man_node_delete(m
, p
->child
);
370 man_node_unlink(m
, p
);
376 man_ptext(struct man
*m
, int line
, char *buf
)
381 /* Literal free-form text whitespace is preserved. */
383 if (MAN_LITERAL
& m
->flags
) {
384 if ( ! man_word_alloc(m
, line
, 0, buf
))
389 /* First de-chunk and allocate words. */
391 for (i
= 0; ' ' == buf
[i
]; i
++)
392 /* Skip leading whitespace. */ ;
394 if ('\0' == buf
[i
]) {
395 /* Trailing whitespace? */
396 if (i
&& ' ' == buf
[i
- 1])
397 if ( ! man_pwarn(m
, line
, i
- 1, WTSPACE
))
399 if ( ! pstring(m
, line
, 0, &buf
[i
], 0))
404 for (j
= i
; buf
[i
]; i
++) {
408 /* Escaped whitespace. */
409 if (i
&& ' ' == buf
[i
] && '\\' == buf
[i
- 1])
415 if ( ! pstring(m
, line
, j
, &buf
[j
], (size_t)(i
- j
)))
418 /* Trailing whitespace? Check at overwritten byte. */
420 if (' ' == sv
&& '\0' == buf
[i
])
421 if ( ! man_pwarn(m
, line
, i
- 1, WTSPACE
))
424 for ( ; ' ' == buf
[i
]; i
++)
425 /* Skip trailing whitespace. */ ;
429 /* Trailing whitespace? */
431 if (' ' == buf
[i
- 1] && '\0' == buf
[i
])
432 if ( ! man_pwarn(m
, line
, i
- 1, WTSPACE
))
439 if (j
!= i
&& ! pstring(m
, line
, j
, &buf
[j
], (size_t)(i
- j
)))
445 * Co-ordinate what happens with having a next-line scope open:
446 * first close out the element scope (if applicable), then close
447 * out the block scope (also if applicable).
450 if (MAN_ELINE
& m
->flags
) {
451 m
->flags
&= ~MAN_ELINE
;
452 if ( ! man_unscope(m
, m
->last
->parent
, WERRMAX
))
456 if ( ! (MAN_BLINE
& m
->flags
))
458 m
->flags
&= ~MAN_BLINE
;
460 if ( ! man_unscope(m
, m
->last
->parent
, WERRMAX
))
462 return(man_body_alloc(m
, line
, 0, m
->last
->tok
));
467 macrowarn(struct man
*m
, int ln
, const char *buf
)
469 if ( ! (MAN_IGN_MACRO
& m
->pflags
))
470 return(man_verr(m
, ln
, 0,
471 "unknown macro: %s%s",
472 buf
, strlen(buf
) > 3 ? "..." : ""));
473 return(man_vwarn(m
, ln
, 0, "unknown macro: %s%s",
474 buf
, strlen(buf
) > 3 ? "..." : ""));
479 man_pmacro(struct man
*m
, int ln
, char *buf
)
486 /* Comments and empties are quickly ignored. */
488 if (MAN_BLINE
& m
->flags
)
489 m
->flags
|= MAN_BPLINE
;
497 * Skip whitespace between the control character and initial
498 * text. "Whitespace" is both spaces and tabs.
500 if (' ' == buf
[i
] || '\t' == buf
[i
]) {
502 while (buf
[i
] && (' ' == buf
[i
] || '\t' == buf
[i
]))
510 /* Copy the first word into a nil-terminated buffer. */
512 for (j
= 0; j
< 4; j
++, i
++) {
513 if ('\0' == (mac
[j
] = buf
[i
]))
515 else if (' ' == buf
[i
])
518 /* Check for invalid characters. */
520 if (isgraph((u_char
)buf
[i
]))
522 return(man_perr(m
, ln
, i
, WNPRINT
));
527 if (j
== 4 || j
< 1) {
528 if ( ! (MAN_IGN_MACRO
& m
->pflags
)) {
529 (void)man_perr(m
, ln
, ppos
, WMACROFORM
);
532 if ( ! man_pwarn(m
, ln
, ppos
, WMACROFORM
))
537 if (MAN_MAX
== (tok
= man_hash_find(mac
))) {
538 if ( ! macrowarn(m
, ln
, mac
))
543 /* The macro is sane. Jump to the next word. */
545 while (buf
[i
] && ' ' == buf
[i
])
548 /* Trailing whitespace? */
550 if ('\0' == buf
[i
] && ' ' == buf
[i
- 1])
551 if ( ! man_pwarn(m
, ln
, i
- 1, WTSPACE
))
555 * Remove prior ELINE macro, as it's being clobbering by a new
556 * macro. Note that NSCOPED macros do not close out ELINE
557 * macros---they don't print text---so we let those slip by.
560 if ( ! (MAN_NSCOPED
& man_macros
[tok
].flags
) &&
561 m
->flags
& MAN_ELINE
) {
562 assert(MAN_TEXT
!= m
->last
->type
);
565 * This occurs in the following construction:
571 * Flat-out disallow this madness.
573 if (MAN_NSCOPED
& man_macros
[m
->last
->tok
].flags
)
574 return(man_perr(m
, ln
, ppos
, WLNSCOPE
));
579 assert(NULL
== n
->child
);
580 assert(0 == n
->nchild
);
582 if ( ! man_nwarn(m
, n
, WLNSCOPE
))
585 man_node_delete(m
, n
);
586 m
->flags
&= ~MAN_ELINE
;
589 /* Begin recursive parse sequence. */
591 assert(man_macros
[tok
].fp
);
593 if ( ! (*man_macros
[tok
].fp
)(m
, tok
, ln
, ppos
, &i
, buf
))
598 * We weren't in a block-line scope when entering the
599 * above-parsed macro, so return.
601 * FIXME: this prohibits the nesting of blocks (e.g., `de' and
602 * family) within BLINE or ELINE systems. This is annoying.
605 if ( ! (MAN_BPLINE
& m
->flags
)) {
606 m
->flags
&= ~MAN_ILINE
;
609 m
->flags
&= ~MAN_BPLINE
;
612 * If we're in a block scope, then allow this macro to slip by
613 * without closing scope around it.
616 if (MAN_ILINE
& m
->flags
) {
617 m
->flags
&= ~MAN_ILINE
;
622 * If we've opened a new next-line element scope, then return
623 * now, as the next line will close out the block scope.
626 if (MAN_ELINE
& m
->flags
)
629 /* Close out the block scope opened in the prior line. */
631 assert(MAN_BLINE
& m
->flags
);
632 m
->flags
&= ~MAN_BLINE
;
634 if ( ! man_unscope(m
, m
->last
->parent
, WERRMAX
))
636 return(man_body_alloc(m
, ln
, 0, m
->last
->tok
));
638 err
: /* Error out. */
640 m
->flags
|= MAN_HALT
;
646 man_verr(struct man
*man
, int ln
, int pos
, const char *fmt
, ...)
651 if (NULL
== man
->cb
.man_err
)
655 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
657 return((*man
->cb
.man_err
)(man
->data
, ln
, pos
, buf
));
662 man_vwarn(struct man
*man
, int ln
, int pos
, const char *fmt
, ...)
667 if (NULL
== man
->cb
.man_warn
)
671 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
673 return((*man
->cb
.man_warn
)(man
->data
, ln
, pos
, buf
));
678 man_err(struct man
*m
, int line
, int pos
, int iserr
, enum merr type
)
682 p
= __man_merrnames
[(int)type
];
686 return(man_verr(m
, line
, pos
, p
));
688 return(man_vwarn(m
, line
, pos
, p
));
693 * Unlink a node from its context. If "m" is provided, the last parse
694 * point will also be adjusted accordingly.
697 man_node_unlink(struct man
*m
, struct man_node
*n
)
700 /* Adjust siblings. */
703 n
->prev
->next
= n
->next
;
705 n
->next
->prev
= n
->prev
;
711 if (n
->parent
->child
== n
)
712 n
->parent
->child
= n
->prev
? n
->prev
: n
->next
;
715 /* Adjust parse point, if applicable. */
717 if (m
&& m
->last
== n
) {
718 /*XXX: this can occur when bailing from validation. */
719 /*assert(NULL == n->next);*/
722 m
->next
= MAN_NEXT_SIBLING
;
725 m
->next
= MAN_NEXT_CHILD
;
729 if (m
&& m
->first
== n
)