xref: /minix3/lib/libintl/textdomain.c (revision 36dcc4a4a93f782ada76dce3d52fbeab0e063cf1)
1*36dcc4a4SLionel Sambuc /*	$NetBSD: textdomain.c,v 1.14 2015/05/29 12:26:28 christos Exp $	*/
2*36dcc4a4SLionel Sambuc 
3*36dcc4a4SLionel Sambuc /*-
4*36dcc4a4SLionel Sambuc  * Copyright (c) 2000, 2001 Citrus Project,
5*36dcc4a4SLionel Sambuc  * All rights reserved.
6*36dcc4a4SLionel Sambuc  *
7*36dcc4a4SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
8*36dcc4a4SLionel Sambuc  * modification, are permitted provided that the following conditions
9*36dcc4a4SLionel Sambuc  * are met:
10*36dcc4a4SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
11*36dcc4a4SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
12*36dcc4a4SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
13*36dcc4a4SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
14*36dcc4a4SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
15*36dcc4a4SLionel Sambuc  *
16*36dcc4a4SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17*36dcc4a4SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*36dcc4a4SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*36dcc4a4SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20*36dcc4a4SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*36dcc4a4SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*36dcc4a4SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*36dcc4a4SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*36dcc4a4SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*36dcc4a4SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*36dcc4a4SLionel Sambuc  * SUCH DAMAGE.
27*36dcc4a4SLionel Sambuc  */
28*36dcc4a4SLionel Sambuc 
29*36dcc4a4SLionel Sambuc #include <sys/cdefs.h>
30*36dcc4a4SLionel Sambuc __RCSID("$NetBSD: textdomain.c,v 1.14 2015/05/29 12:26:28 christos Exp $");
31*36dcc4a4SLionel Sambuc 
32*36dcc4a4SLionel Sambuc #include <sys/param.h>
33*36dcc4a4SLionel Sambuc 
34*36dcc4a4SLionel Sambuc #include <stdio.h>
35*36dcc4a4SLionel Sambuc #include <string.h>
36*36dcc4a4SLionel Sambuc #include <stdlib.h>
37*36dcc4a4SLionel Sambuc #include <libintl.h>
38*36dcc4a4SLionel Sambuc #include "libintl_local.h"
39*36dcc4a4SLionel Sambuc #include "pathnames.h"
40*36dcc4a4SLionel Sambuc 
41*36dcc4a4SLionel Sambuc static struct domainbinding __default_binding = {
42*36dcc4a4SLionel Sambuc 	.path = { _PATH_TEXTDOMAIN },
43*36dcc4a4SLionel Sambuc 	.domainname = { DEFAULT_DOMAINNAME },
44*36dcc4a4SLionel Sambuc };
45*36dcc4a4SLionel Sambuc struct domainbinding *__bindings = &__default_binding;
46*36dcc4a4SLionel Sambuc char __current_domainname[PATH_MAX] = DEFAULT_DOMAINNAME;
47*36dcc4a4SLionel Sambuc 
48*36dcc4a4SLionel Sambuc static struct domainbinding *domainbinding_lookup(const char *, int);
49*36dcc4a4SLionel Sambuc 
50*36dcc4a4SLionel Sambuc /*
51*36dcc4a4SLionel Sambuc  * set the default domainname for dcngettext() and friends.
52*36dcc4a4SLionel Sambuc  */
53*36dcc4a4SLionel Sambuc char *
textdomain(const char * domainname)54*36dcc4a4SLionel Sambuc textdomain(const char *domainname)
55*36dcc4a4SLionel Sambuc {
56*36dcc4a4SLionel Sambuc 
57*36dcc4a4SLionel Sambuc 	/* NULL pointer gives the current setting */
58*36dcc4a4SLionel Sambuc 	if (!domainname)
59*36dcc4a4SLionel Sambuc 		return __current_domainname;
60*36dcc4a4SLionel Sambuc 
61*36dcc4a4SLionel Sambuc 	/* empty string sets the value back to the default */
62*36dcc4a4SLionel Sambuc 	if (!*domainname) {
63*36dcc4a4SLionel Sambuc 		strlcpy(__current_domainname, DEFAULT_DOMAINNAME,
64*36dcc4a4SLionel Sambuc 		    sizeof(__current_domainname));
65*36dcc4a4SLionel Sambuc 	} else {
66*36dcc4a4SLionel Sambuc 		strlcpy(__current_domainname, domainname,
67*36dcc4a4SLionel Sambuc 		    sizeof(__current_domainname));
68*36dcc4a4SLionel Sambuc 	}
69*36dcc4a4SLionel Sambuc 	return __current_domainname;
70*36dcc4a4SLionel Sambuc }
71*36dcc4a4SLionel Sambuc 
72*36dcc4a4SLionel Sambuc char *
bindtextdomain(const char * domainname,const char * dirname)73*36dcc4a4SLionel Sambuc bindtextdomain(const char *domainname, const char *dirname)
74*36dcc4a4SLionel Sambuc {
75*36dcc4a4SLionel Sambuc 	struct domainbinding *p;
76*36dcc4a4SLionel Sambuc 
77*36dcc4a4SLionel Sambuc 	/* NULL pointer or empty string returns NULL with no operation */
78*36dcc4a4SLionel Sambuc 	if (!domainname || !*domainname)
79*36dcc4a4SLionel Sambuc 		return NULL;
80*36dcc4a4SLionel Sambuc 
81*36dcc4a4SLionel Sambuc 	if (dirname && (strlen(dirname) + 1 > sizeof(p->path)))
82*36dcc4a4SLionel Sambuc 		return NULL;
83*36dcc4a4SLionel Sambuc 
84*36dcc4a4SLionel Sambuc #if 0
85*36dcc4a4SLionel Sambuc 	/* disallow relative path */
86*36dcc4a4SLionel Sambuc 	if (dirname[0] != '/')
87*36dcc4a4SLionel Sambuc 		return NULL;
88*36dcc4a4SLionel Sambuc #endif
89*36dcc4a4SLionel Sambuc 
90*36dcc4a4SLionel Sambuc 	if (strlen(domainname) + 1 > sizeof(p->domainname))
91*36dcc4a4SLionel Sambuc 		return NULL;
92*36dcc4a4SLionel Sambuc 
93*36dcc4a4SLionel Sambuc 	p = domainbinding_lookup(domainname, (dirname != NULL));
94*36dcc4a4SLionel Sambuc 
95*36dcc4a4SLionel Sambuc 	if (!dirname) {
96*36dcc4a4SLionel Sambuc 		if (p)
97*36dcc4a4SLionel Sambuc 			return (p->path);
98*36dcc4a4SLionel Sambuc 		else
99*36dcc4a4SLionel Sambuc 			return (char *)__UNCONST(_PATH_TEXTDOMAIN);
100*36dcc4a4SLionel Sambuc 	}
101*36dcc4a4SLionel Sambuc 
102*36dcc4a4SLionel Sambuc 	strlcpy(p->path, dirname, sizeof(p->path));
103*36dcc4a4SLionel Sambuc 	p->mohandle.mo.mo_magic = 0; /* invalidate current mapping */
104*36dcc4a4SLionel Sambuc 
105*36dcc4a4SLionel Sambuc 	return (p->path);
106*36dcc4a4SLionel Sambuc }
107*36dcc4a4SLionel Sambuc 
108*36dcc4a4SLionel Sambuc char *
bind_textdomain_codeset(const char * domainname,const char * codeset)109*36dcc4a4SLionel Sambuc bind_textdomain_codeset(const char *domainname, const char *codeset)
110*36dcc4a4SLionel Sambuc {
111*36dcc4a4SLionel Sambuc 	struct domainbinding *p;
112*36dcc4a4SLionel Sambuc 
113*36dcc4a4SLionel Sambuc 	p = domainbinding_lookup(domainname, (codeset != NULL));
114*36dcc4a4SLionel Sambuc 	if (p == NULL)
115*36dcc4a4SLionel Sambuc 		return NULL;
116*36dcc4a4SLionel Sambuc 
117*36dcc4a4SLionel Sambuc 	if (codeset) {
118*36dcc4a4SLionel Sambuc 		free(p->codeset);
119*36dcc4a4SLionel Sambuc 		p->codeset = strdup(codeset);
120*36dcc4a4SLionel Sambuc 	}
121*36dcc4a4SLionel Sambuc 
122*36dcc4a4SLionel Sambuc 	return p->codeset;
123*36dcc4a4SLionel Sambuc }
124*36dcc4a4SLionel Sambuc 
125*36dcc4a4SLionel Sambuc /*
126*36dcc4a4SLionel Sambuc  * lookup binding for the domainname
127*36dcc4a4SLionel Sambuc  */
128*36dcc4a4SLionel Sambuc static struct domainbinding *
domainbinding_lookup(const char * domainname,int alloc)129*36dcc4a4SLionel Sambuc domainbinding_lookup(const char *domainname, int alloc)
130*36dcc4a4SLionel Sambuc {
131*36dcc4a4SLionel Sambuc 	struct domainbinding *p;
132*36dcc4a4SLionel Sambuc 
133*36dcc4a4SLionel Sambuc 	for (p = __bindings; p; p = p->next)
134*36dcc4a4SLionel Sambuc 		if (strcmp(p->domainname, domainname) == 0)
135*36dcc4a4SLionel Sambuc 			break;
136*36dcc4a4SLionel Sambuc 
137*36dcc4a4SLionel Sambuc 	if (!p && alloc) {
138*36dcc4a4SLionel Sambuc 		p = (struct domainbinding *)malloc(sizeof(*p));
139*36dcc4a4SLionel Sambuc 		if (!p)
140*36dcc4a4SLionel Sambuc 			return NULL;
141*36dcc4a4SLionel Sambuc 		memset(p, 0, sizeof(*p));
142*36dcc4a4SLionel Sambuc 		p->next = __bindings;
143*36dcc4a4SLionel Sambuc 		strlcpy(p->domainname, domainname, sizeof(p->domainname));
144*36dcc4a4SLionel Sambuc 		__bindings = p;
145*36dcc4a4SLionel Sambuc 	}
146*36dcc4a4SLionel Sambuc 
147*36dcc4a4SLionel Sambuc 	return p;
148*36dcc4a4SLionel Sambuc }
149