Merge branch 'master' of https://git.lain.church/emil/hl
This commit is contained in:
commit
1c0387ad22
156
source/main.c
156
source/main.c
@ -10,113 +10,113 @@
|
|||||||
|
|
||||||
#include "terminal.h"
|
#include "terminal.h"
|
||||||
|
|
||||||
#define ALLOCATION_CHUNK (10UL)
|
#define ALLOCATION_CHUNK (128UL)
|
||||||
|
|
||||||
static const char * argv0;
|
static const char * argv0;
|
||||||
|
|
||||||
static char *
|
static char *
|
||||||
slurp(const char * fn)
|
slurp(const char * fn)
|
||||||
{
|
{
|
||||||
FILE * fp = fopen(fn, "r");
|
FILE * fp = fopen(fn, "r");
|
||||||
if (fp)
|
if (fp)
|
||||||
{
|
{
|
||||||
size_t len;
|
size_t len;
|
||||||
char * b;
|
char * b;
|
||||||
fseek(fp, 0, SEEK_END);
|
fseek(fp, 0, SEEK_END);
|
||||||
len = ftell(fp);
|
len = ftell(fp);
|
||||||
rewind(fp);
|
rewind(fp);
|
||||||
b = malloc(len + 1);
|
b = malloc(len + 1);
|
||||||
if (b && fread(b, 1, len, fp))
|
if (b && fread(b, 1, len, fp))
|
||||||
{
|
{
|
||||||
b[len] = '\0';
|
b[len] = '\0';
|
||||||
}
|
}
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{ return NULL; }
|
{ return NULL; }
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *
|
static char *
|
||||||
get_stdin(void)
|
get_stdin(void)
|
||||||
{
|
{
|
||||||
size_t buffer_size = 0;
|
size_t buffer_size = 0;
|
||||||
char * buffer = malloc(ALLOCATION_CHUNK);
|
char * buffer = malloc(ALLOCATION_CHUNK);
|
||||||
do {
|
do {
|
||||||
if (!((buffer_size + 1) % ALLOCATION_CHUNK)) {
|
if (!((buffer_size + 1) | ALLOCATION_CHUNK)) {
|
||||||
buffer = realloc(buffer, ((buffer_size + 1) / ALLOCATION_CHUNK + 1) * ALLOCATION_CHUNK);
|
buffer = realloc(buffer, (((buffer_size + 1) & ALLOCATION_CHUNK) + 1) * ALLOCATION_CHUNK);
|
||||||
}
|
}
|
||||||
buffer[buffer_size] = '\0';
|
buffer[buffer_size] = '\0';
|
||||||
if (read(STDIN_FILENO, &buffer[buffer_size], sizeof (*buffer)) == -1)
|
if (read(STDIN_FILENO, &buffer[buffer_size], sizeof (*buffer)) == -1)
|
||||||
{
|
{
|
||||||
free(buffer);
|
free(buffer);
|
||||||
fprintf(stderr, "%s: Failed to read from STDIN\n", argv0);
|
fprintf(stderr, "%s: Failed to read from STDIN\n", argv0);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
++buffer_size;
|
++buffer_size;
|
||||||
} while (buffer[buffer_size - 1]);
|
} while (buffer[buffer_size - 1]);
|
||||||
|
|
||||||
buffer[buffer_size - 1] = '\0';
|
buffer[buffer_size - 1] = '\0';
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* TODO: fix the shit going on with syntax/c.h , replace with a function,
|
/* TODO: fix the shit going on with syntax/c.h , replace with a function,
|
||||||
* and ideally how make it hotswappable. */
|
* and ideally how make it hotswappable. */
|
||||||
int
|
int
|
||||||
main(int argc,
|
main(int argc,
|
||||||
char ** argv) {
|
char ** argv) {
|
||||||
int arg = 0;
|
int arg = 0;
|
||||||
int syn = 0;
|
int syn = 0;
|
||||||
char * buffer = NULL;
|
char * buffer = NULL;
|
||||||
|
|
||||||
argv0 = argv[0];
|
argv0 = argv[0];
|
||||||
|
|
||||||
terminal_hl_init();
|
terminal_hl_init();
|
||||||
|
|
||||||
while (++argv,
|
while (++argv,
|
||||||
--argc)
|
--argc)
|
||||||
{
|
{
|
||||||
if (**argv == '-')
|
if (**argv == '-')
|
||||||
{
|
{
|
||||||
syn = 1;
|
syn = 1;
|
||||||
/* fprintf(stderr, "handle '%s'\n", *argv+1); */
|
/* fprintf(stderr, "handle '%s'\n", *argv+1); */
|
||||||
/* lazy as hell, TODO use uthash */
|
/* lazy as hell, TODO use uthash */
|
||||||
if (strcmp(*argv+1, "c") == 0)
|
if (strcmp(*argv+1, "c") == 0)
|
||||||
{
|
{
|
||||||
#include "syntax/c.h"
|
#include "syntax/c.h"
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
fprintf(stderr, "%s: Unimplemented syntax '%s'\n", argv0, *argv+1);
|
fprintf(stderr, "%s: Unimplemented syntax '%s'\n", argv0, *argv+1);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (!syn)
|
if (!syn)
|
||||||
{
|
{
|
||||||
#include "syntax/c.h"
|
#include "syntax/c.h"
|
||||||
}
|
}
|
||||||
free(buffer);
|
free(buffer);
|
||||||
arg = 1;
|
arg = 1;
|
||||||
buffer = slurp(*argv);
|
buffer = slurp(*argv);
|
||||||
render_string(buffer, "cterm");
|
render_string(buffer, "cterm");
|
||||||
if (!buffer)
|
if (!buffer)
|
||||||
{
|
{
|
||||||
perror(argv0);
|
perror(argv0);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!arg)
|
if (!arg)
|
||||||
{
|
{
|
||||||
if (!syn)
|
if (!syn)
|
||||||
{
|
{
|
||||||
#include "syntax/c.h"
|
#include "syntax/c.h"
|
||||||
}
|
}
|
||||||
buffer = get_stdin();
|
buffer = get_stdin();
|
||||||
render_string(buffer, "cterm");
|
render_string(buffer, "cterm");
|
||||||
}
|
}
|
||||||
|
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
hl_deinit();
|
hl_deinit();
|
||||||
|
Loading…
Reference in New Issue
Block a user