get ready for sharing

This commit is contained in:
Stephen Toth 2023-09-24 15:23:15 -04:00
parent 4d2d84217f
commit 00cf0f7b6a
6 changed files with 67 additions and 25 deletions

View File

@ -22,6 +22,7 @@ $(BUILDDIR)/%.o: $(SRCDIR)/%.$(SRCEXT)
clean: clean:
@echo " Cleaning..."; @echo " Cleaning...";
@echo " cat /dev/null > $(TESTDIR)/trace.lp"; cat /dev/null > $(TESTDIR)/trace.lp;
@echo " $(RM) -r $(BUILDDIR) $(TARGET)"; $(RM) -r $(BUILDDIR) $(TARGET) @echo " $(RM) -r $(BUILDDIR) $(TARGET)"; $(RM) -r $(BUILDDIR) $(TARGET)
debug: CFLAGS += -ggdb -O0 -Wall -Wextra -pedantic -D"DEBUG=1" debug: CFLAGS += -ggdb -O0 -Wall -Wextra -pedantic -D"DEBUG=1"

View File

@ -12,7 +12,14 @@ RHEL-like:
`sudo dnf install readline-devel` `sudo dnf install readline-devel`
Debian[12]: 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 ## Installation

View File

@ -1,5 +1,5 @@
#ifndef DEBUG_H #ifndef _DEBUG_H
#define DEBUG_H #define _DEBUG_H
#if defined(DEBUG) && DEBUG > 0 #if defined(DEBUG) && DEBUG > 0
#define DEBUG_PRINT(fmt, ...) fprintf(stderr, "DEBUG: %s:%d:%s(): " fmt, \ #define DEBUG_PRINT(fmt, ...) fprintf(stderr, "DEBUG: %s:%d:%s(): " fmt, \

View File

@ -1,5 +1,5 @@
#ifndef NOTMINE_H #ifndef _NOTMINE_H
#define NOTMINE_H #define _NOTMINE_H
// You must free the result if result is non-NULL. // You must free the result if result is non-NULL.
char *str_replace(char *orig, char *rep, char *with); char *str_replace(char *orig, char *rep, char *with);

5
include/structdef.h Normal file
View File

@ -0,0 +1,5 @@
#ifndef _STRUCTDEF_H
#define _STRUCTDEF_H
#endif

View File

@ -15,6 +15,7 @@
#include <debug.h> #include <debug.h>
#include <notmine.h> #include <notmine.h>
#define VERSIONSTRING "0.2"
#define BUFCHUNKSIZE 4096 #define BUFCHUNKSIZE 4096
#define BUFSAFEPAD 8 #define BUFSAFEPAD 8
#define KNRM "\x1B[0m" #define KNRM "\x1B[0m"
@ -33,37 +34,59 @@
#define BKCYN "\x1B[46m" #define BKCYN "\x1B[46m"
#define BKWHT "\x1B[47m" #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[]){ void child_process(char *argv[]){
execl(argv[1], argv[1], NULL); execl(argv[1], argv[1], NULL);
} }
void processline(char *line){ void processline(const char *line, int first_line){
//char debugstring[] = "HEHE:"KRED; size_t line_length = strlen(line);
char copyline[line_length+3];
copyline[0] = '^';
strncpy(&copyline[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 *)&copyline;
char endstring[] = KNRM"\n"; char endstring[] = KNRM"\n";
char *newline; //if (first_line) newline = str_replace(newline, "^", "^User Input: ");
newline = str_replace(line, "rw-", KRED"rw-"KNRM); if (first_line) write(STDOUT_FILENO, ps1_ack, strlen(ps1_ack));
//write(STDOUT_FILENO, debugstring, strlen(debugstring)); for(unsigned int i = 0; i<(sizeof(replace_rules)/sizeof(replace_rules[0])); i++){
if (newline == NULL) { newline = str_replace(newline, replace_rules[i][0], replace_rules[i][1]);
newline = line; //write(STDOUT_FILENO, debugstring, strlen(debugstring));
if (newline == NULL) {
newline = copyline;
}
} }
newline = str_replace(newline, "r--", KBLU"r--"KNRM); write(STDOUT_FILENO, newline+1, strlen(newline)-2);
//write(STDOUT_FILENO, debugstring, strlen(debugstring));
if (newline == NULL) {
newline = line;
}
write(STDOUT_FILENO, newline, strlen(newline));
if (newline != NULL) free(newline); if (newline != NULL) free(newline);
write(STDOUT_FILENO, endstring, strlen(endstring)); 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 //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; //int found_index;
char *found_char = strtok(roll_buffer, "\r\n"); char *found_char = strtok(roll_buffer, "\r\n");
int first_line = 1;
while(found_char != NULL){ while(found_char != NULL){
processline(found_char); processline(found_char, first_line&&first_read);
first_line = 0;
found_char = strtok(NULL, "\r\n"); found_char = strtok(NULL, "\r\n");
} }
//no more instances //no more instances
@ -71,6 +94,10 @@ void processbuffer(char *roll_buffer, size_t read_length) {
int main(int argc, char *argv[]) { 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(); COLORTEST();
int master; int master;
pid_t child_pid; 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 //remainder is main is the parent process for processing input and shit
fcntl(master, F_SETFL, O_NONBLOCK); fcntl(master, F_SETFL, O_NONBLOCK);
char *child_out_roll_buffer = (char*) malloc(sizeof(char) * (BUFCHUNKSIZE+BUFSAFEPAD+1)); 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 usleep(300*1000); //wait for child shell to init; bad practice, but it works for now
while(1){ while(1){
@ -106,6 +134,7 @@ int main(int argc, char *argv[]) {
// strip lines from top and then move leftover to top, // strip lines from top and then move leftover to top,
// then start reading where we left off // then start reading where we left off
// Later on: it kinda has that behavior, I cant confirm rn, but it "works" // Later on: it kinda has that behavior, I cant confirm rn, but it "works"
int first_read = 1;
while(1){ while(1){
memset(child_out_roll_buffer, 0, BUFCHUNKSIZE+BUFSAFEPAD); memset(child_out_roll_buffer, 0, BUFCHUNKSIZE+BUFSAFEPAD);
ssize_t read_count = read(master, &child_out_roll_buffer[0], BUFCHUNKSIZE - 0); 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 //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 //INPUT HANDLING
memset(child_out_roll_buffer, 0, BUFCHUNKSIZE); memset(parent_in_buffer, 0, BUFCHUNKSIZE);
strncpy(child_out_roll_buffer, readline(KGRN"~>"KNRM), BUFCHUNKSIZE-1); strncpy(parent_in_buffer, readline(ps1), BUFCHUNKSIZE-1);
write(master, parent_in_buffer, strlen(parent_in_buffer));
write(master, child_out_roll_buffer, strlen(child_out_roll_buffer));
write(master, "\n", 1); write(master, "\n", 1);
usleep(100*1000); usleep(100*1000);