-/* $Id: mdoc_argv.c,v 1.105 2015/10/06 18:32:19 schwarze Exp $ */
+/* $Id: mdoc_argv.c,v 1.120 2019/07/11 17:06:17 schwarze Exp $ */
/*
* Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
- * Copyright (c) 2012, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
+ * Copyright (c) 2012, 2014-2019 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
#include "roff.h"
#include "mdoc.h"
#include "libmandoc.h"
+#include "roff_int.h"
#include "libmdoc.h"
#define MULTI_STEP 5 /* pre-allocate argument values */
MDOC_ARG_MAX
};
-static const struct mdocarg mdocargs[MDOC_MAX] = {
- { ARGSFL_DELIM, NULL }, /* Ap */
+static const struct mdocarg mdocargs[MDOC_MAX - MDOC_Dd] = {
{ ARGSFL_NONE, NULL }, /* Dd */
{ ARGSFL_NONE, NULL }, /* Dt */
{ ARGSFL_NONE, NULL }, /* Os */
{ ARGSFL_NONE, NULL }, /* It */
{ ARGSFL_DELIM, NULL }, /* Ad */
{ ARGSFL_DELIM, args_An }, /* An */
+ { ARGSFL_DELIM, NULL }, /* Ap */
{ ARGSFL_DELIM, NULL }, /* Ar */
{ ARGSFL_DELIM, NULL }, /* Cd */
{ ARGSFL_DELIM, NULL }, /* Cm */
{ ARGSFL_DELIM, NULL }, /* En */
{ ARGSFL_DELIM, NULL }, /* Dx */
{ ARGSFL_NONE, NULL }, /* %Q */
- { ARGSFL_NONE, NULL }, /* br */
- { ARGSFL_NONE, NULL }, /* sp */
{ ARGSFL_NONE, NULL }, /* %U */
{ ARGSFL_NONE, NULL }, /* Ta */
- { ARGSFL_NONE, NULL }, /* ll */
};
* Some flags take no argument, some one, some multiple.
*/
void
-mdoc_argv(struct roff_man *mdoc, int line, int tok,
+mdoc_argv(struct roff_man *mdoc, int line, enum roff_tok tok,
struct mdoc_arg **reta, int *pos, char *buf)
{
struct mdoc_argv tmpv;
/* Which flags does this macro support? */
- argtable = mdocargs[tok].argvs;
+ assert(tok >= MDOC_Dd && tok < MDOC_MAX);
+ argtable = mdocargs[tok - MDOC_Dd].argvs;
if (argtable == NULL)
return;
/* Prepare for parsing the next flag. */
*pos = ipos;
- argtable = mdocargs[tok].argvs;
+ argtable = mdocargs[tok - MDOC_Dd].argvs;
}
}
enum margserr
mdoc_args(struct roff_man *mdoc, int line, int *pos,
- char *buf, int tok, char **v)
+ char *buf, enum roff_tok tok, char **v)
{
struct roff_node *n;
- char *v_local;
enum argsflag fl;
- if (v == NULL)
- v = &v_local;
- fl = tok == TOKEN_NONE ? ARGSFL_NONE : mdocargs[tok].flags;
- if (tok != MDOC_It)
- return args(mdoc, line, pos, buf, fl, v);
+ fl = tok == TOKEN_NONE ? ARGSFL_NONE : mdocargs[tok - MDOC_Dd].flags;
/*
* We know that we're in an `It', so it's reasonable to expect
* safe fall-back into the default behaviour.
*/
- for (n = mdoc->last; n; n = n->parent)
- if (MDOC_Bl == n->tok)
- if (LIST_column == n->norm->Bl.type) {
+ if (tok == MDOC_It) {
+ for (n = mdoc->last; n != NULL; n = n->parent) {
+ if (n->tok != MDOC_Bl)
+ continue;
+ if (n->norm->Bl.type == LIST_column)
fl = ARGSFL_TABSEP;
- break;
- }
+ break;
+ }
+ }
return args(mdoc, line, pos, buf, fl, v);
}
args(struct roff_man *mdoc, int line, int *pos,
char *buf, enum argsflag fl, char **v)
{
- char *p, *pp;
+ char *p;
+ char *v_local;
int pairs;
- enum margserr rc;
-
- if ('\0' == buf[*pos]) {
- if (MDOC_PPHRASE & mdoc->flags)
- return ARGS_EOLN;
- /*
- * If we're not in a partial phrase and the flag for
- * being a phrase literal is still set, the punctuation
- * is unterminated.
- */
- if (MDOC_PHRASELIT & mdoc->flags)
- mandoc_msg(MANDOCERR_ARG_QUOTE,
- mdoc->parse, line, *pos, NULL);
- mdoc->flags &= ~MDOC_PHRASELIT;
+ if (buf[*pos] == '\0') {
+ if (mdoc->flags & MDOC_PHRASELIT &&
+ ! (mdoc->flags & MDOC_PHRASE)) {
+ mandoc_msg(MANDOCERR_ARG_QUOTE, line, *pos, NULL);
+ mdoc->flags &= ~MDOC_PHRASELIT;
+ }
+ mdoc->flags &= ~MDOC_PHRASEQL;
return ARGS_EOLN;
}
- *v = &buf[*pos];
+ if (v == NULL)
+ v = &v_local;
+ *v = buf + *pos;
- if (ARGSFL_DELIM == fl)
- if (args_checkpunct(buf, *pos))
- return ARGS_PUNCT;
+ if (fl == ARGSFL_DELIM && args_checkpunct(buf, *pos))
+ return ARGS_PUNCT;
/*
- * First handle TABSEP items, restricted to `Bl -column'. This
- * ignores conventional token parsing and instead uses tabs or
- * `Ta' macros to separate phrases. Phrases are parsed again
- * for arguments at a later phase.
+ * Tabs in `It' lines in `Bl -column' can't be escaped.
+ * Phrases are reparsed for `Ta' and other macros later.
*/
- if (ARGSFL_TABSEP == fl) {
- /* Scan ahead to tab (can't be escaped). */
- p = strchr(*v, '\t');
- pp = NULL;
-
- /* Scan ahead to unescaped `Ta'. */
- if ( ! (MDOC_PHRASELIT & mdoc->flags))
- for (pp = *v; ; pp++) {
- if (NULL == (pp = strstr(pp, "Ta")))
- break;
- if (pp > *v && ' ' != *(pp - 1))
- continue;
- if (' ' == *(pp + 2) || '\0' == *(pp + 2))
- break;
- }
-
- /* By default, assume a phrase. */
- rc = ARGS_PHRASE;
-
- /*
- * Adjust new-buffer position to be beyond delimiter
- * mark (e.g., Ta -> end + 2).
- */
- if (p && pp) {
- *pos += pp < p ? 2 : 1;
- rc = pp < p ? ARGS_PHRASE : ARGS_PPHRASE;
- p = pp < p ? pp : p;
- } else if (p && ! pp) {
- rc = ARGS_PPHRASE;
- *pos += 1;
- } else if (pp && ! p) {
- p = pp;
- *pos += 2;
+ if (fl == ARGSFL_TABSEP) {
+ if ((p = strchr(*v, '\t')) != NULL) {
+
+ /*
+ * Words right before and right after
+ * tab characters are not parsed,
+ * unless there is a blank in between.
+ */
+
+ if (p > buf && p[-1] != ' ')
+ mdoc->flags |= MDOC_PHRASEQL;
+ if (p[1] != ' ')
+ mdoc->flags |= MDOC_PHRASEQN;
+
+ /*
+ * One or more blanks after a tab cause
+ * one leading blank in the next column.
+ * So skip all but one of them.
+ */
+
+ *pos += (int)(p - *v) + 1;
+ while (buf[*pos] == ' ' && buf[*pos + 1] == ' ')
+ (*pos)++;
+
+ /*
+ * A tab at the end of an input line
+ * switches to the next column.
+ */
+
+ if (buf[*pos] == '\0' || buf[*pos + 1] == '\0')
+ mdoc->flags |= MDOC_PHRASEQN;
} else {
- rc = ARGS_PEND;
- p = strchr(*v, 0);
- }
-
- /* Whitespace check for eoln case... */
- if ('\0' == *p && ' ' == *(p - 1))
- mandoc_msg(MANDOCERR_SPACE_EOL, mdoc->parse,
- line, *pos, NULL);
-
- *pos += (int)(p - *v);
-
- /* Strip delimiter's preceding whitespace. */
- pp = p - 1;
- while (pp > *v && ' ' == *pp) {
- if (pp > *v && '\\' == *(pp - 1))
- break;
- pp--;
+ p = strchr(*v, '\0');
+ if (p[-1] == ' ')
+ mandoc_msg(MANDOCERR_SPACE_EOL,
+ line, *pos, NULL);
+ *pos += (int)(p - *v);
}
- *(pp + 1) = 0;
- /* Strip delimiter's proceeding whitespace. */
- for (pp = &buf[*pos]; ' ' == *pp; pp++, (*pos)++)
- /* Skip ahead. */ ;
+ /* Skip any trailing blank characters. */
+ while (p > *v && p[-1] == ' ' &&
+ (p - 1 == *v || p[-2] != '\\'))
+ p--;
+ *p = '\0';
- return rc;
+ return ARGS_PHRASE;
}
/*
* Whitespace is NOT involved in literal termination.
*/
- if (MDOC_PHRASELIT & mdoc->flags || '\"' == buf[*pos]) {
- if ( ! (MDOC_PHRASELIT & mdoc->flags))
+ if (mdoc->flags & MDOC_PHRASELIT ||
+ (mdoc->flags & MDOC_PHRASE && buf[*pos] == '\"')) {
+ if ((mdoc->flags & MDOC_PHRASELIT) == 0) {
*v = &buf[++(*pos)];
-
- if (MDOC_PPHRASE & mdoc->flags)
mdoc->flags |= MDOC_PHRASELIT;
-
+ }
pairs = 0;
for ( ; buf[*pos]; (*pos)++) {
/* Move following text left after quoted quotes. */
if (pairs)
buf[*pos - pairs] = '\0';
- if ('\0' == buf[*pos]) {
- if (MDOC_PPHRASE & mdoc->flags)
- return ARGS_QWORD;
- mandoc_msg(MANDOCERR_ARG_QUOTE,
- mdoc->parse, line, *pos, NULL);
- return ARGS_QWORD;
+ if (buf[*pos] == '\0') {
+ if ( ! (mdoc->flags & MDOC_PHRASE))
+ mandoc_msg(MANDOCERR_ARG_QUOTE,
+ line, *pos, NULL);
+ return ARGS_WORD;
}
mdoc->flags &= ~MDOC_PHRASELIT;
buf[(*pos)++] = '\0';
if ('\0' == buf[*pos])
- return ARGS_QWORD;
+ return ARGS_WORD;
while (' ' == buf[*pos])
(*pos)++;
if ('\0' == buf[*pos])
- mandoc_msg(MANDOCERR_SPACE_EOL, mdoc->parse,
- line, *pos, NULL);
+ mandoc_msg(MANDOCERR_SPACE_EOL, line, *pos, NULL);
- return ARGS_QWORD;
+ return ARGS_WORD;
}
p = &buf[*pos];
- *v = mandoc_getarg(mdoc->parse, &p, line, pos);
+ *v = roff_getarg(mdoc->roff, &p, line, pos);
+ if (v == &v_local)
+ free(*v);
+
+ /*
+ * After parsing the last word in this phrase,
+ * tell lookup() whether or not to interpret it.
+ */
- return ARGS_WORD;
+ if (*p == '\0' && mdoc->flags & MDOC_PHRASEQL) {
+ mdoc->flags &= ~MDOC_PHRASEQL;
+ mdoc->flags |= MDOC_PHRASEQF;
+ }
+ return ARGS_ALLOC;
}
/*
v->value = mandoc_reallocarray(v->value,
v->sz + MULTI_STEP, sizeof(char *));
- v->value[(int)v->sz] = mandoc_strdup(p);
+ if (ac != ARGS_ALLOC)
+ p = mandoc_strdup(p);
+ v->value[(int)v->sz] = p;
}
}
if (ac == ARGS_EOLN)
return;
+ if (ac != ARGS_ALLOC)
+ p = mandoc_strdup(p);
+
v->sz = 1;
v->value = mandoc_malloc(sizeof(char *));
- v->value[0] = mandoc_strdup(p);
+ v->value[0] = p;
}