feat: add support for split forbid

This commit is contained in:
iridiumR 2023-03-30 18:12:29 +08:00
parent d7c03761a4
commit 19eaeb08db
No known key found for this signature in database
GPG key ID: 49735733EB1A32C8
2 changed files with 57 additions and 11 deletions

43
line.c
View file

@ -4,14 +4,16 @@
* @Last Modified by: 1ridic * @Last Modified by: 1ridic
* @Last Modified time: 2022-09-18 14:14:05 * @Last Modified time: 2022-09-18 14:14:05
*/ */
#include "line.h"
#include <stdio.h> #include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include "line.h" #include <readline/history.h>
#include <readline/readline.h>
/*split one line to word, when appears "", save the content to one word*/
char **splitLine(char *line) { char **splitLine(char *line) {
int bufsize = LINE_BUF_SIZE, position = 0; int bufsize = LINE_BUF_SIZE, position = 0;
char **tokens = malloc(bufsize * sizeof(char *)); char **tokens = malloc(bufsize * sizeof(char *));
@ -29,6 +31,41 @@ char **splitLine(char *line) {
token = strtok(line, " \t\r\n\a"); token = strtok(line, " \t\r\n\a");
while (token != NULL) { 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; tokens[position] = token;
#ifdef DEBUG #ifdef DEBUG
fprintf(stdout, "debug: tokens[%d] = %s\n", position, tokens[position]); fprintf(stdout, "debug: tokens[%d] = %s\n", position, tokens[position]);

25
loop.c
View file

@ -4,23 +4,23 @@
* @Last Modified by: 1ridic * @Last Modified by: 1ridic
* @Last Modified time: 2022-09-20 22:26:59 * @Last Modified time: 2022-09-20 22:26:59
*/ */
#include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <stdlib.h>
#include <string.h>
#include "loop.h" #include "loop.h"
#include "exec.h" #include "exec.h"
#include "line.h" #include "line.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <readline/history.h>
#include <readline/readline.h>
int status; int status;
void SIGINT_Handler(int dummy) { void SIGINT_Handler(int dummy) {
#ifdef DEBUG #ifdef DEBUG
fprintf(stdout,"\ndebug: SIGINT\n"); fprintf(stdout, "\ndebug: SIGINT\n");
#endif #endif
/* Move to a new line */ /* Move to a new line */
fprintf(stdout,"\n"); fprintf(stdout, "\n");
/* Regenerate the prompt on a newline */ /* Regenerate the prompt on a newline */
rl_on_new_line(); rl_on_new_line();
/* Clear the previous text */ /* Clear the previous text */
@ -33,11 +33,20 @@ int loop() {
char *line; char *line;
char **args; char **args;
line = readline(ANSI_COLOR_GREEN"> "ANSI_COLOR_RESET); line = readline(ANSI_COLOR_GREEN "> " ANSI_COLOR_RESET);
add_history(line); add_history(line);
args = splitLine(line); args = splitLine(line);
if (args == NULL) {
free(line);
free(args);
return 1;
}
status = commandExec(args); status = commandExec(args);
free(line); free(line);
/*free all array in args*/
for (int i = 0; args[i] != NULL; i++) {
free(args[i]);
}
free(args); free(args);
return 0; return 0;
} }