Deleted Added
full compact
test_basic.c (262398) test_basic.c (263648)
1/* Copyright (c) 2013, Vsevolod Stakhov
2 * All rights reserved.
3 *
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.
11 *
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.
22 */
23
1/* Copyright (c) 2013, Vsevolod Stakhov
2 * All rights reserved.
3 *
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.
11 *
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.
22 */
23
24#include <stdio.h>
25#include <errno.h>
26#include <unistd.h>
27#include "ucl.h"
24#include "ucl.h"
25#include "ucl_internal.h"
28
29int
30main (int argc, char **argv)
31{
32 char inbuf[8192], *test_in = NULL;
33 struct ucl_parser *parser = NULL, *parser2 = NULL;
34 ucl_object_t *obj;
35 FILE *in, *out;
36 unsigned char *emitted = NULL;
37 const char *fname_in = NULL, *fname_out = NULL;
38 int ret = 0, inlen, opt, json = 0;
39
40 while ((opt = getopt(argc, argv, "j")) != -1) {
41 switch (opt) {
42 case 'j':
43 json = 1;
44 break;
45 default: /* '?' */
46 fprintf (stderr, "Usage: %s [-j] [in] [out]\n",
47 argv[0]);
48 exit (EXIT_FAILURE);
49 }
50 }
51
52 argc -= optind;
53 argv += optind;
54
55 switch (argc) {
56 case 1:
57 fname_in = argv[0];
58 break;
59 case 2:
60 fname_in = argv[0];
61 fname_out = argv[1];
62 break;
63 }
64
65 if (fname_in != NULL) {
66 in = fopen (fname_in, "r");
67 if (in == NULL) {
68 exit (-errno);
69 }
70 }
71 else {
72 in = stdin;
73 }
74 parser = ucl_parser_new (UCL_PARSER_KEY_LOWERCASE);
75 ucl_parser_register_variable (parser, "ABI", "unknown");
76
77 if (fname_in != NULL) {
78 ucl_parser_set_filevars (parser, fname_in, true);
79 }
80
81 while (!feof (in)) {
82 memset (inbuf, 0, sizeof (inbuf));
83 (void)fread (inbuf, sizeof (inbuf) - 1, 1, in);
84 inlen = strlen (inbuf);
85 test_in = malloc (inlen);
86 memcpy (test_in, inbuf, inlen);
87 ucl_parser_add_chunk (parser, test_in, inlen);
88 }
89 fclose (in);
90
91 if (fname_out != NULL) {
92 out = fopen (fname_out, "w");
93 if (out == NULL) {
94 exit (-errno);
95 }
96 }
97 else {
98 out = stdout;
99 }
26
27int
28main (int argc, char **argv)
29{
30 char inbuf[8192], *test_in = NULL;
31 struct ucl_parser *parser = NULL, *parser2 = NULL;
32 ucl_object_t *obj;
33 FILE *in, *out;
34 unsigned char *emitted = NULL;
35 const char *fname_in = NULL, *fname_out = NULL;
36 int ret = 0, inlen, opt, json = 0;
37
38 while ((opt = getopt(argc, argv, "j")) != -1) {
39 switch (opt) {
40 case 'j':
41 json = 1;
42 break;
43 default: /* '?' */
44 fprintf (stderr, "Usage: %s [-j] [in] [out]\n",
45 argv[0]);
46 exit (EXIT_FAILURE);
47 }
48 }
49
50 argc -= optind;
51 argv += optind;
52
53 switch (argc) {
54 case 1:
55 fname_in = argv[0];
56 break;
57 case 2:
58 fname_in = argv[0];
59 fname_out = argv[1];
60 break;
61 }
62
63 if (fname_in != NULL) {
64 in = fopen (fname_in, "r");
65 if (in == NULL) {
66 exit (-errno);
67 }
68 }
69 else {
70 in = stdin;
71 }
72 parser = ucl_parser_new (UCL_PARSER_KEY_LOWERCASE);
73 ucl_parser_register_variable (parser, "ABI", "unknown");
74
75 if (fname_in != NULL) {
76 ucl_parser_set_filevars (parser, fname_in, true);
77 }
78
79 while (!feof (in)) {
80 memset (inbuf, 0, sizeof (inbuf));
81 (void)fread (inbuf, sizeof (inbuf) - 1, 1, in);
82 inlen = strlen (inbuf);
83 test_in = malloc (inlen);
84 memcpy (test_in, inbuf, inlen);
85 ucl_parser_add_chunk (parser, test_in, inlen);
86 }
87 fclose (in);
88
89 if (fname_out != NULL) {
90 out = fopen (fname_out, "w");
91 if (out == NULL) {
92 exit (-errno);
93 }
94 }
95 else {
96 out = stdout;
97 }
100 if (ucl_parser_get_error(parser) != NULL) {
98 if (ucl_parser_get_error (parser) != NULL) {
101 fprintf (out, "Error occurred: %s\n", ucl_parser_get_error(parser));
102 ret = 1;
103 goto end;
104 }
105 obj = ucl_parser_get_object (parser);
106 if (json) {
107 emitted = ucl_object_emit (obj, UCL_EMIT_JSON);
108 }
109 else {
110 emitted = ucl_object_emit (obj, UCL_EMIT_CONFIG);
111 }
112 ucl_parser_free (parser);
113 ucl_object_unref (obj);
114 parser2 = ucl_parser_new (UCL_PARSER_KEY_LOWERCASE);
99 fprintf (out, "Error occurred: %s\n", ucl_parser_get_error(parser));
100 ret = 1;
101 goto end;
102 }
103 obj = ucl_parser_get_object (parser);
104 if (json) {
105 emitted = ucl_object_emit (obj, UCL_EMIT_JSON);
106 }
107 else {
108 emitted = ucl_object_emit (obj, UCL_EMIT_CONFIG);
109 }
110 ucl_parser_free (parser);
111 ucl_object_unref (obj);
112 parser2 = ucl_parser_new (UCL_PARSER_KEY_LOWERCASE);
115 ucl_parser_add_chunk (parser2, emitted, strlen (emitted));
113 ucl_parser_add_string (parser2, emitted, 0);
116
117 if (ucl_parser_get_error(parser2) != NULL) {
118 fprintf (out, "Error occurred: %s\n", ucl_parser_get_error(parser2));
119 fprintf (out, "%s\n", emitted);
120 ret = 1;
121 goto end;
122 }
123 if (emitted != NULL) {
124 free (emitted);
125 }
126 obj = ucl_parser_get_object (parser2);
127 if (json) {
128 emitted = ucl_object_emit (obj, UCL_EMIT_JSON);
129 }
130 else {
131 emitted = ucl_object_emit (obj, UCL_EMIT_CONFIG);
132 }
133
134 fprintf (out, "%s\n", emitted);
135 ucl_object_unref (obj);
136
137end:
138 if (emitted != NULL) {
139 free (emitted);
140 }
141 if (parser2 != NULL) {
142 ucl_parser_free (parser2);
143 }
144 if (test_in != NULL) {
145 free (test_in);
146 }
147
148 fclose (out);
149
150 return ret;
151}
114
115 if (ucl_parser_get_error(parser2) != NULL) {
116 fprintf (out, "Error occurred: %s\n", ucl_parser_get_error(parser2));
117 fprintf (out, "%s\n", emitted);
118 ret = 1;
119 goto end;
120 }
121 if (emitted != NULL) {
122 free (emitted);
123 }
124 obj = ucl_parser_get_object (parser2);
125 if (json) {
126 emitted = ucl_object_emit (obj, UCL_EMIT_JSON);
127 }
128 else {
129 emitted = ucl_object_emit (obj, UCL_EMIT_CONFIG);
130 }
131
132 fprintf (out, "%s\n", emitted);
133 ucl_object_unref (obj);
134
135end:
136 if (emitted != NULL) {
137 free (emitted);
138 }
139 if (parser2 != NULL) {
140 ucl_parser_free (parser2);
141 }
142 if (test_in != NULL) {
143 free (test_in);
144 }
145
146 fclose (out);
147
148 return ret;
149}