-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
67 lines (61 loc) · 1.26 KB
/
main.c
File metadata and controls
67 lines (61 loc) · 1.26 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#include <ctype.h>
#include "parse.h"
#include "gen.h"
#define OUTPUT_FILE "lex.yy.c"
/* read_file: reads contents of file and returns them
* caller must free returned string
* see https://stackoverflow.com/a/14002993 */
char *
read_file(char *path)
{
FILE *f = fopen(path, "rb");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET); /* same as rewind(f); */
char *str = malloc(fsize + 1);
fread(str, fsize, 1, f);
fclose(f);
str[fsize] = '\0';
return str;
}
int
runlex(char *inputpath, char *outputpath)
{
char* in = read_file(inputpath);
FILE *out = fopen(outputpath, "w");
if (out == NULL) {
fprintf(stderr, "error writing to file\n");
return 1;
}
struct lexer *lx = parse(in);
gen(out, lx, true);
lexer_destroy(lx);
fclose(out);
free(in);
return 0;
}
int
main(int argc, char *argv[])
{
char *output = OUTPUT_FILE;
int opt;
while ((opt = getopt(argc, argv, "o:")) != -1) {
switch (opt) {
case 'o':
output = optarg;
break;
default:
fprintf(stderr, "Usage: %s [-o output] lex.l\n", argv[0]);
exit(1);
}
}
if (optind >= argc) {
fprintf(stderr, "must provide input as string\n");
exit(1);
}
return runlex(argv[optind], output);
}