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.
42 #include <sys/types.h>
44 #include "localedef.h"
53 static int is_stdin
= 1;
56 //static FILE *input = stdin;
57 static const char *filename
= "<stdin>";
58 static int instring
= 0;
59 static int escaped
= 0;
62 * Token space ... grows on demand.
64 static char *token
= NULL
;
67 static int hadtok
= 0;
70 * Wide string space ... grows on demand.
72 static wchar_t *widestr
= NULL
;
73 static int wideidx
= 0;
74 static int widesz
= 0;
77 * The last keyword seen. This is useful to trigger the special lexer rules
78 * for "copy" and also collating symbols and elements.
81 static int category
= T_END
;
87 { T_COM_CHAR
, "comment_char" },
88 { T_ESC_CHAR
, "escape_char" },
91 { T_MESSAGES
, "LC_MESSAGES" },
92 { T_YESSTR
, "yesstr" },
93 { T_YESEXPR
, "yesexpr" },
95 { T_NOEXPR
, "noexpr" },
96 { T_MONETARY
, "LC_MONETARY" },
97 { T_INT_CURR_SYMBOL
, "int_curr_symbol" },
98 { T_CURRENCY_SYMBOL
, "currency_symbol" },
99 { T_MON_DECIMAL_POINT
, "mon_decimal_point" },
100 { T_MON_THOUSANDS_SEP
, "mon_thousands_sep" },
101 { T_POSITIVE_SIGN
, "positive_sign" },
102 { T_NEGATIVE_SIGN
, "negative_sign" },
103 { T_MON_GROUPING
, "mon_grouping" },
104 { T_INT_FRAC_DIGITS
, "int_frac_digits" },
105 { T_FRAC_DIGITS
, "frac_digits" },
106 { T_P_CS_PRECEDES
, "p_cs_precedes" },
107 { T_P_SEP_BY_SPACE
, "p_sep_by_space" },
108 { T_N_CS_PRECEDES
, "n_cs_precedes" },
109 { T_N_SEP_BY_SPACE
, "n_sep_by_space" },
110 { T_P_SIGN_POSN
, "p_sign_posn" },
111 { T_N_SIGN_POSN
, "n_sign_posn" },
112 { T_INT_P_CS_PRECEDES
, "int_p_cs_precedes" },
113 { T_INT_N_CS_PRECEDES
, "int_n_cs_precedes" },
114 { T_INT_P_SEP_BY_SPACE
, "int_p_sep_by_space" },
115 { T_INT_N_SEP_BY_SPACE
, "int_n_sep_by_space" },
116 { T_INT_P_SIGN_POSN
, "int_p_sign_posn" },
117 { T_INT_N_SIGN_POSN
, "int_n_sign_posn" },
118 { T_COLLATE
, "LC_COLLATE" },
119 { T_COLLATING_SYMBOL
, "collating-symbol" },
120 { T_COLLATING_ELEMENT
, "collating-element" },
122 { T_ORDER_START
, "order_start" },
123 { T_ORDER_END
, "order_end" },
124 { T_FORWARD
, "forward" },
125 { T_BACKWARD
, "backward" },
126 { T_POSITION
, "position" },
127 { T_IGNORE
, "IGNORE" },
128 { T_UNDEFINED
, "UNDEFINED" },
129 { T_NUMERIC
, "LC_NUMERIC" },
130 { T_DECIMAL_POINT
, "decimal_point" },
131 { T_THOUSANDS_SEP
, "thousands_sep" },
132 { T_GROUPING
, "grouping" },
133 { T_TIME
, "LC_TIME" },
134 { T_ABDAY
, "abday" },
136 { T_ABMON
, "abmon" },
138 { T_D_T_FMT
, "d_t_fmt" },
139 { T_D_FMT
, "d_fmt" },
140 { T_T_FMT
, "t_fmt" },
141 { T_AM_PM
, "am_pm" },
142 { T_T_FMT_AMPM
, "t_fmt_ampm" },
144 { T_ERA_D_FMT
, "era_d_fmt" },
145 { T_ERA_T_FMT
, "era_t_fmt" },
146 { T_ERA_D_T_FMT
, "era_d_t_fmt" },
147 { T_ALT_DIGITS
, "alt_digits" },
148 { T_CTYPE
, "LC_CTYPE" },
149 { T_ISUPPER
, "upper" },
150 { T_ISLOWER
, "lower" },
151 { T_ISALPHA
, "alpha" },
152 { T_ISDIGIT
, "digit" },
153 { T_ISPUNCT
, "punct" },
154 { T_ISXDIGIT
, "xdigit" },
155 { T_ISSPACE
, "space" },
156 { T_ISPRINT
, "print" },
157 { T_ISGRAPH
, "graph" },
158 { T_ISBLANK
, "blank" },
159 { T_ISCNTRL
, "cntrl" },
161 * These entries are local additions, and not specified by
162 * TOG. Note that they are not guaranteed to be accurate for
163 * all locales, and so applications should not depend on them.
165 { T_ISSPECIAL
, "special" },
166 { T_ISENGLISH
, "english" },
167 { T_ISPHONOGRAM
, "phonogram" },
168 { T_ISIDEOGRAM
, "ideogram" },
169 { T_ISNUMBER
, "number" },
171 * We have to support this in the grammar, but it would be a
172 * syntax error to define a character as one of these without
173 * also defining it as an alpha or digit. We ignore it in our
176 { T_ISALNUM
, "alnum" },
177 { T_TOUPPER
, "toupper" },
178 { T_TOLOWER
, "tolower" },
181 * These are keywords used in the charmap file. Note that
182 * Solaris orginally used angle brackets to wrap some of them,
183 * but we removed that to simplify our parser. The first of these
184 * items are "global items."
186 { T_CHARMAP
, "CHARMAP" },
187 { T_WIDTH
, "WIDTH" },
193 * These special words are only used in a charmap file, enclosed in <>.
195 static struct token symwords
[] = {
196 { T_COM_CHAR
, "comment_char" },
197 { T_ESC_CHAR
, "escape_char" },
198 { T_CODE_SET
, "code_set_name" },
199 { T_MB_CUR_MAX
, "mb_cur_max" },
200 { T_MB_CUR_MIN
, "mb_cur_min" },
204 static int categories
[] = {
217 reset_scanner(const char *fname
)
220 filename
= "<stdin>";
224 (void) fclose(input
);
225 if ((input
= fopen(fname
, "r")) == NULL
) {
244 (isdigit(x) ? (x - '0') : ((islower(x) ? (x - 'a') : (x - 'A')) + 10))
245 #define isodigit(x) ((x >= '0') && (x <= '7'))
269 if (ungetc(c
, is_stdin
? stdin
: input
) < 0) {
270 yyerror("ungetc failed");
282 yyerror("malformed hex digit");
287 yyerror("malformed hex digit");
290 v
= ((hex(c1
) << 4) | hex(c2
));
302 yyerror("malformed decimal digit");
308 yyerror("malformed decimal digit");
333 yyerror("malformed octal digit");
339 yyerror("malformed octal digit");
357 if ((tokidx
+ 1) >= toksz
) {
359 if ((token
= realloc(token
, toksz
)) == NULL
) {
360 yyerror("out of memory");
367 token
[tokidx
++] = (char)c
;
373 if ((wideidx
+ 1) >= widesz
) {
375 widestr
= realloc(widestr
, (widesz
* sizeof (wchar_t)));
376 if (widestr
== NULL
) {
377 yyerror("out of memory");
384 widestr
[wideidx
++] = c
;
385 widestr
[wideidx
] = 0;
391 wchar_t *ws
= widestr
;
396 if ((ws
= wcsdup(L
"")) == NULL
) {
397 yyerror("out of memory");
408 if ((c
= scanc()) != esc_char
) {
417 return (scan_dec_byte());
420 return (scan_hex_byte());
429 /* put the character back so we can get it */
431 return (scan_oct_byte());
465 static char mbs
[MB_LEN_MAX
+ 1] = "";
470 if (mb_cur_max
>= (int)sizeof (mbs
)) {
471 yyerror("max multibyte character size too big");
476 if ((mbi
== mb_cur_max
) || ((c
= get_byte()) == EOF
)) {
478 * end of the byte sequence reached, but no
479 * valid wide decoding. fatal error.
482 yyerror("not a valid character encoding");
488 /* does it decode? */
489 if (to_wide(&wc
, mbs
) >= 0) {
495 if ((category
!= T_CHARMAP
) && (category
!= T_WIDTH
)) {
496 if (check_charmap(wc
) < 0) {
497 yyerror("no symbolic name for character");
511 while ((c
= scanc()) != EOF
) {
516 add_tok(get_escaped(c
));
523 if (c
== '\n') { /* well that's strange! */
524 yyerror("unterminated symbolic name");
527 if (c
== '>') { /* end of symbol */
530 * This restarts the token from the beginning
531 * the next time we scan a character. (This
532 * token is complete.)
536 yyerror("missing symbolic name");
542 * A few symbols are handled as keywords outside
543 * of the normal categories.
545 if (category
== T_END
) {
547 for (i
= 0; symwords
[i
].name
!= NULL
; i
++) {
548 if (strcmp(token
, symwords
[i
].name
) ==
550 last_kw
= symwords
[i
].id
;
556 * Contextual rule: Only literal characters are
557 * permitted in CHARMAP. Anywhere else the symbolic
560 if ((category
!= T_CHARMAP
) &&
561 (lookup_charmap(token
, &yylval
.wc
)) != -1) {
564 if ((yylval
.collsym
= lookup_collsym(token
)) != NULL
) {
567 if ((yylval
.collelem
= lookup_collelem(token
)) !=
571 /* its an undefined symbol */
572 yylval
.token
= strdup(token
);
581 yyerror("unterminated symbolic name");
602 * this one is special, because we don't want it to alter the
605 if (strcmp(token
, "...") == 0) {
609 /* search for reserved words first */
610 for (i
= 0; keywords
[i
].name
; i
++) {
612 if (strcmp(keywords
[i
].name
, token
) != 0) {
616 last_kw
= keywords
[i
].id
;
618 /* clear the top level category if we're done with it */
619 if (last_kw
== T_END
) {
623 /* set the top level category if we're changing */
624 for (j
= 0; categories
[j
]; j
++) {
625 if (categories
[j
] != last_kw
)
630 return (keywords
[i
].id
);
633 /* maybe its a numeric constant? */
634 if (isdigit(*token
) || (*token
== '-' && isdigit(token
[1]))) {
636 yylval
.num
= strtol(token
, &eptr
, 10);
638 yyerror("malformed number");
643 * A single lone character is treated as a character literal.
644 * To avoid duplication of effort, we stick in the charmap.
647 yylval
.wc
= token
[0];
651 /* anything else is treated as a symbolic name */
652 yylval
.token
= strdup(token
);
663 while ((c
= scanc()) != '\n') {
665 /* end of file without newline! */
666 errf("missing newline");
678 while ((c
= scanc()) != EOF
) {
680 /* special handling for quoted string */
685 /* if newline, just eat and forget it */
689 if (strchr("xXd01234567", c
)) {
694 yylval
.wc
= get_escaped(c
);
703 return (get_symbol());
705 /* oops! should generate syntax error */
716 /* escaped characters first */
720 /* eat the newline */
725 /* an escape mid-token is nonsense */
729 /* numeric escapes are treated as wide characters */
730 if (strchr("xXd01234567", c
)) {
736 add_tok(get_escaped(c
));
740 /* if it is the escape charter itself note it */
746 /* remove from the comment char to end of line */
749 if ((c
= scanc()) == EOF
) {
750 /* end of file without newline! */
757 * If there were no tokens on this line,
758 * then just pretend it didn't exist at all.
766 if (strchr(" \t\n;()<>,\"", c
) && (tokidx
!= 0)) {
768 * These are all token delimiters. If there
769 * is a token already in progress, we need to
773 return (consume_token());
780 * If the line was completely devoid of tokens,
781 * then just ignore it.
785 /* we're starting a new line, reset the token state */
806 return (get_symbol());
809 /* whitespace, just ignore it */
825 yyerror(const char *msg
)
827 (void) fprintf(stderr
, "%s: %d: error: %s\n",
828 filename
, lineno
, msg
);
832 __printflike(1, 2) void
833 errf(const char *fmt
, ...)
839 (void) vasprintf(&msg
, fmt
, va
);
842 (void) fprintf(stderr
, "%s: %d: error: %s\n",
843 filename
, lineno
, msg
);
848 __printflike(1, 2) void
849 warn(const char *fmt
, ...)
855 (void) vasprintf(&msg
, fmt
, va
);
858 (void) fprintf(stderr
, "%s: %d: warning: %s\n",
859 filename
, lineno
, msg
);