17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
52e145884Sraf * Common Development and Distribution License (the "License").
62e145884Sraf * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
212e145884Sraf
227c478bd9Sstevel@tonic-gate /*
23*7257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
287c478bd9Sstevel@tonic-gate
29*7257d1b4Sraf #include "lint.h"
307c478bd9Sstevel@tonic-gate #include "mtlib.h"
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <ctype.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate #include <euc.h>
367c478bd9Sstevel@tonic-gate #include <widec.h>
377c478bd9Sstevel@tonic-gate #include <wctype.h>
387c478bd9Sstevel@tonic-gate #include <limits.h>
397c478bd9Sstevel@tonic-gate #include <synch.h>
407c478bd9Sstevel@tonic-gate #include <thread.h>
417c478bd9Sstevel@tonic-gate #include <libintl.h>
427c478bd9Sstevel@tonic-gate #include "libc.h"
437c478bd9Sstevel@tonic-gate
447c478bd9Sstevel@tonic-gate #include <locale.h>
457c478bd9Sstevel@tonic-gate #include <dlfcn.h>
467c478bd9Sstevel@tonic-gate #include "_loc_path.h"
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate static int wdchkind_C(wchar_t);
497c478bd9Sstevel@tonic-gate static int (*wdchknd)(wchar_t) = wdchkind_C;
507c478bd9Sstevel@tonic-gate static int wdbindf_C(wchar_t, wchar_t, int);
517c478bd9Sstevel@tonic-gate static int (*wdbdg)(wchar_t, wchar_t, int) = wdbindf_C;
527c478bd9Sstevel@tonic-gate static wchar_t *wddelim_C(wchar_t, wchar_t, int);
537c478bd9Sstevel@tonic-gate static wchar_t *(*wddlm)(wchar_t, wchar_t, int) = wddelim_C;
547c478bd9Sstevel@tonic-gate static wchar_t (*mcfllr)(void) = NULL;
557c478bd9Sstevel@tonic-gate static int (*mcwrp)(void) = NULL;
567c478bd9Sstevel@tonic-gate static void *modhandle = NULL;
577c478bd9Sstevel@tonic-gate static int initialized = 0;
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate static int
_wdinitialize(void)607c478bd9Sstevel@tonic-gate _wdinitialize(void)
617c478bd9Sstevel@tonic-gate {
627c478bd9Sstevel@tonic-gate #define _DFLTLOCPATH_LEN (sizeof (_DFLT_LOC_PATH) - 1)
637c478bd9Sstevel@tonic-gate #define _WDMODPATH_LEN (sizeof (_WDMOD_PATH) - 1)
647c478bd9Sstevel@tonic-gate char wdmodpath[PATH_MAX];
657c478bd9Sstevel@tonic-gate char *loc;
667c478bd9Sstevel@tonic-gate size_t loclen;
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate initialized = 1;
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate if (modhandle)
717c478bd9Sstevel@tonic-gate (void) dlclose(modhandle);
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate loc = setlocale(LC_CTYPE, NULL); /* this never return NULL */
747c478bd9Sstevel@tonic-gate loclen = strlen(loc);
757c478bd9Sstevel@tonic-gate if (_DFLTLOCPATH_LEN + loclen + _WDMODPATH_LEN >= sizeof (wdmodpath)) {
767c478bd9Sstevel@tonic-gate /* pathname too long */
777c478bd9Sstevel@tonic-gate modhandle = NULL;
787c478bd9Sstevel@tonic-gate goto C_fallback;
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate (void) strcpy(wdmodpath, _DFLT_LOC_PATH);
827c478bd9Sstevel@tonic-gate (void) strcpy(wdmodpath + _DFLTLOCPATH_LEN, loc);
837c478bd9Sstevel@tonic-gate (void) strcpy(wdmodpath + _DFLTLOCPATH_LEN + loclen, _WDMOD_PATH);
847c478bd9Sstevel@tonic-gate
857c478bd9Sstevel@tonic-gate if ((modhandle = dlopen(wdmodpath, RTLD_LAZY)) != NULL) {
867c478bd9Sstevel@tonic-gate wdchknd = (int(*)(wchar_t))dlsym(modhandle, "_wdchkind_");
877c478bd9Sstevel@tonic-gate if (wdchknd == NULL)
887c478bd9Sstevel@tonic-gate wdchknd = wdchkind_C;
897c478bd9Sstevel@tonic-gate wdbdg = (int(*)(wchar_t, wchar_t, int))dlsym(modhandle,
907c478bd9Sstevel@tonic-gate "_wdbindf_");
917c478bd9Sstevel@tonic-gate if (wdbdg == NULL)
927c478bd9Sstevel@tonic-gate wdbdg = wdbindf_C;
937c478bd9Sstevel@tonic-gate wddlm = (wchar_t *(*)(wchar_t, wchar_t, int))
947c478bd9Sstevel@tonic-gate dlsym(modhandle, "_wddelim_");
957c478bd9Sstevel@tonic-gate if (wddlm == NULL)
967c478bd9Sstevel@tonic-gate wddlm = wddelim_C;
977c478bd9Sstevel@tonic-gate mcfllr = (wchar_t(*)(void))dlsym(modhandle, "_mcfiller_");
987c478bd9Sstevel@tonic-gate mcwrp = (int(*)(void))dlsym(modhandle, "_mcwrap_");
997c478bd9Sstevel@tonic-gate return ((mcfllr && mcwrp) ? 0 : -1);
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate C_fallback:
1037c478bd9Sstevel@tonic-gate wdchknd = wdchkind_C;
1047c478bd9Sstevel@tonic-gate wdbdg = wdbindf_C;
1057c478bd9Sstevel@tonic-gate wddlm = wddelim_C;
1067c478bd9Sstevel@tonic-gate mcfllr = NULL;
1077c478bd9Sstevel@tonic-gate mcwrp = NULL;
1087c478bd9Sstevel@tonic-gate return (-1);
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate /*
1127c478bd9Sstevel@tonic-gate * wdinit() initializes other word-analyzing routines according to the
1137c478bd9Sstevel@tonic-gate * current locale. Programmers are supposed to call this routine every
1147c478bd9Sstevel@tonic-gate * time the locale for the LC_CTYPE category is changed. It returns 0
1157c478bd9Sstevel@tonic-gate * when every initialization completes successfully, or -1 otherwise.
1167c478bd9Sstevel@tonic-gate */
1177c478bd9Sstevel@tonic-gate /* XXX: wdinit() is not exported from libc. Should it be? */
1187c478bd9Sstevel@tonic-gate int
wdinit()1197c478bd9Sstevel@tonic-gate wdinit()
1207c478bd9Sstevel@tonic-gate {
1217c478bd9Sstevel@tonic-gate int res;
1227c478bd9Sstevel@tonic-gate
12398c1a6b4Sraf callout_lock_enter();
1247c478bd9Sstevel@tonic-gate res = _wdinitialize();
12598c1a6b4Sraf callout_lock_exit();
1267c478bd9Sstevel@tonic-gate return (res);
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate
1297c478bd9Sstevel@tonic-gate /*
1307c478bd9Sstevel@tonic-gate * wdchkind() returns a non-negative integral value unique to the kind
1317c478bd9Sstevel@tonic-gate * of the character represented by given argument.
1327c478bd9Sstevel@tonic-gate */
1337c478bd9Sstevel@tonic-gate int
wdchkind(wchar_t wc)1347c478bd9Sstevel@tonic-gate wdchkind(wchar_t wc)
1357c478bd9Sstevel@tonic-gate {
1367c478bd9Sstevel@tonic-gate int i;
1377c478bd9Sstevel@tonic-gate
13898c1a6b4Sraf callout_lock_enter();
1397c478bd9Sstevel@tonic-gate if (!initialized)
1407c478bd9Sstevel@tonic-gate (void) _wdinitialize();
1417c478bd9Sstevel@tonic-gate i = (*wdchknd)(wc);
14298c1a6b4Sraf callout_lock_exit();
1437c478bd9Sstevel@tonic-gate return (i);
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate static int
wdchkind_C(wchar_t wc)1467c478bd9Sstevel@tonic-gate wdchkind_C(wchar_t wc)
1477c478bd9Sstevel@tonic-gate {
1487c478bd9Sstevel@tonic-gate switch (wcsetno(wc)) {
1497c478bd9Sstevel@tonic-gate case 1:
1507c478bd9Sstevel@tonic-gate return (2);
1517c478bd9Sstevel@tonic-gate case 2:
1527c478bd9Sstevel@tonic-gate return (3);
1537c478bd9Sstevel@tonic-gate case 3:
1547c478bd9Sstevel@tonic-gate return (4);
1557c478bd9Sstevel@tonic-gate case 0:
1567c478bd9Sstevel@tonic-gate return (isascii(wc) &&
1577c478bd9Sstevel@tonic-gate (isalpha(wc) || isdigit(wc) || wc == ' '));
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate return (0);
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate
1627c478bd9Sstevel@tonic-gate /*
1637c478bd9Sstevel@tonic-gate * wdbindf() returns an integral value (0 - 7) indicating binding
1647c478bd9Sstevel@tonic-gate * strength of two characters represented by the first two arguments.
1657c478bd9Sstevel@tonic-gate * It returns -1 when either of the two character is not printable.
1667c478bd9Sstevel@tonic-gate */
1677c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1687c478bd9Sstevel@tonic-gate int
wdbindf(wchar_t wc1,wchar_t wc2,int type)1697c478bd9Sstevel@tonic-gate wdbindf(wchar_t wc1, wchar_t wc2, int type)
1707c478bd9Sstevel@tonic-gate {
1717c478bd9Sstevel@tonic-gate int i;
1727c478bd9Sstevel@tonic-gate
17398c1a6b4Sraf callout_lock_enter();
1747c478bd9Sstevel@tonic-gate if (!initialized)
1757c478bd9Sstevel@tonic-gate (void) _wdinitialize();
1767c478bd9Sstevel@tonic-gate if (!iswprint(wc1) || !iswprint(wc2)) {
17798c1a6b4Sraf callout_lock_exit();
1787c478bd9Sstevel@tonic-gate return (-1);
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate i = (*wdbdg)(wc1, wc2, type);
18198c1a6b4Sraf callout_lock_exit();
1827c478bd9Sstevel@tonic-gate return (i);
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1857c478bd9Sstevel@tonic-gate static int
wdbindf_C(wchar_t wc1,wchar_t wc2,int type)1867c478bd9Sstevel@tonic-gate wdbindf_C(wchar_t wc1, wchar_t wc2, int type)
1877c478bd9Sstevel@tonic-gate {
1887c478bd9Sstevel@tonic-gate if (csetlen(wc1) > 1 && csetlen(wc2) > 1)
1897c478bd9Sstevel@tonic-gate return (4);
1907c478bd9Sstevel@tonic-gate return (6);
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate
1937c478bd9Sstevel@tonic-gate /*
1947c478bd9Sstevel@tonic-gate * wddelim() returns a pointer to a null-terminated word delimiter
1957c478bd9Sstevel@tonic-gate * string in wchar_t type that is thought most appropriate to join
1967c478bd9Sstevel@tonic-gate * a text line ending with the first argument and a line beginning
1977c478bd9Sstevel@tonic-gate * with the second argument, with. When either of the two character
1987c478bd9Sstevel@tonic-gate * is not printable it returns a pointer to a null wide character.
1997c478bd9Sstevel@tonic-gate */
2007c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2017c478bd9Sstevel@tonic-gate wchar_t *
wddelim(wchar_t wc1,wchar_t wc2,int type)2027c478bd9Sstevel@tonic-gate wddelim(wchar_t wc1, wchar_t wc2, int type)
2037c478bd9Sstevel@tonic-gate {
2047c478bd9Sstevel@tonic-gate wchar_t *i;
2057c478bd9Sstevel@tonic-gate
20698c1a6b4Sraf callout_lock_enter();
2077c478bd9Sstevel@tonic-gate if (!initialized)
2087c478bd9Sstevel@tonic-gate (void) _wdinitialize();
2097c478bd9Sstevel@tonic-gate if (!iswprint(wc1) || !iswprint(wc2)) {
21098c1a6b4Sraf callout_lock_exit();
2117c478bd9Sstevel@tonic-gate return ((wchar_t *)L"");
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate i = (*wddlm)(wc1, wc2, type);
21498c1a6b4Sraf callout_lock_exit();
2157c478bd9Sstevel@tonic-gate return (i);
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2187c478bd9Sstevel@tonic-gate static wchar_t *
wddelim_C(wchar_t wc1,wchar_t wc2,int type)2197c478bd9Sstevel@tonic-gate wddelim_C(wchar_t wc1, wchar_t wc2, int type)
2207c478bd9Sstevel@tonic-gate {
2217c478bd9Sstevel@tonic-gate return ((wchar_t *)L" ");
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate
2247c478bd9Sstevel@tonic-gate /*
2257c478bd9Sstevel@tonic-gate * mcfiller returns a printable ASCII character suggested for use in
2267c478bd9Sstevel@tonic-gate * filling space resulted by a multicolumn character at the right margin.
2277c478bd9Sstevel@tonic-gate */
2287c478bd9Sstevel@tonic-gate wchar_t
mcfiller(void)2297c478bd9Sstevel@tonic-gate mcfiller(void)
2307c478bd9Sstevel@tonic-gate {
2317c478bd9Sstevel@tonic-gate wchar_t fillerchar;
2327c478bd9Sstevel@tonic-gate
23398c1a6b4Sraf callout_lock_enter();
2347c478bd9Sstevel@tonic-gate if (!initialized)
2357c478bd9Sstevel@tonic-gate (void) _wdinitialize();
2367c478bd9Sstevel@tonic-gate if (mcfllr) {
2377c478bd9Sstevel@tonic-gate fillerchar = (*mcfllr)();
2387c478bd9Sstevel@tonic-gate if (!fillerchar)
2397c478bd9Sstevel@tonic-gate fillerchar = (wchar_t)'~';
2407c478bd9Sstevel@tonic-gate if (iswprint(fillerchar)) {
24198c1a6b4Sraf callout_lock_exit();
2427c478bd9Sstevel@tonic-gate return (fillerchar);
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate }
24598c1a6b4Sraf callout_lock_exit();
2467c478bd9Sstevel@tonic-gate return ((wchar_t)'~');
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate
2497c478bd9Sstevel@tonic-gate /*
2507c478bd9Sstevel@tonic-gate * mcwrap returns an integral value indicating if a multicolumn character
2517c478bd9Sstevel@tonic-gate * on the right margin should be wrapped around on a terminal screen.
2527c478bd9Sstevel@tonic-gate */
2537c478bd9Sstevel@tonic-gate /* XXX: mcwrap() is not exported from libc. Should it be? */
2547c478bd9Sstevel@tonic-gate int
mcwrap(void)2557c478bd9Sstevel@tonic-gate mcwrap(void)
2567c478bd9Sstevel@tonic-gate {
25798c1a6b4Sraf callout_lock_enter();
2587c478bd9Sstevel@tonic-gate if (!initialized)
2597c478bd9Sstevel@tonic-gate (void) _wdinitialize();
2607c478bd9Sstevel@tonic-gate if (mcwrp)
2617c478bd9Sstevel@tonic-gate if ((*mcwrp)() == 0) {
26298c1a6b4Sraf callout_lock_exit();
2637c478bd9Sstevel@tonic-gate return (0);
2647c478bd9Sstevel@tonic-gate }
26598c1a6b4Sraf callout_lock_exit();
2667c478bd9Sstevel@tonic-gate return (1);
2677c478bd9Sstevel@tonic-gate }
268