aboutsummaryrefslogtreecommitdiffstats
path: root/bcsh.c
diff options
context:
space:
mode:
Diffstat (limited to 'bcsh.c')
-rw-r--r--bcsh.c44
1 files changed, 43 insertions, 1 deletions
diff --git a/bcsh.c b/bcsh.c
index 8e0dd32..6830671 100644
--- a/bcsh.c
+++ b/bcsh.c
@@ -9,12 +9,54 @@ int main(int argc, char **argv) {
9 return 0; 9 return 0;
10} 10}
11 11
12Command *newcmd() {
13 Command *cmd = (Command *)malloc(sizeof(Command));
14 cmd->argc = 0;
15 cmd->args = NULL;
16 cmd->type = CMD_TYPE_NORMAL;
17 cmd->left = NULL;
18 cmd->right = NULL;
19 return cmd;
20}
21
12int runcmd(Command *cmd) { 22int runcmd(Command *cmd) {
13 int i; 23 int i;
14 if (cmd == NULL) { 24 if (cmd == NULL) {
15 fprintf(stderr, "cmd is NULL\n"); 25 fprintf(stderr, "cmd is NULL\n");
26 } else if (cmd->type != CMD_TYPE_NORMAL) {
27 runcmd(cmd->left);
28 runcmd(cmd->right);
29 return 0;
30 }
31 printcmd(cmd);
32 if (!strcmp(cmd->args[0], "exit")) {
33 freecmd(cmd);
34 exit(0);
35 }
36 freecmd(cmd);
37 return 0;
38}
39
40void freecmd(Command *cmd) {
41 if (cmd == NULL) {
42 return;
16 } 43 }
17 printf("Run cmd: "); 44 if (cmd->left != NULL) {
45 freecmd(cmd->left);
46 }
47 if (cmd->right != NULL) {
48 freecmd(cmd->right);
49 }
50 int i;
51 for (i = 0; i < cmd->argc; i++) {
52 free(cmd->args[i]);
53 }
54 free(cmd->args);
55 free(cmd);
56}
57
58void printcmd(Command *cmd) {
59 int i;
18 for (i = 0; i < cmd->argc; i++) { 60 for (i = 0; i < cmd->argc; i++) {
19 printf("%s ", cmd->args[i]); 61 printf("%s ", cmd->args[i]);
20 } 62 }