-
Notifications
You must be signed in to change notification settings - Fork 2
/
jv_cli.c
51 lines (45 loc) · 1.19 KB
/
jv_cli.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
#include "jv.c"
int main(int argc, char **argv) {
FILE *fp;
const char *path;
struct json_stream_t stream;
struct output_t out;
if (argc == 2) {
// Stream from stdin
fp = stdin;
path = argv[1];
}
else if (argc == 3) {
// Stream in a file.
fp = fopen(argv[1], "r");
if (fp == NULL) {
fprintf(stderr, "Error opening file %s.\n", argv[1]);
exit(2);
}
path = argv[2];
}
else {
fprintf(stderr, "Usage: jv [<file>] <attr>\n\n");
fprintf(stderr, " For example: jv \"dogs[34].breed\" < animals.json\n\n");
exit(1);
}
if (init_stream(&stream, fp) != OK) {
fprintf(stderr, "Problem initializing stream.\n");
exit(1);
}
path = scan_value(&stream, path);
if (path == NULL) {
// Error bomb. For now, just exit with code.
exit(stream.code);
}
if (path[0] != '\0') {
// Successful scan, no match. Return nonzero code.
exit(END_OF_STREAM);
}
// Match! Let's pipe.
init_output(&out, stdout, NULL, 0);
if (pipe_value(&stream, &out) != OK) {
exit(stream.code);
}
exit(OK);
}