#include <config.h>
#include <stddef.h>
#include <stdlib.h>
#include <isc/lib.h>
#include <isc/msgs.h>
#include <isc/mutex.h>
#include <isc/once.h>
#include <isc/resultclass.h>
#include <isc/util.h>
typedef struct resulttable {
unsigned int base;
unsigned int last;
const char ** text;
isc_msgcat_t * msgcat;
int set;
ISC_LINK(struct resulttable) link;
} resulttable;
static const char *description[ISC_R_NRESULTS] = {
"success",
"out of memory",
"timed out",
"no available threads",
"address not available",
"address in use",
"permission denied",
"no pending connections",
"network unreachable",
"host unreachable",
"network down",
"host down",
"connection refused",
"not enough free resources",
"end of file",
"socket already bound",
"reload",
"lock busy",
"already exists",
"ran out of space",
"operation canceled",
"socket is not bound",
"shutting down",
"not found",
"unexpected end of input",
"failure",
"I/O error",
"not implemented",
"unbalanced parentheses",
"no more",
"invalid file",
"bad base64 encoding",
"unexpected token",
"quota reached",
"unexpected error",
"already running",
"ignore",
"address mask not contiguous",
"file not found",
"file already exists",
"socket is not connected",
"out of range",
"out of entropy",
"invalid use of multicast address",
"not a file",
"not a directory",
"queue is full",
"address family mismatch",
"address family not supported",
"bad hex encoding",
"too many open files",
"not blocking",
"unbalanced quotes",
"operation in progress",
"connection reset",
"soft quota reached",
"not a valid number",
"disabled",
"max size",
"invalid address format",
"bad base32 encoding",
"unset",
"multiple",
};
#define ISC_RESULT_RESULTSET 2
#define ISC_RESULT_UNAVAILABLESET 3
static isc_once_t once = ISC_ONCE_INIT;
static ISC_LIST(resulttable) tables;
static isc_mutex_t lock;
static isc_result_t
register_table(unsigned int base, unsigned int nresults, const char **text,
isc_msgcat_t *msgcat, int set)
{
resulttable *table;
REQUIRE(base % ISC_RESULTCLASS_SIZE == 0);
REQUIRE(nresults <= ISC_RESULTCLASS_SIZE);
REQUIRE(text != NULL);
table = malloc(sizeof(*table));
if (table == NULL)
return (ISC_R_NOMEMORY);
table->base = base;
table->last = base + nresults - 1;
table->text = text;
table->msgcat = msgcat;
table->set = set;
ISC_LINK_INIT(table, link);
LOCK(&lock);
ISC_LIST_APPEND(tables, table, link);
UNLOCK(&lock);
return (ISC_R_SUCCESS);
}
static void
initialize_action(void) {
isc_result_t result;
RUNTIME_CHECK(isc_mutex_init(&lock) == ISC_R_SUCCESS);
ISC_LIST_INIT(tables);
result = register_table(ISC_RESULTCLASS_ISC, ISC_R_NRESULTS,
description, isc_msgcat, ISC_RESULT_RESULTSET);
if (result != ISC_R_SUCCESS)
UNEXPECTED_ERROR(__FILE__, __LINE__,
"register_table() %s: %u",
isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
ISC_MSG_FAILED, "failed"),
result);
}
static void
initialize(void) {
isc_lib_initmsgcat();
RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
}
const char *
isc_result_totext(isc_result_t result) {
resulttable *table;
const char *text, *default_text;
int index;
initialize();
LOCK(&lock);
text = NULL;
for (table = ISC_LIST_HEAD(tables);
table != NULL;
table = ISC_LIST_NEXT(table, link)) {
if (result >= table->base && result <= table->last) {
index = (int)(result - table->base);
default_text = table->text[index];
text = isc_msgcat_get(table->msgcat, table->set,
index + 1, default_text);
break;
}
}
if (text == NULL)
text = isc_msgcat_get(isc_msgcat, ISC_RESULT_UNAVAILABLESET,
1, "(result code text not available)");
UNLOCK(&lock);
return (text);
}
isc_result_t
isc_result_register(unsigned int base, unsigned int nresults,
const char **text, isc_msgcat_t *msgcat, int set)
{
initialize();
return (register_table(base, nresults, text, msgcat, set));
}