2023-08-21 09:05:27 -04:00
|
|
|
#include <assert.h>
|
2023-08-19 07:18:34 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include "hl.h"
|
|
|
|
|
|
|
|
#define ALLOCATION_CHUNK (10UL)
|
|
|
|
|
|
|
|
static char * buffer = NULL;
|
|
|
|
static size_t buffer_size = 0;
|
|
|
|
|
|
|
|
typedef struct {
|
2023-08-19 18:49:10 -04:00
|
|
|
char * attribute;
|
|
|
|
char * foreground_color;
|
|
|
|
char * background_color;
|
2023-08-19 07:18:34 -04:00
|
|
|
} terminal_hl_t;
|
|
|
|
|
|
|
|
void cterm_render_callback(const char * const string,
|
|
|
|
const int length,
|
|
|
|
void * const attributes) {
|
|
|
|
if(!length){
|
2023-08-19 18:49:10 -04:00
|
|
|
fputs(TERMINAL_STYLE_BOLD, stdout);
|
2023-08-19 07:18:34 -04:00
|
|
|
putchar(*string);
|
2023-08-19 18:49:10 -04:00
|
|
|
fputs(TERMINAL_RESET, stdout);
|
2023-08-19 07:18:34 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-19 18:49:10 -04:00
|
|
|
terminal_hl_t * term_hl = (terminal_hl_t*)attributes;
|
|
|
|
fputs(term_hl->attribute, stdout);
|
|
|
|
fputs(term_hl->foreground_color, stdout);
|
2023-08-19 07:18:34 -04:00
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
putchar(*(string+i));
|
|
|
|
}
|
|
|
|
fputs(TERMINAL_RESET, stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc,
|
|
|
|
char * * argv) {
|
|
|
|
UNUSED(argc);
|
|
|
|
UNUSED(argv);
|
|
|
|
|
|
|
|
// Buffer init
|
|
|
|
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';
|
2023-08-21 09:05:27 -04:00
|
|
|
/* TODO handle me */
|
|
|
|
assert(read(STDIN_FILENO, &buffer[buffer_size], sizeof (*buffer)) != -1);
|
2023-08-19 07:18:34 -04:00
|
|
|
++buffer_size;
|
|
|
|
} while (buffer[buffer_size - 1]);
|
|
|
|
|
|
|
|
buffer[buffer_size - 1] = '\0';
|
|
|
|
|
|
|
|
// Highlight init
|
2023-08-21 10:13:24 -04:00
|
|
|
hl_init();
|
2023-08-19 07:18:34 -04:00
|
|
|
const char * c_keywords[] = {
|
|
|
|
"register", "volatile", "auto", "const", "static", "extern", "if", "else",
|
|
|
|
"do", "while", "for", "continue", "switch", "case", "default", "break",
|
|
|
|
"enum", "union", "struct", "typedef", "goto", "void", "return", "sizeof",
|
|
|
|
"char", "short", "int", "long", "signed", "unsigned", "float", "double",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
const char * preprocessor_keywords[] = {
|
|
|
|
"#include", "#pragma", "#define", "#undef", "#ifdef", "#ifndef", "#elifdef", "#elifndef",
|
|
|
|
"#if", "#elif", "#else", "#endif", "#embed", "#line", "#error", "#warning",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2023-08-21 10:13:24 -04:00
|
|
|
|
2023-08-19 18:49:10 -04:00
|
|
|
//
|
2023-08-19 07:18:34 -04:00
|
|
|
display_t * cterm = &(display_t) {
|
|
|
|
.key = "cterm",
|
|
|
|
.callback = cterm_render_callback
|
|
|
|
};
|
2023-08-19 18:49:10 -04:00
|
|
|
//
|
|
|
|
terminal_hl_t terminal_keyword_hl = (terminal_hl_t) {
|
|
|
|
.attribute = TERMINAL_STYLE_BOLD,
|
|
|
|
.foreground_color = TERMINAL_COLOR_FG_GREEN,
|
|
|
|
.background_color = NULL
|
|
|
|
};
|
|
|
|
hl_group_t keyword_hl = (hl_group_t) {
|
|
|
|
.link = NULL,
|
|
|
|
.attributes = (void*)&terminal_keyword_hl
|
|
|
|
};
|
|
|
|
//
|
|
|
|
terminal_hl_t terminal_preprocessor_hl = (terminal_hl_t) {
|
|
|
|
.attribute = TERMINAL_STYLE_BOLD,
|
|
|
|
.foreground_color = TERMINAL_COLOR_FG_BLUE,
|
|
|
|
.background_color = NULL
|
2023-08-19 07:18:34 -04:00
|
|
|
};
|
2023-08-19 18:49:10 -04:00
|
|
|
hl_group_t preprocessor_hl = (hl_group_t) {
|
|
|
|
.link = NULL,
|
|
|
|
.attributes = (void*)&terminal_preprocessor_hl
|
|
|
|
};
|
|
|
|
//
|
|
|
|
terminal_hl_t terminal_symbol_hl = (terminal_hl_t) {
|
|
|
|
.attribute = TERMINAL_STYLE_BOLD,
|
|
|
|
.foreground_color = TERMINAL_COLOR_FG_YELLOW,
|
|
|
|
.background_color = NULL
|
|
|
|
};
|
|
|
|
hl_group_t symbol_hl = (hl_group_t) {
|
|
|
|
.link = NULL,
|
|
|
|
.attributes = (void*)&terminal_symbol_hl
|
|
|
|
};
|
|
|
|
//
|
2023-08-19 14:47:42 -04:00
|
|
|
new_display_mode(cterm);
|
2023-08-21 14:07:39 -04:00
|
|
|
new_char_tokens("&|()[]{}*,", &symbol_hl);
|
2023-08-19 18:49:10 -04:00
|
|
|
new_keyword_tokens(c_keywords, &keyword_hl);
|
|
|
|
new_keyword_tokens(preprocessor_keywords, &preprocessor_hl);
|
2023-08-19 07:18:34 -04:00
|
|
|
//
|
|
|
|
render_string(buffer, "cterm");
|
|
|
|
putchar('\n');
|
2023-08-21 14:07:39 -04:00
|
|
|
fflush(stdout);
|
|
|
|
//hl_deinit();
|
2023-08-19 18:49:10 -04:00
|
|
|
free(buffer);
|
2023-08-19 07:18:34 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|