]>
git.cameronkatri.com Git - mandoc.git/blob - man.c
1 /* $Id: man.c,v 1.66 2010/05/12 17:08:03 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 */
56 "deprecated comment style", /* WBADCOMMENT */
59 const char *const __man_macronames
[MAN_MAX
] = {
60 "br", "TH", "SH", "SS",
61 "TP", "LP", "PP", "P",
62 "IP", "HP", "SM", "SB",
63 "BI", "IB", "BR", "RB",
65 "RI", "na", "i", "sp",
66 "nf", "fi", "r", "RE",
67 "RS", "DT", "UC", "PD",
68 "Sp", "Vb", "Ve", "de",
69 "dei", "am", "ami", "ig",
73 const char * const *man_macronames
= __man_macronames
;
75 static struct man_node
*man_node_alloc(int, int,
76 enum man_type
, enum mant
);
77 static int man_node_append(struct man
*,
79 static void man_node_free(struct man_node
*);
80 static void man_node_unlink(struct man
*,
82 static int man_ptext(struct man
*, int, char *);
83 static int man_pmacro(struct man
*, int, char *);
84 static void man_free1(struct man
*);
85 static void man_alloc1(struct man
*);
86 static int macrowarn(struct man
*, int, const char *);
89 const struct man_node
*
90 man_node(const struct man
*m
)
93 return(MAN_HALT
& m
->flags
? NULL
: m
->first
);
97 const struct man_meta
*
98 man_meta(const struct man
*m
)
101 return(MAN_HALT
& m
->flags
? NULL
: &m
->meta
);
106 man_reset(struct man
*man
)
115 man_free(struct man
*man
)
124 man_alloc(void *data
, int pflags
, const struct man_cb
*cb
)
128 p
= mandoc_calloc(1, sizeof(struct man
));
131 memcpy(&p
->cb
, cb
, sizeof(struct man_cb
));
143 man_endparse(struct man
*m
)
146 if (MAN_HALT
& m
->flags
)
148 else if (man_macroend(m
))
150 m
->flags
|= MAN_HALT
;
156 man_parseln(struct man
*m
, int ln
, char *buf
)
159 return('.' == *buf
|| '\'' == *buf
?
160 man_pmacro(m
, ln
, buf
) :
161 man_ptext(m
, ln
, buf
));
166 man_free1(struct man
*man
)
170 man_node_delete(man
, man
->first
);
172 free(man
->meta
.title
);
173 if (man
->meta
.source
)
174 free(man
->meta
.source
);
181 man_alloc1(struct man
*m
)
184 memset(&m
->meta
, 0, sizeof(struct man_meta
));
186 m
->last
= mandoc_calloc(1, sizeof(struct man_node
));
188 m
->last
->type
= MAN_ROOT
;
189 m
->last
->tok
= MAN_MAX
;
190 m
->next
= MAN_NEXT_CHILD
;
195 man_node_append(struct man
*man
, struct man_node
*p
)
200 assert(MAN_ROOT
!= p
->type
);
203 case (MAN_NEXT_SIBLING
):
206 p
->parent
= man
->last
->parent
;
208 case (MAN_NEXT_CHILD
):
209 man
->last
->child
= p
;
210 p
->parent
= man
->last
;
220 if ( ! man_valid_pre(man
, p
))
225 assert(MAN_BLOCK
== p
->parent
->type
);
229 assert(MAN_BLOCK
== p
->parent
->type
);
240 if ( ! man_valid_post(man
))
242 if ( ! man_action_post(man
))
253 static struct man_node
*
254 man_node_alloc(int line
, int pos
, enum man_type type
, enum mant tok
)
258 p
= mandoc_calloc(1, sizeof(struct man_node
));
268 man_elem_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
272 p
= man_node_alloc(line
, pos
, MAN_ELEM
, tok
);
273 if ( ! man_node_append(m
, p
))
275 m
->next
= MAN_NEXT_CHILD
;
281 man_head_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
285 p
= man_node_alloc(line
, pos
, MAN_HEAD
, tok
);
286 if ( ! man_node_append(m
, p
))
288 m
->next
= MAN_NEXT_CHILD
;
294 man_body_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
298 p
= man_node_alloc(line
, pos
, MAN_BODY
, tok
);
299 if ( ! man_node_append(m
, p
))
301 m
->next
= MAN_NEXT_CHILD
;
307 man_block_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
311 p
= man_node_alloc(line
, pos
, MAN_BLOCK
, tok
);
312 if ( ! man_node_append(m
, p
))
314 m
->next
= MAN_NEXT_CHILD
;
320 man_word_alloc(struct man
*m
, int line
, int pos
, const char *word
)
327 n
= man_node_alloc(line
, pos
, MAN_TEXT
, MAN_MAX
);
328 n
->string
= mandoc_malloc(len
+ 1);
329 sv
= strlcpy(n
->string
, word
, len
+ 1);
331 /* Prohibit truncation. */
332 assert(sv
< len
+ 1);
334 if ( ! man_node_append(m
, n
))
337 m
->next
= MAN_NEXT_SIBLING
;
343 * Free all of the resources held by a node. This does NOT unlink a
344 * node from its context; for that, see man_node_unlink().
347 man_node_free(struct man_node
*p
)
357 man_node_delete(struct man
*m
, struct man_node
*p
)
361 man_node_delete(m
, p
->child
);
363 man_node_unlink(m
, p
);
369 man_ptext(struct man
*m
, int line
, char *buf
)
373 /* Ignore bogus comments. */
375 if ('\\' == buf
[0] && '.' == buf
[1] && '\"' == buf
[2])
376 return(man_pwarn(m
, line
, 0, WBADCOMMENT
));
378 /* Literal free-form text whitespace is preserved. */
380 if (MAN_LITERAL
& m
->flags
) {
381 if ( ! man_word_alloc(m
, line
, 0, buf
))
386 /* Pump blank lines directly into the backend. */
388 for (i
= 0; ' ' == buf
[i
]; i
++)
389 /* Skip leading whitespace. */ ;
391 if ('\0' == buf
[i
]) {
392 /* Allocate a blank entry. */
393 if ( ! man_word_alloc(m
, line
, 0, ""))
399 * Warn if the last un-escaped character is whitespace. Then
400 * strip away the remaining spaces (tabs stay!).
403 i
= (int)strlen(buf
);
406 if (' ' == buf
[i
- 1] || '\t' == buf
[i
- 1]) {
407 if (i
> 1 && '\\' != buf
[i
- 2])
408 if ( ! man_pwarn(m
, line
, i
- 1, WTSPACE
))
411 for (--i
; i
&& ' ' == buf
[i
]; i
--)
412 /* Spin back to non-space. */ ;
414 /* Jump ahead of escaped whitespace. */
415 i
+= '\\' == buf
[i
] ? 2 : 1;
420 if ( ! man_word_alloc(m
, line
, 0, buf
))
424 * End-of-sentence check. If the last character is an unescaped
425 * EOS character, then flag the node as being the end of a
426 * sentence. The front-end will know how to interpret this.
431 if (mandoc_eos(buf
, (size_t)i
))
432 m
->last
->flags
|= MAN_EOS
;
436 * Co-ordinate what happens with having a next-line scope open:
437 * first close out the element scope (if applicable), then close
438 * out the block scope (also if applicable).
441 if (MAN_ELINE
& m
->flags
) {
442 m
->flags
&= ~MAN_ELINE
;
443 if ( ! man_unscope(m
, m
->last
->parent
, WERRMAX
))
447 if ( ! (MAN_BLINE
& m
->flags
))
449 m
->flags
&= ~MAN_BLINE
;
451 if ( ! man_unscope(m
, m
->last
->parent
, WERRMAX
))
453 return(man_body_alloc(m
, line
, 0, m
->last
->tok
));
458 macrowarn(struct man
*m
, int ln
, const char *buf
)
460 if ( ! (MAN_IGN_MACRO
& m
->pflags
))
461 return(man_verr(m
, ln
, 0, "unknown macro: %s%s",
462 buf
, strlen(buf
) > 3 ? "..." : ""));
463 return(man_vwarn(m
, ln
, 0, "unknown macro: %s%s",
464 buf
, strlen(buf
) > 3 ? "..." : ""));
469 man_pmacro(struct man
*m
, int ln
, char *buf
)
476 /* Comments and empties are quickly ignored. */
484 * Skip whitespace between the control character and initial
485 * text. "Whitespace" is both spaces and tabs.
488 if (' ' == buf
[i
] || '\t' == buf
[i
]) {
490 while (buf
[i
] && (' ' == buf
[i
] || '\t' == buf
[i
]))
498 /* Copy the first word into a nil-terminated buffer. */
500 for (j
= 0; j
< 4; j
++, i
++) {
501 if ('\0' == (mac
[j
] = buf
[i
]))
503 else if (' ' == buf
[i
])
506 /* Check for invalid characters. */
508 if (isgraph((u_char
)buf
[i
]))
510 return(man_perr(m
, ln
, i
, WNPRINT
));
515 if (j
== 4 || j
< 1) {
516 if ( ! (MAN_IGN_MACRO
& m
->pflags
)) {
517 (void)man_perr(m
, ln
, ppos
, WMACROFORM
);
520 if ( ! man_pwarn(m
, ln
, ppos
, WMACROFORM
))
525 if (MAN_MAX
== (tok
= man_hash_find(mac
))) {
526 if ( ! macrowarn(m
, ln
, mac
))
531 /* The macro is sane. Jump to the next word. */
533 while (buf
[i
] && ' ' == buf
[i
])
537 * Trailing whitespace. Note that tabs are allowed to be passed
538 * into the parser as "text", so we only warn about spaces here.
541 if ('\0' == buf
[i
] && ' ' == buf
[i
- 1])
542 if ( ! man_pwarn(m
, ln
, i
- 1, WTSPACE
))
546 * Remove prior ELINE macro, as it's being clobbering by a new
547 * macro. Note that NSCOPED macros do not close out ELINE
548 * macros---they don't print text---so we let those slip by.
549 * NOTE: we don't allow roff blocks (NOCLOSE) to be embedded
550 * here because that would stipulate blocks as children of
554 if ( ! (MAN_NSCOPED
& man_macros
[tok
].flags
) &&
555 m
->flags
& MAN_ELINE
) {
556 assert(MAN_TEXT
!= m
->last
->type
);
559 * This occurs in the following construction:
565 * Flat-out disallow this madness.
567 if (MAN_NSCOPED
& man_macros
[m
->last
->tok
].flags
)
568 return(man_perr(m
, ln
, ppos
, WLNSCOPE
));
573 assert(NULL
== n
->child
);
574 assert(0 == n
->nchild
);
576 if ( ! man_nwarn(m
, n
, WLNSCOPE
))
579 man_node_delete(m
, n
);
580 m
->flags
&= ~MAN_ELINE
;
584 * Save the fact that we're in the next-line for a block. In
585 * this way, embedded roff instructions can "remember" state
589 if (MAN_BLINE
& m
->flags
)
590 m
->flags
|= MAN_BPLINE
;
592 /* Call to handler... */
594 assert(man_macros
[tok
].fp
);
595 if ( ! (*man_macros
[tok
].fp
)(m
, tok
, ln
, ppos
, &i
, buf
))
600 * We weren't in a block-line scope when entering the
601 * above-parsed macro, so return.
604 if ( ! (MAN_BPLINE
& m
->flags
)) {
605 m
->flags
&= ~MAN_ILINE
;
608 m
->flags
&= ~MAN_BPLINE
;
611 * If we're in a block scope, then allow this macro to slip by
612 * without closing scope around it.
615 if (MAN_ILINE
& m
->flags
) {
616 m
->flags
&= ~MAN_ILINE
;
621 * If we've opened a new next-line element scope, then return
622 * now, as the next line will close out the block scope.
625 if (MAN_ELINE
& m
->flags
)
628 /* Close out the block scope opened in the prior line. */
630 assert(MAN_BLINE
& m
->flags
);
631 m
->flags
&= ~MAN_BLINE
;
633 if ( ! man_unscope(m
, m
->last
->parent
, WERRMAX
))
635 return(man_body_alloc(m
, ln
, 0, m
->last
->tok
));
637 err
: /* Error out. */
639 m
->flags
|= MAN_HALT
;
645 man_verr(struct man
*man
, int ln
, int pos
, const char *fmt
, ...)
650 if (NULL
== man
->cb
.man_err
)
654 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
656 return((*man
->cb
.man_err
)(man
->data
, ln
, pos
, buf
));
661 man_vwarn(struct man
*man
, int ln
, int pos
, const char *fmt
, ...)
666 if (NULL
== man
->cb
.man_warn
)
670 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
672 return((*man
->cb
.man_warn
)(man
->data
, ln
, pos
, buf
));
677 man_err(struct man
*m
, int line
, int pos
, int iserr
, enum merr type
)
681 p
= __man_merrnames
[(int)type
];
685 return(man_verr(m
, line
, pos
, p
));
687 return(man_vwarn(m
, line
, pos
, p
));
692 * Unlink a node from its context. If "m" is provided, the last parse
693 * point will also be adjusted accordingly.
696 man_node_unlink(struct man
*m
, struct man_node
*n
)
699 /* Adjust siblings. */
702 n
->prev
->next
= n
->next
;
704 n
->next
->prev
= n
->prev
;
710 if (n
->parent
->child
== n
)
711 n
->parent
->child
= n
->prev
? n
->prev
: n
->next
;
714 /* Adjust parse point, if applicable. */
716 if (m
&& m
->last
== n
) {
717 /*XXX: this can occur when bailing from validation. */
718 /*assert(NULL == n->next);*/
721 m
->next
= MAN_NEXT_SIBLING
;
724 m
->next
= MAN_NEXT_CHILD
;
728 if (m
&& m
->first
== n
)