diff options
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/mandoc/libmandoc.h | 3 | ||||
-rw-r--r-- | usr.bin/mandoc/man_argv.c | 78 | ||||
-rw-r--r-- | usr.bin/mandoc/mandoc.c | 84 | ||||
-rw-r--r-- | usr.bin/mandoc/roff.c | 49 |
4 files changed, 98 insertions, 116 deletions
diff --git a/usr.bin/mandoc/libmandoc.h b/usr.bin/mandoc/libmandoc.h index c79a3c46689..c066e6db9c3 100644 --- a/usr.bin/mandoc/libmandoc.h +++ b/usr.bin/mandoc/libmandoc.h @@ -1,4 +1,4 @@ -/* $Id: libmandoc.h,v 1.7 2010/07/16 00:34:33 schwarze Exp $ */ +/* $Id: libmandoc.h,v 1.8 2011/01/03 22:27:21 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/usr.bin/mandoc/man_argv.c b/usr.bin/mandoc/man_argv.c index 60fd3d42b22..25271da7a04 100644 --- a/usr.bin/mandoc/man_argv.c +++ b/usr.bin/mandoc/man_argv.c @@ -1,6 +1,6 @@ -/* $Id: man_argv.c,v 1.3 2010/07/31 23:42:04 schwarze Exp $ */ +/* $Id: man_argv.c,v 1.4 2011/01/03 22:27:21 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 @@ -17,84 +17,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/usr.bin/mandoc/mandoc.c b/usr.bin/mandoc/mandoc.c index cd8707adc84..92bd1a3454b 100644 --- a/usr.bin/mandoc/mandoc.c +++ b/usr.bin/mandoc/mandoc.c @@ -1,14 +1,15 @@ -/* $Id: mandoc.c,v 1.20 2010/09/27 21:25:28 schwarze Exp $ */ +/* $Id: mandoc.c,v 1.21 2011/01/03 22:27:21 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 @@ -279,6 +280,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/usr.bin/mandoc/roff.c b/usr.bin/mandoc/roff.c index a7337063603..49dd9fb0047 100644 --- a/usr.bin/mandoc/roff.c +++ b/usr.bin/mandoc/roff.c @@ -1,7 +1,7 @@ -/* $Id: roff.c,v 1.24 2010/12/21 01:30:58 schwarze Exp $ */ +/* $Id: roff.c,v 1.25 2011/01/03 22:27:21 schwarze Exp $ */ /* * Copyright (c) 2010 Kristaps Dzonsons <kristaps@bsd.lv> - * Copyright (c) 2010 Ingo Schwarze <schwarze@openbsd.org> + * 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 @@ -1124,53 +1124,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. |