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