wdresolve.c revision b599bd937c305a895426e8c412ca920ce7824850
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * CDDL HEADER START
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *
3e14f97f673e8a630f076077de35afdd43dc1587Roger A. Faulkner * The contents of this file are subject to the terms of the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Common Development and Distribution License (the "License").
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * You may not use this file except in compliance with the License.
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * or http://www.opensolaris.org/os/licensing.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * See the License for the specific language governing permissions
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * and limitations under the License.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * When distributing Covered Code, include this CDDL HEADER in each
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * If applicable, add the following below this CDDL HEADER, with the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * fields enclosed by brackets "[]" replaced with your own identifying
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * information: Portions Copyright [yyyy] [name of copyright owner]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * CDDL HEADER END
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
3e14f97f673e8a630f076077de35afdd43dc1587Roger A. Faulkner * Use is subject to license terms.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include "lint.h"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include "mtlib.h"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <sys/types.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <ctype.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <string.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <stdlib.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <euc.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <widec.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <wctype.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <limits.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <synch.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <thread.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <libintl.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include "libc.h"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <locale.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <dlfcn.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include "_loc_path.h"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic int wdchkind_C(wchar_t);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic int (*wdchknd)(wchar_t) = wdchkind_C;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic int wdbindf_C(wchar_t, wchar_t, int);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic int (*wdbdg)(wchar_t, wchar_t, int) = wdbindf_C;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic wchar_t *wddelim_C(wchar_t, wchar_t, int);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic wchar_t *(*wddlm)(wchar_t, wchar_t, int) = wddelim_C;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic wchar_t (*mcfllr)(void) = NULL;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic int (*mcwrp)(void) = NULL;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic void *modhandle = NULL;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic int initialized = 0;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic int
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin_wdinitialize(void)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin{
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define _DFLTLOCPATH_LEN (sizeof (_DFLT_LOC_PATH) - 1)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define _WDMODPATH_LEN (sizeof (_WDMOD_PATH) - 1)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin char wdmodpath[PATH_MAX];
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin char *loc;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin size_t loclen;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin locale_t curloc;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin initialized = 1;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (modhandle)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin (void) dlclose(modhandle);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin curloc = uselocale(NULL);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin loc = current_locale(curloc, LC_CTYPE);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin loclen = strlen(loc);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (_DFLTLOCPATH_LEN + loclen + _WDMODPATH_LEN >= sizeof (wdmodpath)) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* pathname too long */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin modhandle = NULL;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin goto C_fallback;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin (void) strcpy(wdmodpath, _DFLT_LOC_PATH);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin (void) strcpy(wdmodpath + _DFLTLOCPATH_LEN, loc);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin (void) strcpy(wdmodpath + _DFLTLOCPATH_LEN + loclen, _WDMOD_PATH);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if ((modhandle = dlopen(wdmodpath, RTLD_LAZY)) != NULL) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin wdchknd = (int(*)(wchar_t))dlsym(modhandle, "_wdchkind_");
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (wdchknd == NULL)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin wdchknd = wdchkind_C;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin wdbdg = (int(*)(wchar_t, wchar_t, int))dlsym(modhandle,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "_wdbindf_");
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (wdbdg == NULL)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin wdbdg = wdbindf_C;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin wddlm = (wchar_t *(*)(wchar_t, wchar_t, int))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin dlsym(modhandle, "_wddelim_");
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (wddlm == NULL)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin wddlm = wddelim_C;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin mcfllr = (wchar_t(*)(void))dlsym(modhandle, "_mcfiller_");
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin mcwrp = (int(*)(void))dlsym(modhandle, "_mcwrap_");
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return ((mcfllr && mcwrp) ? 0 : -1);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinC_fallback:
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin wdchknd = wdchkind_C;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin wdbdg = wdbindf_C;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin wddlm = wddelim_C;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin mcfllr = NULL;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin mcwrp = NULL;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return (-1);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin}
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * wdinit() initializes other word-analyzing routines according to the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * current locale. Programmers are supposed to call this routine every
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * time the locale for the LC_CTYPE category is changed. It returns 0
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * when every initialization completes successfully, or -1 otherwise.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/* XXX: wdinit() is not exported from libc. Should it be? */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinint
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwdinit()
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin{
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin int res;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin callout_lock_enter();
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin res = _wdinitialize();
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin callout_lock_exit();
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return (res);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin}
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * wdchkind() returns a non-negative integral value unique to the kind
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * of the character represented by given argument.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinint
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwdchkind(wchar_t wc)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin{
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin int i;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin callout_lock_enter();
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (!initialized)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin (void) _wdinitialize();
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin i = (*wdchknd)(wc);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin callout_lock_exit();
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return (i);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin}
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic int
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwdchkind_C(wchar_t wc)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin{
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin switch (wcsetno(wc)) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin case 1:
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return (2);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin case 2:
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return (3);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin case 3:
return (4);
case 0:
return (isascii(wc) &&
(isalpha(wc) || isdigit(wc) || wc == ' '));
}
return (0);
}
/*
* wdbindf() returns an integral value (0 - 7) indicating binding
* strength of two characters represented by the first two arguments.
* It returns -1 when either of the two character is not printable.
*/
/*ARGSUSED*/
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);
}
/*ARGSUSED*/
static int
wdbindf_C(wchar_t wc1, wchar_t wc2, int type)
{
if (csetlen(wc1) > 1 && csetlen(wc2) > 1)
return (4);
return (6);
}
/*
* wddelim() returns a pointer to a null-terminated word delimiter
* string in wchar_t type that is thought most appropriate to join
* a text line ending with the first argument and a line beginning
* with the second argument, with. When either of the two character
* is not printable it returns a pointer to a null wide character.
*/
/*ARGSUSED*/
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);
}
/*ARGSUSED*/
static wchar_t *
wddelim_C(wchar_t wc1, wchar_t wc2, int type)
{
return ((wchar_t *)L" ");
}
/*
* mcfiller returns a printable ASCII character suggested for use in
* filling space resulted by a multicolumn character at the right margin.
*/
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)'~');
}
/*
* mcwrap returns an integral value indicating if a multicolumn character
* on the right margin should be wrapped around on a terminal screen.
*/
/* XXX: mcwrap() is not exported from libc. Should it be? */
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);
}