1 /* Copyright (c) 2015, Cesanta Software
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5 * * Redistributions of source code must retain the above copyright
6 * notice, this list of conditions and the following disclaimer.
7 * * Redistributions in binary form must reproduce the above copyright
8 * notice, this list of conditions and the following disclaimer in the
9 * documentation and/or other materials provided with the distribution.
11 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
12 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
13 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
14 * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
15 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
16 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
17 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
18 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
19 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
20 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 static struct option opts[] = {
30 {"help", no_argument, NULL, 'h'},
31 {"in", required_argument, NULL, 'i' },
32 {"out", required_argument, NULL, 'o' },
33 {"schema", required_argument, NULL, 's'},
34 {"format", required_argument, NULL, 'f'},
38 void usage(const char *name, FILE *out) {
39 fprintf(out, "Usage: %s [--help] [-i|--in file] [-o|--out file]\n", name);
40 fprintf(out, " [-s|--schema file] [-f|--format format]\n\n");
41 fprintf(out, " --help - print this message and exit\n");
42 fprintf(out, " --in - specify input filename "
43 "(default: standard input)\n");
44 fprintf(out, " --out - specify output filename "
45 "(default: standard output)\n");
46 fprintf(out, " --schema - specify schema file for validation\n");
47 fprintf(out, " --format - output format. Options: ucl (default), "
48 "json, compact_json, yaml, msgpack\n");
51 int main(int argc, char **argv) {
53 FILE *in = stdin, *out = stdout;
54 const char *schema = NULL;
55 unsigned char *buf = NULL;
56 size_t size = 0, r = 0;
57 struct ucl_parser *parser = NULL;
58 ucl_object_t *obj = NULL;
59 ucl_emitter_t emitter = UCL_EMIT_CONFIG;
61 while((ch = getopt_long(argc, argv, "hi:o:s:f:", opts, NULL)) != -1) {
64 in = fopen(optarg, "r");
66 perror("fopen on input file");
71 out = fopen(optarg, "w");
73 perror("fopen on output file");
81 if (strcmp(optarg, "ucl") == 0) {
82 emitter = UCL_EMIT_CONFIG;
83 } else if (strcmp(optarg, "json") == 0) {
84 emitter = UCL_EMIT_JSON;
85 } else if (strcmp(optarg, "yaml") == 0) {
86 emitter = UCL_EMIT_YAML;
87 } else if (strcmp(optarg, "compact_json") == 0) {
88 emitter = UCL_EMIT_JSON_COMPACT;
89 } else if (strcmp(optarg, "msgpack") == 0) {
90 emitter = UCL_EMIT_MSGPACK;
92 fprintf(stderr, "Unknown output format: %s\n", optarg);
97 usage(argv[0], stdout);
100 usage(argv[0], stderr);
106 parser = ucl_parser_new(0);
107 buf = malloc(BUFSIZ);
109 while(!feof(in) && !ferror(in)) {
111 buf = realloc(buf, size*2);
118 r += fread(buf + r, 1, size - r, in);
121 fprintf(stderr, "Failed to read the input file.\n");
125 if (!ucl_parser_add_chunk(parser, buf, r)) {
126 fprintf(stderr, "Failed to parse input file: %s\n",
127 ucl_parser_get_error(parser));
130 if ((obj = ucl_parser_get_object(parser)) == NULL) {
131 fprintf(stderr, "Failed to get root object: %s\n",
132 ucl_parser_get_error(parser));
135 if (schema != NULL) {
136 struct ucl_parser *schema_parser = ucl_parser_new(0);
137 ucl_object_t *schema_obj = NULL;
138 struct ucl_schema_error error;
140 if (!ucl_parser_add_file(schema_parser, schema)) {
141 fprintf(stderr, "Failed to parse schema file: %s\n",
142 ucl_parser_get_error(schema_parser));
145 if ((schema_obj = ucl_parser_get_object(schema_parser)) == NULL) {
146 fprintf(stderr, "Failed to get root object: %s\n",
147 ucl_parser_get_error(schema_parser));
150 if (!ucl_object_validate(schema_obj, obj, &error)) {
151 fprintf(stderr, "Validation failed: %s\n", error.msg);
156 if (emitter != UCL_EMIT_MSGPACK) {
157 fprintf(out, "%s\n", ucl_object_emit(obj, emitter));
163 res = ucl_object_emit_len(obj, emitter, &len);
164 fwrite(res, 1, len, out);