]>
git.cameronkatri.com Git - mandoc.git/blob - man.c
1 /* $Id: man.c,v 1.50 2010/03/22 05:59:32 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 "trailing whitespace", /* WTSPACE */
39 "unterminated quoted parameter", /* WTQUOTE */
40 "document has no body", /* WNODATA */
41 "document has no title/section", /* WNOTITLE */
42 "invalid escape sequence", /* WESCAPE */
43 "invalid number format", /* WNUMFMT */
44 "expected block head arguments", /* WHEADARGS */
45 "expected block body arguments", /* WBODYARGS */
46 "expected empty block head", /* WNHEADARGS */
47 "ill-formed macro", /* WMACROFORM */
48 "scope open on exit", /* WEXITSCOPE */
49 "no scope context", /* WNOSCOPE */
50 "literal context already open", /* WOLITERAL */
51 "no literal context open" /* WNLITERAL */
54 const char *const __man_macronames
[MAN_MAX
] = {
55 "br", "TH", "SH", "SS",
56 "TP", "LP", "PP", "P",
57 "IP", "HP", "SM", "SB",
58 "BI", "IB", "BR", "RB",
60 "RI", "na", "i", "sp",
61 "nf", "fi", "r", "RE",
62 "RS", "DT", "UC", "PD"
65 const char * const *man_macronames
= __man_macronames
;
67 static struct man_node
*man_node_alloc(int, int,
69 static int man_node_append(struct man
*,
71 static int man_ptext(struct man
*, int, char *);
72 static int man_pmacro(struct man
*, int, char *);
73 static void man_free1(struct man
*);
74 static void man_alloc1(struct man
*);
75 static int pstring(struct man
*, int, int,
76 const char *, size_t);
77 static int macrowarn(struct man
*, int, const char *);
80 const struct man_node
*
81 man_node(const struct man
*m
)
84 return(MAN_HALT
& m
->flags
? NULL
: m
->first
);
88 const struct man_meta
*
89 man_meta(const struct man
*m
)
92 return(MAN_HALT
& m
->flags
? NULL
: &m
->meta
);
97 man_reset(struct man
*man
)
106 man_free(struct man
*man
)
115 man_alloc(void *data
, int pflags
, const struct man_cb
*cb
)
119 p
= mandoc_calloc(1, sizeof(struct man
));
122 memcpy(&p
->cb
, cb
, sizeof(struct man_cb
));
134 man_endparse(struct man
*m
)
137 if (MAN_HALT
& m
->flags
)
139 else if (man_macroend(m
))
141 m
->flags
|= MAN_HALT
;
147 man_parseln(struct man
*m
, int ln
, char *buf
)
151 man_pmacro(m
, ln
, buf
) :
152 man_ptext(m
, ln
, buf
));
157 man_free1(struct man
*man
)
161 man_node_freelist(man
->first
);
163 free(man
->meta
.title
);
164 if (man
->meta
.source
)
165 free(man
->meta
.source
);
172 man_alloc1(struct man
*m
)
175 memset(&m
->meta
, 0, sizeof(struct man_meta
));
177 m
->last
= mandoc_calloc(1, sizeof(struct man_node
));
179 m
->last
->type
= MAN_ROOT
;
180 m
->next
= MAN_NEXT_CHILD
;
185 man_node_append(struct man
*man
, struct man_node
*p
)
190 assert(MAN_ROOT
!= p
->type
);
193 case (MAN_NEXT_SIBLING
):
196 p
->parent
= man
->last
->parent
;
198 case (MAN_NEXT_CHILD
):
199 man
->last
->child
= p
;
200 p
->parent
= man
->last
;
209 if ( ! man_valid_pre(man
, p
))
214 assert(MAN_BLOCK
== p
->parent
->type
);
218 assert(MAN_BLOCK
== p
->parent
->type
);
229 if ( ! man_valid_post(man
))
231 if ( ! man_action_post(man
))
242 static struct man_node
*
243 man_node_alloc(int line
, int pos
, enum man_type type
, int tok
)
247 p
= mandoc_calloc(1, sizeof(struct man_node
));
257 man_elem_alloc(struct man
*m
, int line
, int pos
, int tok
)
261 p
= man_node_alloc(line
, pos
, MAN_ELEM
, tok
);
262 if ( ! man_node_append(m
, p
))
264 m
->next
= MAN_NEXT_CHILD
;
270 man_head_alloc(struct man
*m
, int line
, int pos
, int tok
)
274 p
= man_node_alloc(line
, pos
, MAN_HEAD
, tok
);
275 if ( ! man_node_append(m
, p
))
277 m
->next
= MAN_NEXT_CHILD
;
283 man_body_alloc(struct man
*m
, int line
, int pos
, int tok
)
287 p
= man_node_alloc(line
, pos
, MAN_BODY
, tok
);
288 if ( ! man_node_append(m
, p
))
290 m
->next
= MAN_NEXT_CHILD
;
296 man_block_alloc(struct man
*m
, int line
, int pos
, int tok
)
300 p
= man_node_alloc(line
, pos
, MAN_BLOCK
, tok
);
301 if ( ! man_node_append(m
, p
))
303 m
->next
= MAN_NEXT_CHILD
;
309 pstring(struct man
*m
, int line
, int pos
,
310 const char *p
, size_t len
)
315 n
= man_node_alloc(line
, pos
, MAN_TEXT
, -1);
316 n
->string
= mandoc_malloc(len
+ 1);
317 sv
= strlcpy(n
->string
, p
, len
+ 1);
319 /* Prohibit truncation. */
320 assert(sv
< len
+ 1);
322 if ( ! man_node_append(m
, n
))
324 m
->next
= MAN_NEXT_SIBLING
;
330 man_word_alloc(struct man
*m
, int line
, int pos
, const char *word
)
333 return(pstring(m
, line
, pos
, word
, strlen(word
)));
338 man_node_free(struct man_node
*p
)
350 man_node_freelist(struct man_node
*p
)
355 man_node_freelist(p
->child
);
356 assert(0 == p
->nchild
);
360 man_node_freelist(n
);
365 man_ptext(struct man
*m
, int line
, char *buf
)
370 /* Literal free-form text whitespace is preserved. */
372 if (MAN_LITERAL
& m
->flags
) {
373 if ( ! man_word_alloc(m
, line
, 0, buf
))
378 /* First de-chunk and allocate words. */
380 for (i
= 0; ' ' == buf
[i
]; i
++)
381 /* Skip leading whitespace. */ ;
383 if ('\0' == buf
[i
]) {
384 /* Trailing whitespace? */
385 if (i
&& ' ' == buf
[i
- 1])
386 if ( ! man_pwarn(m
, line
, i
- 1, WTSPACE
))
388 if ( ! pstring(m
, line
, 0, &buf
[i
], 0))
393 for (j
= i
; buf
[i
]; i
++) {
397 /* Escaped whitespace. */
398 if (i
&& ' ' == buf
[i
] && '\\' == buf
[i
- 1])
404 if ( ! pstring(m
, line
, j
, &buf
[j
], (size_t)(i
- j
)))
407 /* Trailing whitespace? Check at overwritten byte. */
409 if (' ' == sv
&& '\0' == buf
[i
])
410 if ( ! man_pwarn(m
, line
, i
- 1, WTSPACE
))
413 for ( ; ' ' == buf
[i
]; i
++)
414 /* Skip trailing whitespace. */ ;
418 /* Trailing whitespace? */
420 if (' ' == buf
[i
- 1] && '\0' == buf
[i
])
421 if ( ! man_pwarn(m
, line
, i
- 1, WTSPACE
))
428 if (j
!= i
&& ! pstring(m
, line
, j
, &buf
[j
], (size_t)(i
- j
)))
434 * Co-ordinate what happens with having a next-line scope open:
435 * first close out the element scope (if applicable), then close
436 * out the block scope (also if applicable).
439 if (MAN_ELINE
& m
->flags
) {
440 m
->flags
&= ~MAN_ELINE
;
441 if ( ! man_unscope(m
, m
->last
->parent
))
445 if ( ! (MAN_BLINE
& m
->flags
))
447 m
->flags
&= ~MAN_BLINE
;
449 if ( ! man_unscope(m
, m
->last
->parent
))
451 return(man_body_alloc(m
, line
, 0, m
->last
->tok
));
456 macrowarn(struct man
*m
, int ln
, const char *buf
)
458 if ( ! (MAN_IGN_MACRO
& m
->pflags
))
459 return(man_verr(m
, ln
, 0,
460 "unknown macro: %s%s",
461 buf
, strlen(buf
) > 3 ? "..." : ""));
462 return(man_vwarn(m
, ln
, 0, "unknown macro: %s%s",
463 buf
, strlen(buf
) > 3 ? "..." : ""));
468 man_pmacro(struct man
*m
, int ln
, char *buf
)
470 int i
, j
, c
, ppos
, fl
;
474 /* Comments and empties are quickly ignored. */
485 while (buf
[i
] && ' ' == buf
[i
])
493 /* Copy the first word into a nil-terminated buffer. */
495 for (j
= 0; j
< 4; j
++, i
++) {
496 if ('\0' == (mac
[j
] = buf
[i
]))
498 else if (' ' == buf
[i
])
501 /* Check for invalid characters. */
503 if (isgraph((u_char
)buf
[i
]))
505 return(man_perr(m
, ln
, i
, WNPRINT
));
510 if (j
== 4 || j
< 1) {
511 if ( ! (MAN_IGN_MACRO
& m
->pflags
)) {
512 (void)man_perr(m
, ln
, ppos
, WMACROFORM
);
515 if ( ! man_pwarn(m
, ln
, ppos
, WMACROFORM
))
520 if (MAN_MAX
== (c
= man_hash_find(mac
))) {
521 if ( ! macrowarn(m
, ln
, mac
))
526 /* The macro is sane. Jump to the next word. */
528 while (buf
[i
] && ' ' == buf
[i
])
531 /* Trailing whitespace? */
533 if ('\0' == buf
[i
] && ' ' == buf
[i
- 1])
534 if ( ! man_pwarn(m
, ln
, i
- 1, WTSPACE
))
538 * Remove prior ELINE macro, as a macro is clobbering it by
539 * being invoked without prior text. Note that NSCOPED macros
540 * do not close out ELINE macros, as they print no text.
543 if (m
->flags
& MAN_ELINE
&&
544 ! (MAN_NSCOPED
& man_macros
[c
].flags
)) {
546 assert(NULL
== n
->child
);
547 assert(0 == n
->nchild
);
548 if ( ! man_nwarn(m
, n
, WLNSCOPE
))
551 /* FIXME: when called as in:
561 assert(n
!= n
->parent
->child
);
562 assert(n
== n
->prev
->next
);
563 n
->prev
->next
= NULL
;
565 m
->next
= MAN_NEXT_SIBLING
;
567 assert(n
== n
->parent
->child
);
568 n
->parent
->child
= NULL
;
570 m
->next
= MAN_NEXT_CHILD
;
574 m
->flags
&= ~MAN_ELINE
;
577 /* Begin recursive parse sequence. */
579 assert(man_macros
[c
].fp
);
581 if ( ! (*man_macros
[c
].fp
)(m
, c
, ln
, ppos
, &i
, buf
))
586 * We weren't in a block-line scope when entering the
587 * above-parsed macro, so return.
590 if ( ! (MAN_BLINE
& fl
)) {
591 m
->flags
&= ~MAN_ILINE
;
596 * If we're in a block scope, then allow this macro to slip by
597 * without closing scope around it.
600 if (MAN_ILINE
& m
->flags
) {
601 m
->flags
&= ~MAN_ILINE
;
606 * If we've opened a new next-line element scope, then return
607 * now, as the next line will close out the block scope.
610 if (MAN_ELINE
& m
->flags
)
613 /* Close out the block scope opened in the prior line. */
615 assert(MAN_BLINE
& m
->flags
);
616 m
->flags
&= ~MAN_BLINE
;
618 if ( ! man_unscope(m
, m
->last
->parent
))
620 return(man_body_alloc(m
, ln
, 0, m
->last
->tok
));
622 err
: /* Error out. */
624 m
->flags
|= MAN_HALT
;
630 man_verr(struct man
*man
, int ln
, int pos
, const char *fmt
, ...)
635 if (NULL
== man
->cb
.man_err
)
639 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
641 return((*man
->cb
.man_err
)(man
->data
, ln
, pos
, buf
));
646 man_vwarn(struct man
*man
, int ln
, int pos
, const char *fmt
, ...)
651 if (NULL
== man
->cb
.man_warn
)
655 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
657 return((*man
->cb
.man_warn
)(man
->data
, ln
, pos
, buf
));
662 man_err(struct man
*m
, int line
, int pos
, int iserr
, enum merr type
)
666 p
= __man_merrnames
[(int)type
];
670 return(man_verr(m
, line
, pos
, p
));
672 return(man_vwarn(m
, line
, pos
, p
));