-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathmain.c
99 lines (89 loc) · 2.22 KB
/
main.c
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "specs-gen.c"
static void
print_usage(char *prog)
{
fprintf(
stderr,
"Usage: %s [-h|-c|-d|-f] -o output-file -i include-headers input-file \n"
" -h generate header\n"
" -c generate data and function definitions\n"
" -d generate data and function declarations\n"
" -S generate struct declarations\n"
" -E generate enum declarations\n"
" -F generate function declarations\n"
" -f generate function definitions\n"
" -O generate all opaque struct declarations\n"
" -a append to output\n",
prog);
exit(EXIT_FAILURE);
}
int
main(int argc, char **argv)
{
size_t len = 0;
char *s;
char *config_file = NULL;
struct emit_option eo = { .type = FILE_SINGLE_FILE };
char *open_mode = "w";
NTL_T(name_t) incl_headers = NULL;
int opt;
while (-1 != (opt = getopt(argc, argv, "ahcdfSEFOo:i:"))) {
switch (opt) {
case 'a':
open_mode = "a";
break;
case 'o':
config_file = strdup(optarg);
break;
case 'i': {
name_t header = "";
snprintf(header, sizeof(name_t), "%s", optarg);
ntl_append2((ntl_t *)&incl_headers, sizeof(name_t), &header);
break;
}
case 'h':
eo.type = FILE_HEADER;
break;
case 'c':
eo.type = FILE_CODE;
break;
case 'd':
eo.type = FILE_DECLARATION;
break;
case 'f':
eo.type = FILE_DEFINITION;
break;
case 'S':
eo.type = FILE_STRUCT_DECLARATION;
break;
case 'E':
eo.type = FILE_ENUM_DECLARATION;
break;
case 'F':
eo.type = FILE_FUN_DECLARATION;
break;
case 'O':
eo.type = FILE_OPAQUE_STRUCT_DECLARATION;
break;
default: /* '?' */
print_usage(argv[0]);
}
}
if (!config_file) print_usage(argv[0]);
char *file = argv[optind];
s = cog_load_whole_file(file, &len);
spec_name = file;
spec_buffer.start = s;
spec_buffer.size = len;
struct jc_definition d;
memset(&d, 0, sizeof(d));
definition_from_json(s, len, &d);
d.spec_name = file;
d.incl_headers = incl_headers;
gen_definition(config_file, open_mode, &eo, &d);
return EXIT_SUCCESS;
}