]>
git.cameronkatri.com Git - mandoc.git/blob - man.c
1 /* $Id: man.c,v 1.51 2010/03/22 14:03: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 */
55 const char *const __man_macronames
[MAN_MAX
] = {
56 "br", "TH", "SH", "SS",
57 "TP", "LP", "PP", "P",
58 "IP", "HP", "SM", "SB",
59 "BI", "IB", "BR", "RB",
61 "RI", "na", "i", "sp",
62 "nf", "fi", "r", "RE",
63 "RS", "DT", "UC", "PD"
66 const char * const *man_macronames
= __man_macronames
;
68 static struct man_node
*man_node_alloc(int, int,
70 static int man_node_append(struct man
*,
72 static int man_ptext(struct man
*, int, char *);
73 static int man_pmacro(struct man
*, int, char *);
74 static void man_free1(struct man
*);
75 static void man_alloc1(struct man
*);
76 static int pstring(struct man
*, int, int,
77 const char *, size_t);
78 static int macrowarn(struct man
*, int, const char *);
81 const struct man_node
*
82 man_node(const struct man
*m
)
85 return(MAN_HALT
& m
->flags
? NULL
: m
->first
);
89 const struct man_meta
*
90 man_meta(const struct man
*m
)
93 return(MAN_HALT
& m
->flags
? NULL
: &m
->meta
);
98 man_reset(struct man
*man
)
107 man_free(struct man
*man
)
116 man_alloc(void *data
, int pflags
, const struct man_cb
*cb
)
120 p
= mandoc_calloc(1, sizeof(struct man
));
123 memcpy(&p
->cb
, cb
, sizeof(struct man_cb
));
135 man_endparse(struct man
*m
)
138 if (MAN_HALT
& m
->flags
)
140 else if (man_macroend(m
))
142 m
->flags
|= MAN_HALT
;
148 man_parseln(struct man
*m
, int ln
, char *buf
)
152 man_pmacro(m
, ln
, buf
) :
153 man_ptext(m
, ln
, buf
));
158 man_free1(struct man
*man
)
162 man_node_freelist(man
->first
);
164 free(man
->meta
.title
);
165 if (man
->meta
.source
)
166 free(man
->meta
.source
);
173 man_alloc1(struct man
*m
)
176 memset(&m
->meta
, 0, sizeof(struct man_meta
));
178 m
->last
= mandoc_calloc(1, sizeof(struct man_node
));
180 m
->last
->type
= MAN_ROOT
;
181 m
->next
= MAN_NEXT_CHILD
;
186 man_node_append(struct man
*man
, struct man_node
*p
)
191 assert(MAN_ROOT
!= p
->type
);
194 case (MAN_NEXT_SIBLING
):
197 p
->parent
= man
->last
->parent
;
199 case (MAN_NEXT_CHILD
):
200 man
->last
->child
= p
;
201 p
->parent
= man
->last
;
210 if ( ! man_valid_pre(man
, p
))
215 assert(MAN_BLOCK
== p
->parent
->type
);
219 assert(MAN_BLOCK
== p
->parent
->type
);
230 if ( ! man_valid_post(man
))
232 if ( ! man_action_post(man
))
243 static struct man_node
*
244 man_node_alloc(int line
, int pos
, enum man_type type
, int tok
)
248 p
= mandoc_calloc(1, sizeof(struct man_node
));
258 man_elem_alloc(struct man
*m
, int line
, int pos
, int tok
)
262 p
= man_node_alloc(line
, pos
, MAN_ELEM
, tok
);
263 if ( ! man_node_append(m
, p
))
265 m
->next
= MAN_NEXT_CHILD
;
271 man_head_alloc(struct man
*m
, int line
, int pos
, int tok
)
275 p
= man_node_alloc(line
, pos
, MAN_HEAD
, tok
);
276 if ( ! man_node_append(m
, p
))
278 m
->next
= MAN_NEXT_CHILD
;
284 man_body_alloc(struct man
*m
, int line
, int pos
, int tok
)
288 p
= man_node_alloc(line
, pos
, MAN_BODY
, tok
);
289 if ( ! man_node_append(m
, p
))
291 m
->next
= MAN_NEXT_CHILD
;
297 man_block_alloc(struct man
*m
, int line
, int pos
, int tok
)
301 p
= man_node_alloc(line
, pos
, MAN_BLOCK
, tok
);
302 if ( ! man_node_append(m
, p
))
304 m
->next
= MAN_NEXT_CHILD
;
310 pstring(struct man
*m
, int line
, int pos
,
311 const char *p
, size_t len
)
316 n
= man_node_alloc(line
, pos
, MAN_TEXT
, -1);
317 n
->string
= mandoc_malloc(len
+ 1);
318 sv
= strlcpy(n
->string
, p
, len
+ 1);
320 /* Prohibit truncation. */
321 assert(sv
< len
+ 1);
323 if ( ! man_node_append(m
, n
))
325 m
->next
= MAN_NEXT_SIBLING
;
331 man_word_alloc(struct man
*m
, int line
, int pos
, const char *word
)
334 return(pstring(m
, line
, pos
, word
, strlen(word
)));
339 man_node_free(struct man_node
*p
)
351 man_node_freelist(struct man_node
*p
)
356 man_node_freelist(p
->child
);
357 assert(0 == p
->nchild
);
361 man_node_freelist(n
);
366 man_ptext(struct man
*m
, int line
, char *buf
)
371 /* Literal free-form text whitespace is preserved. */
373 if (MAN_LITERAL
& m
->flags
) {
374 if ( ! man_word_alloc(m
, line
, 0, buf
))
379 /* First de-chunk and allocate words. */
381 for (i
= 0; ' ' == buf
[i
]; i
++)
382 /* Skip leading whitespace. */ ;
384 if ('\0' == buf
[i
]) {
385 /* Trailing whitespace? */
386 if (i
&& ' ' == buf
[i
- 1])
387 if ( ! man_pwarn(m
, line
, i
- 1, WTSPACE
))
389 if ( ! pstring(m
, line
, 0, &buf
[i
], 0))
394 for (j
= i
; buf
[i
]; i
++) {
398 /* Escaped whitespace. */
399 if (i
&& ' ' == buf
[i
] && '\\' == buf
[i
- 1])
405 if ( ! pstring(m
, line
, j
, &buf
[j
], (size_t)(i
- j
)))
408 /* Trailing whitespace? Check at overwritten byte. */
410 if (' ' == sv
&& '\0' == buf
[i
])
411 if ( ! man_pwarn(m
, line
, i
- 1, WTSPACE
))
414 for ( ; ' ' == buf
[i
]; i
++)
415 /* Skip trailing whitespace. */ ;
419 /* Trailing whitespace? */
421 if (' ' == buf
[i
- 1] && '\0' == buf
[i
])
422 if ( ! man_pwarn(m
, line
, i
- 1, WTSPACE
))
429 if (j
!= i
&& ! pstring(m
, line
, j
, &buf
[j
], (size_t)(i
- j
)))
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
))
446 if ( ! (MAN_BLINE
& m
->flags
))
448 m
->flags
&= ~MAN_BLINE
;
450 if ( ! man_unscope(m
, m
->last
->parent
))
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,
461 "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
)
471 int i
, j
, c
, ppos
, fl
;
475 /* Comments and empties are quickly ignored. */
486 while (buf
[i
] && ' ' == buf
[i
])
494 /* Copy the first word into a nil-terminated buffer. */
496 for (j
= 0; j
< 4; j
++, i
++) {
497 if ('\0' == (mac
[j
] = buf
[i
]))
499 else if (' ' == buf
[i
])
502 /* Check for invalid characters. */
504 if (isgraph((u_char
)buf
[i
]))
506 return(man_perr(m
, ln
, i
, WNPRINT
));
511 if (j
== 4 || j
< 1) {
512 if ( ! (MAN_IGN_MACRO
& m
->pflags
)) {
513 (void)man_perr(m
, ln
, ppos
, WMACROFORM
);
516 if ( ! man_pwarn(m
, ln
, ppos
, WMACROFORM
))
521 if (MAN_MAX
== (c
= man_hash_find(mac
))) {
522 if ( ! macrowarn(m
, ln
, mac
))
527 /* The macro is sane. Jump to the next word. */
529 while (buf
[i
] && ' ' == buf
[i
])
532 /* Trailing whitespace? */
534 if ('\0' == buf
[i
] && ' ' == buf
[i
- 1])
535 if ( ! man_pwarn(m
, ln
, i
- 1, WTSPACE
))
539 * Remove prior ELINE macro, as it's being clobbering by a new
540 * macro. Note that NSCOPED macros do not close out ELINE
541 * macros---they don't print text---so we let those slip by.
544 if ( ! (MAN_NSCOPED
& man_macros
[c
].flags
) &&
545 m
->flags
& MAN_ELINE
) {
546 assert(MAN_TEXT
!= m
->last
->type
);
549 * This occurs in the following construction:
555 * Flat-out disallow this madness.
557 if (MAN_NSCOPED
& man_macros
[m
->last
->tok
].flags
)
558 return(man_perr(m
, ln
, ppos
, WLNSCOPE
));
563 assert(NULL
== n
->child
);
564 assert(0 == n
->nchild
);
566 if ( ! man_nwarn(m
, n
, WLNSCOPE
))
569 man_node_unlink(m
, n
);
571 m
->flags
&= ~MAN_ELINE
;
574 /* Begin recursive parse sequence. */
576 assert(man_macros
[c
].fp
);
578 if ( ! (*man_macros
[c
].fp
)(m
, c
, ln
, ppos
, &i
, buf
))
583 * We weren't in a block-line scope when entering the
584 * above-parsed macro, so return.
587 if ( ! (MAN_BLINE
& fl
)) {
588 m
->flags
&= ~MAN_ILINE
;
593 * If we're in a block scope, then allow this macro to slip by
594 * without closing scope around it.
597 if (MAN_ILINE
& m
->flags
) {
598 m
->flags
&= ~MAN_ILINE
;
603 * If we've opened a new next-line element scope, then return
604 * now, as the next line will close out the block scope.
607 if (MAN_ELINE
& m
->flags
)
610 /* Close out the block scope opened in the prior line. */
612 assert(MAN_BLINE
& m
->flags
);
613 m
->flags
&= ~MAN_BLINE
;
615 if ( ! man_unscope(m
, m
->last
->parent
))
617 return(man_body_alloc(m
, ln
, 0, m
->last
->tok
));
619 err
: /* Error out. */
621 m
->flags
|= MAN_HALT
;
627 man_verr(struct man
*man
, int ln
, int pos
, const char *fmt
, ...)
632 if (NULL
== man
->cb
.man_err
)
636 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
638 return((*man
->cb
.man_err
)(man
->data
, ln
, pos
, buf
));
643 man_vwarn(struct man
*man
, int ln
, int pos
, const char *fmt
, ...)
648 if (NULL
== man
->cb
.man_warn
)
652 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
654 return((*man
->cb
.man_warn
)(man
->data
, ln
, pos
, buf
));
659 man_err(struct man
*m
, int line
, int pos
, int iserr
, enum merr type
)
663 p
= __man_merrnames
[(int)type
];
667 return(man_verr(m
, line
, pos
, p
));
669 return(man_vwarn(m
, line
, pos
, p
));
674 man_node_unlink(struct man
*m
, struct man_node
*n
)
678 n
->prev
->next
= n
->next
;
680 assert(NULL
== n
->next
);
682 m
->next
= MAN_NEXT_SIBLING
;
685 n
->parent
->child
= n
->next
;
687 assert(NULL
== n
->next
);
689 m
->next
= MAN_NEXT_CHILD
;
694 n
->next
->prev
= n
->prev
;