Expand PMF_FN_* macros.
[netbsd-mini2440.git] / external / bsd / atf / dist / atf-c / text.c
blob93e10c1c4a413f3edbb7b0d902d261b132a1cbfa
1 /*
2 * Automated Testing Framework (atf)
4 * Copyright (c) 2008, 2009 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 <errno.h>
31 #include <limits.h>
32 #include <string.h>
33 #include <stdlib.h>
35 #include "atf-c/dynstr.h"
36 #include "atf-c/error.h"
37 #include "atf-c/sanity.h"
38 #include "atf-c/text.h"
40 atf_error_t
41 atf_text_for_each_word(const char *instr, const char *sep,
42 atf_error_t (*func)(const char *, void *),
43 void *data)
45 atf_error_t err;
46 char *str, *str2, *last;
48 str = strdup(instr);
49 if (str == NULL) {
50 err = atf_no_memory_error();
51 goto out;
54 err = atf_no_error();
55 str2 = strtok_r(str, sep, &last);
56 while (str2 != NULL && !atf_is_error(err)) {
57 err = func(str2, data);
58 str2 = strtok_r(NULL, sep, &last);
61 free(str);
62 out:
63 return err;
66 atf_error_t
67 atf_text_format(char **dest, const char *fmt, ...)
69 atf_error_t err;
70 va_list ap;
72 va_start(ap, fmt);
73 err = atf_text_format_ap(dest, fmt, ap);
74 va_end(ap);
76 return err;
79 atf_error_t
80 atf_text_format_ap(char **dest, const char *fmt, va_list ap)
82 atf_error_t err;
83 atf_dynstr_t tmp;
84 va_list ap2;
86 va_copy(ap2, ap);
87 err = atf_dynstr_init_ap(&tmp, fmt, ap2);
88 va_end(ap2);
89 if (!atf_is_error(err))
90 *dest = atf_dynstr_fini_disown(&tmp);
92 return err;
95 atf_error_t
96 atf_text_split(const char *str, const char *delim, atf_list_t *words)
98 atf_error_t err;
99 const char *end;
100 const char *iter;
102 err = atf_list_init(words);
103 if (atf_is_error(err))
104 goto err;
106 end = str + strlen(str);
107 INV(*end == '\0');
108 iter = str;
109 while (iter < end) {
110 const char *ptr;
112 INV(iter != NULL);
113 ptr = strstr(iter, delim);
114 if (ptr == NULL)
115 ptr = end;
117 INV(ptr >= iter);
118 if (ptr > iter) {
119 atf_dynstr_t word;
121 err = atf_dynstr_init_raw(&word, iter, ptr - iter);
122 if (atf_is_error(err))
123 goto err_list;
125 err = atf_list_append(words, atf_dynstr_fini_disown(&word), true);
126 if (atf_is_error(err))
127 goto err_list;
130 iter = ptr + strlen(delim);
133 INV(!atf_is_error(err));
134 return err;
136 err_list:
137 atf_list_fini(words);
138 err:
139 return err;
142 atf_error_t
143 atf_text_to_bool(const char *str, bool *b)
145 atf_error_t err;
147 if (strcasecmp(str, "yes") == 0 ||
148 strcasecmp(str, "true") == 0) {
149 *b = true;
150 err = atf_no_error();
151 } else if (strcasecmp(str, "no") == 0 ||
152 strcasecmp(str, "false") == 0) {
153 *b = false;
154 err = atf_no_error();
155 } else {
156 /* XXX Not really a libc error. */
157 err = atf_libc_error(EINVAL, "Cannot convert string '%s' "
158 "to boolean", str);
161 return err;
164 atf_error_t
165 atf_text_to_long(const char *str, long *l)
167 atf_error_t err;
168 char *endptr;
169 long tmp;
171 errno = 0;
172 tmp = strtol(str, &endptr, 10);
173 if (str[0] == '\0' || *endptr != '\0')
174 err = atf_libc_error(EINVAL, "'%s' is not a number", str);
175 else if (errno == ERANGE || (tmp == LONG_MAX || tmp == LONG_MIN))
176 err = atf_libc_error(ERANGE, "'%s' is out of range", str);
177 else {
178 *l = tmp;
179 err = atf_no_error();
182 return err;