1 /* Copyright (c) 2013, Vsevolod Stakhov
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution.
12 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
13 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15 * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
16 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
19 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 #include "ucl_internal.h"
26 #include <sys/types.h>
32 main (int argc, char **argv)
35 struct ucl_parser *parser = NULL, *parser2 = NULL;
36 ucl_object_t *obj, *comments = NULL;
39 unsigned char *emitted = NULL;
40 const char *fname_in = NULL, *fname_out = NULL;
41 int ret = 0, opt, json = 0, compact = 0, yaml = 0,
42 save_comments = 0, skip_macro = 0,
43 flags, fd_out, fd_in, use_fd = 0;
44 struct ucl_emitter_functions *func;
46 while ((opt = getopt(argc, argv, "fjcyCM")) != -1) {
67 fprintf (stderr, "Usage: %s [-jcy] [-CM] [-f] [in] [out]\n",
87 if (fname_in != NULL) {
88 in = fopen (fname_in, "r");
98 if (fname_in != NULL) {
99 fd_in = open (fname_in, O_RDONLY);
105 fd_in = STDIN_FILENO;
109 flags = UCL_PARSER_KEY_LOWERCASE;
112 flags |= UCL_PARSER_SAVE_COMMENTS;
116 flags |= UCL_PARSER_DISABLE_MACRO;
119 parser = ucl_parser_new (flags);
120 ucl_parser_register_variable (parser, "ABI", "unknown");
122 if (fname_in != NULL) {
123 ucl_parser_set_filevars (parser, fname_in, true);
127 inbuf = malloc (BUFSIZ);
131 while (!feof (in) && !ferror (in)) {
133 inbuf = realloc (inbuf, bufsize * 2);
140 r += fread (inbuf + r, 1, bufsize - r, in);
144 fprintf (stderr, "Failed to read the input file.\n");
148 ucl_parser_add_chunk (parser, (const unsigned char *)inbuf, r);
152 ucl_parser_add_fd (parser, fd_in);
157 if (fname_out != NULL) {
158 out = fopen (fname_out, "w");
168 if (fname_out != NULL) {
169 fd_out = open (fname_out, O_WRONLY | O_CREAT, 00644);
175 fd_out = STDOUT_FILENO;
180 if (ucl_parser_get_error (parser) != NULL) {
181 fprintf (out, "Error occurred (phase 1): %s\n",
182 ucl_parser_get_error(parser));
187 obj = ucl_parser_get_object (parser);
190 comments = ucl_object_ref (ucl_parser_get_comments (parser));
195 emitted = ucl_object_emit (obj, UCL_EMIT_JSON_COMPACT);
198 emitted = ucl_object_emit (obj, UCL_EMIT_JSON);
202 emitted = ucl_object_emit (obj, UCL_EMIT_YAML);
206 func = ucl_object_emit_memory_funcs ((void **)&emitted);
209 ucl_object_emit_full (obj, UCL_EMIT_CONFIG, func, comments);
210 ucl_object_emit_funcs_free (func);
215 fprintf (out, "%s\n****\n", emitted);
218 ucl_parser_free (parser);
219 ucl_object_unref (obj);
220 parser2 = ucl_parser_new (flags);
221 ucl_parser_add_string (parser2, (const char *)emitted, 0);
223 if (ucl_parser_get_error(parser2) != NULL) {
224 fprintf (out, "Error occurred (phase 2): %s\n",
225 ucl_parser_get_error(parser2));
226 fprintf (out, "%s\n", emitted);
231 if (emitted != NULL) {
235 ucl_object_unref (comments);
240 comments = ucl_object_ref (ucl_parser_get_comments (parser2));
243 obj = ucl_parser_get_object (parser2);
246 func = ucl_object_emit_file_funcs (out);
249 func = ucl_object_emit_fd_funcs (fd_out);
255 ucl_object_emit_full (obj, UCL_EMIT_JSON_COMPACT,
259 ucl_object_emit_full (obj, UCL_EMIT_JSON,
264 ucl_object_emit_full (obj, UCL_EMIT_YAML,
268 ucl_object_emit_full (obj, UCL_EMIT_CONFIG,
272 ucl_object_emit_funcs_free (func);
280 write (fd_out, "\n", 1);
284 ucl_object_unref (obj);
287 if (parser2 != NULL) {
288 ucl_parser_free (parser2);
291 ucl_object_unref (comments);