1 /* $NetBSD: text.c,v 1.3 2014/12/10 04:38:03 christos Exp $ */
2
3 /*
4 * Automated Testing Framework (atf)
5 *
6 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
19 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
29 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <errno.h>
33 #include <limits.h>
34 #include <string.h>
35 #include <stdlib.h>
36
37 #include "atf-c/error.h"
38
39 #include "dynstr.h"
40 #include "sanity.h"
41 #include "text.h"
42
43 atf_error_t
atf_text_for_each_word(const char * instr,const char * sep,atf_error_t (* func)(const char *,void *),void * data)44 atf_text_for_each_word(const char *instr, const char *sep,
45 atf_error_t (*func)(const char *, void *),
46 void *data)
47 {
48 atf_error_t err;
49 char *str, *str2, *last;
50
51 str = strdup(instr);
52 if (str == NULL) {
53 err = atf_no_memory_error();
54 goto out;
55 }
56
57 err = atf_no_error();
58 str2 = strtok_r(str, sep, &last);
59 while (str2 != NULL && !atf_is_error(err)) {
60 err = func(str2, data);
61 str2 = strtok_r(NULL, sep, &last);
62 }
63
64 free(str);
65 out:
66 return err;
67 }
68
69 atf_error_t
atf_text_format(char ** dest,const char * fmt,...)70 atf_text_format(char **dest, const char *fmt, ...)
71 {
72 atf_error_t err;
73 va_list ap;
74
75 va_start(ap, fmt);
76 err = atf_text_format_ap(dest, fmt, ap);
77 va_end(ap);
78
79 return err;
80 }
81
82 atf_error_t
atf_text_format_ap(char ** dest,const char * fmt,va_list ap)83 atf_text_format_ap(char **dest, const char *fmt, va_list ap)
84 {
85 atf_error_t err;
86 atf_dynstr_t tmp;
87 va_list ap2;
88
89 va_copy(ap2, ap);
90 err = atf_dynstr_init_ap(&tmp, fmt, ap2);
91 va_end(ap2);
92 if (!atf_is_error(err))
93 *dest = atf_dynstr_fini_disown(&tmp);
94
95 return err;
96 }
97
98 atf_error_t
atf_text_split(const char * str,const char * delim,atf_list_t * words)99 atf_text_split(const char *str, const char *delim, atf_list_t *words)
100 {
101 atf_error_t err;
102 const char *end;
103 const char *iter;
104
105 err = atf_list_init(words);
106 if (atf_is_error(err))
107 goto err;
108
109 end = str + strlen(str);
110 INV(*end == '\0');
111 iter = str;
112 while (iter < end) {
113 const char *ptr;
114
115 INV(iter != NULL);
116 ptr = strstr(iter, delim);
117 if (ptr == NULL)
118 ptr = end;
119
120 INV(ptr >= iter);
121 if (ptr > iter) {
122 atf_dynstr_t word;
123
124 err = atf_dynstr_init_raw(&word, iter, ptr - iter);
125 if (atf_is_error(err))
126 goto err_list;
127
128 err = atf_list_append(words, atf_dynstr_fini_disown(&word), true);
129 if (atf_is_error(err))
130 goto err_list;
131 }
132
133 iter = ptr + strlen(delim);
134 }
135
136 INV(!atf_is_error(err));
137 return err;
138
139 err_list:
140 atf_list_fini(words);
141 err:
142 return err;
143 }
144
145 atf_error_t
atf_text_to_bool(const char * str,bool * b)146 atf_text_to_bool(const char *str, bool *b)
147 {
148 atf_error_t err;
149
150 if (strcasecmp(str, "yes") == 0 ||
151 strcasecmp(str, "true") == 0) {
152 *b = true;
153 err = atf_no_error();
154 } else if (strcasecmp(str, "no") == 0 ||
155 strcasecmp(str, "false") == 0) {
156 *b = false;
157 err = atf_no_error();
158 } else {
159 /* XXX Not really a libc error. */
160 err = atf_libc_error(EINVAL, "Cannot convert string '%s' "
161 "to boolean", str);
162 }
163
164 return err;
165 }
166
167 atf_error_t
atf_text_to_long(const char * str,long * l)168 atf_text_to_long(const char *str, long *l)
169 {
170 atf_error_t err;
171 char *endptr;
172 long tmp;
173
174 errno = 0;
175 tmp = strtol(str, &endptr, 10);
176 if (str[0] == '\0' || *endptr != '\0')
177 err = atf_libc_error(EINVAL, "'%s' is not a number", str);
178 else if (errno == ERANGE || (tmp == LONG_MAX || tmp == LONG_MIN))
179 err = atf_libc_error(ERANGE, "'%s' is out of range", str);
180 else {
181 *l = tmp;
182 err = atf_no_error();
183 }
184
185 return err;
186 }
187