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
This commit is contained in:
commit
c74167aa9f
|
|
@ -0,0 +1,6 @@
|
||||||
|
# Ignore the build and lib dirs
|
||||||
|
build/*
|
||||||
|
#lib/*
|
||||||
|
|
||||||
|
# Ignore any executables
|
||||||
|
bin/*
|
||||||
|
|
@ -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
|
||||||
|
|
@ -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
|
||||||
|
```
|
||||||
|
|
@ -0,0 +1,87 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <pty.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <sysexits.h>
|
||||||
|
|
||||||
|
#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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -0,0 +1,249 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <pty.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <sysexits.h>
|
||||||
|
|
||||||
|
#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<instance_count; i++){
|
||||||
|
printf("[*]DEBUG: %d: %p, %s\n", i, instances[i], instances[i]);
|
||||||
|
}*/
|
||||||
|
|
||||||
|
// we know we need to actually do shit now
|
||||||
|
int outbufsize = strlen(orig) + ( strlen(color) + strlen(KNRM) * instance_count); //Calculate length of buffer needed
|
||||||
|
output_string = realloc(output_string, outbufsize + BUFSAFEPAD); //this padding is not really needed
|
||||||
|
if (output_string == NULL) return (char *)orig;
|
||||||
|
memset(output_string, '\0', outbufsize+BUFSAFEPAD);
|
||||||
|
|
||||||
|
|
||||||
|
int orig_read_index, buf_write_index;
|
||||||
|
orig_read_index = buf_write_index = (instances[0] - orig); //these are for the loop, initalized to the part where we need to start writing
|
||||||
|
//-rw-r--r--. 1 user group 0 Sep 19 20:05
|
||||||
|
// write_index=^
|
||||||
|
strncpy(output_string, orig, orig_read_index);
|
||||||
|
|
||||||
|
//printf("count: %d, insize: %d, outbufsize: %d\n", instance_count, strlen(orig), outbufsize);
|
||||||
|
for (int i = 0; i<instance_count; i++){
|
||||||
|
int wlen = strlen(word_to_match);
|
||||||
|
int clen = strlen(color);
|
||||||
|
int rlen = strlen(KNRM);
|
||||||
|
//printf("wlen: %d, clen: %d, rlen: %d\n", wlen, clen, rlen);
|
||||||
|
strncpy(output_string + buf_write_index, (char *)color, clen);
|
||||||
|
buf_write_index = buf_write_index + clen;
|
||||||
|
strncpy(output_string + buf_write_index, (char *)word_to_match, wlen);
|
||||||
|
buf_write_index += wlen;
|
||||||
|
orig_read_index += wlen;
|
||||||
|
strncpy(output_string + buf_write_index, (char *)KNRM, rlen);
|
||||||
|
buf_write_index += rlen;
|
||||||
|
strncpy(output_string + buf_write_index, (char *)orig + orig_read_index, outbufsize-buf_write_index);
|
||||||
|
break;
|
||||||
|
if (i == instance_count - 1) break;
|
||||||
|
//strncpy(output_string + buf_write_index, orig + orig_read_index, (instances[i+1] - (instances[i]+ wlen)));
|
||||||
|
//buf_write_index += (instances[i+1] - instances[i] + wlen);
|
||||||
|
//orig_read_index += (instances[i+1] - instances[i] + wlen);
|
||||||
|
}
|
||||||
|
//printf("orig_read_index: %d, buf_write_index: %d\n", orig_read_index, buf_write_index);
|
||||||
|
//output_string[buf_write_index] = '\n';
|
||||||
|
//strncpy(output_string + buf_write_index, orig + orig_read_index, outbufsize - buf_write_index);
|
||||||
|
/*for (int i = 0; i<instance_count; i++){
|
||||||
|
//strncpy(output_string+ write_index, orig, read_index);
|
||||||
|
strncpy(write_point, (char*)color, strlen(color));
|
||||||
|
write_point += strlen(color);
|
||||||
|
strncpy(write_point, word_to_match, strlen(word_to_match));
|
||||||
|
write_point += strlen(word_to_match);
|
||||||
|
read_point += strlen(word_to_match);
|
||||||
|
strncpy(write_point, KNRM, strlen(KNRM));
|
||||||
|
write_point += strlen(KNRM);
|
||||||
|
if (i != instance_count - 1) break;
|
||||||
|
strncpy(output_string+(write_point - orig), read_point, (read_point - orig));
|
||||||
|
}*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//char *nxt_found = strstr(orig, word);
|
||||||
|
//if (nxt_found == NULL) return output_string;
|
||||||
|
//int len2first = nxt_found - orig;
|
||||||
|
//printf("FUNC: %d, %c\n", len2first, output_string[len2first]);
|
||||||
|
//char first_part[len2first + 1];
|
||||||
|
//strncpy(first_part, orig, len2first);
|
||||||
|
//return first_part;
|
||||||
|
return output_string;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
int master;
|
||||||
|
struct winsize win = {
|
||||||
|
.ws_col = 80, .ws_row = 24,
|
||||||
|
.ws_xpixel = 480, .ws_ypixel = 192,
|
||||||
|
};
|
||||||
|
pid_t child;
|
||||||
|
|
||||||
|
if (argc < 2) {
|
||||||
|
printf("Usage: %s cmd [args...]\n", argv[0]);
|
||||||
|
exit(EX_USAGE);
|
||||||
|
}
|
||||||
|
|
||||||
|
child = forkpty(&master, NULL, NULL, &win);
|
||||||
|
if (child == -1) {
|
||||||
|
perror("forkpty failed");
|
||||||
|
exit(EX_OSERR);
|
||||||
|
}
|
||||||
|
if (child == 0) {
|
||||||
|
execl(argv[1], argv[1], NULL);
|
||||||
|
perror("exec failed");
|
||||||
|
exit(EX_OSERR);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* now the child is attached to a real pseudo-TTY instead of a pipe,
|
||||||
|
* while the parent can use "master" much like a normal pipe */
|
||||||
|
char childoutbuffer[BUFCHUNKSIZE] = {0};
|
||||||
|
//fcntl(master, F_SETFL, O_NONBLOCK);
|
||||||
|
FILE* thein = fdopen(master, "rw");
|
||||||
|
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);
|
||||||
|
//ssize_t inc = read(master, childoutbuffer, BUFCHUNKSIZE);
|
||||||
|
//ssize_t theint = read(STDIN_FILENO, childoutbuffer, BUFCHUNKSIZE);
|
||||||
|
fgets(childoutbuffer, BUFCHUNKSIZE, stdin);
|
||||||
|
printf("%s\n", childoutbuffer);
|
||||||
|
write(master, childoutbuffer, strlen(childoutbuffer));
|
||||||
|
|
||||||
|
memset(childoutbuffer, 0, BUFCHUNKSIZE);
|
||||||
|
//ssize_t count = strlen(childoutbuffer);
|
||||||
|
ssize_t 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);
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
}*/
|
||||||
Loading…
Reference in New Issue