xref: /spdk/test/app/jsoncat/jsoncat.c (revision 8fb123553a690dca615baec02990aa0dc2c655d0)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /* Simple JSON "cat" utility */
35 
36 #include "spdk/stdinc.h"
37 
38 #include "spdk/json.h"
39 
40 static void
41 usage(const char *prog)
42 {
43 	printf("usage: %s [-c] [-f] file.json\n", prog);
44 	printf("Options:\n");
45 	printf("-c\tallow comments in input (non-standard)\n");
46 	printf("-f\tformatted output (default: compact output)\n");
47 }
48 
49 static void
50 print_json_error(FILE *pf, int rc, const char *filename)
51 {
52 	switch (rc) {
53 	case SPDK_JSON_PARSE_INVALID:
54 		fprintf(pf, "%s: invalid JSON\n", filename);
55 		break;
56 	case SPDK_JSON_PARSE_INCOMPLETE:
57 		fprintf(pf, "%s: incomplete JSON\n", filename);
58 		break;
59 	case SPDK_JSON_PARSE_MAX_DEPTH_EXCEEDED:
60 		fprintf(pf, "%s: maximum nesting depth exceeded\n", filename);
61 		break;
62 	default:
63 		fprintf(pf, "%s: unknown JSON parse error\n", filename);
64 		break;
65 	}
66 }
67 
68 static int
69 json_write_cb(void *cb_ctx, const void *data, size_t size)
70 {
71 	FILE *f = cb_ctx;
72 	size_t rc;
73 
74 	rc = fwrite(data, 1, size, f);
75 	return rc == size ? 0 : -1;
76 }
77 
78 static void *
79 read_file(FILE *f, size_t *psize)
80 {
81 	void *buf, *newbuf;
82 	size_t cur_size, buf_size, rc;
83 
84 	buf = NULL;
85 	cur_size = 0;
86 	buf_size = 128 * 1024;
87 
88 	while (buf_size <= 1024 * 1024 * 1024) {
89 		newbuf = realloc(buf, buf_size);
90 		if (newbuf == NULL) {
91 			free(buf);
92 			return NULL;
93 		}
94 		buf = newbuf;
95 
96 		rc = fread(buf + cur_size, 1, buf_size - cur_size, f);
97 		cur_size += rc;
98 
99 		if (feof(f)) {
100 			*psize = cur_size;
101 			return buf;
102 		}
103 
104 		if (ferror(f)) {
105 			free(buf);
106 			return NULL;
107 		}
108 
109 		buf_size *= 2;
110 	}
111 
112 	free(buf);
113 	return NULL;
114 }
115 
116 static int
117 process_file(const char *filename, FILE *f, uint32_t parse_flags, uint32_t write_flags)
118 {
119 	size_t size;
120 	void *buf, *end;
121 	ssize_t rc;
122 	struct spdk_json_val *values;
123 	size_t num_values;
124 	struct spdk_json_write_ctx *w;
125 
126 	buf = read_file(f, &size);
127 	if (buf == NULL) {
128 		fprintf(stderr, "%s: file read error\n", filename);
129 		return 1;
130 	}
131 
132 	rc = spdk_json_parse(buf, size, NULL, 0, NULL, parse_flags);
133 	if (rc <= 0) {
134 		print_json_error(stderr, rc, filename);
135 		free(buf);
136 		return 1;
137 	}
138 
139 	num_values = (size_t)rc;
140 	values = calloc(num_values, sizeof(*values));
141 	if (values == NULL) {
142 		perror("values calloc");
143 		free(buf);
144 		return 1;
145 	}
146 
147 	rc = spdk_json_parse(buf, size, values, num_values, &end,
148 			     parse_flags | SPDK_JSON_PARSE_FLAG_DECODE_IN_PLACE);
149 	if (rc <= 0) {
150 		print_json_error(stderr, rc, filename);
151 		free(values);
152 		free(buf);
153 		return 1;
154 	}
155 
156 	w = spdk_json_write_begin(json_write_cb, stdout, write_flags);
157 	if (w == NULL) {
158 		fprintf(stderr, "json_write_begin failed\n");
159 		free(values);
160 		free(buf);
161 		return 1;
162 	}
163 
164 	spdk_json_write_val(w, values);
165 	spdk_json_write_end(w);
166 	printf("\n");
167 
168 	if (end != buf + size) {
169 		fprintf(stderr, "%s: garbage at end of file\n", filename);
170 		free(values);
171 		free(buf);
172 		return 1;
173 	}
174 
175 	free(values);
176 	free(buf);
177 	return 0;
178 }
179 
180 int
181 main(int argc, char **argv)
182 {
183 	FILE *f;
184 	int ch;
185 	int rc;
186 	uint32_t parse_flags = 0, write_flags = 0;
187 	const char *filename;
188 
189 	while ((ch = getopt(argc, argv, "cf")) != -1) {
190 		switch (ch) {
191 		case 'c':
192 			parse_flags |= SPDK_JSON_PARSE_FLAG_ALLOW_COMMENTS;
193 			break;
194 		case 'f':
195 			write_flags |= SPDK_JSON_WRITE_FLAG_FORMATTED;
196 			break;
197 		default:
198 			usage(argv[0]);
199 			return 1;
200 		}
201 	}
202 
203 	if (optind == argc) {
204 		filename = "-";
205 	} else if (optind == argc - 1) {
206 		filename = argv[optind];
207 	} else {
208 		usage(argv[0]);
209 		return 1;
210 	}
211 
212 	if (strcmp(filename, "-") == 0) {
213 		f = stdin;
214 	} else {
215 		f = fopen(filename, "r");
216 		if (f == NULL) {
217 			perror("fopen");
218 			return 1;
219 		}
220 	}
221 
222 	rc = process_file(filename, f, parse_flags, write_flags);
223 
224 	if (f != stdin) {
225 		fclose(f);
226 	}
227 
228 	return rc;
229 }
230