version 1.69, 2009/03/21 09:42:07 |
version 1.71, 2009/03/23 15:20:51 |
|
|
#include <stdio.h> |
#include <stdio.h> |
#include <string.h> |
#include <string.h> |
|
|
#include "private.h" |
#include "libmdoc.h" |
|
|
/* |
/* |
* Main caller in the libmdoc library. This begins the parsing routine, |
* Main caller in the libmdoc library. This begins the parsing routine, |
|
|
* in macro.c, validate.c and action.c. |
* in macro.c, validate.c and action.c. |
*/ |
*/ |
|
|
static struct mdoc_node *mdoc_node_alloc(const struct mdoc *); |
|
static int mdoc_node_append(struct mdoc *, |
|
struct mdoc_node *); |
|
|
|
static int parsetext(struct mdoc *, int, char *); |
|
static int parsemacro(struct mdoc *, int, char *); |
|
static int macrowarn(struct mdoc *, int, const char *); |
|
|
|
|
|
const char *const __mdoc_macronames[MDOC_MAX] = { |
const char *const __mdoc_macronames[MDOC_MAX] = { |
"\\\"", "Dd", "Dt", "Os", |
"\\\"", "Dd", "Dt", "Os", |
"Sh", "Ss", "Pp", "D1", |
"Sh", "Ss", "Pp", "D1", |
Line 94 const char *const __mdoc_argnames[MDOC_ARG_MAX] = { |
|
Line 85 const char *const __mdoc_argnames[MDOC_ARG_MAX] = { |
|
const char * const *mdoc_macronames = __mdoc_macronames; |
const char * const *mdoc_macronames = __mdoc_macronames; |
const char * const *mdoc_argnames = __mdoc_argnames; |
const char * const *mdoc_argnames = __mdoc_argnames; |
|
|
|
/* FIXME: have this accept line/pos/tok. */ |
|
/* FIXME: mdoc_alloc1 and mdoc_free1 like in man.c. */ |
|
static struct mdoc_node *mdoc_node_alloc(const struct mdoc *); |
|
static int mdoc_node_append(struct mdoc *, |
|
struct mdoc_node *); |
|
|
|
static int parsetext(struct mdoc *, int, char *); |
|
static int parsemacro(struct mdoc *, int, char *); |
|
static int macrowarn(struct mdoc *, int, const char *); |
|
|
|
|
/* |
/* |
* Get the first (root) node of the parse tree. |
* Get the first (root) node of the parse tree. |
*/ |
*/ |
const struct mdoc_node * |
const struct mdoc_node * |
mdoc_node(const struct mdoc *mdoc) |
mdoc_node(const struct mdoc *m) |
{ |
{ |
|
|
if (MDOC_HALT & mdoc->flags) |
return(MDOC_HALT & m->flags ? NULL : m->first); |
return(NULL); |
|
if (mdoc->first) |
|
assert(MDOC_ROOT == mdoc->first->type); |
|
return(mdoc->first); |
|
} |
} |
|
|
|
|
const struct mdoc_meta * |
const struct mdoc_meta * |
mdoc_meta(const struct mdoc *mdoc) |
mdoc_meta(const struct mdoc *m) |
{ |
{ |
|
|
if (MDOC_HALT & mdoc->flags) |
return(MDOC_HALT & m->flags ? NULL : &m->meta); |
return(NULL); |
|
return(&mdoc->meta); |
|
} |
} |
|
|
|
|
/* |
/* |
* Free up all resources contributed by a parse: the node tree, meta-data and |
* Free up all resources contributed by a parse: the node tree, |
* so on. Then reallocate the root node for another parse. |
* meta-data and so on. Then reallocate the root node for another |
|
* parse. |
*/ |
*/ |
void |
void |
mdoc_reset(struct mdoc *mdoc) |
mdoc_reset(struct mdoc *mdoc) |
Line 144 mdoc_reset(struct mdoc *mdoc) |
|
Line 140 mdoc_reset(struct mdoc *mdoc) |
|
bzero(&mdoc->meta, sizeof(struct mdoc_meta)); |
bzero(&mdoc->meta, sizeof(struct mdoc_meta)); |
mdoc->flags = 0; |
mdoc->flags = 0; |
mdoc->lastnamed = mdoc->lastsec = 0; |
mdoc->lastnamed = mdoc->lastsec = 0; |
|
mdoc->last = calloc(1, sizeof(struct mdoc_node)); |
mdoc->first = mdoc->last = |
if (NULL == mdoc->last) |
xcalloc(1, sizeof(struct mdoc_node)); |
err(1, "calloc"); |
|
mdoc->first = mdoc->last; |
mdoc->last->type = MDOC_ROOT; |
mdoc->last->type = MDOC_ROOT; |
mdoc->next = MDOC_NEXT_CHILD; |
mdoc->next = MDOC_NEXT_CHILD; |
} |
} |
Line 184 mdoc_alloc(void *data, int pflags, const struct mdoc_c |
|
Line 181 mdoc_alloc(void *data, int pflags, const struct mdoc_c |
|
{ |
{ |
struct mdoc *p; |
struct mdoc *p; |
|
|
p = xcalloc(1, sizeof(struct mdoc)); |
if (NULL == (p = calloc(1, sizeof(struct mdoc)))) |
|
err(1, "calloc"); |
|
|
p->data = data; |
p->data = data; |
if (cb) |
if (cb) |
(void)memcpy(&p->cb, cb, sizeof(struct mdoc_cb)); |
(void)memcpy(&p->cb, cb, sizeof(struct mdoc_cb)); |
|
|
p->last = p->first = |
if (NULL == (p->first = calloc(1, sizeof(struct mdoc_node)))) |
xcalloc(1, sizeof(struct mdoc_node)); |
err(1, "calloc"); |
|
p->last = p->first; |
p->last->type = MDOC_ROOT; |
p->last->type = MDOC_ROOT; |
p->pflags = pflags; |
p->pflags = pflags; |
p->next = MDOC_NEXT_CHILD; |
p->next = MDOC_NEXT_CHILD; |
Line 361 mdoc_node_append(struct mdoc *mdoc, struct mdoc_node * |
|
Line 360 mdoc_node_append(struct mdoc *mdoc, struct mdoc_node * |
|
} |
} |
|
|
mdoc->last = p; |
mdoc->last = p; |
|
|
|
switch (p->type) { |
|
case (MDOC_TEXT): |
|
if ( ! mdoc_valid_post(mdoc)) |
|
return(0); |
|
if ( ! mdoc_action_post(mdoc)) |
|
return(0); |
|
break; |
|
default: |
|
break; |
|
} |
|
|
return(1); |
return(1); |
} |
} |
|
|
Line 370 mdoc_node_alloc(const struct mdoc *mdoc) |
|
Line 381 mdoc_node_alloc(const struct mdoc *mdoc) |
|
{ |
{ |
struct mdoc_node *p; |
struct mdoc_node *p; |
|
|
p = xcalloc(1, sizeof(struct mdoc_node)); |
if (NULL == (p = calloc(1, sizeof(struct mdoc_node)))) |
|
err(1, "calloc"); |
p->sec = mdoc->lastsec; |
p->sec = mdoc->lastsec; |
|
|
return(p); |
return(p); |
Line 435 mdoc_body_alloc(struct mdoc *mdoc, int line, int pos, |
|
Line 447 mdoc_body_alloc(struct mdoc *mdoc, int line, int pos, |
|
|
|
|
|
int |
int |
mdoc_root_alloc(struct mdoc *mdoc) |
|
{ |
|
struct mdoc_node *p; |
|
|
|
p = mdoc_node_alloc(mdoc); |
|
|
|
p->type = MDOC_ROOT; |
|
|
|
return(mdoc_node_append(mdoc, p)); |
|
} |
|
|
|
|
|
int |
|
mdoc_block_alloc(struct mdoc *mdoc, int line, int pos, |
mdoc_block_alloc(struct mdoc *mdoc, int line, int pos, |
int tok, struct mdoc_arg *args) |
int tok, struct mdoc_arg *args) |
{ |
{ |
Line 500 mdoc_word_alloc(struct mdoc *mdoc, |
|
Line 499 mdoc_word_alloc(struct mdoc *mdoc, |
|
p->line = line; |
p->line = line; |
p->pos = pos; |
p->pos = pos; |
p->type = MDOC_TEXT; |
p->type = MDOC_TEXT; |
p->string = xstrdup(word); |
if (NULL == (p->string = strdup(word))) |
|
err(1, "strdup"); |
|
|
return(mdoc_node_append(mdoc, p)); |
return(mdoc_node_append(mdoc, p)); |
} |
} |
Line 628 parsemacro(struct mdoc *m, int ln, char *buf) |
|
Line 628 parsemacro(struct mdoc *m, int ln, char *buf) |
|
|
|
if ( ! mdoc_macro(m, c, ln, 1, &i, buf)) |
if ( ! mdoc_macro(m, c, ln, 1, &i, buf)) |
goto err; |
goto err; |
|
|
/* |
|
* If we're in literal mode, then add a newline to the end of |
|
* macro lines. Our frontends will interpret this correctly |
|
* (it's documented in mdoc.3). |
|
*/ |
|
|
|
return(1); |
return(1); |
|
|