summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorA. Gordon <assafgordon@gmail.com>2014-08-11 23:48:51 (GMT)
committerA. Gordon <assafgordon@gmail.com>2014-08-11 23:48:51 (GMT)
commit65eb85e26af65ad542a48ac9e5fb78dca5c63982 (patch)
treed5418ca3422b94899e4c346316a6f429eccaae59
parentc867322d6020c470d847b23658e1c7932acdef0b (diff)
downloadagnostic-65eb85e26af65ad542a48ac9e5fb78dca5c63982.zip
agnostic-65eb85e26af65ad542a48ac9e5fb78dca5c63982.tar.gz
agnostic-65eb85e26af65ad542a48ac9e5fb78dca5c63982.tar.bz2
Shell parsing: implement ${#FOO} expansion
-rw-r--r--src/node_modules/shell/shell_executor2.js9
-rw-r--r--src/shell_parser/posix_shell.pegjs1
-rw-r--r--src/tests/shell_executor_env_var_operators1.js3
-rw-r--r--src/tests/shell_syntax_tests.js1
4 files changed, 14 insertions, 0 deletions
diff --git a/src/node_modules/shell/shell_executor2.js b/src/node_modules/shell/shell_executor2.js
index ab91cf9..6a311f7 100644
--- a/src/node_modules/shell/shell_executor2.js
+++ b/src/node_modules/shell/shell_executor2.js
@@ -929,6 +929,15 @@ function EnvVarOperation(process_state, obj)
else
return "";
+ /* 'strlen' is the internal representation
+ if the following shell parameter-expansion operation:
+ ${#FOO} => string length of variable "FOO" .
+ It is defined in 'posix_parser.pegjs' in 'ParameterOperationExpandable'
+ rule. */
+ case 'strlen':
+ /* TODO: fail if 'set -u' is set and the variable is not set */
+ return current_value.length;
+
/*
case '#':
break;
diff --git a/src/shell_parser/posix_shell.pegjs b/src/shell_parser/posix_shell.pegjs
index 158a4dc..aa98245 100644
--- a/src/shell_parser/posix_shell.pegjs
+++ b/src/shell_parser/posix_shell.pegjs
@@ -551,6 +551,7 @@ ParameterExpandable =
but the "VALUE" part can recursively contain more expandable items. */
ParameterOperationExpandable =
"${" varname:ParameterName varop:ParameterExpansionOperator opvalue:Token_NoBraces* "}" { return { "envvar_operation" : { "envvar" : varname, "operation" : varop, "value": (opvalue?opvalue[0]:[]) } }; }
+ / "${#" varname:ParameterName "}" { return { "envvar_operation" : { "envvar" : varname, "operation" : "strlen", "value" : null } }; }
/* Alphanumeric parameter name, or special parameter name (section 2.5.2) */
ParameterName =
diff --git a/src/tests/shell_executor_env_var_operators1.js b/src/tests/shell_executor_env_var_operators1.js
index ee5c113..a11fc4b 100644
--- a/src/tests/shell_executor_env_var_operators1.js
+++ b/src/tests/shell_executor_env_var_operators1.js
@@ -103,6 +103,9 @@ var tests = [
// Double-quoted expansion - becomes one parameter despite whitespace
[ "e54", "echo \"${SETNUL:-AAA BBB}\"", { stdout: ["AAA BBB"] }],
+// Length Operation
+[ "e55", "echo ${#SET}", { stdout: ["3"] }],
+
];
diff --git a/src/tests/shell_syntax_tests.js b/src/tests/shell_syntax_tests.js
index 82e7624..09bc236 100644
--- a/src/tests/shell_syntax_tests.js
+++ b/src/tests/shell_syntax_tests.js
@@ -441,6 +441,7 @@ var tests = [
["ParmOpExp28", "${FOO=HELLO\"WORLD}", false, "ParameterOperationExpandable"],
["ParmOpExp29", "${FOO=HELLO\'WORLD}", false, "ParameterOperationExpandable"],
["ParmOpExp30", "${FOO=HEL${LOWORLD}", false, "ParameterOperationExpandable"],
+["ParmOpExp31", "${#FOO}", true, "ParameterOperationExpandable"],
/* Test Arithmatic Expansion */