1995-02-16 04:24:39 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1995 Alex Tatmanjants <alex@elvisti.kiev.ua>
|
|
|
|
* at Electronni Visti IA, Kiev, Ukraine.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1997-02-22 15:12:41 +00:00
|
|
|
* $Id$
|
1995-02-16 04:24:39 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rune.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1995-02-17 16:36:12 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
1995-02-16 04:24:39 +00:00
|
|
|
#include <sysexits.h>
|
|
|
|
#include "collate.h"
|
1997-02-06 09:11:06 +00:00
|
|
|
#include "setlocale.h"
|
1995-02-16 04:24:39 +00:00
|
|
|
|
|
|
|
int __collate_load_error = 1;
|
1996-10-15 21:53:23 +00:00
|
|
|
char __collate_version[STR_LEN];
|
1995-02-16 04:24:39 +00:00
|
|
|
u_char __collate_substitute_table[UCHAR_MAX + 1][STR_LEN];
|
|
|
|
struct __collate_st_char_pri __collate_char_pri_table[UCHAR_MAX + 1];
|
|
|
|
struct __collate_st_chain_pri __collate_chain_pri_table[TABLE_SIZE];
|
|
|
|
|
|
|
|
#define FREAD(a, b, c, d) \
|
|
|
|
do { \
|
|
|
|
if(fread(a, b, c, d) != c) { \
|
|
|
|
fclose(d); \
|
|
|
|
return -1; \
|
|
|
|
} \
|
|
|
|
} while(0)
|
1995-05-30 05:51:47 +00:00
|
|
|
|
1996-09-14 03:00:32 +00:00
|
|
|
void __collate_err(int ex, const char *f) __dead2;
|
1995-02-17 16:36:12 +00:00
|
|
|
|
|
|
|
int
|
1995-02-16 04:24:39 +00:00
|
|
|
__collate_load_tables(encoding)
|
|
|
|
char *encoding;
|
|
|
|
{
|
|
|
|
char buf[PATH_MAX];
|
|
|
|
FILE *fp;
|
1995-10-23 20:08:24 +00:00
|
|
|
int save_load_error;
|
1995-02-16 04:24:39 +00:00
|
|
|
|
1995-10-23 20:08:24 +00:00
|
|
|
save_load_error = __collate_load_error;
|
1995-02-16 04:24:39 +00:00
|
|
|
__collate_load_error = 1;
|
1995-10-23 20:08:24 +00:00
|
|
|
if (!encoding) {
|
|
|
|
__collate_load_error = save_load_error;
|
1995-02-16 04:24:39 +00:00
|
|
|
return -1;
|
1995-10-23 20:08:24 +00:00
|
|
|
}
|
1996-11-26 02:49:53 +00:00
|
|
|
if (!strcmp(encoding, "C") || !strcmp(encoding, "POSIX"))
|
1995-10-23 20:08:24 +00:00
|
|
|
return 0;
|
1996-11-26 02:49:53 +00:00
|
|
|
if (!_PathLocale) {
|
|
|
|
__collate_load_error = save_load_error;
|
|
|
|
return -1;
|
|
|
|
}
|
1997-02-05 19:17:10 +00:00
|
|
|
/* Range checking not needed, encoding has fixed size */
|
1996-12-28 05:04:24 +00:00
|
|
|
(void) strcpy(buf, _PathLocale);
|
|
|
|
(void) strcat(buf, "/");
|
|
|
|
(void) strcat(buf, encoding);
|
|
|
|
(void) strcat(buf, "/LC_COLLATE");
|
1995-10-23 20:08:24 +00:00
|
|
|
if ((fp = fopen(buf, "r")) == NULL) {
|
|
|
|
__collate_load_error = save_load_error;
|
1995-02-16 04:24:39 +00:00
|
|
|
return -1;
|
1995-10-23 20:08:24 +00:00
|
|
|
}
|
1996-10-15 21:53:23 +00:00
|
|
|
FREAD(__collate_version, sizeof(__collate_version), 1, fp);
|
|
|
|
if (strcmp(__collate_version, COLLATE_VERSION) != 0) {
|
|
|
|
fclose(fp);
|
|
|
|
return -1;
|
|
|
|
}
|
1995-02-16 04:24:39 +00:00
|
|
|
FREAD(__collate_substitute_table, sizeof(__collate_substitute_table),
|
|
|
|
1, fp);
|
|
|
|
FREAD(__collate_char_pri_table, sizeof(__collate_char_pri_table), 1,
|
|
|
|
fp);
|
|
|
|
FREAD(__collate_chain_pri_table, sizeof(__collate_chain_pri_table), 1,
|
|
|
|
fp);
|
|
|
|
fclose(fp);
|
|
|
|
__collate_load_error = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
u_char *
|
|
|
|
__collate_substitute(s)
|
|
|
|
const u_char *s;
|
|
|
|
{
|
|
|
|
int dest_len = 0, len = 0;
|
|
|
|
int delta = strlen(s);
|
1995-02-17 16:36:12 +00:00
|
|
|
u_char *dest_str = NULL;
|
1995-02-16 04:24:39 +00:00
|
|
|
|
1995-02-17 16:36:12 +00:00
|
|
|
if(s == NULL || *s == '\0')
|
1995-02-16 04:24:39 +00:00
|
|
|
return __collate_strdup("");
|
|
|
|
while(*s) {
|
|
|
|
len += strlen(__collate_substitute_table[*s]);
|
|
|
|
while(dest_len <= len) {
|
|
|
|
if(!dest_str)
|
|
|
|
dest_str = calloc(dest_len = delta, 1);
|
|
|
|
else
|
|
|
|
dest_str = realloc(dest_str, dest_len += delta);
|
1995-02-17 16:36:12 +00:00
|
|
|
if(dest_str == NULL)
|
|
|
|
__collate_err(EX_OSERR, __FUNCTION__);
|
1995-02-16 04:24:39 +00:00
|
|
|
}
|
|
|
|
strcat(dest_str, __collate_substitute_table[*s++]);
|
|
|
|
}
|
|
|
|
return dest_str;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
__collate_lookup(t, len, prim, sec)
|
|
|
|
u_char *t;
|
|
|
|
int *len, *prim, *sec;
|
|
|
|
{
|
|
|
|
struct __collate_st_chain_pri *p2;
|
|
|
|
|
|
|
|
*len = 1;
|
|
|
|
*prim = *sec = 0;
|
|
|
|
for(p2 = __collate_chain_pri_table; p2->str[0]; p2++) {
|
|
|
|
if(strncmp(t, p2->str, strlen(p2->str)) == 0) {
|
|
|
|
*len = strlen(p2->str);
|
|
|
|
*prim = p2->prim;
|
|
|
|
*sec = p2->sec;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*prim = __collate_char_pri_table[*t].prim;
|
|
|
|
*sec = __collate_char_pri_table[*t].sec;
|
|
|
|
}
|
|
|
|
|
|
|
|
u_char *
|
|
|
|
__collate_strdup(s)
|
|
|
|
u_char *s;
|
|
|
|
{
|
1995-02-17 16:36:12 +00:00
|
|
|
u_char *t = strdup(s);
|
1995-02-16 04:24:39 +00:00
|
|
|
|
1995-02-17 16:36:12 +00:00
|
|
|
if (t == NULL)
|
|
|
|
__collate_err(EX_OSERR, __FUNCTION__);
|
1995-02-16 04:24:39 +00:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
1996-09-14 03:00:32 +00:00
|
|
|
void
|
|
|
|
__collate_err(int ex, const char *f)
|
1995-02-17 16:36:12 +00:00
|
|
|
{
|
|
|
|
extern char *__progname; /* Program name, from crt0. */
|
|
|
|
const char *s;
|
|
|
|
int serrno = errno;
|
|
|
|
|
|
|
|
s = __progname;
|
|
|
|
write(STDERR_FILENO, s, strlen(s));
|
1995-02-18 01:42:02 +00:00
|
|
|
write(STDERR_FILENO, ": ", 2);
|
1995-02-17 16:36:12 +00:00
|
|
|
s = f;
|
|
|
|
write(STDERR_FILENO, s, strlen(s));
|
1995-02-18 01:42:02 +00:00
|
|
|
write(STDERR_FILENO, ": ", 2);
|
1995-02-17 16:36:12 +00:00
|
|
|
s = strerror(serrno);
|
|
|
|
write(STDERR_FILENO, s, strlen(s));
|
1995-02-18 01:42:02 +00:00
|
|
|
write(STDERR_FILENO, "\n", 1);
|
1995-02-17 16:36:12 +00:00
|
|
|
exit(ex);
|
|
|
|
}
|
|
|
|
|
1995-02-16 04:24:39 +00:00
|
|
|
#ifdef COLLATE_DEBUG
|
|
|
|
void
|
|
|
|
__collate_print_tables()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct __collate_st_chain_pri *p2;
|
|
|
|
|
|
|
|
printf("Substitute table:\n");
|
|
|
|
for (i = 0; i < UCHAR_MAX + 1; i++)
|
1996-10-23 15:35:46 +00:00
|
|
|
if (i != *__collate_substitute_table[i])
|
1995-02-16 04:24:39 +00:00
|
|
|
printf("\t'%c' --> \"%s\"\n", i,
|
|
|
|
__collate_substitute_table[i]);
|
|
|
|
printf("Chain priority table:\n");
|
|
|
|
for (p2 = __collate_chain_pri_table; p2->str[0]; p2++)
|
|
|
|
printf("\t\"%s\" : %d %d\n\n", p2->str, p2->prim, p2->sec);
|
|
|
|
printf("Char priority table:\n");
|
|
|
|
for (i = 0; i < UCHAR_MAX + 1; i++)
|
|
|
|
printf("\t'%c' : %d %d\n", i, __collate_char_pri_table[i].prim,
|
|
|
|
__collate_char_pri_table[i].sec);
|
|
|
|
}
|
|
|
|
#endif
|