diff options
-rw-r--r-- | shell/hush_test/hush-read/read_ifs2.right | 9 | ||||
-rwxr-xr-x | shell/hush_test/hush-read/read_ifs2.tests | 9 | ||||
-rw-r--r-- | shell/shell_common.c | 37 |
3 files changed, 54 insertions, 1 deletions
diff --git a/shell/hush_test/hush-read/read_ifs2.right b/shell/hush_test/hush-read/read_ifs2.right new file mode 100644 index 000000000..797137dae --- /dev/null +++ b/shell/hush_test/hush-read/read_ifs2.right | |||
@@ -0,0 +1,9 @@ | |||
1 | |X|Y:Z:| | ||
2 | |X|Y:Z| | ||
3 | |X|Y| | ||
4 | |X|Y| | ||
5 | |X|| | ||
6 | |X|| | ||
7 | ||| | ||
8 | Whitespace should be trimmed too: | ||
9 | |X|Y| | ||
diff --git a/shell/hush_test/hush-read/read_ifs2.tests b/shell/hush_test/hush-read/read_ifs2.tests new file mode 100755 index 000000000..f01a68978 --- /dev/null +++ b/shell/hush_test/hush-read/read_ifs2.tests | |||
@@ -0,0 +1,9 @@ | |||
1 | echo "X:Y:Z:" | (IFS=": " read x y; echo "|$x|$y|") | ||
2 | echo "X:Y:Z" | (IFS=": " read x y; echo "|$x|$y|") | ||
3 | echo "X:Y:" | (IFS=": " read x y; echo "|$x|$y|") | ||
4 | echo "X:Y" | (IFS=": " read x y; echo "|$x|$y|") | ||
5 | echo "X:" | (IFS=": " read x y; echo "|$x|$y|") | ||
6 | echo "X" | (IFS=": " read x y; echo "|$x|$y|") | ||
7 | echo "" | (IFS=": " read x y; echo "|$x|$y|") | ||
8 | echo Whitespace should be trimmed too: | ||
9 | echo "X:Y : " | (IFS=": " read x y; echo "|$x|$y|") | ||
diff --git a/shell/shell_common.c b/shell/shell_common.c index 9e58ee4fe..0a07296f3 100644 --- a/shell/shell_common.c +++ b/shell/shell_common.c | |||
@@ -274,9 +274,44 @@ shell_builtin_read(void FAST_FUNC (*setvar)(const char *name, const char *val), | |||
274 | 274 | ||
275 | if (argv[0]) { | 275 | if (argv[0]) { |
276 | /* Remove trailing space $IFS chars */ | 276 | /* Remove trailing space $IFS chars */ |
277 | while (--bufpos >= 0 && isspace(buffer[bufpos]) && strchr(ifs, buffer[bufpos]) != NULL) | 277 | while (--bufpos >= 0 |
278 | && isspace(buffer[bufpos]) | ||
279 | && strchr(ifs, buffer[bufpos]) != NULL | ||
280 | ) { | ||
278 | continue; | 281 | continue; |
282 | } | ||
279 | buffer[bufpos + 1] = '\0'; | 283 | buffer[bufpos + 1] = '\0'; |
284 | |||
285 | /* Last variable takes the entire remainder with delimiters | ||
286 | * (sans trailing whitespace $IFS), | ||
287 | * but ***only "if there are fewer vars than fields"(c)***! | ||
288 | * The "X:Y:" case below: there are two fields, | ||
289 | * and therefore last delimiter (:) is eaten: | ||
290 | * IFS=": " | ||
291 | * echo "X:Y:Z:" | (read x y; echo "|$x|$y|") # |X|Y:Z:| | ||
292 | * echo "X:Y:Z" | (read x y; echo "|$x|$y|") # |X|Y:Z| | ||
293 | * echo "X:Y:" | (read x y; echo "|$x|$y|") # |X|Y|, not |X|Y:| | ||
294 | * echo "X:Y : " | (read x y; echo "|$x|$y|") # |X|Y| | ||
295 | */ | ||
296 | if (bufpos >= 0 | ||
297 | && strchr(ifs, buffer[bufpos]) != NULL | ||
298 | ) { | ||
299 | /* There _is_ a non-whitespace IFS char */ | ||
300 | /* Skip whitespace IFS char before it */ | ||
301 | while (--bufpos >= 0 | ||
302 | && isspace(buffer[bufpos]) | ||
303 | && strchr(ifs, buffer[bufpos]) != NULL | ||
304 | ) { | ||
305 | continue; | ||
306 | } | ||
307 | /* Are there $IFS chars? */ | ||
308 | if (strcspn(buffer, ifs) >= ++bufpos) { | ||
309 | /* No: last var takes one field, not more */ | ||
310 | /* So, drop trailing IFS delims */ | ||
311 | buffer[bufpos] = '\0'; | ||
312 | } | ||
313 | } | ||
314 | |||
280 | /* Use the remainder as a value for the next variable */ | 315 | /* Use the remainder as a value for the next variable */ |
281 | setvar(*argv, buffer); | 316 | setvar(*argv, buffer); |
282 | /* Set the rest to "" */ | 317 | /* Set the rest to "" */ |