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 time: 2022-09-18 14:14:05
*/
#include "line.h"
#include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <stdlib.h>
#include <string.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) {
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]);

19
loop.c
View file

@ -4,14 +4,14 @@
* @Last Modified by: 1ridic
* @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 "exec.h"
#include "line.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <readline/history.h>
#include <readline/readline.h>
int status;
@ -36,8 +36,17 @@ int loop() {
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;
}