-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequence.c
88 lines (75 loc) · 2.23 KB
/
sequence.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
* Author: Samuel Marsh
* Date: 30/10/2015
*/
#include "mysh.h"
/**
* Executes the left and right branches of the command tree in sequential order.
*
* @param t the command tree, with root node of type N_SEMICOLON
* @return the exit status of the right child of the command tree
*/
int execute_semicolon(CMDTREE *t)
{
//execute in order, no matter the result of the left side
int exit_status = EXIT_SUCCESS;
if (t->left != NULL)
{
execute_cmdtree(t->left);
}
if (t->right != NULL)
{
exit_status = execute_cmdtree(t->right);
}
return exit_status;
}
/**
* Executes the left branch of the command tree, and only on failure executes
* the right branch (i.e. a short-circuiting OR operator).
*
* @param t the command tree, with root node of type N_OR
* @return the exit status of the most recently executed command (that is,
* if the left branch returns an exit status indicating success that
* status is returned, otherwise the right branch is executed and the
* exit status of that branch is returned)
*/
int execute_or(CMDTREE *t)
{
//always execute left hand side
int exit_status = EXIT_SUCCESS;
if (t->left != NULL)
{
exit_status = execute_cmdtree(t->left);
}
//short circuit upon failure
if (exit_status != EXIT_SUCCESS && t->right != NULL)
{
exit_status = execute_cmdtree(t->right);
}
return exit_status;
}
/**
* Executes the left branch of the command tree, and only on success executes
* the right branch (i.e. a short-circuiting AND operator)
*
* @param t the command tree, with root node of type N_AND
* @return the exit status of the most recently executed command (that is,
* if the left branch returns an exit status indicating failure that
* status is returned otherwise the right branch is executed and the
* exit status of that branch is returned)
*/
int execute_and(CMDTREE *t)
{
//always execute left hand side
int exit_status = EXIT_SUCCESS;
if (t->left != NULL)
{
exit_status = execute_cmdtree(t->left);
}
//short circuit upon success
if (exit_status == EXIT_SUCCESS && t->right != NULL)
{
exit_status = execute_cmdtree(t->right);
}
return exit_status;
}