aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: f318c523d9130519a4970ec7d3c7b78b3b52996b (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
#define _POSIX_C_SOURCE 200809L

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "lex.h"
#include "parse.h"
#include "util.h"

int
main(int argc, char *argv[])
{
	int optind = 1;

	int nsources = argc - optind;

	struct lexer lexer;
	struct ast_unit ast;

	for (int i = 0; i < nsources; ++i) {
		FILE *in;
		const char *path = argv[optind + i];
		if (strcmp(path, "-") == 0) {
			in = stdin;
		} else {
			in = fopen(path, "r");
			struct stat stat;
			if (in && fstat(fileno(in), &stat) == 0
				&& S_ISDIR(stat.st_mode) != 0) {
				fprintf(stderr, "Unable to open %s for reading: Is a directory\n",
					path);
				return EXIT_USER;
			}
		}

		if (!in) {
			fprintf(stderr, "Unable to open %s for reading: %s\n",
					path, strerror(errno));
			return EXIT_ABNORMAL;
		}

		lex_init(&lexer, in, path);
		parse(&lexer, &ast);
		lex_finish(&lexer);
	}

	return EXIT_SUCCESS;
}