]>
git.cameronkatri.com Git - mandoc.git/blob - man.c
1 /* $Id: man.c,v 1.71 2010/05/16 00:04:46 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 date format", /* WDATE */
36 "scope of prior line violated", /* WLNSCOPE */
37 "over-zealous prior line scope violation", /* WLNSCOPE2 */
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 */
52 "document title should be uppercase", /* WTITLECASE */
53 "deprecated comment style", /* WBADCOMMENT */
56 const char *const __man_macronames
[MAN_MAX
] = {
57 "br", "TH", "SH", "SS",
58 "TP", "LP", "PP", "P",
59 "IP", "HP", "SM", "SB",
60 "BI", "IB", "BR", "RB",
62 "RI", "na", "i", "sp",
63 "nf", "fi", "r", "RE",
64 "RS", "DT", "UC", "PD",
68 const char * const *man_macronames
= __man_macronames
;
70 static struct man_node
*man_node_alloc(int, int,
71 enum man_type
, enum mant
);
72 static int man_node_append(struct man
*,
74 static void man_node_free(struct man_node
*);
75 static void man_node_unlink(struct man
*,
77 static int man_ptext(struct man
*, int, char *);
78 static int man_pmacro(struct man
*, int, char *);
79 static void man_free1(struct man
*);
80 static void man_alloc1(struct man
*);
81 static int macrowarn(struct man
*, int, const char *);
84 const struct man_node
*
85 man_node(const struct man
*m
)
88 return(MAN_HALT
& m
->flags
? NULL
: m
->first
);
92 const struct man_meta
*
93 man_meta(const struct man
*m
)
96 return(MAN_HALT
& m
->flags
? NULL
: &m
->meta
);
101 man_reset(struct man
*man
)
110 man_free(struct man
*man
)
119 man_alloc(void *data
, int pflags
, const struct man_cb
*cb
)
123 p
= mandoc_calloc(1, sizeof(struct man
));
126 memcpy(&p
->cb
, cb
, sizeof(struct man_cb
));
138 man_endparse(struct man
*m
)
141 if (MAN_HALT
& m
->flags
)
143 else if (man_macroend(m
))
145 m
->flags
|= MAN_HALT
;
151 man_parseln(struct man
*m
, int ln
, char *buf
)
154 return(('.' == *buf
|| '\'' == *buf
) ?
155 man_pmacro(m
, ln
, buf
) :
156 man_ptext(m
, ln
, buf
));
161 man_free1(struct man
*man
)
165 man_node_delete(man
, man
->first
);
167 free(man
->meta
.title
);
168 if (man
->meta
.source
)
169 free(man
->meta
.source
);
173 free(man
->meta
.msec
);
178 man_alloc1(struct man
*m
)
181 memset(&m
->meta
, 0, sizeof(struct man_meta
));
183 m
->last
= mandoc_calloc(1, sizeof(struct man_node
));
185 m
->last
->type
= MAN_ROOT
;
186 m
->last
->tok
= MAN_MAX
;
187 m
->next
= MAN_NEXT_CHILD
;
192 man_node_append(struct man
*man
, struct man_node
*p
)
197 assert(MAN_ROOT
!= p
->type
);
200 case (MAN_NEXT_SIBLING
):
203 p
->parent
= man
->last
->parent
;
205 case (MAN_NEXT_CHILD
):
206 man
->last
->child
= p
;
207 p
->parent
= man
->last
;
217 if ( ! man_valid_pre(man
, p
))
222 assert(MAN_BLOCK
== p
->parent
->type
);
226 assert(MAN_BLOCK
== p
->parent
->type
);
237 if ( ! man_valid_post(man
))
239 if ( ! man_action_post(man
))
250 static struct man_node
*
251 man_node_alloc(int line
, int pos
, enum man_type type
, enum mant tok
)
255 p
= mandoc_calloc(1, sizeof(struct man_node
));
265 man_elem_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
269 p
= man_node_alloc(line
, pos
, MAN_ELEM
, tok
);
270 if ( ! man_node_append(m
, p
))
272 m
->next
= MAN_NEXT_CHILD
;
278 man_head_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
282 p
= man_node_alloc(line
, pos
, MAN_HEAD
, tok
);
283 if ( ! man_node_append(m
, p
))
285 m
->next
= MAN_NEXT_CHILD
;
291 man_body_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
295 p
= man_node_alloc(line
, pos
, MAN_BODY
, tok
);
296 if ( ! man_node_append(m
, p
))
298 m
->next
= MAN_NEXT_CHILD
;
304 man_block_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
308 p
= man_node_alloc(line
, pos
, MAN_BLOCK
, tok
);
309 if ( ! man_node_append(m
, p
))
311 m
->next
= MAN_NEXT_CHILD
;
317 man_word_alloc(struct man
*m
, int line
, int pos
, const char *word
)
324 n
= man_node_alloc(line
, pos
, MAN_TEXT
, MAN_MAX
);
325 n
->string
= mandoc_malloc(len
+ 1);
326 sv
= strlcpy(n
->string
, word
, len
+ 1);
328 /* Prohibit truncation. */
329 assert(sv
< len
+ 1);
331 if ( ! man_node_append(m
, n
))
334 m
->next
= MAN_NEXT_SIBLING
;
340 * Free all of the resources held by a node. This does NOT unlink a
341 * node from its context; for that, see man_node_unlink().
344 man_node_free(struct man_node
*p
)
354 man_node_delete(struct man
*m
, struct man_node
*p
)
358 man_node_delete(m
, p
->child
);
360 man_node_unlink(m
, p
);
366 man_ptext(struct man
*m
, int line
, char *buf
)
370 /* Ignore bogus comments. */
372 if ('\\' == buf
[0] && '.' == buf
[1] && '\"' == buf
[2])
373 return(man_pwarn(m
, line
, 0, WBADCOMMENT
));
375 /* Literal free-form text whitespace is preserved. */
377 if (MAN_LITERAL
& m
->flags
) {
378 if ( ! man_word_alloc(m
, line
, 0, buf
))
383 /* Pump blank lines directly into the backend. */
385 for (i
= 0; ' ' == buf
[i
]; i
++)
386 /* Skip leading whitespace. */ ;
388 if ('\0' == buf
[i
]) {
389 /* Allocate a blank entry. */
390 if ( ! man_word_alloc(m
, line
, 0, ""))
396 * Warn if the last un-escaped character is whitespace. Then
397 * strip away the remaining spaces (tabs stay!).
400 i
= (int)strlen(buf
);
403 if (' ' == buf
[i
- 1] || '\t' == buf
[i
- 1]) {
404 if (i
> 1 && '\\' != buf
[i
- 2])
405 if ( ! man_pwarn(m
, line
, i
- 1, WTSPACE
))
408 for (--i
; i
&& ' ' == buf
[i
]; i
--)
409 /* Spin back to non-space. */ ;
411 /* Jump ahead of escaped whitespace. */
412 i
+= '\\' == buf
[i
] ? 2 : 1;
417 if ( ! man_word_alloc(m
, line
, 0, buf
))
421 * End-of-sentence check. If the last character is an unescaped
422 * EOS character, then flag the node as being the end of a
423 * sentence. The front-end will know how to interpret this.
426 /* FIXME: chain of close delims. */
430 if (mandoc_eos(buf
, (size_t)i
))
431 m
->last
->flags
|= MAN_EOS
;
435 * Co-ordinate what happens with having a next-line scope open:
436 * first close out the element scope (if applicable), then close
437 * out the block scope (also if applicable).
440 if (MAN_ELINE
& m
->flags
) {
441 m
->flags
&= ~MAN_ELINE
;
442 if ( ! man_unscope(m
, m
->last
->parent
, WERRMAX
))
446 if ( ! (MAN_BLINE
& m
->flags
))
448 m
->flags
&= ~MAN_BLINE
;
450 if ( ! man_unscope(m
, m
->last
->parent
, WERRMAX
))
452 return(man_body_alloc(m
, line
, 0, m
->last
->tok
));
457 macrowarn(struct man
*m
, int ln
, const char *buf
)
459 if ( ! (MAN_IGN_MACRO
& m
->pflags
))
460 return(man_verr(m
, ln
, 0, "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
)
475 /* Comments and empties are quickly ignored. */
483 * Skip whitespace between the control character and initial
484 * text. "Whitespace" is both spaces and tabs.
487 if (' ' == buf
[i
] || '\t' == buf
[i
]) {
489 while (buf
[i
] && (' ' == buf
[i
] || '\t' == buf
[i
]))
497 /* Copy the first word into a nil-terminated buffer. */
499 for (j
= 0; j
< 4; j
++, i
++) {
500 if ('\0' == (mac
[j
] = buf
[i
]))
502 else if (' ' == buf
[i
])
505 /* Check for invalid characters. */
507 if (isgraph((u_char
)buf
[i
]))
509 return(man_perr(m
, ln
, i
, WNPRINT
));
514 if (j
== 4 || j
< 1) {
515 if ( ! (MAN_IGN_MACRO
& m
->pflags
)) {
516 (void)man_perr(m
, ln
, ppos
, WMACROFORM
);
519 if ( ! man_pwarn(m
, ln
, ppos
, WMACROFORM
))
524 if (MAN_MAX
== (tok
= man_hash_find(mac
))) {
525 if ( ! macrowarn(m
, ln
, mac
))
530 /* The macro is sane. Jump to the next word. */
532 while (buf
[i
] && ' ' == buf
[i
])
536 * Trailing whitespace. Note that tabs are allowed to be passed
537 * into the parser as "text", so we only warn about spaces here.
540 if ('\0' == buf
[i
] && ' ' == buf
[i
- 1])
541 if ( ! man_pwarn(m
, ln
, i
- 1, WTSPACE
))
545 * Remove prior ELINE macro, as it's being clobbering by a new
546 * macro. Note that NSCOPED macros do not close out ELINE
547 * macros---they don't print text---so we let those slip by.
550 if ( ! (MAN_NSCOPED
& man_macros
[tok
].flags
) &&
551 m
->flags
& MAN_ELINE
) {
552 assert(MAN_TEXT
!= m
->last
->type
);
555 * This occurs in the following construction:
561 * Flat-out disallow this madness.
563 if (MAN_NSCOPED
& man_macros
[m
->last
->tok
].flags
)
564 return(man_perr(m
, ln
, ppos
, WLNSCOPE
));
569 assert(NULL
== n
->child
);
570 assert(0 == n
->nchild
);
572 if ( ! man_nwarn(m
, n
, WLNSCOPE
))
575 man_node_delete(m
, n
);
576 m
->flags
&= ~MAN_ELINE
;
580 * Save the fact that we're in the next-line for a block. In
581 * this way, embedded roff instructions can "remember" state
585 if (MAN_BLINE
& m
->flags
)
586 m
->flags
|= MAN_BPLINE
;
588 /* Call to handler... */
590 assert(man_macros
[tok
].fp
);
591 if ( ! (*man_macros
[tok
].fp
)(m
, tok
, ln
, ppos
, &i
, buf
))
596 * We weren't in a block-line scope when entering the
597 * above-parsed macro, so return.
600 if ( ! (MAN_BPLINE
& m
->flags
)) {
601 m
->flags
&= ~MAN_ILINE
;
604 m
->flags
&= ~MAN_BPLINE
;
607 * If we're in a block scope, then allow this macro to slip by
608 * without closing scope around it.
611 if (MAN_ILINE
& m
->flags
) {
612 m
->flags
&= ~MAN_ILINE
;
617 * If we've opened a new next-line element scope, then return
618 * now, as the next line will close out the block scope.
621 if (MAN_ELINE
& m
->flags
)
624 /* Close out the block scope opened in the prior line. */
626 assert(MAN_BLINE
& m
->flags
);
627 m
->flags
&= ~MAN_BLINE
;
629 if ( ! man_unscope(m
, m
->last
->parent
, WERRMAX
))
631 return(man_body_alloc(m
, ln
, 0, m
->last
->tok
));
633 err
: /* Error out. */
635 m
->flags
|= MAN_HALT
;
641 man_verr(struct man
*man
, int ln
, int pos
, const char *fmt
, ...)
646 if (NULL
== man
->cb
.man_err
)
650 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
652 return((*man
->cb
.man_err
)(man
->data
, ln
, pos
, buf
));
657 man_vwarn(struct man
*man
, int ln
, int pos
, const char *fmt
, ...)
662 if (NULL
== man
->cb
.man_warn
)
666 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
668 return((*man
->cb
.man_warn
)(man
->data
, ln
, pos
, buf
));
673 man_err(struct man
*m
, int line
, int pos
, int iserr
, enum merr type
)
677 p
= __man_merrnames
[(int)type
];
681 return(man_verr(m
, line
, pos
, p
));
683 return(man_vwarn(m
, line
, pos
, p
));
688 * Unlink a node from its context. If "m" is provided, the last parse
689 * point will also be adjusted accordingly.
692 man_node_unlink(struct man
*m
, struct man_node
*n
)
695 /* Adjust siblings. */
698 n
->prev
->next
= n
->next
;
700 n
->next
->prev
= n
->prev
;
706 if (n
->parent
->child
== n
)
707 n
->parent
->child
= n
->prev
? n
->prev
: n
->next
;
710 /* Adjust parse point, if applicable. */
712 if (m
&& m
->last
== n
) {
713 /*XXX: this can occur when bailing from validation. */
714 /*assert(NULL == n->next);*/
717 m
->next
= MAN_NEXT_SIBLING
;
720 m
->next
= MAN_NEXT_CHILD
;
724 if (m
&& m
->first
== n
)