-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathechttp_get.c
More file actions
202 lines (175 loc) · 5.55 KB
/
echttp_get.c
File metadata and controls
202 lines (175 loc) · 5.55 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* echttp - Embedded HTTP server.
*
* Copyright 2019, Pascal Martin
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
* ------------------------------------------------------------------------
*
* A minimal JSON interpreter.
*
* This JSON interpreter was designed to test echttp_json.c, but is
* also useful to visually inspect raw JSON data.
*
* SYNOPSYS:
*
* echttp_get [-d] <file> element ..
*/
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "echttp.h"
#include "echttp_json.h"
#include "echttp_xml.h"
#define PRINT_MAX 20480
static int show_raw_value = 0;
static void print_string (const char *key, const char *value) {
static char escapelist [128];
if (show_raw_value) {
printf ("%s\n", value);
return;
}
char *buffer = (char *)malloc (3*strlen(value) + 1); // Worst case
char *to = buffer;
if (escapelist['n'] == 0) {
escapelist['\"'] = '\"';
escapelist['\\'] = '\\';
escapelist['/'] = '/';
escapelist[8] = 'b';
escapelist[12] = 'f';
escapelist[10] = 'n';
escapelist[13] = 'r';
escapelist[9] = 't';
}
while (*value) {
char escape = escapelist[(int)(*value)];
if (escape) {
*to++ = '\\';
*to++ = escape;
value++;
} else {
*to++ = *value++;
}
// TBD: Unicode?
}
*to = 0;
printf ("\"%s\"\n", buffer);
free(buffer);
}
static void print_json (ParserToken *token, int i, int deep);
static void enumerate (ParserToken *parent) {
int i;
int index[PRINT_MAX];
const char *error = echttp_json_enumerate (parent, index, PRINT_MAX);
if (error) {
printf ("error: %s\n", error);
return;
}
for (i = 0; i < parent->length; ++i) {
if (parent->type == PARSER_OBJECT)
printf (" %s: ", parent[index[i]].key);
else
printf (" [%2d] ", i);
print_json (parent, index[i], 0);
}
}
static void print_json (ParserToken *token, int i, int deep) {
switch (token[i].type) {
case PARSER_NULL:
printf ("null\n"); break;
case PARSER_BOOL:
printf ("%s\n", token[i].value.boolean?"true":"false"); break;
case PARSER_INTEGER:
printf ("%lld\n", token[i].value.integer); break;
case PARSER_REAL:
printf ("%e\n", token[i].value.real); break;
case PARSER_STRING:
print_string (token[i].key, token[i].value.string); break;
case PARSER_ARRAY:
printf ("array, length %d\n", token[i].length);
if (deep) enumerate (token+i);
break;
case PARSER_OBJECT:
printf ("object, %d elements\n", token[i].length);
if (deep) enumerate (token+i);
break;
default:
fprintf (stderr, "Invalid token type %d at index %d\n",
token[i].type, i);
}
}
static void print_tokens (ParserToken *token, int count) {
int i;
for (i = 0; i < count; ++i) {
printf ("Token type %d at index %d, length %d, key %s\n",
token[i].type, i, token[i].length, token[i].key?token[i].key:"(none)");
}
}
int main (int argc, const char **argv) {
int i;
int count = 0;
int index = 0;
int show_tokens = 0;
int xml_input = 0;
const char *error;
ParserToken token[PRINT_MAX];
char *buffer;
for (i = 1; i < argc; ++i) {
if (strcmp (argv[i], "-d") == 0) {
echttp_json_enable_debug();
continue;
}
if (strcmp (argv[i], "-t") == 0) {
show_tokens = 1;
continue;
}
if (strcmp (argv[i], "-x") == 0) {
xml_input = 1;
continue;
}
if (strcmp (argv[i], "-r") == 0) {
show_raw_value = 1;
continue;
}
if (!count) { // The first non-option is the file name.
if (strstr(argv[i], ".xml")) xml_input = 1;
buffer = echttp_parser_load (argv[i]);
if (!buffer) return 0; // No file, no data to print.
count = PRINT_MAX;
if (xml_input)
error = echttp_xml_parse (buffer, token, &count);
else
error = echttp_json_parse (buffer, token, &count);
if (error) {
fprintf (stderr, "Cannot decode %s: %s\n", argv[i], error);
echttp_parser_free (buffer);
return -1;
}
if (show_tokens) print_tokens (token, count);
continue;
}
index = echttp_json_search (token, argv[i]);
if (!show_raw_value) printf ("%s (%d): ", argv[i], index);
if (index >= 0) print_json (token, index, 1);
else printf ("invalid path\n");
}
return 0;
}