summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAssaf Gordon <assafgordon@gmail.com>2014-12-25 11:10:37 (GMT)
committerAssaf Gordon <assafgordon@gmail.com>2014-12-27 19:55:47 (GMT)
commitb603258dbc9912e570ad912ec0fac1150063487a (patch)
tree02ba2fa958e40425b0b11e80b23a4ac67aeadaa9
parentdb127407269b38a10ab07c779148f7583b7fc350 (diff)
downloadagnostic-b603258dbc9912e570ad912ec0fac1150063487a.zip
agnostic-b603258dbc9912e570ad912ec0fac1150063487a.tar.gz
agnostic-b603258dbc9912e570ad912ec0fac1150063487a.tar.bz2
shell: implement 'while/until' processing.
The following now works: $ ./sh_c 'A=1 ; while test $A -lt 5 ; do echo A=$A ; A=$((A+1)) ; done' A=1 A=2 A=3 A=4
-rw-r--r--src/node_modules/shell/shell_executor2.js31
-rw-r--r--src/shell_parser/posix_shell.pegjs2
2 files changed, 32 insertions, 1 deletions
diff --git a/src/node_modules/shell/shell_executor2.js b/src/node_modules/shell/shell_executor2.js
index f989f65..e5e028c 100644
--- a/src/node_modules/shell/shell_executor2.js
+++ b/src/node_modules/shell/shell_executor2.js
@@ -151,6 +151,8 @@ function Command(process_state, obj)
return CompoundCurrentshell(value);
case "for_clause":
return ForClause(process_state,value);
+ case "while_until_clause":
+ return WhileUntilClause(process_state,value);
default:
throw "Invalid Command type '" + type + '"';
}
@@ -324,6 +326,35 @@ function ForClause(process_state, obj)
return exit_code;
}
+/* WhileUntilClause() - process a while/until statement */
+function WhileUntilClause(process_state, obj)
+{
+ var exit_code = 0 ;
+ ob_utils.VerifyAllowedKeys(obj, ["type","condition","action"]);
+
+ var type = obj["type"]; // 'while' or 'until'
+
+ /* The commands to run and check its exit code as condition */
+ var condition = obj["condition"];
+ /* The commands to exeute each iteration */
+ var action = obj["action"];
+
+ var exit_code;
+
+ while (true) {
+ exit_code = Command(process_state, condition);
+ if ( ( type === "while" && exit_code !== 0 )
+ ||( type === "until" && exit_code === 0 ) )
+ break;
+
+ // Condition met, execute the action
+ // TODO: which exit code to return?
+ exit_code = Command(process_state, action);
+ }
+
+ return exit_code;
+}
+
/*
SimpleCommand() - Process a single execution command,
diff --git a/src/shell_parser/posix_shell.pegjs b/src/shell_parser/posix_shell.pegjs
index afa2f0c..8de100b 100644
--- a/src/shell_parser/posix_shell.pegjs
+++ b/src/shell_parser/posix_shell.pegjs
@@ -473,7 +473,7 @@ While_Until_Clause = type:( "while" / "until" ) EmptyDelimiter*
condition:Compound_List EmptyDelimiter*
action:Do_group
{
- return { "while_until" : {
+ return { "while_until_clause" : {
"type" : type,
"condition" : condition,
"action" : action