-
Notifications
You must be signed in to change notification settings - Fork 23
/
main.c
161 lines (133 loc) · 3.04 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <libgen.h>
#include "toml.h"
static void
usage(char *progname, int exit_code, char *msg)
{
char *bname = basename(progname);
if (msg) {
fprintf(stderr, "%s\n", msg);
}
fprintf(stderr, "Usage: %s [-t <toml_file>] [-d] [-j] [-g <key>]\n", bname);
fprintf(stderr, "\t-t <toml_file> file to parse\n");
fprintf(stderr, "\t-d dump file contents\n");
fprintf(stderr, "\t-j dump as JSON (default is TOML)\n");
fprintf(stderr, "\t-g <key> dump file contents starting from <key>\n");
exit(exit_code);
}
int main(int argc, char **argv)
{
int fd, ret, toml_content_size = 0;
struct toml_node *toml_root;
void *toml_content = NULL;
struct stat st;
int ch, dump = 0, json = 0;
char *file = NULL, *get = NULL;
int exit_code = EXIT_SUCCESS;
char* bname;
bname = basename(argv[0]);
if (strcmp(bname, "parser_test") == 0) {
dump = 1;
json = 1;
}
while((ch = getopt(argc, argv, "t:dg:hj")) != -1) {
switch (ch) {
case 't':
file = optarg;
break;
case 'd':
dump = 1;
break;
case 'g':
get = optarg;
break;
case 'h':
usage(argv[0], 1, NULL);
break;
case 'j':
json = 1;
break;
default:
usage(argv[0], 1, NULL);
break;
}
}
if (file) {
fd = open(file, O_RDONLY);
if (fd == -1) {
fprintf(stderr, "open: %s\n", strerror(errno));
exit(1);
}
ret = fstat(fd, &st);
if (ret == -1) {
fprintf(stderr, "stat: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
toml_content = mmap(NULL, st.st_size, PROT_READ, MAP_FILE|MAP_PRIVATE, fd, 0);
if (!toml_content) {
fprintf(stderr, "mmap: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
toml_content_size = st.st_size;
} else {
const int size = 1024*1024;
int i, bytes_read;
for (i = 0; 1; i++) {
toml_content = realloc(toml_content, size * (i+1));
if (!toml_content) {
fprintf(stderr, "realloc: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
bytes_read = read(STDIN_FILENO, &toml_content[size*i], size);
toml_content_size += bytes_read;
if (bytes_read < size)
break;
}
}
ret = toml_init(&toml_root);
if (ret == -1) {
fprintf(stderr, "toml_init: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
ret = toml_parse(toml_root, toml_content, toml_content_size);
if (ret) {
exit_code = EXIT_FAILURE;
goto bail;
}
if (file) {
ret = munmap(toml_content, toml_content_size);
if (ret) {
fprintf(stderr, "munmap: %s\n", strerror(errno));
exit_code = EXIT_FAILURE;
goto bail;
}
close(fd);
}
if (dump) {
if (json)
toml_tojson(toml_root, stdout);
else
toml_dump(toml_root, stdout);
} else if (get) {
struct toml_node *node = toml_get(toml_root, get);
if (!node) {
fprintf(stderr, "no node '%s'\n", get);
exit_code = EXIT_FAILURE;
goto bail;
}
if (json)
toml_tojson(node, stdout);
else
toml_dump(node, stdout);
}
bail:
toml_free(toml_root);
exit(exit_code);
}