diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2014-10-23 12:12:02 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2015-12-17 15:25:24 +0200 |
commit | e66e2edfa50c8535e05b334c6a5d867fafeb9469 (patch) | |
tree | 5af187dea3750df11c404658c624300bae324d97 /src | |
parent | beda81512afdcf16432ebd5b76930392f427a7fc (diff) | |
download | grecs-e66e2edfa50c8535e05b334c6a5d867fafeb9469.tar.gz grecs-e66e2edfa50c8535e05b334c6a5d867fafeb9469.tar.bz2 |
Change prototypes of ws_getvar and ws_command.
New invocation sequence ensures proper error handling.
This is an incompatible change. Authors using ws_getvar member will
have to rewrite their ws_getvar function accordingly.
* src/wordsplit.c (wordsplit_init0): Call wordsplit_clearerr
on reuse.
(wordsplit_init): Fix ws_errno
(expvar): Change invocation of ws_getvar.
(expcmd): Change invocation of ws_command.
(wordsplit_clearerr): New function.
(wordsplit_strerror): Handle WRDSE_USERERR.
* src/wordsplit.h (ws_getvar): Change return value and signature of
ws_getvar and ws_command.
New member 'ws_usererr'.
(WRDSF_ARGV): New flag.
(WRDSE_OK): New define. Same as WRDSE_EOF.
(WRDSE_USERERR): New error code.
(wordsplit_clearerr): New proto.
* tests/wsp.c (wsp_getvar, wsp_runcmd): Rewrite.
Diffstat (limited to 'src')
-rw-r--r-- | src/wordsplit.c | 67 | ||||
-rw-r--r-- | src/wordsplit.h | 14 |
2 files changed, 74 insertions, 7 deletions
diff --git a/src/wordsplit.c b/src/wordsplit.c index f17ed98..2b99b42 100644 --- a/src/wordsplit.c +++ b/src/wordsplit.c @@ -98,2 +98,3 @@ wordsplit_init0 (struct wordsplit *wsp) wordsplit_free_words (wsp); + wordsplit_clearerr (wsp); } @@ -136,3 +137,3 @@ wordsplit_init (struct wordsplit *wsp, const char *input, size_t len, errno = EINVAL; - wsp->ws_errno = WRDSE_NOSUPP; + wsp->ws_errno = WRDSE_USAGE; if (wsp->ws_flags & WRDSF_SHOWERR) @@ -788,3 +789,26 @@ expvar (struct wordsplit *wsp, const char *str, size_t len, else if (wsp->ws_flags & WRDSF_GETVAR) - value = wsp->ws_getvar (str, i, wsp->ws_closure); + { + int rc = wsp->ws_getvar (&value, str, i, wsp->ws_closure); + switch (rc) + { + case WRDSE_OK: + break; + + case WRDSE_NOSPACE: + return _wsplt_nomem (wsp); + + case WRDSE_UNDEF: + value = NULL; + break; + + case WRDSE_USERERR: + wsp->ws_usererr = value; + /* fall through */ + default: + wsp->ws_errno = rc; + if (wsp->ws_flags & WRDSF_SHOWERR) + wordsplit_perror (wsp); + return 1; + } + } else @@ -820,3 +844,3 @@ expvar (struct wordsplit *wsp, const char *str, size_t len, wsp->ws_errno = WRDSE_UNDEF; - if (wsp->ws_flags & WRDSF_SHOWERR) + if (wsp->ws_flags & WRDSF_SHOWERR) wordsplit_perror (wsp); @@ -1035,2 +1059,3 @@ expcmd (struct wordsplit *wsp, const char *str, size_t len, { + int rc; size_t j; @@ -1049,3 +1074,26 @@ expcmd (struct wordsplit *wsp, const char *str, size_t len, *pend = str + j; - value = wsp->ws_command (str, j, wsp); + if (wsp->ws_flags & WRDSF_ARGV) + { + struct wordsplit ws; + + ws.ws_delim = wsp->ws_delim; + rc = wordsplit_len (str, j, &ws, + WRDSF_NOVAR | WRDSF_NOCMD | WRDSF_DELIM | + (wsp->ws_flags & (WRDSF_WS | WRDSF_QUOTE))); + rc = wsp->ws_command (&value, str, j, ws.ws_wordv, wsp->ws_closure); + } + else + rc = wsp->ws_command (&value, str, j, NULL, wsp->ws_closure); + + if (rc == WRDSE_NOSPACE) + return _wsplt_nomem (wsp); + else if (rc) + { + if (rc == WRDSE_USERERR) + wsp->ws_usererr = value; + wsp->ws_errno = rc; + if (wsp->ws_flags & WRDSF_SHOWERR) + wordsplit_perror (wsp); + return 1; + } @@ -1754,2 +1802,11 @@ wordsplit_free_words (struct wordsplit *ws) void +wordsplit_clearerr (struct wordsplit *ws) +{ + if (ws->ws_errno == WRDSE_USERERR) + free (ws->ws_usererr); + ws->ws_usererr = NULL; + ws->ws_errno = WRDSE_OK; +} + +void wordsplit_free (struct wordsplit *ws) @@ -1821,2 +1878,4 @@ wordsplit_strerror (struct wordsplit *ws) { + if (ws->ws_errno == WRDSE_USERERR) + return ws->ws_usererr; if (ws->ws_errno < _wordsplit_nerrs) diff --git a/src/wordsplit.h b/src/wordsplit.h index 96088f9..4a0cd88 100644 --- a/src/wordsplit.h +++ b/src/wordsplit.h @@ -38,6 +38,6 @@ struct wordsplit const char **ws_env; - char *(*ws_getvar) (const char *, size_t, void *); + int (*ws_getvar) (char **, const char *, size_t, void *); void *ws_closure; - char *(*ws_command) (const char *, size_t, struct wordsplit const *); + int (*ws_command) (char **, const char *, size_t, char **, void *); @@ -47,2 +47,3 @@ struct wordsplit int ws_errno; + char *ws_usererr; struct wordsplit_node *ws_head, *ws_tail; @@ -50,3 +51,3 @@ struct wordsplit -/* Wordsplit flags. Only 2 bits of a 32-bit word remain unused. +/* Wordsplit flags. Only 1 bit of a 32-bit word remains unused. It is getting crowded... */ @@ -127,2 +128,5 @@ struct wordsplit +/* ws_command needs argv parameter */ +#define WRDSF_ARGV 0x40000000 + #define WRDSF_DEFFLAGS \ @@ -131,2 +135,3 @@ struct wordsplit +#define WRDSE_OK 0 #define WRDSE_EOF 0 @@ -139,2 +144,3 @@ struct wordsplit #define WRDSE_NOINPUT 7 +#define WRDSE_USERERR 8 @@ -159,2 +165,4 @@ const char *wordsplit_strerror (struct wordsplit *ws); +void wordsplit_clearerr (struct wordsplit *ws); + |