aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorIngo Schwarze <schwarze@openbsd.org>2011-01-03 22:42:37 +0000
committerIngo Schwarze <schwarze@openbsd.org>2011-01-03 22:42:37 +0000
commit98c1b1ff061599876c28f029646bead0e9b01616 (patch)
treeb90d8dda335c222ccdc9d9a010be8c1f2df9193d
parent70bf8bf8ee386c7026a0bd4a22486dd13e3cf329 (diff)
downloadmandoc-98c1b1ff061599876c28f029646bead0e9b01616.tar.gz
mandoc-98c1b1ff061599876c28f029646bead0e9b01616.tar.zst
mandoc-98c1b1ff061599876c28f029646bead0e9b01616.zip
Unify roff macro argument parsing (in roff.c, roff_userdef()) and man macro
argument parsing (in man_argv.c, man_args()), both having different bugs, to use one common macro argument parser (in mandoc.c, mandoc_getarg()), because from the point of view of roff, man macros are just roff macros, hence their arguments are parsed in exactly the same way. While doing so, fix these bugs: * Escaped blanks (i.e. those preceded by an odd number of backslashes) were mishandled as argument separators in unquoted arguments to user-defined roff macros. * Unescaped blanks preceded by an even number of backslashes were not recognized as argument separators in unquoted arguments to man macros. * Escaped backslashes (i.e. pairs of backslashes) were not reduced to single backslashes both in unquoted and quoted arguments both to user-defined roff macros and to man macros. * Escaped quotes (i.e. pairs of quotes inside quoted arguments) were not reduced to single quotes in man macros. OK kristaps@ Note that mdoc macro argument parsing is yet another beast for no good reason and is probably afflicted by similar bugs. But i don't attempt to fix that right now because it is intricately entangled with lots of unrelated high-level mdoc(7) functionality, like delimiter handling and column list phrase handling. Disentagling that would waste too much time now.
-rw-r--r--libmandoc.h3
-rw-r--r--man_argv.c78
-rw-r--r--mandoc.c84
-rw-r--r--roff.c51
4 files changed, 99 insertions, 117 deletions
diff --git a/libmandoc.h b/libmandoc.h
index ab21a421..0e9a7492 100644
--- a/libmandoc.h
+++ b/libmandoc.h
@@ -1,4 +1,4 @@
-/* $Id: libmandoc.h,v 1.9 2010/07/18 17:00:26 schwarze Exp $ */
+/* $Id: libmandoc.h,v 1.10 2011/01/03 22:42:37 schwarze Exp $ */
/*
* Copyright (c) 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
*
@@ -24,6 +24,7 @@ void *mandoc_calloc(size_t, size_t);
char *mandoc_strdup(const char *);
void *mandoc_malloc(size_t);
void *mandoc_realloc(void *, size_t);
+char *mandoc_getarg(char **, mandocmsg, void *, int, int *);
time_t mandoc_a2time(int, const char *);
#define MTIME_CANONICAL (1 << 0)
#define MTIME_REDUCED (1 << 1)
diff --git a/man_argv.c b/man_argv.c
index 44b9a25c..37aac030 100644
--- a/man_argv.c
+++ b/man_argv.c
@@ -1,6 +1,6 @@
-/* $Id: man_argv.c,v 1.4 2010/06/19 20:46:28 kristaps Exp $ */
+/* $Id: man_argv.c,v 1.5 2011/01/03 22:42:37 schwarze Exp $ */
/*
- * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@bsd.lv>
+ * Copyright (c) 2011 Ingo Schwarze <schwarze@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
@@ -21,84 +21,24 @@
#include <sys/types.h>
#include <assert.h>
-#include <stdlib.h>
-#include <string.h>
#include "mandoc.h"
#include "libman.h"
+#include "libmandoc.h"
int
man_args(struct man *m, int line, int *pos, char *buf, char **v)
{
+ char *start;
assert(*pos);
- assert(' ' != buf[*pos]);
+ *v = start = buf + *pos;
+ assert(' ' != *start);
- if (0 == buf[*pos])
+ if ('\0' == *start)
return(ARGS_EOLN);
- *v = &buf[*pos];
-
- /*
- * Process a quoted literal. A quote begins with a double-quote
- * and ends with a double-quote NOT preceded by a double-quote.
- * Whitespace is NOT involved in literal termination.
- */
-
- if ('\"' == buf[*pos]) {
- *v = &buf[++(*pos)];
-
- for ( ; buf[*pos]; (*pos)++) {
- if ('\"' != buf[*pos])
- continue;
- if ('\"' != buf[*pos + 1])
- break;
- (*pos)++;
- }
-
- if (0 == buf[*pos]) {
- if ( ! man_pmsg(m, line, *pos, MANDOCERR_BADQUOTE))
- return(ARGS_ERROR);
- return(ARGS_QWORD);
- }
-
- buf[(*pos)++] = 0;
-
- if (0 == buf[*pos])
- return(ARGS_QWORD);
-
- while (' ' == buf[*pos])
- (*pos)++;
-
- if (0 == buf[*pos])
- if ( ! man_pmsg(m, line, *pos, MANDOCERR_EOLNSPACE))
- return(ARGS_ERROR);
-
- return(ARGS_QWORD);
- }
-
- /*
- * A non-quoted term progresses until either the end of line or
- * a non-escaped whitespace.
- */
-
- for ( ; buf[*pos]; (*pos)++)
- if (' ' == buf[*pos] && '\\' != buf[*pos - 1])
- break;
-
- if (0 == buf[*pos])
- return(ARGS_WORD);
-
- buf[(*pos)++] = 0;
-
- while (' ' == buf[*pos])
- (*pos)++;
-
- if (0 == buf[*pos])
- if ( ! man_pmsg(m, line, *pos, MANDOCERR_EOLNSPACE))
- return(ARGS_ERROR);
-
- return(ARGS_WORD);
+ *v = mandoc_getarg(v, m->msg, m->data, line, pos);
+ return('"' == *start ? ARGS_QWORD : ARGS_WORD);
}
-
diff --git a/mandoc.c b/mandoc.c
index 56a187de..4faa5a78 100644
--- a/mandoc.c
+++ b/mandoc.c
@@ -1,14 +1,15 @@
-/* $Id: mandoc.c,v 1.35 2010/09/04 20:18:53 kristaps Exp $ */
+/* $Id: mandoc.c,v 1.36 2011/01/03 22:42:37 schwarze Exp $ */
/*
* Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
+ * Copyright (c) 2011 Ingo Schwarze <schwarze@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
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS 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
@@ -283,6 +284,83 @@ mandoc_strdup(const char *ptr)
return(p);
}
+/*
+ * Parse a quoted or unquoted roff-style request or macro argument.
+ * Return a pointer to the parsed argument, which is either the original
+ * pointer or advanced by one byte in case the argument is quoted.
+ * Null-terminate the argument in place.
+ * Collapse pairs of quotes inside quoted arguments.
+ * Advance the argument pointer to the next argument,
+ * or to the null byte terminating the argument line.
+ */
+char *
+mandoc_getarg(char **cpp, mandocmsg msg, void *data, int ln, int *pos)
+{
+ char *start, *cp;
+ int quoted, pairs, white;
+
+ /* Quoting can only start with a new word. */
+ start = *cpp;
+ if ('"' == *start) {
+ quoted = 1;
+ start++;
+ } else
+ quoted = 0;
+
+ pairs = 0;
+ white = 0;
+ for (cp = start; '\0' != *cp; cp++) {
+ /* Move left after quoted quotes and escaped backslashes. */
+ if (pairs)
+ cp[-pairs] = cp[0];
+ if ('\\' == cp[0]) {
+ if ('\\' == cp[1]) {
+ /* Poor man's copy mode. */
+ pairs++;
+ cp++;
+ } else if (0 == quoted && ' ' == cp[1])
+ /* Skip escaped blanks. */
+ cp++;
+ } else if (0 == quoted) {
+ if (' ' == cp[0]) {
+ /* Unescaped blanks end unquoted args. */
+ white = 1;
+ break;
+ }
+ } else if ('"' == cp[0]) {
+ if ('"' == cp[1]) {
+ /* Quoted quotes collapse. */
+ pairs++;
+ cp++;
+ } else {
+ /* Unquoted quotes end quoted args. */
+ quoted = 2;
+ break;
+ }
+ }
+ }
+
+ /* Quoted argument without a closing quote. */
+ if (1 == quoted && msg)
+ (*msg)(MANDOCERR_BADQUOTE, data, ln, *pos, NULL);
+
+ /* Null-terminate this argument and move to the next one. */
+ if (pairs)
+ cp[-pairs] = '\0';
+ if ('\0' != *cp) {
+ *cp++ = '\0';
+ while (' ' == *cp)
+ cp++;
+ }
+ *pos += (cp - start) + (quoted ? 1 : 0);
+ *cpp = cp;
+
+ if ('\0' == *cp && msg && (white || ' ' == cp[-1]))
+ (*msg)(MANDOCERR_EOLNSPACE, data, ln, *pos, NULL);
+
+ return(start);
+}
+
static int
a2time(time_t *t, const char *fmt, const char *p)
diff --git a/roff.c b/roff.c
index 65989cd1..64a8f629 100644
--- a/roff.c
+++ b/roff.c
@@ -1,7 +1,7 @@
-/* $Id: roff.c,v 1.118 2011/01/02 10:10:57 kristaps Exp $ */
+/* $Id: roff.c,v 1.119 2011/01/03 22:42:37 schwarze Exp $ */
/*
- * Copyright (c) 2010 Kristaps Dzonsons <kristaps@bsd.lv>
- * Copyright (c) 2010 Ingo Schwarze <schwarze@openbsd.org>
+ * Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
+ * Copyright (c) 2010, 2011 Ingo Schwarze <schwarze@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
@@ -1201,53 +1201,16 @@ roff_userdef(ROFF_ARGS)
{
const char *arg[9];
char *cp, *n1, *n2;
- int i, quoted, pairs;
+ int i;
/*
* Collect pointers to macro argument strings
* and null-terminate them.
*/
cp = *bufp + pos;
- for (i = 0; i < 9; i++) {
- /* Quoting can only start with a new word. */
- if ('"' == *cp) {
- quoted = 1;
- cp++;
- } else
- quoted = 0;
- arg[i] = cp;
- for (pairs = 0; '\0' != *cp; cp++) {
- /* Unquoted arguments end at blanks. */
- if (0 == quoted) {
- if (' ' == *cp)
- break;
- continue;
- }
- /* After pairs of quotes, move left. */
- if (pairs)
- cp[-pairs] = cp[0];
- /* Pairs of quotes do not end words, ... */
- if ('"' == cp[0] && '"' == cp[1]) {
- pairs++;
- cp++;
- continue;
- }
- /* ... but solitary quotes do. */
- if ('"' != *cp)
- continue;
- if (pairs)
- cp[-pairs] = '\0';
- *cp = ' ';
- break;
- }
- /* Last argument; the remaining ones are empty strings. */
- if ('\0' == *cp)
- continue;
- /* Null-terminate argument and move to the next one. */
- *cp++ = '\0';
- while (' ' == *cp)
- cp++;
- }
+ for (i = 0; i < 9; i++)
+ arg[i] = '\0' == *cp ? NULL :
+ mandoc_getarg(&cp, r->msg, r->data, ln, &pos);
/*
* Expand macro arguments.