diff --git a/Makefile b/Makefile index c51ce09..bab32e5 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ TARGET := bin/cterm SRCEXT := c SOURCES := $(shell find $(SRCDIR) -type f -name *.$(SRCEXT)) OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:.$(SRCEXT)=.o)) -CFLAGS := -Wall -lreadline +CFLAGS := -lreadline LIB := -L lib -lreadline INC := -I include @@ -24,7 +24,7 @@ clean: @echo " Cleaning..."; @echo " $(RM) -r $(BUILDDIR) $(TARGET)"; $(RM) -r $(BUILDDIR) $(TARGET) -debug: CFLAGS += -ggdb -O0 -D"DEBUG=1" +debug: CFLAGS += -ggdb -O0 -Wall -Wextra -pedantic -D"DEBUG=1" debug: $(TARGET) test: @@ -33,6 +33,7 @@ test: 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 diff --git a/include/debug.h b/include/debug.h new file mode 100644 index 0000000..0c183b8 --- /dev/null +++ b/include/debug.h @@ -0,0 +1,61 @@ +#if defined(DEBUG) && DEBUG > 0 + #define DEBUG_PRINT(fmt, ...) fprintf(stderr, "DEBUG: %s:%d:%s(): " fmt, \ + __FILE__, __LINE__, __func__, ##__VA_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 + + + +// You must free the result if result is non-NULL. +char *str_replace(char *orig, char *rep, char *with) { + char *result; // the return string + char *ins; // the next insert point + char *tmp; // varies + int len_rep; // length of rep (the string to remove) + int len_with; // length of with (the string to replace rep with) + int len_front; // distance between rep and end of last rep + int count; // number of replacements + + // sanity checks and initialization + if (!orig || !rep) + return NULL; + len_rep = strlen(rep); + if (len_rep == 0) + return NULL; // empty rep causes infinite loop during count + if (!with) + with = ""; + len_with = strlen(with); + + // count the number of replacements needed + ins = orig; + for (count = 0; tmp = strstr(ins, rep); ++count) { + ins = tmp + len_rep; + } + + tmp = result = malloc(strlen(orig) + (len_with - len_rep) * count + 1); + + if (!result) + return NULL; + + // first time through the loop, all the variable are set correctly + // from here on, + // tmp points to the end of the result string + // ins points to the next occurrence of rep in orig + // orig points to the remainder of orig after "end of rep" + while (count--) { + ins = strstr(orig, rep); + len_front = ins - orig; + tmp = strncpy(tmp, orig, len_front) + len_front; + tmp = strcpy(tmp, with) + len_with; + orig += len_front + len_rep; // move to next "end of rep" + } + strcpy(tmp, orig); + return result; +} \ No newline at end of file diff --git a/src/main.c b/src/main.c index 68c3bf8..2b48aba 100644 --- a/src/main.c +++ b/src/main.c @@ -9,7 +9,10 @@ #include #include #include -#include +//#include + +// Custom debug include for some debug macros and functions +#include #define BUFCHUNKSIZE 4096 #define BUFSAFEPAD 8 @@ -30,34 +33,31 @@ #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); } void processline(char *line){ - char debugstring[] = "HEHE:\x1B[31m"; - char endstring[] = "\x1B[0m\n"; - write(STDOUT_FILENO, debugstring, strlen(debugstring)); - write(STDOUT_FILENO, line, strlen(line)); + char debugstring[] = "HEHE:"KRED; + char endstring[] = KNRM"\n"; + char *newline; + newline = str_replace(line, "rw-", KRED"rw-"KNRM); + //write(STDOUT_FILENO, debugstring, strlen(debugstring)); + if (newline == NULL) { + newline = line; + } + newline = str_replace(newline, "r--", KBLU"r--"KNRM); + //write(STDOUT_FILENO, debugstring, strlen(debugstring)); + if (newline == NULL) { + newline = line; + } + write(STDOUT_FILENO, newline, strlen(newline)); + if (newline != NULL) free(newline); write(STDOUT_FILENO, endstring, strlen(endstring)); } -// go through the buffer and find the lines to be edited and print them after -void processbufferpop(char *roll_buffer, size_t read_length) { +void processbuffer(char *roll_buffer, size_t read_length) { //this is where some optimization could come in with using malloc and then freeing subset of memory; dog garn dynamic memory allocation int found_index; char *found_char = strtok(roll_buffer, "\r\n"); @@ -66,7 +66,6 @@ void processbufferpop(char *roll_buffer, size_t read_length) { found_char = strtok(NULL, "\r\n"); } //no more instances - //printf("\n\nFOUND: %s\n", found_char); } @@ -97,7 +96,7 @@ int main(int argc, char *argv[]) { fcntl(master, F_SETFL, O_NONBLOCK); char *child_out_roll_buffer = (char*) malloc(sizeof(char) * (BUFCHUNKSIZE+BUFSAFEPAD+1)); - usleep(500*1000); + usleep(300*1000); //wait for child shell to init; bad practice, but it works for now while(1){ // 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 @@ -105,6 +104,7 @@ int main(int argc, char *argv[]) { // 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 + // Later on: it kinda has that behavior, I cant confirm rn, but it "works" while(1){ memset(child_out_roll_buffer, 0, BUFCHUNKSIZE+BUFSAFEPAD); ssize_t read_count = read(master, &child_out_roll_buffer[0], BUFCHUNKSIZE - 0); @@ -121,7 +121,7 @@ int main(int argc, char *argv[]) { } } //process da block then write it - processbufferpop(child_out_roll_buffer, read_count); + processbuffer(child_out_roll_buffer, read_count); } //INPUT HANDLING