diff options
author | bapt <bapt@FreeBSD.org> | 2015-10-27 21:24:09 +0000 |
---|---|---|
committer | bapt <bapt@FreeBSD.org> | 2015-10-27 21:24:09 +0000 |
commit | 1db684f7ee1410ba0acf51937dc2748211c38efa (patch) | |
tree | 9b000a9777302debbb64fb5d2b8670bf0685b757 /contrib/libucl/utils | |
parent | f4de602b0d1cd88f3d73de6148c83c4158e58a15 (diff) | |
parent | bf66c97c4a64e64410bf0223d221a54ca9555f52 (diff) | |
download | FreeBSD-src-1db684f7ee1410ba0acf51937dc2748211c38efa.zip FreeBSD-src-1db684f7ee1410ba0acf51937dc2748211c38efa.tar.gz |
Update libucl to latest git snapshot (20151027)
Diffstat (limited to 'contrib/libucl/utils')
-rw-r--r-- | contrib/libucl/utils/Makefile.am | 6 | ||||
-rw-r--r-- | contrib/libucl/utils/chargen.c | 6 | ||||
-rw-r--r-- | contrib/libucl/utils/objdump.c | 25 | ||||
-rw-r--r-- | contrib/libucl/utils/ucl-tool.c | 168 |
4 files changed, 198 insertions, 7 deletions
diff --git a/contrib/libucl/utils/Makefile.am b/contrib/libucl/utils/Makefile.am index 23eeeac..ec85aaa 100644 --- a/contrib/libucl/utils/Makefile.am +++ b/contrib/libucl/utils/Makefile.am @@ -11,8 +11,12 @@ ucl_objdump_SOURCES = objdump.c ucl_objdump_LDADD = $(common_utils_ldadd) ucl_objdump_CFLAGS = $(common_utils_cflags) +ucl_tool_SOURCES = ucl-tool.c +ucl_tool_LDADD = $(common_utils_ldadd) +ucl_tool_CFLAGS = $(common_utils_cflags) + if UTILS -UTL = ucl_chargen ucl_objdump +UTL = ucl_chargen ucl_objdump ucl_tool else UTL = endif diff --git a/contrib/libucl/utils/chargen.c b/contrib/libucl/utils/chargen.c index d6fa86a..3981340 100644 --- a/contrib/libucl/utils/chargen.c +++ b/contrib/libucl/utils/chargen.c @@ -54,9 +54,9 @@ main (int argc, char **argv) name = argv[1]; } - printf ("static const unsigned int %s[255] = {\n", name); + printf ("static const unsigned int %s[256] = {\n", name); - for (i = 0; i < 255; i ++) { + for (i = 0; i < 256; i ++) { need_or = false; r = 0; /* UCL_CHARACTER_VALUE_END */ @@ -110,7 +110,7 @@ main (int argc, char **argv) if (isprint (i)) { r += sprintf (valbuf + r, " /* %c */", i); } - if (i != 254) { + if (i != 255) { r += sprintf (valbuf + r, ", "); } col += r; diff --git a/contrib/libucl/utils/objdump.c b/contrib/libucl/utils/objdump.c index 74581ba..6fde2f4 100644 --- a/contrib/libucl/utils/objdump.c +++ b/contrib/libucl/utils/objdump.c @@ -99,9 +99,10 @@ int main(int argc, char **argv) { const char *fn = NULL; - unsigned char inbuf[8192]; + unsigned char *inbuf; struct ucl_parser *parser; int k, ret = 0, r = 0; + ssize_t bufsize; ucl_object_t *obj = NULL; const ucl_object_t *par; FILE *in; @@ -121,9 +122,27 @@ main(int argc, char **argv) } parser = ucl_parser_new (0); - while (!feof (in) && r < (int)sizeof (inbuf)) { - r += fread (inbuf + r, 1, sizeof (inbuf) - r, in); + inbuf = malloc (BUFSIZ); + bufsize = BUFSIZ; + r = 0; + + while (!feof (in) && !ferror (in)) { + if (r == bufsize) { + inbuf = realloc (inbuf, bufsize * 2); + bufsize *= 2; + if (inbuf == NULL) { + perror ("realloc"); + exit (EXIT_FAILURE); + } + } + r += fread (inbuf + r, 1, bufsize - r, in); } + + if (ferror (in)) { + fprintf (stderr, "Failed to read the input file.\n"); + exit (EXIT_FAILURE); + } + ucl_parser_add_chunk (parser, inbuf, r); fclose (in); if (ucl_parser_get_error(parser)) { diff --git a/contrib/libucl/utils/ucl-tool.c b/contrib/libucl/utils/ucl-tool.c new file mode 100644 index 0000000..feea9c2 --- /dev/null +++ b/contrib/libucl/utils/ucl-tool.c @@ -0,0 +1,168 @@ +/* Copyright (c) 2015, Cesanta Software + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include <stdio.h> +#include <getopt.h> +#include <stdlib.h> + +#include "ucl.h" + +static struct option opts[] = { + {"help", no_argument, NULL, 'h'}, + {"in", required_argument, NULL, 'i' }, + {"out", required_argument, NULL, 'o' }, + {"schema", required_argument, NULL, 's'}, + {"format", required_argument, NULL, 'f'}, + {0, 0, 0, 0} +}; + +void usage(const char *name, FILE *out) { + fprintf(out, "Usage: %s [--help] [-i|--in file] [-o|--out file]\n", name); + fprintf(out, " [-s|--schema file] [-f|--format format]\n\n"); + fprintf(out, " --help - print this message and exit\n"); + fprintf(out, " --in - specify input filename " + "(default: standard input)\n"); + fprintf(out, " --out - specify output filename " + "(default: standard output)\n"); + fprintf(out, " --schema - specify schema file for validation\n"); + fprintf(out, " --format - output format. Options: ucl (default), " + "json, compact_json, yaml, msgpack\n"); +} + +int main(int argc, char **argv) { + char ch; + FILE *in = stdin, *out = stdout; + const char *schema = NULL; + unsigned char *buf = NULL; + size_t size = 0, r = 0; + struct ucl_parser *parser = NULL; + ucl_object_t *obj = NULL; + ucl_emitter_t emitter = UCL_EMIT_CONFIG; + + while((ch = getopt_long(argc, argv, "hi:o:s:f:", opts, NULL)) != -1) { + switch (ch) { + case 'i': + in = fopen(optarg, "r"); + if (in == NULL) { + perror("fopen on input file"); + exit(EXIT_FAILURE); + } + break; + case 'o': + out = fopen(optarg, "w"); + if (out == NULL) { + perror("fopen on output file"); + exit(EXIT_FAILURE); + } + break; + case 's': + schema = optarg; + break; + case 'f': + if (strcmp(optarg, "ucl") == 0) { + emitter = UCL_EMIT_CONFIG; + } else if (strcmp(optarg, "json") == 0) { + emitter = UCL_EMIT_JSON; + } else if (strcmp(optarg, "yaml") == 0) { + emitter = UCL_EMIT_YAML; + } else if (strcmp(optarg, "compact_json") == 0) { + emitter = UCL_EMIT_JSON_COMPACT; + } else if (strcmp(optarg, "msgpack") == 0) { + emitter = UCL_EMIT_MSGPACK; + } else { + fprintf(stderr, "Unknown output format: %s\n", optarg); + exit(EXIT_FAILURE); + } + break; + case 'h': + usage(argv[0], stdout); + exit(0); + default: + usage(argv[0], stderr); + exit(EXIT_FAILURE); + break; + } + } + + parser = ucl_parser_new(0); + buf = malloc(BUFSIZ); + size = BUFSIZ; + while(!feof(in) && !ferror(in)) { + if (r == size) { + buf = realloc(buf, size*2); + size *= 2; + if (buf == NULL) { + perror("realloc"); + exit(EXIT_FAILURE); + } + } + r += fread(buf + r, 1, size - r, in); + } + if (ferror(in)) { + fprintf(stderr, "Failed to read the input file.\n"); + exit(EXIT_FAILURE); + } + fclose(in); + if (!ucl_parser_add_chunk(parser, buf, r)) { + fprintf(stderr, "Failed to parse input file: %s\n", + ucl_parser_get_error(parser)); + exit(EXIT_FAILURE); + } + if ((obj = ucl_parser_get_object(parser)) == NULL) { + fprintf(stderr, "Failed to get root object: %s\n", + ucl_parser_get_error(parser)); + exit(EXIT_FAILURE); + } + if (schema != NULL) { + struct ucl_parser *schema_parser = ucl_parser_new(0); + ucl_object_t *schema_obj = NULL; + struct ucl_schema_error error; + + if (!ucl_parser_add_file(schema_parser, schema)) { + fprintf(stderr, "Failed to parse schema file: %s\n", + ucl_parser_get_error(schema_parser)); + exit(EXIT_FAILURE); + } + if ((schema_obj = ucl_parser_get_object(schema_parser)) == NULL) { + fprintf(stderr, "Failed to get root object: %s\n", + ucl_parser_get_error(schema_parser)); + exit(EXIT_FAILURE); + } + if (!ucl_object_validate(schema_obj, obj, &error)) { + fprintf(stderr, "Validation failed: %s\n", error.msg); + exit(EXIT_FAILURE); + } + } + + if (emitter != UCL_EMIT_MSGPACK) { + fprintf(out, "%s\n", ucl_object_emit(obj, emitter)); + } + else { + size_t len; + unsigned char *res; + + res = ucl_object_emit_len(obj, emitter, &len); + fwrite(res, 1, len, out); + } + + return 0; +} |