1 /* Copyright (c) 2013, Vsevolod Stakhov
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.
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.
25 * @file this utility generates character table for ucl
33 print_flag (const char *flag, bool *need_or, char *val)
36 res = sprintf (val, "%s%s", *need_or ? "|" : "", flag);
44 main (int argc, char **argv)
47 const char *name = "ucl_chartable";
57 printf ("static const unsigned int %s[255] = {\n", name);
59 for (i = 0; i < 255; i ++) {
62 /* UCL_CHARACTER_VALUE_END */
64 if (i == ' ' || i == '\t') {
65 r += print_flag ("UCL_CHARACTER_WHITESPACE", &need_or, valbuf + r);
68 r += print_flag ("UCL_CHARACTER_WHITESPACE_UNSAFE", &need_or, valbuf + r);
70 if (isalnum (i) || i >= 0x80 || i == '/' || i == '_') {
71 r += print_flag ("UCL_CHARACTER_KEY_START", &need_or, valbuf + r);
73 if (isalnum (i) || i == '-' || i == '_' || i == '/' || i == '.' || i >= 0x80) {
74 r += print_flag ("UCL_CHARACTER_KEY", &need_or, valbuf + r);
76 if (i == 0 || i == '\r' || i == '\n' || i == ']' || i == '}' || i == ';' || i == ',' || i == '#') {
77 r += print_flag ("UCL_CHARACTER_VALUE_END", &need_or, valbuf + r);
80 if (isprint (i) || i >= 0x80) {
81 r += print_flag ("UCL_CHARACTER_VALUE_STR", &need_or, valbuf + r);
83 if (isdigit (i) || i == '-') {
84 r += print_flag ("UCL_CHARACTER_VALUE_DIGIT_START", &need_or, valbuf + r);
86 if (isalnum (i) || i == '.' || i == '-' || i == '+') {
87 r += print_flag ("UCL_CHARACTER_VALUE_DIGIT", &need_or, valbuf + r);
90 if (i == '"' || i == '\\' || i == '/' || i == 'b' ||
91 i == 'f' || i == 'n' || i == 'r' || i == 't' || i == 'u') {
92 r += print_flag ("UCL_CHARACTER_ESCAPE", &need_or, valbuf + r);
94 if (i == ' ' || i == '\t' || i == ':' || i == '=') {
95 r += print_flag ("UCL_CHARACTER_KEY_SEP", &need_or, valbuf + r);
97 if (i == '\n' || i == '\r' || i == '\\' || i == '\b' || i == '\t' ||
98 i == '"' || i == '\f') {
99 r += print_flag ("UCL_CHARACTER_JSON_UNSAFE", &need_or, valbuf + r);
101 if (i == '\n' || i == '\r' || i == '\\' || i == '\b' || i == '\t' ||
102 i == '"' || i == '\f' || i == '=' || i == ':' || i == '{' || i == '[' || i == ' ') {
103 r += print_flag ("UCL_CHARACTER_UCL_UNSAFE", &need_or, valbuf + r);
107 r += print_flag ("UCL_CHARACTER_DENIED", &need_or, valbuf + r);
111 r += sprintf (valbuf + r, " /* %c */", i);
114 r += sprintf (valbuf + r, ", ");
118 printf ("\n%s", valbuf);
122 printf ("%s", valbuf);