Home | History | Annotate | Line # | Download | only in isc
assertions.c revision 1.2
      1  1.2  christos /*	$NetBSD: assertions.c,v 1.2 2004/05/20 19:52:31 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
      5  1.1  christos  * Copyright (c) 1997,1999 by Internet Software Consortium.
      6  1.1  christos  *
      7  1.1  christos  * Permission to use, copy, modify, and 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
     12  1.1  christos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13  1.1  christos  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
     14  1.1  christos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15  1.1  christos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16  1.1  christos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
     17  1.1  christos  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18  1.1  christos  */
     19  1.1  christos 
     20  1.2  christos #include <sys/cdefs.h>
     21  1.2  christos #if !defined(LINT) && !defined(CODECENTER) && !defined(lint)
     22  1.2  christos #ifdef notdef
     23  1.1  christos static const char rcsid[] = "Id: assertions.c,v 1.1.206.1 2004/03/09 08:33:39 marka Exp";
     24  1.2  christos #else
     25  1.2  christos __RCSID("$NetBSD: assertions.c,v 1.2 2004/05/20 19:52:31 christos Exp $");
     26  1.2  christos #endif
     27  1.1  christos #endif
     28  1.1  christos 
     29  1.1  christos #include "port_before.h"
     30  1.1  christos 
     31  1.1  christos #include <errno.h>
     32  1.1  christos #include <stdio.h>
     33  1.1  christos #include <stdlib.h>
     34  1.1  christos #include <string.h>
     35  1.1  christos 
     36  1.1  christos #include <isc/assertions.h>
     37  1.1  christos 
     38  1.1  christos #include "port_after.h"
     39  1.1  christos 
     40  1.1  christos /*
     41  1.1  christos  * Forward.
     42  1.1  christos  */
     43  1.1  christos 
     44  1.1  christos static void default_assertion_failed(const char *, int, assertion_type,
     45  1.1  christos 				     const char *, int);
     46  1.1  christos 
     47  1.1  christos /*
     48  1.1  christos  * Public.
     49  1.1  christos  */
     50  1.1  christos 
     51  1.1  christos assertion_failure_callback __assertion_failed = default_assertion_failed;
     52  1.1  christos 
     53  1.1  christos void
     54  1.1  christos set_assertion_failure_callback(assertion_failure_callback f) {
     55  1.1  christos 	if (f == NULL)
     56  1.1  christos 		__assertion_failed = default_assertion_failed;
     57  1.1  christos 	else
     58  1.1  christos 		__assertion_failed = f;
     59  1.1  christos }
     60  1.1  christos 
     61  1.1  christos const char *
     62  1.1  christos assertion_type_to_text(assertion_type type) {
     63  1.1  christos 	const char *result;
     64  1.1  christos 
     65  1.1  christos 	switch (type) {
     66  1.1  christos 	case assert_require:
     67  1.1  christos 		result = "REQUIRE";
     68  1.1  christos 		break;
     69  1.1  christos 	case assert_ensure:
     70  1.1  christos 		result = "ENSURE";
     71  1.1  christos 		break;
     72  1.1  christos 	case assert_insist:
     73  1.1  christos 		result = "INSIST";
     74  1.1  christos 		break;
     75  1.1  christos 	case assert_invariant:
     76  1.1  christos 		result = "INVARIANT";
     77  1.1  christos 		break;
     78  1.1  christos 	default:
     79  1.1  christos 		result = NULL;
     80  1.1  christos 	}
     81  1.1  christos 	return (result);
     82  1.1  christos }
     83  1.1  christos 
     84  1.1  christos /*
     85  1.1  christos  * Private.
     86  1.1  christos  */
     87  1.1  christos 
     88  1.1  christos static void
     89  1.1  christos default_assertion_failed(const char *file, int line, assertion_type type,
     90  1.1  christos 			 const char *cond, int print_errno)
     91  1.1  christos {
     92  1.1  christos 	fprintf(stderr, "%s:%d: %s(%s)%s%s failed.\n",
     93  1.1  christos 		file, line, assertion_type_to_text(type), cond,
     94  1.1  christos 		(print_errno) ? ": " : "",
     95  1.1  christos 		(print_errno) ? strerror(errno) : "");
     96  1.1  christos 	abort();
     97  1.1  christos 	/* NOTREACHED */
     98  1.1  christos }
     99