From 00cf0f7b6a473a2193555e4632ce97a67b0c7a8a Mon Sep 17 00:00:00 2001 From: Stephen Toth Date: Sun, 24 Sep 2023 15:23:15 -0400 Subject: [PATCH] get ready for sharing --- Makefile | 1 + README.md | 9 +++++- include/debug.h | 4 +-- include/notmine.h | 4 +-- include/structdef.h | 5 ++++ src/main.c | 69 ++++++++++++++++++++++++++++++++------------- 6 files changed, 67 insertions(+), 25 deletions(-) create mode 100644 include/structdef.h diff --git a/Makefile b/Makefile index bab32e5..e170ebe 100644 --- a/Makefile +++ b/Makefile @@ -22,6 +22,7 @@ $(BUILDDIR)/%.o: $(SRCDIR)/%.$(SRCEXT) clean: @echo " Cleaning..."; + @echo " cat /dev/null > $(TESTDIR)/trace.lp"; cat /dev/null > $(TESTDIR)/trace.lp; @echo " $(RM) -r $(BUILDDIR) $(TARGET)"; $(RM) -r $(BUILDDIR) $(TARGET) debug: CFLAGS += -ggdb -O0 -Wall -Wextra -pedantic -D"DEBUG=1" diff --git a/README.md b/README.md index a8c277f..c40301e 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,14 @@ RHEL-like: `sudo dnf install readline-devel` Debian[12]: -`sudo apt install libreadline-dev` +`sudo apt install build-essential libreadline-dev make` + +## Building from source + +One Liner for Debain-based distros: +` sudo apt install build-essential libreadline-dev make git; git clone https://git.stephenltoth.com/icaema/colorwrap.git; cd colorwrap; make;` + +Run: `bin/cterm /bin/sh` ## Installation diff --git a/include/debug.h b/include/debug.h index 672ed13..fef4c9a 100644 --- a/include/debug.h +++ b/include/debug.h @@ -1,5 +1,5 @@ -#ifndef DEBUG_H -#define DEBUG_H +#ifndef _DEBUG_H +#define _DEBUG_H #if defined(DEBUG) && DEBUG > 0 #define DEBUG_PRINT(fmt, ...) fprintf(stderr, "DEBUG: %s:%d:%s(): " fmt, \ diff --git a/include/notmine.h b/include/notmine.h index 004a40d..41b25c5 100644 --- a/include/notmine.h +++ b/include/notmine.h @@ -1,5 +1,5 @@ -#ifndef NOTMINE_H -#define NOTMINE_H +#ifndef _NOTMINE_H +#define _NOTMINE_H // You must free the result if result is non-NULL. char *str_replace(char *orig, char *rep, char *with); diff --git a/include/structdef.h b/include/structdef.h new file mode 100644 index 0000000..ccd7494 --- /dev/null +++ b/include/structdef.h @@ -0,0 +1,5 @@ +#ifndef _STRUCTDEF_H +#define _STRUCTDEF_H + + +#endif \ No newline at end of file diff --git a/src/main.c b/src/main.c index 64cf10a..50367b9 100644 --- a/src/main.c +++ b/src/main.c @@ -15,6 +15,7 @@ #include #include +#define VERSIONSTRING "0.2" #define BUFCHUNKSIZE 4096 #define BUFSAFEPAD 8 #define KNRM "\x1B[0m" @@ -33,37 +34,59 @@ #define BKCYN "\x1B[46m" #define BKWHT "\x1B[47m" +//temporary +char *replace_rules[][2] = { + { "r--", KGRN"r--"KNRM }, + { "rw-", KRED"rw-"KNRM }, + { "alpine", KMAG"alpine"KNRM }, + { " ..$", KBLU" .."KNRM"$" }, + { " .$", KBLU" ."KNRM"$" }, + { "^d", "^"KMAG"d"KNRM }, +}; +char ps1[] = KGRN"~>"KNRM; +char ps1_ack[] = KBLU"1~"KNRM; + + void child_process(char *argv[]){ execl(argv[1], argv[1], NULL); } -void processline(char *line){ - //char debugstring[] = "HEHE:"KRED; +void processline(const char *line, int first_line){ + size_t line_length = strlen(line); + char copyline[line_length+3]; + copyline[0] = '^'; + strncpy(©line[1], line, line_length); //yes, I know this is "jank" its fine tho. (see next) + // 1. line *should* always be NULL terminated + // 2. line is ALWAYS < 4097 + copyline[line_length+1] = '$'; + copyline[line_length+2] = '\0'; + //strncat(copyline, "$", 2); + char *newline = (char *)©line; 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; + //if (first_line) newline = str_replace(newline, "^", "^User Input: "); + if (first_line) write(STDOUT_FILENO, ps1_ack, strlen(ps1_ack)); + for(unsigned int i = 0; i<(sizeof(replace_rules)/sizeof(replace_rules[0])); i++){ + newline = str_replace(newline, replace_rules[i][0], replace_rules[i][1]); + //write(STDOUT_FILENO, debugstring, strlen(debugstring)); + if (newline == NULL) { + newline = copyline; + } } - 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)); + write(STDOUT_FILENO, newline+1, strlen(newline)-2); if (newline != NULL) free(newline); write(STDOUT_FILENO, endstring, strlen(endstring)); } -void processbuffer(char *roll_buffer, size_t read_length) { +void processbuffer(char *roll_buffer, size_t read_length, int first_read) { //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"); + int first_line = 1; while(found_char != NULL){ - processline(found_char); + processline(found_char, first_line&&first_read); + first_line = 0; found_char = strtok(NULL, "\r\n"); } //no more instances @@ -71,6 +94,10 @@ void processbuffer(char *roll_buffer, size_t read_length) { int main(int argc, char *argv[]) { + printf(" --- cterm v%s ---\n" , VERSIONSTRING); + printf(" -- %d replace rules loaded --\n", (int *)(sizeof(replace_rules)/sizeof(replace_rules[0]))); + printf(" --- ---\n"); + printf("\n"); COLORTEST(); int master; pid_t child_pid; @@ -96,6 +123,7 @@ int main(int argc, char *argv[]) { //remainder is main is the parent process for processing input and shit fcntl(master, F_SETFL, O_NONBLOCK); char *child_out_roll_buffer = (char*) malloc(sizeof(char) * (BUFCHUNKSIZE+BUFSAFEPAD+1)); + char *parent_in_buffer = (char*) malloc(sizeof(char) * (BUFCHUNKSIZE+BUFSAFEPAD+1)); usleep(300*1000); //wait for child shell to init; bad practice, but it works for now while(1){ @@ -106,6 +134,7 @@ int main(int argc, char *argv[]) { // 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" + int first_read = 1; while(1){ memset(child_out_roll_buffer, 0, BUFCHUNKSIZE+BUFSAFEPAD); ssize_t read_count = read(master, &child_out_roll_buffer[0], BUFCHUNKSIZE - 0); @@ -122,14 +151,14 @@ int main(int argc, char *argv[]) { } } //process da block then write it - processbuffer(child_out_roll_buffer, read_count); + processbuffer(child_out_roll_buffer, read_count, first_read); + first_read = 0; } //INPUT HANDLING - memset(child_out_roll_buffer, 0, BUFCHUNKSIZE); - strncpy(child_out_roll_buffer, readline(KGRN"~>"KNRM), BUFCHUNKSIZE-1); - - write(master, child_out_roll_buffer, strlen(child_out_roll_buffer)); + memset(parent_in_buffer, 0, BUFCHUNKSIZE); + strncpy(parent_in_buffer, readline(ps1), BUFCHUNKSIZE-1); + write(master, parent_in_buffer, strlen(parent_in_buffer)); write(master, "\n", 1); usleep(100*1000);