]>
git.cameronkatri.com Git - mandoc.git/blob - man.c
1 /* $Id: man.c,v 1.44 2009/10/27 08:26:12 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.
17 #include <sys/types.h>
28 const char *const __man_merrnames
[WERRMAX
] = {
29 "invalid character", /* WNPRINT */
30 "system: malloc error", /* WNMEM */
31 "invalid manual section", /* WMSEC */
32 "invalid date format", /* WDATE */
33 "scope of prior line violated", /* WLNSCOPE */
34 "trailing whitespace", /* WTSPACE */
35 "unterminated quoted parameter", /* WTQUOTE */
36 "document has no body", /* WNODATA */
37 "document has no title/section", /* WNOTITLE */
38 "invalid escape sequence", /* WESCAPE */
39 "invalid number format", /* WNUMFMT */
40 "expected block head arguments", /* WHEADARGS */
41 "expected block body arguments", /* WBODYARGS */
42 "expected empty block head", /* WNHEADARGS */
43 "ill-formed macro", /* WMACROFORM */
44 "scope open on exit", /* WEXITSCOPE */
45 "no scope context", /* WNOSCOPE */
46 "literal context already open", /* WOLITERAL */
47 "no literal context open" /* WNLITERAL */
50 const char *const __man_macronames
[MAN_MAX
] = {
51 "br", "TH", "SH", "SS",
52 "TP", "LP", "PP", "P",
53 "IP", "HP", "SM", "SB",
54 "BI", "IB", "BR", "RB",
56 "RI", "na", "i", "sp",
57 "nf", "fi", "r", "RE",
58 "RS", "DT", "UC", "PD"
61 const char * const *man_macronames
= __man_macronames
;
63 static struct man_node
*man_node_alloc(int, int,
65 static int man_node_append(struct man
*,
67 static int man_ptext(struct man
*, int, char *);
68 static int man_pmacro(struct man
*, int, char *);
69 static void man_free1(struct man
*);
70 static int man_alloc1(struct man
*);
71 static int pstring(struct man
*, int, int,
72 const char *, size_t);
73 static int macrowarn(struct man
*, int, const char *);
76 extern size_t strlcpy(char *, const char *, size_t);
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
)
101 return(man_alloc1(man
));
106 man_free(struct man
*man
)
115 man_alloc(void *data
, int pflags
, const struct man_cb
*cb
)
119 if (NULL
== (p
= calloc(1, sizeof(struct man
))))
122 if ( ! man_alloc1(p
)) {
131 (void)memcpy(&p
->cb
, cb
, sizeof(struct man_cb
));
137 man_endparse(struct man
*m
)
140 if (MAN_HALT
& m
->flags
)
142 else if (man_macroend(m
))
144 m
->flags
|= MAN_HALT
;
150 man_parseln(struct man
*m
, int ln
, char *buf
)
154 man_pmacro(m
, ln
, buf
) :
155 man_ptext(m
, ln
, buf
));
160 man_free1(struct man
*man
)
164 man_node_freelist(man
->first
);
166 free(man
->meta
.title
);
167 if (man
->meta
.source
)
168 free(man
->meta
.source
);
175 man_alloc1(struct man
*m
)
178 memset(&m
->meta
, 0, sizeof(struct man_meta
));
180 m
->last
= calloc(1, sizeof(struct man_node
));
184 m
->last
->type
= MAN_ROOT
;
185 m
->next
= MAN_NEXT_CHILD
;
191 man_node_append(struct man
*man
, struct man_node
*p
)
196 assert(MAN_ROOT
!= p
->type
);
199 case (MAN_NEXT_SIBLING
):
202 p
->parent
= man
->last
->parent
;
204 case (MAN_NEXT_CHILD
):
205 man
->last
->child
= p
;
206 p
->parent
= man
->last
;
215 if ( ! man_valid_pre(man
, p
))
220 assert(MAN_BLOCK
== p
->parent
->type
);
224 assert(MAN_BLOCK
== p
->parent
->type
);
235 if ( ! man_valid_post(man
))
237 if ( ! man_action_post(man
))
248 static struct man_node
*
249 man_node_alloc(int line
, int pos
, enum man_type type
, int tok
)
253 p
= calloc(1, sizeof(struct man_node
));
266 man_elem_alloc(struct man
*m
, int line
, int pos
, int tok
)
270 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
, int tok
)
285 p
= man_node_alloc(line
, pos
, MAN_HEAD
, tok
);
288 if ( ! man_node_append(m
, p
))
290 m
->next
= MAN_NEXT_CHILD
;
296 man_body_alloc(struct man
*m
, int line
, int pos
, int tok
)
300 p
= man_node_alloc(line
, pos
, MAN_BODY
, tok
);
303 if ( ! man_node_append(m
, p
))
305 m
->next
= MAN_NEXT_CHILD
;
311 man_block_alloc(struct man
*m
, int line
, int pos
, int tok
)
315 p
= man_node_alloc(line
, pos
, MAN_BLOCK
, tok
);
318 if ( ! man_node_append(m
, p
))
320 m
->next
= MAN_NEXT_CHILD
;
326 pstring(struct man
*m
, int line
, int pos
,
327 const char *p
, size_t len
)
332 n
= man_node_alloc(line
, pos
, MAN_TEXT
, -1);
336 n
->string
= malloc(len
+ 1);
337 if (NULL
== n
->string
) {
342 sv
= strlcpy(n
->string
, p
, len
+ 1);
344 /* Prohibit truncation. */
345 assert(sv
< len
+ 1);
347 if ( ! man_node_append(m
, n
))
349 m
->next
= MAN_NEXT_SIBLING
;
355 man_word_alloc(struct man
*m
, int line
, int pos
, const char *word
)
358 return(pstring(m
, line
, pos
, word
, strlen(word
)));
363 man_node_free(struct man_node
*p
)
375 man_node_freelist(struct man_node
*p
)
380 man_node_freelist(p
->child
);
381 assert(0 == p
->nchild
);
385 man_node_freelist(n
);
390 man_ptext(struct man
*m
, int line
, char *buf
)
394 /* Literal free-form text whitespace is preserved. */
396 if (MAN_LITERAL
& m
->flags
) {
397 if ( ! man_word_alloc(m
, line
, 0, buf
))
402 /* First de-chunk and allocate words. */
404 for (i
= 0; ' ' == buf
[i
]; i
++)
405 /* Skip leading whitespace. */ ;
407 if ( ! pstring(m
, line
, 0, &buf
[i
], 0))
412 for (j
= i
; buf
[i
]; i
++) {
416 /* Escaped whitespace. */
417 if (i
&& ' ' == buf
[i
] && '\\' == buf
[i
- 1])
421 if ( ! pstring(m
, line
, j
, &buf
[j
], (size_t)(i
- j
)))
424 for ( ; ' ' == buf
[i
]; i
++)
425 /* Skip trailing whitespace. */ ;
432 if (j
!= i
&& ! pstring(m
, line
, j
, &buf
[j
], (size_t)(i
- j
)))
438 * Co-ordinate what happens with having a next-line scope open:
439 * first close out the element scope (if applicable), then close
440 * out the block scope (also if applicable).
443 if (MAN_ELINE
& m
->flags
) {
444 m
->flags
&= ~MAN_ELINE
;
445 if ( ! man_unscope(m
, m
->last
->parent
))
449 if ( ! (MAN_BLINE
& m
->flags
))
451 m
->flags
&= ~MAN_BLINE
;
453 if ( ! man_unscope(m
, m
->last
->parent
))
455 return(man_body_alloc(m
, line
, 0, m
->last
->tok
));
460 macrowarn(struct man
*m
, int ln
, const char *buf
)
462 if ( ! (MAN_IGN_MACRO
& m
->pflags
))
463 return(man_verr(m
, ln
, 0,
464 "unknown macro: %s%s",
465 buf
, strlen(buf
) > 3 ? "..." : ""));
466 return(man_vwarn(m
, ln
, 0, "unknown macro: %s%s",
467 buf
, strlen(buf
) > 3 ? "..." : ""));
472 man_pmacro(struct man
*m
, int ln
, char *buf
)
474 int i
, j
, c
, ppos
, fl
;
478 /* Comments and empties are quickly ignored. */
489 while (buf
[i
] && ' ' == 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
== (c
= 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
])
535 /* Remove prior ELINE macro, if applicable. */
537 if (m
->flags
& MAN_ELINE
) {
539 assert(NULL
== n
->child
);
540 assert(0 == n
->nchild
);
541 if ( ! man_nwarn(m
, n
, WLNSCOPE
))
545 assert(n
!= n
->parent
->child
);
546 assert(n
== n
->prev
->next
);
547 n
->prev
->next
= NULL
;
549 m
->next
= MAN_NEXT_SIBLING
;
551 assert(n
== n
->parent
->child
);
552 n
->parent
->child
= NULL
;
554 m
->next
= MAN_NEXT_CHILD
;
558 m
->flags
&= ~MAN_ELINE
;
561 /* Begin recursive parse sequence. */
563 assert(man_macros
[c
].fp
);
565 if ( ! (*man_macros
[c
].fp
)(m
, c
, ln
, ppos
, &i
, buf
))
569 if ( ! (MAN_BLINE
& fl
))
573 * If we've opened a new next-line element scope, then return
574 * now, as the next line will close out the block scope.
577 if (MAN_ELINE
& m
->flags
)
580 /* Close out the block scope opened in the prior line. */
582 assert(MAN_BLINE
& m
->flags
);
583 m
->flags
&= ~MAN_BLINE
;
585 if ( ! man_unscope(m
, m
->last
->parent
))
587 return(man_body_alloc(m
, ln
, 0, m
->last
->tok
));
589 err
: /* Error out. */
591 m
->flags
|= MAN_HALT
;
597 man_verr(struct man
*man
, int ln
, int pos
, const char *fmt
, ...)
602 if (NULL
== man
->cb
.man_err
)
606 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
608 return((*man
->cb
.man_err
)(man
->data
, ln
, pos
, buf
));
613 man_vwarn(struct man
*man
, int ln
, int pos
, const char *fmt
, ...)
618 if (NULL
== man
->cb
.man_warn
)
622 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
624 return((*man
->cb
.man_warn
)(man
->data
, ln
, pos
, buf
));
629 man_err(struct man
*m
, int line
, int pos
, int iserr
, enum merr type
)
633 p
= __man_merrnames
[(int)type
];
637 return(man_verr(m
, line
, pos
, p
));
639 return(man_vwarn(m
, line
, pos
, p
));