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 * $Id: collate.c,v 1.5 1995/10/23 20:08:24 ache Exp $ 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 39 char *_PathLocale; 40 int __collate_load_error = 1; 41 u_char __collate_charmap_table[UCHAR_MAX + 1][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_name_pri __collate_name_pri_table[TABLE_SIZE]; 45 struct __collate_st_chain_pri __collate_chain_pri_table[TABLE_SIZE]; 46 47 #define FREAD(a, b, c, d) \ 48 do { \ 49 if(fread(a, b, c, d) != c) { \ 50 fclose(d); \ 51 return -1; \ 52 } \ 53 } while(0) 54 55 void __collate_err(int ex, const char *f) __dead2; 56 57 int 58 __collate_load_tables(encoding) 59 char *encoding; 60 { 61 char buf[PATH_MAX]; 62 FILE *fp; 63 int save_load_error; 64 65 save_load_error = __collate_load_error; 66 __collate_load_error = 1; 67 if (!encoding) { 68 __collate_load_error = save_load_error; 69 return -1; 70 } 71 if (!*encoding || !strcmp(encoding, "C") || !strcmp(encoding, "POSIX")) 72 return 0; 73 if (!_PathLocale && !(_PathLocale = getenv("PATH_LOCALE"))) 74 _PathLocale = _PATH_LOCALE; 75 strcpy(buf, _PathLocale); 76 strcat(buf, "/"); 77 strcat(buf, encoding); 78 strcat(buf, "/LC_COLLATE"); 79 if ((fp = fopen(buf, "r")) == NULL) { 80 __collate_load_error = save_load_error; 81 return -1; 82 } 83 FREAD(__collate_charmap_table, sizeof(__collate_charmap_table), 1, fp); 84 FREAD(__collate_substitute_table, sizeof(__collate_substitute_table), 85 1, fp); 86 FREAD(__collate_char_pri_table, sizeof(__collate_char_pri_table), 1, 87 fp); 88 FREAD(__collate_chain_pri_table, sizeof(__collate_chain_pri_table), 1, 89 fp); 90 FREAD(__collate_name_pri_table, sizeof(__collate_name_pri_table), 1, 91 fp); 92 fclose(fp); 93 __collate_load_error = 0; 94 return 0; 95 } 96 97 u_char * 98 __collate_substitute(s) 99 const u_char *s; 100 { 101 int dest_len = 0, len = 0; 102 int delta = strlen(s); 103 u_char *dest_str = NULL; 104 105 if(s == NULL || *s == '\0') 106 return __collate_strdup(""); 107 while(*s) { 108 len += strlen(__collate_substitute_table[*s]); 109 while(dest_len <= len) { 110 if(!dest_str) 111 dest_str = calloc(dest_len = delta, 1); 112 else 113 dest_str = realloc(dest_str, dest_len += delta); 114 if(dest_str == NULL) 115 __collate_err(EX_OSERR, __FUNCTION__); 116 } 117 strcat(dest_str, __collate_substitute_table[*s++]); 118 } 119 return dest_str; 120 } 121 122 void 123 __collate_lookup(t, len, prim, sec) 124 u_char *t; 125 int *len, *prim, *sec; 126 { 127 struct __collate_st_name_pri *p; 128 struct __collate_st_chain_pri *p2; 129 130 *len = 1; 131 *prim = *sec = 0; 132 if(__collate_charmap_table[*t][0]) { 133 for(p = __collate_name_pri_table; p->str[0]; p++) { 134 if(strncmp(__collate_charmap_table[*t], p->str, strlen(p->str)) 135 == 0) { 136 *prim = p->prim; 137 *sec = p->sec; 138 return; 139 } 140 } 141 return; 142 } 143 for(p2 = __collate_chain_pri_table; p2->str[0]; p2++) { 144 if(strncmp(t, p2->str, strlen(p2->str)) == 0) { 145 *len = strlen(p2->str); 146 *prim = p2->prim; 147 *sec = p2->sec; 148 return; 149 } 150 } 151 *prim = __collate_char_pri_table[*t].prim; 152 *sec = __collate_char_pri_table[*t].sec; 153 } 154 155 u_char * 156 __collate_strdup(s) 157 u_char *s; 158 { 159 u_char *t = strdup(s); 160 161 if (t == NULL) 162 __collate_err(EX_OSERR, __FUNCTION__); 163 return t; 164 } 165 166 void 167 __collate_err(int ex, const char *f) 168 { 169 extern char *__progname; /* Program name, from crt0. */ 170 const char *s; 171 int serrno = errno; 172 173 s = __progname; 174 write(STDERR_FILENO, s, strlen(s)); 175 write(STDERR_FILENO, ": ", 2); 176 s = f; 177 write(STDERR_FILENO, s, strlen(s)); 178 write(STDERR_FILENO, ": ", 2); 179 s = strerror(serrno); 180 write(STDERR_FILENO, s, strlen(s)); 181 write(STDERR_FILENO, "\n", 1); 182 exit(ex); 183 } 184 185 #ifdef COLLATE_DEBUG 186 void 187 __collate_print_tables() 188 { 189 int i; 190 struct __collate_st_name_pri *p; 191 struct __collate_st_chain_pri *p2; 192 193 printf("Substitute table:\n"); 194 for (i = 0; i < UCHAR_MAX + 1; i++) 195 printf("\t'%c' --> \"%s\"\n", i, 196 __collate_substitute_table[i]); 197 printf("Charmap table:\n"); 198 for (i = 0; i < UCHAR_MAX + 1; i++) { 199 if (__collate_charmap_table[i][0]) 200 printf("\t\\x%02x --> \"%s\"\n", i, 201 __collate_charmap_table[i]); 202 } 203 printf("Name priority table:\n"); 204 for (p = __collate_name_pri_table; p->str[0]; p++) 205 printf("\t\"%s\" : %d %d\n\n", p->str, p->prim, p->sec); 206 printf("Chain priority table:\n"); 207 for (p2 = __collate_chain_pri_table; p2->str[0]; p2++) 208 printf("\t\"%s\" : %d %d\n\n", p2->str, p2->prim, p2->sec); 209 printf("Char priority table:\n"); 210 for (i = 0; i < UCHAR_MAX + 1; i++) 211 printf("\t'%c' : %d %d\n", i, __collate_char_pri_table[i].prim, 212 __collate_char_pri_table[i].sec); 213 } 214 #endif 215