-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
151 lines (123 loc) · 2.54 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <qute/qute.h>
#include <fs/fs.h>
#include <buffer/buffer.h>
#include "json.h"
#define EQ(a, b) (0 == strcmp(a, b))
static int stream = 0;
static int each = 0;
static int tty = 0;
static char *
read_stdin () {
buffer_t *buf = buffer_new();
char *ret = NULL;
char *tmp = NULL;
size_t size = 0;
if (NULL == buf) { return NULL; }
// read
while (getline(&tmp, &size, stdin) > 0) {
tmp[size] = '\0';
buffer_append(buf, tmp);
if (stream) {
break;
}
}
ret = buf->data;
return ret;
}
int
main (int argc, char **argv) {
json_value_t *value = NULL;
json_value_t *root = NULL;
char *filename = NULL;
char *key = NULL;
char *src = NULL;
int i = 0;
#define usage() printf("usage: %s <file> [key]\n", argv[0]);
if (argc < 2) {
usage();
return 1;
}
// parse opts
for (i = 1; i < argc; ++i) {
if (EQ(argv[i], "--each")) {
each = 1;
} else if (EQ(argv[i], "--stream")) {
stream = 1;
} else if (EQ(argv[i], "--help")) {
usage();
return 0;
}
}
if (ferror(stdin)) {
return 1;
} else if (0 == isatty(0)) {
filename = "<stdin>";
tty = 1;
if (argc > 1 && '-' != argv[1][0]) {
key = argv[1];
}
} else {
filename = argv[1];
if (0 != fs_exists(filename)) {
printf("E: not found - `%s'\n", filename);
return 1;
}
src = fs_read(filename);
if (NULL == src) {
return 1;
}
if (argc > 2 && '-' != argv[2][0]) {
key = argv[2];
}
}
do {
if (tty) {
src = read_stdin();
}
if (NULL == src || 0 == strlen(src)) {
break;
}
// proxy source if just streaming stdin
// without a key lookup
if (NULL == key && stream) {
printf("%s", src);
continue;
}
root = json_parse(filename, src);
if (NULL == root) {
return 1;
} else if (root->errno) {
json_perror(root);
return 1;
}
if (NULL != key) {
value = json_get(root, key);
if (NULL == value) {
return 1;
}
free(root);
root = value;
} else {
value = root;
}
if (1 == each && JSON_ARRAY == value->type) {
value = value->values[0];
while (value) {
printf("%s\n", json_stringify(value));
value = value->next;
}
} else {
printf("%s\n", json_stringify(value));
}
json_destroy(root);
free(src);
value = NULL;
root = NULL;
src = NULL;
} while (tty);
return 0;
}