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"
28 main (int argc, char **argv)
30 char inbuf[8192], *test_in = NULL;
31 struct ucl_parser *parser = NULL, *parser2 = NULL;
34 unsigned char *emitted = NULL;
35 const char *fname_in = NULL, *fname_out = NULL;
36 int ret = 0, inlen, opt, json = 0, compact = 0, yaml = 0;
38 while ((opt = getopt(argc, argv, "jcy")) != -1) {
50 fprintf (stderr, "Usage: %s [-jcy] [in] [out]\n",
69 if (fname_in != NULL) {
70 in = fopen (fname_in, "r");
78 parser = ucl_parser_new (UCL_PARSER_KEY_LOWERCASE);
79 ucl_parser_register_variable (parser, "ABI", "unknown");
81 if (fname_in != NULL) {
82 ucl_parser_set_filevars (parser, fname_in, true);
86 memset (inbuf, 0, sizeof (inbuf));
87 if (fread (inbuf, 1, sizeof (inbuf) - 1, in) == 0) {
90 inlen = strlen (inbuf);
91 test_in = malloc (inlen);
92 memcpy (test_in, inbuf, inlen);
93 ucl_parser_add_chunk (parser, (const unsigned char *)test_in, inlen);
97 if (fname_out != NULL) {
98 out = fopen (fname_out, "w");
106 if (ucl_parser_get_error (parser) != NULL) {
107 fprintf (out, "Error occurred: %s\n", ucl_parser_get_error(parser));
111 obj = ucl_parser_get_object (parser);
114 emitted = ucl_object_emit (obj, UCL_EMIT_JSON_COMPACT);
117 emitted = ucl_object_emit (obj, UCL_EMIT_JSON);
121 emitted = ucl_object_emit (obj, UCL_EMIT_YAML);
124 emitted = ucl_object_emit (obj, UCL_EMIT_CONFIG);
126 ucl_parser_free (parser);
127 ucl_object_unref (obj);
128 parser2 = ucl_parser_new (UCL_PARSER_KEY_LOWERCASE);
129 ucl_parser_add_string (parser2, (const char *)emitted, 0);
131 if (ucl_parser_get_error(parser2) != NULL) {
132 fprintf (out, "Error occurred: %s\n", ucl_parser_get_error(parser2));
133 fprintf (out, "%s\n", emitted);
137 if (emitted != NULL) {
140 obj = ucl_parser_get_object (parser2);
143 emitted = ucl_object_emit (obj, UCL_EMIT_JSON_COMPACT);
146 emitted = ucl_object_emit (obj, UCL_EMIT_JSON);
150 emitted = ucl_object_emit (obj, UCL_EMIT_YAML);
153 emitted = ucl_object_emit (obj, UCL_EMIT_CONFIG);
156 fprintf (out, "%s\n", emitted);
157 ucl_object_unref (obj);
160 if (emitted != NULL) {
163 if (parser2 != NULL) {
164 ucl_parser_free (parser2);
166 if (test_in != NULL) {