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
5f13ac639Smuffin * Common Development and Distribution License (the "License").
6f13ac639Smuffin * 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 */
21e8031f0aSraf
227c478bd9Sstevel@tonic-gate /*
23f13ac639Smuffin * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
26b599bd93SRobert Mustacchi /*
27b599bd93SRobert Mustacchi * Copyright 2015 Joyent, Inc.
28b599bd93SRobert Mustacchi */
297c478bd9Sstevel@tonic-gate
307257d1b4Sraf #include "lint.h"
317c478bd9Sstevel@tonic-gate #include "mtlib.h"
327c478bd9Sstevel@tonic-gate #include <ctype.h>
337c478bd9Sstevel@tonic-gate #include <locale.h>
347c478bd9Sstevel@tonic-gate #include <stdio.h>
357c478bd9Sstevel@tonic-gate #include <stdlib.h>
367c478bd9Sstevel@tonic-gate #include <string.h>
377c478bd9Sstevel@tonic-gate #include <sys/types.h>
387c478bd9Sstevel@tonic-gate #include <sys/mman.h>
397c478bd9Sstevel@tonic-gate #include <sys/param.h>
407c478bd9Sstevel@tonic-gate #include <sys/stat.h>
417c478bd9Sstevel@tonic-gate #include <libintl.h>
427c478bd9Sstevel@tonic-gate #include <thread.h>
437c478bd9Sstevel@tonic-gate #include <synch.h>
447c478bd9Sstevel@tonic-gate #include <limits.h>
457c478bd9Sstevel@tonic-gate #include <unistd.h>
467c478bd9Sstevel@tonic-gate #include "libc.h"
477c478bd9Sstevel@tonic-gate #include "_loc_path.h"
487c478bd9Sstevel@tonic-gate #include "msgfmt.h"
497c478bd9Sstevel@tonic-gate #include "gettext.h"
507c478bd9Sstevel@tonic-gate #include "nlspath_checks.h"
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate static int process_nlspath(const char *, const char *,
53f13ac639Smuffin const char *, char **);
54f13ac639Smuffin static char *replace_nls_option(char *, const char *, char *,
55f13ac639Smuffin char *, char *, char *, char *);
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate char *
_real_gettext_u(const char * domain,const char * msgid1,const char * msgid2,unsigned long int ln,int category,int plural,locale_t loc)58f13ac639Smuffin _real_gettext_u(const char *domain, const char *msgid1, const char *msgid2,
59b599bd93SRobert Mustacchi unsigned long int ln, int category, int plural, locale_t loc)
607c478bd9Sstevel@tonic-gate {
61*0a9a25a2SRichard Hansen char msgfile[MAXPATHLEN]; /* 1024 */
627c478bd9Sstevel@tonic-gate char mydomain[TEXTDOMAINMAX + 1]; /* 256 + 1 */
637c478bd9Sstevel@tonic-gate char *cur_binding; /* points to current binding in list */
64b599bd93SRobert Mustacchi const char *cur_locale;
65b599bd93SRobert Mustacchi char *cur_domain, *result, *nlspath;
66f13ac639Smuffin char *msgloc, *cb, *cur_domain_binding;
677c478bd9Sstevel@tonic-gate char *language;
68f13ac639Smuffin unsigned int n = (unsigned int)ln; /* we don't need long for n */
69f13ac639Smuffin uint32_t cur_domain_len, cblen;
70f13ac639Smuffin uint32_t hash_domain;
71e8031f0aSraf struct msg_pack *mp, omp;
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate #ifdef GETTEXT_DEBUG
74f13ac639Smuffin gprintf(0, "*************** _real_gettext_u(\"%s\", \"%s\", "
75f13ac639Smuffin "\"%s\", %d, %d, %d)\n",
767c478bd9Sstevel@tonic-gate domain ? domain : "NULL", msgid1 ? msgid1 : "NULL",
77f13ac639Smuffin msgid2 ? msgid2 : "NULL", n, category, plural);
78f13ac639Smuffin gprintf(0, "***************** global_gt: 0x%p\n", global_gt);
79f13ac639Smuffin printgt(global_gt, 1);
807c478bd9Sstevel@tonic-gate #endif
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate if (msgid1 == NULL)
837c478bd9Sstevel@tonic-gate return (NULL);
847c478bd9Sstevel@tonic-gate
85e8031f0aSraf mp = memset(&omp, 0, sizeof (omp)); /* msg pack */
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate /*
887c478bd9Sstevel@tonic-gate * category may be LC_MESSAGES or LC_TIME
897c478bd9Sstevel@tonic-gate * locale contains the value of 'category'
907c478bd9Sstevel@tonic-gate */
91b599bd93SRobert Mustacchi if (loc == NULL)
92b599bd93SRobert Mustacchi loc = uselocale(NULL);
93b599bd93SRobert Mustacchi cur_locale = current_locale(loc, category);
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate language = getenv("LANGUAGE"); /* for GNU */
967c478bd9Sstevel@tonic-gate if (language) {
977c478bd9Sstevel@tonic-gate if (!*language || strchr(language, '/') != NULL) {
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate * LANGUAGE is an empty string or
1007c478bd9Sstevel@tonic-gate * LANGUAGE contains '/'.
1017c478bd9Sstevel@tonic-gate * Ignore it.
1027c478bd9Sstevel@tonic-gate */
1037c478bd9Sstevel@tonic-gate language = NULL;
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate }
1067c478bd9Sstevel@tonic-gate
1077c478bd9Sstevel@tonic-gate /*
1087c478bd9Sstevel@tonic-gate * Query the current domain if domain argument is NULL pointer
1097c478bd9Sstevel@tonic-gate */
1107c478bd9Sstevel@tonic-gate mydomain[0] = '\0';
111f13ac639Smuffin if (domain == NULL) {
1127c478bd9Sstevel@tonic-gate /*
1137c478bd9Sstevel@tonic-gate * if NULL is specified for domainname,
1147c478bd9Sstevel@tonic-gate * use the currently bound domain.
1157c478bd9Sstevel@tonic-gate */
1167c478bd9Sstevel@tonic-gate cur_domain = _textdomain_u(NULL, mydomain);
1177c478bd9Sstevel@tonic-gate } else if (!*domain) {
1187c478bd9Sstevel@tonic-gate /*
1197c478bd9Sstevel@tonic-gate * if an empty string is specified
1207c478bd9Sstevel@tonic-gate */
1217c478bd9Sstevel@tonic-gate cur_domain = DEFAULT_DOMAIN;
1227c478bd9Sstevel@tonic-gate } else {
1237c478bd9Sstevel@tonic-gate cur_domain = (char *)domain;
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate
126f13ac639Smuffin hash_domain = get_hashid(cur_domain, &cur_domain_len);
127f13ac639Smuffin if (cur_domain_len > TEXTDOMAINMAX) {
128f13ac639Smuffin /* domain is invalid, return msg_id */
129f13ac639Smuffin DFLTMSG(result, msgid1, msgid2, n, plural);
130f13ac639Smuffin return (result);
131f13ac639Smuffin }
132f13ac639Smuffin
1337c478bd9Sstevel@tonic-gate nlspath = getenv("NLSPATH"); /* get the content of NLSPATH */
134f13ac639Smuffin if (nlspath == NULL || !*nlspath) {
1357c478bd9Sstevel@tonic-gate /* no NLSPATH is defined in the environ */
1367c478bd9Sstevel@tonic-gate if ((*cur_locale == 'C') && (*(cur_locale + 1) == '\0')) {
1377c478bd9Sstevel@tonic-gate /*
1387c478bd9Sstevel@tonic-gate * If C locale,
1397c478bd9Sstevel@tonic-gate * return the original msgid immediately.
1407c478bd9Sstevel@tonic-gate */
1417c478bd9Sstevel@tonic-gate DFLTMSG(result, msgid1, msgid2, n, plural);
1427c478bd9Sstevel@tonic-gate return (result);
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate nlspath = NULL;
1457c478bd9Sstevel@tonic-gate } else {
1467c478bd9Sstevel@tonic-gate /* NLSPATH is set */
1477c478bd9Sstevel@tonic-gate int ret;
1487c478bd9Sstevel@tonic-gate
149b599bd93SRobert Mustacchi msgloc = current_locale(loc, LC_MESSAGES);
1507c478bd9Sstevel@tonic-gate
1517c478bd9Sstevel@tonic-gate ret = process_nlspath(cur_domain, msgloc,
152f13ac639Smuffin (const char *)nlspath, &cur_binding);
1537c478bd9Sstevel@tonic-gate if (ret == -1) {
1547c478bd9Sstevel@tonic-gate /* error occurred */
1557c478bd9Sstevel@tonic-gate DFLTMSG(result, msgid1, msgid2, n, plural);
1567c478bd9Sstevel@tonic-gate return (result);
1577c478bd9Sstevel@tonic-gate } else if (ret == 0) {
1587c478bd9Sstevel@tonic-gate nlspath = NULL;
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate
1627c478bd9Sstevel@tonic-gate cur_domain_binding = _real_bindtextdomain_u(cur_domain,
163f13ac639Smuffin NULL, TP_BINDING);
164f13ac639Smuffin if (cur_domain_binding == NULL) {
1657c478bd9Sstevel@tonic-gate DFLTMSG(result, msgid1, msgid2, n, plural);
1667c478bd9Sstevel@tonic-gate return (result);
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate mp->msgid1 = msgid1;
1707c478bd9Sstevel@tonic-gate mp->msgid2 = msgid2;
1717c478bd9Sstevel@tonic-gate mp->msgfile = msgfile;
1727c478bd9Sstevel@tonic-gate mp->domain = cur_domain;
1737c478bd9Sstevel@tonic-gate mp->binding = cur_domain_binding;
1747c478bd9Sstevel@tonic-gate mp->locale = cur_locale;
1757c478bd9Sstevel@tonic-gate mp->language = language;
1767c478bd9Sstevel@tonic-gate mp->domain_len = cur_domain_len;
1777c478bd9Sstevel@tonic-gate mp->n = n;
1787c478bd9Sstevel@tonic-gate mp->category = category;
1797c478bd9Sstevel@tonic-gate mp->plural = plural;
180f13ac639Smuffin mp->hash_domain = hash_domain;
1817c478bd9Sstevel@tonic-gate
1827c478bd9Sstevel@tonic-gate /*
1837c478bd9Sstevel@tonic-gate * Spec1170 requires that we use NLSPATH if it's defined, to
1847c478bd9Sstevel@tonic-gate * override any system default variables. If NLSPATH is not
1857c478bd9Sstevel@tonic-gate * defined or if a message catalog is not found in any of the
1867c478bd9Sstevel@tonic-gate * components (bindings) specified by NLSPATH, dcgettext_u() will
1877c478bd9Sstevel@tonic-gate * search for the message catalog in either a) the binding path set
1887c478bd9Sstevel@tonic-gate * by any previous application calls to bindtextdomain() or
1897c478bd9Sstevel@tonic-gate * b) the default binding path (/usr/lib/locale). Save the original
1907c478bd9Sstevel@tonic-gate * binding path so that we can search it if the message catalog
1917c478bd9Sstevel@tonic-gate * is not found via NLSPATH. The original binding is restored before
1927c478bd9Sstevel@tonic-gate * returning from this routine because the gettext routines should
1937c478bd9Sstevel@tonic-gate * not change the binding set by the application. This allows
1947c478bd9Sstevel@tonic-gate * bindtextdomain() to be called once for all gettext() calls in the
1957c478bd9Sstevel@tonic-gate * application.
1967c478bd9Sstevel@tonic-gate */
1977c478bd9Sstevel@tonic-gate
1987c478bd9Sstevel@tonic-gate /*
1997c478bd9Sstevel@tonic-gate * First, examine NLSPATH
2007c478bd9Sstevel@tonic-gate */
2017c478bd9Sstevel@tonic-gate if (nlspath) {
2027c478bd9Sstevel@tonic-gate /*
2037c478bd9Sstevel@tonic-gate * NLSPATH binding has been successfully built
2047c478bd9Sstevel@tonic-gate */
2057c478bd9Sstevel@tonic-gate #ifdef GETTEXT_DEBUG
206f13ac639Smuffin gprintf(0, "************************** examining NLSPATH\n");
207f13ac639Smuffin gprintf(0, " cur_binding: \"%s\"\n",
208f13ac639Smuffin cur_binding ? cur_binding : "(null)");
2097c478bd9Sstevel@tonic-gate #endif
2107c478bd9Sstevel@tonic-gate
2117c478bd9Sstevel@tonic-gate mp->nlsp = 1;
2127c478bd9Sstevel@tonic-gate /*
2137c478bd9Sstevel@tonic-gate * cur_binding always ends with ':' before a null
2147c478bd9Sstevel@tonic-gate * termination.
2157c478bd9Sstevel@tonic-gate */
2167c478bd9Sstevel@tonic-gate while (*cur_binding) {
2177c478bd9Sstevel@tonic-gate cb = cur_binding;
2187c478bd9Sstevel@tonic-gate while (*cur_binding != ':')
2197c478bd9Sstevel@tonic-gate cur_binding++;
2207c478bd9Sstevel@tonic-gate cblen = cur_binding - cb;
2217c478bd9Sstevel@tonic-gate cur_binding++;
2227c478bd9Sstevel@tonic-gate if (cblen >= MAXPATHLEN) {
2237c478bd9Sstevel@tonic-gate /* cur_binding too long */
2247c478bd9Sstevel@tonic-gate DFLTMSG(result, msgid1, msgid2, n, plural);
2257c478bd9Sstevel@tonic-gate return (result);
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate
228f13ac639Smuffin (void) memcpy(mp->msgfile, cb, cblen);
229f13ac639Smuffin *(mp->msgfile + cblen) = '\0';
230f13ac639Smuffin
2317c478bd9Sstevel@tonic-gate #ifdef GETTEXT_DEBUG
232f13ac639Smuffin gprintf(0, "*******************"
233f13ac639Smuffin "********************* \n");
234f13ac639Smuffin gprintf(0, " msgfile: \"%s\"\n",
235f13ac639Smuffin msgfile ? msgfile : "(null)");
236f13ac639Smuffin gprintf(0, "*******************"
237f13ac639Smuffin "********************* \n");
2387c478bd9Sstevel@tonic-gate #endif
239f13ac639Smuffin result = handle_mo(mp);
2407c478bd9Sstevel@tonic-gate if (result) {
2417c478bd9Sstevel@tonic-gate return (result);
2427c478bd9Sstevel@tonic-gate }
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate
2467c478bd9Sstevel@tonic-gate mp->nlsp = 0;
2477c478bd9Sstevel@tonic-gate mp->binding = cur_domain_binding;
2487c478bd9Sstevel@tonic-gate /*
2497c478bd9Sstevel@tonic-gate * Next, examine LANGUAGE
2507c478bd9Sstevel@tonic-gate */
2517c478bd9Sstevel@tonic-gate if (language) {
2527c478bd9Sstevel@tonic-gate char *ret_msg;
253f13ac639Smuffin ret_msg = handle_lang(mp);
2547c478bd9Sstevel@tonic-gate if (ret_msg != NULL) {
255f13ac639Smuffin /* valid msg found in GNU MO */
2567c478bd9Sstevel@tonic-gate return (ret_msg);
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate /*
259f13ac639Smuffin * handle_lang() may have overridden locale
2607c478bd9Sstevel@tonic-gate */
2617c478bd9Sstevel@tonic-gate mp->locale = cur_locale;
262f13ac639Smuffin mp->status = 0;
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate
2657c478bd9Sstevel@tonic-gate /*
2667c478bd9Sstevel@tonic-gate * Finally, handle a single binding
2677c478bd9Sstevel@tonic-gate */
2687c478bd9Sstevel@tonic-gate #ifdef GETTEXT_DEBUG
2697c478bd9Sstevel@tonic-gate *mp->msgfile = '\0';
2707c478bd9Sstevel@tonic-gate #endif
2717c478bd9Sstevel@tonic-gate if (mk_msgfile(mp) == NULL) {
2727c478bd9Sstevel@tonic-gate DFLTMSG(result, msgid1, msgid2, n, plural);
2737c478bd9Sstevel@tonic-gate return (result);
2747c478bd9Sstevel@tonic-gate }
2757c478bd9Sstevel@tonic-gate
276f13ac639Smuffin result = handle_mo(mp);
2777c478bd9Sstevel@tonic-gate if (result) {
2787c478bd9Sstevel@tonic-gate return (result);
2797c478bd9Sstevel@tonic-gate }
2807c478bd9Sstevel@tonic-gate DFLTMSG(result, msgid1, msgid2, n, plural);
2817c478bd9Sstevel@tonic-gate return (result);
2827c478bd9Sstevel@tonic-gate } /* _real_gettext_u */
2837c478bd9Sstevel@tonic-gate
2847c478bd9Sstevel@tonic-gate #define ALLFREE \
285f13ac639Smuffin free_all(nlstmp, nnp, pathname, ppaths, lang)
2867c478bd9Sstevel@tonic-gate
2877c478bd9Sstevel@tonic-gate static void
free_all(Nlstmp * nlstmp,Nls_node * nnp,char * pathname,char * ppaths,char * lang)2887c478bd9Sstevel@tonic-gate free_all(Nlstmp *nlstmp, Nls_node *nnp, char *pathname,
289f13ac639Smuffin char *ppaths, char *lang)
2907c478bd9Sstevel@tonic-gate {
2917c478bd9Sstevel@tonic-gate Nlstmp *tp, *tq;
2927c478bd9Sstevel@tonic-gate
2937c478bd9Sstevel@tonic-gate tp = nlstmp;
2947c478bd9Sstevel@tonic-gate while (tp) {
2957c478bd9Sstevel@tonic-gate tq = tp->next;
2967c478bd9Sstevel@tonic-gate free(tp);
2977c478bd9Sstevel@tonic-gate tp = tq;
2987c478bd9Sstevel@tonic-gate }
2997c478bd9Sstevel@tonic-gate if (nnp->locale)
3007c478bd9Sstevel@tonic-gate free(nnp->locale);
3017c478bd9Sstevel@tonic-gate if (nnp->domain)
3027c478bd9Sstevel@tonic-gate free(nnp->domain);
3037c478bd9Sstevel@tonic-gate if (pathname)
3047c478bd9Sstevel@tonic-gate free(pathname);
3057c478bd9Sstevel@tonic-gate if (ppaths)
3067c478bd9Sstevel@tonic-gate free(ppaths);
3077c478bd9Sstevel@tonic-gate if (lang)
3087c478bd9Sstevel@tonic-gate free(lang);
3097c478bd9Sstevel@tonic-gate free(nnp);
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate
3127c478bd9Sstevel@tonic-gate /*
3137c478bd9Sstevel@tonic-gate * process_nlspath(): process the NLSPATH environment variable.
3147c478bd9Sstevel@tonic-gate *
3157c478bd9Sstevel@tonic-gate * this routine looks at NLSPATH in the environment,
3167c478bd9Sstevel@tonic-gate * and will try to build up the binding list based
3177c478bd9Sstevel@tonic-gate * on the settings of NLSPATH.
3187c478bd9Sstevel@tonic-gate *
3197c478bd9Sstevel@tonic-gate * RETURN:
3207c478bd9Sstevel@tonic-gate * -1: Error occurred
3217c478bd9Sstevel@tonic-gate * 0: No error, but no binding list has been built
3227c478bd9Sstevel@tonic-gate * 1: No error, and a binding list has been built
3237c478bd9Sstevel@tonic-gate *
3247c478bd9Sstevel@tonic-gate */
3257c478bd9Sstevel@tonic-gate static int
process_nlspath(const char * cur_domain,const char * cur_msgloc,const char * nlspath,char ** binding)3267c478bd9Sstevel@tonic-gate process_nlspath(const char *cur_domain, const char *cur_msgloc,
327f13ac639Smuffin const char *nlspath, char **binding)
3287c478bd9Sstevel@tonic-gate {
329*0a9a25a2SRichard Hansen char *s; /* generic string ptr */
3307c478bd9Sstevel@tonic-gate char *territory; /* our current territory element */
3317c478bd9Sstevel@tonic-gate char *codeset; /* our current codeset element */
3327c478bd9Sstevel@tonic-gate char *s1; /* for handling territory */
3337c478bd9Sstevel@tonic-gate char *s2; /* for handling codeset */
3347c478bd9Sstevel@tonic-gate char *lang = NULL; /* our current language element */
3357c478bd9Sstevel@tonic-gate char *ppaths = NULL; /* ptr to all of the templates */
3367c478bd9Sstevel@tonic-gate char *pathname = NULL; /* the full pathname to the file */
3377c478bd9Sstevel@tonic-gate size_t nlspath_len, domain_len, locale_len, path_len;
3387c478bd9Sstevel@tonic-gate size_t ppaths_len = 0;
3397c478bd9Sstevel@tonic-gate Nlstmp *nlstmp = NULL;
3407c478bd9Sstevel@tonic-gate Nlstmp *pnlstmp, *qnlstmp;
341f13ac639Smuffin Nls_node *cur_nls, *nnp;
3427c478bd9Sstevel@tonic-gate Gettext_t *gt = global_gt;
3437c478bd9Sstevel@tonic-gate
3447c478bd9Sstevel@tonic-gate #ifdef GETTEXT_DEBUG
345f13ac639Smuffin gprintf(0, "*************** process_nlspath(%s, %s, "
346f13ac639Smuffin "%s, 0x%p)\n", cur_domain,
3477c478bd9Sstevel@tonic-gate cur_msgloc, nlspath, (void *)binding);
3487c478bd9Sstevel@tonic-gate #endif
3497c478bd9Sstevel@tonic-gate
3507c478bd9Sstevel@tonic-gate cur_nls = gt->c_n_node;
3517c478bd9Sstevel@tonic-gate if (cur_nls &&
352f13ac639Smuffin (strcmp(cur_nls->domain, cur_domain) == 0 &&
353f13ac639Smuffin strcmp(cur_nls->locale, cur_msgloc) == 0 &&
354f13ac639Smuffin strcmp(cur_nls->nlspath, nlspath) == 0)) {
3557c478bd9Sstevel@tonic-gate *binding = cur_nls->ppaths;
3567c478bd9Sstevel@tonic-gate return (1);
3577c478bd9Sstevel@tonic-gate }
3587c478bd9Sstevel@tonic-gate
359f13ac639Smuffin nnp = gt->n_node;
360f13ac639Smuffin while (nnp) {
361f13ac639Smuffin if (strcmp(nnp->domain, cur_domain) == 0 &&
362f13ac639Smuffin strcmp(nnp->locale, cur_msgloc) == 0 &&
363f13ac639Smuffin strcmp(nnp->nlspath, nlspath) == 0) {
364f13ac639Smuffin /* found */
365f13ac639Smuffin gt->c_n_node = nnp;
366f13ac639Smuffin *binding = nnp->ppaths;
367f13ac639Smuffin return (1);
3687c478bd9Sstevel@tonic-gate }
369f13ac639Smuffin nnp = nnp->next;
3707c478bd9Sstevel@tonic-gate }
371f13ac639Smuffin /* not found */
3727c478bd9Sstevel@tonic-gate
373f13ac639Smuffin nnp = calloc(1, sizeof (Nls_node));
374f13ac639Smuffin if (nnp == NULL) {
375f13ac639Smuffin ALLFREE;
376f13ac639Smuffin return (-1);
3777c478bd9Sstevel@tonic-gate }
3787c478bd9Sstevel@tonic-gate
3797c478bd9Sstevel@tonic-gate nlspath_len = strlen(nlspath);
3807c478bd9Sstevel@tonic-gate locale_len = strlen(cur_msgloc);
3817c478bd9Sstevel@tonic-gate domain_len = strlen(cur_domain);
3827c478bd9Sstevel@tonic-gate
383f13ac639Smuffin lang = s = strdup(cur_msgloc);
384f13ac639Smuffin if (lang == NULL) {
385f13ac639Smuffin ALLFREE;
386f13ac639Smuffin return (-1);
387f13ac639Smuffin }
388f13ac639Smuffin s1 = s2 = NULL;
389f13ac639Smuffin while (*s) {
390f13ac639Smuffin if (*s == '_') {
391f13ac639Smuffin s1 = s;
392f13ac639Smuffin *s1++ = '\0';
393f13ac639Smuffin } else if (*s == '.') {
394f13ac639Smuffin s2 = s;
395f13ac639Smuffin *s2++ = '\0';
3967c478bd9Sstevel@tonic-gate }
397f13ac639Smuffin s++;
3987c478bd9Sstevel@tonic-gate }
399f13ac639Smuffin territory = s1;
400f13ac639Smuffin codeset = s2;
4017c478bd9Sstevel@tonic-gate
4027c478bd9Sstevel@tonic-gate /*
4037c478bd9Sstevel@tonic-gate * now that we have the name (domain), we first look through NLSPATH,
4047c478bd9Sstevel@tonic-gate * in an attempt to get the locale. A locale may be completely
4057c478bd9Sstevel@tonic-gate * specified as "language_territory.codeset". NLSPATH consists
4067c478bd9Sstevel@tonic-gate * of templates separated by ":" characters. The following are
4077c478bd9Sstevel@tonic-gate * the substitution values within NLSPATH:
4087c478bd9Sstevel@tonic-gate * %N = DEFAULT_DOMAIN
4097c478bd9Sstevel@tonic-gate * %L = The value of the LC_MESSAGES category.
4107c478bd9Sstevel@tonic-gate * %I = The language element from the LC_MESSAGES category.
4117c478bd9Sstevel@tonic-gate * %t = The territory element from the LC_MESSAGES category.
4127c478bd9Sstevel@tonic-gate * %c = The codeset element from the LC_MESSAGES category.
4137c478bd9Sstevel@tonic-gate * %% = A single character.
4147c478bd9Sstevel@tonic-gate * if we find one of these characters, we will carry out the
4157c478bd9Sstevel@tonic-gate * appropriate substitution.
4167c478bd9Sstevel@tonic-gate */
417f13ac639Smuffin pathname = malloc(MAXPATHLEN);
4187c478bd9Sstevel@tonic-gate if (pathname == NULL) {
4197c478bd9Sstevel@tonic-gate ALLFREE;
4207c478bd9Sstevel@tonic-gate return (-1);
4217c478bd9Sstevel@tonic-gate }
4227c478bd9Sstevel@tonic-gate s = (char *)nlspath; /* s has a content of NLSPATH */
4237c478bd9Sstevel@tonic-gate while (*s) { /* march through NLSPATH */
4247c478bd9Sstevel@tonic-gate (void) memset(pathname, 0, MAXPATHLEN);
4257c478bd9Sstevel@tonic-gate if (*s == ':') {
4267c478bd9Sstevel@tonic-gate /*
4277c478bd9Sstevel@tonic-gate * this loop only occurs if we have to replace
4287c478bd9Sstevel@tonic-gate * ":" by "name". replace_nls_option() below
4297c478bd9Sstevel@tonic-gate * will handle the subsequent ":"'s.
4307c478bd9Sstevel@tonic-gate */
431f13ac639Smuffin pnlstmp = malloc(sizeof (Nlstmp));
432f13ac639Smuffin if (pnlstmp == NULL) {
4337c478bd9Sstevel@tonic-gate ALLFREE;
4347c478bd9Sstevel@tonic-gate return (-1);
4357c478bd9Sstevel@tonic-gate }
4367c478bd9Sstevel@tonic-gate
4377c478bd9Sstevel@tonic-gate (void) memcpy(pnlstmp->pathname, cur_domain,
438f13ac639Smuffin domain_len + 1);
439f13ac639Smuffin pnlstmp->len = domain_len;
440f13ac639Smuffin ppaths_len += domain_len + 1; /* 1 for ':' */
441f13ac639Smuffin
4427c478bd9Sstevel@tonic-gate
4437c478bd9Sstevel@tonic-gate pnlstmp->next = NULL;
4447c478bd9Sstevel@tonic-gate
445f13ac639Smuffin if (nlstmp == NULL) {
4467c478bd9Sstevel@tonic-gate nlstmp = pnlstmp;
4477c478bd9Sstevel@tonic-gate qnlstmp = pnlstmp;
4487c478bd9Sstevel@tonic-gate } else {
4497c478bd9Sstevel@tonic-gate qnlstmp->next = pnlstmp;
4507c478bd9Sstevel@tonic-gate qnlstmp = pnlstmp;
4517c478bd9Sstevel@tonic-gate }
4527c478bd9Sstevel@tonic-gate
4537c478bd9Sstevel@tonic-gate ++s;
4547c478bd9Sstevel@tonic-gate continue;
4557c478bd9Sstevel@tonic-gate }
4567c478bd9Sstevel@tonic-gate /* replace Substitution field */
4577c478bd9Sstevel@tonic-gate s = replace_nls_option(s, cur_domain, pathname,
458f13ac639Smuffin (char *)cur_msgloc, lang, territory, codeset);
4597c478bd9Sstevel@tonic-gate
4607c478bd9Sstevel@tonic-gate if (s == NULL) {
4617c478bd9Sstevel@tonic-gate ALLFREE;
4627c478bd9Sstevel@tonic-gate return (-1);
4637c478bd9Sstevel@tonic-gate }
4647c478bd9Sstevel@tonic-gate
4657c478bd9Sstevel@tonic-gate /* if we've found a valid file: */
4667c478bd9Sstevel@tonic-gate if (*pathname) {
4677c478bd9Sstevel@tonic-gate /* add template to end of chain of pathnames: */
468f13ac639Smuffin pnlstmp = malloc(sizeof (Nlstmp));
469f13ac639Smuffin if (pnlstmp == NULL) {
4707c478bd9Sstevel@tonic-gate ALLFREE;
4717c478bd9Sstevel@tonic-gate return (-1);
4727c478bd9Sstevel@tonic-gate }
4737c478bd9Sstevel@tonic-gate
474f13ac639Smuffin path_len = strlen(pathname);
4757c478bd9Sstevel@tonic-gate (void) memcpy(pnlstmp->pathname, pathname,
476f13ac639Smuffin path_len + 1);
477f13ac639Smuffin pnlstmp->len = path_len;
478f13ac639Smuffin ppaths_len += path_len + 1; /* 1 for ':' */
4797c478bd9Sstevel@tonic-gate
4807c478bd9Sstevel@tonic-gate pnlstmp->next = NULL;
4817c478bd9Sstevel@tonic-gate
482f13ac639Smuffin if (nlstmp == NULL) {
4837c478bd9Sstevel@tonic-gate nlstmp = pnlstmp;
4847c478bd9Sstevel@tonic-gate qnlstmp = pnlstmp;
4857c478bd9Sstevel@tonic-gate } else {
4867c478bd9Sstevel@tonic-gate qnlstmp->next = pnlstmp;
4877c478bd9Sstevel@tonic-gate qnlstmp = pnlstmp;
4887c478bd9Sstevel@tonic-gate }
4897c478bd9Sstevel@tonic-gate }
4907c478bd9Sstevel@tonic-gate if (*s) {
4917c478bd9Sstevel@tonic-gate ++s;
4927c478bd9Sstevel@tonic-gate }
4937c478bd9Sstevel@tonic-gate }
4947c478bd9Sstevel@tonic-gate /*
4957c478bd9Sstevel@tonic-gate * now that we've handled the pathname templates, concatenate them
4967c478bd9Sstevel@tonic-gate * all into the form "template1:template2:..." for _bindtextdomain_u()
4977c478bd9Sstevel@tonic-gate */
4987c478bd9Sstevel@tonic-gate
4997c478bd9Sstevel@tonic-gate if (ppaths_len != 0) {
500f13ac639Smuffin ppaths = malloc(ppaths_len + 1);
501f13ac639Smuffin if (ppaths == NULL) {
5027c478bd9Sstevel@tonic-gate ALLFREE;
5037c478bd9Sstevel@tonic-gate return (-1);
5047c478bd9Sstevel@tonic-gate }
5057c478bd9Sstevel@tonic-gate *ppaths = '\0';
5067c478bd9Sstevel@tonic-gate } else {
5077c478bd9Sstevel@tonic-gate ALLFREE;
5087c478bd9Sstevel@tonic-gate return (0);
5097c478bd9Sstevel@tonic-gate }
5107c478bd9Sstevel@tonic-gate
5117c478bd9Sstevel@tonic-gate /*
5127c478bd9Sstevel@tonic-gate * extract the path templates (fifo), and concatenate them
5137c478bd9Sstevel@tonic-gate * all into a ":" separated string for _bindtextdomain_u()
5147c478bd9Sstevel@tonic-gate */
5157c478bd9Sstevel@tonic-gate pnlstmp = nlstmp;
516f13ac639Smuffin s = ppaths;
5177c478bd9Sstevel@tonic-gate while (pnlstmp) {
518f13ac639Smuffin (void) memcpy(s, pnlstmp->pathname, pnlstmp->len);
519f13ac639Smuffin s += pnlstmp->len;
520f13ac639Smuffin *s++ = ':';
5217c478bd9Sstevel@tonic-gate qnlstmp = pnlstmp->next;
5227c478bd9Sstevel@tonic-gate free(pnlstmp);
5237c478bd9Sstevel@tonic-gate pnlstmp = qnlstmp;
5247c478bd9Sstevel@tonic-gate }
525f13ac639Smuffin *s = '\0';
5267c478bd9Sstevel@tonic-gate nlstmp = NULL;
5277c478bd9Sstevel@tonic-gate
528f13ac639Smuffin nnp->domain = malloc(domain_len + 1);
529f13ac639Smuffin if (nnp->domain == NULL) {
5307c478bd9Sstevel@tonic-gate ALLFREE;
5317c478bd9Sstevel@tonic-gate return (-1);
5327c478bd9Sstevel@tonic-gate } else {
533f13ac639Smuffin (void) memcpy(nnp->domain, cur_domain, domain_len + 1);
5347c478bd9Sstevel@tonic-gate }
535f13ac639Smuffin nnp->locale = malloc(locale_len + 1);
536f13ac639Smuffin if (nnp->locale == NULL) {
5377c478bd9Sstevel@tonic-gate ALLFREE;
5387c478bd9Sstevel@tonic-gate return (-1);
5397c478bd9Sstevel@tonic-gate } else {
540f13ac639Smuffin (void) memcpy(nnp->locale, cur_msgloc, locale_len + 1);
5417c478bd9Sstevel@tonic-gate }
542f13ac639Smuffin nnp->nlspath = malloc(nlspath_len + 1);
543f13ac639Smuffin if (nnp->nlspath == NULL) {
5447c478bd9Sstevel@tonic-gate ALLFREE;
5457c478bd9Sstevel@tonic-gate return (-1);
5467c478bd9Sstevel@tonic-gate } else {
547f13ac639Smuffin (void) memcpy(nnp->nlspath, nlspath, nlspath_len + 1);
5487c478bd9Sstevel@tonic-gate }
5497c478bd9Sstevel@tonic-gate nnp->ppaths = ppaths;
550f13ac639Smuffin
551f13ac639Smuffin nnp->next = gt->n_node;
552f13ac639Smuffin gt->n_node = nnp;
5537c478bd9Sstevel@tonic-gate gt->c_n_node = nnp;
5547c478bd9Sstevel@tonic-gate
5557c478bd9Sstevel@tonic-gate free(pathname);
5567c478bd9Sstevel@tonic-gate free(lang);
5577c478bd9Sstevel@tonic-gate #ifdef GETTEXT_DEBUG
558f13ac639Smuffin gprintf(0, "*************** existing process_nlspath with success\n");
559f13ac639Smuffin gprintf(0, " binding: \"%s\"\n", ppaths);
5607c478bd9Sstevel@tonic-gate #endif
5617c478bd9Sstevel@tonic-gate *binding = ppaths;
5627c478bd9Sstevel@tonic-gate return (1);
5637c478bd9Sstevel@tonic-gate }
5647c478bd9Sstevel@tonic-gate
5657c478bd9Sstevel@tonic-gate
5667c478bd9Sstevel@tonic-gate /*
5677c478bd9Sstevel@tonic-gate * This routine will replace substitution parameters in NLSPATH
5687c478bd9Sstevel@tonic-gate * with appropiate values.
5697c478bd9Sstevel@tonic-gate */
5707c478bd9Sstevel@tonic-gate static char *
replace_nls_option(char * s,const char * name,char * pathname,char * locale,char * lang,char * territory,char * codeset)5717c478bd9Sstevel@tonic-gate replace_nls_option(char *s, const char *name, char *pathname,
572f13ac639Smuffin char *locale, char *lang, char *territory, char *codeset)
5737c478bd9Sstevel@tonic-gate {
5747c478bd9Sstevel@tonic-gate char *t, *u;
5757c478bd9Sstevel@tonic-gate char *limit;
5767c478bd9Sstevel@tonic-gate
5777c478bd9Sstevel@tonic-gate t = pathname;
5787c478bd9Sstevel@tonic-gate limit = pathname + MAXPATHLEN - 1;
5797c478bd9Sstevel@tonic-gate
5807c478bd9Sstevel@tonic-gate while (*s && *s != ':') {
5817c478bd9Sstevel@tonic-gate if (t < limit) {
5827c478bd9Sstevel@tonic-gate /*
5837c478bd9Sstevel@tonic-gate * %% is considered a single % character (XPG).
5847c478bd9Sstevel@tonic-gate * %L : LC_MESSAGES (XPG4) LANG(XPG3)
5857c478bd9Sstevel@tonic-gate * %l : The language element from the current locale.
5867c478bd9Sstevel@tonic-gate * (XPG3, XPG4)
5877c478bd9Sstevel@tonic-gate */
5887c478bd9Sstevel@tonic-gate if (*s != '%')
5897c478bd9Sstevel@tonic-gate *t++ = *s;
5907c478bd9Sstevel@tonic-gate else if (*++s == 'N') {
5917c478bd9Sstevel@tonic-gate if (name) {
5927c478bd9Sstevel@tonic-gate u = (char *)name;
5937c478bd9Sstevel@tonic-gate while (*u && (t < limit))
5947c478bd9Sstevel@tonic-gate *t++ = *u++;
5957c478bd9Sstevel@tonic-gate }
5967c478bd9Sstevel@tonic-gate } else if (*s == 'L') {
5977c478bd9Sstevel@tonic-gate if (locale) {
5987c478bd9Sstevel@tonic-gate u = locale;
5997c478bd9Sstevel@tonic-gate while (*u && (t < limit))
6007c478bd9Sstevel@tonic-gate *t++ = *u++;
6017c478bd9Sstevel@tonic-gate }
6027c478bd9Sstevel@tonic-gate } else if (*s == 'l') {
6037c478bd9Sstevel@tonic-gate if (lang) {
6047c478bd9Sstevel@tonic-gate u = lang;
6057c478bd9Sstevel@tonic-gate while (*u && (*u != '_') &&
606f13ac639Smuffin (t < limit))
6077c478bd9Sstevel@tonic-gate *t++ = *u++;
6087c478bd9Sstevel@tonic-gate }
6097c478bd9Sstevel@tonic-gate } else if (*s == 't') {
6107c478bd9Sstevel@tonic-gate if (territory) {
6117c478bd9Sstevel@tonic-gate u = territory;
6127c478bd9Sstevel@tonic-gate while (*u && (*u != '.') &&
613f13ac639Smuffin (t < limit))
6147c478bd9Sstevel@tonic-gate *t++ = *u++;
6157c478bd9Sstevel@tonic-gate }
6167c478bd9Sstevel@tonic-gate } else if (*s == 'c') {
6177c478bd9Sstevel@tonic-gate if (codeset) {
6187c478bd9Sstevel@tonic-gate u = codeset;
6197c478bd9Sstevel@tonic-gate while (*u && (t < limit))
6207c478bd9Sstevel@tonic-gate *t++ = *u++;
6217c478bd9Sstevel@tonic-gate }
6227c478bd9Sstevel@tonic-gate } else {
6237c478bd9Sstevel@tonic-gate if (t < limit)
6247c478bd9Sstevel@tonic-gate *t++ = *s;
6257c478bd9Sstevel@tonic-gate }
6267c478bd9Sstevel@tonic-gate } else {
6277c478bd9Sstevel@tonic-gate /* too long pathname */
6287c478bd9Sstevel@tonic-gate return (NULL);
6297c478bd9Sstevel@tonic-gate }
6307c478bd9Sstevel@tonic-gate ++s;
6317c478bd9Sstevel@tonic-gate }
6327c478bd9Sstevel@tonic-gate *t = '\0';
6337c478bd9Sstevel@tonic-gate return (s);
6347c478bd9Sstevel@tonic-gate }
6357c478bd9Sstevel@tonic-gate
6367c478bd9Sstevel@tonic-gate
6377c478bd9Sstevel@tonic-gate char *
_real_bindtextdomain_u(const char * domain,const char * binding,int type)6387c478bd9Sstevel@tonic-gate _real_bindtextdomain_u(const char *domain, const char *binding,
639f13ac639Smuffin int type)
6407c478bd9Sstevel@tonic-gate {
6417c478bd9Sstevel@tonic-gate struct domain_binding *bind, *prev;
6427c478bd9Sstevel@tonic-gate Gettext_t *gt = global_gt;
6437c478bd9Sstevel@tonic-gate char **binding_addr;
6447c478bd9Sstevel@tonic-gate
6457c478bd9Sstevel@tonic-gate #ifdef GETTEXT_DEBUG
646f13ac639Smuffin gprintf(0, "*************** _real_bindtextdomain_u(\"%s\", "
647f13ac639Smuffin "\"%s\", \"%s\")\n",
648f13ac639Smuffin (domain ? domain : ""),
649f13ac639Smuffin (binding ? binding : ""),
650f13ac639Smuffin (type == TP_BINDING) ? "TP_BINDING" : "TP_CODESET");
6517c478bd9Sstevel@tonic-gate #endif
6527c478bd9Sstevel@tonic-gate
6537c478bd9Sstevel@tonic-gate /*
6547c478bd9Sstevel@tonic-gate * If domain is a NULL pointer, no change will occur regardless
6557c478bd9Sstevel@tonic-gate * of binding value. Just return NULL.
6567c478bd9Sstevel@tonic-gate */
657f13ac639Smuffin if (domain == NULL) {
6587c478bd9Sstevel@tonic-gate return (NULL);
6597c478bd9Sstevel@tonic-gate }
6607c478bd9Sstevel@tonic-gate
6617c478bd9Sstevel@tonic-gate /*
6627c478bd9Sstevel@tonic-gate * Global Binding is not supported any more.
6637c478bd9Sstevel@tonic-gate * Just return NULL if domain is NULL string.
6647c478bd9Sstevel@tonic-gate */
6657c478bd9Sstevel@tonic-gate if (*domain == '\0') {
6667c478bd9Sstevel@tonic-gate return (NULL);
6677c478bd9Sstevel@tonic-gate }
6687c478bd9Sstevel@tonic-gate
6697c478bd9Sstevel@tonic-gate /* linear search for binding, rebind if found, add if not */
6707c478bd9Sstevel@tonic-gate bind = FIRSTBIND(gt);
6717c478bd9Sstevel@tonic-gate prev = NULL; /* Two pointers needed for pointer operations */
6727c478bd9Sstevel@tonic-gate
6737c478bd9Sstevel@tonic-gate while (bind) {
6747c478bd9Sstevel@tonic-gate if (strcmp(domain, bind->domain) == 0) {
6757c478bd9Sstevel@tonic-gate /*
6767c478bd9Sstevel@tonic-gate * Domain found.
6777c478bd9Sstevel@tonic-gate */
6787c478bd9Sstevel@tonic-gate binding_addr = (type == TP_BINDING) ? &(bind->binding) :
679f13ac639Smuffin &(bind->codeset);
680f13ac639Smuffin if (binding == NULL) {
6817c478bd9Sstevel@tonic-gate /*
6827c478bd9Sstevel@tonic-gate * if binding is null, then query
6837c478bd9Sstevel@tonic-gate */
6847c478bd9Sstevel@tonic-gate return (*binding_addr);
6857c478bd9Sstevel@tonic-gate }
6867c478bd9Sstevel@tonic-gate /* replace existing binding with new binding */
687*0a9a25a2SRichard Hansen char *new_binding = strdup(binding);
688*0a9a25a2SRichard Hansen if (new_binding == NULL) {
6897c478bd9Sstevel@tonic-gate return (NULL);
6907c478bd9Sstevel@tonic-gate }
691*0a9a25a2SRichard Hansen free(*binding_addr);
692*0a9a25a2SRichard Hansen *binding_addr = new_binding;
6937c478bd9Sstevel@tonic-gate #ifdef GETTEXT_DEBUG
6947c478bd9Sstevel@tonic-gate printlist();
6957c478bd9Sstevel@tonic-gate #endif
6967c478bd9Sstevel@tonic-gate return (*binding_addr);
6977c478bd9Sstevel@tonic-gate }
6987c478bd9Sstevel@tonic-gate prev = bind;
6997c478bd9Sstevel@tonic-gate bind = bind->next;
7007c478bd9Sstevel@tonic-gate } /* while (bind) */
7017c478bd9Sstevel@tonic-gate
7027c478bd9Sstevel@tonic-gate /* domain has not been found in the list at this point */
7037c478bd9Sstevel@tonic-gate if (binding) {
7047c478bd9Sstevel@tonic-gate /*
7057c478bd9Sstevel@tonic-gate * domain is not found, but binding is not NULL.
7067c478bd9Sstevel@tonic-gate * Then add a new node to the end of linked list.
7077c478bd9Sstevel@tonic-gate */
7087c478bd9Sstevel@tonic-gate
709f13ac639Smuffin if ((bind = malloc(sizeof (Dbinding))) == NULL) {
7107c478bd9Sstevel@tonic-gate return (NULL);
7117c478bd9Sstevel@tonic-gate }
7127c478bd9Sstevel@tonic-gate if ((bind->domain = strdup(domain)) == NULL) {
7137c478bd9Sstevel@tonic-gate free(bind);
7147c478bd9Sstevel@tonic-gate return (NULL);
7157c478bd9Sstevel@tonic-gate }
7167c478bd9Sstevel@tonic-gate bind->binding = NULL;
7177c478bd9Sstevel@tonic-gate bind->codeset = NULL;
7187c478bd9Sstevel@tonic-gate binding_addr = (type == TP_BINDING) ? &(bind->binding) :
719f13ac639Smuffin &(bind->codeset);
7207c478bd9Sstevel@tonic-gate if ((*binding_addr = strdup(binding)) == NULL) {
7217c478bd9Sstevel@tonic-gate free(bind->domain);
7227c478bd9Sstevel@tonic-gate free(bind);
7237c478bd9Sstevel@tonic-gate return (NULL);
7247c478bd9Sstevel@tonic-gate }
7257c478bd9Sstevel@tonic-gate bind->next = NULL;
7267c478bd9Sstevel@tonic-gate
7277c478bd9Sstevel@tonic-gate if (prev) {
7287c478bd9Sstevel@tonic-gate /* reached the end of list */
7297c478bd9Sstevel@tonic-gate prev->next = bind;
7307c478bd9Sstevel@tonic-gate } else {
7317c478bd9Sstevel@tonic-gate /* list was empty */
7327c478bd9Sstevel@tonic-gate FIRSTBIND(gt) = bind;
7337c478bd9Sstevel@tonic-gate }
7347c478bd9Sstevel@tonic-gate
7357c478bd9Sstevel@tonic-gate #ifdef GETTEXT_DEBUG
7367c478bd9Sstevel@tonic-gate printlist();
7377c478bd9Sstevel@tonic-gate #endif
7387c478bd9Sstevel@tonic-gate return (*binding_addr);
7397c478bd9Sstevel@tonic-gate } else {
7407c478bd9Sstevel@tonic-gate /*
7417c478bd9Sstevel@tonic-gate * Query of domain which is not found in the list
7427c478bd9Sstevel@tonic-gate * for bindtextdomain, returns defaultbind
7437c478bd9Sstevel@tonic-gate * for bind_textdomain_codeset, returns NULL
7447c478bd9Sstevel@tonic-gate */
7457c478bd9Sstevel@tonic-gate if (type == TP_BINDING) {
7467c478bd9Sstevel@tonic-gate return ((char *)defaultbind);
7477c478bd9Sstevel@tonic-gate } else {
7487c478bd9Sstevel@tonic-gate return (NULL);
7497c478bd9Sstevel@tonic-gate }
7507c478bd9Sstevel@tonic-gate } /* if (binding) */
7517c478bd9Sstevel@tonic-gate
7527c478bd9Sstevel@tonic-gate /* Must not reach here */
7537c478bd9Sstevel@tonic-gate
7547c478bd9Sstevel@tonic-gate } /* _real_bindtextdomain_u */
7557c478bd9Sstevel@tonic-gate
7567c478bd9Sstevel@tonic-gate
7577c478bd9Sstevel@tonic-gate char *
_textdomain_u(const char * domain,char * result)7587c478bd9Sstevel@tonic-gate _textdomain_u(const char *domain, char *result)
7597c478bd9Sstevel@tonic-gate {
7607c478bd9Sstevel@tonic-gate char *p;
7617c478bd9Sstevel@tonic-gate size_t domain_len;
7627c478bd9Sstevel@tonic-gate Gettext_t *gt = global_gt;
7637c478bd9Sstevel@tonic-gate
7647c478bd9Sstevel@tonic-gate #ifdef GETTEXT_DEBUG
765f13ac639Smuffin gprintf(0, "*************** _textdomain_u(\"%s\", 0x%p)\n",
766f13ac639Smuffin (domain ? domain : ""), (void *)result);
7677c478bd9Sstevel@tonic-gate #endif
7687c478bd9Sstevel@tonic-gate
7697c478bd9Sstevel@tonic-gate /* Query is performed for NULL domain pointer */
7707c478bd9Sstevel@tonic-gate if (domain == NULL) {
771f13ac639Smuffin (void) strcpy(result, CURRENT_DOMAIN(gt));
7727c478bd9Sstevel@tonic-gate return (result);
7737c478bd9Sstevel@tonic-gate }
7747c478bd9Sstevel@tonic-gate
7757c478bd9Sstevel@tonic-gate /* check for error. */
7767c478bd9Sstevel@tonic-gate /*
7777c478bd9Sstevel@tonic-gate * domain is limited to TEXTDOMAINMAX bytes
7787c478bd9Sstevel@tonic-gate * excluding a null termination.
7797c478bd9Sstevel@tonic-gate */
780f13ac639Smuffin domain_len = strlen(domain);
7817c478bd9Sstevel@tonic-gate if (domain_len > TEXTDOMAINMAX) {
7827c478bd9Sstevel@tonic-gate /* too long */
7837c478bd9Sstevel@tonic-gate return (NULL);
7847c478bd9Sstevel@tonic-gate }
7857c478bd9Sstevel@tonic-gate
7867c478bd9Sstevel@tonic-gate /*
7877c478bd9Sstevel@tonic-gate * Calling textdomain() with a null domain string sets
7887c478bd9Sstevel@tonic-gate * the domain to the default domain.
7897c478bd9Sstevel@tonic-gate * If non-null string is passwd, current domain is changed
7907c478bd9Sstevel@tonic-gate * to the new domain.
7917c478bd9Sstevel@tonic-gate */
7927c478bd9Sstevel@tonic-gate
7937c478bd9Sstevel@tonic-gate /* actually this if clause should be protected from signals */
7947c478bd9Sstevel@tonic-gate if (*domain == '\0') {
7957c478bd9Sstevel@tonic-gate if (CURRENT_DOMAIN(gt) != default_domain) {
7967c478bd9Sstevel@tonic-gate free(CURRENT_DOMAIN(gt));
7977c478bd9Sstevel@tonic-gate CURRENT_DOMAIN(gt) = (char *)default_domain;
7987c478bd9Sstevel@tonic-gate }
7997c478bd9Sstevel@tonic-gate } else {
800f13ac639Smuffin p = malloc(domain_len + 1);
801f13ac639Smuffin if (p == NULL)
8027c478bd9Sstevel@tonic-gate return (NULL);
803f13ac639Smuffin (void) strcpy(p, domain);
8047c478bd9Sstevel@tonic-gate if (CURRENT_DOMAIN(gt) != default_domain)
8057c478bd9Sstevel@tonic-gate free(CURRENT_DOMAIN(gt));
8067c478bd9Sstevel@tonic-gate CURRENT_DOMAIN(gt) = p;
8077c478bd9Sstevel@tonic-gate }
8087c478bd9Sstevel@tonic-gate
809f13ac639Smuffin (void) strcpy(result, CURRENT_DOMAIN(gt));
8107c478bd9Sstevel@tonic-gate return (result);
8117c478bd9Sstevel@tonic-gate } /* _textdomain_u */
8127c478bd9Sstevel@tonic-gate
8137c478bd9Sstevel@tonic-gate /*
8147c478bd9Sstevel@tonic-gate * key_2_text() translates msd_id into target string.
8157c478bd9Sstevel@tonic-gate */
8167c478bd9Sstevel@tonic-gate static char *
key_2_text(Msg_s_node * messages,const char * key_string)8177c478bd9Sstevel@tonic-gate key_2_text(Msg_s_node *messages, const char *key_string)
8187c478bd9Sstevel@tonic-gate {
8197c478bd9Sstevel@tonic-gate int val;
8207c478bd9Sstevel@tonic-gate char *msg_id_str;
8217c478bd9Sstevel@tonic-gate unsigned char kc = *(unsigned char *)key_string;
8227c478bd9Sstevel@tonic-gate struct msg_struct *check_msg_list;
8237c478bd9Sstevel@tonic-gate
8247c478bd9Sstevel@tonic-gate #ifdef GETTEXT_DEBUG
825f13ac639Smuffin gprintf(0, "*************** key_2_text(0x%p, \"%s\")\n",
826f13ac639Smuffin (void *)messages, key_string ? key_string : "(null)");
827f13ac639Smuffin printsunmsg(messages, 1);
8287c478bd9Sstevel@tonic-gate #endif
8297c478bd9Sstevel@tonic-gate
8307c478bd9Sstevel@tonic-gate check_msg_list = messages->msg_list +
831f13ac639Smuffin messages->msg_file_info->msg_mid;
8327c478bd9Sstevel@tonic-gate for (;;) {
8337c478bd9Sstevel@tonic-gate msg_id_str = messages->msg_ids +
834f13ac639Smuffin check_msg_list->msgid_offset;
8357c478bd9Sstevel@tonic-gate /*
8367c478bd9Sstevel@tonic-gate * To maintain the compatibility with Zeus mo file,
8377c478bd9Sstevel@tonic-gate * msg_id's are stored in descending order.
8387c478bd9Sstevel@tonic-gate * If the ascending order is desired, change "msgfmt.c"
8397c478bd9Sstevel@tonic-gate * and switch msg_id_str and key_string in the following
8407c478bd9Sstevel@tonic-gate * strcmp() statement.
8417c478bd9Sstevel@tonic-gate */
8427c478bd9Sstevel@tonic-gate val = *(unsigned char *)msg_id_str - kc;
8437c478bd9Sstevel@tonic-gate if ((val == 0) &&
844f13ac639Smuffin (val = strcmp(msg_id_str, key_string)) == 0) {
8457c478bd9Sstevel@tonic-gate return (messages->msg_strs
846f13ac639Smuffin + check_msg_list->msgstr_offset);
8477c478bd9Sstevel@tonic-gate } else if (val < 0) {
8487c478bd9Sstevel@tonic-gate if (check_msg_list->less != LEAFINDICATOR) {
8497c478bd9Sstevel@tonic-gate check_msg_list = messages->msg_list +
850f13ac639Smuffin check_msg_list->less;
8517c478bd9Sstevel@tonic-gate continue;
8527c478bd9Sstevel@tonic-gate }
8537c478bd9Sstevel@tonic-gate return ((char *)key_string);
8547c478bd9Sstevel@tonic-gate } else {
8557c478bd9Sstevel@tonic-gate /* val > 0 */
8567c478bd9Sstevel@tonic-gate if (check_msg_list->more != LEAFINDICATOR) {
8577c478bd9Sstevel@tonic-gate check_msg_list = messages->msg_list +
858f13ac639Smuffin check_msg_list->more;
8597c478bd9Sstevel@tonic-gate continue;
8607c478bd9Sstevel@tonic-gate }
8617c478bd9Sstevel@tonic-gate return ((char *)key_string);
8627c478bd9Sstevel@tonic-gate }
8637c478bd9Sstevel@tonic-gate }
8647c478bd9Sstevel@tonic-gate }
8657c478bd9Sstevel@tonic-gate
866f13ac639Smuffin /*
867f13ac639Smuffin * sun_setmsg
868f13ac639Smuffin *
869f13ac639Smuffin * INPUT
870f13ac639Smuffin * mnp - message node
871f13ac639Smuffin * addr - address to the mmapped file
872f13ac639Smuffin * size - size of the file
873f13ac639Smuffin *
874f13ac639Smuffin * RETURN
875f13ac639Smuffin * 0 - either T_SUN_MO or T_ILL_MO has been set
876f13ac639Smuffin * 1 - not a valid sun mo file
877f13ac639Smuffin * -1 - failed
878f13ac639Smuffin */
879f13ac639Smuffin static int
sun_setmsg(Msg_node * mnp,char * addr,size_t size)880f13ac639Smuffin sun_setmsg(Msg_node *mnp, char *addr, size_t size)
881f13ac639Smuffin {
882f13ac639Smuffin struct msg_info *sun_header;
883f13ac639Smuffin Msg_s_node *p;
884f13ac639Smuffin uint32_t first_4bytes;
885f13ac639Smuffin int mid, count;
886f13ac639Smuffin int struct_size, struct_size_old;
887f13ac639Smuffin int msg_struct_size;
888f13ac639Smuffin
889f13ac639Smuffin if (size < sizeof (struct msg_info)) {
890f13ac639Smuffin /* invalid mo file */
891f13ac639Smuffin mnp->type = T_ILL_MO;
892f13ac639Smuffin #ifdef GETTEXT_DEBUG
893f13ac639Smuffin gprintf(0, "********* exiting sun_setmsg\n");
894f13ac639Smuffin printmnp(mnp, 1);
895f13ac639Smuffin #endif
896f13ac639Smuffin return (0);
897f13ac639Smuffin }
898f13ac639Smuffin
899f13ac639Smuffin first_4bytes = *((uint32_t *)(uintptr_t)addr);
900f13ac639Smuffin if (first_4bytes > INT_MAX) {
901f13ac639Smuffin /*
902f13ac639Smuffin * Not a valid sun mo file
903f13ac639Smuffin */
904f13ac639Smuffin return (1);
905f13ac639Smuffin }
906f13ac639Smuffin
907f13ac639Smuffin /* candidate for sun mo */
908f13ac639Smuffin
909f13ac639Smuffin sun_header = (struct msg_info *)(uintptr_t)addr;
910f13ac639Smuffin mid = sun_header->msg_mid;
911f13ac639Smuffin count = sun_header->msg_count;
912f13ac639Smuffin msg_struct_size = sun_header->msg_struct_size;
913f13ac639Smuffin struct_size_old = (int)(OLD_MSG_STRUCT_SIZE * count);
914f13ac639Smuffin struct_size = (int)(MSG_STRUCT_SIZE * count);
915f13ac639Smuffin
916f13ac639Smuffin if ((((count - 1) / 2) != mid) ||
917f13ac639Smuffin ((msg_struct_size != struct_size_old) &&
918f13ac639Smuffin (msg_struct_size != struct_size))) {
919f13ac639Smuffin /* invalid mo file */
920f13ac639Smuffin mnp->type = T_ILL_MO;
921f13ac639Smuffin #ifdef GETTEXT_DEBUG
922f13ac639Smuffin gprintf(0, "********* exiting sun_setmsg\n");
923f13ac639Smuffin printmnp(mnp, 1);
924f13ac639Smuffin #endif
925f13ac639Smuffin return (0);
926f13ac639Smuffin }
927f13ac639Smuffin /* valid sun mo file */
928f13ac639Smuffin
929f13ac639Smuffin p = malloc(sizeof (Msg_s_node));
930f13ac639Smuffin if (p == NULL) {
931f13ac639Smuffin return (-1);
932f13ac639Smuffin }
933f13ac639Smuffin
934f13ac639Smuffin p->msg_file_info = sun_header;
935f13ac639Smuffin p->msg_list = (struct msg_struct *)(uintptr_t)
936f13ac639Smuffin (addr + sizeof (struct msg_info));
937f13ac639Smuffin p->msg_ids = (char *)(addr + sizeof (struct msg_info) +
938f13ac639Smuffin struct_size);
939f13ac639Smuffin p->msg_strs = (char *)(addr + sizeof (struct msg_info) +
940f13ac639Smuffin struct_size + sun_header->str_count_msgid);
941f13ac639Smuffin
942f13ac639Smuffin mnp->msg.sunmsg = p;
943f13ac639Smuffin mnp->type = T_SUN_MO;
944f13ac639Smuffin #ifdef GETTEXT_DEBUG
945f13ac639Smuffin gprintf(0, "******** exiting sun_setmsg\n");
946f13ac639Smuffin printmnp(mnp, 1);
947f13ac639Smuffin #endif
948f13ac639Smuffin return (0);
949f13ac639Smuffin }
950f13ac639Smuffin
951f13ac639Smuffin /*
952f13ac639Smuffin * setmsg
953f13ac639Smuffin *
954f13ac639Smuffin * INPUT
955f13ac639Smuffin * mnp - message node
956f13ac639Smuffin * addr - address to the mmapped file
957f13ac639Smuffin * size - size of the file
958f13ac639Smuffin *
959f13ac639Smuffin * RETURN
960f13ac639Smuffin * 0 - succeeded
961f13ac639Smuffin * -1 - failed
962f13ac639Smuffin */
963f13ac639Smuffin static int
setmsg(Msg_node * mnp,char * addr,size_t size)964f13ac639Smuffin setmsg(Msg_node *mnp, char *addr, size_t size)
965f13ac639Smuffin {
966f13ac639Smuffin int ret;
967f13ac639Smuffin if ((ret = sun_setmsg(mnp, addr, size)) <= 0)
968f13ac639Smuffin return (ret);
969f13ac639Smuffin
970f13ac639Smuffin return (gnu_setmsg(mnp, addr, size));
971f13ac639Smuffin }
972f13ac639Smuffin
9737c478bd9Sstevel@tonic-gate static char *
handle_type_mo(Msg_node * mnp,struct msg_pack * mp)974f13ac639Smuffin handle_type_mo(Msg_node *mnp, struct msg_pack *mp)
9757c478bd9Sstevel@tonic-gate {
9767c478bd9Sstevel@tonic-gate char *result;
9777c478bd9Sstevel@tonic-gate
978f13ac639Smuffin switch (mnp->type) {
9797c478bd9Sstevel@tonic-gate case T_ILL_MO:
980f13ac639Smuffin /* invalid MO */
9817c478bd9Sstevel@tonic-gate return (NULL);
9827c478bd9Sstevel@tonic-gate case T_SUN_MO:
983f13ac639Smuffin /* Sun MO found */
984f13ac639Smuffin mp->status |= ST_SUN_MO_FOUND;
985f13ac639Smuffin
9867c478bd9Sstevel@tonic-gate if (mp->plural) {
9877c478bd9Sstevel@tonic-gate /*
9887c478bd9Sstevel@tonic-gate * *ngettext is called against
9897c478bd9Sstevel@tonic-gate * Sun MO file
9907c478bd9Sstevel@tonic-gate */
9917c478bd9Sstevel@tonic-gate int exp = (mp->n == 1);
9927c478bd9Sstevel@tonic-gate result = (char *)mp->msgid1;
9937c478bd9Sstevel@tonic-gate if (!exp)
9947c478bd9Sstevel@tonic-gate result = (char *)mp->msgid2;
9957c478bd9Sstevel@tonic-gate return (result);
9967c478bd9Sstevel@tonic-gate }
997f13ac639Smuffin result = key_2_text(mnp->msg.sunmsg, mp->msgid1);
998f13ac639Smuffin if (!mnp->trusted) {
9997c478bd9Sstevel@tonic-gate result = check_format(mp->msgid1, result, 0);
10007c478bd9Sstevel@tonic-gate }
10017c478bd9Sstevel@tonic-gate return (result);
10027c478bd9Sstevel@tonic-gate case T_GNU_MO:
1003f13ac639Smuffin /* GNU MO found */
1004f13ac639Smuffin mp->status |= ST_GNU_MO_FOUND;
1005f13ac639Smuffin
1006f13ac639Smuffin result = gnu_key_2_text(mnp->msg.gnumsg,
1007f13ac639Smuffin get_codeset(mp->domain), mp);
1008f13ac639Smuffin
1009f13ac639Smuffin if (result == mp->msgid1 || result == mp->msgid2) {
1010f13ac639Smuffin /* no valid msg found */
10117c478bd9Sstevel@tonic-gate return (result);
10127c478bd9Sstevel@tonic-gate }
1013f13ac639Smuffin
1014f13ac639Smuffin /* valid msg found */
1015f13ac639Smuffin mp->status |= ST_GNU_MSG_FOUND;
1016f13ac639Smuffin
1017f13ac639Smuffin if (!mnp->trusted) {
10187c478bd9Sstevel@tonic-gate result = check_format(mp->msgid1, result, 0);
10197c478bd9Sstevel@tonic-gate if (result == mp->msgid1) {
10207c478bd9Sstevel@tonic-gate DFLTMSG(result, mp->msgid1, mp->msgid2,
1021f13ac639Smuffin mp->n, mp->plural);
10227c478bd9Sstevel@tonic-gate }
10237c478bd9Sstevel@tonic-gate }
10247c478bd9Sstevel@tonic-gate return (result);
10257c478bd9Sstevel@tonic-gate default:
10267c478bd9Sstevel@tonic-gate /* this should never happen */
1027f13ac639Smuffin DFLTMSG(result, mp->msgid1, mp->msgid2, mp->n, mp->plural);
1028f13ac639Smuffin return (result);
10297c478bd9Sstevel@tonic-gate }
10307c478bd9Sstevel@tonic-gate /* NOTREACHED */
10317c478bd9Sstevel@tonic-gate }
10327c478bd9Sstevel@tonic-gate
1033f13ac639Smuffin /*
1034f13ac639Smuffin * handle_mo() returns NULL if invalid MO found.
1035f13ac639Smuffin */
1036f13ac639Smuffin char *
handle_mo(struct msg_pack * mp)1037f13ac639Smuffin handle_mo(struct msg_pack *mp)
10387c478bd9Sstevel@tonic-gate {
1039f13ac639Smuffin int fd;
10407c478bd9Sstevel@tonic-gate char *result;
10417c478bd9Sstevel@tonic-gate struct stat64 statbuf;
1042f13ac639Smuffin Msg_node *mnp;
10437c478bd9Sstevel@tonic-gate Gettext_t *gt = global_gt;
10447c478bd9Sstevel@tonic-gate
1045f13ac639Smuffin #define CONNECT_ENTRY \
1046f13ac639Smuffin mnp->next = gt->m_node; \
1047f13ac639Smuffin gt->m_node = mnp; \
1048f13ac639Smuffin gt->c_m_node = mnp
1049f13ac639Smuffin
10507c478bd9Sstevel@tonic-gate #ifdef GETTEXT_DEBUG
1051f13ac639Smuffin gprintf(0, "*************** handle_mo(0x%p)\n", (void *)mp);
1052f13ac639Smuffin printmp(mp, 1);
10537c478bd9Sstevel@tonic-gate #endif
10547c478bd9Sstevel@tonic-gate
1055f13ac639Smuffin mnp = check_cache(mp);
10567c478bd9Sstevel@tonic-gate
1057f13ac639Smuffin if (mnp != NULL) {
10587c478bd9Sstevel@tonic-gate /* cache found */
1059f13ac639Smuffin return (handle_type_mo(mnp, mp));
10607c478bd9Sstevel@tonic-gate }
1061f13ac639Smuffin
10627c478bd9Sstevel@tonic-gate /*
10637c478bd9Sstevel@tonic-gate * Valid entry not found in the cache
10647c478bd9Sstevel@tonic-gate */
1065f13ac639Smuffin mnp = calloc(1, sizeof (Msg_node));
1066f13ac639Smuffin if (mnp == NULL) {
1067f13ac639Smuffin DFLTMSG(result, mp->msgid1, mp->msgid2, mp->n, mp->plural);
1068f13ac639Smuffin return (result);
1069f13ac639Smuffin }
1070f13ac639Smuffin mnp->hashid = mp->hash_domain;
1071f13ac639Smuffin mnp->path = strdup(mp->msgfile);
1072f13ac639Smuffin if (mnp->path == NULL) {
1073f13ac639Smuffin free(mnp);
1074f13ac639Smuffin DFLTMSG(result, mp->msgid1, mp->msgid2, mp->n, mp->plural);
1075f13ac639Smuffin return (result);
1076f13ac639Smuffin }
1077f13ac639Smuffin
1078f13ac639Smuffin fd = nls_safe_open(mp->msgfile, &statbuf, &mp->trusted, !mp->nlsp);
10797c478bd9Sstevel@tonic-gate if ((fd == -1) || (statbuf.st_size > LONG_MAX)) {
1080f13ac639Smuffin if (fd != -1)
1081f13ac639Smuffin (void) close(fd);
1082f13ac639Smuffin mnp->type = T_ILL_MO;
1083f13ac639Smuffin CONNECT_ENTRY;
10847c478bd9Sstevel@tonic-gate return (NULL);
10857c478bd9Sstevel@tonic-gate }
10867c478bd9Sstevel@tonic-gate mp->fsz = (size_t)statbuf.st_size;
1087f13ac639Smuffin mp->addr = mmap(NULL, mp->fsz, PROT_READ, MAP_SHARED, fd, 0);
10887c478bd9Sstevel@tonic-gate (void) close(fd);
10897c478bd9Sstevel@tonic-gate
1090f13ac639Smuffin if (mp->addr == MAP_FAILED) {
1091f13ac639Smuffin free(mnp->path);
1092f13ac639Smuffin free(mnp);
10937c478bd9Sstevel@tonic-gate DFLTMSG(result, mp->msgid1, mp->msgid2, mp->n, mp->plural);
10947c478bd9Sstevel@tonic-gate return (result);
10957c478bd9Sstevel@tonic-gate }
10967c478bd9Sstevel@tonic-gate
1097f13ac639Smuffin if (setmsg(mnp, (char *)mp->addr, mp->fsz) == -1) {
1098f13ac639Smuffin free(mnp->path);
1099f13ac639Smuffin free(mnp);
11007c478bd9Sstevel@tonic-gate (void) munmap(mp->addr, mp->fsz);
11017c478bd9Sstevel@tonic-gate DFLTMSG(result, mp->msgid1, mp->msgid2, mp->n, mp->plural);
11027c478bd9Sstevel@tonic-gate return (result);
11037c478bd9Sstevel@tonic-gate }
1104f13ac639Smuffin mnp->trusted = mp->trusted;
1105f13ac639Smuffin CONNECT_ENTRY;
11067c478bd9Sstevel@tonic-gate
1107f13ac639Smuffin return (handle_type_mo(mnp, mp));
11087c478bd9Sstevel@tonic-gate }
1109