Add semicolumn split

This commit is contained in:
g1n 2021-09-17 21:49:46 +03:00
parent 53fc85a9e6
commit e088bfbac0
1 changed files with 11 additions and 6 deletions

View File

@ -7,7 +7,7 @@
int position = 0; int position = 0;
char pwd[128]; // FIXME: change to PATH_MAX char pwd[128]; // FIXME: change to PATH_MAX
char **split_line(char *line) { char **split_line(char *line, char *delim) {
int bufsize = 64; int bufsize = 64;
position = 0; position = 0;
char **tokens = malloc(bufsize * sizeof(char*)); char **tokens = malloc(bufsize * sizeof(char*));
@ -18,11 +18,11 @@ char **split_line(char *line) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
token = strtok(line, " "); token = strtok(line, delim);
while (token != NULL) { while (token != NULL) {
tokens[position] = token; tokens[position] = token;
position++; position++;
token = strtok(NULL, " "); token = strtok(NULL, delim);
} }
tokens[position] = NULL; tokens[position] = NULL;
return tokens; return tokens;
@ -85,7 +85,7 @@ int parse_command(char **command) {
printf("exit\n"); printf("exit\n");
exit(0); exit(0);
} else if (!strcmp(command[0], "echo")) { } else if (!strcmp(command[0], "echo")) {
for (int i = 1; i <= position - 1; i++) for (int i = 1; i <= position - 1; i++)
printf("%s ", command[i]); printf("%s ", command[i]);
printf("\n"); printf("\n");
return 0; return 0;
@ -109,6 +109,7 @@ int parse_command(char **command) {
int main() {//int argc, char *argv[]) { int main() {//int argc, char *argv[]) {
char *line = NULL; char *line = NULL;
size_t len = 0; size_t len = 0;
char** commands;
char** args; char** args;
int status = 0; // FIXME int status = 0; // FIXME
int command_status = 0; // FIXME int command_status = 0; // FIXME
@ -121,9 +122,13 @@ int main() {//int argc, char *argv[]) {
break; break;
} }
line[strlen(line) - 1] = '\0'; line[strlen(line) - 1] = '\0';
args = split_line(line); commands = split_line(line, ";");
int after_parse_position = position; // To know where we in ; list FIXME
for (int i = 0; i <= after_parse_position - 1; i++) {
args = split_line(commands[i], " ");
command_status = parse_command(args); command_status = parse_command(args);
}
} }
return command_status; return command_status;