|
|
|
@ -2,6 +2,7 @@
@@ -2,6 +2,7 @@
|
|
|
|
|
#include "run.h" |
|
|
|
|
#include "main.h" |
|
|
|
|
#include "util.h" |
|
|
|
|
#include <err.h> |
|
|
|
|
|
|
|
|
|
#include <dirent.h> |
|
|
|
|
#include <string.h> |
|
|
|
@ -17,6 +18,8 @@ int run_command(State *state, int argc, char *argv[]);
@@ -17,6 +18,8 @@ int run_command(State *state, int argc, char *argv[]);
|
|
|
|
|
int get_array_element(ArrayElement **ret, GrowingArray* array, char* key);
|
|
|
|
|
int del_array_element(GrowingArray* array, char* key);
|
|
|
|
|
|
|
|
|
|
const int GLOB_DEBUG; |
|
|
|
|
|
|
|
|
|
GrowingArray get_growing_array(size_t num_elements, size_t size_elem) { |
|
|
|
|
GrowingArray arr; |
|
|
|
|
arr.inner = calloc(num_elements, size_elem); |
|
|
|
@ -206,8 +209,11 @@ void shutdown(State *state) {
@@ -206,8 +209,11 @@ void shutdown(State *state) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int run_pipes(State *state, int num_args, char *args []) { |
|
|
|
|
char *joined; |
|
|
|
|
char *joined = 0; |
|
|
|
|
int err = join_string(&joined, num_args, args, " "); |
|
|
|
|
if (err) { |
|
|
|
|
errx(1, "no"); |
|
|
|
|
} |
|
|
|
|
char * separated_by_pipe; |
|
|
|
|
|
|
|
|
|
int pipe_count = 0;
|
|
|
|
@ -216,6 +222,7 @@ int run_pipes(State *state, int num_args, char *args []) {
@@ -216,6 +222,7 @@ int run_pipes(State *state, int num_args, char *args []) {
|
|
|
|
|
pipe_count++; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int num_commands = pipe_count + 1; |
|
|
|
|
|
|
|
|
|
char **sep_commands = calloc(pipe_count + 1, sizeof(char *)); |
|
|
|
@ -229,22 +236,13 @@ int run_pipes(State *state, int num_args, char *args []) {
@@ -229,22 +236,13 @@ int run_pipes(State *state, int num_args, char *args []) {
|
|
|
|
|
i++; |
|
|
|
|
} while ((piece = (char *)strtok(NULL, "|")) != 0); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
int *pipes = calloc(pipe_count * 2, sizeof(int)); |
|
|
|
|
|
|
|
|
|
for (int i = 0; i < num_commands; i++) { |
|
|
|
|
for (int i = 0; i < pipe_count; i++) { |
|
|
|
|
pipe(pipes + (2 * i)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -253,14 +251,6 @@ int run_pipes(State *state, int num_args, char *args []) {
@@ -253,14 +251,6 @@ int run_pipes(State *state, int num_args, char *args []) {
|
|
|
|
|
int in; |
|
|
|
|
int out; |
|
|
|
|
|
|
|
|
|
/*dup2(pipes[pipe_count*2+1], 1);*/ |
|
|
|
|
/*dup2(pipes[0], 0);*/ |
|
|
|
|
|
|
|
|
|
// read = 0
|
|
|
|
|
// write = 1
|
|
|
|
|
|
|
|
|
|
//int split_string(char **sep_string[], int *num, char *string, char delim) {
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < num_commands; i++) { |
|
|
|
|
char **command; |
|
|
|
|
int words; |
|
|
|
@ -279,13 +269,22 @@ int run_pipes(State *state, int num_args, char *args []) {
@@ -279,13 +269,22 @@ int run_pipes(State *state, int num_args, char *args []) {
|
|
|
|
|
|
|
|
|
|
printf("in %d out: %d exec: ", in, out); |
|
|
|
|
for (int i = 0; i < words; i++) { |
|
|
|
|
printf("%s ", command[i]); |
|
|
|
|
} |
|
|
|
|
printf("\n"); |
|
|
|
|
|
|
|
|
|
execute(in, out, command, &child); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int status; |
|
|
|
|
while (!wait(&status)); |
|
|
|
|
|
|
|
|
|
free(pipes); |
|
|
|
|
for (int i = 0; i < num_commands; i++) { |
|
|
|
|
free(sep_commands[i]); |
|
|
|
|
}
|
|
|
|
|
free(sep_commands); |
|
|
|
|
free(joined); |
|
|
|
|
|
|
|
|
|
return ER_SUCCESS; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -389,6 +388,7 @@ int check_builtins(State *state, int num_args, char *args[]) {
@@ -389,6 +388,7 @@ int check_builtins(State *state, int num_args, char *args[]) {
|
|
|
|
|
char **split; |
|
|
|
|
int num; |
|
|
|
|
err = split_string(&split, &num, joined, '='); |
|
|
|
|
printf("%s\n", joined); |
|
|
|
|
if (num == 2) { |
|
|
|
|
char * key = split[0]; |
|
|
|
|
char * value = split[1]; |
|
|
|
@ -567,6 +567,9 @@ int repl(State* state, FILE *input) {
@@ -567,6 +567,9 @@ int repl(State* state, FILE *input) {
|
|
|
|
|
|
|
|
|
|
split_string(&sep_string, &num_words, line, ' '); |
|
|
|
|
|
|
|
|
|
if (num_words == 0) |
|
|
|
|
continue; |
|
|
|
|
|
|
|
|
|
// int err = check_pipes(state, num_words, sep_string);
|
|
|
|
|
int err = run_pipes(state, num_words, sep_string); |
|
|
|
|
|
|
|
|
|