diff options
Diffstat (limited to 'utils')
-rw-r--r-- | utils/chargen.c | 128 | ||||
-rw-r--r-- | utils/objdump.c | 159 |
2 files changed, 287 insertions, 0 deletions
diff --git a/utils/chargen.c b/utils/chargen.c new file mode 100644 index 000000000000..d6fa86a20bd2 --- /dev/null +++ b/utils/chargen.c @@ -0,0 +1,128 @@ +/* Copyright (c) 2013, Vsevolod Stakhov + * All rights reserved. + * + * 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. + */ + +/** + * @file this utility generates character table for ucl + */ + +#include <stdio.h> +#include <ctype.h> +#include <stdbool.h> + +static inline int +print_flag (const char *flag, bool *need_or, char *val) +{ + int res; + res = sprintf (val, "%s%s", *need_or ? "|" : "", flag); + + *need_or |= true; + + return res; +} + +int +main (int argc, char **argv) +{ + int i, col, r; + const char *name = "ucl_chartable"; + bool need_or; + char valbuf[2048]; + + col = 0; + + if (argc > 1) { + name = argv[1]; + } + + printf ("static const unsigned int %s[255] = {\n", name); + + for (i = 0; i < 255; i ++) { + need_or = false; + r = 0; + /* UCL_CHARACTER_VALUE_END */ + + if (i == ' ' || i == '\t') { + r += print_flag ("UCL_CHARACTER_WHITESPACE", &need_or, valbuf + r); + } + if (isspace (i)) { + r += print_flag ("UCL_CHARACTER_WHITESPACE_UNSAFE", &need_or, valbuf + r); + } + if (isalnum (i) || i >= 0x80 || i == '/' || i == '_') { + r += print_flag ("UCL_CHARACTER_KEY_START", &need_or, valbuf + r); + } + if (isalnum (i) || i == '-' || i == '_' || i == '/' || i == '.' || i >= 0x80) { + r += print_flag ("UCL_CHARACTER_KEY", &need_or, valbuf + r); + } + if (i == 0 || i == '\r' || i == '\n' || i == ']' || i == '}' || i == ';' || i == ',' || i == '#') { + r += print_flag ("UCL_CHARACTER_VALUE_END", &need_or, valbuf + r); + } + else { + if (isprint (i) || i >= 0x80) { + r += print_flag ("UCL_CHARACTER_VALUE_STR", &need_or, valbuf + r); + } + if (isdigit (i) || i == '-') { + r += print_flag ("UCL_CHARACTER_VALUE_DIGIT_START", &need_or, valbuf + r); + } + if (isalnum (i) || i == '.' || i == '-' || i == '+') { + r += print_flag ("UCL_CHARACTER_VALUE_DIGIT", &need_or, valbuf + r); + } + } + if (i == '"' || i == '\\' || i == '/' || i == 'b' || + i == 'f' || i == 'n' || i == 'r' || i == 't' || i == 'u') { + r += print_flag ("UCL_CHARACTER_ESCAPE", &need_or, valbuf + r); + } + if (i == ' ' || i == '\t' || i == ':' || i == '=') { + r += print_flag ("UCL_CHARACTER_KEY_SEP", &need_or, valbuf + r); + } + if (i == '\n' || i == '\r' || i == '\\' || i == '\b' || i == '\t' || + i == '"' || i == '\f') { + r += print_flag ("UCL_CHARACTER_JSON_UNSAFE", &need_or, valbuf + r); + } + if (i == '\n' || i == '\r' || i == '\\' || i == '\b' || i == '\t' || + i == '"' || i == '\f' || i == '=' || i == ':' || i == '{' || i == '[' || i == ' ') { + r += print_flag ("UCL_CHARACTER_UCL_UNSAFE", &need_or, valbuf + r); + } + + if (!need_or) { + r += print_flag ("UCL_CHARACTER_DENIED", &need_or, valbuf + r); + } + + if (isprint (i)) { + r += sprintf (valbuf + r, " /* %c */", i); + } + if (i != 254) { + r += sprintf (valbuf + r, ", "); + } + col += r; + if (col > 80) { + printf ("\n%s", valbuf); + col = r; + } + else { + printf ("%s", valbuf); + } + } + printf ("\n}\n"); + + return 0; +} diff --git a/utils/objdump.c b/utils/objdump.c new file mode 100644 index 000000000000..8bf6e28edd53 --- /dev/null +++ b/utils/objdump.c @@ -0,0 +1,159 @@ +/* Copyright (c) 2013, Dmitriy V. Reshetnikov + * Copyright (c) 2013, Vsevolod Stakhov + * All rights reserved. + * + * 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 <errno.h> + +#include "ucl.h" + +void +ucl_obj_dump(ucl_object_t *obj, unsigned int shift) +{ + int num = shift * 4 + 5; + char *pre = (char *) malloc (num * sizeof(char)); + ucl_object_t *cur, *tmp; + ucl_object_iter_t it = NULL, it_obj = NULL; + + pre[--num] = 0x00; + while (num--) + pre[num] = 0x20; + + tmp = obj; + + while ((obj = ucl_iterate_object (tmp, &it, false))) { + printf ("%sucl object address: %p\n", pre + 4, obj); + if (obj->key != NULL) { + printf ("%skey: \"%s\"\n", pre, ucl_object_key (obj)); + } + printf ("%sref: %hd\n", pre, obj->ref); + printf ("%slen: %u\n", pre, obj->len); + printf ("%sprev: %p\n", pre, obj->prev); + printf ("%snext: %p\n", pre, obj->next); + if (obj->type == UCL_OBJECT) { + printf ("%stype: UCL_OBJECT\n", pre); + printf ("%svalue: %p\n", pre, obj->value.ov); + while ((cur = ucl_iterate_object (obj, &it_obj, true))) { + ucl_obj_dump (cur, shift + 2); + } + } + else if (obj->type == UCL_ARRAY) { + printf ("%stype: UCL_ARRAY\n", pre); + printf ("%svalue: %p\n", pre, obj->value.av); + ucl_obj_dump (obj->value.av, shift + 2); + } + else if (obj->type == UCL_INT) { + printf ("%stype: UCL_INT\n", pre); + printf ("%svalue: %ld\n", pre, ucl_object_toint (obj)); + } + else if (obj->type == UCL_FLOAT) { + printf ("%stype: UCL_FLOAT\n", pre); + printf ("%svalue: %f\n", pre, ucl_object_todouble (obj)); + } + else if (obj->type == UCL_STRING) { + printf ("%stype: UCL_STRING\n", pre); + printf ("%svalue: \"%s\"\n", pre, ucl_object_tostring (obj)); + } + else if (obj->type == UCL_BOOLEAN) { + printf ("%stype: UCL_BOOLEAN\n", pre); + printf ("%svalue: %s\n", pre, ucl_object_tostring_forced (obj)); + } + else if (obj->type == UCL_TIME) { + printf ("%stype: UCL_TIME\n", pre); + printf ("%svalue: %f\n", pre, ucl_object_todouble (obj)); + } + else if (obj->type == UCL_USERDATA) { + printf ("%stype: UCL_USERDATA\n", pre); + printf ("%svalue: %p\n", pre, obj->value.ud); + } + } + + free (pre); +} + +int +main(int argc, char **argv) +{ + const char *fn = NULL; + char inbuf[8192]; + struct ucl_parser *parser; + int k, ret = 0, r = 0; + ucl_object_t *obj = NULL; + ucl_object_t *par; + FILE *in; + + if (argc > 1) { + fn = argv[1]; + } + + if (fn != NULL) { + in = fopen (fn, "r"); + if (in == NULL) { + exit (-errno); + } + } + else { + in = stdin; + } + + parser = ucl_parser_new (0); + while (!feof (in) && r < (int)sizeof (inbuf)) { + r += fread (inbuf + r, 1, sizeof (inbuf) - r, in); + } + ucl_parser_add_chunk (parser, inbuf, r); + fclose (in); + if (ucl_parser_get_error(parser)) { + printf ("Error occured: %s\n", ucl_parser_get_error(parser)); + ret = 1; + goto end; + } + + obj = ucl_parser_get_object (parser); + if (ucl_parser_get_error(parser)) { + printf ("Error occured: %s\n", ucl_parser_get_error(parser)); + ret = 1; + goto end; + } + + if (argc > 2) { + for (k = 2; k < argc; k++) { + printf ("search for \"%s\"... ", argv[k]); + par = ucl_object_find_key (obj, argv[k]); + printf ("%sfound\n", (par == NULL )?"not ":""); + ucl_obj_dump (par, 0); + } + } + else { + ucl_obj_dump (obj, 0); + } + +end: + if (parser != NULL) { + ucl_parser_free (parser); + } + if (obj != NULL) { + ucl_object_unref (obj); + } + + return ret; +} |