xref: /illumos-gate/usr/src/lib/libtnf/error.c (revision 7c478bd9)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  *	Copyright (c) 1994, by Sun Microsytems, Inc.
24  */
25 
26 #pragma	ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include "libtnf.h"
29 #include <libintl.h>
30 
31 #if !defined(TEXT_DOMAIN)
32 #define	TEXT_DOMAIN	"SYS_TEST"
33 #endif
34 
35 /*
36  *
37  */
38 
39 static tnf_error_handler_t *_err_handler = &tnf_default_error_handler;
40 static void *_err_arg = 0;
41 
42 /*
43  *
44  */
45 
46 void
tnf_set_error_handler(tnf_error_handler_t * handler,void * arg)47 tnf_set_error_handler(tnf_error_handler_t *handler, void *arg)
48 {
49 	/* XXX Not MT-safe */
50 	_err_arg = arg;
51 	_err_handler = handler;
52 }
53 
54 /*
55  *
56  */
57 
58 void
_tnf_error(TNF * tnf,tnf_errcode_t err)59 _tnf_error(TNF *tnf, tnf_errcode_t err)
60 {
61 	(*_err_handler)(_err_arg, tnf, err);
62 }
63 
64 /*
65  *
66  */
67 
68 char *
tnf_error_message(tnf_errcode_t err)69 tnf_error_message(tnf_errcode_t err)
70 {
71 	if (err == TNF_ERR_NONE)
72 		return (dgettext(TEXT_DOMAIN, "no error"));
73 	else if (err <= TNF_ERRNO_MAX)
74 		return (strerror(err));
75 	else {
76 		switch (err) {
77 		case TNF_ERR_NOTTNF:
78 			return (dgettext(TEXT_DOMAIN, "not a TNF file"));
79 		case TNF_ERR_BADDATUM:
80 			return (dgettext(TEXT_DOMAIN,
81 				"operation on bad or NULL data handle"));
82 		case TNF_ERR_TYPEMISMATCH:
83 			return (dgettext(TEXT_DOMAIN, "type mismatch"));
84 		case TNF_ERR_BADINDEX:
85 			return (dgettext(TEXT_DOMAIN, "index out of bounds"));
86 		case TNF_ERR_BADSLOT:
87 			return (dgettext(TEXT_DOMAIN, "no such slot"));
88 		case TNF_ERR_BADREFTYPE:
89 			return (dgettext(TEXT_DOMAIN, "bad reference type"));
90 		case TNF_ERR_ALLOCFAIL:
91 			return (dgettext(TEXT_DOMAIN,
92 				"memory allocation failure"));
93 		case TNF_ERR_BADTNF:
94 			return (dgettext(TEXT_DOMAIN, "bad TNF file"));
95 		case TNF_ERR_INTERNAL:
96 			return (dgettext(TEXT_DOMAIN, "internal error"));
97 		default:
98 			return (dgettext(TEXT_DOMAIN, "unknown error code"));
99 		}
100 	}
101 }
102 
103 /*
104  *
105  */
106 
107 void
108 /* ARGSUSED */
tnf_default_error_handler(void * arg,TNF * tnf,tnf_errcode_t err)109 tnf_default_error_handler(void *arg, TNF *tnf, tnf_errcode_t err)
110 {
111 	fprintf(stderr, dgettext(TEXT_DOMAIN, "error: libtnf: %d: %s\n"),
112 		err, tnf_error_message(err));
113 	abort();
114 }
115