]> Dogcows Code - chaz/yoink/blob - yajl/verify/json_verify.c
fixed documentation about where to find licenses
[chaz/yoink] / yajl / verify / json_verify.c
1 /*
2 * Copyright 2007-2009, Lloyd Hilaiel.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * 3. Neither the name of Lloyd Hilaiel nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <yajl/yajl_parse.h>
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 static void
40 usage(const char * progname)
41 {
42 fprintf(stderr, "%s: validate json from stdin\n"
43 "usage: json_verify [options]\n"
44 " -q quiet mode\n"
45 " -c allow comments\n"
46 " -u allow invalid utf8 inside strings\n",
47 progname);
48 exit(1);
49 }
50
51 int
52 main(int argc, char ** argv)
53 {
54 yajl_status stat;
55 size_t rd;
56 yajl_handle hand;
57 static unsigned char fileData[65536];
58 int quiet = 0;
59 int retval = 0, done = 0;
60 yajl_parser_config cfg = { 0, 1 };
61
62 /* check arguments.*/
63 if (argc > 1 && argc < 4) {
64 int i;
65
66 for (i=1; i < argc;i++) {
67 if (!strcmp("-q", argv[i])) {
68 quiet = 1;
69 } else if (!strcmp("-c", argv[i])) {
70 cfg.allowComments = 1;
71 } else if (!strcmp("-u", argv[i])) {
72 cfg.checkUTF8 = 0;
73 } else {
74 fprintf(stderr, "unrecognized option: '%s'\n\n", argv[i]);
75 usage(argv[0]);
76 }
77 }
78 } else if (argc != 1) {
79 usage(argv[0]);
80 }
81
82 /* allocate a parser */
83 hand = yajl_alloc(NULL, &cfg, NULL, NULL);
84
85 while (!done) {
86 rd = fread((void *) fileData, 1, sizeof(fileData) - 1, stdin);
87
88 retval = 0;
89
90 if (rd == 0) {
91 if (!feof(stdin)) {
92 if (!quiet) {
93 fprintf(stderr, "error encountered on file read\n");
94 }
95 retval = 1;
96 break;
97 }
98 done = 1;
99 }
100 fileData[rd] = 0;
101
102 if (done)
103 /* parse any remaining buffered data */
104 stat = yajl_parse_complete(hand);
105 else
106 /* read file data, pass to parser */
107 stat = yajl_parse(hand, fileData, rd);
108
109 if (stat != yajl_status_ok &&
110 stat != yajl_status_insufficient_data)
111 {
112 if (!quiet) {
113 unsigned char * str = yajl_get_error(hand, 1, fileData, rd);
114 fprintf(stderr, (const char *) str);
115 yajl_free_error(hand, str);
116 }
117 retval = 1;
118 break;
119 }
120 }
121
122 yajl_free(hand);
123
124 if (!quiet) {
125 printf("JSON is %s\n", retval ? "invalid" : "valid");
126 }
127
128 return retval;
129 }
This page took 0.036158 seconds and 4 git commands to generate.