1*f14fb602SLionel Sambuc /* $NetBSD: assertions.c,v 1.7 2011/09/16 16:05:58 joerg Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras * Copyright (C) 2004, 2005, 2008 Internet Systems Consortium, Inc. ("ISC")
52fe8fb19SBen Gras * Copyright (C) 1997, 1999, 2001 Internet Software Consortium.
62fe8fb19SBen Gras *
72fe8fb19SBen Gras * Permission to use, copy, modify, and/or distribute this software for any
82fe8fb19SBen Gras * purpose with or without fee is hereby granted, provided that the above
92fe8fb19SBen Gras * copyright notice and this permission notice appear in all copies.
102fe8fb19SBen Gras *
112fe8fb19SBen Gras * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
122fe8fb19SBen Gras * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
132fe8fb19SBen Gras * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
142fe8fb19SBen Gras * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
152fe8fb19SBen Gras * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
162fe8fb19SBen Gras * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
172fe8fb19SBen Gras * PERFORMANCE OF THIS SOFTWARE.
182fe8fb19SBen Gras */
192fe8fb19SBen Gras
202fe8fb19SBen Gras #include <sys/cdefs.h>
212fe8fb19SBen Gras #if !defined(LINT) && !defined(CODECENTER) && !defined(lint)
222fe8fb19SBen Gras #ifdef notdef
232fe8fb19SBen Gras static const char rcsid[] = "Id: assertions.c,v 1.5 2008/11/14 02:36:51 marka Exp";
242fe8fb19SBen Gras #else
25*f14fb602SLionel Sambuc __RCSID("$NetBSD: assertions.c,v 1.7 2011/09/16 16:05:58 joerg Exp $");
262fe8fb19SBen Gras #endif
272fe8fb19SBen Gras #endif
282fe8fb19SBen Gras
292fe8fb19SBen Gras #include "port_before.h"
302fe8fb19SBen Gras
312fe8fb19SBen Gras #include <errno.h>
322fe8fb19SBen Gras #include <stdio.h>
332fe8fb19SBen Gras #include <stdlib.h>
342fe8fb19SBen Gras #include <string.h>
352fe8fb19SBen Gras
362fe8fb19SBen Gras #include <isc/assertions.h>
372fe8fb19SBen Gras
382fe8fb19SBen Gras #include "port_after.h"
392fe8fb19SBen Gras
402fe8fb19SBen Gras /*
412fe8fb19SBen Gras * Forward.
422fe8fb19SBen Gras */
432fe8fb19SBen Gras
44*f14fb602SLionel Sambuc __dead static void default_assertion_failed(const char *, int, assertion_type,
452fe8fb19SBen Gras const char *, int);
462fe8fb19SBen Gras
472fe8fb19SBen Gras /*
482fe8fb19SBen Gras * Public.
492fe8fb19SBen Gras */
502fe8fb19SBen Gras
512fe8fb19SBen Gras assertion_failure_callback __assertion_failed = default_assertion_failed;
522fe8fb19SBen Gras
532fe8fb19SBen Gras void
set_assertion_failure_callback(assertion_failure_callback f)542fe8fb19SBen Gras set_assertion_failure_callback(assertion_failure_callback f) {
552fe8fb19SBen Gras if (f == NULL)
562fe8fb19SBen Gras __assertion_failed = default_assertion_failed;
572fe8fb19SBen Gras else
582fe8fb19SBen Gras __assertion_failed = f;
592fe8fb19SBen Gras }
602fe8fb19SBen Gras
612fe8fb19SBen Gras const char *
assertion_type_to_text(assertion_type type)622fe8fb19SBen Gras assertion_type_to_text(assertion_type type) {
632fe8fb19SBen Gras const char *result;
642fe8fb19SBen Gras
652fe8fb19SBen Gras switch (type) {
662fe8fb19SBen Gras case assert_require:
672fe8fb19SBen Gras result = "REQUIRE";
682fe8fb19SBen Gras break;
692fe8fb19SBen Gras case assert_ensure:
702fe8fb19SBen Gras result = "ENSURE";
712fe8fb19SBen Gras break;
722fe8fb19SBen Gras case assert_insist:
732fe8fb19SBen Gras result = "INSIST";
742fe8fb19SBen Gras break;
752fe8fb19SBen Gras case assert_invariant:
762fe8fb19SBen Gras result = "INVARIANT";
772fe8fb19SBen Gras break;
782fe8fb19SBen Gras default:
792fe8fb19SBen Gras result = NULL;
802fe8fb19SBen Gras }
812fe8fb19SBen Gras return (result);
822fe8fb19SBen Gras }
832fe8fb19SBen Gras
842fe8fb19SBen Gras /*
852fe8fb19SBen Gras * Private.
862fe8fb19SBen Gras */
872fe8fb19SBen Gras
882fe8fb19SBen Gras /* coverity[+kill] */
892fe8fb19SBen Gras static void
default_assertion_failed(const char * file,int line,assertion_type type,const char * cond,int print_errno)902fe8fb19SBen Gras default_assertion_failed(const char *file, int line, assertion_type type,
912fe8fb19SBen Gras const char *cond, int print_errno)
922fe8fb19SBen Gras {
932fe8fb19SBen Gras fprintf(stderr, "%s:%d: %s(%s)%s%s failed.\n",
942fe8fb19SBen Gras file, line, assertion_type_to_text(type), cond,
952fe8fb19SBen Gras (print_errno) ? ": " : "",
962fe8fb19SBen Gras (print_errno) ? strerror(errno) : "");
972fe8fb19SBen Gras abort();
982fe8fb19SBen Gras /* NOTREACHED */
992fe8fb19SBen Gras }
1002fe8fb19SBen Gras
1012fe8fb19SBen Gras /*! \file */
102