xref: /freebsd/contrib/atf/atf-c/detail/text.c (revision 145992504973bd16cf3518af9ba5ce185fefa82a)
1 /*
2  * Automated Testing Framework (atf)
3  *
4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
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.
15  *
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.
28  */
29 
30 #include <errno.h>
31 #include <limits.h>
32 #include <string.h>
33 #include <stdlib.h>
34 
35 #include "atf-c/error.h"
36 
37 #include "dynstr.h"
38 #include "sanity.h"
39 #include "text.h"
40 
41 atf_error_t
42 atf_text_for_each_word(const char *instr, const char *sep,
43                        atf_error_t (*func)(const char *, void *),
44                        void *data)
45 {
46     atf_error_t err;
47     char *str, *str2, *last;
48 
49     str = strdup(instr);
50     if (str == NULL) {
51         err = atf_no_memory_error();
52         goto out;
53     }
54 
55     err = atf_no_error();
56     str2 = strtok_r(str, sep, &last);
57     while (str2 != NULL && !atf_is_error(err)) {
58         err = func(str2, data);
59         str2 = strtok_r(NULL, sep, &last);
60     }
61 
62     free(str);
63 out:
64     return err;
65 }
66 
67 atf_error_t
68 atf_text_format(char **dest, const char *fmt, ...)
69 {
70     atf_error_t err;
71     va_list ap;
72 
73     va_start(ap, fmt);
74     err = atf_text_format_ap(dest, fmt, ap);
75     va_end(ap);
76 
77     return err;
78 }
79 
80 atf_error_t
81 atf_text_format_ap(char **dest, const char *fmt, va_list ap)
82 {
83     atf_error_t err;
84     atf_dynstr_t tmp;
85     va_list ap2;
86 
87     va_copy(ap2, ap);
88     err = atf_dynstr_init_ap(&tmp, fmt, ap2);
89     va_end(ap2);
90     if (!atf_is_error(err))
91         *dest = atf_dynstr_fini_disown(&tmp);
92 
93     return err;
94 }
95 
96 atf_error_t
97 atf_text_split(const char *str, const char *delim, atf_list_t *words)
98 {
99     atf_error_t err;
100     const char *end;
101     const char *iter;
102 
103     err = atf_list_init(words);
104     if (atf_is_error(err))
105         goto err;
106 
107     end = str + strlen(str);
108     INV(*end == '\0');
109     iter = str;
110     while (iter < end) {
111         const char *ptr;
112 
113         INV(iter != NULL);
114         ptr = strstr(iter, delim);
115         if (ptr == NULL)
116             ptr = end;
117 
118         INV(ptr >= iter);
119         if (ptr > iter) {
120             atf_dynstr_t word;
121 
122             err = atf_dynstr_init_raw(&word, iter, ptr - iter);
123             if (atf_is_error(err))
124                 goto err_list;
125 
126             err = atf_list_append(words, atf_dynstr_fini_disown(&word), true);
127             if (atf_is_error(err))
128                 goto err_list;
129         }
130 
131         iter = ptr + strlen(delim);
132     }
133 
134     INV(!atf_is_error(err));
135     return err;
136 
137 err_list:
138     atf_list_fini(words);
139 err:
140     return err;
141 }
142 
143 atf_error_t
144 atf_text_to_bool(const char *str, bool *b)
145 {
146     atf_error_t err;
147 
148     if (strcasecmp(str, "yes") == 0 ||
149         strcasecmp(str, "true") == 0) {
150         *b = true;
151         err = atf_no_error();
152     } else if (strcasecmp(str, "no") == 0 ||
153                strcasecmp(str, "false") == 0) {
154         *b = false;
155         err = atf_no_error();
156     } else {
157         /* XXX Not really a libc error. */
158         err = atf_libc_error(EINVAL, "Cannot convert string '%s' "
159                              "to boolean", str);
160     }
161 
162     return err;
163 }
164 
165 atf_error_t
166 atf_text_to_long(const char *str, long *l)
167 {
168     atf_error_t err;
169     char *endptr;
170     long tmp;
171 
172     errno = 0;
173     tmp = strtol(str, &endptr, 10);
174     if (str[0] == '\0' || *endptr != '\0')
175         err = atf_libc_error(EINVAL, "'%s' is not a number", str);
176     else if (errno == ERANGE || (tmp == LONG_MAX || tmp == LONG_MIN))
177         err = atf_libc_error(ERANGE, "'%s' is out of range", str);
178     else {
179         *l = tmp;
180         err = atf_no_error();
181     }
182 
183     return err;
184 }
185