2 * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
3 * Copyright 2015 John Marino <draco@marino.st>
5 * This source code is derived from the illumos localedef command, and
6 * provided under BSD-style license terms by Nexenta Systems, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
32 * This file contains the "scanner", which tokenizes the input files
33 * for localedef for processing by the higher level grammar processor.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
44 #include <sys/types.h>
46 #include "localedef.h"
58 //static FILE *input = stdin;
59 static const char *filename = "<stdin>";
60 static int instring = 0;
61 static int escaped = 0;
64 * Token space ... grows on demand.
66 static char *token = NULL;
69 static int hadtok = 0;
72 * Wide string space ... grows on demand.
74 static wchar_t *widestr = NULL;
75 static int wideidx = 0;
76 static int widesz = 0;
79 * The last keyword seen. This is useful to trigger the special lexer rules
80 * for "copy" and also collating symbols and elements.
83 static int category = T_END;
89 { T_COM_CHAR, "comment_char" },
90 { T_ESC_CHAR, "escape_char" },
93 { T_MESSAGES, "LC_MESSAGES" },
94 { T_YESSTR, "yesstr" },
95 { T_YESEXPR, "yesexpr" },
97 { T_NOEXPR, "noexpr" },
98 { T_MONETARY, "LC_MONETARY" },
99 { T_INT_CURR_SYMBOL, "int_curr_symbol" },
100 { T_CURRENCY_SYMBOL, "currency_symbol" },
101 { T_MON_DECIMAL_POINT, "mon_decimal_point" },
102 { T_MON_THOUSANDS_SEP, "mon_thousands_sep" },
103 { T_POSITIVE_SIGN, "positive_sign" },
104 { T_NEGATIVE_SIGN, "negative_sign" },
105 { T_MON_GROUPING, "mon_grouping" },
106 { T_INT_FRAC_DIGITS, "int_frac_digits" },
107 { T_FRAC_DIGITS, "frac_digits" },
108 { T_P_CS_PRECEDES, "p_cs_precedes" },
109 { T_P_SEP_BY_SPACE, "p_sep_by_space" },
110 { T_N_CS_PRECEDES, "n_cs_precedes" },
111 { T_N_SEP_BY_SPACE, "n_sep_by_space" },
112 { T_P_SIGN_POSN, "p_sign_posn" },
113 { T_N_SIGN_POSN, "n_sign_posn" },
114 { T_INT_P_CS_PRECEDES, "int_p_cs_precedes" },
115 { T_INT_N_CS_PRECEDES, "int_n_cs_precedes" },
116 { T_INT_P_SEP_BY_SPACE, "int_p_sep_by_space" },
117 { T_INT_N_SEP_BY_SPACE, "int_n_sep_by_space" },
118 { T_INT_P_SIGN_POSN, "int_p_sign_posn" },
119 { T_INT_N_SIGN_POSN, "int_n_sign_posn" },
120 { T_COLLATE, "LC_COLLATE" },
121 { T_COLLATING_SYMBOL, "collating-symbol" },
122 { T_COLLATING_ELEMENT, "collating-element" },
124 { T_ORDER_START, "order_start" },
125 { T_ORDER_END, "order_end" },
126 { T_FORWARD, "forward" },
127 { T_BACKWARD, "backward" },
128 { T_POSITION, "position" },
129 { T_IGNORE, "IGNORE" },
130 { T_UNDEFINED, "UNDEFINED" },
131 { T_NUMERIC, "LC_NUMERIC" },
132 { T_DECIMAL_POINT, "decimal_point" },
133 { T_THOUSANDS_SEP, "thousands_sep" },
134 { T_GROUPING, "grouping" },
135 { T_TIME, "LC_TIME" },
136 { T_ABDAY, "abday" },
138 { T_ABMON, "abmon" },
140 { T_D_T_FMT, "d_t_fmt" },
141 { T_D_FMT, "d_fmt" },
142 { T_T_FMT, "t_fmt" },
143 { T_AM_PM, "am_pm" },
144 { T_T_FMT_AMPM, "t_fmt_ampm" },
146 { T_ERA_D_FMT, "era_d_fmt" },
147 { T_ERA_T_FMT, "era_t_fmt" },
148 { T_ERA_D_T_FMT, "era_d_t_fmt" },
149 { T_ALT_DIGITS, "alt_digits" },
150 { T_CTYPE, "LC_CTYPE" },
151 { T_ISUPPER, "upper" },
152 { T_ISLOWER, "lower" },
153 { T_ISALPHA, "alpha" },
154 { T_ISDIGIT, "digit" },
155 { T_ISPUNCT, "punct" },
156 { T_ISXDIGIT, "xdigit" },
157 { T_ISSPACE, "space" },
158 { T_ISPRINT, "print" },
159 { T_ISGRAPH, "graph" },
160 { T_ISBLANK, "blank" },
161 { T_ISCNTRL, "cntrl" },
163 * These entries are local additions, and not specified by
164 * TOG. Note that they are not guaranteed to be accurate for
165 * all locales, and so applications should not depend on them.
167 { T_ISSPECIAL, "special" },
168 { T_ISENGLISH, "english" },
169 { T_ISPHONOGRAM, "phonogram" },
170 { T_ISIDEOGRAM, "ideogram" },
171 { T_ISNUMBER, "number" },
173 * We have to support this in the grammar, but it would be a
174 * syntax error to define a character as one of these without
175 * also defining it as an alpha or digit. We ignore it in our
178 { T_ISALNUM, "alnum" },
179 { T_TOUPPER, "toupper" },
180 { T_TOLOWER, "tolower" },
183 * These are keywords used in the charmap file. Note that
184 * Solaris originally used angle brackets to wrap some of them,
185 * but we removed that to simplify our parser. The first of these
186 * items are "global items."
188 { T_CHARMAP, "CHARMAP" },
189 { T_WIDTH, "WIDTH" },
195 * These special words are only used in a charmap file, enclosed in <>.
197 static struct token symwords[] = {
198 { T_COM_CHAR, "comment_char" },
199 { T_ESC_CHAR, "escape_char" },
200 { T_CODE_SET, "code_set_name" },
201 { T_MB_CUR_MAX, "mb_cur_max" },
202 { T_MB_CUR_MIN, "mb_cur_min" },
206 static int categories[] = {
219 reset_scanner(const char *fname)
222 filename = "<stdin>";
226 (void) fclose(input);
227 if ((input = fopen(fname, "r")) == NULL) {
246 (isdigit(x) ? (x - '0') : ((islower(x) ? (x - 'a') : (x - 'A')) + 10))
247 #define isodigit(x) ((x >= '0') && (x <= '7'))
271 if (ungetc(c, is_stdin ? stdin : input) < 0) {
272 yyerror("ungetc failed");
284 yyerror("malformed hex digit");
289 yyerror("malformed hex digit");
292 v = ((hex(c1) << 4) | hex(c2));
304 yyerror("malformed decimal digit");
310 yyerror("malformed decimal digit");
335 yyerror("malformed octal digit");
341 yyerror("malformed octal digit");
359 if ((tokidx + 1) >= toksz) {
361 if ((token = realloc(token, toksz)) == NULL) {
362 yyerror("out of memory");
369 token[tokidx++] = (char)c;
375 if ((wideidx + 1) >= widesz) {
377 widestr = realloc(widestr, (widesz * sizeof (wchar_t)));
378 if (widestr == NULL) {
379 yyerror("out of memory");
386 widestr[wideidx++] = c;
387 widestr[wideidx] = 0;
393 wchar_t *ws = widestr;
398 if ((ws = wcsdup(L"")) == NULL) {
399 yyerror("out of memory");
410 if ((c = scanc()) != esc_char) {
419 return (scan_dec_byte());
422 return (scan_hex_byte());
431 /* put the character back so we can get it */
433 return (scan_oct_byte());
467 static char mbs[MB_LEN_MAX + 1] = "";
472 if (mb_cur_max >= (int)sizeof (mbs)) {
473 yyerror("max multibyte character size too big");
478 if ((mbi == mb_cur_max) || ((c = get_byte()) == EOF)) {
480 * end of the byte sequence reached, but no
481 * valid wide decoding. fatal error.
484 yyerror("not a valid character encoding");
490 /* does it decode? */
491 if (to_wide(&wc, mbs) >= 0) {
497 if ((category != T_CHARMAP) && (category != T_WIDTH)) {
498 if (check_charmap(wc) < 0) {
499 yyerror("no symbolic name for character");
513 while ((c = scanc()) != EOF) {
518 add_tok(get_escaped(c));
525 if (c == '\n') { /* well that's strange! */
526 yyerror("unterminated symbolic name");
529 if (c == '>') { /* end of symbol */
532 * This restarts the token from the beginning
533 * the next time we scan a character. (This
534 * token is complete.)
538 yyerror("missing symbolic name");
544 * A few symbols are handled as keywords outside
545 * of the normal categories.
547 if (category == T_END) {
549 for (i = 0; symwords[i].name != 0; i++) {
550 if (strcmp(token, symwords[i].name) ==
552 last_kw = symwords[i].id;
558 * Contextual rule: Only literal characters are
559 * permitted in CHARMAP. Anywhere else the symbolic
562 if ((category != T_CHARMAP) &&
563 (lookup_charmap(token, &yylval.wc)) != -1) {
566 if ((yylval.collsym = lookup_collsym(token)) != NULL) {
569 if ((yylval.collelem = lookup_collelem(token)) !=
573 /* its an undefined symbol */
574 yylval.token = strdup(token);
583 yyerror("unterminated symbolic name");
604 * this one is special, because we don't want it to alter the
607 if (strcmp(token, "...") == 0) {
611 /* search for reserved words first */
612 for (i = 0; keywords[i].name; i++) {
614 if (strcmp(keywords[i].name, token) != 0) {
618 last_kw = keywords[i].id;
620 /* clear the top level category if we're done with it */
621 if (last_kw == T_END) {
625 /* set the top level category if we're changing */
626 for (j = 0; categories[j]; j++) {
627 if (categories[j] != last_kw)
632 return (keywords[i].id);
635 /* maybe its a numeric constant? */
636 if (isdigit(*token) || (*token == '-' && isdigit(token[1]))) {
638 yylval.num = strtol(token, &eptr, 10);
640 yyerror("malformed number");
645 * A single lone character is treated as a character literal.
646 * To avoid duplication of effort, we stick in the charmap.
649 yylval.wc = token[0];
653 /* anything else is treated as a symbolic name */
654 yylval.token = strdup(token);
665 while ((c = scanc()) != '\n') {
667 /* end of file without newline! */
668 errf("missing newline");
680 while ((c = scanc()) != EOF) {
682 /* special handling for quoted string */
687 /* if newline, just eat and forget it */
691 if (strchr("xXd01234567", c)) {
696 yylval.wc = get_escaped(c);
705 return (get_symbol());
707 /* oops! should generate syntax error */
718 /* escaped characters first */
722 /* eat the newline */
727 /* an escape mid-token is nonsense */
731 /* numeric escapes are treated as wide characters */
732 if (strchr("xXd01234567", c)) {
738 add_tok(get_escaped(c));
742 /* if it is the escape charter itself note it */
748 /* remove from the comment char to end of line */
751 if ((c = scanc()) == EOF) {
752 /* end of file without newline! */
759 * If there were no tokens on this line,
760 * then just pretend it didn't exist at all.
768 if (strchr(" \t\n;()<>,\"", c) && (tokidx != 0)) {
770 * These are all token delimiters. If there
771 * is a token already in progress, we need to
775 return (consume_token());
782 * If the line was completely devoid of tokens,
783 * then just ignore it.
787 /* we're starting a new line, reset the token state */
808 return (get_symbol());
811 /* whitespace, just ignore it */
827 yyerror(const char *msg)
829 (void) fprintf(stderr, "%s: %d: error: %s\n",
830 filename, lineno, msg);
835 errf(const char *fmt, ...)
841 (void) vasprintf(&msg, fmt, va);
844 (void) fprintf(stderr, "%s: %d: error: %s\n",
845 filename, lineno, msg);
851 warn(const char *fmt, ...)
857 (void) vasprintf(&msg, fmt, va);
860 (void) fprintf(stderr, "%s: %d: warning: %s\n",
861 filename, lineno, msg);