From c74167aa9f0f0ba3b58672f3e0fe4376e0ef5b0c Mon Sep 17 00:00:00 2001 From: Stephen Toth Date: Sat, 23 Sep 2023 00:06:10 -0400 Subject: [PATCH] start tracking via git project is underway with lots of trial and error for figuring it out. sw.c is old code that is only there for reference while reimplementing in new file --- .gitignore | 6 ++ Makefile | 33 +++++++ README.md | 44 +++++++++ src/main.c | 87 +++++++++++++++++ src/sw.c.old | 249 +++++++++++++++++++++++++++++++++++++++++++++++ testing/alpine | 0 testing/alpine01 | 0 testing/alpine02 | 0 testing/alpine03 | 0 9 files changed, 419 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 README.md create mode 100644 src/main.c create mode 100644 src/sw.c.old create mode 100644 testing/alpine create mode 100644 testing/alpine01 create mode 100644 testing/alpine02 create mode 100644 testing/alpine03 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0205b1c --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +# Ignore the build and lib dirs +build/* +#lib/* + +# Ignore any executables +bin/* diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9114944 --- /dev/null +++ b/Makefile @@ -0,0 +1,33 @@ +CC := gcc +SRCDIR := src +BUILDDIR := build +TARGET := bin/cterm +SRCEXT := c +SOURCES := $(shell find $(SRCDIR) -type f -name *.$(SRCEXT)) +OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:.$(SRCEXT)=.o)) +CFLAGS := +LIB := -L lib +INC := -I include + +$(TARGET): $(OBJECTS) + @echo " Linking..." + @echo " $(CC) $^ -o $(TARGET) $(LIB)"; $(CC) $^ -o $(TARGET) $(LIB) -O3 + +$(BUILDDIR)/%.o: $(SRCDIR)/%.$(SRCEXT) + @echo " Building..." + @mkdir -p $(BUILDDIR) + @echo " $(CC) $(CFLAGS) $(INC) -c -o $@ $<"; $(CC) $(CFLAGS) $(INC) -c -o $@ $< -save-temps -O3 + +clean: + @echo " Cleaning..."; + @echo " $(RM) -r $(BUILDDIR) $(TARGET)"; $(RM) -r $(BUILDDIR) $(TARGET) + +# Tests +tester: + $(CC) $(CFLAGS) test/tester.cpp $(INC) $(LIB) -o bin/tester + +# Spikes +ticket: + $(CC) $(CFLAGS) spikes/ticket.cpp $(INC) $(LIB) -o bin/ticket + +.PHONY: clean diff --git a/README.md b/README.md new file mode 100644 index 0000000..d50cb37 --- /dev/null +++ b/README.md @@ -0,0 +1,44 @@ +# c-project-structure + +Project description + +Project image/gif + +## Requirements + +Requirements + +## Installation + +`make` + +## Usage + +`bin/run` + +## Example + +`bin/run` + +## Documentation + +Location of Documentation + +## Directory structure +``` +|-- Makefile <- Project makefile +|-- README <- Project README +|-- bin <- Compiled binaries +| `-- run <- Project main executable +|-- build <- Static objects and intermediate files +|-- data <- Project data +| |-- raw <- Raw data +| |-- interim <- Interm data +| |-- input <- Input data +| |-- output <- Output data +|-- docs <- Documentation +|-- include <- Header files +|-- lib <- Dynamic objects +|-- src <- Source files +`-- tests <- Unit tests +``` diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..aa79cab --- /dev/null +++ b/src/main.c @@ -0,0 +1,87 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define BUFCHUNKSIZE 4096 +#define BUFSAFEPAD 8 +#define KNRM "\x1B[0m" +#define KRED "\x1B[31m" +#define KGRN "\x1B[32m" +#define KYEL "\x1B[33m" +#define KBLU "\x1B[34m" +#define KMAG "\x1B[35m" +#define KCYN "\x1B[36m" +#define KWHT "\x1B[37m" + +void child_process(char *argv[]){ + execl(argv[1], argv[1], NULL); +} + + + + + + +int main(int argc, char *argv[]) { + int master; + pid_t child_pid; + //struct winsize win = { + // .ws_col = 80, .ws_row = 24, + // .ws_xpixel = 480, .ws_ypixel = 192, + //}; + + if (argc < 2) { + printf("Usage: %s cmd [args...]\n", argv[0]); + exit(EX_USAGE); + } + + child_pid = forkpty(&master, NULL, NULL, NULL); + if (child_pid == -1) { + perror("forkpty failed"); + exit(EX_OSERR); + } + if (child_pid == 0) { + child_process(argv); + exit(0); + } + //remainder is main is the parent process for processing input and shit + char childoutbuffer[BUFCHUNKSIZE] = {0}; + fcntl(master, F_SETFL, O_NONBLOCK); + //FILE* thein = fdopen(master, "rw"); + sleep(1); + 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 + // + while(1){ + ssize_t count = read(master, childoutbuffer, BUFCHUNKSIZE); + if (count == -1){ + if(errno == EAGAIN){ + break; + } else exit(1); + } else if (count == 0 ){ + exit(0); + } + write(STDOUT_FILENO, childoutbuffer, count); + } + memset(childoutbuffer, 0, BUFCHUNKSIZE); + fgets(childoutbuffer, BUFCHUNKSIZE, stdin); + + //printf("%s\n", childoutbuffer); + write(master, childoutbuffer, strlen(childoutbuffer)); + + //memset(childoutbuffer, 0, BUFCHUNKSIZE); + //ssize_t count = strlen(childoutbuffer); + usleep(200*1000); + } +} + diff --git a/src/sw.c.old b/src/sw.c.old new file mode 100644 index 0000000..a08a2d2 --- /dev/null +++ b/src/sw.c.old @@ -0,0 +1,249 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define BUFCHUNKSIZE 4096 +#define BUFSAFEPAD 8 +#define KNRM "\x1B[0m" +#define KRED "\x1B[31m" +#define KGRN "\x1B[32m" +#define KYEL "\x1B[33m" +#define KBLU "\x1B[34m" +#define KMAG "\x1B[35m" +#define KCYN "\x1B[36m" +#define KWHT "\x1B[37m" + + + +char* substr_add_color(const char *orig, const char *word_to_match, const char *color) { + //printf("INPUT::%s\n", orig); + char* output_string = malloc(BUFCHUNKSIZE+ 10); //malloc first so free in other code doesn't break + strncpy(output_string, orig, BUFCHUNKSIZE); //copy the orig into the buffer so if need return early, we can + //if (strlen(orig) > 16) memcpy(output_string + (strlen(orig)-15), "testing", 7); + //return output_string; + char* instances[BUFCHUNKSIZE] = {0}; + char* pickup_point = (char *)orig; //pointer into orig where we need to start checking again; init to start of orig + + int instance_count = 0; + while (1) { + char* nxt_found = strstr(pickup_point, word_to_match); + if (nxt_found == NULL) break; + pickup_point = nxt_found + strlen(word_to_match); + instances[instance_count] = nxt_found; + instance_count++; + } + if (instance_count == 0) return output_string; // <=== + + + /*printf("FUNC: %d\n", instance_count); + for (int i =0; i 0){ + //childoutbuffer[count] = '\0'; + //char* added = substr_add_color(childoutbuffer, "alpine", KRED); + //memset(childoutbuffer, '\0', BUFCHUNKSIZE); + //strncpy(childoutbuffer, added, strlen(added)); + + write(STDOUT_FILENO, childoutbuffer, count); + //write(STDOUT_FILENO, childoutbuffer, strlen(added)); + //free(added); + //printf("%s", childoutbuffer); + } + count = read(master, childoutbuffer, BUFCHUNKSIZE); + if (count == 0 ){ + break; + } else if(count > 0){ + //childoutbuffer[count] = '\0'; + //char* added = substr_add_color(childoutbuffer, "alpine", KRED); + //memset(childoutbuffer, '\0', BUFCHUNKSIZE); + //strncpy(childoutbuffer, added, strlen(added)); + + write(STDOUT_FILENO, childoutbuffer, count); + //write(STDOUT_FILENO, childoutbuffer, strlen(added)); + //free(added); + //printf("%s", childoutbuffer); + } + } +} + + + + +/*int main ( int argc, char *argv[] ){ + if (argc != 2) return 1; + //int catchin[2]; + int catchout[2]; + //if ( pipe(catchin) == -1 ) { + // perror("pipe"); + // exit(1); + //} + if ( pipe(catchout) == -1 ) { + perror("pipe"); + exit(1); + } + //setup new process + pid_t child_shell = fork(); + if ( child_shell == 0 ){ + //child + //dup2( catchin[0], STDIN_FILENO ); + //close(catchin[0]); + //close(catchin[1]); + //close(catchout[1]); + dup2( catchout[1], STDOUT_FILENO ); + //dup2( catchout[1], STDERR_FILENO ); + close(catchout[0]); + execl(argv[1], argv[1], NULL); + } else if ( child_shell > 0 ){ + //parent + //close(catchin[0]); + //FILE* pipeout = fdopen(catchin[1], "w"); + //char childinbuffer[BUFCHUNKSIZE] = {0}; + close(catchout[1]); + FILE* pipein = fdopen(catchout[0], "r"); + char childoutbuffer[BUFCHUNKSIZE] = {0}; + 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); + //fgets(childoutbuffer, BUFCHUNKSIZE, pipein); + //ssize_t count = strlen(childoutbuffer); + ssize_t count = read(catchout[0], childoutbuffer, BUFCHUNKSIZE); + if (count == 0 ){ + break; + } else if(count > 0){ + //childoutbuffer[count] = '\0'; + //char* added = substr_add_color(childoutbuffer, "alpine", KRED); + //memset(childoutbuffer, '\0', BUFCHUNKSIZE); + //strncpy(childoutbuffer, added, strlen(added)); + + write(STDOUT_FILENO, childoutbuffer, count); + //write(STDOUT_FILENO, childoutbuffer, strlen(added)); + //free(added); + //printf("%s", childoutbuffer); + } + } + close(catchout[0]); + } + return 0; +}*/ diff --git a/testing/alpine b/testing/alpine new file mode 100644 index 0000000..e69de29 diff --git a/testing/alpine01 b/testing/alpine01 new file mode 100644 index 0000000..e69de29 diff --git a/testing/alpine02 b/testing/alpine02 new file mode 100644 index 0000000..e69de29 diff --git a/testing/alpine03 b/testing/alpine03 new file mode 100644 index 0000000..e69de29