101 lines
2.0 KiB
C
101 lines
2.0 KiB
C
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <string.h>
|
||
|
#include <unistd.h>
|
||
|
#include <fcntl.h>
|
||
|
#include <ctype.h>
|
||
|
|
||
|
#define UNUSED(x) ((void) (x))
|
||
|
|
||
|
#define ALLOCATION_CHUNK (10UL)
|
||
|
|
||
|
enum {
|
||
|
NORMAL, BOLD, DARKNESS, ITALIC,
|
||
|
UNDERLINE, BLINK, DUNNO_6, REVERSE,
|
||
|
INVISIBLE
|
||
|
};
|
||
|
|
||
|
enum {
|
||
|
GREY, RED, GREEN, YELLOW,
|
||
|
BLUE, PINK, CYAN, WHITE,
|
||
|
CANCEL
|
||
|
};
|
||
|
|
||
|
static char * buffer = NULL;
|
||
|
static size_t buffer_size = 0;
|
||
|
|
||
|
static void render_character(char * character) {
|
||
|
write(STDOUT_FILENO, character, sizeof (*character));
|
||
|
}
|
||
|
|
||
|
static void render_string(char * string) {
|
||
|
write(STDOUT_FILENO, string, strlen(string));
|
||
|
}
|
||
|
|
||
|
static void render_colour(int colour,
|
||
|
int effect) {
|
||
|
char format[8] = "\033[ ;3 m";
|
||
|
|
||
|
format[2] = (char) (effect % 9) + '0';
|
||
|
format[5] = (char) (colour % 8) + '0';
|
||
|
|
||
|
render_string(format);
|
||
|
}
|
||
|
|
||
|
static void render_cancel(void) {
|
||
|
render_string("\033[0m");
|
||
|
}
|
||
|
|
||
|
static int is_separator(char character) {
|
||
|
if (( isascii(character))
|
||
|
&& (!isalnum(character))
|
||
|
&& (character != '_')) {
|
||
|
return 1;
|
||
|
} else {
|
||
|
return 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static int compare_multiple_strings(char * string,
|
||
|
const char * * strings,
|
||
|
const int count) {
|
||
|
int i = 0;
|
||
|
|
||
|
do {
|
||
|
if (!strcmp(string, strings[i])) {
|
||
|
return 1;
|
||
|
}
|
||
|
} while (++i != count);
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
int main(int argc,
|
||
|
char * * argv) {
|
||
|
UNUSED(argc);
|
||
|
UNUSED(argv);
|
||
|
|
||
|
buffer = realloc(buffer, ALLOCATION_CHUNK);
|
||
|
|
||
|
do {
|
||
|
if (!((buffer_size + 1) % ALLOCATION_CHUNK)) {
|
||
|
// Linear incremental reallocation (advanced)!
|
||
|
size_t chunks = (buffer_size + 1) / ALLOCATION_CHUNK;
|
||
|
buffer = realloc(buffer, ++chunks * ALLOCATION_CHUNK);
|
||
|
}
|
||
|
buffer[buffer_size] = '\0';
|
||
|
read(STDIN_FILENO, &buffer[buffer_size], sizeof (*buffer));
|
||
|
++buffer_size;
|
||
|
} while (buffer[buffer_size - 1]);
|
||
|
|
||
|
buffer[buffer_size - 1] = '\0';
|
||
|
|
||
|
render_colour(RED, BOLD);
|
||
|
render_string(buffer);
|
||
|
render_cancel();
|
||
|
|
||
|
free (buffer);
|
||
|
|
||
|
return 0;
|
||
|
}
|