diff --git a/line.c b/line.c index 0a864c5..24d1786 100644 --- a/line.c +++ b/line.c @@ -4,14 +4,16 @@ * @Last Modified by: 1ridic * @Last Modified time: 2022-09-18 14:14:05 */ +#include "line.h" #include -#include -#include #include #include #include -#include "line.h" +#include +#include + +/*split one line to word, when appears "", save the content to one word*/ char **splitLine(char *line) { int bufsize = LINE_BUF_SIZE, position = 0; char **tokens = malloc(bufsize * sizeof(char *)); @@ -29,6 +31,41 @@ char **splitLine(char *line) { token = strtok(line, " \t\r\n\a"); while (token != NULL) { + /*detect if there exists "" pair*/ + if (token[0] == '"') { + char *temp = malloc(bufsize * sizeof(char *)); + for (int i = 0; i < strlen(token); i++) { + /*delete the first " */ + temp[i] = token[i + 1]; + } + /*read next token and detect " */ + int pair_flag = 0; + while (1) { + token = strtok(NULL, " \t\r\n\a"); + if (token[strlen(token) - 1] == '"') { + /*delete the last " */ + token[strlen(token) - 1] = '\0'; + strcat(temp, " "); + strcat(temp, token); + token=temp; + break; + } else { + strcat(temp, " "); + strcat(temp, token); + pair_flag = 1; + } + if(token==NULL&&pair_flag==0){ + fprintf(stderr,"error: no pair \"\n"); + return NULL; + } + } + } + else + { + char* temp=malloc(bufsize * sizeof(char *)); + strcpy(temp,token); + token=temp; + } tokens[position] = token; #ifdef DEBUG fprintf(stdout, "debug: tokens[%d] = %s\n", position, tokens[position]); diff --git a/loop.c b/loop.c index 790306b..a428a70 100644 --- a/loop.c +++ b/loop.c @@ -4,23 +4,23 @@ * @Last Modified by: 1ridic * @Last Modified time: 2022-09-20 22:26:59 */ -#include -#include -#include -#include -#include #include "loop.h" #include "exec.h" #include "line.h" +#include +#include +#include +#include +#include int status; void SIGINT_Handler(int dummy) { #ifdef DEBUG - fprintf(stdout,"\ndebug: SIGINT\n"); + fprintf(stdout, "\ndebug: SIGINT\n"); #endif /* Move to a new line */ - fprintf(stdout,"\n"); + fprintf(stdout, "\n"); /* Regenerate the prompt on a newline */ rl_on_new_line(); /* Clear the previous text */ @@ -33,11 +33,20 @@ int loop() { char *line; char **args; - line = readline(ANSI_COLOR_GREEN"> "ANSI_COLOR_RESET); + line = readline(ANSI_COLOR_GREEN "> " ANSI_COLOR_RESET); add_history(line); args = splitLine(line); + if (args == NULL) { + free(line); + free(args); + return 1; + } status = commandExec(args); free(line); + /*free all array in args*/ + for (int i = 0; args[i] != NULL; i++) { + free(args[i]); + } free(args); return 0; } \ No newline at end of file