|
|
|
@ -237,6 +237,12 @@ int run_pipes(State *state, int num_args, char *args []) {
@@ -237,6 +237,12 @@ int run_pipes(State *state, int num_args, char *args []) {
|
|
|
|
|
} while ((piece = (char *)strtok(NULL, "|")) != 0); |
|
|
|
|
|
|
|
|
|
if (num_commands == 1) { |
|
|
|
|
for (int i = 0; i < num_commands; i++) { |
|
|
|
|
free(sep_commands[i]); |
|
|
|
|
}
|
|
|
|
|
free(sep_commands); |
|
|
|
|
free(joined); |
|
|
|
|
// free everything
|
|
|
|
|
return ER_NOT_EXISTS; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -288,81 +294,6 @@ int run_pipes(State *state, int num_args, char *args []) {
@@ -288,81 +294,6 @@ int run_pipes(State *state, int num_args, char *args []) {
|
|
|
|
|
return ER_SUCCESS; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int check_pipes(State* state, int num_args, char* args[]) { |
|
|
|
|
int debug = 1; |
|
|
|
|
|
|
|
|
|
int left = 0; |
|
|
|
|
int num_commands = 0; |
|
|
|
|
int space = 10; |
|
|
|
|
// an array of string-arrays which end in NULL
|
|
|
|
|
char** *pipes = calloc(space, sizeof(char***));
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < num_args; i++) { |
|
|
|
|
if (!strcmp(args[i], "|") || i == num_args - 1) { |
|
|
|
|
int command_length = i - left + 1 + num_commands; |
|
|
|
|
char **command = calloc(command_length, sizeof(char**)); |
|
|
|
|
// copy the command into the array
|
|
|
|
|
for (int k = 0; k < command_length - 1; k++) { |
|
|
|
|
command[k] = args[left + k]; |
|
|
|
|
} |
|
|
|
|
command[command_length - 1] = 0; |
|
|
|
|
|
|
|
|
|
// grow the array if needed
|
|
|
|
|
if (num_commands == space - 2) { |
|
|
|
|
pipes = calloc((space *= 2), sizeof(char***)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pipes[num_commands] = command; |
|
|
|
|
num_commands++; |
|
|
|
|
|
|
|
|
|
left = i + 1; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (debug) { |
|
|
|
|
printf("Running: \n"); |
|
|
|
|
for (int i = 0; i < num_commands; i++) { |
|
|
|
|
printf("%s\n", pipes[i][0]); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fflush(stdout); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (num_commands == 1) { |
|
|
|
|
return ER_NOT_EXISTS; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int pipein[2]; |
|
|
|
|
int pipeout[2]; |
|
|
|
|
memset(pipein, 0, sizeof(int) * 2); |
|
|
|
|
memset(pipeout, 0, sizeof(int) * 2); |
|
|
|
|
|
|
|
|
|
pipe(pipein); |
|
|
|
|
pipe(pipeout); |
|
|
|
|
// 0 is read, 1 is write
|
|
|
|
|
|
|
|
|
|
pid_t child; |
|
|
|
|
|
|
|
|
|
int in; |
|
|
|
|
int out; |
|
|
|
|
|
|
|
|
|
in = 0; |
|
|
|
|
out = pipeout[1]; |
|
|
|
|
|
|
|
|
|
for (int i = 0; i < num_commands; i++) { |
|
|
|
|
execute(in, out, pipes[i], &child); |
|
|
|
|
if (i != num_commands - 1) { |
|
|
|
|
in = pipeout[0]; |
|
|
|
|
out = pipein[1]; |
|
|
|
|
} else { |
|
|
|
|
out = 1;
|
|
|
|
|
in = out - 1; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return ER_SUCCESS; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int check_builtins(State *state, int num_args, char *args[]) { |
|
|
|
|
char *builtins[NUM_BUILTINS] = { |
|
|
|
@ -539,8 +470,8 @@ int expand_command(State *state, int argc, char* argv[]) {
@@ -539,8 +470,8 @@ int expand_command(State *state, int argc, char* argv[]) {
|
|
|
|
|
int run_command(State *state, int argc, char *argv[]) { |
|
|
|
|
pid_t child; |
|
|
|
|
if (check_builtins(state, argc, argv) == ER_NOT_EXISTS) { |
|
|
|
|
execute(0,1, argv, &child); |
|
|
|
|
waitpid(child, NULL, 0); |
|
|
|
|
if (!execute(0,1, argv, &child)) |
|
|
|
|
waitpid(child, NULL, 0); |
|
|
|
|
} |
|
|
|
|
return ER_SUCCESS; |
|
|
|
|
} |
|
|
|
@ -567,9 +498,13 @@ int repl(State* state, FILE *input) {
@@ -567,9 +498,13 @@ int repl(State* state, FILE *input) {
|
|
|
|
|
|
|
|
|
|
split_string(&sep_string, &num_words, line, ' '); |
|
|
|
|
|
|
|
|
|
if (num_words == 0) |
|
|
|
|
if (num_words == 0) { |
|
|
|
|
for (int i = 0; i <= num_words; i ++)
|
|
|
|
|
free(sep_string[i]); |
|
|
|
|
free(sep_string); |
|
|
|
|
free(line); |
|
|
|
|
continue; |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
// int err = check_pipes(state, num_words, sep_string);
|
|
|
|
|
int err = run_pipes(state, num_words, sep_string); |
|
|
|
|
|
|
|
|
@ -578,10 +513,9 @@ int repl(State* state, FILE *input) {
@@ -578,10 +513,9 @@ int repl(State* state, FILE *input) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < num_words; i++) { |
|
|
|
|
for (int i = 0; i <= num_words; i ++) { |
|
|
|
|
free(sep_string[i]); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
free(sep_string); |
|
|
|
|
free(line); |
|
|
|
|
} |
|
|
|
@ -597,6 +531,7 @@ int startup(State *state, int argc, char **argv) {
@@ -597,6 +531,7 @@ int startup(State *state, int argc, char **argv) {
|
|
|
|
|
|
|
|
|
|
char *config_home; |
|
|
|
|
char *config = calloc(500, sizeof(char)); |
|
|
|
|
|
|
|
|
|
if ((config_home = getenv("XDG_CONFIG_HOME"))) { |
|
|
|
|
strcpy(config, config_home); |
|
|
|
|
int len = strlen(config_home); |
|
|
|
@ -607,10 +542,10 @@ int startup(State *state, int argc, char **argv) {
@@ -607,10 +542,10 @@ int startup(State *state, int argc, char **argv) {
|
|
|
|
|
int len = strlen(config_home); |
|
|
|
|
strcpy(config + len, "/.chickenrc"); |
|
|
|
|
} |
|
|
|
|
printf("%s\n", config); |
|
|
|
|
|
|
|
|
|
int err; |
|
|
|
|
FILE* rc = fopen(config, "r"); |
|
|
|
|
free(config); |
|
|
|
|
if (rc) { |
|
|
|
|
state->interactive = false; |
|
|
|
|
state->input = rc; |
|
|
|
|