Remove building with NOCRYPTO option
[minix3.git] / external / bsd / atf / dist / atf-c / error.c
blobaeb55a8ba47bf21022a6aa6e2d20fe1129f54832
1 /*
2 * Automated Testing Framework (atf)
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
35 #include "atf-c/error.h"
37 #include "detail/sanity.h"
39 /* Theoretically, there can only be a single error intance at any given
40 * point in time, because errors are raised at one point and must be
41 * handled immediately. If another error has to be raised during the
42 * handling process, something else has to be done with the previous
43 * error.
45 * This is per-thread information and will break threaded tests, but we
46 * currently do not have any threading support; therefore, this is fine. */
47 static bool error_on_flight = false;
49 /* ---------------------------------------------------------------------
50 * Auxiliary functions.
51 * --------------------------------------------------------------------- */
53 static
54 void
55 error_format(const atf_error_t err, char *buf, size_t buflen)
57 PRE(err != NULL);
58 snprintf(buf, buflen, "Error '%s'", err->m_type);
61 static
62 bool
63 error_init(atf_error_t err, const char *type, void *data, size_t datalen,
64 void (*format)(const atf_error_t, char *, size_t))
66 bool ok;
68 PRE(data != NULL || datalen == 0);
69 PRE(datalen != 0 || data == NULL);
71 err->m_free = false;
72 err->m_type = type;
73 err->m_format = (format == NULL) ? error_format : format;
75 ok = true;
76 if (data == NULL) {
77 err->m_data = NULL;
78 } else {
79 err->m_data = malloc(datalen);
80 if (err->m_data == NULL) {
81 ok = false;
82 } else
83 memcpy(err->m_data, data, datalen);
86 return ok;
89 /* ---------------------------------------------------------------------
90 * The "atf_error" type.
91 * --------------------------------------------------------------------- */
93 atf_error_t
94 atf_error_new(const char *type, void *data, size_t datalen,
95 void (*format)(const atf_error_t, char *, size_t))
97 atf_error_t err;
99 PRE(!error_on_flight);
100 PRE(data != NULL || datalen == 0);
101 PRE(datalen != 0 || data == NULL);
103 err = malloc(sizeof(*err));
104 if (err == NULL)
105 err = atf_no_memory_error();
106 else {
107 if (!error_init(err, type, data, datalen, format)) {
108 free(err);
109 err = atf_no_memory_error();
110 } else {
111 err->m_free = true;
112 error_on_flight = true;
116 INV(err != NULL);
117 POST(error_on_flight);
118 return err;
121 void
122 atf_error_free(atf_error_t err)
124 bool freeit;
126 PRE(error_on_flight);
127 PRE(err != NULL);
129 freeit = err->m_free;
131 if (err->m_data != NULL)
132 free(err->m_data);
134 if (freeit)
135 free(err);
137 error_on_flight = false;
140 atf_error_t
141 atf_no_error(void)
143 return NULL;
146 bool
147 atf_is_error(const atf_error_t err)
149 return err != NULL;
152 bool
153 atf_error_is(const atf_error_t err, const char *type)
155 PRE(err != NULL);
157 return strcmp(err->m_type, type) == 0;
160 const void *
161 atf_error_data(const atf_error_t err)
163 PRE(err != NULL);
165 return err->m_data;
168 void
169 atf_error_format(const atf_error_t err, char *buf, size_t buflen)
171 PRE(err != NULL);
172 err->m_format(err, buf, buflen);
175 /* ---------------------------------------------------------------------
176 * Common error types.
177 * --------------------------------------------------------------------- */
180 * The "libc" error.
183 struct atf_libc_error_data {
184 int m_errno;
185 char m_what[4096];
187 typedef struct atf_libc_error_data atf_libc_error_data_t;
189 static
190 void
191 libc_format(const atf_error_t err, char *buf, size_t buflen)
193 const atf_libc_error_data_t *data;
195 PRE(atf_error_is(err, "libc"));
197 data = atf_error_data(err);
198 snprintf(buf, buflen, "%s: %s", data->m_what, strerror(data->m_errno));
201 atf_error_t
202 atf_libc_error(int syserrno, const char *fmt, ...)
204 atf_error_t err;
205 atf_libc_error_data_t data;
206 va_list ap;
208 data.m_errno = syserrno;
209 va_start(ap, fmt);
210 vsnprintf(data.m_what, sizeof(data.m_what), fmt, ap);
211 va_end(ap);
213 err = atf_error_new("libc", &data, sizeof(data), libc_format);
215 return err;
219 atf_libc_error_code(const atf_error_t err)
221 const struct atf_libc_error_data *data;
223 PRE(atf_error_is(err, "libc"));
225 data = atf_error_data(err);
227 return data->m_errno;
230 const char *
231 atf_libc_error_msg(const atf_error_t err)
233 const struct atf_libc_error_data *data;
235 PRE(atf_error_is(err, "libc"));
237 data = atf_error_data(err);
239 return data->m_what;
243 * The "no_memory" error.
246 static struct atf_error no_memory_error;
248 static
249 void
250 no_memory_format(const atf_error_t err, char *buf, size_t buflen)
252 PRE(atf_error_is(err, "no_memory"));
254 snprintf(buf, buflen, "Not enough memory");
257 atf_error_t
258 atf_no_memory_error(void)
260 PRE(!error_on_flight);
262 error_init(&no_memory_error, "no_memory", NULL, 0,
263 no_memory_format);
265 error_on_flight = true;
266 return &no_memory_error;