ucl-tool.c (b626f5a73a48f44a31a200291b141e1da408a2ff) ucl-tool.c (a0409676120c1e558d0ade943019934e0f15118d)
1/* Copyright (c) 2015, Cesanta Software
2 *
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

--- 6 unchanged lines hidden (view full) ---

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.
21 */
22
1/* Copyright (c) 2015, Cesanta Software
2 *
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

--- 6 unchanged lines hidden (view full) ---

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.
21 */
22
23#include <stdio.h>
24#include <getopt.h>
25#include <stdlib.h>
26
27#include "ucl.h"
28
23#include "ucl.h"
24
29static 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'},
35 {0, 0, 0, 0}
36};
37
38void 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");
49}
50
51int main(int argc, char **argv) {
25void usage(const char *name, FILE *out) {
26 fprintf(out, "Usage: %s [--help] [-i|--in file] [-o|--out file]\n", name);
27 fprintf(out, " [-s|--schema file] [-f|--format format]\n\n");
28 fprintf(out, " --help - print this message and exit\n");
29 fprintf(out, " --in - specify input filename "
30 "(default: standard input)\n");
31 fprintf(out, " --out - specify output filename "
32 "(default: standard output)\n");
33 fprintf(out, " --schema - specify schema file for validation\n");
34 fprintf(out, " --format - output format. Options: ucl (default), "
35 "json, compact_json, yaml, msgpack\n");
36}
37
38int main(int argc, char **argv) {
39 int i;
52 char ch;
53 FILE *in = stdin, *out = stdout;
40 char ch;
41 FILE *in = stdin, *out = stdout;
54 const char *schema = NULL;
42 const char *schema = NULL, *parm, *val;
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;
60
43 unsigned char *buf = NULL;
44 size_t size = 0, r = 0;
45 struct ucl_parser *parser = NULL;
46 ucl_object_t *obj = NULL;
47 ucl_emitter_t emitter = UCL_EMIT_CONFIG;
48
61 while((ch = getopt_long(argc, argv, "hi:o:s:f:", opts, NULL)) != -1) {
62 switch (ch) {
63 case 'i':
64 in = fopen(optarg, "r");
49 for (i = 1; i < argc; ++i) {
50 parm = argv[i];
51 val = ((i + 1) < argc) ? argv[++i] : NULL;
52
53 if ((strcmp(parm, "--help") == 0) || (strcmp(parm, "-h") == 0)) {
54 usage(argv[0], stdout);
55 exit(0);
56
57 } else if ((strcmp(parm, "--in") == 0) || (strcmp(parm, "-i") == 0)) {
58 if (!val)
59 goto err_val;
60
61 in = fopen(val, "r");
65 if (in == NULL) {
66 perror("fopen on input file");
67 exit(EXIT_FAILURE);
68 }
62 if (in == NULL) {
63 perror("fopen on input file");
64 exit(EXIT_FAILURE);
65 }
69 break;
70 case 'o':
71 out = fopen(optarg, "w");
66 } else if ((strcmp(parm, "--out") == 0) || (strcmp(parm, "-o") == 0)) {
67 if (!val)
68 goto err_val;
69
70 out = fopen(val, "w");
72 if (out == NULL) {
73 perror("fopen on output file");
74 exit(EXIT_FAILURE);
75 }
71 if (out == NULL) {
72 perror("fopen on output file");
73 exit(EXIT_FAILURE);
74 }
76 break;
77 case 's':
78 schema = optarg;
79 break;
80 case 'f':
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;
91 } else {
92 fprintf(stderr, "Unknown output format: %s\n", optarg);
93 exit(EXIT_FAILURE);
94 }
95 break;
96 case 'h':
97 usage(argv[0], stdout);
98 exit(0);
99 default:
75 } else if ((strcmp(parm, "--schema") == 0) || (strcmp(parm, "-s") == 0)) {
76 if (!val)
77 goto err_val;
78 schema = val;
79
80 } else if ((strcmp(parm, "--format") == 0) || (strcmp(parm, "-f") == 0)) {
81 if (!val)
82 goto err_val;
83
84 if (strcmp(val, "ucl") == 0) {
85 emitter = UCL_EMIT_CONFIG;
86 } else if (strcmp(val, "json") == 0) {
87 emitter = UCL_EMIT_JSON;
88 } else if (strcmp(val, "yaml") == 0) {
89 emitter = UCL_EMIT_YAML;
90 } else if (strcmp(val, "compact_json") == 0) {
91 emitter = UCL_EMIT_JSON_COMPACT;
92 } else if (strcmp(val, "msgpack") == 0) {
93 emitter = UCL_EMIT_MSGPACK;
94 } else {
95 fprintf(stderr, "Unknown output format: %s\n", val);
96 exit(EXIT_FAILURE);
97 }
98 } else {
100 usage(argv[0], stderr);
101 exit(EXIT_FAILURE);
99 usage(argv[0], stderr);
100 exit(EXIT_FAILURE);
102 break;
103 }
104 }
105
106 parser = ucl_parser_new(0);
107 buf = malloc(BUFSIZ);
108 size = BUFSIZ;
101 }
102 }
103
104 parser = ucl_parser_new(0);
105 buf = malloc(BUFSIZ);
106 size = BUFSIZ;
109 while(!feof(in) && !ferror(in)) {
107 while (!feof(in) && !ferror(in)) {
110 if (r == size) {
111 buf = realloc(buf, size*2);
112 size *= 2;
113 if (buf == NULL) {
114 perror("realloc");
115 exit(EXIT_FAILURE);
116 }
117 }

--- 32 unchanged lines hidden (view full) ---

150 if (!ucl_object_validate(schema_obj, obj, &error)) {
151 fprintf(stderr, "Validation failed: %s\n", error.msg);
152 exit(EXIT_FAILURE);
153 }
154 }
155
156 if (emitter != UCL_EMIT_MSGPACK) {
157 fprintf(out, "%s\n", ucl_object_emit(obj, emitter));
108 if (r == size) {
109 buf = realloc(buf, size*2);
110 size *= 2;
111 if (buf == NULL) {
112 perror("realloc");
113 exit(EXIT_FAILURE);
114 }
115 }

--- 32 unchanged lines hidden (view full) ---

148 if (!ucl_object_validate(schema_obj, obj, &error)) {
149 fprintf(stderr, "Validation failed: %s\n", error.msg);
150 exit(EXIT_FAILURE);
151 }
152 }
153
154 if (emitter != UCL_EMIT_MSGPACK) {
155 fprintf(out, "%s\n", ucl_object_emit(obj, emitter));
158 }
159 else {
156 } else {
160 size_t len;
161 unsigned char *res;
162
163 res = ucl_object_emit_len(obj, emitter, &len);
164 fwrite(res, 1, len, out);
165 }
166
167 return 0;
157 size_t len;
158 unsigned char *res;
159
160 res = ucl_object_emit_len(obj, emitter, &len);
161 fwrite(res, 1, len, out);
162 }
163
164 return 0;
165
166err_val:
167 fprintf(stderr, "Parameter %s is missing mandatory value\n", parm);
168 usage(argv[0], stderr);
169 exit(EXIT_FAILURE);
168}
170}