Add CLI elements
This commit is contained in:
parent
d0a490f373
commit
0d623f18d9
108
source/main.c
108
source/main.c
@ -12,34 +12,112 @@
|
||||
|
||||
#define ALLOCATION_CHUNK (10UL)
|
||||
|
||||
static char * buffer = NULL;
|
||||
static size_t buffer_size = 0;
|
||||
static const char * argv0;
|
||||
|
||||
int main(void) {
|
||||
// Buffer init
|
||||
buffer = realloc(buffer, ALLOCATION_CHUNK);
|
||||
static char *
|
||||
slurp(const char * fn)
|
||||
{
|
||||
FILE * fp = fopen(fn, "r");
|
||||
if (fp)
|
||||
{
|
||||
size_t len;
|
||||
char * b;
|
||||
fseek(fp, 0, SEEK_END);
|
||||
len = ftell(fp);
|
||||
rewind(fp);
|
||||
b = malloc(len + 1);
|
||||
if (b && fread(b, 1, len, fp))
|
||||
{
|
||||
b[len] = '\0';
|
||||
}
|
||||
fclose(fp);
|
||||
return b;
|
||||
}
|
||||
else
|
||||
{ return NULL; }
|
||||
}
|
||||
|
||||
static char *
|
||||
get_stdin(void)
|
||||
{
|
||||
size_t buffer_size = 0;
|
||||
char * buffer = malloc(ALLOCATION_CHUNK);
|
||||
do {
|
||||
if (!((buffer_size + 1) % ALLOCATION_CHUNK)) {
|
||||
size_t chunks = (buffer_size + 1) / ALLOCATION_CHUNK;
|
||||
buffer = realloc(buffer, ++chunks * ALLOCATION_CHUNK);
|
||||
buffer = realloc(buffer, ((buffer_size + 1) / ALLOCATION_CHUNK + 1) * ALLOCATION_CHUNK);
|
||||
}
|
||||
buffer[buffer_size] = '\0';
|
||||
/* TODO handle me */
|
||||
assert(read(STDIN_FILENO, &buffer[buffer_size], sizeof (*buffer)) != -1);
|
||||
if (read(STDIN_FILENO, &buffer[buffer_size], sizeof (*buffer)) == -1)
|
||||
{
|
||||
free(buffer);
|
||||
fprintf(stderr, "%s: Failed to read from STDIN\n", argv0);
|
||||
return NULL;
|
||||
}
|
||||
++buffer_size;
|
||||
} while (buffer[buffer_size - 1]);
|
||||
|
||||
buffer[buffer_size - 1] = '\0';
|
||||
return buffer;
|
||||
}
|
||||
|
||||
/* TODO: fix the shit going on with syntax/c.h , replace with a function,
|
||||
* and ideally how make it hotswappable. */
|
||||
int
|
||||
main(int argc,
|
||||
char ** argv) {
|
||||
int arg = 0;
|
||||
int syn = 0;
|
||||
char * buffer = NULL;
|
||||
|
||||
argv0 = argv[0];
|
||||
|
||||
// Highlight init
|
||||
terminal_hl_init();
|
||||
//
|
||||
#include "syntax/c.h"
|
||||
//
|
||||
|
||||
render_string(buffer, "cterm");
|
||||
putchar('\n');
|
||||
while (++argv,
|
||||
--argc)
|
||||
{
|
||||
if (**argv == '-')
|
||||
{
|
||||
syn = 1;
|
||||
/* fprintf(stderr, "handle '%s'\n", *argv+1); */
|
||||
/* lazy as hell, TODO use uthash */
|
||||
if (strcmp(*argv+1, "c") == 0)
|
||||
{
|
||||
#include "syntax/c.h"
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "%s: Unimplemented syntax '%s'\n", argv0, *argv+1);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!syn)
|
||||
{
|
||||
#include "syntax/c.h"
|
||||
}
|
||||
free(buffer);
|
||||
arg = 1;
|
||||
buffer = slurp(*argv);
|
||||
render_string(buffer, "cterm");
|
||||
if (!buffer)
|
||||
{
|
||||
perror(argv0);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!arg)
|
||||
{
|
||||
if (!syn)
|
||||
{
|
||||
#include "syntax/c.h"
|
||||
}
|
||||
buffer = get_stdin();
|
||||
render_string(buffer, "cterm");
|
||||
}
|
||||
|
||||
fflush(stdout);
|
||||
//hl_deinit();
|
||||
free(buffer);
|
||||
|
Loading…
Reference in New Issue
Block a user