Browse Source

OMFGSHDLASHDLAJ PIPES WORK PARTIALLY

master
alistair 4 years ago
parent
commit
1bec572291
  1. 7
      Makefile
  2. 103
      main.c

7
Makefile

@ -3,8 +3,8 @@ GCC=gcc -g -lreadline @@ -3,8 +3,8 @@ GCC=gcc -g -lreadline
.PHONY default: build
build: util run read main
$(GCC) main.o read.o run.o util.o -o chicken
build: util run read main array
$(GCC) main.o read.o run.o util.o erray.o -o chicken
main: main.c
$(GCC) -c main.c
@ -18,6 +18,9 @@ run: run.c run.h @@ -18,6 +18,9 @@ run: run.c run.h
util: util.c util.h
$(GCC) -c util.c
array: erray.c erray.h
$(GCC) -c erray.c
clean:
rm chicken
rm *.o

103
main.c

@ -4,6 +4,7 @@ @@ -4,6 +4,7 @@
#include "util.h"
#include <dirent.h>
#include <string.h>
#include <readline/readline.h>
#include <readline/history.h>
@ -204,7 +205,93 @@ void shutdown(State *state) { @@ -204,7 +205,93 @@ void shutdown(State *state) {
exit(0);
}
int run_pipes(State *state, int num_args, char *args []) {
char *joined;
int err = join_string(&joined, num_args, args, " ");
char * separated_by_pipe;
int pipe_count = 0;
for (int i = 0; i < strlen(joined); i++) {
if (joined[i] == '|') {
pipe_count++;
}
}
int num_commands = pipe_count + 1;
char **sep_commands = calloc(pipe_count + 1, sizeof(char *));
int i = 0;
char *piece = strtok(joined, "|");
do {
sep_commands[i] = calloc(strlen(piece) + 1, sizeof(char));
strcpy(sep_commands[i], piece);
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++) {
pipe(pipes + (2 * i));
}
pid_t child;
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;
int err = split_string(&command, &words, sep_commands[i], ' ');
if (i == 0) {
in = 0;
out = pipes[2*i + 1];
} else if (i == num_commands - 1) {
in = pipes[2*(i-1)];
out = 1;
} else {
in = pipes[2*(i-1)];
out = pipes[2*i + 1];
}
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);
}
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;
@ -233,6 +320,16 @@ int check_pipes(State* state, int num_args, char* args[]) { @@ -233,6 +320,16 @@ int check_pipes(State* state, int num_args, char* args[]) {
}
}
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;
}
@ -455,6 +552,9 @@ int repl(State* state, FILE *input) { @@ -455,6 +552,9 @@ int repl(State* state, FILE *input) {
if (state->interactive) {
line = readline(state->ps1);
if (!line)
continue;
add_history(line);
} else {
if (get_line(input, &line)) {
@ -467,7 +567,8 @@ int repl(State* state, FILE *input) { @@ -467,7 +567,8 @@ int repl(State* state, FILE *input) {
split_string(&sep_string, &num_words, line, ' ');
int err = check_pipes(state, num_words, sep_string);
// int err = check_pipes(state, num_words, sep_string);
int err = run_pipes(state, num_words, sep_string);
if (err == ER_NOT_EXISTS) {
expand_command(state, num_words, sep_string);

Loading…
Cancel
Save