aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org>2019-05-13 14:24:58 +0300
committerSergey Poznyakoff <gray@gnu.org>2019-05-13 14:52:18 +0300
commit07930c5ee74b8d2645fc55465833ab84e2f4444a (patch)
tree45ca5b8b787987a9c6a5980bddd7d6db13e070c7
parent3e07e3ad30e8a7a091e213eb4df839b7cf7f1e64 (diff)
downloadgrecs-07930c5ee74b8d2645fc55465833ab84e2f4444a.tar.gz
grecs-07930c5ee74b8d2645fc55465833ab84e2f4444a.tar.bz2
wordsplit: return single empty string on empty input in WRDSF_NOSPLIT mode
* src/wordsplit.c (wordsplit_finish): Make sure at one non-null argument is always returned in WRDSF_NOSPLIT mode. * tests/wordsplit.at: Add new test.
-rw-r--r--src/wordsplit.c17
-rw-r--r--tests/wordsplit.at13
2 files changed, 25 insertions, 5 deletions
diff --git a/src/wordsplit.c b/src/wordsplit.c
index f94015a..e979f27 100644
--- a/src/wordsplit.c
+++ b/src/wordsplit.c
@@ -802,16 +802,18 @@ wordsplit_finish (struct wordsplit *wsp)
node from the remaining nodes. */
if (wsnode_tail_coalesce (wsp, p))
return wsp->ws_errno;
n++;
}
- if (n == 0 && (wsp->ws_flags & WRDSF_INCREMENTAL))
+ if (n == 0)
{
- /* The loop above have eliminated all nodes. Restart the
- processing, if there's any input left. */
+ /* The loop above have eliminated all nodes. */
+ if (wsp->ws_flags & WRDSF_INCREMENTAL)
+ {
+ /* Restart the processing, if there's any input left. */
if (wsp->ws_endp < wsp->ws_len)
{
int rc;
if (wsp->ws_flags & WRDSF_SHOWDBG)
wsp->ws_debug (_("Restarting"));
rc = wordsplit_process_list (wsp, skip_delim (wsp));
@@ -823,12 +825,20 @@ wordsplit_finish (struct wordsplit *wsp)
wsp->ws_error = WRDSE_EOF;
return WRDSE_EOF;
}
goto again;
}
+ if (wsp->ws_flags & WRDSF_NOSPLIT)
+ {
+ if (wordsplit_add_segm (wsp, 0, 0, _WSNF_EMPTYOK))
+ return wsp->ws_errno;
+ n = 1;
+ }
+ }
+
if (alloc_space (wsp, n + 1))
return wsp->ws_errno;
while (wsp->ws_head)
{
const char *str = wsnode_ptr (wsp, wsp->ws_head);
@@ -2409,12 +2419,13 @@ wordsplit_process_list (struct wordsplit *wsp, size_t start)
wsp->ws_debug ("(%02d) %s", wsp->ws_lvl, _(p->descr));
wordsplit_dump_nodes (wsp);
}
}
}
}
+
return wsp->ws_errno;
}
static int
wordsplit_run (const char *command, size_t length, struct wordsplit *wsp,
int flags, int lvl)
diff --git a/tests/wordsplit.at b/tests/wordsplit.at
index 631d939..e3af703 100644
--- a/tests/wordsplit.at
+++ b/tests/wordsplit.at
@@ -259,30 +259,39 @@ TESTWSP([K/V environment],[wsp-env-kv wsp-env_kv],
1: aqux
TOTAL: 2
],
[],
[FOO=bar BAZ=qux])
-TESTWSP([nosplit with expansion],[wsp-var-nosplit],[nosplit],
+TESTWSP([nosplit with variable expansion],[wsp-var-nosplit],[nosplit],
[a $FOO test],
[NF: 1
0: "a variable expansion test\n"
TOTAL: 1
],
[],
[FOO="variable expansion"])
-TESTWSP([nosplit without expansion],[],[nosplit novar],
+TESTWSP([nosplit without variable expansion],[],[nosplit novar],
[a $FOO test],
[NF: 1
0: "a $FOO test\n"
TOTAL: 1
],
[],
[FOO="variable expansion"])
+TESTWSP([nosplit: empty expansion],[],[nosplit trimnl],
+[$FOO],
+[NF: 1
+0: ""
+TOTAL: 1
+],
+[],
+[FOO=""])
+
TESTWSP([default value (defined)],[],[],
[${FOO:-bar}],
[NF: 1
0: qux
TOTAL: 1
],

Return to:

Send suggestions and report system problems to the System administrator.