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>
|
2023-08-19 07:18:34 -04:00
|
|
|
#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"
|
2023-09-20 19:49:36 -04:00
|
|
|
#include "syntax/syntax.h"
|
2023-08-19 07:18:34 -04:00
|
|
|
|
2023-09-20 14:57:47 -04:00
|
|
|
#define ALLOCATION_CHUNK (128UL)
|
2023-08-19 07:18:34 -04:00
|
|
|
|
2023-08-29 13:11:18 -04:00
|
|
|
static const char * argv0;
|
2023-08-19 07:18:34 -04:00
|
|
|
|
2023-08-29 13:11:18 -04:00
|
|
|
static char *
|
2023-09-20 19:49:36 -04:00
|
|
|
read_entire_file(const char * fn)
|
2023-08-29 13:11:18 -04:00
|
|
|
{
|
2023-09-20 14:57:47 -04:00
|
|
|
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);
|
2023-09-20 19:49:36 -04:00
|
|
|
if (b && fread(b, 1, len, fp)) {
|
2023-09-20 14:57:47 -04:00
|
|
|
b[len] = '\0';
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ return NULL; }
|
2023-08-29 13:11:18 -04:00
|
|
|
}
|
2023-08-19 07:18:34 -04:00
|
|
|
|
2023-08-29 13:11:18 -04:00
|
|
|
static char *
|
|
|
|
get_stdin(void)
|
|
|
|
{
|
2023-09-20 14:57:47 -04:00
|
|
|
size_t buffer_size = 0;
|
2023-09-20 19:49:36 -04:00
|
|
|
size_t n = 1;
|
2023-09-20 14:57:47 -04:00
|
|
|
char * buffer = malloc(ALLOCATION_CHUNK);
|
2023-09-20 19:49:36 -04:00
|
|
|
if (!buffer)
|
|
|
|
{ return NULL; }
|
2023-08-19 07:18:34 -04:00
|
|
|
do {
|
2023-09-20 19:49:36 -04:00
|
|
|
if (buffer_size + 1 >= (ALLOCATION_CHUNK * n)) {
|
|
|
|
buffer = realloc(buffer, ALLOCATION_CHUNK * ++n + 1);
|
|
|
|
if (!buffer)
|
|
|
|
{ return NULL; }
|
|
|
|
buffer[ALLOCATION_CHUNK * n] = '\0';
|
2023-08-19 07:18:34 -04:00
|
|
|
}
|
2023-08-29 13:11:18 -04:00
|
|
|
if (read(STDIN_FILENO, &buffer[buffer_size], sizeof (*buffer)) == -1)
|
2023-09-20 14:57:47 -04:00
|
|
|
{
|
|
|
|
free(buffer);
|
2023-09-20 19:49:36 -04:00
|
|
|
fprintf(stderr, "%s: Failed to read from stdin\n", argv0);
|
2023-09-20 14:57:47 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2023-09-20 19:49:36 -04:00
|
|
|
} while (buffer[buffer_size++]);
|
2023-08-19 07:18:34 -04:00
|
|
|
|
|
|
|
buffer[buffer_size - 1] = '\0';
|
2023-09-20 14:57:47 -04:00
|
|
|
return buffer;
|
2023-08-29 13:11:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2023-09-20 18:52:20 -04:00
|
|
|
main(int argc,
|
2023-08-29 13:11:18 -04:00
|
|
|
char ** argv) {
|
2023-09-20 14:57:47 -04:00
|
|
|
int arg = 0;
|
2023-09-20 19:49:36 -04:00
|
|
|
int ret = 0;
|
2023-09-20 14:57:47 -04:00
|
|
|
char * buffer = NULL;
|
|
|
|
argv0 = argv[0];
|
2023-08-23 21:37:40 -04:00
|
|
|
terminal_hl_init();
|
2023-09-20 19:49:36 -04:00
|
|
|
highlight_c(); /* this mustn't break overrides (but definitely does) */
|
2023-09-20 14:57:47 -04:00
|
|
|
while (++argv,
|
2023-09-20 19:49:36 -04:00
|
|
|
--argc) {
|
|
|
|
if (**argv == '-') {
|
|
|
|
/* TODO use uthash */
|
|
|
|
if (strcmp(*argv+1, "c") == 0) {
|
|
|
|
highlight_c();
|
2023-09-20 14:57:47 -04:00
|
|
|
}
|
2023-09-20 19:49:36 -04:00
|
|
|
else {
|
2023-09-20 14:57:47 -04:00
|
|
|
fprintf(stderr, "%s: Unimplemented syntax '%s'\n", argv0, *argv+1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2023-09-20 19:49:36 -04:00
|
|
|
else {
|
2023-09-20 14:57:47 -04:00
|
|
|
free(buffer);
|
|
|
|
arg = 1;
|
2023-09-20 19:49:36 -04:00
|
|
|
buffer = read_entire_file(*argv);
|
|
|
|
if (!buffer) {
|
|
|
|
fprintf(stderr,"%s: cannot access '%s': ", argv0, *argv);
|
|
|
|
perror(NULL);
|
|
|
|
ret = 2;
|
2023-09-20 14:57:47 -04:00
|
|
|
}
|
2023-09-20 19:49:36 -04:00
|
|
|
else
|
|
|
|
{ render_string(buffer, "cterm"); }
|
2023-09-20 14:57:47 -04:00
|
|
|
}
|
|
|
|
}
|
2023-09-20 19:49:36 -04:00
|
|
|
if (!arg) {
|
2023-09-20 14:57:47 -04:00
|
|
|
buffer = get_stdin();
|
|
|
|
render_string(buffer, "cterm");
|
|
|
|
}
|
2023-08-21 14:07:39 -04:00
|
|
|
fflush(stdout);
|
2023-09-20 16:43:29 -04:00
|
|
|
hl_deinit();
|
2023-08-19 18:49:10 -04:00
|
|
|
free(buffer);
|
2023-09-20 19:49:36 -04:00
|
|
|
return ret;
|
2023-08-19 07:18:34 -04:00
|
|
|
}
|