diff options
author | Sergey Poznyakoff <gray@gnu.org> | 2019-04-20 08:54:21 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org> | 2019-04-20 09:47:40 +0300 |
commit | 3e07e3ad30e8a7a091e213eb4df839b7cf7f1e64 (patch) | |
tree | a3c83a584f8e2b54e294ff1dfa04a2262459d5a4 | |
parent | 1498bd570eb001a6b2bc3f1a5074e8b384d6db30 (diff) | |
download | grecs-3e07e3ad30e8a7a091e213eb4df839b7cf7f1e64.tar.gz grecs-3e07e3ad30e8a7a091e213eb4df839b7cf7f1e64.tar.bz2 |
Improve variable handling in wordsplit.
Positional variables ($N and ${N}) are recognized. Variable names in
curly braces follow the same rules as unadorned ones.
This commit also changes memory reallocation strategy in wsplt_assign_var.
If ws_envbuf needs to be expanded, new allocation size is selected as
3/2 of the previous allocation, if that size is less than max(size_t).
-rw-r--r-- | include/wordsplit.h | 16 | ||||
-rw-r--r-- | src/wordsplit.c | 67 |
2 files changed, 60 insertions, 23 deletions
diff --git a/include/wordsplit.h b/include/wordsplit.h index 1a047f7..3a7ab25 100644 --- a/include/wordsplit.h +++ b/include/wordsplit.h @@ -1,4 +1,4 @@ /* wordsplit - a word splitter - Copyright (C) 2009-2018 Sergey Poznyakoff + Copyright (C) 2009-2019 Sergey Poznyakoff This program is free software; you can redistribute it and/or modify it @@ -67,7 +67,13 @@ struct wordsplit environment variables. */ - char **ws_envbuf; - size_t ws_envidx; - size_t ws_envsiz; + /* Temporary storage for environment variables. It is initialized + upon first assignment which occurs during the parsing process + (e.g. ${x:=2}). When this happens, all variables from ws_env are + moved to ws_envbuf first, and the ws_envbuf address is assigned + to ws_env. From this moment on, all variable expansions are served + from ws_envbuf. */ + char **ws_envbuf; /* Storage for variables */ + size_t ws_envidx; /* Index of first free slot */ + size_t ws_envsiz; /* Size of the ws_envbuf array */ int (*ws_getvar) (char **ret, const char *var, size_t len, void *clos); @@ -198,6 +204,6 @@ struct wordsplit #if 0 /* Unused value */ #define WRDSO_ARGV 0x00000008 -/* Keep backslash in unrecognized escape sequences in words */ #endif +/* Keep backslash in unrecognized escape sequences in words */ #define WRDSO_BSKEEP_WORD 0x00000010 /* Handle octal escapes in words */ diff --git a/src/wordsplit.c b/src/wordsplit.c index bad59b1..f94015a 100644 --- a/src/wordsplit.c +++ b/src/wordsplit.c @@ -1087,10 +1087,14 @@ wsplt_assign_var (struct wordsplit *wsp, const char *name, size_t namelen, else { - wsp->ws_envsiz *= 2; - newenv = realloc (wsp->ws_envbuf, - wsp->ws_envsiz * sizeof (wsp->ws_envbuf[0])); + size_t n = wsp->ws_envsiz; + + if ((size_t) -1 / 3 * 2 / sizeof (wsp->ws_envbuf[0]) <= n) + return _wsplt_nomem (wsp); + n += (n + 1) / 2; + newenv = realloc (wsp->ws_envbuf, n * sizeof (wsp->ws_envbuf[0])); if (!newenv) return _wsplt_nomem (wsp); wsp->ws_envbuf = newenv; + wsp->ws_envsiz = n; wsp->ws_env = (const char**) wsp->ws_envbuf; } @@ -1129,4 +1133,27 @@ wsplt_assign_var (struct wordsplit *wsp, const char *name, size_t namelen, } +/* Recover from what looked like a variable reference, but turned out + not to be one. STR points to first character after '$'. */ +static int +expvar_recover (struct wordsplit *wsp, const char *str, + struct wordsplit_node **ptail, const char **pend, int flg) +{ + struct wordsplit_node *newnode; + + if (wsnode_new (wsp, &newnode)) + return 1; + wsnode_insert (wsp, newnode, *ptail, 0); + *ptail = newnode; + newnode->flags = _WSNF_WORD | flg; + newnode->v.word = malloc (3); + if (!newnode->v.word) + return _wsplt_nomem (wsp); + newnode->v.word[0] = '$'; + newnode->v.word[1] = str[0]; + newnode->v.word[2] = 0; + *pend = str; + return 0; +} + static int expvar (struct wordsplit *wsp, const char *str, size_t len, @@ -1149,5 +1176,10 @@ expvar (struct wordsplit *wsp, const char *str, size_t len, *pend = str + i - 1; } - else if (str[0] == '{') + else if (ISDIGIT (str[0])) + { + i = 1; + *pend = str; + } + else if (str[0] == '{' && (ISVARBEG (str[1]) || ISDIGIT (str[1]))) { str++; @@ -1181,4 +1213,15 @@ expvar (struct wordsplit *wsp, const char *str, size_t len, break; } + else if (ISDIGIT (str[1])) + { + if (!ISDIGIT (str[i])) + { + return expvar_recover (wsp, str - 1, ptail, pend, flg); + } + } + else if (!ISVARCHR (str[i])) + { + return expvar_recover (wsp, str - 1, ptail, pend, flg); + } } if (i == len) @@ -1187,17 +1230,5 @@ expvar (struct wordsplit *wsp, const char *str, size_t len, else { - if (wsnode_new (wsp, &newnode)) - return 1; - wsnode_insert (wsp, newnode, *ptail, 0); - *ptail = newnode; - newnode->flags = _WSNF_WORD | flg; - newnode->v.word = malloc (3); - if (!newnode->v.word) - return _wsplt_nomem (wsp); - newnode->v.word[0] = '$'; - newnode->v.word[1] = str[0]; - newnode->v.word[2] = 0; - *pend = str; - return 0; + return expvar_recover (wsp, str, ptail, pend, flg); } @@ -1418,5 +1449,5 @@ static int begin_var_p (int c) { - return c == '{' || ISVARBEG (c); + return c == '{' || ISVARBEG (c) || ISDIGIT (c); } |