diff options
-rw-r--r-- | Makefile | 31 | ||||
-rw-r--r-- | argv.c | 857 | ||||
-rw-r--r-- | libmdoc.h | 3 | ||||
-rw-r--r-- | main.c | 14 | ||||
-rw-r--r-- | man_action.c | 11 | ||||
-rw-r--r-- | man_hash.c | 4 | ||||
-rw-r--r-- | man_macro.c | 1 | ||||
-rw-r--r-- | mdoc.3 | 5 | ||||
-rw-r--r-- | mdoc.c | 256 | ||||
-rw-r--r-- | mdoc.h | 2 | ||||
-rw-r--r-- | mdoc_action.c | 202 | ||||
-rw-r--r-- | mdoc_argv.c | 105 | ||||
-rw-r--r-- | mdoc_macro.c | 6 | ||||
-rw-r--r-- | mdoc_strings.c (renamed from strings.c) | 4 | ||||
-rw-r--r-- | mdoc_validate.c | 113 | ||||
-rw-r--r-- | term.c (renamed from terminal.c) | 0 | ||||
-rw-r--r-- | xstd.c | 97 |
17 files changed, 428 insertions, 1283 deletions
@@ -9,21 +9,21 @@ INSTALL_DATA = install -m 0444 INSTALL_LIB = install -m 0644 INSTALL_MAN = $(INSTALL_DATA) -VERSION = 1.7.7 -VDATE = 27 March 2009 +VERSION = 1.7.8 +VDATE = 31 March 2009 VFLAGS = -DVERSION=\"$(VERSION)\" CFLAGS += -W -Wall -Wstrict-prototypes -Wno-unused-parameter -g LINTFLAGS += $(VFLAGS) CFLAGS += $(VFLAGS) -MDOCLNS = mdoc_macro.ln mdoc.ln mdoc_hash.ln strings.ln xstd.ln \ - mdoc_argv.ln mdoc_validate.ln mdoc_action.ln lib.ln \ - att.ln arch.ln vol.ln msec.ln st.ln -MDOCOBJS = mdoc_macro.o mdoc.o mdoc_hash.o strings.o xstd.o \ +MDOCLNS = mdoc_macro.ln mdoc.ln mdoc_hash.ln mdoc_strings.ln \ + mdoc_argv.ln mdoc_validate.ln mdoc_action.ln \ + lib.ln att.ln arch.ln vol.ln msec.ln st.ln +MDOCOBJS = mdoc_macro.o mdoc.o mdoc_hash.o mdoc_strings.o \ mdoc_argv.o mdoc_validate.o mdoc_action.o lib.o att.o \ arch.o vol.o msec.o st.o -MDOCSRCS = mdoc_macro.c mdoc.c mdoc_hash.c strings.c xstd.c \ +MDOCSRCS = mdoc_macro.c mdoc.c mdoc_hash.c mdoc_strings.c \ mdoc_argv.c mdoc_validate.c mdoc_action.c lib.c att.c \ arch.c vol.c msec.c st.c @@ -34,11 +34,11 @@ MANOBJS = man_macro.o man.o man_hash.o man_validate.o \ MANSRCS = man_macro.c man.c man_hash.c man_validate.c \ man_action.c -MAINLNS = main.ln mdoc_term.ln ascii.ln terminal.ln tree.ln \ +MAINLNS = main.ln mdoc_term.ln ascii.ln term.ln tree.ln \ compat.ln man_term.ln -MAINOBJS = main.o mdoc_term.o ascii.o terminal.o tree.o compat.o \ +MAINOBJS = main.o mdoc_term.o ascii.o term.o tree.o compat.o \ man_term.o -MAINSRCS = main.c mdoc_term.c ascii.c terminal.c tree.c compat.c \ +MAINSRCS = main.c mdoc_term.c ascii.c term.c tree.c compat.c \ man_term.c LLNS = llib-llibmdoc.ln llib-llibman.ln llib-lmandoc.ln @@ -135,8 +135,8 @@ mdoc_macro.o: mdoc_macro.c libmdoc.h mdoc_term.ln: mdoc_term.c term.h mdoc.h mdoc_term.o: mdoc_term.c term.h mdoc.h -strings.ln: strings.c libmdoc.h -strings.o: strings.c libmdoc.h +mdoc_strings.ln: mdoc_strings.c libmdoc.h +mdoc_strings.o: mdoc_strings.c libmdoc.h man_hash.ln: man_hash.c libman.h man_hash.o: man_hash.c libman.h @@ -153,11 +153,8 @@ man.o: man.c libman.h main.ln: main.c mdoc.h main.o: main.c mdoc.h -terminal.ln: terminal.c term.h man.h mdoc.h -terminal.o: terminal.c term.h man.h mdoc.h - -xstd.ln: xstd.c libmdoc.h -xstd.o: xstd.c libmdoc.h +term.ln: term.c term.h man.h mdoc.h +term.o: term.c term.h man.h mdoc.h mdoc_argv.ln: mdoc_argv.c libmdoc.h mdoc_argv.o: mdoc_argv.c libmdoc.h diff --git a/argv.c b/argv.c deleted file mode 100644 index b73787dc..00000000 --- a/argv.c +++ /dev/null @@ -1,857 +0,0 @@ -/* $Id$ */ -/* - * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@openbsd.org> - * - * 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 <sys/types.h> - -#include <assert.h> -#include <ctype.h> -#include <err.h> -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -#include "libmdoc.h" - -/* - * Routines to parse arguments of macros. Arguments follow the syntax - * of `-arg [val [valN...]]'. Arguments come in all types: quoted - * arguments, multiple arguments per value, no-value arguments, etc. - * - * There's no limit to the number or arguments that may be allocated. - */ - -#define ARGS_QUOTED (1 << 0) -#define ARGS_DELIM (1 << 1) -#define ARGS_TABSEP (1 << 2) -#define ARGS_ARGVLIKE (1 << 3) - -#define ARGV_NONE (1 << 0) -#define ARGV_SINGLE (1 << 1) -#define ARGV_MULTI (1 << 2) -#define ARGV_OPT_SINGLE (1 << 3) - -#define MULTI_STEP 5 - -enum mwarn { - WQUOTPARM, - WARGVPARM, - WCOLEMPTY, - WTAILWS -}; - -enum merr { - EQUOTTERM, - EARGVAL -}; - -static int argv_a2arg(int, const char *); -static int args(struct mdoc *, int, int *, - char *, int, char **); -static int argv(struct mdoc *, int, - struct mdoc_argv *, int *, char *); -static int argv_single(struct mdoc *, int, - struct mdoc_argv *, int *, char *); -static int argv_opt_single(struct mdoc *, int, - struct mdoc_argv *, int *, char *); -static int argv_multi(struct mdoc *, int, - struct mdoc_argv *, int *, char *); -static int pwarn(struct mdoc *, int, int, enum mwarn); -static int perr(struct mdoc *, int, int, enum merr); - -/* Per-argument flags. */ - -static int mdoc_argvflags[MDOC_ARG_MAX] = { - ARGV_NONE, /* MDOC_Split */ - ARGV_NONE, /* MDOC_Nosplit */ - ARGV_NONE, /* MDOC_Ragged */ - ARGV_NONE, /* MDOC_Unfilled */ - ARGV_NONE, /* MDOC_Literal */ - ARGV_NONE, /* MDOC_File */ - ARGV_SINGLE, /* MDOC_Offset */ - ARGV_NONE, /* MDOC_Bullet */ - ARGV_NONE, /* MDOC_Dash */ - ARGV_NONE, /* MDOC_Hyphen */ - ARGV_NONE, /* MDOC_Item */ - ARGV_NONE, /* MDOC_Enum */ - ARGV_NONE, /* MDOC_Tag */ - ARGV_NONE, /* MDOC_Diag */ - ARGV_NONE, /* MDOC_Hang */ - ARGV_NONE, /* MDOC_Ohang */ - ARGV_NONE, /* MDOC_Inset */ - ARGV_MULTI, /* MDOC_Column */ - ARGV_SINGLE, /* MDOC_Width */ - ARGV_NONE, /* MDOC_Compact */ - ARGV_OPT_SINGLE, /* MDOC_Std */ - ARGV_NONE, /* MDOC_Filled */ - ARGV_NONE, /* MDOC_Words */ - ARGV_NONE, /* MDOC_Emphasis */ - ARGV_NONE, /* MDOC_Symbolic */ - ARGV_NONE /* MDOC_Symbolic */ -}; - -static int mdoc_argflags[MDOC_MAX] = { - 0, /* \" */ - 0, /* Dd */ - 0, /* Dt */ - 0, /* Os */ - 0, /* Sh */ - 0, /* Ss */ - ARGS_DELIM, /* Pp */ - ARGS_DELIM, /* D1 */ - ARGS_DELIM, /* Dl */ - 0, /* Bd */ - 0, /* Ed */ - 0, /* Bl */ - 0, /* El */ - 0, /* It */ - ARGS_DELIM, /* Ad */ - ARGS_DELIM, /* An */ - ARGS_DELIM, /* Ar */ - ARGS_QUOTED, /* Cd */ - ARGS_DELIM, /* Cm */ - ARGS_DELIM, /* Dv */ - ARGS_DELIM, /* Er */ - ARGS_DELIM, /* Ev */ - 0, /* Ex */ - ARGS_DELIM | ARGS_QUOTED, /* Fa */ - 0, /* Fd */ - ARGS_DELIM, /* Fl */ - ARGS_DELIM | ARGS_QUOTED, /* Fn */ - ARGS_DELIM | ARGS_QUOTED, /* Ft */ - ARGS_DELIM, /* Ic */ - 0, /* In */ - ARGS_DELIM, /* Li */ - 0, /* Nd */ - ARGS_DELIM, /* Nm */ - ARGS_DELIM, /* Op */ - 0, /* Ot */ - ARGS_DELIM, /* Pa */ - 0, /* Rv */ - ARGS_DELIM | ARGS_ARGVLIKE, /* St */ - ARGS_DELIM, /* Va */ - ARGS_DELIM, /* Vt */ - ARGS_DELIM, /* Xr */ - ARGS_QUOTED, /* %A */ - ARGS_QUOTED, /* %B */ - ARGS_QUOTED, /* %D */ - ARGS_QUOTED, /* %I */ - ARGS_QUOTED, /* %J */ - ARGS_QUOTED, /* %N */ - ARGS_QUOTED, /* %O */ - ARGS_QUOTED, /* %P */ - ARGS_QUOTED, /* %R */ - ARGS_QUOTED, /* %T */ - ARGS_QUOTED, /* %V */ - ARGS_DELIM, /* Ac */ - 0, /* Ao */ - ARGS_DELIM, /* Aq */ - ARGS_DELIM, /* At */ - ARGS_DELIM, /* Bc */ - 0, /* Bf */ - 0, /* Bo */ - ARGS_DELIM, /* Bq */ - ARGS_DELIM, /* Bsx */ - ARGS_DELIM, /* Bx */ - 0, /* Db */ - ARGS_DELIM, /* Dc */ - 0, /* Do */ - ARGS_DELIM, /* Dq */ - ARGS_DELIM, /* Ec */ - 0, /* Ef */ - ARGS_DELIM, /* Em */ - 0, /* Eo */ - ARGS_DELIM, /* Fx */ - ARGS_DELIM, /* Ms */ - ARGS_DELIM, /* No */ - ARGS_DELIM, /* Ns */ - ARGS_DELIM, /* Nx */ - ARGS_DELIM, /* Ox */ - ARGS_DELIM, /* Pc */ - ARGS_DELIM, /* Pf */ - 0, /* Po */ - ARGS_DELIM, /* Pq */ - ARGS_DELIM, /* Qc */ - ARGS_DELIM, /* Ql */ - 0, /* Qo */ - ARGS_DELIM, /* Qq */ - 0, /* Re */ - 0, /* Rs */ - ARGS_DELIM, /* Sc */ - 0, /* So */ - ARGS_DELIM, /* Sq */ - 0, /* Sm */ - ARGS_DELIM, /* Sx */ - ARGS_DELIM, /* Sy */ - ARGS_DELIM, /* Tn */ - ARGS_DELIM, /* Ux */ - ARGS_DELIM, /* Xc */ - 0, /* Xo */ - 0, /* Fo */ - 0, /* Fc */ - 0, /* Oo */ - ARGS_DELIM, /* Oc */ - 0, /* Bk */ - 0, /* Ek */ - 0, /* Bt */ - 0, /* Hf */ - 0, /* Fr */ - 0, /* Ud */ - 0, /* Lb */ - 0, /* Ap */ - ARGS_DELIM, /* Lp */ - ARGS_DELIM | ARGS_QUOTED, /* Lk */ - ARGS_DELIM | ARGS_QUOTED, /* Mt */ - ARGS_DELIM, /* Brq */ - 0, /* Bro */ - ARGS_DELIM, /* Brc */ - ARGS_QUOTED, /* %C */ - 0, /* Es */ - 0, /* En */ - 0, /* Dx */ - ARGS_QUOTED, /* %Q */ -}; - - -/* - * Parse an argument from line text. This comes in the form of -key - * [value0...], which may either have a single mandatory value, at least - * one mandatory value, an optional single value, or no value. - */ -int -mdoc_argv(struct mdoc *mdoc, int line, int tok, - struct mdoc_arg **v, int *pos, char *buf) -{ - int i; - char *p, sv; - struct mdoc_argv tmp; - struct mdoc_arg *arg; - - if (0 == buf[*pos]) - return(ARGV_EOLN); - - assert(' ' != buf[*pos]); - - if ('-' != buf[*pos] || ARGS_ARGVLIKE & mdoc_argflags[tok]) - return(ARGV_WORD); - - /* Parse through to the first unescaped space. */ - - i = *pos; - p = &buf[++(*pos)]; - - assert(*pos > 0); - - /* LINTED */ - while (buf[*pos]) { - if (' ' == buf[*pos]) - if ('\\' != buf[*pos - 1]) - break; - (*pos)++; - } - - /* XXX - save zeroed byte, if not an argument. */ - - sv = 0; - if (buf[*pos]) { - sv = buf[*pos]; - buf[(*pos)++] = 0; - } - - (void)memset(&tmp, 0, sizeof(struct mdoc_argv)); - tmp.line = line; - tmp.pos = *pos; - - /* See if our token accepts the argument. */ - - if (MDOC_ARG_MAX == (tmp.arg = argv_a2arg(tok, p))) { - /* XXX - restore saved zeroed byte. */ - if (sv) - buf[*pos - 1] = sv; - if ( ! pwarn(mdoc, line, i, WARGVPARM)) - return(ARGV_ERROR); - return(ARGV_WORD); - } - - while (buf[*pos] && ' ' == buf[*pos]) - (*pos)++; - - if ( ! argv(mdoc, line, &tmp, pos, buf)) - return(ARGV_ERROR); - - if (NULL == (arg = *v)) { - if (NULL == (*v = calloc(1, sizeof(struct mdoc_arg)))) - err(1, "calloc"); - arg = *v; - } - - arg->argc++; - arg->argv = xrealloc(arg->argv, arg->argc * - sizeof(struct mdoc_argv)); - - (void)memcpy(&arg->argv[(int)arg->argc - 1], - &tmp, sizeof(struct mdoc_argv)); - - return(ARGV_ARG); -} - - -void -mdoc_argv_free(struct mdoc_arg *p) -{ - int i, j; - - if (NULL == p) - return; - - if (p->refcnt) { - --(p->refcnt); - if (p->refcnt) - return; - } - - assert(p->argc); - - /* LINTED */ - for (i = 0; i < (int)p->argc; i++) { - if (0 == p->argv[i].sz) - continue; - /* LINTED */ - for (j = 0; j < (int)p->argv[i].sz; j++) - free(p->argv[i].value[j]); - - free(p->argv[i].value); - } - - free(p->argv); - free(p); -} - - - -static int -perr(struct mdoc *mdoc, int line, int pos, enum merr code) -{ - char *p; - - p = NULL; - - switch (code) { - case (EQUOTTERM): - p = "unterminated quoted parameter"; - break; - case (EARGVAL): - p = "argument requires a value"; - break; - } - - assert(p); - return(mdoc_perr(mdoc, line, pos, p)); -} - - -static int -pwarn(struct mdoc *mdoc, int line, int pos, enum mwarn code) -{ - char *p; - int c; - - p = NULL; - c = WARN_SYNTAX; - - switch (code) { - case (WQUOTPARM): - p = "unexpected quoted parameter"; - break; - case (WARGVPARM): - p = "argument-like parameter"; - break; - case (WCOLEMPTY): - p = "last list column is empty"; - c = WARN_COMPAT; - break; - case (WTAILWS): - p = "trailing whitespace"; - c = WARN_COMPAT; - break; - } - - assert(p); - return(mdoc_pwarn(mdoc, line, pos, c, p)); -} - - -int -mdoc_args(struct mdoc *mdoc, int line, - int *pos, char *buf, int tok, char **v) -{ - int fl, c, i; - struct mdoc_node *n; - - fl = (0 == tok) ? 0 : mdoc_argflags[tok]; - - /* - * Override per-macro argument flags with context-specific ones. - * As of now, this is only valid for `It' depending on its list - * context. - */ - - switch (tok) { - case (MDOC_It): - for (n = mdoc->last; n; n = n->parent) - if (MDOC_BLOCK == n->type && MDOC_Bl == n->tok) - break; - - assert(n); - c = (int)(n->args ? n->args->argc : 0); - assert(c > 0); - - /* - * Using `Bl -column' adds ARGS_TABSEP to the arguments - * and invalidates ARGS_DELIM. Using `Bl -diag' allows - * for quoted arguments. - */ - - /* LINTED */ - for (i = 0; i < c; i++) { - switch (n->args->argv[i].arg) { - case (MDOC_Column): - fl |= ARGS_TABSEP; - fl &= ~ARGS_DELIM; - i = c; - break; - case (MDOC_Diag): - fl |= ARGS_QUOTED; - i = c; - break; - default: - break; - } - } - break; - default: - break; - } - - return(args(mdoc, line, pos, buf, fl, v)); -} - - -static int -args(struct mdoc *mdoc, int line, - int *pos, char *buf, int fl, char **v) -{ - int i; - char *p, *pp; - - assert(*pos > 0); - - if (0 == buf[*pos]) - return(ARGS_EOLN); - - if ('\"' == buf[*pos] && ! (fl & ARGS_QUOTED)) - if ( ! pwarn(mdoc, line, *pos, WQUOTPARM)) - return(ARGS_ERROR); - - if ( ! (fl & ARGS_ARGVLIKE) && '-' == buf[*pos]) - if ( ! pwarn(mdoc, line, *pos, WARGVPARM)) - return(ARGS_ERROR); - - /* - * If the first character is a delimiter and we're to look for - * delimited strings, then pass down the buffer seeing if it - * follows the pattern of [[::delim::][ ]+]+. - */ - - if ((fl & ARGS_DELIM) && mdoc_iscdelim(buf[*pos])) { - for (i = *pos; buf[i]; ) { - if ( ! mdoc_iscdelim(buf[i])) - break; - i++; - /* There must be at least one space... */ - if (0 == buf[i] || ' ' != buf[i]) - break; - i++; - while (buf[i] && ' ' == buf[i]) - i++; - } - if (0 == buf[i]) { - *v = &buf[*pos]; - return(ARGS_PUNCT); - } - } - - /* First parse non-quoted strings. */ - - if ('\"' != buf[*pos] || ! (ARGS_QUOTED & fl)) { - *v = &buf[*pos]; - - /* - * Thar be dragons here! If we're tab-separated, search - * ahead for either a tab or the `Ta' macro. - * If a `Ta' is detected, it must be space-buffered before and - * after. If either of these hold true, then prune out the - * extra spaces and call it an argument. - */ - - if (ARGS_TABSEP & fl) { - /* Scan ahead to unescaped tab. */ - - p = strchr(*v, '\t'); - - /* Scan ahead to unescaped `Ta'. */ - - for (pp = *v; ; pp++) { - if (NULL == (pp = strstr(pp, "Ta"))) - break; - if (pp > *v && ' ' != *(pp - 1)) - continue; - if (' ' == *(pp + 2) || 0 == *(pp + 2)) - break; - } - - /* Choose delimiter tab/Ta. */ - - if (p && pp) - p = (p < pp ? p : pp); - else if ( ! p && pp) - p = pp; - - /* Strip delimiter's preceding whitespace. */ - - if (p && p > *v) { - pp = p - 1; - while (pp > *v && ' ' == *pp) - pp--; - if (pp == *v && ' ' == *pp) - *pp = 0; - else if (' ' == *pp) - *(pp + 1) = 0; - } - - /* ...in- and proceding whitespace. */ - - if (p && ('\t' != *p)) { - *p++ = 0; - *p++ = 0; - } else if (p) - *p++ = 0; - - if (p) { - while (' ' == *p) - p++; - if (0 != *p) - *(p - 1) = 0; - *pos += (int)(p - *v); - } - - if (p && 0 == *p) - if ( ! pwarn(mdoc, line, *pos, WCOLEMPTY)) - return(0); - if (p && 0 == *p && p > *v && ' ' == *(p - 1)) - if ( ! pwarn(mdoc, line, *pos, WTAILWS)) - return(0); - - if (p) - return(ARGS_PHRASE); - - /* Configure the eoln case, too. */ - - p = strchr(*v, 0); - assert(p); - - if (p > *v && ' ' == *(p - 1)) - if ( ! pwarn(mdoc, line, *pos, WTAILWS)) - return(0); - *pos += (int)(p - *v); - - return(ARGS_PHRASE); - } - - /* Do non-tabsep look-ahead here. */ - - if ( ! (ARGS_TABSEP & fl)) - while (buf[*pos]) { - if (' ' == buf[*pos]) - if ('\\' != buf[*pos - 1]) - break; - (*pos)++; - } - - if (0 == buf[*pos]) - return(ARGS_WORD); - - buf[(*pos)++] = 0; - - if (0 == buf[*pos]) - return(ARGS_WORD); - - if ( ! (ARGS_TABSEP & fl)) - while (buf[*pos] && ' ' == buf[*pos]) - (*pos)++; - - if (buf[*pos]) - return(ARGS_WORD); - - if ( ! pwarn(mdoc, line, *pos, WTAILWS)) - return(ARGS_ERROR); - - return(ARGS_WORD); - } - - /* - * If we're a quoted string (and quoted strings are allowed), - * then parse ahead to the next quote. If none's found, it's an - * error. After, parse to the next word. - */ - - *v = &buf[++(*pos)]; - - while (buf[*pos] && '\"' != buf[*pos]) - (*pos)++; - - if (0 == buf[*pos]) { - (void)perr(mdoc, line, *pos, EQUOTTERM); - return(ARGS_ERROR); - } - - buf[(*pos)++] = 0; - if (0 == buf[*pos]) - return(ARGS_QWORD); - - while (buf[*pos] && ' ' == buf[*pos]) - (*pos)++; - - if (buf[*pos]) - return(ARGS_QWORD); - - if ( ! pwarn(mdoc, line, *pos, WTAILWS)) - return(ARGS_ERROR); - - return(ARGS_QWORD); -} - - -static int -argv_a2arg(int tok, const char *argv) -{ - - /* - * Parse an argument identifier from its text. XXX - this - * should really be table-driven to clarify the code. - * - * If you add an argument to the list, make sure that you - * register it here with its one or more macros! - */ - - switch (tok) { - case (MDOC_An): - if (xstrcmp(argv, "split")) - return(MDOC_Split); - else if (xstrcmp(argv, "nosplit")) - return(MDOC_Nosplit); - break; - - case (MDOC_Bd): - if (xstrcmp(argv, "ragged")) - return(MDOC_Ragged); - else if (xstrcmp(argv, "unfilled")) - return(MDOC_Unfilled); - else if (xstrcmp(argv, "filled")) - return(MDOC_Filled); - else if (xstrcmp(argv, "literal")) - return(MDOC_Literal); - else if (xstrcmp(argv, "file")) - return(MDOC_File); - else if (xstrcmp(argv, "offset")) - return(MDOC_Offset); - else if (xstrcmp(argv, "compact")) - return(MDOC_Compact); - break; - - case (MDOC_Bf): - if (xstrcmp(argv, "emphasis")) - return(MDOC_Emphasis); - else if (xstrcmp(argv, "literal")) - return(MDOC_Literal); - else if (xstrcmp(argv, "symbolic")) - return(MDOC_Symbolic); - break; - - case (MDOC_Bk): - if (xstrcmp(argv, "words")) - return(MDOC_Words); - break; - - case (MDOC_Bl): - if (xstrcmp(argv, "bullet")) - return(MDOC_Bullet); - else if (xstrcmp(argv, "dash")) - return(MDOC_Dash); - else if (xstrcmp(argv, "hyphen")) - return(MDOC_Hyphen); - else if (xstrcmp(argv, "item")) - return(MDOC_Item); - else if (xstrcmp(argv, "enum")) - return(MDOC_Enum); - else if (xstrcmp(argv, "tag")) - return(MDOC_Tag); - else if (xstrcmp(argv, "diag")) - return(MDOC_Diag); - else if (xstrcmp(argv, "hang")) - return(MDOC_Hang); - else if (xstrcmp(argv, "ohang")) - return(MDOC_Ohang); - else if (xstrcmp(argv, "inset")) - return(MDOC_Inset); - else if (xstrcmp(argv, "column")) - return(MDOC_Column); - else if (xstrcmp(argv, "width")) - return(MDOC_Width); - else if (xstrcmp(argv, "offset")) - return(MDOC_Offset); - else if (xstrcmp(argv, "compact")) - return(MDOC_Compact); - else if (xstrcmp(argv, "nested")) - return(MDOC_Nested); - break; - - case (MDOC_Rv): - /* FALLTHROUGH */ - case (MDOC_Ex): - if (xstrcmp(argv, "std")) - return(MDOC_Std); - break; - default: - break; - } - - return(MDOC_ARG_MAX); -} - - -static int -argv_multi(struct mdoc *mdoc, int line, - struct mdoc_argv *v, int *pos, char *buf) -{ - int c, ppos; - char *p; - - ppos = *pos; - - for (v->sz = 0; ; v->sz++) { - if ('-' == buf[*pos]) - break; - c = args(mdoc, line, pos, buf, ARGS_QUOTED, &p); - if (ARGS_ERROR == c) - return(0); - else if (ARGS_EOLN == c) - break; - - if (0 == v->sz % MULTI_STEP) - v->value = xrealloc(v->value, - (v->sz + MULTI_STEP) * sizeof(char *)); - if (NULL == (v->value[(int)v->sz] = strdup(p))) - err(1, "strdup"); - } - - if (v->sz) - return(1); - - return(perr(mdoc, line, ppos, EARGVAL)); -} - - -static int -argv_opt_single(struct mdoc *mdoc, int line, - struct mdoc_argv *v, int *pos, char *buf) -{ - int c; - char *p; - - if ('-' == buf[*pos]) - return(1); - - c = args(mdoc, line, pos, buf, ARGS_QUOTED, &p); - if (ARGS_ERROR == c) - return(0); - if (ARGS_EOLN == c) - return(1); - - v->sz = 1; - if (NULL == (v->value = calloc(1, sizeof(char *)))) - err(1, "calloc"); - if (NULL == (v->value[0] = strdup(p))) - err(1, "strdup"); - return(1); -} - - -/* - * Parse a single, mandatory value from the stream. - */ -static int -argv_single(struct mdoc *mdoc, int line, - struct mdoc_argv *v, int *pos, char *buf) -{ - int c, ppos; - char *p; - - ppos = *pos; - - c = args(mdoc, line, pos, buf, ARGS_QUOTED, &p); - if (ARGS_ERROR == c) - return(0); - if (ARGS_EOLN == c) - return(perr(mdoc, line, ppos, EARGVAL)); - - v->sz = 1; - if (NULL == (v->value = calloc(1, sizeof(char *)))) - err(1, "calloc"); - if (NULL == (v->value[0] = strdup(p))) - err(1, "strdup"); - return(1); -} - - -/* - * Determine rules for parsing arguments. Arguments can either accept - * no parameters, an optional single parameter, one parameter, or - * multiple parameters. - */ -static int -argv(struct mdoc *mdoc, int line, - struct mdoc_argv *v, int *pos, char *buf) -{ - - v->sz = 0; - v->value = NULL; - - switch (mdoc_argvflags[v->arg]) { - case (ARGV_SINGLE): - return(argv_single(mdoc, line, v, pos, buf)); - case (ARGV_MULTI): - return(argv_multi(mdoc, line, v, pos, buf)); - case (ARGV_OPT_SINGLE): - return(argv_opt_single(mdoc, line, v, pos, buf)); - default: - /* ARGV_NONE */ - break; - } - - return(1); -} @@ -148,9 +148,6 @@ int mdoc_args(struct mdoc *, int, /* FIXME: get rid of these. */ int xstrlcpys(char *, const struct mdoc_node *, size_t); -int xstrlcat(char *, const char *, size_t); -int xstrlcpy(char *, const char *, size_t); -int xstrcmp(const char *, const char *); void *xrealloc(void *, size_t); char *xstrdup(const char *); int mdoc_macroend(struct mdoc *); @@ -220,6 +220,8 @@ main(int argc, char *argv[]) if (fflags & IGN_MACRO) pflags |= MDOC_IGN_MACRO; mdoc = mdoc_alloc(&curp, pflags, &mdoccb); + if (NULL == mdoc) + errx(1, "memory exhausted"); break; } @@ -246,8 +248,10 @@ main(int argc, char *argv[]) break; if (man) man_reset(man); - if (mdoc) - mdoc_reset(mdoc); + if (mdoc && ! mdoc_reset(mdoc)) { + warnx("memory exhausted"); + break; + } argv++; } rc = NULL == *argv; @@ -273,8 +277,7 @@ version(void) { (void)printf("%s %s\n", __progname, VERSION); - exit(0); - /* NOTREACHED */ + exit(EXIT_SUCCESS); } @@ -285,8 +288,7 @@ usage(void) (void)fprintf(stderr, "usage: %s [-V] [-foption...] " "[-mformat] [-Toutput] [-Werr...]\n", __progname); - exit(1); - /* NOTREACHED */ + exit(EXIT_FAILURE); } diff --git a/man_action.c b/man_action.c index 912d2bcc..b14afa41 100644 --- a/man_action.c +++ b/man_action.c @@ -19,9 +19,7 @@ #include <sys/utsname.h> #include <assert.h> -#include <err.h> #include <errno.h> -#include <stdio.h> #include <stdlib.h> #include <string.h> @@ -111,7 +109,8 @@ post_TH(struct man *m) assert(n); if (NULL == (m->meta.title = strdup(n->string))) - return(man_verr(m, n->line, n->pos, "malloc")); + return(man_verr(m, n->line, n->pos, + "memory exhausted")); /* TITLE ->MSEC<- DATE SOURCE VOL */ @@ -139,13 +138,15 @@ post_TH(struct man *m) if (n && (n = n->next)) if (NULL == (m->meta.source = strdup(n->string))) - return(man_verr(m, n->line, n->pos, "malloc")); + return(man_verr(m, n->line, n->pos, + "memory exhausted")); /* TITLE MSEC DATE SOURCE ->VOL<- */ if (n && (n = n->next)) if (NULL == (m->meta.vol = strdup(n->string))) - return(man_verr(m, n->line, n->pos, "malloc")); + return(man_verr(m, n->line, n->pos, + "memory exhausted")); /* * The end document shouldn't have the prologue macros as part @@ -16,11 +16,7 @@ * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR * PERFORMANCE OF THIS SOFTWARE. */ -#include <assert.h> -#include <ctype.h> -#include <err.h> #include <stdlib.h> -#include <stdio.h> #include <string.h> #include "libman.h" diff --git a/man_macro.c b/man_macro.c index b3e9cc05..6447b3bb 100644 --- a/man_macro.c +++ b/man_macro.c @@ -19,7 +19,6 @@ #include <assert.h> #include <ctype.h> #include <stdlib.h> -#include <stdio.h> #include <string.h> #include "libman.h" @@ -36,7 +36,7 @@ .Vt extern const char * const * mdoc_argnames; .Ft "struct mdoc *" .Fn mdoc_alloc "void *data" "int pflags" "const struct mdoc_cb *cb" -.Ft void +.Ft int .Fn mdoc_reset "struct mdoc *mdoc" .Ft void .Fn mdoc_free "struct mdoc *mdoc" @@ -133,7 +133,8 @@ Returns NULL on failure. If non-NULL, the pointer must be freed with .It Fn mdoc_reset Reset the parser for another parse routine. After its use, .Fn mdoc_parseln -behaves as if invoked for the first time. +behaves as if invoked for the first time. If it returns 0, memory could +not be allocated. .\" LIST-ITEM .It Fn mdoc_free Free all resources of a parser. The pointer is no longer valid after @@ -18,19 +18,22 @@ */ #include <assert.h> #include <ctype.h> -#include <err.h> #include <stdarg.h> -#include <stdlib.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> #include "libmdoc.h" -/* - * Main caller in the libmdoc library. This begins the parsing routine, - * handles allocation of data, and so forth. Most of the "work" is done - * in macro.c, validate.c and action.c. - */ +enum merr { + ENOCALL, + EBODYPROL, + EPROLBODY, + ESPACE, + ETEXTPROL, + ENOBLANK, + EMALLOC +}; const char *const __mdoc_macronames[MDOC_MAX] = { "\\\"", "Dd", "Dt", "Os", @@ -85,16 +88,18 @@ const char *const __mdoc_argnames[MDOC_ARG_MAX] = { const char * const *mdoc_macronames = __mdoc_macronames; 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 *, +static void mdoc_free1(struct mdoc *); +static int mdoc_alloc1(struct mdoc *); +static struct mdoc_node *node_alloc(struct mdoc *, int, int, + int, enum mdoc_type); +static int 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 *); +static int perr(struct mdoc *, int, int, enum merr); +#define verr(m, t) perr((m), (m)->last->line, (m)->last->pos, (t)) /* * Get the first (root) node of the parse tree. @@ -115,13 +120,8 @@ mdoc_meta(const struct mdoc *m) } -/* - * Free up all resources contributed by a parse: the node tree, - * meta-data and so on. Then reallocate the root node for another - * parse. - */ -void -mdoc_reset(struct mdoc *mdoc) +static void +mdoc_free1(struct mdoc *mdoc) { if (mdoc->first) @@ -136,16 +136,38 @@ mdoc_reset(struct mdoc *mdoc) free(mdoc->meta.arch); if (mdoc->meta.vol) free(mdoc->meta.vol); +} + + +static int +mdoc_alloc1(struct mdoc *mdoc) +{ bzero(&mdoc->meta, sizeof(struct mdoc_meta)); mdoc->flags = 0; mdoc->lastnamed = mdoc->lastsec = 0; mdoc->last = calloc(1, sizeof(struct mdoc_node)); if (NULL == mdoc->last) - err(1, "calloc"); + return(0); + mdoc->first = mdoc->last; mdoc->last->type = MDOC_ROOT; mdoc->next = MDOC_NEXT_CHILD; + return(1); +} + + +/* + * Free up all resources contributed by a parse: the node tree, + * meta-data and so on. Then reallocate the root node for another + * parse. + */ +int +mdoc_reset(struct mdoc *mdoc) +{ + + mdoc_free1(mdoc); + return(mdoc_alloc1(mdoc)); } @@ -156,22 +178,9 @@ void mdoc_free(struct mdoc *mdoc) { - if (mdoc->first) - mdoc_node_freelist(mdoc->first); - if (mdoc->meta.title) - free(mdoc->meta.title); - if (mdoc->meta.os) - free(mdoc->meta.os); - if (mdoc->meta.name) - free(mdoc->meta.name); - if (mdoc->meta.arch) - free(mdoc->meta.arch); - if (mdoc->meta.vol) - free(mdoc->meta.vol); - + mdoc_free1(mdoc); if (mdoc->htab) mdoc_tokhash_free(mdoc->htab); - free(mdoc); } @@ -182,20 +191,19 @@ mdoc_alloc(void *data, int pflags, const struct mdoc_cb *cb) struct mdoc *p; if (NULL == (p = calloc(1, sizeof(struct mdoc)))) - err(1, "calloc"); + return(NULL); p->data = data; + p->htab = mdoc_tokhash_alloc(); + p->pflags = pflags; + if (cb) (void)memcpy(&p->cb, cb, sizeof(struct mdoc_cb)); - if (NULL == (p->first = calloc(1, sizeof(struct mdoc_node)))) - err(1, "calloc"); - p->last = p->first; - p->last->type = MDOC_ROOT; - p->pflags = pflags; - p->next = MDOC_NEXT_CHILD; - p->htab = mdoc_tokhash_alloc(); - return(p); + if (mdoc_alloc1(p)) + return(p); + free(p); + return(NULL); } @@ -293,24 +301,55 @@ mdoc_macro(struct mdoc *m, int tok, if (MDOC_PROLOGUE & mdoc_macros[tok].flags && SEC_PROLOGUE != m->lastnamed) - return(mdoc_perr(m, ln, pp, - "disallowed in document body")); + return(perr(m, ln, pp, EPROLBODY)); if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) && SEC_PROLOGUE == m->lastnamed) - return(mdoc_perr(m, ln, pp, - "disallowed in prologue")); + return(perr(m, ln, pp, EBODYPROL)); if (1 != pp && ! (MDOC_CALLABLE & mdoc_macros[tok].flags)) - return(mdoc_perr(m, ln, pp, "%s not callable", - mdoc_macronames[tok])); + return(perr(m, ln, pp, ENOCALL)); return((*mdoc_macros[tok].fp)(m, tok, ln, pp, pos, buf)); } static int -mdoc_node_append(struct mdoc *mdoc, struct mdoc_node *p) +perr(struct mdoc *m, int line, int pos, enum merr type) +{ + char *p; + + p = NULL; + switch (type) { + case (ENOCALL): + p = "not callable"; + break; + case (EPROLBODY): + p = "macro disallowed in document body"; + break; + case (EBODYPROL): + p = "macro disallowed in document prologue"; + break; + case (EMALLOC): + p = "memory exhausted"; + break; + case (ETEXTPROL): + p = "text disallowed in document prologue"; + break; + case (ENOBLANK): + p = "blank lines disallowed in non-literal contexts"; + break; + case (ESPACE): + p = "whitespace disallowed after delimiter"; + break; + } + assert(p); + return(mdoc_perr(m, line, pos, p)); +} + + +static int +node_append(struct mdoc *mdoc, struct mdoc_node *p) { assert(mdoc->last); @@ -372,13 +411,22 @@ mdoc_node_append(struct mdoc *mdoc, struct mdoc_node *p) static struct mdoc_node * -mdoc_node_alloc(const struct mdoc *mdoc) +node_alloc(struct mdoc *mdoc, int line, + int pos, int tok, enum mdoc_type type) { struct mdoc_node *p; - if (NULL == (p = calloc(1, sizeof(struct mdoc_node)))) - err(1, "calloc"); + if (NULL == (p = calloc(1, sizeof(struct mdoc_node)))) { + (void)verr(mdoc, EMALLOC); + return(NULL); + } + p->sec = mdoc->lastsec; + p->line = line; + p->pos = pos; + p->tok = tok; + if (MDOC_TEXT != (p->type = type)) + assert(p->tok >= 0); return(p); } @@ -389,17 +437,10 @@ mdoc_tail_alloc(struct mdoc *mdoc, int line, int pos, int tok) { struct mdoc_node *p; - assert(mdoc->first); - assert(mdoc->last); - - p = mdoc_node_alloc(mdoc); - - p->line = line; - p->pos = pos; - p->type = MDOC_TAIL; - p->tok = tok; - - return(mdoc_node_append(mdoc, p)); + p = node_alloc(mdoc, line, pos, tok, MDOC_TAIL); + if (NULL == p) + return(0); + return(node_append(mdoc, p)); } @@ -411,14 +452,10 @@ mdoc_head_alloc(struct mdoc *mdoc, int line, int pos, int tok) assert(mdoc->first); assert(mdoc->last); - p = mdoc_node_alloc(mdoc); - - p->line = line; - p->pos = pos; - p->type = MDOC_HEAD; - p->tok = tok; - - return(mdoc_node_append(mdoc, p)); + p = node_alloc(mdoc, line, pos, tok, MDOC_HEAD); + if (NULL == p) + return(0); + return(node_append(mdoc, p)); } @@ -427,17 +464,10 @@ mdoc_body_alloc(struct mdoc *mdoc, int line, int pos, int tok) { struct mdoc_node *p; - assert(mdoc->first); - assert(mdoc->last); - - p = mdoc_node_alloc(mdoc); - - p->line = line; - p->pos = pos; - p->type = MDOC_BODY; - p->tok = tok; - - return(mdoc_node_append(mdoc, p)); + p = node_alloc(mdoc, line, pos, tok, MDOC_BODY); + if (NULL == p) + return(0); + return(node_append(mdoc, p)); } @@ -447,18 +477,12 @@ mdoc_block_alloc(struct mdoc *mdoc, int line, int pos, { struct mdoc_node *p; - p = mdoc_node_alloc(mdoc); - - p->pos = pos; - p->line = line; - p->type = MDOC_BLOCK; - p->tok = tok; - p->args = args; - - if (args) + p = node_alloc(mdoc, line, pos, tok, MDOC_BLOCK); + if (NULL == p) + return(0); + if ((p->args = args)) (args->refcnt)++; - - return(mdoc_node_append(mdoc, p)); + return(node_append(mdoc, p)); } @@ -468,18 +492,12 @@ mdoc_elem_alloc(struct mdoc *mdoc, int line, int pos, { struct mdoc_node *p; - p = mdoc_node_alloc(mdoc); - - p->line = line; - p->pos = pos; - p->type = MDOC_ELEM; - p->tok = tok; - p->args = args; - - if (args) + p = node_alloc(mdoc, line, pos, tok, MDOC_ELEM); + if (NULL == p) + return(0); + if ((p->args = args)) (args->refcnt)++; - - return(mdoc_node_append(mdoc, p)); + return(node_append(mdoc, p)); } @@ -489,15 +507,14 @@ mdoc_word_alloc(struct mdoc *mdoc, { struct mdoc_node *p; - p = mdoc_node_alloc(mdoc); - - p->line = line; - p->pos = pos; - p->type = MDOC_TEXT; - if (NULL == (p->string = strdup(word))) - err(1, "strdup"); - - return(mdoc_node_append(mdoc, p)); + p = node_alloc(mdoc, line, pos, -1, MDOC_TEXT); + if (NULL == p) + return(0); + if (NULL == (p->string = strdup(word))) { + (void)verr(mdoc, EMALLOC); + return(0); + } + return(node_append(mdoc, p)); } @@ -535,12 +552,10 @@ parsetext(struct mdoc *m, int line, char *buf) { if (SEC_PROLOGUE == m->lastnamed) - return(mdoc_perr(m, line, 0, - "text disallowed in prologue")); + return(perr(m, line, 0, ETEXTPROL)); if (0 == buf[0] && ! (MDOC_LITERAL & m->flags)) - return(mdoc_perr(m, line, 0, - "blank lines only in literal context")); + return(perr(m, line, 0, ENOBLANK)); if ( ! mdoc_word_alloc(m, line, 0, buf)) return(0); @@ -554,7 +569,8 @@ static int macrowarn(struct mdoc *m, int ln, const char *buf) { if ( ! (MDOC_IGN_MACRO & m->pflags)) - return(mdoc_perr(m, ln, 1, "unknown macro: %s%s", + return(mdoc_perr(m, ln, 1, + "unknown macro: %s%s", buf, strlen(buf) > 3 ? "..." : "")); return(mdoc_pwarn(m, ln, 1, WARN_SYNTAX, "unknown macro: %s%s", @@ -584,7 +600,7 @@ parsemacro(struct mdoc *m, int ln, char *buf) i++; if (0 == buf[i]) return(1); - return(mdoc_perr(m, ln, 1, "invalid syntax")); + return(perr(m, ln, 1, ESPACE)); } if (buf[1] && '\\' == buf[1]) @@ -302,7 +302,7 @@ void mdoc_free(struct mdoc *); struct mdoc *mdoc_alloc(void *, int, const struct mdoc_cb *); /* Gets system ready for another parse. */ -void mdoc_reset(struct mdoc *); +int mdoc_reset(struct mdoc *); /* Parse a single line in a stream (boolean retval). */ int mdoc_parseln(struct mdoc *, int, char *buf); diff --git a/mdoc_action.c b/mdoc_action.c index b8592c0d..cbba4a43 100644 --- a/mdoc_action.c +++ b/mdoc_action.c @@ -19,7 +19,6 @@ #include <sys/utsname.h> #include <assert.h> -#include <err.h> #include <errno.h> #include <stdio.h> #include <stdlib.h> @@ -27,18 +26,18 @@ #include "libmdoc.h" -/* - * Actions are executed on macros after they've been post-validated: in - * other words, a macro will not be "acted upon" until all of its - * children have been filled in (post-fix order). - */ - enum mwarn { WBADSEC, WNOWIDTH, WBADDATE }; +enum merr { + ETOOLONG, + EMALLOC, + ENUMFMT +}; + #define PRE_ARGS struct mdoc *m, const struct mdoc_node *n #define POST_ARGS struct mdoc *m @@ -48,6 +47,9 @@ struct actions { }; static int pwarn(struct mdoc *, int, int, enum mwarn); +static int perr(struct mdoc *, int, int, enum merr); +static int concat(struct mdoc *, const struct mdoc_node *, + char *, size_t); static int post_ar(POST_ARGS); static int post_bl(POST_ARGS); @@ -65,7 +67,9 @@ static int post_std(POST_ARGS); static int pre_bd(PRE_ARGS); static int pre_dl(PRE_ARGS); -#define mwarn(m, t) pwarn((m), (m)->last->line, (m)->last->pos, (t)) +#define vwarn(m, t) pwarn((m), (m)->last->line, (m)->last->pos, (t)) +#define verr(m, t) perr((m), (m)->last->line, (m)->last->pos, (t)) +#define nerr(m, n, t) perr((m), (n)->line, (n)->pos, (t)) const struct actions mdoc_actions[MDOC_MAX] = { { NULL, NULL }, /* \" */ @@ -190,21 +194,28 @@ const struct actions mdoc_actions[MDOC_MAX] = { }; +#ifdef __linux__ +extern size_t strlcpy(char *, const char *, size_t); +extern size_t strlcat(char *, const char *, size_t); +#endif + + int mdoc_action_pre(struct mdoc *m, const struct mdoc_node *n) { switch (n->type) { case (MDOC_ROOT): - break; + /* FALLTHROUGH */ case (MDOC_TEXT): - break; + return(1); default: - if (NULL == mdoc_actions[m->last->tok].pre) - break; - return((*mdoc_actions[m->last->tok].pre)(m, n)); + break; } - return(1); + + if (NULL == mdoc_actions[m->last->tok].pre) + return(1); + return((*mdoc_actions[m->last->tok].pre)(m, n)); } @@ -218,19 +229,61 @@ mdoc_action_post(struct mdoc *m) switch (m->last->type) { case (MDOC_TEXT): - break; + /* FALLTHROUGH */ case (MDOC_ROOT): - break; + return(1); default: - if (NULL == mdoc_actions[m->last->tok].post) - break; - return((*mdoc_actions[m->last->tok].post)(m)); + break; + } + + if (NULL == mdoc_actions[m->last->tok].post) + return(1); + return((*mdoc_actions[m->last->tok].post)(m)); +} + + +static int +concat(struct mdoc *m, const struct mdoc_node *n, + char *buf, size_t sz) +{ + + for ( ; n; n = n->next) { + assert(MDOC_TEXT == n->type); + if (strlcat(buf, n->string, sz) >= sz) + return(nerr(m, n, ETOOLONG)); + if (NULL == n->next) + continue; + if (strlcat(buf, " ", sz) >= sz) + return(nerr(m, n, ETOOLONG)); } + return(1); } static int +perr(struct mdoc *m, int line, int pos, enum merr type) +{ + char *p; + + p = NULL; + switch (type) { + case (ENUMFMT): + p = "bad number format"; + break; + case (ETOOLONG): + p = "argument text too long"; + break; + case (EMALLOC): + p = "memory exhausted"; + break; + } + assert(p); + return(mdoc_perr(m, line, pos, p)); +} + + +static int pwarn(struct mdoc *m, int line, int pos, enum mwarn type) { char *p; @@ -238,7 +291,6 @@ pwarn(struct mdoc *m, int line, int pos, enum mwarn type) p = NULL; c = WARN_SYNTAX; - switch (type) { case (WBADSEC): p = "inappropriate document section in manual section"; @@ -251,7 +303,6 @@ pwarn(struct mdoc *m, int line, int pos, enum mwarn type) p = "malformed date syntax"; break; } - assert(p); return(mdoc_pwarn(m, line, pos, c, p)); } @@ -275,10 +326,13 @@ post_std(POST_ARGS) m->last->args->argv[0].value = calloc(1, sizeof(char *)); if (NULL == m->last->args->argv[0].value) - err(1, "calloc"); + return(verr(m, EMALLOC)); m->last->args->argv[0].sz = 1; - m->last->args->argv[0].value[0] = xstrdup(m->meta.name); + m->last->args->argv[0].value[0] = strdup(m->meta.name); + if (NULL == m->last->args->argv[0].value[0]) + return(verr(m, EMALLOC)); + return(1); } @@ -290,9 +344,10 @@ post_nm(POST_ARGS) if (m->meta.name) return(1); - - (void)xstrlcpys(buf, m->last->child, sizeof(buf)); - m->meta.name = xstrdup(buf); + if ( ! concat(m, m->last->child, buf, sizeof(buf))) + return(0); + if (NULL == (m->meta.name = strdup(buf))) + return(verr(m, EMALLOC)); return(1); } @@ -312,8 +367,8 @@ post_sh(POST_ARGS) if (MDOC_HEAD != m->last->type) return(1); - - (void)xstrlcpys(buf, m->last->child, sizeof(buf)); + if ( ! concat(m, m->last->child, buf, sizeof(buf))) + return(0); if (SEC_CUSTOM != (sec = mdoc_atosec(buf))) m->lastnamed = sec; @@ -329,7 +384,7 @@ post_sh(POST_ARGS) case (9): break; default: - return(mwarn(m, WBADSEC)); + return(vwarn(m, WBADSEC)); } break; default: @@ -362,8 +417,10 @@ post_dt(POST_ARGS) */ if (NULL == (n = m->last->child)) { - m->meta.title = xstrdup("unknown"); - m->meta.vol = xstrdup("local"); + if (NULL == (m->meta.title = strdup("unknown"))) + return(verr(m, EMALLOC)); + if (NULL == (m->meta.vol = strdup("local"))) + return(verr(m, EMALLOC)); return(post_prol(m)); } @@ -371,10 +428,12 @@ post_dt(POST_ARGS) * --> title = TITLE, volume = local, msec = 0, arch = NULL */ - m->meta.title = xstrdup(n->string); + if (NULL == (m->meta.title = strdup(n->string))) + return(verr(m, EMALLOC)); if (NULL == (n = n->next)) { - m->meta.vol = xstrdup("local"); + if (NULL == (m->meta.vol = strdup("local"))) + return(verr(m, EMALLOC)); return(post_prol(m)); } @@ -387,13 +446,14 @@ post_dt(POST_ARGS) cp = mdoc_a2msec(n->string); if (cp) { - m->meta.vol = xstrdup(cp); + if (NULL == (m->meta.vol = strdup(cp))) + return(verr(m, EMALLOC)); errno = 0; lval = strtol(n->string, &ep, 10); if (n->string[0] != '\0' && *ep == '\0') m->meta.msec = (int)lval; - } else - m->meta.vol = xstrdup(n->string); + } else if (NULL == (m->meta.vol = strdup(n->string))) + return(verr(m, EMALLOC)); if (NULL == (n = n->next)) return(post_prol(m)); @@ -408,15 +468,17 @@ post_dt(POST_ARGS) cp = mdoc_a2vol(n->string); if (cp) { free(m->meta.vol); - m->meta.vol = xstrdup(cp); + if (NULL == (m->meta.vol = strdup(cp))) + return(verr(m, EMALLOC)); n = n->next; } else { cp = mdoc_a2arch(n->string); if (NULL == cp) { free(m->meta.vol); - m->meta.vol = xstrdup(n->string); - } else - m->meta.arch = xstrdup(cp); + if (NULL == (m->meta.vol = strdup(n->string))) + return(verr(m, EMALLOC)); + } else if (NULL == (m->meta.arch = strdup(cp))) + return(verr(m, EMALLOC)); } /* Ignore any subsequent parameters... */ @@ -433,18 +495,22 @@ post_os(POST_ARGS) if (m->meta.os) free(m->meta.os); - - (void)xstrlcpys(buf, m->last->child, sizeof(buf)); + if ( ! concat(m, m->last->child, buf, sizeof(buf))) + return(0); if (0 == buf[0]) { if (-1 == uname(&utsname)) return(mdoc_err(m, "utsname")); - (void)xstrlcpy(buf, utsname.sysname, sizeof(buf)); - (void)xstrlcat(buf, " ", sizeof(buf)); - (void)xstrlcat(buf, utsname.release, sizeof(buf)); + if (strlcpy(buf, utsname.sysname, 64) >= 64) + return(verr(m, ETOOLONG)); + if (strlcat(buf, " ", 64) >= 64) + return(verr(m, ETOOLONG)); + if (strlcat(buf, utsname.release, 64) >= 64) + return(verr(m, ETOOLONG)); } - m->meta.os = xstrdup(buf); + if (NULL == (m->meta.os = strdup(buf))) + return(verr(m, EMALLOC)); m->lastnamed = m->lastsec = SEC_BODY; return(post_prol(m)); @@ -478,13 +544,14 @@ post_bl_tagwidth(struct mdoc *m) if (n) { if (MDOC_TEXT != n->type) { if (0 == (sz = (int)mdoc_macro2len(n->tok))) - if ( ! mwarn(m, WNOWIDTH)) + if ( ! vwarn(m, WNOWIDTH)) return(0); } else sz = (int)strlen(n->string) + 1; } - (void)snprintf(buf, sizeof(buf), "%dn", sz); + if (-1 == snprintf(buf, sizeof(buf), "%dn", sz)) + return(verr(m, ENUMFMT)); /* * We have to dynamically add this to the macro's argument list. @@ -493,19 +560,24 @@ post_bl_tagwidth(struct mdoc *m) n = m->last; assert(n->args); - sz = (int)(n->args->argc)++; - n->args->argv = xrealloc(n->args->argv, + + n->args->argv = realloc(n->args->argv, n->args->argc * sizeof(struct mdoc_argv)); + if (NULL == n->args->argv) + return(verr(m, EMALLOC)); + n->args->argv[sz].arg = MDOC_Width; n->args->argv[sz].line = m->last->line; n->args->argv[sz].pos = m->last->pos; n->args->argv[sz].sz = 1; n->args->argv[sz].value = calloc(1, sizeof(char *)); + if (NULL == n->args->argv[sz].value) - err(1, "calloc"); - n->args->argv[sz].value[0] = xstrdup(buf); + return(verr(m, EMALLOC)); + if (NULL == (n->args->argv[sz].value[0] = strdup(buf))) + return(verr(m, EMALLOC)); return(1); } @@ -528,7 +600,6 @@ post_bl_width(struct mdoc *m) if (i == (int)m->last->args->argc) return(1); - p = m->last->args->argv[i].value[0]; /* @@ -536,19 +607,22 @@ post_bl_width(struct mdoc *m) * the macro's width as set in share/tmac/mdoc/doc-common. */ - if (xstrcmp(p, "Ds")) + if (0 == strcmp(p, "Ds")) width = 8; else if (MDOC_MAX == (tok = mdoc_tokhash_find(m->htab, p))) return(1); else if (0 == (width = mdoc_macro2len(tok))) - return(mwarn(m, WNOWIDTH)); + return(vwarn(m, WNOWIDTH)); /* The value already exists: free and reallocate it. */ - (void)snprintf(buf, sizeof(buf), "%zun", width); + if (-1 == snprintf(buf, sizeof(buf), "%zun", width)) + return(verr(m, ENUMFMT)); free(m->last->args->argv[i].value[0]); - m->last->args->argv[i].value[0] = xstrdup(buf); + m->last->args->argv[i].value[0] = strdup(buf); + if (NULL == m->last->args->argv[i].value[0]) + return(verr(m, EMALLOC)); return(1); } @@ -619,10 +693,11 @@ post_dd(POST_ARGS) { char buf[64]; - (void)xstrlcpys(buf, m->last->child, sizeof(buf)); + if ( ! concat(m, m->last->child, buf, sizeof(buf))) + return(0); if (0 == (m->meta.date = mdoc_atotime(buf))) { - if ( ! mwarn(m, WBADDATE)) + if ( ! vwarn(m, WBADDATE)) return(0); m->meta.date = time(NULL); } @@ -681,14 +756,9 @@ pre_bd(PRE_ARGS) if (MDOC_BODY != n->type) return(1); - /* - * We ONLY enter a literal context if `Bd -literal' or `Bd - * -unfilled'. - */ - - n = n->parent; + /* Enter literal context if `Bd -literal' or * -unfilled'. */ - for (i = 0; i < (int)n->args->argc; i++) + for (n = n->parent, i = 0; i < (int)n->args->argc; i++) if (MDOC_Literal == n->args->argv[i].arg) break; else if (MDOC_Unfilled == n->args->argv[i].arg) diff --git a/mdoc_argv.c b/mdoc_argv.c index 6d0c9c33..c32747bd 100644 --- a/mdoc_argv.c +++ b/mdoc_argv.c @@ -20,7 +20,6 @@ #include <assert.h> #include <ctype.h> -#include <err.h> #include <stdlib.h> #include <stdio.h> #include <string.h> @@ -56,6 +55,7 @@ enum mwarn { enum merr { EQUOTTERM, + EMALLOC, EARGVAL }; @@ -73,6 +73,8 @@ static int argv_multi(struct mdoc *, int, static int pwarn(struct mdoc *, int, int, enum mwarn); static int perr(struct mdoc *, int, int, enum merr); +#define verr(m, t) perr((m), (m)->last->line, (m)->last->pos, (t)) + /* Per-argument flags. */ static int mdoc_argvflags[MDOC_ARG_MAX] = { @@ -294,15 +296,23 @@ mdoc_argv(struct mdoc *mdoc, int line, int tok, return(ARGV_ERROR); if (NULL == (arg = *v)) { - if (NULL == (*v = calloc(1, sizeof(struct mdoc_arg)))) - err(1, "calloc"); + *v = calloc(1, sizeof(struct mdoc_arg)); + if (NULL == *v) { + (void)verr(mdoc, EMALLOC); + return(ARGV_ERROR); + } arg = *v; } arg->argc++; - arg->argv = xrealloc(arg->argv, arg->argc * + arg->argv = realloc(arg->argv, arg->argc * sizeof(struct mdoc_argv)); + if (NULL == arg->argv) { + (void)verr(mdoc, EMALLOC); + return(ARGV_ERROR); + } + (void)memcpy(&arg->argv[(int)arg->argc - 1], &tmp, sizeof(struct mdoc_argv)); @@ -323,7 +333,6 @@ mdoc_argv_free(struct mdoc_arg *p) if (p->refcnt) return; } - assert(p->argc); /* LINTED */ @@ -349,8 +358,10 @@ perr(struct mdoc *mdoc, int line, int pos, enum merr code) char *p; p = NULL; - switch (code) { + case (EMALLOC): + p = "memory exhausted"; + break; case (EQUOTTERM): p = "unterminated quoted parameter"; break; @@ -358,7 +369,6 @@ perr(struct mdoc *mdoc, int line, int pos, enum merr code) p = "argument requires a value"; break; } - assert(p); return(mdoc_perr(mdoc, line, pos, p)); } @@ -372,7 +382,6 @@ pwarn(struct mdoc *mdoc, int line, int pos, enum mwarn code) p = NULL; c = WARN_SYNTAX; - switch (code) { case (WQUOTPARM): p = "unexpected quoted parameter"; @@ -389,7 +398,6 @@ pwarn(struct mdoc *mdoc, int line, int pos, enum mwarn code) c = WARN_COMPAT; break; } - assert(p); return(mdoc_pwarn(mdoc, line, pos, c, p)); } @@ -660,80 +668,80 @@ argv_a2arg(int tok, const char *argv) switch (tok) { case (MDOC_An): - if (xstrcmp(argv, "split")) + if (0 == strcmp(argv, "split")) return(MDOC_Split); - else if (xstrcmp(argv, "nosplit")) + else if (0 == strcmp(argv, "nosplit")) return(MDOC_Nosplit); break; case (MDOC_Bd): - if (xstrcmp(argv, "ragged")) + if (0 == strcmp(argv, "ragged")) return(MDOC_Ragged); - else if (xstrcmp(argv, "unfilled")) + else if (0 == strcmp(argv, "unfilled")) return(MDOC_Unfilled); - else if (xstrcmp(argv, "filled")) + else if (0 == strcmp(argv, "filled")) return(MDOC_Filled); - else if (xstrcmp(argv, "literal")) + else if (0 == strcmp(argv, "literal")) return(MDOC_Literal); - else if (xstrcmp(argv, "file")) + else if (0 == strcmp(argv, "file")) return(MDOC_File); - else if (xstrcmp(argv, "offset")) + else if (0 == strcmp(argv, "offset")) return(MDOC_Offset); - else if (xstrcmp(argv, "compact")) + else if (0 == strcmp(argv, "compact")) return(MDOC_Compact); break; case (MDOC_Bf): - if (xstrcmp(argv, "emphasis")) + if (0 == strcmp(argv, "emphasis")) return(MDOC_Emphasis); - else if (xstrcmp(argv, "literal")) + else if (0 == strcmp(argv, "literal")) return(MDOC_Literal); - else if (xstrcmp(argv, "symbolic")) + else if (0 == strcmp(argv, "symbolic")) return(MDOC_Symbolic); break; case (MDOC_Bk): - if (xstrcmp(argv, "words")) + if (0 == strcmp(argv, "words")) return(MDOC_Words); break; case (MDOC_Bl): - if (xstrcmp(argv, "bullet")) + if (0 == strcmp(argv, "bullet")) return(MDOC_Bullet); - else if (xstrcmp(argv, "dash")) + else if (0 == strcmp(argv, "dash")) return(MDOC_Dash); - else if (xstrcmp(argv, "hyphen")) + else if (0 == strcmp(argv, "hyphen")) return(MDOC_Hyphen); - else if (xstrcmp(argv, "item")) + else if (0 == strcmp(argv, "item")) return(MDOC_Item); - else if (xstrcmp(argv, "enum")) + else if (0 == strcmp(argv, "enum")) return(MDOC_Enum); - else if (xstrcmp(argv, "tag")) + else if (0 == strcmp(argv, "tag")) return(MDOC_Tag); - else if (xstrcmp(argv, "diag")) + else if (0 == strcmp(argv, "diag")) return(MDOC_Diag); - else if (xstrcmp(argv, "hang")) + else if (0 == strcmp(argv, "hang")) return(MDOC_Hang); - else if (xstrcmp(argv, "ohang")) + else if (0 == strcmp(argv, "ohang")) return(MDOC_Ohang); - else if (xstrcmp(argv, "inset")) + else if (0 == strcmp(argv, "inset")) return(MDOC_Inset); - else if (xstrcmp(argv, "column")) + else if (0 == strcmp(argv, "column")) return(MDOC_Column); - else if (xstrcmp(argv, "width")) + else if (0 == strcmp(argv, "width")) return(MDOC_Width); - else if (xstrcmp(argv, "offset")) + else if (0 == strcmp(argv, "offset")) return(MDOC_Offset); - else if (xstrcmp(argv, "compact")) + else if (0 == strcmp(argv, "compact")) return(MDOC_Compact); - else if (xstrcmp(argv, "nested")) + else if (0 == strcmp(argv, "nested")) return(MDOC_Nested); break; case (MDOC_Rv): /* FALLTHROUGH */ case (MDOC_Ex): - if (xstrcmp(argv, "std")) + if (0 == strcmp(argv, "std")) return(MDOC_Std); break; default: @@ -762,11 +770,16 @@ argv_multi(struct mdoc *mdoc, int line, else if (ARGS_EOLN == c) break; - if (0 == v->sz % MULTI_STEP) - v->value = xrealloc(v->value, + if (0 == v->sz % MULTI_STEP) { + v->value = realloc(v->value, (v->sz + MULTI_STEP) * sizeof(char *)); + if (NULL == v->value) { + (void)verr(mdoc, EMALLOC); + return(ARGV_ERROR); + } + } if (NULL == (v->value[(int)v->sz] = strdup(p))) - err(1, "strdup"); + return(verr(mdoc, EMALLOC)); } if (v->sz) @@ -794,9 +807,10 @@ argv_opt_single(struct mdoc *mdoc, int line, v->sz = 1; if (NULL == (v->value = calloc(1, sizeof(char *)))) - err(1, "calloc"); + return(verr(mdoc, EMALLOC)); if (NULL == (v->value[0] = strdup(p))) - err(1, "strdup"); + return(verr(mdoc, EMALLOC)); + return(1); } @@ -821,9 +835,10 @@ argv_single(struct mdoc *mdoc, int line, v->sz = 1; if (NULL == (v->value = calloc(1, sizeof(char *)))) - err(1, "calloc"); + return(verr(mdoc, EMALLOC)); if (NULL == (v->value[0] = strdup(p))) - err(1, "strdup"); + return(verr(mdoc, EMALLOC)); + return(1); } diff --git a/mdoc_macro.c b/mdoc_macro.c index cf6915d9..e2aacaab 100644 --- a/mdoc_macro.c +++ b/mdoc_macro.c @@ -24,12 +24,6 @@ #include "libmdoc.h" -/* - * This has scanning/parsing routines, each of which extract a macro and - * its arguments and parameters, then know how to progress to the next - * macro. - */ - /* FIXME: .Fl, .Ar, .Cd handling of `|'. */ enum mwarn { diff --git a/strings.c b/mdoc_strings.c index ac7446c8..cdd6c45d 100644 --- a/strings.c +++ b/mdoc_strings.c @@ -197,7 +197,7 @@ mdoc_atosec(const char *p) for (i = 0, n = secnames; n->name; n++, i++) if ( ! (n->flag & MSECNAME_META)) - if (xstrcmp(p, n->name)) + if (0 == strcmp(p, n->name)) return((enum mdoc_sec)i); return(SEC_CUSTOM); @@ -212,7 +212,7 @@ mdoc_atotime(const char *p) (void)memset(&tm, 0, sizeof(struct tm)); - if (xstrcmp(p, "$Mdocdate$")) + if (0 == strcmp(p, "$Mdocdate$")) return(time(NULL)); if ((pp = strptime(p, "$Mdocdate$", &tm)) && 0 == *pp) return(mktime(&tm)); diff --git a/mdoc_validate.c b/mdoc_validate.c index 09b88f45..0ea04225 100644 --- a/mdoc_validate.c +++ b/mdoc_validate.c @@ -22,24 +22,18 @@ #include <ctype.h> #include <stdarg.h> #include <stdlib.h> +#include <string.h> #include "libmdoc.h" /* FIXME: .Bl -diag can't have non-text children in HEAD. */ /* TODO: ignoring Pp (it's superfluous in some invocations). */ -/* - * Pre- and post-validate macros as they're parsed. Pre-validation - * occurs when the macro has been detected and its arguments parsed. - * Post-validation occurs when all child macros have also been parsed. - * In the ELEMENT case, this is simply the parameters of the macro; in - * the BLOCK case, this is the HEAD, BODY, TAIL and so on. - */ - #define PRE_ARGS struct mdoc *mdoc, const struct mdoc_node *n #define POST_ARGS struct mdoc *mdoc enum merr { + ETOOLONG, EESCAPE, EPRINT, ENODATA, @@ -82,8 +76,6 @@ struct valids { v_post *post; }; -/* Utility checks. */ - static int pwarn(struct mdoc *, int, int, enum mwarn); static int perr(struct mdoc *, int, int, enum merr); static int check_parent(PRE_ARGS, int, enum mdoc_type); @@ -102,10 +94,10 @@ static int err_child_gt(struct mdoc *, const char *, int); static int warn_child_gt(struct mdoc *, const char *, int); static int err_child_eq(struct mdoc *, const char *, int); static int warn_child_eq(struct mdoc *, const char *, int); -static inline int count_child(struct mdoc *); -static inline int warn_count(struct mdoc *, const char *, +static int count_child(struct mdoc *); +static int warn_count(struct mdoc *, const char *, int, const char *, int); -static inline int err_count(struct mdoc *, const char *, +static int err_count(struct mdoc *, const char *, int, const char *, int); static int pre_an(PRE_ARGS); static int pre_bd(PRE_ARGS); @@ -137,7 +129,6 @@ static int bwarn_ge1(POST_ARGS); static int hwarn_eq1(POST_ARGS); static int ewarn_ge1(POST_ARGS); static int ebool(POST_ARGS); - static int post_an(POST_ARGS); static int post_args(POST_ARGS); static int post_at(POST_ARGS); @@ -151,8 +142,8 @@ static int post_sh_body(POST_ARGS); static int post_sh_head(POST_ARGS); static int post_st(POST_ARGS); -#define mwarn(m, t) nwarn((m), (m)->last, (t)) -#define merr(m, t) nerr((m), (m)->last, (t)) +#define vwarn(m, t) nwarn((m), (m)->last, (t)) +#define verr(m, t) nerr((m), (m)->last, (t)) #define nwarn(m, n, t) pwarn((m), (n)->line, (n)->pos, (t)) #define nerr(m, n, t) perr((m), (n)->line, (n)->pos, (t)) @@ -319,6 +310,12 @@ const struct valids mdoc_valids[MDOC_MAX] = { }; +#ifdef __linux__ +extern size_t strlcpy(char *, const char *, size_t); +extern size_t strlcat(char *, const char *, size_t); +#endif + + int mdoc_valid_pre(struct mdoc *mdoc, const struct mdoc_node *n) @@ -384,6 +381,9 @@ perr(struct mdoc *m, int line, int pos, enum merr type) p = NULL; switch (type) { + case (ETOOLONG): + p = "text argument too long"; + break; case (EESCAPE): p = "invalid escape sequence"; break; @@ -1039,11 +1039,11 @@ post_bf(POST_ARGS) return(mdoc_err(mdoc, "text argument expected")); p = head->child->string; - if (xstrcmp(p, "Em")) + if (0 == strcmp(p, "Em")) return(1); - else if (xstrcmp(p, "Li")) + else if (0 == strcmp(p, "Li")) return(1); - else if (xstrcmp(p, "Sm")) + else if (0 == strcmp(p, "Sm")) return(1); return(mdoc_nerr(mdoc, head->child, "invalid font")); } @@ -1063,7 +1063,7 @@ post_nm(POST_ARGS) return(1); if (mdoc->meta.name) return(1); - return(merr(mdoc, ENAME)); + return(verr(mdoc, ENAME)); } @@ -1074,10 +1074,10 @@ post_at(POST_ARGS) if (NULL == mdoc->last->child) return(1); if (MDOC_TEXT != mdoc->last->child->type) - return(merr(mdoc, EATT)); + return(verr(mdoc, EATT)); if (mdoc_a2att(mdoc->last->child->string)) return(1); - return(merr(mdoc, EATT)); + return(verr(mdoc, EATT)); } @@ -1088,12 +1088,12 @@ post_an(POST_ARGS) if (mdoc->last->args) { if (NULL == mdoc->last->child) return(1); - return(merr(mdoc, ELINE)); + return(verr(mdoc, ELINE)); } if (mdoc->last->child) return(1); - return(merr(mdoc, ELINE)); + return(verr(mdoc, ELINE)); } @@ -1103,7 +1103,7 @@ post_args(POST_ARGS) if (mdoc->last->args) return(1); - return(merr(mdoc, ELINE)); + return(verr(mdoc, ELINE)); } @@ -1118,7 +1118,7 @@ post_it(POST_ARGS) n = mdoc->last->parent->parent; if (NULL == n->args) - return(merr(mdoc, ELISTTYPE)); + return(verr(mdoc, ELISTTYPE)); /* Some types require block-head, some not. */ @@ -1156,12 +1156,12 @@ post_it(POST_ARGS) } if (-1 == type) - return(merr(mdoc, ELISTTYPE)); + return(verr(mdoc, ELISTTYPE)); switch (type) { case (MDOC_Tag): if (NULL == mdoc->last->head->child) - if ( ! mwarn(mdoc, WLINE)) + if ( ! vwarn(mdoc, WLINE)) return(0); break; case (MDOC_Hang): @@ -1172,10 +1172,10 @@ post_it(POST_ARGS) /* FALLTHROUGH */ case (MDOC_Diag): if (NULL == mdoc->last->head->child) - if ( ! mwarn(mdoc, WLINE)) + if ( ! vwarn(mdoc, WLINE)) return(0); if (NULL == mdoc->last->body->child) - if ( ! mwarn(mdoc, WMULTILINE)) + if ( ! vwarn(mdoc, WMULTILINE)) return(0); break; case (MDOC_Bullet): @@ -1188,18 +1188,18 @@ post_it(POST_ARGS) /* FALLTHROUGH */ case (MDOC_Item): if (mdoc->last->head->child) - if ( ! mwarn(mdoc, WNOLINE)) + if ( ! vwarn(mdoc, WNOLINE)) return(0); if (NULL == mdoc->last->body->child) - if ( ! mwarn(mdoc, WMULTILINE)) + if ( ! vwarn(mdoc, WMULTILINE)) return(0); break; case (MDOC_Column): if (NULL == mdoc->last->head->child) - if ( ! mwarn(mdoc, WLINE)) + if ( ! vwarn(mdoc, WLINE)) return(0); if (mdoc->last->body->child) - if ( ! mwarn(mdoc, WNOMULTILINE)) + if ( ! vwarn(mdoc, WNOMULTILINE)) return(0); c = mdoc->last->child; for (i = 0; c && MDOC_HEAD == c->type; c = c->next) @@ -1248,9 +1248,9 @@ ebool(struct mdoc *mdoc) for (n = mdoc->last->child; n; n = n->next) { if (MDOC_TEXT != n->type) break; - if (xstrcmp(n->string, "on")) + if (0 == strcmp(n->string, "on")) continue; - if (xstrcmp(n->string, "off")) + if (0 == strcmp(n->string, "off")) continue; break; } @@ -1266,14 +1266,14 @@ post_root(POST_ARGS) { if (NULL == mdoc->first->child) - return(merr(mdoc, ENODATA)); + return(verr(mdoc, ENODATA)); if (SEC_PROLOGUE == mdoc->lastnamed) - return(merr(mdoc, ENOPROLOGUE)); + return(verr(mdoc, ENOPROLOGUE)); if (MDOC_BLOCK != mdoc->first->child->type) - return(merr(mdoc, ENODATA)); + return(verr(mdoc, ENODATA)); if (MDOC_Sh != mdoc->first->child->tok) - return(merr(mdoc, ENODATA)); + return(verr(mdoc, ENODATA)); return(1); } @@ -1285,7 +1285,7 @@ post_st(POST_ARGS) if (mdoc_a2st(mdoc->last->child->string)) return(1); - return(mwarn(mdoc, WBADSTAND)); + return(vwarn(mdoc, WBADSTAND)); } @@ -1317,28 +1317,29 @@ post_sh_body(POST_ARGS) */ if (NULL == (n = mdoc->last->child)) - return(mwarn(mdoc, WNAMESECINC)); + return(vwarn(mdoc, WNAMESECINC)); for ( ; n && n->next; n = n->next) { if (MDOC_ELEM == n->type && MDOC_Nm == n->tok) continue; if (MDOC_TEXT == n->type) continue; - if ( ! mwarn(mdoc, WNAMESECINC)) + if ( ! vwarn(mdoc, WNAMESECINC)) return(0); } if (MDOC_ELEM == n->type && MDOC_Nd == n->tok) return(1); - return(mwarn(mdoc, WNAMESECINC)); + return(vwarn(mdoc, WNAMESECINC)); } static int post_sh_head(POST_ARGS) { - char buf[64]; - enum mdoc_sec sec; + char buf[64]; + enum mdoc_sec sec; + const struct mdoc_node *n; /* * Process a new section. Sections are either "named" or @@ -1349,23 +1350,33 @@ post_sh_head(POST_ARGS) assert(MDOC_Sh == mdoc->last->tok); - (void)xstrlcpys(buf, mdoc->last->child, sizeof(buf)); + /* This is just concat() inlined, which is irritating. */ + + for (n = mdoc->last->child; n; n = n->next) { + assert(MDOC_TEXT == n->type); + if (strlcat(buf, n->string, 64) >= 64) + return(nerr(mdoc, n, ETOOLONG)); + if (NULL == n->next) + continue; + if (strlcat(buf, " ", 64) >= 64) + return(nerr(mdoc, n, ETOOLONG)); + } sec = mdoc_atosec(buf); /* The NAME section should always be first. */ if (SEC_BODY == mdoc->lastnamed && SEC_NAME != sec) - return(mwarn(mdoc, WSECOOO)); + return(vwarn(mdoc, WSECOOO)); if (SEC_CUSTOM == sec) return(1); /* Check for repeated or out-of-order sections. */ if (sec == mdoc->lastnamed) - return(mwarn(mdoc, WSECREP)); + return(vwarn(mdoc, WSECREP)); if (sec < mdoc->lastnamed) - return(mwarn(mdoc, WSECOOO)); + return(vwarn(mdoc, WSECOOO)); /* Check particular section/manual section conventions. */ @@ -1377,7 +1388,7 @@ post_sh_head(POST_ARGS) case (3): break; default: - return(mwarn(mdoc, WWRONGMSEC)); + return(vwarn(mdoc, WWRONGMSEC)); } break; default: diff --git a/xstd.c b/xstd.c deleted file mode 100644 index dc9b0b10..00000000 --- a/xstd.c +++ /dev/null @@ -1,97 +0,0 @@ -/* $Id$ */ -/* - * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@openbsd.org> - * - * 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 <err.h> -#include <stdlib.h> -#include <string.h> - -#include "libmdoc.h" - -#ifdef __linux__ -extern size_t strlcpy(char *, const char *, size_t); -extern size_t strlcat(char *, const char *, size_t); -#endif - -/* - * Contains wrappers for common functions to simplify their general - * usage throughout this codebase. - */ - -int -xstrcmp(const char *p1, const char *p2) -{ - - return(0 == strcmp(p1, p2)); -} - -int -xstrlcat(char *dst, const char *src, size_t sz) -{ - - return(strlcat(dst, src, sz) < sz); -} - -int -xstrlcpy(char *dst, const char *src, size_t sz) -{ - - return(strlcpy(dst, src, sz) < sz); -} - -void * -xrealloc(void *ptr, size_t sz) -{ - void *p; - - if (NULL == (p = realloc(ptr, sz))) - err(EXIT_FAILURE, "realloc"); - return(p); -} - - -char * -xstrdup(const char *p) -{ - char *pp; - - if (NULL == (pp = strdup(p))) - err(EXIT_FAILURE, "strdup"); - return(pp); -} - -int -xstrlcpys(char *buf, const struct mdoc_node *n, size_t sz) -{ - char *p; - - assert(sz > 0); - assert(buf); - *buf = 0; - - for ( ; n; n = n->next) { - assert(MDOC_TEXT == n->type); - p = n->string; - if ( ! xstrlcat(buf, p, sz)) - return(0); - if (n->next && ! xstrlcat(buf, " ", sz)) - return(0); - } - - return(1); -} |