aboutsummaryrefslogtreecommitdiffhomepage
path: root/builtin.cpp
diff options
context:
space:
mode:
authorGravatar Kevin Ballard <kevin@sb.org>2014-07-13 22:36:26 -0700
committerGravatar Kevin Ballard <kevin@sb.org>2014-07-14 00:46:38 -0700
commit973dd6ffbdc189f22b634de0d684e92a9c160c9d (patch)
tree242487f472926817ac2b805fad0f68b169aee76e /builtin.cpp
parent72e8489d50d749c86d5b57609bb0c4d83a03b41a (diff)
read: Support arrays, character splitting
Enhance the `read` builtin to support creating an array with the --array flag. With --array, only a single variable name is allowed and the entire input is tokenized and placed into that variable as an array. Also add custom behavior if IFS is empty or unset. In that event, split the input on every character, instead of the previous behavior of doing no splitting at all.
Diffstat (limited to 'builtin.cpp')
-rw-r--r--builtin.cpp81
1 files changed, 72 insertions, 9 deletions
diff --git a/builtin.cpp b/builtin.cpp
index 6f7cc33c..1420e33c 100644
--- a/builtin.cpp
+++ b/builtin.cpp
@@ -2301,6 +2301,7 @@ static int builtin_read(parser_t &parser, wchar_t **argv)
int exit_res=STATUS_BUILTIN_OK;
const wchar_t *mode_name = READ_MODE_NAME;
int shell = 0;
+ int array = 0;
woptind=0;
@@ -2346,6 +2347,10 @@ static int builtin_read(parser_t &parser, wchar_t **argv)
}
,
{
+ L"array", no_argument, 0, 'a'
+ }
+ ,
+ {
L"help", no_argument, 0, 'h'
}
,
@@ -2359,7 +2364,7 @@ static int builtin_read(parser_t &parser, wchar_t **argv)
int opt = wgetopt_long(argc,
argv,
- L"xglUup:c:hm:s",
+ L"xglUup:c:hm:sa",
long_options,
&opt_index);
if (opt == -1)
@@ -2414,6 +2419,10 @@ static int builtin_read(parser_t &parser, wchar_t **argv)
shell = 1;
break;
+ case 'a':
+ array = 1;
+ break;
+
case 'h':
builtin_print_help(parser, argv[0], stdout_buffer);
return STATUS_BUILTIN_OK;
@@ -2446,6 +2455,14 @@ static int builtin_read(parser_t &parser, wchar_t **argv)
return STATUS_BUILTIN_ERROR;
}
+ if (array && woptind+1 != argc)
+ {
+ append_format(stderr_buffer, _(L"%ls: --array option requires a single variable name.\n"), argv[0]);
+ builtin_print_help(parser, argv[0], stderr_buffer);
+
+ return STATUS_BUILTIN_ERROR;
+ }
+
/*
Verify all variable names
*/
@@ -2580,18 +2597,64 @@ static int builtin_read(parser_t &parser, wchar_t **argv)
wchar_t *state;
env_var_t ifs = env_get_string(L"IFS");
- if (ifs.missing())
- ifs = L"";
- nxt = wcstok(buff, (i<argc-1)?ifs.c_str():L"", &state);
+ if (ifs.missing_or_empty())
+ {
+ /* Every character is a separate token */
+ size_t bufflen = wcslen(buff);
+ if (array)
+ {
+ if (bufflen > 0)
+ {
+ wcstring chars(bufflen+(bufflen-1), ARRAY_SEP);
+ for (size_t j=0; j<bufflen; ++j)
+ {
+ chars[j*2] = buff[j];
+ }
+ env_set(argv[i], chars.c_str(), place);
+ }
+ else
+ {
+ env_set(argv[i], NULL, place);
+ }
+ }
+ else
+ {
+ size_t j = 0;
+ for (; i+1 < argc; ++i)
+ {
+ env_set(argv[i], j < bufflen ? (wchar_t[2]){buff[j], 0} : L"", place);
+ if (j < bufflen) ++j;
+ }
+ if (i < argc) env_set(argv[i], &buff[j], place);
+ }
+ }
+ else if (array)
+ {
+ wcstring tokens;
+ tokens.reserve(wcslen(buff));
+ bool empty = true;
- while (i<argc)
+ for (nxt = wcstok(buff, ifs.c_str(), &state); nxt != 0; nxt = wcstok(0, ifs.c_str(), &state))
+ {
+ if (! tokens.empty()) tokens.push_back(ARRAY_SEP);
+ tokens.append(nxt);
+ empty = false;
+ }
+ env_set(argv[i], empty ? NULL : tokens.c_str(), place);
+ }
+ else
{
- env_set(argv[i], nxt != 0 ? nxt: L"", place);
+ nxt = wcstok(buff, (i<argc-1)?ifs.c_str():L"", &state);
- i++;
- if (nxt != 0)
- nxt = wcstok(0, (i<argc-1)?ifs.c_str():L"", &state);
+ while (i<argc)
+ {
+ env_set(argv[i], nxt != 0 ? nxt: L"", place);
+
+ i++;
+ if (nxt != 0)
+ nxt = wcstok(0, (i<argc-1)?ifs.c_str():L"", &state);
+ }
}
}