aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: de742882acd263d045b5957bc19c3613d8ca2c5d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright (c) 2023, Marvin Borner <dev@marvinborner.de>
// SPDX-License-Identifier: MIT

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

#include <parse.h>
#include <log.h>

static char *read_file(FILE *f)
{
	fseek(f, 0, SEEK_END);
	long fsize = ftell(f);
	fseek(f, 0, SEEK_SET);

	char *string = malloc(fsize + 1);
	if (!string)
		fatal("out of memory!\n");
	int ret = fread(string, fsize, 1, f);

	if (ret != 1) {
		free(string);
		fatal("can't read file: %s\n", strerror(errno));
	}

	string[fsize] = 0;
	return string;
}

static char *read_path(const char *path)
{
	debug("reading from %s\n", path);
	FILE *f = fopen(path, "rb");
	if (!f)
		fatal("can't open file %s: %s\n", path, strerror(errno));
	char *string = read_file(f);
	fclose(f);
	return string;
}

int main(int argc, char *argv[])
{
	debug_enable(1);
	if (argc != 2)
		fatal("usage: %s <file>\n", argc ? argv[0] : "sharing");

	char *term = read_path(argv[1]);

	char *orig_term = term;
	struct term *parsed = parse_blc(&term);
	free(orig_term);

	term_print(parsed);
	fprintf(stderr, "\n");

	term_free(parsed);
	return 0;
}