aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWe-unite <3205135446@qq.com>2024-06-03 22:09:20 +0800
committerWe-unite <3205135446@qq.com>2024-06-03 22:09:20 +0800
commit8b4481c878b185779d32fe7aa027de3cea3f77a2 (patch)
tree88e55845f827d485504cbc15ff779bc9ef3fbeb7
parent69a2b6eaaa21354e78b5fe6d79b0e7e10e127dcc (diff)
downloadbcsh-8b4481c878b185779d32fe7aa027de3cea3f77a2.tar.gz
bcsh-8b4481c878b185779d32fe7aa027de3cea3f77a2.zip
run cmd failed
-rw-r--r--bcsh.c12
-rw-r--r--cmd.h10
-rw-r--r--lex.l9
-rw-r--r--syntax.y80
4 files changed, 70 insertions, 41 deletions
diff --git a/bcsh.c b/bcsh.c
index ca83402..8e0dd32 100644
--- a/bcsh.c
+++ b/bcsh.c
@@ -7,4 +7,16 @@ int main(int argc, char **argv) {
7 yyparse(); 7 yyparse();
8 } 8 }
9 return 0; 9 return 0;
10}
11
12int runcmd(Command *cmd) {
13 int i;
14 if (cmd == NULL) {
15 fprintf(stderr, "cmd is NULL\n");
16 }
17 printf("Run cmd: ");
18 for (i = 0; i < cmd->argc; i++) {
19 printf("%s ", cmd->args[i]);
20 }
21 printf("\n");
10} \ No newline at end of file 22} \ No newline at end of file
diff --git a/cmd.h b/cmd.h
index adf03c7..c094e04 100644
--- a/cmd.h
+++ b/cmd.h
@@ -16,15 +16,11 @@ typedef enum {
16typedef struct Command { 16typedef struct Command {
17 CommandType type; // the type of the command 17 CommandType type; // the type of the command
18 char **args; // an array of strings for the arguments of the command 18 char **args; // an array of strings for the arguments of the command
19 int argc;
19 struct Command *left; // a pointer to the left sub-command 20 struct Command *left; // a pointer to the left sub-command
20 struct Command *right; // a pointer to the right sub-command 21 struct Command *right; // a pointer to the right sub-command
21 char *redirect_in; // the file name for input redirection, or NULL if there
22 // is no input redirection
23 char *redirect_out; // the file name for output redirection, or NULL if
24 // there is no output redirection
25 char *append_out; // the file name for output appending, or NULL if there is
26 // no output appending
27 // add more fields as needed
28} Command; 22} Command;
29 23
24int runcmd(Command *cmd);
25
30#endif \ No newline at end of file 26#endif \ No newline at end of file
diff --git a/lex.l b/lex.l
index 036d58e..230a5b0 100644
--- a/lex.l
+++ b/lex.l
@@ -9,18 +9,11 @@ void yyerror(const char *s);
9%% 9%%
10 10
11[\t ]+ { /* Ignore whitespace */ } 11[\t ]+ { /* Ignore whitespace */ }
12"\n" { printf("new line\n");return NEWLINE; } 12"\n" { return NEWLINE; }
13"\\\n" { printf("> "); } 13"\\\n" { printf("> "); }
14">>" { return APPEND_OUT; }
15"<<" { return HEREDOC; }
16">" { return REDIRECT_OUT; }
17"<" { return REDIRECT_IN; }
18">>&" { return APPEND_OUTPUT_ERR; }
19">&" { return REDIRECT_OUTPUT_ERR; }
20"|" { return PIPE; } 14"|" { return PIPE; }
21"&&" { return AND; } 15"&&" { return AND; }
22"||" { return OR; } 16"||" { return OR; }
23"&" { return BACKGROUND; }
24\"(\\.|[^\"])*\" { yylval.str = strdup(yytext); return STRING; } 17\"(\\.|[^\"])*\" { yylval.str = strdup(yytext); return STRING; }
25[a-zA-Z0-9_\-\/.]+ { yylval.str = strdup(yytext); return WORD; } 18[a-zA-Z0-9_\-\/.]+ { yylval.str = strdup(yytext); return WORD; }
26 19
diff --git a/syntax.y b/syntax.y
index aa0df71..1ce2223 100644
--- a/syntax.y
+++ b/syntax.y
@@ -1,50 +1,78 @@
1%{ 1%{
2#include "lex.yy.c"
3#include "cmd.h" 2#include "cmd.h"
4 3#include "lex.yy.c"
5%} 4%}
6 5
7%union { 6%union {
8 char *str; 7 char *str;
8 struct Command *cmd;
9 CommandType cmdType;
9} 10}
10 11
11%token <str> WORD STRING 12%token <str> WORD STRING
12%token NEWLINE CONTINUATION APPEND_OUT REDIRECT_OUT REDIRECT_IN APPEND_OUTPUT_ERR REDIRECT_OUTPUT_ERR PIPE AND OR BACKGROUND HEREDOC UNKNOWN 13%token NEWLINE PIPE AND OR
14
15%type <cmd> line command part
16%type <cmdType> separator
13 17
14%% 18%%
15 19
16line: 20line:
17 command NEWLINE { printf("Parsed a command.\n"); } 21 command NEWLINE { $$ = $1; runcmd($$); }
18 | NEWLINE { /* empty line */ } 22 | NEWLINE { $$ = NULL; }
19 ; 23 ;
20 24
21command: 25command:
22 part {printf("PART\n"); } 26 part { $$ = $1; }
23 | part separator command { printf("COMMAND\n"); } 27 | part separator command {
24 | part REDIRECT_OUT WORD tail { printf("REDIRECT_OUT: %s\n", $3); free($3); } 28 $$ = malloc(sizeof(Command));
25 | part REDIRECT_IN WORD tail { printf("REDIRECT_IN: %s\n", $3); free($3); } 29 $$->type = $2;
26 | part HEREDOC WORD tail { printf("HEREDOC: %s\n", $3); free($3); } 30 $$->left = $1;
27 | part BACKGROUND { printf("BACKGROUND\n"); } 31 $$->right = $3;
28 ; 32 }
29
30tail:
31 part
32 | part REDIRECT_OUT WORD tail { printf("REDIRECT_OUT: %s\n", $3); free($3); }
33 | part REDIRECT_IN WORD tail { printf("REDIRECT_IN: %s\n", $3); free($3); }
34 | part HEREDOC WORD tail { printf("HEREDOC: %s\n", $3); free($3); }
35 ; 33 ;
36 34
37part: 35part:
38 part WORD { printf("WORD: %s\n", $2); free($2); } 36 part WORD {
39 | part STRING { printf("STRING: %s\n", $2); free($2); } 37 $$ = $1;
40 | WORD { printf("WORD: %s\n", $1); free($1); } 38 $$->argc++;
41 | STRING { printf("STRING: %s\n", $1); free($1); } 39 $$->args = realloc($$->args, ($$->argc) * sizeof(char *));
40 $$->args[$$->argc - 1] = $2;
41 $$->args[$$->argc] = NULL;
42 printf("word[%d] = %s\n", $$->argc-1,$$->args[$$->argc-1]);
43 }
44 | part STRING {
45 $$ = $1;
46 $$->argc++;
47 $$->args = realloc($$->args, ($$->argc) * sizeof(char *));
48 $$->args[$$->argc - 1] = $2;
49 $$->args[$$->argc] = NULL;
50 }
51 | WORD {
52 $$ = malloc(sizeof(Command));
53 $$->type = CMD_TYPE_NORMAL;
54 $$->args = malloc(2 * sizeof(char *));
55 $$->args[0] = $1;
56 $$->args[1] = NULL;
57 $$->argc = 1;
58 printf("word[%d] = %s\n", $$->argc-1,$$->args[$$->argc-1]);
59 $$->left = $$->right = NULL;
60 }
61 | STRING {
62 $$ = malloc(sizeof(Command));
63 $$->type = CMD_TYPE_NORMAL;
64 $$->args = malloc(2 * sizeof(char *));
65 $$->args[0] = $1;
66 $$->args[1] = NULL;
67 $$->argc = 1;
68 $$->left = $$->right = NULL;
69 }
42 ; 70 ;
43 71
44separator: 72separator:
45 PIPE { printf("PIPE\n"); } 73 PIPE { $$ = CMD_TYPE_PIPE; }
46 | AND { printf("AND\n"); } 74 | AND { $$ = CMD_TYPE_AND; }
47 | OR { printf("OR\n"); } 75 | OR { $$ = CMD_TYPE_OR; }
48 ; 76 ;
49 77
50%% \ No newline at end of file 78%%