=================================================================== RCS file: /cvs/mandoc/mdoc.c,v retrieving revision 1.1 retrieving revision 1.6 diff -u -p -r1.1 -r1.6 --- mandoc/mdoc.c 2008/12/15 01:54:58 1.1 +++ mandoc/mdoc.c 2008/12/28 21:25:09 1.6 @@ -1,4 +1,4 @@ -/* $Id: mdoc.c,v 1.1 2008/12/15 01:54:58 kristaps Exp $ */ +/* $Id: mdoc.c,v 1.6 2008/12/28 21:25:09 kristaps Exp $ */ /* * Copyright (c) 2008 Kristaps Dzonsons * @@ -26,10 +26,6 @@ #include "private.h" -extern int macro_text(struct mdoc *, int, int, int *, char *); -extern int macro_scoped_implicit(struct mdoc *, - int, int, int *, char *); - const char *const __mdoc_macronames[MDOC_MAX] = { "\\\"", "Dd", "Dt", "Os", "Sh", "Ss", "Pp", "D1", @@ -88,18 +84,18 @@ const char *const __mdoc_argnames[MDOC_ARG_MAX] = { const struct mdoc_macro __mdoc_macros[MDOC_MAX] = { { NULL, 0 }, /* \" */ - { NULL, 0 }, /* Dd */ - { NULL, 0 }, /* Dt */ - { NULL, 0 }, /* Os */ + { macro_prologue_ddate, 0 }, /* Dd */ + { macro_prologue_dtitle, 0 }, /* Dt */ + { macro_prologue_os, 0 }, /* Os */ { macro_scoped_implicit, 0 }, /* Sh */ { macro_scoped_implicit, 0 }, /* Ss */ - { NULL, 0 }, /* Pp */ - { NULL, 0 }, /* D1 */ - { NULL, 0 }, /* Dl */ - { NULL, 0 }, /* Bd */ - { NULL, 0 }, /* Ed */ - { NULL, 0 }, /* Bl */ - { NULL, 0 }, /* El */ + { macro_text, 0 }, /* Pp */ + { macro_scoped_line, 0 }, /* D1 */ + { macro_scoped_line, 0 }, /* Dl */ + { macro_scoped_explicit, MDOC_EXPLICIT }, /* Bd */ + { macro_scoped_explicit, 0 }, /* Ed */ + { macro_scoped_explicit, MDOC_EXPLICIT }, /* Bl */ + { macro_scoped_explicit, 0 }, /* El */ { NULL, 0 }, /* It */ { macro_text, MDOC_CALLABLE }, /* Ad */ { NULL, 0 }, /* An */ @@ -170,7 +166,7 @@ const struct mdoc_macro __mdoc_macros[MDOC_MAX] = { { NULL, 0 }, /* Qc */ { NULL, 0 }, /* Ql */ { NULL, 0 }, /* Qo */ - { NULL, 0 }, /* Qq */ + { macro_scoped_pline, MDOC_CALLABLE }, /* Qq */ { NULL, 0 }, /* Re */ { NULL, 0 }, /* Rs */ { NULL, 0 }, /* Sc */ @@ -200,9 +196,6 @@ const char * const *mdoc_argnames = __mdoc_argnames; const struct mdoc_macro * const mdoc_macros = __mdoc_macros; -static void *xcalloc(size_t, size_t); -static char *xstrdup(const char *); - static struct mdoc_arg *argdup(size_t, const struct mdoc_arg *); static void argfree(size_t, struct mdoc_arg *); static void argcpy(struct mdoc_arg *, @@ -232,7 +225,7 @@ mdoc_free(struct mdoc *mdoc) if (mdoc->first) mdoc_node_freelist(mdoc->first); if (mdoc->htab) - mdoc_hash_free(mdoc->htab); + mdoc_tokhash_free(mdoc->htab); free(mdoc); } @@ -248,42 +241,20 @@ mdoc_alloc(void *data, const struct mdoc_cb *cb) p->data = data; (void)memcpy(&p->cb, cb, sizeof(struct mdoc_cb)); - p->htab = mdoc_hash_alloc(); + p->htab = mdoc_tokhash_alloc(); return(p); } -static void * -xcalloc(size_t num, size_t sz) -{ - void *p; - - if (NULL == (p = calloc(num, sz))) - err(EXIT_FAILURE, "calloc"); - return(p); -} - - -static char * -xstrdup(const char *p) -{ - char *pp; - - if (NULL == (pp = strdup(p))) - err(EXIT_FAILURE, "strdup"); - return(pp); -} - - int mdoc_parseln(struct mdoc *mdoc, char *buf) { int c, i; char tmp[5]; - if ('.' != *buf) { - /* TODO. */ - return(1); + if ('.' != *buf) { + mdoc_word_alloc(mdoc, 0, buf); + return(1); } if (buf[1] && '\\' == buf[1]) @@ -301,7 +272,7 @@ mdoc_parseln(struct mdoc *mdoc, char *buf) i--; - (void)memcpy(tmp, buf + 1, i); + (void)memcpy(tmp, buf + 1, (size_t)i); tmp[i++] = 0; if (MDOC_MAX == (c = mdoc_find(mdoc, tmp))) @@ -310,12 +281,7 @@ mdoc_parseln(struct mdoc *mdoc, char *buf) while (buf[i] && isspace(buf[i])) i++; - if (NULL == (mdoc_macros[c].fp)) { - (void)mdoc_err(mdoc, c, 1, ERR_MACRO_NOTSUP); - return(0); - } - - return((*mdoc_macros[c].fp)(mdoc, c, 1, &i, buf)); + return(mdoc_macro(mdoc, c, 1, &i, buf)); } @@ -363,11 +329,15 @@ mdoc_macro(struct mdoc *mdoc, int tok, int ppos, int * if (NULL == (mdoc_macros[tok].fp)) { (void)mdoc_err(mdoc, tok, ppos, ERR_MACRO_NOTSUP); return(0); - } else if ( ! (MDOC_CALLABLE & mdoc_macros[tok].flags)) { + } + + if (1 != ppos && ! (MDOC_CALLABLE & mdoc_macros[tok].flags)) { (void)mdoc_err(mdoc, tok, ppos, ERR_MACRO_NOTCALL); return(0); } + /*mdoc_msg(mdoc, ppos, "calling `%s'", mdoc_macronames[tok]);*/ + return((*mdoc_macros[tok].fp)(mdoc, tok, ppos, pos, buf)); } @@ -379,7 +349,7 @@ mdoc_node_append(struct mdoc *mdoc, int pos, struct md switch (p->type) { case (MDOC_TEXT): - nn = ""; + nn = p->data.text.string; nt = "text"; break; case (MDOC_BODY): @@ -398,6 +368,9 @@ mdoc_node_append(struct mdoc *mdoc, int pos, struct md nn = mdoc_macronames[p->data.block.tok]; nt = "block"; break; + default: + abort(); + /* NOTREACHED */ } if (NULL == mdoc->first) { @@ -429,43 +402,38 @@ mdoc_node_append(struct mdoc *mdoc, int pos, struct md on = mdoc_macronames[mdoc->last->data.block.tok]; ot = "block"; break; + default: + abort(); + /* NOTREACHED */ } switch (p->type) { case (MDOC_BODY): - switch (mdoc->last->type) { - case (MDOC_BLOCK): - p->parent = mdoc->last; - mdoc->last->child = p; - act = "child"; - break; - case (MDOC_HEAD): - p->parent = mdoc->last->parent; - mdoc->last->next = p; - act = "sibling"; - break; - default: - abort(); - /* NOTREACHED */ - } + p->parent = mdoc->last->parent; + mdoc->last->next = p; + p->prev = mdoc->last; + act = "sibling"; break; + case (MDOC_HEAD): assert(mdoc->last->type == MDOC_BLOCK); p->parent = mdoc->last; mdoc->last->child = p; act = "child"; break; + default: switch (mdoc->last->type) { case (MDOC_BODY): /* FALLTHROUGH */ case (MDOC_HEAD): - p->parent = mdoc->last->parent; + p->parent = mdoc->last; mdoc->last->child = p; act = "child"; break; default: p->parent = mdoc->last->parent; + p->prev = mdoc->last; mdoc->last->next = p; act = "sibling"; break; @@ -479,6 +447,7 @@ mdoc_node_append(struct mdoc *mdoc, int pos, struct md } +/* FIXME: deprecate paramsz, params. */ void mdoc_head_alloc(struct mdoc *mdoc, int pos, int tok, size_t paramsz, const char **params) @@ -575,16 +544,18 @@ mdoc_word_alloc(struct mdoc *mdoc, int pos, const char static void argfree(size_t sz, struct mdoc_arg *p) { - size_t i, j; + int i, j; if (0 == sz) return; assert(p); - for (i = 0; i < sz; i++) + /* LINTED */ + for (i = 0; i < (int)sz; i++) if (p[i].sz > 0) { assert(p[i].value); - for (j = 0; j < p[i].sz; j++) + /* LINTED */ + for (j = 0; j < (int)p[i].sz; j++) free(p[i].value[j]); } free(p); @@ -667,20 +638,20 @@ int mdoc_find(const struct mdoc *mdoc, const char *key) { - return(mdoc_hash_find(mdoc->htab, key)); + return(mdoc_tokhash_find(mdoc->htab, key)); } static void argcpy(struct mdoc_arg *dst, const struct mdoc_arg *src) { - size_t i; + int i; dst->arg = src->arg; if (0 == (dst->sz = src->sz)) return; dst->value = xcalloc(dst->sz, sizeof(char *)); - for (i = 0; i < dst->sz; i++) + for (i = 0; i < (int)dst->sz; i++) dst->value[i] = xstrdup(src->value[i]); } @@ -689,13 +660,13 @@ static struct mdoc_arg * argdup(size_t argsz, const struct mdoc_arg *args) { struct mdoc_arg *pp; - size_t i; + int i; if (0 == argsz) return(NULL); pp = xcalloc((size_t)argsz, sizeof(struct mdoc_arg)); - for (i = 0; i < argsz; i++) + for (i = 0; i < (int)argsz; i++) argcpy(&pp[i], &args[i]); return(pp); @@ -705,13 +676,14 @@ argdup(size_t argsz, const struct mdoc_arg *args) static void paramfree(size_t sz, char **p) { - size_t i; + int i; if (0 == sz) return; assert(p); - for (i = 0; i < sz; i++) + /* LINTED */ + for (i = 0; i < (int)sz; i++) free(p[i]); free(p); } @@ -721,13 +693,13 @@ static char ** paramdup(size_t sz, const char **p) { char **pp; - size_t i; + int i; if (0 == sz) return(NULL); pp = xcalloc(sz, sizeof(char *)); - for (i = 0; i < sz; i++) + for (i = 0; i < (int)sz; i++) pp[i] = xstrdup(p[i]); return(pp);