diff options
author | 2024-06-04 15:50:16 +0800 | |
---|---|---|
committer | 2024-06-04 16:21:45 +0800 | |
commit | 2501c46c383bf4a113667db9d372d45aee2cd695 (patch) | |
tree | 5d2c58d618ef84c493af45113fb5bc281ed4027e | |
parent | 8b4481c878b185779d32fe7aa027de3cea3f77a2 (diff) | |
download | bcsh-2501c46c383bf4a113667db9d372d45aee2cd695.tar.gz bcsh-2501c46c383bf4a113667db9d372d45aee2cd695.zip |
Fix the gramma, del redirection, it can parse now.
-rw-r--r-- | bcsh.c | 44 | ||||
-rw-r--r-- | cmd.h | 4 | ||||
-rw-r--r-- | lex.l | 5 | ||||
-rw-r--r-- | syntax.y | 42 |
4 files changed, 72 insertions, 23 deletions
@@ -9,12 +9,54 @@ int main(int argc, char **argv) { | |||
9 | return 0; | 9 | return 0; |
10 | } | 10 | } |
11 | 11 | ||
12 | Command *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 | |||
12 | int runcmd(Command *cmd) { | 22 | int 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 | |||
40 | void 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 | |||
58 | void 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 | } |
@@ -4,6 +4,7 @@ | |||
4 | #include <stdbool.h> | 4 | #include <stdbool.h> |
5 | #include <stdio.h> | 5 | #include <stdio.h> |
6 | #include <stdlib.h> | 6 | #include <stdlib.h> |
7 | #include <string.h> | ||
7 | 8 | ||
8 | typedef enum { | 9 | typedef enum { |
9 | CMD_TYPE_NORMAL, | 10 | CMD_TYPE_NORMAL, |
@@ -21,6 +22,9 @@ typedef struct Command { | |||
21 | struct Command *right; // a pointer to the right sub-command | 22 | struct Command *right; // a pointer to the right sub-command |
22 | } Command; | 23 | } Command; |
23 | 24 | ||
25 | Command* newcmd(); | ||
24 | int runcmd(Command *cmd); | 26 | int runcmd(Command *cmd); |
27 | void freecmd(Command *cmd); | ||
28 | void printcmd(Command *cmd); | ||
25 | 29 | ||
26 | #endif \ No newline at end of file | 30 | #endif \ No newline at end of file |
@@ -1,5 +1,5 @@ | |||
1 | %{ | 1 | %{ |
2 | #include "syntax.tab.h" // Bison 生成的头文件 | 2 | #include "syntax.tab.h" |
3 | 3 | ||
4 | void yyerror(const char *s); | 4 | void yyerror(const char *s); |
5 | %} | 5 | %} |
@@ -20,7 +20,6 @@ void yyerror(const char *s); | |||
20 | 20 | ||
21 | %% | 21 | %% |
22 | 22 | ||
23 | void yyerror(const char *s) | 23 | void yyerror(const char *s) { |
24 | { | ||
25 | fprintf(stderr, "error: %s\n", s); | 24 | fprintf(stderr, "error: %s\n", s); |
26 | } | 25 | } |
@@ -1,6 +1,7 @@ | |||
1 | %{ | 1 | %{ |
2 | #include "cmd.h" | 2 | #include "cmd.h" |
3 | #include "lex.yy.c" | 3 | #include "lex.yy.c" |
4 | #include <stdio.h> | ||
4 | %} | 5 | %} |
5 | 6 | ||
6 | %union { | 7 | %union { |
@@ -9,23 +10,34 @@ | |||
9 | CommandType cmdType; | 10 | CommandType cmdType; |
10 | } | 11 | } |
11 | 12 | ||
12 | %token <str> WORD STRING | 13 | %token <str> WORD STRING error |
13 | %token NEWLINE PIPE AND OR | 14 | %token NEWLINE PIPE AND OR |
14 | 15 | ||
15 | %type <cmd> line command part | 16 | %type <cmd> line command part runcommand |
16 | %type <cmdType> separator | 17 | %type <cmdType> separator |
17 | 18 | ||
18 | %% | 19 | %% |
19 | 20 | ||
20 | line: | 21 | line: |
21 | command NEWLINE { $$ = $1; runcmd($$); } | 22 | runcommand line { /* empty */ } |
23 | | /* empty */ { $$ = NULL; } | ||
24 | ; | ||
25 | |||
26 | runcommand: | ||
27 | command NEWLINE { | ||
28 | $$ = $1; | ||
29 | printf("running command: \n"); | ||
30 | runcmd($$); | ||
31 | } | ||
22 | | NEWLINE { $$ = NULL; } | 32 | | NEWLINE { $$ = NULL; } |
23 | ; | 33 | ; |
24 | 34 | ||
25 | command: | 35 | command: |
26 | part { $$ = $1; } | 36 | part { |
37 | $$ = $1; | ||
38 | } | ||
27 | | part separator command { | 39 | | part separator command { |
28 | $$ = malloc(sizeof(Command)); | 40 | $$ = newcmd(); |
29 | $$->type = $2; | 41 | $$->type = $2; |
30 | $$->left = $1; | 42 | $$->left = $1; |
31 | $$->right = $3; | 43 | $$->right = $3; |
@@ -39,7 +51,6 @@ part: | |||
39 | $$->args = realloc($$->args, ($$->argc) * sizeof(char *)); | 51 | $$->args = realloc($$->args, ($$->argc) * sizeof(char *)); |
40 | $$->args[$$->argc - 1] = $2; | 52 | $$->args[$$->argc - 1] = $2; |
41 | $$->args[$$->argc] = NULL; | 53 | $$->args[$$->argc] = NULL; |
42 | printf("word[%d] = %s\n", $$->argc-1,$$->args[$$->argc-1]); | ||
43 | } | 54 | } |
44 | | part STRING { | 55 | | part STRING { |
45 | $$ = $1; | 56 | $$ = $1; |
@@ -49,23 +60,16 @@ part: | |||
49 | $$->args[$$->argc] = NULL; | 60 | $$->args[$$->argc] = NULL; |
50 | } | 61 | } |
51 | | WORD { | 62 | | WORD { |
52 | $$ = malloc(sizeof(Command)); | 63 | $$ = newcmd(); |
53 | $$->type = CMD_TYPE_NORMAL; | ||
54 | $$->args = malloc(2 * sizeof(char *)); | 64 | $$->args = malloc(2 * sizeof(char *)); |
55 | $$->args[0] = $1; | 65 | $$->args[$$->argc++] = $1; |
56 | $$->args[1] = NULL; | 66 | $$->args[$$->argc] = NULL; |
57 | $$->argc = 1; | ||
58 | printf("word[%d] = %s\n", $$->argc-1,$$->args[$$->argc-1]); | ||
59 | $$->left = $$->right = NULL; | ||
60 | } | 67 | } |
61 | | STRING { | 68 | | STRING { |
62 | $$ = malloc(sizeof(Command)); | 69 | $$ = newcmd(); |
63 | $$->type = CMD_TYPE_NORMAL; | ||
64 | $$->args = malloc(2 * sizeof(char *)); | 70 | $$->args = malloc(2 * sizeof(char *)); |
65 | $$->args[0] = $1; | 71 | $$->args[$$->argc++] = $1; |
66 | $$->args[1] = NULL; | 72 | $$->args[$$->argc] = NULL; |
67 | $$->argc = 1; | ||
68 | $$->left = $$->right = NULL; | ||
69 | } | 73 | } |
70 | ; | 74 | ; |
71 | 75 | ||