1 /* json-glib-validate - Checks JSON data for errors
2 *
3 * This file is part of JSON-GLib
4 *
5 * Copyright © 2013 Emmanuele Bassi
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * Author:
21 * Emmanuele Bassi <ebassi@gnome.org>
22 */
23
24 #include "config.h"
25
26 #ifdef HAVE_UNISTD_H
27 #include <unistd.h>
28 #endif
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <locale.h>
32 #include <errno.h>
33
34 #include <glib.h>
35 #include <glib/gi18n.h>
36 #include <json-glib/json-glib.h>
37
38 static char **files = NULL;
39
40 static GOptionEntry entries[] = {
41 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &files, NULL, NULL },
42 { NULL },
43 };
44
45 static gboolean
validate(JsonParser * parser,GFile * file)46 validate (JsonParser *parser,
47 GFile *file)
48 {
49 GInputStream *in;
50 GError *error;
51 gboolean res = TRUE;
52 gboolean parse_res;
53 gboolean close_res;
54
55 error = NULL;
56
57 in = (GInputStream *) g_file_read (file, NULL, &error);
58 if (in == NULL)
59 {
60 /* Translators: the first %s is the program name, the second one
61 * is the URI of the file, the third is the error message.
62 */
63 g_printerr (_("%s: %s: error opening file: %s\n"),
64 g_get_prgname (), g_file_get_uri (file), error->message);
65 g_error_free (error);
66 return FALSE;
67 }
68
69 parse_res = json_parser_load_from_stream (parser, in, NULL, &error);
70 if (!parse_res)
71 {
72 /* Translators: the first %s is the program name, the second one
73 * is the URI of the file, the third is the error message.
74 */
75 g_printerr (_("%s: %s: error parsing file: %s\n"),
76 g_get_prgname (), g_file_get_uri (file), error->message);
77 g_clear_error (&error);
78 res = FALSE;
79 }
80
81 close_res = g_input_stream_close (in, NULL, &error);
82 if (!close_res)
83 {
84 /* Translators: the first %s is the program name, the second one
85 * is the URI of the file, the third is the error message.
86 */
87 g_printerr (_("%s: %s: error closing: %s\n"),
88 g_get_prgname (), g_file_get_uri (file), error->message);
89 g_clear_error (&error);
90 res = FALSE;
91 }
92
93 return res;
94 }
95
96 int
main(int argc,char * argv[])97 main (int argc,
98 char *argv[])
99 {
100 GOptionContext *context = NULL;
101 GError *error = NULL;
102 const char *description;
103 const char *summary;
104 gchar *param;
105 JsonParser *parser;
106 gboolean res;
107 int i;
108
109 setlocale (LC_ALL, "");
110
111 bindtextdomain (GETTEXT_PACKAGE, JSON_LOCALEDIR);
112 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
113 textdomain (GETTEXT_PACKAGE);
114
115 param = g_strdup_printf (("%s..."), _("FILE"));
116 /* Translators: this message will appear after the usage string */
117 /* and before the list of options. */
118 summary = _("Validate JSON files.");
119 description = _("json-glib-validate validates JSON data at the given URI.");
120
121 context = g_option_context_new (param);
122 g_option_context_set_summary (context, summary);
123 g_option_context_set_description (context, description);
124 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
125 g_option_context_parse (context, &argc, &argv, &error);
126 g_option_context_free (context);
127
128 g_free (param);
129
130 if (error != NULL)
131 {
132 /* Translators: the %s is the program name. This error message
133 * means the user is calling json-glib-validate without any
134 * argument.
135 */
136 g_printerr (_("Error parsing commandline options: %s\n"), error->message);
137 g_printerr ("\n");
138 g_printerr (_("Try “%s --help” for more information."), g_get_prgname ());
139 g_printerr ("\n");
140 g_error_free (error);
141 return 1;
142 }
143
144 if (files == NULL)
145 {
146 /* Translators: the %s is the program name. This error message
147 * means the user is calling json-glib-validate without any
148 * argument.
149 */
150 g_printerr (_("%s: missing files"), g_get_prgname ());
151 g_printerr ("\n");
152 g_printerr (_("Try “%s --help” for more information."), g_get_prgname ());
153 g_printerr ("\n");
154 return 1;
155 }
156
157 parser = json_parser_new ();
158 res = TRUE;
159 i = 0;
160
161 do
162 {
163 GFile *file = g_file_new_for_commandline_arg (files[i]);
164
165 res = validate (parser, file) && res;
166 g_object_unref (file);
167 }
168 while (files[++i] != NULL);
169
170 g_object_unref (parser);
171
172 return res ? EXIT_SUCCESS : EXIT_FAILURE;
173 }
174