#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; }*/