xref: /freebsd/lib/libc/locale/collate.c (revision 3e0f6b97b257a96f7275e4442204263e44b16686)
1 /*-
2  * Copyright (c) 1995 Alex Tatmanjants <alex@elvisti.kiev.ua>
3  *		at Electronni Visti IA, Kiev, Ukraine.
4  *			All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29 
30 #include <rune.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <unistd.h>
36 #include <sysexits.h>
37 #include "collate.h"
38 #include "setlocale.h"
39 
40 int __collate_load_error = 1;
41 char __collate_version[STR_LEN];
42 u_char __collate_substitute_table[UCHAR_MAX + 1][STR_LEN];
43 struct __collate_st_char_pri __collate_char_pri_table[UCHAR_MAX + 1];
44 struct __collate_st_chain_pri __collate_chain_pri_table[TABLE_SIZE];
45 
46 #define FREAD(a, b, c, d) \
47 	do { \
48 		if(fread(a, b, c, d) != c) { \
49 			fclose(d); \
50 			return -1; \
51 		} \
52 	} while(0)
53 
54 void __collate_err(int ex, const char *f) __dead2;
55 
56 int
57 __collate_load_tables(encoding)
58 	char *encoding;
59 {
60 	char buf[PATH_MAX];
61 	FILE *fp;
62 	int save_load_error;
63 
64 	save_load_error = __collate_load_error;
65 	__collate_load_error = 1;
66 	if (!encoding) {
67 		__collate_load_error = save_load_error;
68 		return -1;
69 	}
70 	if (!strcmp(encoding, "C") || !strcmp(encoding, "POSIX"))
71 		return 0;
72 	if (!_PathLocale) {
73 		__collate_load_error = save_load_error;
74 		return -1;
75 	}
76 	/* Range checking not needed, encoding has fixed size */
77 	(void) strcpy(buf, _PathLocale);
78 	(void) strcat(buf, "/");
79 	(void) strcat(buf, encoding);
80 	(void) strcat(buf, "/LC_COLLATE");
81 	if ((fp = fopen(buf, "r")) == NULL) {
82 		__collate_load_error = save_load_error;
83 		return -1;
84 	}
85 	FREAD(__collate_version, sizeof(__collate_version), 1, fp);
86 	if (strcmp(__collate_version, COLLATE_VERSION) != 0) {
87 		fclose(fp);
88 		return -1;
89 	}
90 	FREAD(__collate_substitute_table, sizeof(__collate_substitute_table),
91 	      1, fp);
92 	FREAD(__collate_char_pri_table, sizeof(__collate_char_pri_table), 1,
93 	      fp);
94 	FREAD(__collate_chain_pri_table, sizeof(__collate_chain_pri_table), 1,
95 	      fp);
96 	fclose(fp);
97 	__collate_load_error = 0;
98 	return 0;
99 }
100 
101 u_char *
102 __collate_substitute(s)
103 	const u_char *s;
104 {
105 	int dest_len = 0, len = 0;
106 	int delta = strlen(s);
107 	u_char *dest_str = NULL;
108 
109 	if(s == NULL || *s == '\0')
110 		return __collate_strdup("");
111 	while(*s) {
112 		len += strlen(__collate_substitute_table[*s]);
113 		while(dest_len <= len) {
114 			if(!dest_str)
115 				dest_str = calloc(dest_len = delta, 1);
116 			else
117 				dest_str = realloc(dest_str, dest_len += delta);
118 			if(dest_str == NULL)
119 				__collate_err(EX_OSERR, __FUNCTION__);
120 		}
121 		strcat(dest_str, __collate_substitute_table[*s++]);
122 	}
123 	return dest_str;
124 }
125 
126 void
127 __collate_lookup(t, len, prim, sec)
128 	u_char *t;
129 	int *len, *prim, *sec;
130 {
131 	struct __collate_st_chain_pri *p2;
132 
133 	*len = 1;
134 	*prim = *sec = 0;
135 	for(p2 = __collate_chain_pri_table; p2->str[0]; p2++) {
136 		if(strncmp(t, p2->str, strlen(p2->str)) == 0) {
137 			*len = strlen(p2->str);
138 			*prim = p2->prim;
139 			*sec = p2->sec;
140 			return;
141 		}
142 	}
143 	*prim = __collate_char_pri_table[*t].prim;
144 	*sec = __collate_char_pri_table[*t].sec;
145 }
146 
147 u_char *
148 __collate_strdup(s)
149 	u_char *s;
150 {
151 	u_char *t = strdup(s);
152 
153 	if (t == NULL)
154 		__collate_err(EX_OSERR, __FUNCTION__);
155 	return t;
156 }
157 
158 void
159 __collate_err(int ex, const char *f)
160 {
161 	extern char *__progname;                /* Program name, from crt0. */
162 	const char *s;
163 	int serrno = errno;
164 
165 	s = __progname;
166 	write(STDERR_FILENO, s, strlen(s));
167 	write(STDERR_FILENO, ": ", 2);
168 	s = f;
169 	write(STDERR_FILENO, s, strlen(s));
170 	write(STDERR_FILENO, ": ", 2);
171 	s = strerror(serrno);
172 	write(STDERR_FILENO, s, strlen(s));
173 	write(STDERR_FILENO, "\n", 1);
174 	exit(ex);
175 }
176 
177 #ifdef COLLATE_DEBUG
178 void
179 __collate_print_tables()
180 {
181 	int i;
182 	struct __collate_st_chain_pri *p2;
183 
184 	printf("Substitute table:\n");
185 	for (i = 0; i < UCHAR_MAX + 1; i++)
186 	    if (i != *__collate_substitute_table[i])
187 		printf("\t'%c' --> \"%s\"\n", i,
188 		       __collate_substitute_table[i]);
189 	printf("Chain priority table:\n");
190 	for (p2 = __collate_chain_pri_table; p2->str[0]; p2++)
191 		printf("\t\"%s\" : %d %d\n\n", p2->str, p2->prim, p2->sec);
192 	printf("Char priority table:\n");
193 	for (i = 0; i < UCHAR_MAX + 1; i++)
194 		printf("\t'%c' : %d %d\n", i, __collate_char_pri_table[i].prim,
195 		       __collate_char_pri_table[i].sec);
196 }
197 #endif
198