1 /*-
2 * Copyright 2018 Nexenta Systems, Inc.
3 * Copyright 2015 John Marino <draco@marino.st>
4 *
5 * This source code is derived from the illumos localedef command, and
6 * provided under BSD-style license terms by Nexenta Systems, Inc.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
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.
17 *
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.
29 */
30
31 /*
32 * POSIX localedef.
33 */
34
35 /* Common header files. */
36
37 #include <sys/types.h>
38 #include <stdarg.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <wchar.h>
42
43 extern int com_char;
44 extern int esc_char;
45 extern int mb_cur_max;
46 extern int mb_cur_min;
47 extern int last_kw;
48 extern int verbose;
49 #if YYDEBUG
50 extern int yydebug;
51 #endif
52 extern int lineno;
53 extern int undefok; /* mostly ignore undefined symbols */
54 extern int warnok;
55 extern int warnings;
56
57 extern char *version;
58
59 int yylex(void);
60 void yyerror(const char *);
61 _Noreturn void errf(const char *, ...) __printflike(1, 2);
62 void warn(const char *, ...) __printflike(1, 2);
63
64 int putl_category(const char *, FILE *);
65 int wr_category(void *, size_t, FILE *);
66 FILE *open_category(void);
67 void close_category(FILE *);
68 void copy_category(char *);
69 const char *category_name(void);
70
71 int get_category(void);
72 int get_symbol(void);
73 int get_escaped(int);
74 int get_wide(void);
75 void reset_scanner(const char *);
76 void scan_to_eol(void);
77 void add_wcs(wchar_t);
78 void add_tok(int);
79 wchar_t *get_wcs(void);
80
81 uint32_t htote(uint32_t);
82
83 /* charmap.c - CHARMAP handling */
84 void init_charmap(void);
85 void add_charmap(const char *, int);
86 void add_charmap_undefined(char *);
87 void add_charmap_posix(void);
88 void add_charmap_range(char *, char *, int);
89 void add_charmap_char(const char *name, int val);
90 int lookup_charmap(const char *, wchar_t *);
91 int check_charmap_undefined(char *);
92 int check_charmap(wchar_t);
93
94 /* collate.o - LC_COLLATE handling */
95 typedef struct collelem collelem_t;
96 typedef struct collsym collsym_t;
97 void init_collate(void);
98 void define_collsym(char *);
99 void define_collelem(char *, wchar_t *);
100 void add_order_directive(void);
101 void add_order_bit(int);
102 void dump_collate(void);
103 collsym_t *lookup_collsym(char *);
104 collelem_t *lookup_collelem(char *);
105 void start_order_collelem(collelem_t *);
106 void start_order_undefined(void);
107 void start_order_symbol(char *);
108 void start_order_char(wchar_t);
109 void start_order_ellipsis(void);
110 void end_order_collsym(collsym_t *);
111 void end_order(void);
112 void add_weight(int32_t, int);
113 void add_weights(int32_t *);
114 void add_weight_num(int);
115 void add_order_collelem(collelem_t *);
116 void add_order_collsym(collsym_t *);
117 void add_order_char(wchar_t);
118 void add_order_ignore(void);
119 void add_order_ellipsis(void);
120 void add_order_symbol(char *);
121 void add_order_subst(void);
122 void add_subst_char(wchar_t);
123 void add_subst_collsym(collsym_t *);
124 void add_subst_collelem(collelem_t *);
125 void add_subst_symbol(char *);
126 int32_t get_weight(int32_t, int);
127 wchar_t * wsncpy(wchar_t *, const wchar_t *, size_t);
128
129
130 /* ctype.c - LC_CTYPE handling */
131 void init_ctype(void);
132 void add_ctype(int);
133 void add_ctype_range(wchar_t);
134 void add_width(int, int);
135 void add_width_range(int, int, int);
136 void add_caseconv(int, int);
137 void dump_ctype(void);
138
139 /* messages.c - LC_MESSAGES handling */
140 void init_messages(void);
141 void add_message(wchar_t *);
142 void dump_messages(void);
143
144 /* monetary.c - LC_MONETARY handling */
145 void init_monetary(void);
146 void add_monetary_str(wchar_t *);
147 void add_monetary_num(int);
148 void reset_monetary_group(void);
149 void add_monetary_group(int);
150 void dump_monetary(void);
151
152 /* numeric.c - LC_NUMERIC handling */
153 void init_numeric(void);
154 void add_numeric_str(wchar_t *);
155 void reset_numeric_group(void);
156 void add_numeric_group(int);
157 void dump_numeric(void);
158
159 /* time.c - LC_TIME handling */
160 void init_time(void);
161 void add_time_str(wchar_t *);
162 void reset_time_list(void);
163 void add_time_list(wchar_t *);
164 void check_time_list(void);
165 void dump_time(void);
166
167 /* wide.c - Wide character handling. */
168 int to_wide(wchar_t *, const char *);
169 int to_mbs(char *, wchar_t);
170 int to_mb(char *, wchar_t);
171 char *to_mb_string(const wchar_t *);
172 void set_wide_encoding(const char *);
173 void werr(const char *, ...);
174 const char *get_wide_encoding(void);
175 int max_wide(void);
176
177 /*
178 * A helper function to compare wide characters when sorting. Forcibly cast to
179 * an unsigned type to help ensure that output is consistent no matter the
180 * signedness of wchar_t.
181 */
182 static inline int
wchar_cmp(const wchar_t a,const wchar_t b)183 wchar_cmp(const wchar_t a, const wchar_t b)
184 {
185 return ((uint32_t)a < (uint32_t)b ? -1 :
186 ((uint32_t)a > (uint32_t)b ? 1 : 0));
187 }
188 _Static_assert(sizeof(wchar_t) == sizeof(uint32_t),
189 "wchar_t must be 32 bits wide");
190
191 //#define _(x) gettext(x)
192 #define INTERR fprintf(stderr,"internal fault (%s:%d)\n", __FILE__, __LINE__)
193