xref: /freebsd/contrib/ntp/libntp/lib/isc/nls/msgcat.c (revision a466cc55373fc3cf86837f09da729535b57e69a1)
1*a466cc55SCy Schubert /*
2*a466cc55SCy Schubert  * Copyright (C) 2004, 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
3*a466cc55SCy Schubert  * Copyright (C) 1999-2001  Internet Software Consortium.
4*a466cc55SCy Schubert  *
5*a466cc55SCy Schubert  * Permission to use, copy, modify, and/or distribute this software for any
6*a466cc55SCy Schubert  * purpose with or without fee is hereby granted, provided that the above
7*a466cc55SCy Schubert  * copyright notice and this permission notice appear in all copies.
8*a466cc55SCy Schubert  *
9*a466cc55SCy Schubert  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10*a466cc55SCy Schubert  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11*a466cc55SCy Schubert  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12*a466cc55SCy Schubert  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13*a466cc55SCy Schubert  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14*a466cc55SCy Schubert  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15*a466cc55SCy Schubert  * PERFORMANCE OF THIS SOFTWARE.
16*a466cc55SCy Schubert  */
17*a466cc55SCy Schubert 
18*a466cc55SCy Schubert /* $Id: msgcat.c,v 1.18 2007/06/19 23:47:18 tbox Exp $ */
19*a466cc55SCy Schubert 
20*a466cc55SCy Schubert /*! \file msgcat.c
21*a466cc55SCy Schubert  *
22*a466cc55SCy Schubert  * \author Principal Author: Bob Halley
23*a466cc55SCy Schubert  */
24*a466cc55SCy Schubert 
25*a466cc55SCy Schubert #include <config.h>
26*a466cc55SCy Schubert 
27*a466cc55SCy Schubert #include <stddef.h>
28*a466cc55SCy Schubert #include <stdlib.h>
29*a466cc55SCy Schubert 
30*a466cc55SCy Schubert #include <isc/magic.h>
31*a466cc55SCy Schubert #include <isc/msgcat.h>
32*a466cc55SCy Schubert #include <isc/util.h>
33*a466cc55SCy Schubert 
34*a466cc55SCy Schubert #ifdef HAVE_CATGETS
35*a466cc55SCy Schubert #include <nl_types.h>		/* Required for nl_catd. */
36*a466cc55SCy Schubert #endif
37*a466cc55SCy Schubert 
38*a466cc55SCy Schubert /*
39*a466cc55SCy Schubert  * Implementation Notes:
40*a466cc55SCy Schubert  *
41*a466cc55SCy Schubert  *	We use malloc() and free() instead of isc_mem_get() and isc_mem_put()
42*a466cc55SCy Schubert  *	because we don't want to require a memory context to be specified
43*a466cc55SCy Schubert  *	in order to use a message catalog.
44*a466cc55SCy Schubert  */
45*a466cc55SCy Schubert 
46*a466cc55SCy Schubert struct isc_msgcat {
47*a466cc55SCy Schubert 	unsigned int	magic;
48*a466cc55SCy Schubert #ifdef HAVE_CATGETS
49*a466cc55SCy Schubert 	nl_catd		catalog;
50*a466cc55SCy Schubert #endif
51*a466cc55SCy Schubert };
52*a466cc55SCy Schubert 
53*a466cc55SCy Schubert #define MSGCAT_MAGIC			ISC_MAGIC('M', 'C', 'a', 't')
54*a466cc55SCy Schubert #define VALID_MSGCAT(m)			ISC_MAGIC_VALID(m, MSGCAT_MAGIC)
55*a466cc55SCy Schubert 
56*a466cc55SCy Schubert void
isc_msgcat_open(const char * name,isc_msgcat_t ** msgcatp)57*a466cc55SCy Schubert isc_msgcat_open(const char *name, isc_msgcat_t **msgcatp) {
58*a466cc55SCy Schubert 	isc_msgcat_t *msgcat;
59*a466cc55SCy Schubert 
60*a466cc55SCy Schubert 	/*
61*a466cc55SCy Schubert 	 * Open a message catalog.
62*a466cc55SCy Schubert 	 */
63*a466cc55SCy Schubert 
64*a466cc55SCy Schubert 	REQUIRE(name != NULL);
65*a466cc55SCy Schubert 	REQUIRE(msgcatp != NULL && *msgcatp == NULL);
66*a466cc55SCy Schubert 
67*a466cc55SCy Schubert 	msgcat = malloc(sizeof(*msgcat));
68*a466cc55SCy Schubert 	if (msgcat == NULL) {
69*a466cc55SCy Schubert 		*msgcatp = NULL;
70*a466cc55SCy Schubert 		return;
71*a466cc55SCy Schubert 	}
72*a466cc55SCy Schubert 
73*a466cc55SCy Schubert #ifdef HAVE_CATGETS
74*a466cc55SCy Schubert 	/*
75*a466cc55SCy Schubert 	 * We don't check if catopen() fails because we don't care.
76*a466cc55SCy Schubert 	 * If it does fail, then when we call catgets(), it will use
77*a466cc55SCy Schubert 	 * the default string.
78*a466cc55SCy Schubert 	 */
79*a466cc55SCy Schubert 	msgcat->catalog = catopen(name, 0);
80*a466cc55SCy Schubert #endif
81*a466cc55SCy Schubert 	msgcat->magic = MSGCAT_MAGIC;
82*a466cc55SCy Schubert 
83*a466cc55SCy Schubert 	*msgcatp = msgcat;
84*a466cc55SCy Schubert }
85*a466cc55SCy Schubert 
86*a466cc55SCy Schubert void
isc_msgcat_close(isc_msgcat_t ** msgcatp)87*a466cc55SCy Schubert isc_msgcat_close(isc_msgcat_t **msgcatp) {
88*a466cc55SCy Schubert 	isc_msgcat_t *msgcat;
89*a466cc55SCy Schubert 
90*a466cc55SCy Schubert 	/*
91*a466cc55SCy Schubert 	 * Close a message catalog.
92*a466cc55SCy Schubert 	 */
93*a466cc55SCy Schubert 
94*a466cc55SCy Schubert 	REQUIRE(msgcatp != NULL);
95*a466cc55SCy Schubert 	msgcat = *msgcatp;
96*a466cc55SCy Schubert 	REQUIRE(VALID_MSGCAT(msgcat) || msgcat == NULL);
97*a466cc55SCy Schubert 
98*a466cc55SCy Schubert 	if (msgcat != NULL) {
99*a466cc55SCy Schubert #ifdef HAVE_CATGETS
100*a466cc55SCy Schubert 		if (msgcat->catalog != (nl_catd)(-1))
101*a466cc55SCy Schubert 			(void)catclose(msgcat->catalog);
102*a466cc55SCy Schubert #endif
103*a466cc55SCy Schubert 		msgcat->magic = 0;
104*a466cc55SCy Schubert 		free(msgcat);
105*a466cc55SCy Schubert 	}
106*a466cc55SCy Schubert 
107*a466cc55SCy Schubert 	*msgcatp = NULL;
108*a466cc55SCy Schubert }
109*a466cc55SCy Schubert 
110*a466cc55SCy Schubert const char *
isc_msgcat_get(isc_msgcat_t * msgcat,int set,int message,const char * default_text)111*a466cc55SCy Schubert isc_msgcat_get(isc_msgcat_t *msgcat, int set, int message,
112*a466cc55SCy Schubert 	       const char *default_text)
113*a466cc55SCy Schubert {
114*a466cc55SCy Schubert 	/*
115*a466cc55SCy Schubert 	 * Get message 'message' from message set 'set' in 'msgcat'.  If it
116*a466cc55SCy Schubert 	 * is not available, use 'default'.
117*a466cc55SCy Schubert 	 */
118*a466cc55SCy Schubert 
119*a466cc55SCy Schubert 	REQUIRE(VALID_MSGCAT(msgcat) || msgcat == NULL);
120*a466cc55SCy Schubert 	REQUIRE(set > 0);
121*a466cc55SCy Schubert 	REQUIRE(message > 0);
122*a466cc55SCy Schubert 	REQUIRE(default_text != NULL);
123*a466cc55SCy Schubert 
124*a466cc55SCy Schubert #ifdef HAVE_CATGETS
125*a466cc55SCy Schubert 	if (msgcat == NULL)
126*a466cc55SCy Schubert 		return (default_text);
127*a466cc55SCy Schubert 	return (catgets(msgcat->catalog, set, message, default_text));
128*a466cc55SCy Schubert #else
129*a466cc55SCy Schubert 	return (default_text);
130*a466cc55SCy Schubert #endif
131*a466cc55SCy Schubert }
132