diff options
-rw-r--r-- | Makefile | 18 | ||||
-rw-r--r-- | argv.c | 93 | ||||
-rw-r--r-- | macro.c | 705 | ||||
-rw-r--r-- | mdoc.c | 152 | ||||
-rw-r--r-- | mdoc.h | 3 | ||||
-rw-r--r-- | mdocml.c | 5 | ||||
-rw-r--r-- | private.h | 26 | ||||
-rw-r--r-- | prologue.c | 217 | ||||
-rw-r--r-- | validate.c | 354 |
9 files changed, 817 insertions, 756 deletions
@@ -2,15 +2,15 @@ VERSION = 1.1.0 CFLAGS += -W -Wall -Wno-unused-parameter -g -LNS = macro.ln mdoc.ln mdocml.ln hash.ln strings.ln xstd.ln argv.ln validate.ln +LNS = macro.ln mdoc.ln mdocml.ln hash.ln strings.ln xstd.ln argv.ln validate.ln prologue.ln LLNS = llib-llibmdoc.ln llib-lmdocml.ln LIBS = libmdoc.a -OBJS = macro.o mdoc.o mdocml.o hash.o strings.o xstd.o argv.o validate.o +OBJS = macro.o mdoc.o mdocml.o hash.o strings.o xstd.o argv.o validate.o prologue.o -SRCS = macro.c mdoc.c mdocml.c hash.c strings.c xstd.c argv.c validate.c +SRCS = macro.c mdoc.c mdocml.c hash.c strings.c xstd.c argv.c validate.c prologue.c HEADS = mdoc.h @@ -28,8 +28,8 @@ mdocml: mdocml.o libmdoc.a clean: rm -f $(CLEAN) -llib-llibmdoc.ln: macro.ln mdoc.ln hash.ln strings.ln xstd.ln argv.ln validate.ln - $(LINT) $(LINTFLAGS) -Clibmdoc mdoc.ln macro.ln hash.ln strings.ln xstd.ln argv.ln validate.ln +llib-llibmdoc.ln: macro.ln mdoc.ln hash.ln strings.ln xstd.ln argv.ln validate.ln prologue.ln + $(LINT) $(LINTFLAGS) -Clibmdoc mdoc.ln macro.ln hash.ln strings.ln xstd.ln argv.ln validate.ln prologue.ln llib-lmdocml.ln: mdocml.ln llib-llibmdoc.ln $(LINT) $(LINTFLAGS) -Cmdocml mdocml.ln llib-llibmdoc.ln @@ -66,8 +66,12 @@ validate.ln: validate.c private.h validate.o: validate.c private.h +prologue.ln: prologue.c private.h + +prologue.o: prologue.c private.h + private.h: mdoc.h -libmdoc.a: macro.o mdoc.o hash.o strings.o xstd.o argv.o validate.o - $(AR) rs $@ macro.o mdoc.o hash.o strings.o xstd.o argv.o validate.o +libmdoc.a: macro.o mdoc.o hash.o strings.o xstd.o argv.o validate.o prologue.o + $(AR) rs $@ macro.o mdoc.o hash.o strings.o xstd.o argv.o validate.o prologue.o @@ -183,6 +183,83 @@ lookup(int tok, const char *argv) if (xstrcmp(argv, "std")) return(MDOC_Std); break; + + case (MDOC_St): + if (xstrcmp(argv, "p1003.1-88")) + return(MDOC_p1003_1_88); + else if (xstrcmp(argv, "p1003.1-90")) + return(MDOC_p1003_1_90); + else if (xstrcmp(argv, "p1003.1-96")) + return(MDOC_p1003_1_96); + else if (xstrcmp(argv, "p1003.1-2001")) + return(MDOC_p1003_1_2001); + else if (xstrcmp(argv, "p1003.1-2004")) + return(MDOC_p1003_1_2004); + else if (xstrcmp(argv, "p1003.1")) + return(MDOC_p1003_1); + else if (xstrcmp(argv, "p1003.1b")) + return(MDOC_p1003_1b); + else if (xstrcmp(argv, "p1003.1b-93")) + return(MDOC_p1003_1b_93); + else if (xstrcmp(argv, "p1003.1c-95")) + return(MDOC_p1003_1c_95); + else if (xstrcmp(argv, "p1003.1g-2000")) + return(MDOC_p1003_1g_2000); + else if (xstrcmp(argv, "p1003.2-92")) + return(MDOC_p1003_2_92); + else if (xstrcmp(argv, "p1003.2-95")) + return(MDOC_p1387_2_95); + else if (xstrcmp(argv, "p1003.2")) + return(MDOC_p1003_2); + else if (xstrcmp(argv, "p1387.2-95")) + return(MDOC_p1387_2); + else if (xstrcmp(argv, "isoC-90")) + return(MDOC_isoC_90); + else if (xstrcmp(argv, "isoC-amd1")) + return(MDOC_isoC_amd1); + else if (xstrcmp(argv, "isoC-tcor1")) + return(MDOC_isoC_tcor1); + else if (xstrcmp(argv, "isoC-tcor2")) + return(MDOC_isoC_tcor2); + else if (xstrcmp(argv, "isoC-99")) + return(MDOC_isoC_99); + else if (xstrcmp(argv, "ansiC")) + return(MDOC_ansiC); + else if (xstrcmp(argv, "ansiC-89")) + return(MDOC_ansiC_89); + else if (xstrcmp(argv, "ansiC-99")) + return(MDOC_ansiC_99); + else if (xstrcmp(argv, "ieee754")) + return(MDOC_ieee754); + else if (xstrcmp(argv, "iso8802-3")) + return(MDOC_iso8802_3); + else if (xstrcmp(argv, "xpg3")) + return(MDOC_xpg3); + else if (xstrcmp(argv, "xpg4")) + return(MDOC_xpg4); + else if (xstrcmp(argv, "xpg4.2")) + return(MDOC_xpg4_2); + else if (xstrcmp(argv, "xpg4.3")) + return(MDOC_xpg4_3); + else if (xstrcmp(argv, "xbd5")) + return(MDOC_xbd5); + else if (xstrcmp(argv, "xcu5")) + return(MDOC_xcu5); + else if (xstrcmp(argv, "xsh5")) + return(MDOC_xsh5); + else if (xstrcmp(argv, "xns5")) + return(MDOC_xns5); + else if (xstrcmp(argv, "xns5.2d2.0")) + return(MDOC_xns5_2d2_0); + else if (xstrcmp(argv, "xcurses4.2")) + return(MDOC_xcurses4_2); + else if (xstrcmp(argv, "susv2")) + return(MDOC_susv2); + else if (xstrcmp(argv, "susv3")) + return(MDOC_susv3); + else if (xstrcmp(argv, "svid4")) + return(MDOC_svid4); + break; default: abort(); @@ -326,14 +403,12 @@ mdoc_argv(struct mdoc *mdoc, int tok, (void)memset(v, 0, sizeof(struct mdoc_arg)); if (0 == buf[*pos]) - return(0); + return(ARGV_EOLN); assert( ! isspace(buf[*pos])); - if ('-' != buf[*pos]) { - (void)mdoc_err(mdoc, tok, *pos, ERR_SYNTAX_ARGFORM); - return(-1); - } + if ('-' != buf[*pos]) + return(ARGV_WORD); i = *pos; argv = &buf[++(*pos)]; @@ -346,7 +421,7 @@ mdoc_argv(struct mdoc *mdoc, int tok, if (MDOC_ARG_MAX == (v->arg = lookup(tok, argv))) { (void)mdoc_err(mdoc, tok, i, ERR_SYNTAX_ARG); - return(-1); + return(ARGV_ERROR); } while (buf[*pos] && isspace(buf[*pos])) @@ -356,11 +431,11 @@ mdoc_argv(struct mdoc *mdoc, int tok, ppos = *pos; if ( ! parse(mdoc, tok, v, pos, buf)) - return(-1); + return(ARGV_ERROR); if ( ! postparse(mdoc, tok, v, ppos)) - return(-1); + return(ARGV_ERROR); - return(1); + return(ARGV_ARG); } @@ -28,50 +28,44 @@ #include "private.h" /* FIXME: maxlineargs should be per LINE, no per TOKEN. */ -/* FIXME: prologue check should be in macro_call. */ -/* FIXME: prologue macros should be part of macro_constant. */ #define _CC(p) ((const char **)p) -static int scope_rewind_exp(struct mdoc *, int, int, int); static int scope_rewind_imp(struct mdoc *, int, int); +static int scope_rewind_exp(struct mdoc *, int, int, int); +static int scope_rewind_line(struct mdoc *, int, int); static int append_text(struct mdoc *, int, int, int, char *[]); -static int append_const(struct mdoc *, int, int, int, char *[]); -static int append_constarg(struct mdoc *, int, int, - int, const struct mdoc_arg *); -static int append_scoped(struct mdoc *, int, int, int, - const char *[], int, const struct mdoc_arg *); +static int append_text_argv(struct mdoc *, int, int, + int, char *[], int, const struct mdoc_arg *); static int append_delims(struct mdoc *, int, int *, char *); static int -scope_rewind_imp(struct mdoc *mdoc, int ppos, int tok) +scope_rewind_line(struct mdoc *mdoc, int ppos, int tok) { struct mdoc_node *n; int t; - n = mdoc->last ? mdoc->last->parent : NULL; - /* LINTED */ - for ( ; n; n = n->parent) { - if (MDOC_BLOCK != n->type) + for (n = mdoc->last; n; n = n->parent) { + if (MDOC_HEAD != n->type) continue; - if (tok == (t = n->data.block.tok)) + if (tok == (t = n->data.head.tok)) break; if ( ! (MDOC_EXPLICIT & mdoc_macros[t].flags)) continue; return(mdoc_err(mdoc, tok, ppos, ERR_SCOPE_BREAK)); } - if (n) - mdoc->last = n; + mdoc->last = n ? n : mdoc->last; + mdoc->next = MDOC_NEXT_SIBLING; return(1); } static int -scope_rewind_exp(struct mdoc *mdoc, int ppos, int tok, int dst) +scope_rewind_exp(struct mdoc *mdoc, int ppos, int tok, int tt) { struct mdoc_node *n; @@ -81,14 +75,41 @@ scope_rewind_exp(struct mdoc *mdoc, int ppos, int tok, int dst) for (n = mdoc->last->parent; n; n = n->parent) { if (MDOC_BLOCK != n->type) continue; - if (dst == n->data.block.tok) + if (tt == n->data.block.tok) break; return(mdoc_err(mdoc, tok, ppos, ERR_SCOPE_BREAK)); } - if ((mdoc->last = n)) - return(1); - return(mdoc_err(mdoc, tok, ppos, ERR_SCOPE_NOCTX)); + if (NULL == (mdoc->last = n)) + return(mdoc_err(mdoc, tok, ppos, ERR_SCOPE_NOCTX)); + + mdoc->next = MDOC_NEXT_SIBLING; + return(mdoc_valid_post(mdoc, tok, ppos)); +} + + +static int +scope_rewind_imp(struct mdoc *mdoc, int ppos, int tok) +{ + struct mdoc_node *n; + int t; + + n = mdoc->last ? mdoc->last->parent : NULL; + + /* LINTED */ + for ( ; n; n = n->parent) { + if (MDOC_BLOCK != n->type) + continue; + if (tok == (t = n->data.block.tok)) + break; + if ( ! (MDOC_EXPLICIT & mdoc_macros[t].flags)) + continue; + return(mdoc_err(mdoc, tok, ppos, ERR_SCOPE_BREAK)); + } + + mdoc->last = n ? n : mdoc->last; + mdoc->next = MDOC_NEXT_SIBLING; + return(mdoc_valid_post(mdoc, tok, ppos)); } @@ -110,6 +131,7 @@ append_delims(struct mdoc *mdoc, int tok, int *pos, char *buf) break; assert(mdoc_isdelim(p)); mdoc_word_alloc(mdoc, lastarg, p); + mdoc->next = MDOC_NEXT_SIBLING; } return(1); @@ -117,51 +139,26 @@ append_delims(struct mdoc *mdoc, int tok, int *pos, char *buf) static int -append_constarg(struct mdoc *mdoc, int tok, int pos, +append_text_argv(struct mdoc *mdoc, int tok, int pos, + int sz, char *args[], int argc, const struct mdoc_arg *argv) { - if ( ! mdoc_valid(mdoc, tok, pos, 0, NULL, argc, argv)) + if ( ! mdoc_valid_pre(mdoc, tok, pos, 0, NULL, argc, argv)) return(0); - mdoc_elem_alloc(mdoc, pos, tok, argc, argv, 0, NULL); + mdoc_elem_alloc(mdoc, pos, tok, (size_t)argc, + argv, (size_t)sz, _CC(args)); + mdoc->next = MDOC_NEXT_SIBLING; return(1); } static int -append_scoped(struct mdoc *mdoc, int tok, int pos, - int sz, const char *args[], - int argc, const struct mdoc_arg *argv) -{ - enum mdoc_sec sec; - - if ( ! mdoc_valid(mdoc, tok, pos, sz, args, argc, argv)) - return(0); - - switch (tok) { - case (MDOC_Sh): - sec = mdoc_atosec((size_t)sz, _CC(args)); - if (SEC_CUSTOM != sec) - mdoc->sec_lastn = sec; - mdoc->sec_last = sec; - break; - default: - break; - } - - mdoc_block_alloc(mdoc, pos, tok, (size_t)argc, argv); - mdoc_head_alloc(mdoc, pos, tok, (size_t)sz, _CC(args)); - mdoc_body_alloc(mdoc, pos, tok); - return(1); -} - - -static int -append_const(struct mdoc *mdoc, int tok, +append_text(struct mdoc *mdoc, int tok, int pos, int sz, char *args[]) { - if ( ! mdoc_valid(mdoc, tok, pos, sz, _CC(args), 0, NULL)) + if ( ! mdoc_valid_pre(mdoc, tok, pos, sz, _CC(args), 0, NULL)) return(0); switch (tok) { @@ -171,31 +168,26 @@ append_const(struct mdoc *mdoc, int tok, if (ATT_DEFAULT == mdoc_atoatt(args[0])) { mdoc_elem_alloc(mdoc, pos, tok, 0, NULL, 0, NULL); + mdoc->next = MDOC_NEXT_SIBLING; mdoc_word_alloc(mdoc, pos, args[0]); - } else + mdoc->next = MDOC_NEXT_SIBLING; + } else { mdoc_elem_alloc(mdoc, pos, tok, 0, NULL, 1, _CC(&args[0])); + mdoc->next = MDOC_NEXT_SIBLING; + } if (sz > 1) mdoc_word_alloc(mdoc, pos, args[1]); + mdoc->next = MDOC_NEXT_SIBLING; return(1); + default: break; } mdoc_elem_alloc(mdoc, pos, tok, 0, NULL, (size_t)sz, _CC(args)); - return(1); -} - - -static int -append_text(struct mdoc *mdoc, int tok, - int pos, int sz, char *args[]) -{ - - if ( ! mdoc_valid(mdoc, tok, pos, sz, _CC(args), 0, NULL)) - return(0); - mdoc_elem_alloc(mdoc, pos, tok, 0, NULL, (size_t)sz, _CC(args)); + mdoc->next = MDOC_NEXT_SIBLING; return(1); } @@ -206,9 +198,6 @@ macro_text(MACRO_PROT_ARGS) int lastarg, lastpunct, c, j, fl; char *args[MDOC_LINEARG_MAX]; - if (SEC_PROLOGUE == mdoc->sec_lastn) - return(mdoc_err(mdoc, tok, ppos, ERR_SEC_PROLOGUE)); - /* Token pre-processing. */ switch (tok) { @@ -311,6 +300,7 @@ again: return(0); mdoc_word_alloc(mdoc, lastarg, args[j]); + mdoc->next = MDOC_NEXT_SIBLING; j = 0; lastpunct = 1; @@ -319,203 +309,48 @@ again: } -int -macro_prologue_dtitle(MACRO_PROT_ARGS) -{ - int lastarg, j; - char *args[MDOC_LINEARG_MAX]; - - if (SEC_PROLOGUE != mdoc->sec_lastn) - return(mdoc_err(mdoc, tok, ppos, ERR_SEC_NPROLOGUE)); - if (0 == mdoc->meta.date) - return(mdoc_err(mdoc, tok, ppos, ERR_SEC_PROLOGUE_OO)); - if (mdoc->meta.title[0]) - return(mdoc_err(mdoc, tok, ppos, ERR_SEC_PROLOGUE_REP)); - - j = -1; - lastarg = ppos; - -again: - if (j == MDOC_LINEARG_MAX) - return(mdoc_err(mdoc, tok, lastarg, ERR_ARGS_MANY)); - - lastarg = *pos; - - switch (mdoc_args(mdoc, tok, pos, buf, 0, &args[++j])) { - case (ARGS_EOLN): - if (mdoc->meta.title) - return(1); - if ( ! mdoc_warn(mdoc, tok, ppos, WARN_ARGS_GE1)) - return(0); - (void)xstrlcpy(mdoc->meta.title, - "UNTITLED", META_TITLE_SZ); - return(1); - case (ARGS_ERROR): - return(0); - default: - break; - } - - if (MDOC_MAX != mdoc_find(mdoc, args[j]) && ! mdoc_warn - (mdoc, tok, lastarg, WARN_SYNTAX_MACLIKE)) - return(0); - - if (0 == j) { - if (xstrlcpy(mdoc->meta.title, args[0], META_TITLE_SZ)) - goto again; - return(mdoc_err(mdoc, tok, lastarg, ERR_SYNTAX_ARGFORM)); - - } else if (1 == j) { - mdoc->meta.msec = mdoc_atomsec(args[1]); - if (MSEC_DEFAULT != mdoc->meta.msec) - goto again; - return(mdoc_err(mdoc, tok, -1, ERR_SYNTAX_ARGFORM)); - - } else if (2 == j) { - mdoc->meta.vol = mdoc_atovol(args[2]); - if (VOL_DEFAULT != mdoc->meta.vol) - goto again; - mdoc->meta.arch = mdoc_atoarch(args[2]); - if (ARCH_DEFAULT != mdoc->meta.arch) - goto again; - return(mdoc_err(mdoc, tok, lastarg, ERR_SYNTAX_ARGFORM)); - } - - return(mdoc_err(mdoc, tok, lastarg, ERR_ARGS_MANY)); -} - +/* ARGSUSED */ int -macro_prologue_os(MACRO_PROT_ARGS) +macro_close_explicit(MACRO_PROT_ARGS) { - int lastarg, j; - char *args[MDOC_LINEARG_MAX]; - - /* FIXME: if we use `Os' again... ? */ - - if (SEC_PROLOGUE != mdoc->sec_lastn) - return(mdoc_err(mdoc, tok, ppos, ERR_SEC_NPROLOGUE)); - if (0 == mdoc->meta.title[0]) - return(mdoc_err(mdoc, tok, ppos, ERR_SEC_PROLOGUE_OO)); - if (mdoc->meta.os[0]) - return(mdoc_err(mdoc, tok, ppos, ERR_SEC_PROLOGUE_REP)); - - j = -1; - lastarg = ppos; - -again: - if (j == MDOC_LINEARG_MAX) - return(mdoc_err(mdoc, tok, lastarg, ERR_ARGS_MANY)); + int tt; - lastarg = *pos; + /* + * First close out the explicit scope. The `end' tags (such as + * `.El' to `.Bl' don't cause anything to happen: we merely + * readjust our last parse point. + */ - switch (mdoc_args(mdoc, tok, pos, buf, - ARGS_QUOTED, &args[++j])) { - case (ARGS_EOLN): - mdoc->sec_lastn = mdoc->sec_last = SEC_BODY; - return(1); - case (ARGS_ERROR): - return(0); - default: + switch (tok) { + case (MDOC_El): + tt = MDOC_Bl; break; - } - - if ( ! xstrlcat(mdoc->meta.os, args[j], sizeof(mdoc->meta.os))) - return(mdoc_err(mdoc, tok, lastarg, ERR_SYNTAX_ARGFORM)); - if ( ! xstrlcat(mdoc->meta.os, " ", sizeof(mdoc->meta.os))) - return(mdoc_err(mdoc, tok, lastarg, ERR_SYNTAX_ARGFORM)); - - goto again; - /* NOTREACHED */ -} - - -int -macro_prologue_ddate(MACRO_PROT_ARGS) -{ - int lastarg, j; - char *args[MDOC_LINEARG_MAX], date[64]; - - if (SEC_PROLOGUE != mdoc->sec_lastn) - return(mdoc_err(mdoc, tok, ppos, ERR_SEC_NPROLOGUE)); - if (mdoc->meta.title[0]) - return(mdoc_err(mdoc, tok, ppos, ERR_SEC_PROLOGUE_OO)); - if (mdoc->meta.date) - return(mdoc_err(mdoc, tok, ppos, ERR_SEC_PROLOGUE_REP)); - - j = -1; - date[0] = 0; - lastarg = ppos; - -again: - if (j == MDOC_LINEARG_MAX) - return(mdoc_err(mdoc, tok, lastarg, ERR_ARGS_MANY)); - - lastarg = *pos; - switch (mdoc_args(mdoc, tok, pos, buf, 0, &args[++j])) { - case (ARGS_EOLN): - if (mdoc->meta.date) - return(1); - mdoc->meta.date = mdoc_atotime(date); - if (mdoc->meta.date) - return(1); - return(mdoc_err(mdoc, tok, ppos, ERR_SYNTAX_ARGFORM)); - case (ARGS_ERROR): - return(0); - default: + case (MDOC_Ed): + tt = MDOC_Bd; + break; + case (MDOC_Re): + tt = MDOC_Rs; break; + default: + abort(); + /* NOTREACHED */ } - - if (MDOC_MAX != mdoc_find(mdoc, args[j]) && ! mdoc_warn - (mdoc, tok, lastarg, WARN_SYNTAX_MACLIKE)) - return(0); - - if (0 == j) { - if (xstrcmp("$Mdocdate$", args[j])) { - mdoc->meta.date = time(NULL); - goto again; - } else if (xstrcmp("$Mdocdate:", args[j])) - goto again; - } else if (4 == j) - if ( ! xstrcmp("$", args[j])) - goto again; - - if ( ! xstrlcat(date, args[j], sizeof(date))) - return(mdoc_err(mdoc, tok, lastarg, ERR_SYNTAX_ARGFORM)); - if ( ! xstrlcat(date, " ", sizeof(date))) - return(mdoc_err(mdoc, tok, lastarg, ERR_SYNTAX_ARGFORM)); - goto again; - /* NOTREACHED */ + return(scope_rewind_exp(mdoc, ppos, tok, tt)); } int -macro_scoped_explicit(MACRO_PROT_ARGS) +macro_scoped(MACRO_PROT_ARGS) { - int c, lastarg, j; + int i, c, lastarg, argc, sz; + char *args[MDOC_LINEARG_MAX]; struct mdoc_arg argv[MDOC_LINEARG_MAX]; + enum mdoc_sec sec; struct mdoc_node *n; - if (SEC_PROLOGUE == mdoc->sec_lastn) - return(mdoc_err(mdoc, tok, ppos, ERR_SEC_PROLOGUE)); - - /* - * First close out the explicit scope. The `end' tags (such as - * `.El' to `.Bl' don't cause anything to happen: we merely - * readjust our last parse point. - */ - - switch (tok) { - case (MDOC_El): - return(scope_rewind_exp(mdoc, ppos, tok, MDOC_Bl)); - case (MDOC_Ed): - return(scope_rewind_exp(mdoc, ppos, tok, MDOC_Bd)); - default: - break; - } - - assert(MDOC_EXPLICIT & mdoc_macros[tok].flags); + assert ( ! (MDOC_CALLABLE & mdoc_macros[tok].flags)); /* Token pre-processing. */ @@ -537,65 +372,11 @@ macro_scoped_explicit(MACRO_PROT_ARGS) mdoc->last->next = NULL; mdoc_node_free(n); break; - default: - break; - } - - lastarg = *pos; - - for (j = 0; j < MDOC_LINEARG_MAX; j++) { - lastarg = *pos; - c = mdoc_argv(mdoc, tok, &argv[j], pos, buf); - if (0 == c) - break; - else if (1 == c) - continue; - - mdoc_argv_free(j, argv); - return(0); - } - - if (MDOC_LINEARG_MAX == j) { - mdoc_argv_free(j, argv); - return(mdoc_err(mdoc, tok, lastarg, ERR_ARGS_MANY)); - } - - c = append_scoped(mdoc, tok, ppos, 0, NULL, j, argv); - mdoc_argv_free(j, argv); - return(c); -} - - -/* - * Implicity-scoped macros, like `.Ss', have a scope that terminates - * with a subsequent call to the same macro. Implicit macros cannot - * break the scope of explicitly-scoped macros; however, they can break - * the scope of other implicit macros (so `.Sh' can break `.Ss'). This - * is ok with macros like `.It' because they exist only within an - * explicit context. - * - * These macros put line arguments (which it's allowed to have) into the - * HEAD section and open a BODY scope to be used until the macro scope - * closes. - */ -int -macro_scoped_implicit(MACRO_PROT_ARGS) -{ - int lastarg, j; - char *args[MDOC_LINEARG_MAX]; - struct mdoc_node *n; - - assert( ! (MDOC_EXPLICIT & mdoc_macros[tok].flags)); - - if (SEC_PROLOGUE == mdoc->sec_lastn) - return(mdoc_err(mdoc, tok, ppos, ERR_SEC_PROLOGUE)); - - /* Token pre-processing. */ - - switch (tok) { - case (MDOC_Ss): - /* FALLTHROUGH */ case (MDOC_Sh): + /* FALLTHROUGH */ + case (MDOC_Ss): + if ( ! scope_rewind_imp(mdoc, ppos, tok)) + return(0); /* `.Pp' ignored when preceding `.Ss' or `.Sh'. */ if (NULL == mdoc->last) break; @@ -616,163 +397,96 @@ macro_scoped_implicit(MACRO_PROT_ARGS) break; } - /* Rewind our scope. */ - - if ( ! scope_rewind_imp(mdoc, ppos, tok)) - return(0); - - j = 0; - lastarg = ppos; - - /* - * Process until we hit a line. Note that current implicit - * macros don't have any arguments, so we don't need to do any - * argument processing. - */ - -again: - if (j == MDOC_LINEARG_MAX) - return(mdoc_err(mdoc, tok, lastarg, ERR_ARGS_MANY)); + /* Argument processing. */ lastarg = *pos; - switch (mdoc_args(mdoc, tok, pos, buf, 0, &args[j])) { - case (ARGS_ERROR): + for (argc = 0; argc < MDOC_LINEARG_MAX; argc++) { + lastarg = *pos; + c = mdoc_argv(mdoc, tok, &argv[argc], pos, buf); + if (ARGV_EOLN == c || ARGV_WORD == c) + break; + else if (ARGV_ARG == c) + continue; + mdoc_argv_free(argc, argv); return(0); - case (ARGS_EOLN): - return(append_scoped(mdoc, tok, ppos, j, _CC(args), 0, NULL)); - default: - break; } - if (MDOC_MAX != mdoc_find(mdoc, args[j])) - if ( ! mdoc_warn(mdoc, tok, lastarg, WARN_SYNTAX_MACLIKE)) - return(0); - - j++; - goto again; - /* NOTREACHED */ -} + /* Parameter processing. */ + for (sz = 0; argc + sz < MDOC_LINEARG_MAX; sz++) { + lastarg = *pos; + c = mdoc_args(mdoc, tok, pos, buf, 0, &args[sz]); + if (ARGS_EOLN == c) + break; + if (ARGS_WORD == c) + continue; + mdoc_argv_free(argc, argv); + return(0); + } -/* - * A line-scoped macro opens a scope for the contents of its line, which - * are placed under the HEAD node. Punctuation trailing the line is put - * as a sibling to the HEAD node, under the BLOCK node. - */ -int -macro_scoped_line(MACRO_PROT_ARGS) -{ - int lastarg, c, j; - char *p; - struct mdoc_node *n; + if (MDOC_LINEARG_MAX == (argc + sz)) { + mdoc_argv_free(argc, argv); + return(mdoc_err(mdoc, tok, lastarg, ERR_ARGS_MANY)); + } - if (SEC_PROLOGUE == mdoc->sec_lastn) - return(mdoc_err(mdoc, tok, ppos, ERR_SEC_PROLOGUE)); + /* Post-processing. */ - assert(1 == ppos); - - /* Token pre-processing. */ + if ( ! mdoc_valid_pre(mdoc, tok, ppos, sz, _CC(args), argc, argv)) { + mdoc_argv_free(argc, argv); + return(0); + } switch (tok) { - case (MDOC_D1): - /* FALLTHROUGH */ - case (MDOC_Dl): - /* These can't be nested in a display block. */ - assert(mdoc->last); - for (n = mdoc->last->parent ; n; n = n->parent) - if (MDOC_BLOCK != n->type) - continue; - else if (MDOC_Bd == n->data.block.tok) - break; - if (NULL == n) - break; - return(mdoc_err(mdoc, tok, ppos, ERR_SCOPE_NONEST)); + case (MDOC_Sh): + sec = mdoc_atosec((size_t)sz, _CC(args)); + if (SEC_CUSTOM != sec) + mdoc->sec_lastn = sec; + mdoc->sec_last = sec; + break; default: break; } - /* - * All line-scoped macros have a HEAD and optionally a BODY - * section. We open our scope here; when we exit this function, - * we'll rewind our scope appropriately. - */ - - mdoc_block_alloc(mdoc, ppos, tok, 0, NULL); - mdoc_head_alloc(mdoc, ppos, tok, 0, NULL); + mdoc_block_alloc(mdoc, ppos, tok, (size_t)argc, argv); + mdoc->next = MDOC_NEXT_CHILD; - /* Process line parameters. */ - - j = 0; - lastarg = ppos; + mdoc_argv_free(argc, argv); -again: - if (j == MDOC_LINEARG_MAX) - return(mdoc_err(mdoc, tok, lastarg, ERR_ARGS_MANY)); + mdoc_head_alloc(mdoc, ppos, tok); + mdoc->next = MDOC_NEXT_CHILD; - lastarg = *pos; - c = mdoc_args(mdoc, tok, pos, buf, ARGS_DELIM, &p); - - switch (c) { - case (ARGS_ERROR): - return(0); - case (ARGS_WORD): - break; - case (ARGS_PUNCT): - if ( ! append_delims(mdoc, tok, pos, buf)) - return(0); - return(scope_rewind_imp(mdoc, ppos, tok)); - case (ARGS_EOLN): - return(scope_rewind_imp(mdoc, ppos, tok)); - default: - abort(); - /* NOTREACHED */ + for (i = 0; i < sz; i++) { + mdoc_word_alloc(mdoc, ppos, args[i]); + mdoc->next = MDOC_NEXT_SIBLING; } - if (MDOC_MAX != (c = mdoc_find(mdoc, p))) { - if ( ! mdoc_macro(mdoc, c, lastarg, pos, buf)) - return(0); - if ( ! append_delims(mdoc, tok, pos, buf)) - return(0); - return(scope_rewind_imp(mdoc, ppos, tok)); - } + if ( ! scope_rewind_line(mdoc, ppos, tok)) + return(0); - if (mdoc_isdelim(p)) - j = 0; + mdoc_body_alloc(mdoc, ppos, tok); + mdoc->next = MDOC_NEXT_CHILD; - mdoc_word_alloc(mdoc, lastarg, p); - goto again; - /* NOTREACHED */ + return(1); } -/* - * Partial-line scope is identical to line scope (macro_scoped_line()) - * except that trailing punctuation is appended to the BLOCK, instead of - * contained within the HEAD. - */ int -macro_scoped_pline(MACRO_PROT_ARGS) +macro_scoped_line(MACRO_PROT_ARGS) { int lastarg, c, j; char *p; - if (SEC_PROLOGUE == mdoc->sec_lastn) - return(mdoc_err(mdoc, tok, ppos, ERR_SEC_PROLOGUE)); - - /* Token pre-processing. */ + assert(1 == ppos); + + mdoc_block_alloc(mdoc, ppos, tok, 0, NULL); + mdoc->next = MDOC_NEXT_CHILD; - switch (tok) { - case (MDOC_Ql): - if ( ! mdoc_warn(mdoc, tok, ppos, WARN_COMPAT_TROFF)) - return(0); - break; - default: - break; - } + mdoc_head_alloc(mdoc, ppos, tok); + mdoc->next = MDOC_NEXT_CHILD; - mdoc_block_alloc(mdoc, ppos, tok, 0, NULL); - mdoc_head_alloc(mdoc, ppos, tok, 0, NULL); + if ( ! mdoc_valid_pre(mdoc, tok, ppos, 0, NULL, 0, NULL)) + return(0); /* Process line parameters. */ @@ -792,11 +506,13 @@ again: case (ARGS_WORD): break; case (ARGS_PUNCT): - if ( ! scope_rewind_imp(mdoc, ppos, tok)) - return(0); if (ppos > 1) - return(1); - return(append_delims(mdoc, tok, pos, buf)); + return(scope_rewind_imp(mdoc, ppos, tok)); + if ( ! scope_rewind_line(mdoc, ppos, tok)) + return(0); + if ( ! append_delims(mdoc, tok, pos, buf)) + return(0); + return(scope_rewind_imp(mdoc, ppos, tok)); case (ARGS_EOLN): return(scope_rewind_imp(mdoc, ppos, tok)); default: @@ -807,36 +523,31 @@ again: if (MDOC_MAX != (c = mdoc_find(mdoc, p))) { if ( ! mdoc_macro(mdoc, c, lastarg, pos, buf)) return(0); - if ( ! scope_rewind_imp(mdoc, ppos, tok)) - return(0); if (ppos > 1) - return(1); - return(append_delims(mdoc, tok, pos, buf)); + return(scope_rewind_imp(mdoc, ppos, tok)); + if ( ! scope_rewind_line(mdoc, ppos, tok)) + return(0); + if ( ! append_delims(mdoc, tok, pos, buf)) + return(0); + return(scope_rewind_imp(mdoc, ppos, tok)); } if (mdoc_isdelim(p)) j = 0; mdoc_word_alloc(mdoc, lastarg, p); + mdoc->next = MDOC_NEXT_SIBLING; goto again; /* NOTREACHED */ } -/* - * A delimited-constant macro is similar to a general text macro: the - * macro is followed by a 0 or 1 arguments (possibly-unspecified) then - * terminating punctuation, other words, or another callable macro. - */ int macro_constant_delimited(MACRO_PROT_ARGS) { int lastarg, flushed, c, maxargs; char *p; - if (SEC_PROLOGUE == mdoc->sec_lastn) - return(mdoc_err(mdoc, tok, ppos, ERR_SEC_PROLOGUE)); - /* Process line parameters. */ lastarg = ppos; @@ -845,6 +556,10 @@ macro_constant_delimited(MACRO_PROT_ARGS) /* Token pre-processing. */ switch (tok) { + case (MDOC_No): + /* FALLTHROUGH */ + case (MDOC_Ns): + /* FALLTHROUGH */ case (MDOC_Ux): maxargs = 0; break; @@ -862,7 +577,7 @@ again: case (ARGS_WORD): break; case (ARGS_PUNCT): - if ( ! flushed && ! append_const(mdoc, tok, ppos, 0, &p)) + if ( ! flushed && ! append_text(mdoc, tok, ppos, 0, &p)) return(0); if (ppos > 1) return(1); @@ -870,7 +585,7 @@ again: case (ARGS_EOLN): if (flushed) return(1); - return(append_const(mdoc, tok, ppos, 0, &p)); + return(append_text(mdoc, tok, ppos, 0, &p)); default: abort(); /* NOTREACHED */ @@ -878,14 +593,14 @@ again: /* Accepts no arguments: flush out symbol and continue. */ - if (0 == maxargs) { - if ( ! append_const(mdoc, tok, ppos, 0, &p)) + if ( ! flushed && 0 == maxargs) { + if ( ! append_text(mdoc, tok, ppos, 0, &p)) return(0); flushed = 1; } if (MDOC_MAX != (c = mdoc_find(mdoc, p))) { - if ( ! flushed && ! append_const(mdoc, tok, ppos, 0, &p)) + if ( ! flushed && ! append_text(mdoc, tok, ppos, 0, &p)) return(0); if ( ! mdoc_macro(mdoc, c, lastarg, pos, buf)) return(0); @@ -900,17 +615,18 @@ again: */ if ( ! flushed && ! mdoc_isdelim(p)) { - if ( ! append_const(mdoc, tok, ppos, 1, &p)) + if ( ! append_text(mdoc, tok, ppos, 1, &p)) return(0); flushed = 1; goto again; } else if ( ! flushed) { - if ( ! append_const(mdoc, tok, ppos, 0, &p)) + if ( ! append_text(mdoc, tok, ppos, 0, &p)) return(0); flushed = 1; } mdoc_word_alloc(mdoc, lastarg, p); + mdoc->next = MDOC_NEXT_SIBLING; goto again; /* NOTREACHED */ } @@ -919,79 +635,54 @@ again: int macro_constant(MACRO_PROT_ARGS) { - int lastarg, j, fl; + int c, lastarg, argc, sz, fl; char *args[MDOC_LINEARG_MAX]; + struct mdoc_arg argv[MDOC_LINEARG_MAX]; - if (SEC_PROLOGUE == mdoc->sec_lastn) - return(mdoc_err(mdoc, tok, ppos, ERR_SEC_PROLOGUE)); - - j = fl = 0; - lastarg = ppos; + fl = 0; if (MDOC_QUOTABLE & mdoc_macros[tok].flags) fl = ARGS_QUOTED; -again: - if (j == MDOC_LINEARG_MAX) - return(mdoc_err(mdoc, tok, lastarg, ERR_ARGS_MANY)); - - lastarg = *pos; + for (argc = 0; argc < MDOC_LINEARG_MAX; argc++) { + lastarg = *pos; + c = mdoc_argv(mdoc, tok, &argv[argc], pos, buf); + if (ARGV_EOLN == c) + break; + else if (ARGV_ARG == c) + continue; + else if (ARGV_WORD == c) + break; - switch (mdoc_args(mdoc, tok, pos, buf, fl, &args[j])) { - case (ARGS_ERROR): + mdoc_argv_free(argc, argv); return(0); - case (ARGS_WORD): - break; - case (ARGS_EOLN): - return(append_const(mdoc, tok, ppos, j, args)); - default: - abort(); - /* NOTREACHED */ } - if (MDOC_MAX != mdoc_find(mdoc, args[j])) - if ( ! mdoc_warn(mdoc, tok, lastarg, WARN_SYNTAX_MACLIKE)) - return(0); - - j++; - goto again; - /* NOTREACHED */ -} - - -int -macro_constant_argv(MACRO_PROT_ARGS) -{ - int c, lastarg, j; - struct mdoc_arg argv[MDOC_LINEARG_MAX]; - - if (SEC_PROLOGUE == mdoc->sec_lastn) - return(mdoc_err(mdoc, tok, ppos, ERR_SEC_PROLOGUE)); - - lastarg = *pos; + if (MDOC_LINEARG_MAX == argc) { + mdoc_argv_free(argc, argv); + return(mdoc_err(mdoc, tok, lastarg, ERR_ARGS_MANY)); + } - for (j = 0; j < MDOC_LINEARG_MAX; j++) { + for (sz = 0; sz + argc < MDOC_LINEARG_MAX; sz++) { lastarg = *pos; - c = mdoc_argv(mdoc, tok, &argv[j], pos, buf); - if (0 == c) + c = mdoc_args(mdoc, tok, pos, buf, fl, &args[sz]); + if (ARGS_ERROR == c) + return(0); + if (ARGS_EOLN == c) break; - else if (1 == c) - continue; - - mdoc_argv_free(j, argv); - return(0); } - if (MDOC_LINEARG_MAX == j) { - mdoc_argv_free(j, argv); + if (MDOC_LINEARG_MAX == sz + argc) { + mdoc_argv_free(argc, argv); return(mdoc_err(mdoc, tok, lastarg, ERR_ARGS_MANY)); } - c = append_constarg(mdoc, tok, ppos, j, argv); - mdoc_argv_free(j, argv); + c = append_text_argv(mdoc, tok, ppos, sz, args, argc, argv); + mdoc_argv_free(argc, argv); return(c); } +/* ARGSUSED */ int macro_obsolete(MACRO_PROT_ARGS) { @@ -84,18 +84,18 @@ const char *const __mdoc_argnames[MDOC_ARG_MAX] = { const struct mdoc_macro __mdoc_macros[MDOC_MAX] = { { NULL, 0 }, /* \" */ - { 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, MDOC_PROLOGUE }, /* Dd */ + { NULL, MDOC_PROLOGUE }, /* Dt */ + { NULL, MDOC_PROLOGUE }, /* Os */ + { macro_scoped, 0 }, /* Sh */ + { macro_scoped, 0 }, /* Ss */ { 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 */ + { macro_scoped, MDOC_EXPLICIT }, /* Bd */ + { macro_close_explicit, 0 }, /* Ed */ + { macro_scoped, MDOC_EXPLICIT }, /* Bl */ + { macro_close_explicit, 0 }, /* El */ { NULL, 0 }, /* It */ { macro_text, MDOC_CALLABLE }, /* Ad */ { NULL, 0 }, /* An */ @@ -105,74 +105,74 @@ const struct mdoc_macro __mdoc_macros[MDOC_MAX] = { { macro_text, MDOC_CALLABLE }, /* Dv */ { macro_text, MDOC_CALLABLE }, /* Er */ { macro_text, MDOC_CALLABLE }, /* Ev */ - { macro_constant_argv, 0 }, /* Ex */ + { macro_constant, 0 }, /* Ex */ { macro_text, MDOC_CALLABLE }, /* Fa */ - { NULL, 0 }, /* Fd */ + { macro_constant, 0 }, /* Fd */ { macro_text, MDOC_CALLABLE }, /* Fl */ { macro_text, MDOC_CALLABLE | MDOC_QUOTABLE }, /* Fn */ { macro_text, 0 }, /* Ft */ { macro_text, MDOC_CALLABLE }, /* Ic */ - { NULL, 0 }, /* In */ + { macro_constant, 0 }, /* In */ { macro_text, MDOC_CALLABLE }, /* Li */ { macro_constant, 0 }, /* Nd */ { NULL, 0 }, /* Nm */ - { NULL, 0 }, /* Op */ - { NULL, 0 }, /* Ot */ + { macro_scoped_line, MDOC_CALLABLE }, /* Op */ + { macro_obsolete, 0 }, /* Ot */ { macro_text, MDOC_CALLABLE }, /* Pa */ - { macro_constant_argv, 0 }, /* Rv */ - { NULL, 0 }, /* St */ + { macro_constant, 0 }, /* Rv */ + { macro_constant, 0 }, /* St */ { macro_text, MDOC_CALLABLE }, /* Va */ { macro_text, MDOC_CALLABLE }, /* Vt */ - { NULL, 0 }, /* Xr */ - { NULL, 0 }, /* %A */ - { NULL, 0 }, /* %B */ - { NULL, 0 }, /* %D */ - { NULL, 0 }, /* %I */ - { NULL, 0 }, /* %J */ - { NULL, 0 }, /* %N */ - { NULL, 0 }, /* %O */ - { NULL, 0 }, /* %P */ - { NULL, 0 }, /* %R */ - { NULL, 0 }, /* %T */ - { NULL, 0 }, /* %V */ + { macro_text, MDOC_CALLABLE }, /* Xr */ + { macro_constant, MDOC_QUOTABLE }, /* %A */ + { macro_constant, MDOC_QUOTABLE }, /* %B */ + { macro_constant, MDOC_QUOTABLE }, /* %D */ + { macro_constant, MDOC_QUOTABLE }, /* %I */ + { macro_constant, MDOC_QUOTABLE }, /* %J */ + { macro_constant, MDOC_QUOTABLE }, /* %N */ + { macro_constant, MDOC_QUOTABLE }, /* %O */ + { macro_constant, MDOC_QUOTABLE }, /* %P */ + { macro_constant, MDOC_QUOTABLE }, /* %R */ + { macro_constant, MDOC_QUOTABLE }, /* %T */ + { macro_constant, MDOC_QUOTABLE }, /* %V */ { NULL, 0 }, /* Ac */ { NULL, 0 }, /* Ao */ - { macro_scoped_pline, MDOC_CALLABLE }, /* Aq */ + { macro_scoped_line, MDOC_CALLABLE }, /* Aq */ { macro_constant, 0 }, /* At */ { NULL, 0 }, /* Bc */ { NULL, 0 }, /* Bf */ { NULL, 0 }, /* Bo */ - { macro_scoped_pline, MDOC_CALLABLE }, /* Bq */ + { macro_scoped_line, MDOC_CALLABLE }, /* Bq */ { macro_constant_delimited, 0 }, /* Bsx */ { macro_constant_delimited, 0 }, /* Bx */ - { NULL, 0 }, /* Db */ + { macro_constant, 0 }, /* Db */ { NULL, 0 }, /* Dc */ { NULL, 0 }, /* Do */ - { macro_scoped_pline, MDOC_CALLABLE }, /* Dq */ + { macro_scoped_line, MDOC_CALLABLE }, /* Dq */ { NULL, 0 }, /* Ec */ { NULL, 0 }, /* Ef */ { macro_text, MDOC_CALLABLE }, /* Em */ { NULL, 0 }, /* Eo */ { macro_constant_delimited, 0 }, /* Fx */ { macro_text, 0 }, /* Ms */ - { NULL, 0 }, /* No */ - { NULL, 0 }, /* Ns */ + { macro_constant_delimited, MDOC_CALLABLE }, /* No */ + { macro_constant_delimited, MDOC_CALLABLE }, /* Ns */ { macro_constant_delimited, 0 }, /* Nx */ { macro_constant_delimited, 0 }, /* Ox */ { NULL, 0 }, /* Pc */ { NULL, 0 }, /* Pf */ { NULL, 0 }, /* Po */ - { macro_scoped_pline, MDOC_CALLABLE }, /* Pq */ + { macro_scoped_line, MDOC_CALLABLE }, /* Pq */ { NULL, 0 }, /* Qc */ - { macro_scoped_pline, MDOC_CALLABLE }, /* Ql */ + { macro_scoped_line, MDOC_CALLABLE }, /* Ql */ { NULL, 0 }, /* Qo */ - { macro_scoped_pline, MDOC_CALLABLE }, /* Qq */ - { NULL, 0 }, /* Re */ - { NULL, 0 }, /* Rs */ + { macro_scoped_line, MDOC_CALLABLE }, /* Qq */ + { macro_scoped, MDOC_EXPLICIT }, /* Re */ + { macro_scoped, MDOC_EXPLICIT }, /* Rs */ { NULL, 0 }, /* Sc */ { NULL, 0 }, /* So */ - { macro_scoped_pline, MDOC_CALLABLE }, /* Sq */ - { NULL, 0 }, /* Sm */ + { macro_scoped_line, MDOC_CALLABLE }, /* Sq */ + { macro_constant, 0 }, /* Sm */ { macro_text, MDOC_CALLABLE }, /* Sx */ { macro_text, MDOC_CALLABLE }, /* Sy */ { macro_text, MDOC_CALLABLE }, /* Tn */ @@ -254,6 +254,7 @@ mdoc_parseln(struct mdoc *mdoc, char *buf) if ('.' != *buf) { mdoc_word_alloc(mdoc, 0, buf); + mdoc->next = MDOC_NEXT_SIBLING; return(1); } @@ -326,6 +327,13 @@ int mdoc_macro(struct mdoc *mdoc, int tok, int ppos, int *pos, char *buf) { + if (MDOC_PROLOGUE & mdoc_macros[tok].flags && + SEC_PROLOGUE == mdoc->sec_lastn) + return(macro_prologue(mdoc, tok, ppos, pos, buf)); + + if (SEC_PROLOGUE == mdoc->sec_lastn) + return(mdoc_err(mdoc, tok, ppos, ERR_SEC_PROLOGUE)); + if (NULL == (mdoc_macros[tok].fp)) { (void)mdoc_err(mdoc, tok, ppos, ERR_MACRO_NOTSUP); return(0); @@ -336,8 +344,6 @@ mdoc_macro(struct mdoc *mdoc, int tok, int ppos, int *pos, char *buf) return(0); } - /*mdoc_msg(mdoc, ppos, "calling `%s'", mdoc_macronames[tok]);*/ - return((*mdoc_macros[tok].fp)(mdoc, tok, ppos, pos, buf)); } @@ -407,63 +413,42 @@ mdoc_node_append(struct mdoc *mdoc, int pos, struct mdoc_node *p) /* NOTREACHED */ } - switch (p->type) { - case (MDOC_BODY): - p->parent = mdoc->last->parent; + switch (mdoc->next) { + case (MDOC_NEXT_SIBLING): mdoc->last->next = p; p->prev = mdoc->last; + p->parent = mdoc->last->parent; act = "sibling"; break; - - case (MDOC_HEAD): - assert(mdoc->last->type == MDOC_BLOCK); - p->parent = mdoc->last; + case (MDOC_NEXT_CHILD): mdoc->last->child = p; + p->parent = mdoc->last; act = "child"; break; - default: - switch (mdoc->last->type) { - case (MDOC_BODY): - /* FALLTHROUGH */ - case (MDOC_HEAD): - 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; - } - break; + abort(); + /* NOTREACHED */ } - mdoc_msg(mdoc, pos, "parse: %s `%s' %s %s `%s'", + mdoc_msg(mdoc, pos, "parse: %s `%s' %s of %s `%s'", nt, nn, act, ot, on); + mdoc->last = p; } -/* FIXME: deprecate paramsz, params. */ void -mdoc_head_alloc(struct mdoc *mdoc, int pos, int tok, - size_t paramsz, const char **params) +mdoc_head_alloc(struct mdoc *mdoc, int pos, int tok) { struct mdoc_node *p; assert(mdoc->first); assert(mdoc->last); - assert(mdoc->last->type == MDOC_BLOCK); - assert(mdoc->last->data.block.tok == tok); p = xcalloc(1, sizeof(struct mdoc_node)); + p->type = MDOC_HEAD; p->data.head.tok = tok; - p->data.head.sz = paramsz; - p->data.head.args = paramdup(paramsz, params); mdoc_node_append(mdoc, pos, p); } @@ -476,12 +461,6 @@ mdoc_body_alloc(struct mdoc *mdoc, int pos, int tok) assert(mdoc->first); assert(mdoc->last); - assert((mdoc->last->type == MDOC_BLOCK) || - (mdoc->last->type == MDOC_HEAD)); - if (mdoc->last->type == MDOC_BLOCK) - assert(mdoc->last->data.block.tok == tok); - else - assert(mdoc->last->data.head.tok == tok); p = xcalloc(1, sizeof(struct mdoc_node)); @@ -588,14 +567,6 @@ mdoc_text_free(struct mdoc_text *p) } -static void -mdoc_head_free(struct mdoc_head *p) -{ - - paramfree(p->sz, p->args); -} - - void mdoc_node_free(struct mdoc_node *p) { @@ -610,9 +581,6 @@ mdoc_node_free(struct mdoc_node *p) case (MDOC_BLOCK): mdoc_block_free(&p->data.block); break; - case (MDOC_HEAD): - mdoc_head_free(&p->data.head); - break; default: break; } @@ -215,7 +215,8 @@ enum mdoc_err { ERR_ARGS_EQ1, ERR_ARGS_GE1, ERR_ARGS_LE2, - ERR_ARGS_MANY + ERR_ARGS_MANY, + ERR_CHILD_HEAD }; enum mdoc_att { @@ -205,8 +205,6 @@ print_node(const struct mdoc_node *n, int indent) case (MDOC_HEAD): p = mdoc_macronames[n->data.head.tok]; t = "block-head"; - params = n->data.head.args; - sz = n->data.head.sz; break; case (MDOC_ELEM): assert(NULL == n->child); @@ -400,6 +398,9 @@ msg_err(void *arg, int tok, int col, enum mdoc_err type) case (ERR_SYNTAX_ARGMANY): lit = "syntax: too many values for macro argument"; break; + case (ERR_CHILD_HEAD): + lit = "scope context expects block-header child"; + break; default: abort(); /* NOTREACHED */ @@ -21,10 +21,17 @@ #include "mdoc.h" +enum mdoc_next { + MDOC_NEXT_SIBLING = 0, + MDOC_NEXT_CHILD +}; + struct mdoc { void *data; struct mdoc_cb cb; void *htab; + int flags; + enum mdoc_next next; struct mdoc_node *last; struct mdoc_node *first; struct mdoc_meta meta; @@ -38,6 +45,7 @@ struct mdoc_macro { #define MDOC_CALLABLE (1 << 0) #define MDOC_EXPLICIT (1 << 1) #define MDOC_QUOTABLE (1 << 2) +#define MDOC_PROLOGUE (1 << 3) }; extern const struct mdoc_macro *const mdoc_macros; @@ -58,8 +66,7 @@ void mdoc_elem_alloc(struct mdoc *, int, int, size_t, const char **); void mdoc_block_alloc(struct mdoc *, int, int, size_t, const struct mdoc_arg *); -void mdoc_head_alloc(struct mdoc *, - int, int, size_t, const char **); +void mdoc_head_alloc(struct mdoc *, int, int); void mdoc_body_alloc(struct mdoc *, int, int); void mdoc_node_free(struct mdoc_node *); void mdoc_sibling(struct mdoc *, int, struct mdoc_node **, @@ -76,12 +83,17 @@ enum mdoc_arch mdoc_atoarch(const char *); enum mdoc_att mdoc_atoatt(const char *); time_t mdoc_atotime(const char *); -int mdoc_valid(struct mdoc *, int, int, +int mdoc_valid_post(struct mdoc *, int, int); +int mdoc_valid_pre(struct mdoc *, int, int, int, const char *[], int, const struct mdoc_arg *); int mdoc_argv(struct mdoc *, int, struct mdoc_arg *, int *, char *); +#define ARGV_ERROR (-1) +#define ARGV_EOLN (0) +#define ARGV_ARG (1) +#define ARGV_WORD (2) void mdoc_argv_free(int, struct mdoc_arg *); int mdoc_args(struct mdoc *, int, int *, char *, int, char **); @@ -106,13 +118,11 @@ int macro_constant_argv(MACRO_PROT_ARGS); int macro_constant(MACRO_PROT_ARGS); int macro_constant_delimited(MACRO_PROT_ARGS); int macro_text(MACRO_PROT_ARGS); -int macro_scoped_implicit(MACRO_PROT_ARGS); -int macro_scoped_explicit(MACRO_PROT_ARGS); +int macro_scoped(MACRO_PROT_ARGS); +int macro_close_explicit(MACRO_PROT_ARGS); int macro_scoped_line(MACRO_PROT_ARGS); int macro_scoped_pline(MACRO_PROT_ARGS); -int macro_prologue_ddate(MACRO_PROT_ARGS); -int macro_prologue_dtitle(MACRO_PROT_ARGS); -int macro_prologue_os(MACRO_PROT_ARGS); +int macro_prologue(MACRO_PROT_ARGS); __END_DECLS diff --git a/prologue.c b/prologue.c new file mode 100644 index 00000000..678851b6 --- /dev/null +++ b/prologue.c @@ -0,0 +1,217 @@ +/* $Id$ */ +/* + * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the + * above copyright notice and this permission notice appear in all + * copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL + * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE + * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER + * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR + * PERFORMANCE OF THIS SOFTWARE. + */ +#include <assert.h> +#include <stdlib.h> + +#include "private.h" + +static int prologue_dt(MACRO_PROT_ARGS); +static int prologue_dd(MACRO_PROT_ARGS); +static int prologue_os(MACRO_PROT_ARGS); + +static int +prologue_dt(MACRO_PROT_ARGS) +{ + int lastarg, j; + char *args[MDOC_LINEARG_MAX]; + + if (SEC_PROLOGUE != mdoc->sec_lastn) + return(mdoc_err(mdoc, tok, ppos, ERR_SEC_NPROLOGUE)); + if (0 == mdoc->meta.date) + return(mdoc_err(mdoc, tok, ppos, ERR_SEC_PROLOGUE_OO)); + if (mdoc->meta.title[0]) + return(mdoc_err(mdoc, tok, ppos, ERR_SEC_PROLOGUE_REP)); + + j = -1; + lastarg = ppos; + +again: + if (j == MDOC_LINEARG_MAX) + return(mdoc_err(mdoc, tok, lastarg, ERR_ARGS_MANY)); + + lastarg = *pos; + + switch (mdoc_args(mdoc, tok, pos, buf, 0, &args[++j])) { + case (ARGS_EOLN): + if (mdoc->meta.title) + return(1); + if ( ! mdoc_warn(mdoc, tok, ppos, WARN_ARGS_GE1)) + return(0); + (void)xstrlcpy(mdoc->meta.title, + "UNTITLED", META_TITLE_SZ); + return(1); + case (ARGS_ERROR): + return(0); + default: + break; + } + + if (MDOC_MAX != mdoc_find(mdoc, args[j]) && ! mdoc_warn + (mdoc, tok, lastarg, WARN_SYNTAX_MACLIKE)) + return(0); + + if (0 == j) { + if (xstrlcpy(mdoc->meta.title, args[0], META_TITLE_SZ)) + goto again; + return(mdoc_err(mdoc, tok, lastarg, ERR_SYNTAX_ARGFORM)); + + } else if (1 == j) { + mdoc->meta.msec = mdoc_atomsec(args[1]); + if (MSEC_DEFAULT != mdoc->meta.msec) + goto again; + return(mdoc_err(mdoc, tok, -1, ERR_SYNTAX_ARGFORM)); + + } else if (2 == j) { + mdoc->meta.vol = mdoc_atovol(args[2]); + if (VOL_DEFAULT != mdoc->meta.vol) + goto again; + mdoc->meta.arch = mdoc_atoarch(args[2]); + if (ARCH_DEFAULT != mdoc->meta.arch) + goto again; + return(mdoc_err(mdoc, tok, lastarg, ERR_SYNTAX_ARGFORM)); + } + + return(mdoc_err(mdoc, tok, lastarg, ERR_ARGS_MANY)); +} + + +static int +prologue_os(MACRO_PROT_ARGS) +{ + int lastarg, j; + char *args[MDOC_LINEARG_MAX]; + + /* FIXME: if we use `Os' again... ? */ + + if (SEC_PROLOGUE != mdoc->sec_lastn) + return(mdoc_err(mdoc, tok, ppos, ERR_SEC_NPROLOGUE)); + if (0 == mdoc->meta.title[0]) + return(mdoc_err(mdoc, tok, ppos, ERR_SEC_PROLOGUE_OO)); + if (mdoc->meta.os[0]) + return(mdoc_err(mdoc, tok, ppos, ERR_SEC_PROLOGUE_REP)); + + j = -1; + lastarg = ppos; + +again: + if (j == MDOC_LINEARG_MAX) + return(mdoc_err(mdoc, tok, lastarg, ERR_ARGS_MANY)); + + lastarg = *pos; + + switch (mdoc_args(mdoc, tok, pos, buf, + ARGS_QUOTED, &args[++j])) { + case (ARGS_EOLN): + mdoc->sec_lastn = mdoc->sec_last = SEC_BODY; + return(1); + case (ARGS_ERROR): + return(0); + default: + break; + } + + if ( ! xstrlcat(mdoc->meta.os, args[j], sizeof(mdoc->meta.os))) + return(mdoc_err(mdoc, tok, lastarg, ERR_SYNTAX_ARGFORM)); + if ( ! xstrlcat(mdoc->meta.os, " ", sizeof(mdoc->meta.os))) + return(mdoc_err(mdoc, tok, lastarg, ERR_SYNTAX_ARGFORM)); + + goto again; + /* NOTREACHED */ +} + + +static int +prologue_dd(MACRO_PROT_ARGS) +{ + int lastarg, j; + char *args[MDOC_LINEARG_MAX], date[64]; + + if (SEC_PROLOGUE != mdoc->sec_lastn) + return(mdoc_err(mdoc, tok, ppos, ERR_SEC_NPROLOGUE)); + if (mdoc->meta.title[0]) + return(mdoc_err(mdoc, tok, ppos, ERR_SEC_PROLOGUE_OO)); + if (mdoc->meta.date) + return(mdoc_err(mdoc, tok, ppos, ERR_SEC_PROLOGUE_REP)); + + j = -1; + date[0] = 0; + lastarg = ppos; + +again: + if (j == MDOC_LINEARG_MAX) + return(mdoc_err(mdoc, tok, lastarg, ERR_ARGS_MANY)); + + lastarg = *pos; + switch (mdoc_args(mdoc, tok, pos, buf, 0, &args[++j])) { + case (ARGS_EOLN): + if (mdoc->meta.date) + return(1); + mdoc->meta.date = mdoc_atotime(date); + if (mdoc->meta.date) + return(1); + return(mdoc_err(mdoc, tok, ppos, ERR_SYNTAX_ARGFORM)); + case (ARGS_ERROR): + return(0); + default: + break; + } + + if (MDOC_MAX != mdoc_find(mdoc, args[j]) && ! mdoc_warn + (mdoc, tok, lastarg, WARN_SYNTAX_MACLIKE)) + return(0); + + if (0 == j) { + if (xstrcmp("$Mdocdate$", args[j])) { + mdoc->meta.date = time(NULL); + goto again; + } else if (xstrcmp("$Mdocdate:", args[j])) + goto again; + } else if (4 == j) + if ( ! xstrcmp("$", args[j])) + goto again; + + if ( ! xstrlcat(date, args[j], sizeof(date))) + return(mdoc_err(mdoc, tok, lastarg, ERR_SYNTAX_ARGFORM)); + if ( ! xstrlcat(date, " ", sizeof(date))) + return(mdoc_err(mdoc, tok, lastarg, ERR_SYNTAX_ARGFORM)); + + goto again; + /* NOTREACHED */ +} + + +int +macro_prologue(MACRO_PROT_ARGS) +{ + + switch (tok) { + case (MDOC_Dt): + return(prologue_dt(mdoc, tok, ppos, pos, buf)); + case (MDOC_Dd): + return(prologue_dd(mdoc, tok, ppos, pos, buf)); + case (MDOC_Os): + return(prologue_os(mdoc, tok, ppos, pos, buf)); + default: + break; + } + + abort(); + /* NOTREACHED */ +} + @@ -21,146 +21,158 @@ #include "private.h" +/* FIXME: `.St' can only have one argument set. */ -typedef int (*v_sz)(struct mdoc *, int, int, int); -typedef int (*v_extra)(struct mdoc *, int, int, int, +typedef int (*v_args_sz)(struct mdoc *, int, int, int); +typedef int (*v_args)(struct mdoc *, int, int, int, const char *[], int, const struct mdoc_arg *); +typedef int (*v_post)(struct mdoc *, int, int); -static int assert_eq0(struct mdoc *, int, int, int); -static int assert_le1(struct mdoc *, int, int, int); -static int need_eq0(struct mdoc *, int, int, int); -static int need_eq1(struct mdoc *, int, int, int); -static int need_ge1(struct mdoc *, int, int, int); -static int need_le2(struct mdoc *, int, int, int); -static int want_eq0(struct mdoc *, int, int, int); -static int want_ge1(struct mdoc *, int, int, int); -static int v_Sh(struct mdoc *, int, int, int, +static int need_head_child(struct mdoc *, int, int); + +static int assert_eq0(struct mdoc *, int, int, int); +static int assert_le1(struct mdoc *, int, int, int); +static int need_eq0(struct mdoc *, int, int, int); +static int need_eq1(struct mdoc *, int, int, int); +static int need_ge1(struct mdoc *, int, int, int); +static int need_le2(struct mdoc *, int, int, int); +static int want_eq0(struct mdoc *, int, int, int); +static int want_ge1(struct mdoc *, int, int, int); + +static int args_ref(struct mdoc *, int, int, int, + const char *[], int, const struct mdoc_arg *); +static int args_bool(struct mdoc *, int, int, int, + const char *[], int, const struct mdoc_arg *); +static int args_Sh(struct mdoc *, int, int, int, const char *[], int, const struct mdoc_arg *); -static int v_Bd(struct mdoc *, int, int, int, +static int args_blocknest(struct mdoc *, int, int, int, const char *[], int, const struct mdoc_arg *); -static int v_At(struct mdoc *, int, int, int, +static int args_At(struct mdoc *, int, int, int, + const char *[], int, const struct mdoc_arg *); +static int args_Xr(struct mdoc *, int, int, int, const char *[], int, const struct mdoc_arg *); struct valids { - v_sz sz; - v_extra extra; + v_args_sz sz; + v_args args; + v_post post; }; const struct valids mdoc_valids[MDOC_MAX] = { - { NULL, NULL }, /* \" */ - { NULL, NULL }, /* Dd */ - { NULL, NULL }, /* Dt */ - { NULL, NULL }, /* Os */ - { need_ge1, v_Sh }, /* Sh */ - { need_ge1, NULL }, /* Ss */ - { want_eq0, NULL }, /* Pp */ - { NULL, NULL }, /* D1 */ - { NULL, NULL }, /* Dl */ - { NULL, v_Bd }, /* Bd */ - { NULL, NULL }, /* Ed */ - { NULL, NULL }, /* Bl */ - { NULL, NULL }, /* El */ - { NULL, NULL }, /* It */ - { need_ge1, NULL }, /* Ad */ - { NULL, NULL }, /* An */ - { NULL, NULL }, /* Ar */ - { need_ge1, NULL }, /* Cd */ - { NULL, NULL }, /* Cm */ - { need_ge1, NULL }, /* Dv */ - { need_ge1, NULL }, /* Er */ - { need_ge1, NULL }, /* Ev */ - { NULL, NULL }, /* Ex */ - { need_ge1, NULL }, /* Fa */ - { NULL, NULL }, /* Fd */ - { NULL, NULL }, /* Fl */ - { need_ge1, NULL }, /* Fn */ - { want_ge1, NULL }, /* Ft */ - { need_ge1, NULL }, /* Ic */ - { NULL, NULL }, /* In */ - { want_ge1, NULL }, /* Li */ - { want_ge1, NULL }, /* Nd */ - { NULL, NULL }, /* Nm */ - { NULL, NULL }, /* Op */ - { NULL, NULL }, /* Ot */ - { want_ge1, NULL }, /* Pa */ - { NULL, NULL }, /* Rv */ - { NULL, NULL }, /* St */ - { need_ge1, NULL }, /* Va */ - { need_ge1, NULL }, /* Vt */ - { NULL, NULL }, /* Xr */ - { NULL, NULL }, /* %A */ - { NULL, NULL }, /* %B */ - { NULL, NULL }, /* %D */ - { NULL, NULL }, /* %I */ - { NULL, NULL }, /* %J */ - { NULL, NULL }, /* %N */ - { NULL, NULL }, /* %O */ - { NULL, NULL }, /* %P */ - { NULL, NULL }, /* %R */ - { NULL, NULL }, /* %T */ - { NULL, NULL }, /* %V */ - { NULL, NULL }, /* Ac */ - { NULL, NULL }, /* Ao */ - { NULL, NULL }, /* Aq */ - { need_le2, v_At }, /* At */ - { NULL, NULL }, /* Bc */ - { NULL, NULL }, /* Bf */ - { NULL, NULL }, /* Bo */ - { NULL, NULL }, /* Bq */ - { assert_le1, NULL }, /* Bsx */ - { assert_le1, NULL }, /* Bx */ - { NULL, NULL }, /* Db */ - { NULL, NULL }, /* Dc */ - { NULL, NULL }, /* Do */ - { NULL, NULL }, /* Dq */ - { NULL, NULL }, /* Ec */ - { NULL, NULL }, /* Ef */ - { need_ge1, NULL }, /* Em */ - { NULL, NULL }, /* Eo */ - { assert_le1, NULL }, /* Fx */ - { want_ge1, NULL }, /* Ms */ - { NULL, NULL }, /* No */ - { NULL, NULL }, /* Ns */ - { assert_le1, NULL }, /* Nx */ - { assert_le1, NULL }, /* Ox */ - { NULL, NULL }, /* Pc */ - { NULL, NULL }, /* Pf */ - { NULL, NULL }, /* Po */ - { NULL, NULL }, /* Pq */ - { NULL, NULL }, /* Qc */ - { NULL, NULL }, /* Ql */ - { NULL, NULL }, /* Qo */ - { NULL, NULL }, /* Qq */ - { NULL, NULL }, /* Re */ - { NULL, NULL }, /* Rs */ - { NULL, NULL }, /* Sc */ - { NULL, NULL }, /* So */ - { NULL, NULL }, /* Sq */ - { NULL, NULL }, /* Sm */ - { need_ge1, NULL }, /* Sx */ - { need_ge1, NULL }, /* Sy */ - { want_ge1, NULL }, /* Tn */ - { assert_eq0, NULL }, /* Ux */ - { NULL, NULL }, /* Xc */ - { NULL, NULL }, /* Xo */ - { NULL, NULL }, /* Fo */ - { NULL, NULL }, /* Fc */ - { NULL, NULL }, /* Oo */ - { NULL, NULL }, /* Oc */ - { NULL, NULL }, /* Bk */ - { NULL, NULL }, /* Ek */ - { need_eq0, NULL }, /* Bt */ - { need_eq1, NULL }, /* Hf */ - { NULL, NULL }, /* Fr */ - { need_eq0, NULL }, /* Ud */ + { NULL, NULL, NULL }, /* \" */ + { NULL, NULL, NULL }, /* Dd */ /* TODO */ + { NULL, NULL, NULL }, /* Dt */ /* TODO */ + { NULL, NULL, NULL }, /* Os */ /* TODO */ + { NULL, args_Sh, NULL }, /* Sh */ + { NULL, NULL, NULL }, /* Ss */ + { want_eq0, NULL, NULL }, /* Pp */ + { NULL, args_blocknest, need_head_child }, /* D1 */ + { NULL, args_blocknest, need_head_child }, /* Dl */ + { NULL, args_blocknest, NULL }, /* Bd */ + { NULL, NULL, NULL }, /* Ed */ + { NULL, NULL, NULL }, /* Bl */ + { NULL, NULL, NULL }, /* El */ + { NULL, NULL, NULL }, /* It */ + { need_ge1, NULL, NULL }, /* Ad */ + { NULL, NULL, NULL }, /* An */ + { NULL, NULL, NULL }, /* Ar */ + { need_ge1, NULL, NULL }, /* Cd */ + { NULL, NULL, NULL }, /* Cm */ + { need_ge1, NULL, NULL }, /* Dv */ + { need_ge1, NULL, NULL }, /* Er */ + { need_ge1, NULL, NULL }, /* Ev */ + { NULL, NULL, NULL }, /* Ex */ + { need_ge1, NULL, NULL }, /* Fa */ + { NULL, NULL, NULL }, /* Fd */ + { NULL, NULL, NULL }, /* Fl */ + { need_ge1, NULL, NULL }, /* Fn */ + { want_ge1, NULL, NULL }, /* Ft */ + { need_ge1, NULL, NULL }, /* Ic */ + { need_eq1, NULL, NULL }, /* In */ + { want_ge1, NULL, NULL }, /* Li */ + { want_ge1, NULL, NULL }, /* Nd */ + { NULL, NULL, NULL }, /* Nm */ + { NULL, NULL, NULL }, /* Op */ + { NULL, NULL, NULL }, /* Ot */ + { want_ge1, NULL, NULL }, /* Pa */ + { NULL, NULL, NULL }, /* Rv */ + { NULL, NULL, NULL }, /* St */ + { need_ge1, NULL, NULL }, /* Va */ + { need_ge1, NULL, NULL }, /* Vt */ + { need_le2, args_Xr, NULL }, /* Xr */ + { need_ge1, args_ref, NULL }, /* %A */ + { need_ge1, args_ref, NULL }, /* %B */ + { need_ge1, args_ref, NULL }, /* %D */ + { need_ge1, args_ref, NULL }, /* %I */ + { need_ge1, args_ref, NULL }, /* %J */ + { need_ge1, args_ref, NULL }, /* %N */ + { need_ge1, args_ref, NULL }, /* %O */ + { need_ge1, args_ref, NULL }, /* %P */ + { need_ge1, args_ref, NULL }, /* %R */ + { need_ge1, args_ref, NULL }, /* %T */ + { need_ge1, args_ref, NULL }, /* %V */ + { NULL, NULL, NULL }, /* Ac */ + { NULL, NULL, NULL }, /* Ao */ + { NULL, NULL, NULL }, /* Aq */ + { need_le2, args_At, NULL }, /* At */ + { NULL, NULL, NULL }, /* Bc */ + { NULL, NULL, NULL }, /* Bf */ + { NULL, NULL, NULL }, /* Bo */ + { NULL, NULL, NULL }, /* Bq */ + { assert_le1, NULL, NULL }, /* Bsx */ + { assert_le1, NULL, NULL }, /* Bx */ + { need_eq1, args_bool, NULL }, /* Db */ + { NULL, NULL, NULL }, /* Dc */ + { NULL, NULL, NULL }, /* Do */ + { NULL, NULL, NULL }, /* Dq */ + { NULL, NULL, NULL }, /* Ec */ + { NULL, NULL, NULL }, /* Ef */ + { need_ge1, NULL, NULL }, /* Em */ + { NULL, NULL, NULL }, /* Eo */ + { assert_le1, NULL, NULL }, /* Fx */ + { want_ge1, NULL, NULL }, /* Ms */ + { NULL, NULL, NULL }, /* No */ + { NULL, NULL, NULL }, /* Ns */ + { assert_le1, NULL, NULL }, /* Nx */ + { assert_le1, NULL, NULL }, /* Ox */ + { NULL, NULL, NULL }, /* Pc */ + { NULL, NULL, NULL }, /* Pf */ + { NULL, NULL, NULL }, /* Po */ + { NULL, NULL, NULL }, /* Pq */ + { NULL, NULL, NULL }, /* Qc */ + { NULL, NULL, NULL }, /* Ql */ + { NULL, NULL, NULL }, /* Qo */ + { NULL, NULL, NULL }, /* Qq */ + { NULL, NULL, NULL }, /* Re */ + { NULL, NULL, NULL }, /* Rs */ + { NULL, NULL, NULL }, /* Sc */ + { NULL, NULL, NULL }, /* So */ + { NULL, NULL, NULL }, /* Sq */ + { need_eq1, args_bool, NULL }, /* Sm */ + { need_ge1, NULL, NULL }, /* Sx */ + { need_ge1, NULL, NULL }, /* Sy */ + { want_ge1, NULL, NULL }, /* Tn */ + { assert_eq0, NULL, NULL }, /* Ux */ + { NULL, NULL, NULL }, /* Xc */ + { NULL, NULL, NULL }, /* Xo */ + { NULL, NULL, NULL }, /* Fo */ + { NULL, NULL, NULL }, /* Fc */ + { NULL, NULL, NULL }, /* Oo */ + { NULL, NULL, NULL }, /* Oc */ + { NULL, NULL, NULL }, /* Bk */ + { NULL, NULL, NULL }, /* Ek */ + { need_eq0, NULL, NULL }, /* Bt */ + { need_eq1, NULL, NULL }, /* Hf */ + { NULL, NULL, NULL }, /* Fr */ + { need_eq0, NULL, NULL }, /* Ud */ }; static int need_le2(struct mdoc *mdoc, int tok, int pos, int sz) { - if (sz > 2) + if (sz <= 2) return(1); return(mdoc_err(mdoc, tok, pos, ERR_ARGS_LE2)); } @@ -230,7 +242,28 @@ need_ge1(struct mdoc *mdoc, int tok, int pos, int sz) static int -v_Sh(struct mdoc *mdoc, int tok, int pos, +need_head_child(struct mdoc *mdoc, int tok, int pos) +{ + struct mdoc_node *n; + + assert(mdoc->last); + n = mdoc->last; + + assert(MDOC_BLOCK == n->type); + assert(n->child); + + n = n->child; + + if (MDOC_HEAD != n->type) + return(mdoc_err(mdoc, tok, pos, ERR_CHILD_HEAD)); + if (n->child) + return(1); + return(mdoc_err(mdoc, tok, pos, ERR_CHILD_HEAD)); +} + + +static int +args_Sh(struct mdoc *mdoc, int tok, int pos, int sz, const char *args[], int argc, const struct mdoc_arg *argv) { @@ -248,7 +281,59 @@ v_Sh(struct mdoc *mdoc, int tok, int pos, static int -v_At(struct mdoc *mdoc, int tok, int pos, +args_bool(struct mdoc *mdoc, int tok, int pos, + int sz, const char *args[], + int argc, const struct mdoc_arg *argv) +{ + + assert(1 == sz); + if (xstrcmp(args[0], "on")) + return(1); + if (xstrcmp(args[0], "off")) + return(1); + return(mdoc_err(mdoc, tok, pos, ERR_SYNTAX_ARGBAD)); +} + + +static int +args_ref(struct mdoc *mdoc, int tok, int pos, + int sz, const char *args[], + int argc, const struct mdoc_arg *argv) +{ + struct mdoc_node *n; + + assert(mdoc->last); + for (n = mdoc->last ; n; n = n->parent) { + if (MDOC_BLOCK != n->type) + continue; + if (MDOC_Rs != n->data.block.tok) + break; + return(1); + } + + return(mdoc_err(mdoc, tok, pos, ERR_SCOPE_NOCTX)); +} + + +static int +args_Xr(struct mdoc *mdoc, int tok, int pos, + int sz, const char *args[], + int argc, const struct mdoc_arg *argv) +{ + + if (1 == sz) + return(1); + if (0 == sz) + return(mdoc_err(mdoc, tok, pos, ERR_ARGS_GE1)); + + if (MSEC_DEFAULT == mdoc_atomsec(args[1])) + return(mdoc_err(mdoc, tok, pos, ERR_SYNTAX_ARGFORM)); + return(1); +} + + +static int +args_At(struct mdoc *mdoc, int tok, int pos, int sz, const char *args[], int argc, const struct mdoc_arg *argv) { @@ -271,7 +356,7 @@ v_At(struct mdoc *mdoc, int tok, int pos, static int -v_Bd(struct mdoc *mdoc, int tok, int pos, +args_blocknest(struct mdoc *mdoc, int tok, int pos, int sz, const char *args[], int argc, const struct mdoc_arg *argv) { @@ -285,7 +370,7 @@ v_Bd(struct mdoc *mdoc, int tok, int pos, assert(mdoc->last); /* LINTED */ - for (node = mdoc->last->parent ; node; node = node->parent) { + for (node = mdoc->last; node; node = node->parent) { if (node->type != MDOC_BLOCK) continue; if (node->data.block.tok != MDOC_Bd) @@ -299,7 +384,7 @@ v_Bd(struct mdoc *mdoc, int tok, int pos, int -mdoc_valid(struct mdoc *mdoc, int tok, int pos, +mdoc_valid_pre(struct mdoc *mdoc, int tok, int pos, int sz, const char *args[], int argc, const struct mdoc_arg *argv) { @@ -309,9 +394,18 @@ mdoc_valid(struct mdoc *mdoc, int tok, int pos, if ( ! (*mdoc_valids[tok].sz)(mdoc, tok, pos, sz)) return(0); - if (NULL == mdoc_valids[tok].extra) + if (NULL == mdoc_valids[tok].args) return(1); - return(*mdoc_valids[tok].extra)(mdoc, - tok, pos, sz, args, argc, argv); + return((*mdoc_valids[tok].args)(mdoc, + tok, pos, sz, args, argc, argv)); } + +int +mdoc_valid_post(struct mdoc *mdoc, int tok, int pos) +{ + + if (NULL == mdoc_valids[tok].post) + return(1); + return((*mdoc_valids[tok].post)(mdoc, tok, pos)); +} |