17c478bd9Sstevel@tonic-gate /*
2*9525b14bSRao Shoaib  * Copyright (C) 2004, 2005, 2008  Internet Systems Consortium, Inc. ("ISC")
3*9525b14bSRao Shoaib  * Copyright (C) 1997, 1999, 2001  Internet Software Consortium.
47c478bd9Sstevel@tonic-gate  *
5*9525b14bSRao Shoaib  * Permission to use, copy, modify, and/or distribute this software for any
67c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
77c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
87c478bd9Sstevel@tonic-gate  *
9*9525b14bSRao Shoaib  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10*9525b14bSRao Shoaib  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11*9525b14bSRao Shoaib  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12*9525b14bSRao Shoaib  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13*9525b14bSRao Shoaib  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14*9525b14bSRao Shoaib  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15*9525b14bSRao Shoaib  * PERFORMANCE OF THIS SOFTWARE.
167c478bd9Sstevel@tonic-gate  */
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate #include "port_before.h"
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate #include <errno.h>
217c478bd9Sstevel@tonic-gate #include <stdio.h>
227c478bd9Sstevel@tonic-gate #include <stdlib.h>
237c478bd9Sstevel@tonic-gate #include <string.h>
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate #include <isc/assertions.h>
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include "port_after.h"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Forward.
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate static void default_assertion_failed(const char *, int, assertion_type,
347c478bd9Sstevel@tonic-gate 				     const char *, int);
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /*
377c478bd9Sstevel@tonic-gate  * Public.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate assertion_failure_callback __assertion_failed = default_assertion_failed;
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate void
set_assertion_failure_callback(assertion_failure_callback f)437c478bd9Sstevel@tonic-gate set_assertion_failure_callback(assertion_failure_callback f) {
447c478bd9Sstevel@tonic-gate 	if (f == NULL)
457c478bd9Sstevel@tonic-gate 		__assertion_failed = default_assertion_failed;
467c478bd9Sstevel@tonic-gate 	else
477c478bd9Sstevel@tonic-gate 		__assertion_failed = f;
487c478bd9Sstevel@tonic-gate }
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate const char *
assertion_type_to_text(assertion_type type)517c478bd9Sstevel@tonic-gate assertion_type_to_text(assertion_type type) {
527c478bd9Sstevel@tonic-gate 	const char *result;
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 	switch (type) {
557c478bd9Sstevel@tonic-gate 	case assert_require:
567c478bd9Sstevel@tonic-gate 		result = "REQUIRE";
577c478bd9Sstevel@tonic-gate 		break;
587c478bd9Sstevel@tonic-gate 	case assert_ensure:
597c478bd9Sstevel@tonic-gate 		result = "ENSURE";
607c478bd9Sstevel@tonic-gate 		break;
617c478bd9Sstevel@tonic-gate 	case assert_insist:
627c478bd9Sstevel@tonic-gate 		result = "INSIST";
637c478bd9Sstevel@tonic-gate 		break;
647c478bd9Sstevel@tonic-gate 	case assert_invariant:
657c478bd9Sstevel@tonic-gate 		result = "INVARIANT";
667c478bd9Sstevel@tonic-gate 		break;
677c478bd9Sstevel@tonic-gate 	default:
687c478bd9Sstevel@tonic-gate 		result = NULL;
697c478bd9Sstevel@tonic-gate 	}
707c478bd9Sstevel@tonic-gate 	return (result);
717c478bd9Sstevel@tonic-gate }
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate  * Private.
757c478bd9Sstevel@tonic-gate  */
767c478bd9Sstevel@tonic-gate 
77*9525b14bSRao Shoaib /* coverity[+kill] */
787c478bd9Sstevel@tonic-gate static void
default_assertion_failed(const char * file,int line,assertion_type type,const char * cond,int print_errno)797c478bd9Sstevel@tonic-gate default_assertion_failed(const char *file, int line, assertion_type type,
807c478bd9Sstevel@tonic-gate 			 const char *cond, int print_errno)
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate 	fprintf(stderr, "%s:%d: %s(%s)%s%s failed.\n",
837c478bd9Sstevel@tonic-gate 		file, line, assertion_type_to_text(type), cond,
847c478bd9Sstevel@tonic-gate 		(print_errno) ? ": " : "",
857c478bd9Sstevel@tonic-gate 		(print_errno) ? strerror(errno) : "");
867c478bd9Sstevel@tonic-gate 	abort();
877c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
887c478bd9Sstevel@tonic-gate }
89*9525b14bSRao Shoaib 
90*9525b14bSRao Shoaib /*! \file */
91