#include "lint.h"
#include "mtlib.h"
#include <sys/types.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <euc.h>
#include <widec.h>
#include <wctype.h>
#include <limits.h>
#include <synch.h>
#include <thread.h>
#include <libintl.h>
#include "libc.h"
#include <locale.h>
#include <dlfcn.h>
#include "_loc_path.h"
static int wdchkind_C(wchar_t);
static int (*wdchknd)(wchar_t) = wdchkind_C;
static int wdbindf_C(wchar_t, wchar_t, int);
static int (*wdbdg)(wchar_t, wchar_t, int) = wdbindf_C;
static wchar_t *wddelim_C(wchar_t, wchar_t, int);
static wchar_t *(*wddlm)(wchar_t, wchar_t, int) = wddelim_C;
static wchar_t (*mcfllr)(void) = NULL;
static int (*mcwrp)(void) = NULL;
static void *modhandle = NULL;
static int initialized = 0;
static int
_wdinitialize(void)
{
char wdmodpath[PATH_MAX];
char *loc;
locale_t curloc;
int rv;
initialized = 1;
if (modhandle)
(void) dlclose(modhandle);
curloc = uselocale(NULL);
loc = current_locale(curloc, LC_CTYPE);
rv = snprintf(wdmodpath, sizeof (wdmodpath), "%s%s%s",
_DFLT_LOC_PATH, loc, _WDMOD_PATH);
if (rv < 0 || rv >= sizeof (wdmodpath)) {
modhandle = NULL;
goto C_fallback;
}
if ((modhandle = dlopen(wdmodpath, RTLD_LAZY)) != NULL) {
wdchknd = (int(*)(wchar_t))dlsym(modhandle, "_wdchkind_");
if (wdchknd == NULL)
wdchknd = wdchkind_C;
wdbdg = (int(*)(wchar_t, wchar_t, int))dlsym(modhandle,
"_wdbindf_");
if (wdbdg == NULL)
wdbdg = wdbindf_C;
wddlm = (wchar_t *(*)(wchar_t, wchar_t, int))
dlsym(modhandle, "_wddelim_");
if (wddlm == NULL)
wddlm = wddelim_C;
mcfllr = (wchar_t(*)(void))dlsym(modhandle, "_mcfiller_");
mcwrp = (int(*)(void))dlsym(modhandle, "_mcwrap_");
return ((mcfllr && mcwrp) ? 0 : -1);
}
C_fallback:
wdchknd = wdchkind_C;
wdbdg = wdbindf_C;
wddlm = wddelim_C;
mcfllr = NULL;
mcwrp = NULL;
return (-1);
}
int
wdinit()
{
int res;
callout_lock_enter();
res = _wdinitialize();
callout_lock_exit();
return (res);
}
int
wdchkind(wchar_t wc)
{
int i;
callout_lock_enter();
if (!initialized)
(void) _wdinitialize();
i = (*wdchknd)(wc);
callout_lock_exit();
return (i);
}
static int
wdchkind_C(wchar_t wc)
{
switch (wcsetno(wc)) {
case 1:
return (2);
case 2:
return (3);
case 3:
return (4);
case 0:
return (isascii(wc) &&
(isalpha(wc) || isdigit(wc) || wc == ' '));
}
return (0);
}
int
wdbindf(wchar_t wc1, wchar_t wc2, int type)
{
int i;
callout_lock_enter();
if (!initialized)
(void) _wdinitialize();
if (!iswprint(wc1) || !iswprint(wc2)) {
callout_lock_exit();
return (-1);
}
i = (*wdbdg)(wc1, wc2, type);
callout_lock_exit();
return (i);
}
static int
wdbindf_C(wchar_t wc1, wchar_t wc2, int type __unused)
{
if (csetlen(wc1) > 1 && csetlen(wc2) > 1)
return (4);
return (6);
}
wchar_t *
wddelim(wchar_t wc1, wchar_t wc2, int type)
{
wchar_t *i;
callout_lock_enter();
if (!initialized)
(void) _wdinitialize();
if (!iswprint(wc1) || !iswprint(wc2)) {
callout_lock_exit();
return ((wchar_t *)L"");
}
i = (*wddlm)(wc1, wc2, type);
callout_lock_exit();
return (i);
}
static wchar_t *
wddelim_C(wchar_t wc1 __unused, wchar_t wc2 __unused, int type __unused)
{
return ((wchar_t *)L" ");
}
wchar_t
mcfiller(void)
{
wchar_t fillerchar;
callout_lock_enter();
if (!initialized)
(void) _wdinitialize();
if (mcfllr) {
fillerchar = (*mcfllr)();
if (!fillerchar)
fillerchar = (wchar_t)'~';
if (iswprint(fillerchar)) {
callout_lock_exit();
return (fillerchar);
}
}
callout_lock_exit();
return ((wchar_t)'~');
}
int
mcwrap(void)
{
callout_lock_enter();
if (!initialized)
(void) _wdinitialize();
if (mcwrp)
if ((*mcwrp)() == 0) {
callout_lock_exit();
return (0);
}
callout_lock_exit();
return (1);
}