1/*	$NetBSD: assertions.c,v 1.1.1.1 2009/12/13 16:54:13 kardel Exp $	*/
2
3/*
4 * Copyright (C) 2004, 2005, 2007, 2008  Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1997-2001  Internet Software Consortium.
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
18 */
19
20/* Id: assertions.c,v 1.23 2008/10/15 23:47:31 tbox Exp */
21
22/*! \file */
23
24#include <config.h>
25
26#include <stdio.h>
27#include <stdlib.h>
28
29#include <isc/assertions.h>
30#include <isc/msgs.h>
31
32/*%
33 * Forward.
34 */
35/* coverity[+kill] */
36static void
37default_callback(const char *, int, isc_assertiontype_t, const char *);
38
39/*%
40 * Public.
41 */
42
43LIBISC_EXTERNAL_DATA isc_assertioncallback_t isc_assertion_failed =
44					     default_callback;
45
46/*% Set callback. */
47void
48isc_assertion_setcallback(isc_assertioncallback_t cb) {
49	if (cb == NULL)
50		isc_assertion_failed = default_callback;
51	else
52		isc_assertion_failed = cb;
53}
54
55/*% Type to Text */
56const char *
57isc_assertion_typetotext(isc_assertiontype_t type) {
58	const char *result;
59
60	/*
61	 * These strings have purposefully not been internationalized
62	 * because they are considered to essentially be keywords of
63	 * the ISC development environment.
64	 */
65	switch (type) {
66	case isc_assertiontype_require:
67		result = "REQUIRE";
68		break;
69	case isc_assertiontype_ensure:
70		result = "ENSURE";
71		break;
72	case isc_assertiontype_insist:
73		result = "INSIST";
74		break;
75	case isc_assertiontype_invariant:
76		result = "INVARIANT";
77		break;
78	default:
79		result = NULL;
80	}
81	return (result);
82}
83
84/*
85 * Private.
86 */
87
88static void
89default_callback(const char *file, int line, isc_assertiontype_t type,
90		 const char *cond)
91{
92	fprintf(stderr, "%s:%d: %s(%s) %s.\n",
93		file, line, isc_assertion_typetotext(type), cond,
94		isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
95			       ISC_MSG_FAILED, "failed"));
96	fflush(stderr);
97	abort();
98	/* NOTREACHED */
99}
100