1 /* json-glib-format - Formats JSON data
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 G_OS_UNIX
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #endif
31 #include <fcntl.h>
32 #ifdef G_OS_WIN32
33 #include <windows.h>
34 #endif
35 
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <locale.h>
39 #include <errno.h>
40 
41 #include <glib.h>
42 #include <glib/gi18n.h>
43 #include <glib/gstdio.h>
44 #include <json-glib/json-glib.h>
45 
46 #if defined (G_OS_WIN32) && !defined (HAVE_UNISTD_H)
47 #include <io.h>
48 
49 #define STDOUT_FILENO 1
50 #endif
51 
52 static char **files = NULL;
53 static char *output = NULL;
54 static gboolean prettify = FALSE;
55 static int indent_spaces = 2;
56 
57 static GOptionEntry entries[] = {
58   { "prettify", 'p', 0, G_OPTION_ARG_NONE, &prettify, N_("Prettify output"), NULL },
59   { "indent-spaces", 'i', 0, G_OPTION_ARG_INT, &indent_spaces, N_("Indentation spaces"), N_("SPACES") },
60   { "output", 'o', 0, G_OPTION_ARG_FILENAME, &output, N_("Output file"), N_("FILE") },
61   { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &files, NULL, N_("FILE…") },
62   { NULL },
63 };
64 
65 static gboolean
format(JsonParser * parser,JsonGenerator * generator,GFile * file)66 format (JsonParser    *parser,
67         JsonGenerator *generator,
68         GFile         *file)
69 {
70   GInputStream *in;
71   GError *error;
72   gboolean res = TRUE;
73   gboolean parse_res;
74   gboolean close_res;
75   char *data, *p;
76   gsize len;
77   int fd = -1;
78 
79   error = NULL;
80 
81   in = (GInputStream *) g_file_read (file, NULL, &error);
82   if (in == NULL)
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 opening file: %s\n"),
88                   g_get_prgname (), g_file_get_uri (file), error->message);
89       g_error_free (error);
90       return FALSE;
91     }
92 
93   parse_res = json_parser_load_from_stream (parser, in, NULL, &error);
94   if (!parse_res)
95     {
96       char *uri = g_file_get_uri (file);
97 
98       /* Translators: the first %s is the program name, the second one
99        * is the URI of the file, the third is the error message.
100        */
101       g_printerr (_("%s: %s: error parsing file: %s\n"),
102                   g_get_prgname (), uri, error->message);
103       g_clear_error (&error);
104       g_free (uri);
105       res = FALSE;
106       goto out;
107     }
108 
109   json_generator_set_root (generator, json_parser_get_root (parser));
110   data = json_generator_to_data (generator, &len);
111 
112   if (output == NULL)
113     fd = STDOUT_FILENO;
114   else
115     {
116       int sv_errno;
117 
118       fd = g_open (output, O_CREAT | O_WRONLY, 0666);
119       if (fd < 0)
120         {
121           sv_errno = errno;
122 
123           g_printerr (_("%s: %s: error opening file: %s\n"),
124                       g_get_prgname (), output, g_strerror (sv_errno));
125           res = FALSE;
126           goto out;
127         }
128     }
129 
130   p = data;
131 
132   while (len > 0)
133     {
134       gssize written = write (fd, p, len);
135 
136       if (written == -1 && errno != EINTR)
137         {
138           char *uri = g_file_get_uri (file);
139 
140           /* Translators: the first %s is the program name, the
141            * second one is the URI of the file.
142            */
143           g_printerr (_("%s: %s: error writing to stdout"), g_get_prgname (), uri);
144           g_free (uri);
145           res = FALSE;
146           goto out;
147         }
148 
149       len -= written;
150       p += written;
151     }
152 
153   if (write (fd, "\n", 1) < 0)
154     g_error ("%s: %s", g_get_prgname (), g_strerror (errno));
155 
156   g_free (data);
157 
158 out:
159   close_res = g_input_stream_close (in, NULL, &error);
160   if (!close_res)
161     {
162       char *uri = g_file_get_uri (file);
163 
164       /* Translators: the first %s is the program name, the second one
165        * is the URI of the file, the third is the error message.
166        */
167       g_printerr (_("%s: %s: error closing: %s\n"),
168                   g_get_prgname (), uri, error->message);
169       g_clear_error (&error);
170       g_free (uri);
171       res = FALSE;
172     }
173 
174   g_object_unref (in);
175 
176   if (fd != STDOUT_FILENO)
177     g_close (fd, NULL);
178 
179   return res;
180 }
181 
182 int
main(int argc,char * argv[])183 main (int   argc,
184       char *argv[])
185 {
186   GOptionContext *context = NULL;
187   GError *error = NULL;
188   const char *description;
189   const char *summary;
190   JsonParser *parser;
191   JsonGenerator *generator;
192   gboolean res;
193   int i;
194 
195   setlocale (LC_ALL, "");
196 
197   bindtextdomain (GETTEXT_PACKAGE, JSON_LOCALEDIR);
198   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
199   textdomain (GETTEXT_PACKAGE);
200 
201   /* Translators: this message will appear after the usage string */
202   /* and before the list of options.                              */
203   summary = _("Format JSON files.");
204   description = _("json-glib-format formats JSON resources.");
205 
206   context = g_option_context_new (NULL);
207   g_option_context_set_summary (context, summary);
208   g_option_context_set_description (context, description);
209   g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
210   g_option_context_parse (context, &argc, &argv, &error);
211   g_option_context_free (context);
212 
213   if (error != NULL)
214     {
215       /* Translators: the %s is the program name. This error message
216        * means the user is calling json-glib-validate without any
217        * argument.
218        */
219       g_printerr (_("Error parsing commandline options: %s\n"), error->message);
220       g_printerr ("\n");
221       g_printerr (_("Try “%s --help” for more information."), g_get_prgname ());
222       g_printerr ("\n");
223       g_error_free (error);
224       return 1;
225     }
226 
227   if (files == NULL)
228     {
229       /* Translators: the %s is the program name. This error message
230        * means the user is calling json-glib-validate without any
231        * argument.
232        */
233       g_printerr (_("%s: missing files"), g_get_prgname ());
234       g_printerr ("\n");
235       g_printerr (_("Try “%s --help” for more information."), g_get_prgname ());
236       g_printerr ("\n");
237       return 1;
238     }
239 
240   generator = json_generator_new ();
241   json_generator_set_pretty (generator, prettify);
242   json_generator_set_indent (generator, indent_spaces);
243 
244   parser = json_parser_new ();
245   res = TRUE;
246   i = 0;
247 
248   do
249     {
250       GFile *file = g_file_new_for_commandline_arg (files[i]);
251 
252       res = format (parser, generator, file) && res;
253       g_object_unref (file);
254     }
255   while (files[++i] != NULL);
256 
257   g_object_unref (parser);
258   g_object_unref (generator);
259 
260   return res ? EXIT_SUCCESS : EXIT_FAILURE;
261 }
262