Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / ntp / libisc / msgcat.c
blob6463764a73cd6b090f3817e2b29b44af256bbb34
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 1999-2001 Internet Software Consortium.
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
11 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
12 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
13 * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
15 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
16 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
17 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 /* Id: msgcat.c,v 1.12 2001/11/30 01:59:39 gson Exp */
23 * Principal Author: Bob Halley
26 #include <config.h>
28 #include <stdlib.h>
29 #include <unistd.h>
31 #include <isc/magic.h>
32 #include <isc/msgcat.h>
33 #include <isc/util.h>
35 #ifdef HAVE_CATGETS
36 #include <nl_types.h> /* Required for nl_catd. */
37 #endif
40 * Implementation Notes:
42 * We use malloc() and free() instead of isc_mem_get() and isc_mem_put()
43 * because we don't want to require a memory context to be specified
44 * in order to use a message catalog.
47 struct isc_msgcat {
48 unsigned int magic;
49 #ifdef HAVE_CATGETS
50 nl_catd catalog;
51 #endif
54 #define MSGCAT_MAGIC ISC_MAGIC('M', 'C', 'a', 't')
55 #define VALID_MSGCAT(m) ISC_MAGIC_VALID(m, MSGCAT_MAGIC)
57 void
58 isc_msgcat_open(const char *name, isc_msgcat_t **msgcatp) {
59 isc_msgcat_t *msgcat;
62 * Open a message catalog.
65 REQUIRE(name != NULL);
66 REQUIRE(msgcatp != NULL && *msgcatp == NULL);
68 msgcat = malloc(sizeof(*msgcat));
69 if (msgcat == NULL) {
70 *msgcatp = NULL;
71 return;
74 #ifdef HAVE_CATGETS
76 * We don't check if catopen() fails because we don't care.
77 * If it does fail, then when we call catgets(), it will use
78 * the default string.
80 msgcat->catalog = catopen(name, 0);
81 #endif
82 msgcat->magic = MSGCAT_MAGIC;
84 *msgcatp = msgcat;
87 void
88 isc_msgcat_close(isc_msgcat_t **msgcatp) {
89 isc_msgcat_t *msgcat;
92 * Close a message catalog.
95 REQUIRE(msgcatp != NULL);
96 msgcat = *msgcatp;
97 REQUIRE(VALID_MSGCAT(msgcat) || msgcat == NULL);
99 if (msgcat != NULL) {
100 #ifdef HAVE_CATGETS
101 if (msgcat->catalog != (nl_catd)(-1))
102 (void)catclose(msgcat->catalog);
103 #endif
104 msgcat->magic = 0;
105 free(msgcat);
108 *msgcatp = NULL;
111 const char *
112 isc_msgcat_get(isc_msgcat_t *msgcat, int set, int message,
113 const char *default_text)
116 * Get message 'message' from message set 'set' in 'msgcat'. If it
117 * is not available, use 'default'.
120 REQUIRE(VALID_MSGCAT(msgcat) || msgcat == NULL);
121 REQUIRE(set > 0);
122 REQUIRE(message > 0);
123 REQUIRE(default_text != NULL);
125 #ifdef HAVE_CATGETS
126 if (msgcat == NULL)
127 return (default_text);
128 return (catgets(msgcat->catalog, set, message, default_text));
129 #else
130 return (default_text);
131 #endif