diff --git a/Makefile b/Makefile index a6a13b0..fae272d 100644 --- a/Makefile +++ b/Makefile @@ -24,12 +24,17 @@ clean: @echo " Cleaning..."; @echo " $(RM) -r $(BUILDDIR) $(TARGET)"; $(RM) -r $(BUILDDIR) $(TARGET) +debug: CFLAGS += -D"DEBUG=1" +debug: $(TARGET) + test: $(TARGET) /bin/sh trace: + cat /dev/null > $(TESTDIR)/trace.lp; strace -o $(TESTDIR)/trace.lp $(TARGET) /bin/sh tracef: + cat /dev/null > $(TESTDIR)/trace.lp; strace -fo $(TESTDIR)/trace.lp $(TARGET) /bin/sh .PHONY: clean diff --git a/src/main.c b/src/main.c index 4da8234..ca421cb 100644 --- a/src/main.c +++ b/src/main.c @@ -21,6 +21,28 @@ #define KMAG "\x1B[35m" #define KCYN "\x1B[36m" #define KWHT "\x1B[37m" +#define BKRED "\x1B[41m" +#define BKGRN "\x1B[42m" +#define BKYEL "\x1B[43m" +#define BKBLU "\x1B[44m" +#define BKMAG "\x1B[45m" +#define BKCYN "\x1B[46m" +#define BKWHT "\x1B[47m" + + +#if defined(DEBUG) && DEBUG > 0 + #define DEBUG_PRINT(fmt, args...) fprintf(stderr, "DEBUG: %s:%d:%s(): " fmt, \ + __FILE__, __LINE__, __func__, ##args) + #define COLORTEST() fprintf(stderr, "Color Palate:\n" \ + KRED"KRED"KGRN"KGRN"KYEL"KYEL"KBLU"KBLU"KMAG"KMAG"KCYN"KCYN"KWHT"KWHT"KNRM"\n" \ + KRED"▒▒▒▒"KGRN"▒▒▒▒"KYEL"▒▒▒▒"KBLU"▒▒▒▒"KMAG"▒▒▒▒"KCYN"▒▒▒▒"KWHT"▒▒▒▒"KNRM"\n" \ + KRED"████"KGRN"████"KYEL"████"KBLU"████"KMAG"████"KCYN"████"KWHT"████"KNRM"\n" \ + BKRED"KRED"BKGRN"KGRN"BKYEL"KYEL"BKBLU"KBLU"BKMAG"KMAG"BKCYN"KCYN"BKWHT"KWHT"KNRM"\n" \ + ) +#else + #define DEBUG_PRINT(fmt, args...) /* Don't do anything in release builds */ + #define COLORTEST() +#endif void child_process(char *argv[]){ execl(argv[1], argv[1], NULL); @@ -45,8 +67,23 @@ size_t getuserinput(char* input_buffer, size_t max_buffer_size, int fd) { } +// go through the buffer and find the lines to be edited and print them after +int processbufferpop(char *roll_buffer, size_t read_length) { + + /*for (int i; i<= read_length; i++){ + if (roll_buffer[i] = '\n'){ + } + }*/ + + write(STDOUT_FILENO, roll_buffer, read_length); + write(STDOUT_FILENO, "\n", 1); + + return 0; +} + int main(int argc, char *argv[]) { + COLORTEST(); int master; pid_t child_pid; //struct winsize win = { @@ -68,38 +105,50 @@ int main(int argc, char *argv[]) { child_process(argv); exit(0); } + //remainder is main is the parent process for processing input and shit - char childoutbuffer[BUFCHUNKSIZE] = {0}; + char child_out_roll_buffer[BUFCHUNKSIZE+BUFSAFEPAD] = {0}; fcntl(master, F_SETFL, O_NONBLOCK); - //FILE* thein = fdopen(master, "rw"); - sleep(1); + usleep(500*1000); while(1){ - //ssize_t count = read(catchout[0], childoutbuffer, BUFCHUNKSIZE); - //ssize_t count = read(catchout[0], childoutbuffer, sizeof(childoutbuffer)); // ideally I would want to read from the pipe and buffer the output, rewriting the line on change // whenever it sees a newline, it should send it to the processor and print to stdout, with that newline // - memset(childoutbuffer, 0, BUFCHUNKSIZE); + //CHILD OUTPUT HANDLING + memset(child_out_roll_buffer, 0, BUFCHUNKSIZE+BUFSAFEPAD); + int cont_read_offset_index = 0; //when we need to continue reading after process leaves us with half filled buffer + // treating the buffer as kind of a FIFO stack thing + // strip lines from top and then move leftover to top, + // then start reading where we left off while(1){ - ssize_t count = read(master, childoutbuffer, BUFCHUNKSIZE); - if (count == -1){ - if(errno == EAGAIN){ - break; - } else exit(0); - } else if (count == 0 ){ - exit(0); + ssize_t read_count = read(master, &child_out_roll_buffer[cont_read_offset_index], BUFCHUNKSIZE - cont_read_offset_index); + if (read_count == 0 ) { + DEBUG_PRINT("EOF\n"); + exit(0); } - write(STDOUT_FILENO, childoutbuffer, count); + if (read_count == -1){ + if(errno == EAGAIN){ //errored because read would block + break; + } else { + DEBUG_PRINT("I/O \"Error\" (Child Exited?) %d\n", errno); + exit(0); + } + } + //process da block then write it + cont_read_offset_index = processbufferpop(child_out_roll_buffer, read_count); + } - memset(childoutbuffer, 0, BUFCHUNKSIZE); - strncpy(childoutbuffer, readline("~>"), BUFCHUNKSIZE-1); + + //INPUT HANDLING + memset(child_out_roll_buffer, 0, BUFCHUNKSIZE); + strncpy(child_out_roll_buffer, readline(KGRN"~>"KNRM), BUFCHUNKSIZE-1); - //printf("%s\n", childoutbuffer); - write(master, childoutbuffer, strlen(childoutbuffer)); + //printf("%s\n", child_out_roll_buffer); + write(master, child_out_roll_buffer, strlen(child_out_roll_buffer)); write(master, "\n", 1); - //memset(childoutbuffer, 0, BUFCHUNKSIZE); - //ssize_t count = strlen(childoutbuffer); + //memset(child_out_roll_buffer, 0, BUFCHUNKSIZE); + //ssize_t count = strlen(child_out_roll_buffer); usleep(200*1000); } }