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