From 2501c46c383bf4a113667db9d372d45aee2cd695 Mon Sep 17 00:00:00 2001 From: We-unite <3205135446@qq.com> Date: Tue, 4 Jun 2024 15:50:16 +0800 Subject: Fix the gramma, del redirection, it can parse now. --- bcsh.c | 44 +++++++++++++++++++++++++++++++++++++++++++- cmd.h | 4 ++++ lex.l | 5 ++--- syntax.y | 42 +++++++++++++++++++++++------------------- 4 files changed, 72 insertions(+), 23 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) { return 0; } +Command *newcmd() { + Command *cmd = (Command *)malloc(sizeof(Command)); + cmd->argc = 0; + cmd->args = NULL; + cmd->type = CMD_TYPE_NORMAL; + cmd->left = NULL; + cmd->right = NULL; + return cmd; +} + int runcmd(Command *cmd) { int i; if (cmd == NULL) { fprintf(stderr, "cmd is NULL\n"); + } else if (cmd->type != CMD_TYPE_NORMAL) { + runcmd(cmd->left); + runcmd(cmd->right); + return 0; + } + printcmd(cmd); + if (!strcmp(cmd->args[0], "exit")) { + freecmd(cmd); + exit(0); + } + freecmd(cmd); + return 0; +} + +void freecmd(Command *cmd) { + if (cmd == NULL) { + return; } - printf("Run cmd: "); + if (cmd->left != NULL) { + freecmd(cmd->left); + } + if (cmd->right != NULL) { + freecmd(cmd->right); + } + int i; + for (i = 0; i < cmd->argc; i++) { + free(cmd->args[i]); + } + free(cmd->args); + free(cmd); +} + +void printcmd(Command *cmd) { + int i; for (i = 0; i < cmd->argc; i++) { printf("%s ", cmd->args[i]); } diff --git a/cmd.h b/cmd.h index c094e04..38b3fa1 100644 --- a/cmd.h +++ b/cmd.h @@ -4,6 +4,7 @@ #include #include #include +#include typedef enum { CMD_TYPE_NORMAL, @@ -21,6 +22,9 @@ typedef struct Command { struct Command *right; // a pointer to the right sub-command } Command; +Command* newcmd(); int runcmd(Command *cmd); +void freecmd(Command *cmd); +void printcmd(Command *cmd); #endif \ No newline at end of file diff --git a/lex.l b/lex.l index 230a5b0..c530ca0 100644 --- a/lex.l +++ b/lex.l @@ -1,5 +1,5 @@ %{ -#include "syntax.tab.h" // Bison 生成的头文件 +#include "syntax.tab.h" void yyerror(const char *s); %} @@ -20,7 +20,6 @@ void yyerror(const char *s); %% -void yyerror(const char *s) -{ +void yyerror(const char *s) { fprintf(stderr, "error: %s\n", s); } diff --git a/syntax.y b/syntax.y index 1ce2223..d9d3510 100644 --- a/syntax.y +++ b/syntax.y @@ -1,6 +1,7 @@ %{ #include "cmd.h" #include "lex.yy.c" +#include %} %union { @@ -9,23 +10,34 @@ CommandType cmdType; } -%token WORD STRING +%token WORD STRING error %token NEWLINE PIPE AND OR -%type line command part +%type line command part runcommand %type separator %% line: - command NEWLINE { $$ = $1; runcmd($$); } + runcommand line { /* empty */ } + | /* empty */ { $$ = NULL; } + ; + +runcommand: + command NEWLINE { + $$ = $1; + printf("running command: \n"); + runcmd($$); + } | NEWLINE { $$ = NULL; } ; command: - part { $$ = $1; } + part { + $$ = $1; + } | part separator command { - $$ = malloc(sizeof(Command)); + $$ = newcmd(); $$->type = $2; $$->left = $1; $$->right = $3; @@ -39,7 +51,6 @@ part: $$->args = realloc($$->args, ($$->argc) * sizeof(char *)); $$->args[$$->argc - 1] = $2; $$->args[$$->argc] = NULL; - printf("word[%d] = %s\n", $$->argc-1,$$->args[$$->argc-1]); } | part STRING { $$ = $1; @@ -49,23 +60,16 @@ part: $$->args[$$->argc] = NULL; } | WORD { - $$ = malloc(sizeof(Command)); - $$->type = CMD_TYPE_NORMAL; + $$ = newcmd(); $$->args = malloc(2 * sizeof(char *)); - $$->args[0] = $1; - $$->args[1] = NULL; - $$->argc = 1; - printf("word[%d] = %s\n", $$->argc-1,$$->args[$$->argc-1]); - $$->left = $$->right = NULL; + $$->args[$$->argc++] = $1; + $$->args[$$->argc] = NULL; } | STRING { - $$ = malloc(sizeof(Command)); - $$->type = CMD_TYPE_NORMAL; + $$ = newcmd(); $$->args = malloc(2 * sizeof(char *)); - $$->args[0] = $1; - $$->args[1] = NULL; - $$->argc = 1; - $$->left = $$->right = NULL; + $$->args[$$->argc++] = $1; + $$->args[$$->argc] = NULL; } ; -- cgit v1.2.3-70-g09d2