aboutsummaryrefslogtreecommitdiff
path: root/src/core.c
blob: 7eca8bd8f895297ebbeabd13be0222ba5f206f23 (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
61
62
63
64
65
66
67
68
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <errno.h>

#include <posthaste/debug.h>
#include <posthaste/parser.h>
#include <posthaste/core.h>

/**
 * Read whole file into a buffer and return pointer to buffer.
 * Possibly kind of silly to have both \p file and \p f.
 * Apparently there's no standardized way to get the file name of a
 * file pointer.
 *
 * @param fname Name of file to read.
 * @param f File pointer.
 * @return Pointer to buffer with file contents.
 */
static char *read_file(const char *fname, FILE *f)
{
	fseek(f, 0, SEEK_END);
	long s = ftell(f);
	if (s == LONG_MAX) {
		/** @todo should probably do this via fstat or something */
		error("%s might be a directory", fname);
		return NULL;
	}

	fseek(f, 0, SEEK_SET);

	char *buf = malloc(s + 1);
	if (!buf)
		return NULL;

	fread(buf, s + 1, 1, f);
	/* remember terminating null */
	buf[s] = 0;
	return buf;
}

int run(const char *fname)
{
	FILE *f = fopen(fname, "rb");
	if (!f) {
		error("failed opening %s: %s\n", fname, strerror(errno));
		return -1;
	}

	const char *buf = read_file(fname, f);
	fclose(f);

	if (!buf)
		return -1;

	struct parser *p = create_parser();
	if (!p)
		return -1;

	parse(p, fname, buf);
	int ret = p->failed ? -1 : 0;

	/* eventually do other stuff as well */
	free((void *)buf);

	destroy_parser(p);
	return ret;
}