mirror of
git://git.gnu.org.ua/wordsplit.git
synced 2025-04-26 00:29:54 +03:00
ws_getvar allocates memory
* src/wordsplit.c (ISVARBEG,ISVARCHR): New macros. (expvar): ws_getvar allocates memory. * src/wordsplit.h (wordsplit)<ws_getvar>: Remove const from the return value: the function should allocate memory.
This commit is contained in:
parent
29fb748305
commit
9765ed3720
2 changed files with 20 additions and 11 deletions
|
@ -48,6 +48,9 @@
|
|||
#define ISALNUM(c) (ISALPHA(c) || ISDIGIT(c))
|
||||
#define ISPRINT(c) (' ' <= ((unsigned) (c)) && ((unsigned) (c)) <= 127)
|
||||
|
||||
#define ISVARBEG(c) (ISALPHA(c) || c == '_')
|
||||
#define ISVARCHR(c) (ISALNUM(c) || c == '_')
|
||||
|
||||
#define ALLOC_INIT 128
|
||||
#define ALLOC_INCR 128
|
||||
|
||||
|
@ -703,15 +706,16 @@ expvar (struct wordsplit *wsp, const char *str, size_t len,
|
|||
struct wordsplit_node **ptail, const char **pend, int flg)
|
||||
{
|
||||
size_t i = 0;
|
||||
const char *value;
|
||||
const char *defstr = NULL;
|
||||
char *value;
|
||||
const char *vptr;
|
||||
struct wordsplit_node *newnode;
|
||||
const char *start = str - 1;
|
||||
|
||||
if (ISALPHA (str[0]) || str[0] == '_')
|
||||
if (ISVARBEG (str[0]))
|
||||
{
|
||||
for (i = 1; i < len; i++)
|
||||
if (!(ISALNUM (str[i]) || str[i] == '_'))
|
||||
if (!ISVARCHR (str[i]))
|
||||
break;
|
||||
*pend = str + i - 1;
|
||||
}
|
||||
|
@ -789,7 +793,11 @@ expvar (struct wordsplit *wsp, const char *str, size_t len,
|
|||
if (wsp->ws_flags & WRDSF_KEEPUNDEF)
|
||||
value = NULL;
|
||||
else
|
||||
value = "";
|
||||
{
|
||||
value = strdup ("");
|
||||
if (!value)
|
||||
return _wsplt_nomem (wsp);
|
||||
}
|
||||
}
|
||||
/* FIXME: handle defstr */
|
||||
if (value)
|
||||
|
@ -801,12 +809,11 @@ expvar (struct wordsplit *wsp, const char *str, size_t len,
|
|||
wsnode_insert (wsp, newnode, *ptail, 0);
|
||||
*ptail = newnode;
|
||||
newnode->flags = _WSNF_WORD | _WSNF_NOEXPAND | flg;
|
||||
newnode->v.word = strdup (value);
|
||||
if (!newnode->v.word)
|
||||
return _wsplt_nomem (wsp);
|
||||
newnode->v.word = value;
|
||||
}
|
||||
else if (*value == 0)
|
||||
{
|
||||
free (value);
|
||||
/* Empty string is a special case */
|
||||
if (wsnode_new (wsp, &newnode))
|
||||
return 1;
|
||||
|
@ -817,11 +824,13 @@ expvar (struct wordsplit *wsp, const char *str, size_t len,
|
|||
else
|
||||
{
|
||||
struct wordsplit ws;
|
||||
int i;
|
||||
int i, rc;
|
||||
|
||||
ws.ws_delim = wsp->ws_delim;
|
||||
if (wordsplit (value, &ws,
|
||||
WRDSF_NOVAR | WRDSF_NOCMD | WRDSF_DELIM | WRDSF_WS))
|
||||
rc = wordsplit (value, &ws,
|
||||
WRDSF_NOVAR | WRDSF_NOCMD | WRDSF_DELIM | WRDSF_WS);
|
||||
free (value);
|
||||
if (rc)
|
||||
{
|
||||
wordsplit_free (&ws);
|
||||
return 1;
|
||||
|
|
|
@ -36,7 +36,7 @@ struct wordsplit
|
|||
__attribute__ ((__format__ (__printf__, 1, 2)));
|
||||
|
||||
const char **ws_env;
|
||||
const char *(*ws_getvar) (const char *, size_t, void *);
|
||||
char *(*ws_getvar) (const char *, size_t, void *);
|
||||
void *ws_closure;
|
||||
|
||||
const char *ws_input;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue