summaryrefslogtreecommitdiff
path: root/usr.bin/mandoc
diff options
context:
space:
mode:
authorIngo Schwarze <schwarze@cvs.openbsd.org>2009-08-22 19:43:34 +0000
committerIngo Schwarze <schwarze@cvs.openbsd.org>2009-08-22 19:43:34 +0000
commit4e44fe60b2248295d7b0c1a4c9bd3b5d431bfe00 (patch)
treed978d4e72d731ebde4ac980b4c35ebe46f53ee91 /usr.bin/mandoc
parent45e35385f518ea3ad6e1f97089159d36527facaf (diff)
sync to 1.9.0: polishing the core code of mdoc macro handling
1) If a macro is not parsed, do not parse it. Of course, without parsing it, we cannot produce "macro-like parameter" warnings, but these were useless anyway. 2) If a macro is not callable, do not print a useless warning when it occurs as a parameter, just display the raw characters. 3) Below .Bl -column, check whether macros are callable. 4) Like groff, allow whitespace after the initial dot on macro lines.
Diffstat (limited to 'usr.bin/mandoc')
-rw-r--r--usr.bin/mandoc/libmdoc.h5
-rw-r--r--usr.bin/mandoc/mdoc.c44
-rw-r--r--usr.bin/mandoc/mdoc_macro.c74
3 files changed, 59 insertions, 64 deletions
diff --git a/usr.bin/mandoc/libmdoc.h b/usr.bin/mandoc/libmdoc.h
index 0105a120ce4..859d11799b5 100644
--- a/usr.bin/mandoc/libmdoc.h
+++ b/usr.bin/mandoc/libmdoc.h
@@ -1,4 +1,4 @@
-/* $Id: libmdoc.h,v 1.17 2009/07/26 22:48:41 schwarze Exp $ */
+/* $Id: libmdoc.h,v 1.18 2009/08/22 19:43:33 schwarze Exp $ */
/*
* Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
*
@@ -47,7 +47,6 @@ enum merr {
EQUOTTERM,
EMALLOC,
EARGVAL,
- ENOCALL,
EBODYPROL,
EPROLBODY,
ETEXTPROL,
@@ -88,13 +87,11 @@ enum merr {
ENOWIDTH,
EUTSNAME,
EOBS,
- EMACPARM,
EIMPBRK,
EIGNE,
EOPEN,
EQUOTPHR,
ENOCTX,
- ESPACE,
ELIB,
MERRMAX
};
diff --git a/usr.bin/mandoc/mdoc.c b/usr.bin/mandoc/mdoc.c
index 13611c01067..061b0a5d6d8 100644
--- a/usr.bin/mandoc/mdoc.c
+++ b/usr.bin/mandoc/mdoc.c
@@ -1,4 +1,4 @@
-/* $Id: mdoc.c,v 1.23 2009/08/22 17:21:24 schwarze Exp $ */
+/* $Id: mdoc.c,v 1.24 2009/08/22 19:43:33 schwarze Exp $ */
/*
* Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
*
@@ -29,7 +29,6 @@ const char *const __mdoc_merrnames[MERRMAX] = {
"unterminated quoted parameter", /* EQUOTTERM */
"system: malloc error", /* EMALLOC */
"argument parameter suggested", /* EARGVAL */
- "macro not callable", /* ENOCALL */
"macro disallowed in prologue", /* EBODYPROL */
"macro disallowed in body", /* EPROLBODY */
"text disallowed in prologue", /* ETEXTPROL */
@@ -70,13 +69,11 @@ const char *const __mdoc_merrnames[MERRMAX] = {
"superfluous width argument", /* ENOWIDTH */
"system: utsname error", /* EUTSNAME */
"obsolete macro", /* EOBS */
- "macro-like parameter", /* EMACPARM */
"end-of-line scope violation", /* EIMPBRK */
"empty macro ignored", /* EIGNE */
"unclosed explicit scope", /* EOPEN */
"unterminated quoted phrase", /* EQUOTPHR */
"closure macro without prior context", /* ENOCTX */
- "invalid whitespace after control character", /* ESPACE */
"no description found for library" /* ELIB */
};
@@ -349,7 +346,10 @@ int
mdoc_macro(struct mdoc *m, int tok,
int ln, int pp, int *pos, char *buf)
{
-
+ /*
+ * If we're in the prologue, deny "body" macros. Similarly, if
+ * we're in the body, deny prologue calls.
+ */
if (MDOC_PROLOGUE & mdoc_macros[tok].flags &&
MDOC_PBODY & m->flags)
return(mdoc_perr(m, ln, pp, EPROLBODY));
@@ -357,9 +357,6 @@ mdoc_macro(struct mdoc *m, int tok,
! (MDOC_PBODY & m->flags))
return(mdoc_perr(m, ln, pp, EBODYPROL));
- if (1 != pp && ! (MDOC_CALLABLE & mdoc_macros[tok].flags))
- return(mdoc_perr(m, ln, pp, ENOCALL));
-
return((*mdoc_macros[tok].fp)(m, tok, ln, pp, pos, buf));
}
@@ -668,7 +665,7 @@ macrowarn(struct mdoc *m, int ln, const char *buf)
int
parsemacro(struct mdoc *m, int ln, char *buf)
{
- int i, c;
+ int i, j, c, ppos;
char mac[5];
/* Empty lines are ignored. */
@@ -676,27 +673,32 @@ parsemacro(struct mdoc *m, int ln, char *buf)
if (0 == buf[1])
return(1);
- if (' ' == buf[1]) {
- i = 2;
+ i = 1;
+
+ /* Accept whitespace after the initial control char. */
+
+ if (' ' == buf[i]) {
+ i++;
while (buf[i] && ' ' == buf[i])
i++;
if (0 == buf[i])
return(1);
- return(mdoc_perr(m, ln, 1, ESPACE));
}
+ ppos = i;
+
/* Copy the first word into a nil-terminated buffer. */
- for (i = 1; i < 5; i++) {
- if (0 == (mac[i - 1] = buf[i]))
+ for (j = 0; j < 4; j++, i++) {
+ if (0 == (mac[j] = buf[i]))
break;
else if (' ' == buf[i])
break;
}
- mac[i - 1] = 0;
+ mac[j] = 0;
- if (i == 5 || i <= 2) {
+ if (j == 4 || j < 2) {
if ( ! macrowarn(m, ln, mac))
goto err;
return(1);
@@ -713,9 +715,11 @@ parsemacro(struct mdoc *m, int ln, char *buf)
while (buf[i] && ' ' == buf[i])
i++;
- /* Begin recursive parse sequence. */
-
- if ( ! mdoc_macro(m, c, ln, 1, &i, buf))
+ /*
+ * Begin recursive parse sequence. Since we're at the start of
+ * the line, we don't need to do callable/parseable checks.
+ */
+ if ( ! mdoc_macro(m, c, ln, ppos, &i, buf))
goto err;
return(1);
@@ -725,3 +729,5 @@ err: /* Error out. */
m->flags |= MDOC_HALT;
return(0);
}
+
+
diff --git a/usr.bin/mandoc/mdoc_macro.c b/usr.bin/mandoc/mdoc_macro.c
index e170bdb33b7..ccfbedcce73 100644
--- a/usr.bin/mandoc/mdoc_macro.c
+++ b/usr.bin/mandoc/mdoc_macro.c
@@ -1,4 +1,4 @@
-/* $Id: mdoc_macro.c,v 1.20 2009/08/22 15:36:58 schwarze Exp $ */
+/* $Id: mdoc_macro.c,v 1.21 2009/08/22 19:43:33 schwarze Exp $ */
/*
* Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
*
@@ -45,9 +45,10 @@ static int rew_impblock(struct mdoc *, int, int, int);
static int rew_expblock(struct mdoc *, int, int, int);
static int rew_subblock(enum mdoc_type,
struct mdoc *, int, int, int);
-static int rew_last(struct mdoc *, struct mdoc_node *);
+static int rew_last(struct mdoc *, struct mdoc_node *); /* FIXME: make const */
static int append_delims(struct mdoc *, int, int *, char *);
-static int lookup(struct mdoc *, int, int, int, const char *);
+static int lookup(struct mdoc *, int, const char *);
+static int lookup_raw(struct mdoc *, const char *);
static int swarn(struct mdoc *, enum mdoc_type, int, int,
const struct mdoc_node *);
@@ -253,17 +254,24 @@ mdoc_macroend(struct mdoc *mdoc)
}
static int
-lookup(struct mdoc *mdoc, int line, int pos, int from, const char *p)
+lookup(struct mdoc *mdoc, int from, const char *p)
+{
+
+ if ( ! (MDOC_PARSED & mdoc_macros[from].flags))
+ return(MDOC_MAX);
+ return(lookup_raw(mdoc, p));
+}
+
+
+static int
+lookup_raw(struct mdoc *mdoc, const char *p)
{
int res;
- res = mdoc_hash_find(mdoc->htab, p);
- if (MDOC_PARSED & mdoc_macros[from].flags)
- return(res);
- if (MDOC_MAX == res)
+ if (MDOC_MAX == (res = mdoc_hash_find(mdoc->htab, p)))
+ return(MDOC_MAX);
+ if (MDOC_CALLABLE & mdoc_macros[res].flags)
return(res);
- if ( ! mdoc_pwarn(mdoc, line, pos, EMACPARM))
- return(-1);
return(MDOC_MAX);
}
@@ -591,6 +599,7 @@ rew_expblock(struct mdoc *mdoc, int tok, int line, int ppos)
}
+/* FIXME: can this be merged with subblock? */
static int
rew_impblock(struct mdoc *mdoc, int tok, int line, int ppos)
{
@@ -699,9 +708,7 @@ blk_exp_close(MACRO_PROT_ARGS)
if (ARGS_EOLN == c)
break;
- if (-1 == (c = lookup(mdoc, line, lastarg, tok, p)))
- return(0);
- else if (MDOC_MAX != c) {
+ if (MDOC_MAX != (c = lookup(mdoc, tok, p))) {
if ( ! flushed) {
if ( ! rew_expblock(mdoc, tok,
line, ppos))
@@ -790,8 +797,7 @@ in_line(MACRO_PROT_ARGS)
/* Quoted words shouldn't be looked-up. */
- c = ARGS_QWORD == w ? MDOC_MAX :
- lookup(mdoc, line, la, tok, p);
+ c = ARGS_QWORD == w ? MDOC_MAX : lookup(mdoc, tok, p);
/*
* In this case, we've located a submacro and must
@@ -800,7 +806,7 @@ in_line(MACRO_PROT_ARGS)
* or raise a warning.
*/
- if (MDOC_MAX != c && -1 != c) {
+ if (MDOC_MAX != c) {
if (0 == lastpunct && ! rew_elem(mdoc, tok))
return(0);
if (nc && 0 == cnt) {
@@ -820,8 +826,7 @@ in_line(MACRO_PROT_ARGS)
if (ppos > 1)
return(1);
return(append_delims(mdoc, line, pos, buf));
- } else if (-1 == c)
- return(0);
+ }
/*
* Non-quote-enclosed punctuation. Set up our scope, if
@@ -988,10 +993,7 @@ blk_full(MACRO_PROT_ARGS)
continue;
}
- if (-1 == (c = lookup(mdoc, line, lastarg, tok, p)))
- return(0);
-
- if (MDOC_MAX == c) {
+ if (MDOC_MAX == (c = lookup(mdoc, tok, p))) {
if ( ! mdoc_word_alloc(mdoc, line, lastarg, p))
return(0);
mdoc->next = MDOC_NEXT_SIBLING;
@@ -1058,9 +1060,7 @@ blk_part_imp(MACRO_PROT_ARGS)
if (ARGS_EOLN == c)
break;
- if (-1 == (c = lookup(mdoc, line, lastarg, tok, p)))
- return(0);
- else if (MDOC_MAX == c) {
+ if (MDOC_MAX == (c = lookup(mdoc, tok, p))) {
if ( ! mdoc_word_alloc(mdoc, line, lastarg, p))
return(0);
mdoc->next = MDOC_NEXT_SIBLING;
@@ -1162,9 +1162,7 @@ blk_part_exp(MACRO_PROT_ARGS)
if (ARGS_EOLN == c)
break;
- if (-1 == (c = lookup(mdoc, line, lastarg, tok, p)))
- return(0);
- else if (MDOC_MAX != c) {
+ if (MDOC_MAX != (c = lookup(mdoc, tok, p))) {
if ( ! flushed) {
if ( ! rew_subblock(MDOC_HEAD, mdoc,
tok, line, ppos))
@@ -1282,9 +1280,7 @@ in_line_argn(MACRO_PROT_ARGS)
if (ARGS_EOLN == c)
break;
- if (-1 == (c = lookup(mdoc, line, lastarg, tok, p)))
- return(0);
- else if (MDOC_MAX != c) {
+ if (MDOC_MAX != (c = lookup(mdoc, tok, p))) {
if ( ! flushed && ! rew_elem(mdoc, tok))
return(0);
flushed = 1;
@@ -1360,15 +1356,13 @@ in_line_eoln(MACRO_PROT_ARGS)
if (ARGS_EOLN == w)
break;
- c = ARGS_QWORD == w ? MDOC_MAX :
- lookup(mdoc, line, la, tok, p);
+ c = ARGS_QWORD == w ? MDOC_MAX : lookup(mdoc, tok, p);
- if (MDOC_MAX != c && -1 != c) {
+ if (MDOC_MAX != c) {
if ( ! rew_elem(mdoc, tok))
return(0);
return(mdoc_macro(mdoc, c, line, la, pos, buf));
- } else if (-1 == c)
- return(0);
+ }
if ( ! mdoc_word_alloc(mdoc, line, la, p))
return(0);
@@ -1410,15 +1404,13 @@ phrase(struct mdoc *mdoc, int line, int ppos, char *buf)
if (ARGS_EOLN == w)
break;
- c = ARGS_QWORD == w ? MDOC_MAX :
- mdoc_hash_find(mdoc->htab, p);
+ c = ARGS_QWORD == w ? MDOC_MAX : lookup_raw(mdoc, p);
- if (MDOC_MAX != c && -1 != c) {
+ if (MDOC_MAX != c) {
if ( ! mdoc_macro(mdoc, c, line, la, &pos, buf))
return(0);
return(append_delims(mdoc, line, &pos, buf));
- } else if (-1 == c)
- return(0);
+ }
if ( ! mdoc_word_alloc(mdoc, line, la, p))
return(0);