libhl/source/main.c

129 lines
2.3 KiB
C
Raw Normal View History

2023-08-24 06:24:46 -04:00
/* main.c
* Copyright 2023 Anon Anonson, Ognjen 'xolatile' Milan Robovic, Emil Williams
* SPDX Identifier: GPL-3.0-only / NO WARRANTY / NO GUARANTEE */
2023-08-21 09:05:27 -04:00
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
2023-08-28 15:58:20 -04:00
2023-08-29 13:09:55 -04:00
#include "terminal.h"
#define ALLOCATION_CHUNK (10UL)
2023-08-29 13:11:18 -04:00
static const char * argv0;
2023-08-29 13:11:18 -04:00
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; }
}
2023-08-29 13:11:18 -04:00
static char *
get_stdin(void)
{
size_t buffer_size = 0;
char * buffer = malloc(ALLOCATION_CHUNK);
do {
if (!((buffer_size + 1) % ALLOCATION_CHUNK)) {
2023-08-29 13:11:18 -04:00
buffer = realloc(buffer, ((buffer_size + 1) / ALLOCATION_CHUNK + 1) * ALLOCATION_CHUNK);
}
buffer[buffer_size] = '\0';
2023-08-29 13:11:18 -04:00
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';
2023-08-29 13:11:18 -04:00
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];
2023-08-23 21:37:40 -04:00
terminal_hl_init();
2023-08-25 19:16:05 -04:00
2023-08-29 13:11:18 -04:00
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");
}
2023-08-21 14:07:39 -04:00
fflush(stdout);
//hl_deinit();
2023-08-19 18:49:10 -04:00
free(buffer);
2023-08-27 18:49:36 -04:00
//terminal_hl_deinit();
return 0;
}