Drop main() prototype. Syncs with NetBSD-8
[minix.git] / external / bsd / atf / dist / atf-c / config.c
blob3c2c8a9a61eb1a96a37a60e95aeadde50b5e0e39
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 <ctype.h>
31 #include <stdbool.h>
32 #include <stdlib.h>
33 #include <string.h>
35 #include "atf-c/config.h"
37 #include "detail/env.h"
38 #include "detail/sanity.h"
40 static bool initialized = false;
42 static struct var {
43 const char *name;
44 const char *default_value;
45 const char *value;
46 bool can_be_empty;
47 } vars[] = {
48 { "atf_build_cc", ATF_BUILD_CC, NULL, false, },
49 { "atf_build_cflags", ATF_BUILD_CFLAGS, NULL, true, },
50 { "atf_build_cpp", ATF_BUILD_CPP, NULL, false, },
51 { "atf_build_cppflags", ATF_BUILD_CPPFLAGS, NULL, true, },
52 { "atf_build_cxx", ATF_BUILD_CXX, NULL, false, },
53 { "atf_build_cxxflags", ATF_BUILD_CXXFLAGS, NULL, true, },
54 { "atf_includedir", ATF_INCLUDEDIR, NULL, false, },
55 { "atf_libexecdir", ATF_LIBEXECDIR, NULL, false, },
56 { "atf_pkgdatadir", ATF_PKGDATADIR, NULL, false, },
57 { "atf_shell", ATF_SHELL, NULL, false, },
58 { "atf_workdir", ATF_WORKDIR, NULL, false, },
59 { NULL, NULL, NULL, false, },
62 /* Only used for unit testing, so this prototype is private. */
63 void __atf_config_reinit(void);
65 /* ---------------------------------------------------------------------
66 * Auxiliary functions.
67 * --------------------------------------------------------------------- */
69 static
70 char *
71 string_to_upper(const char *str)
73 char *uc;
75 uc = (char *)malloc(strlen(str) + 1);
76 if (uc != NULL) {
77 char *ucptr = uc;
78 while (*str != '\0') {
79 *ucptr = toupper((int)*str);
81 str++;
82 ucptr++;
84 *ucptr = '\0';
87 return uc;
90 static
91 void
92 initialize_var(struct var *var, const char *envname)
94 PRE(var->value == NULL);
96 if (atf_env_has(envname)) {
97 const char *val = atf_env_get(envname);
98 if (strlen(val) > 0 || var->can_be_empty)
99 var->value = val;
100 else
101 var->value = var->default_value;
102 } else
103 var->value = var->default_value;
105 POST(var->value != NULL);
108 static
109 void
110 initialize(void)
112 struct var *var;
114 PRE(!initialized);
116 for (var = vars; var->name != NULL; var++) {
117 char *envname;
119 envname = string_to_upper(var->name);
120 initialize_var(var, envname);
121 free(envname);
124 initialized = true;
127 /* ---------------------------------------------------------------------
128 * Free functions.
129 * --------------------------------------------------------------------- */
131 const char *
132 atf_config_get(const char *name)
134 const struct var *var;
135 const char *value;
137 if (!initialized) {
138 initialize();
139 INV(initialized);
142 value = NULL;
143 for (var = vars; value == NULL && var->name != NULL; var++)
144 if (strcmp(var->name, name) == 0)
145 value = var->value;
146 INV(value != NULL);
148 return value;
151 void
152 __atf_config_reinit(void)
154 struct var *var;
156 initialized = false;
158 for (var = vars; var->name != NULL; var++)
159 var->value = NULL;