1*11be35a1SLionel Sambuc /*
2*11be35a1SLionel Sambuc * Automated Testing Framework (atf)
3*11be35a1SLionel Sambuc *
4*11be35a1SLionel Sambuc * Copyright (c) 2008 The NetBSD Foundation, Inc.
5*11be35a1SLionel Sambuc * All rights reserved.
6*11be35a1SLionel Sambuc *
7*11be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
8*11be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
9*11be35a1SLionel Sambuc * are met:
10*11be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
11*11be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
12*11be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
13*11be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
14*11be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution.
15*11be35a1SLionel Sambuc *
16*11be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17*11be35a1SLionel Sambuc * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18*11be35a1SLionel Sambuc * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19*11be35a1SLionel Sambuc * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*11be35a1SLionel Sambuc * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21*11be35a1SLionel Sambuc * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*11be35a1SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23*11be35a1SLionel Sambuc * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*11be35a1SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25*11be35a1SLionel Sambuc * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26*11be35a1SLionel Sambuc * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27*11be35a1SLionel Sambuc * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc */
29*11be35a1SLionel Sambuc
30*11be35a1SLionel Sambuc #include <ctype.h>
31*11be35a1SLionel Sambuc #include <stdbool.h>
32*11be35a1SLionel Sambuc #include <stdlib.h>
33*11be35a1SLionel Sambuc #include <string.h>
34*11be35a1SLionel Sambuc
35*11be35a1SLionel Sambuc #include "atf-c/config.h"
36*11be35a1SLionel Sambuc
37*11be35a1SLionel Sambuc #include "detail/env.h"
38*11be35a1SLionel Sambuc #include "detail/sanity.h"
39*11be35a1SLionel Sambuc
40*11be35a1SLionel Sambuc static bool initialized = false;
41*11be35a1SLionel Sambuc
42*11be35a1SLionel Sambuc static struct var {
43*11be35a1SLionel Sambuc const char *name;
44*11be35a1SLionel Sambuc const char *default_value;
45*11be35a1SLionel Sambuc const char *value;
46*11be35a1SLionel Sambuc bool can_be_empty;
47*11be35a1SLionel Sambuc } vars[] = {
48*11be35a1SLionel Sambuc { "atf_build_cc", ATF_BUILD_CC, NULL, false, },
49*11be35a1SLionel Sambuc { "atf_build_cflags", ATF_BUILD_CFLAGS, NULL, true, },
50*11be35a1SLionel Sambuc { "atf_build_cpp", ATF_BUILD_CPP, NULL, false, },
51*11be35a1SLionel Sambuc { "atf_build_cppflags", ATF_BUILD_CPPFLAGS, NULL, true, },
52*11be35a1SLionel Sambuc { "atf_build_cxx", ATF_BUILD_CXX, NULL, false, },
53*11be35a1SLionel Sambuc { "atf_build_cxxflags", ATF_BUILD_CXXFLAGS, NULL, true, },
54*11be35a1SLionel Sambuc { "atf_includedir", ATF_INCLUDEDIR, NULL, false, },
55*11be35a1SLionel Sambuc { "atf_libexecdir", ATF_LIBEXECDIR, NULL, false, },
56*11be35a1SLionel Sambuc { "atf_pkgdatadir", ATF_PKGDATADIR, NULL, false, },
57*11be35a1SLionel Sambuc { "atf_shell", ATF_SHELL, NULL, false, },
58*11be35a1SLionel Sambuc { "atf_workdir", ATF_WORKDIR, NULL, false, },
59*11be35a1SLionel Sambuc { NULL, NULL, NULL, false, },
60*11be35a1SLionel Sambuc };
61*11be35a1SLionel Sambuc
62*11be35a1SLionel Sambuc /* Only used for unit testing, so this prototype is private. */
63*11be35a1SLionel Sambuc void __atf_config_reinit(void);
64*11be35a1SLionel Sambuc
65*11be35a1SLionel Sambuc /* ---------------------------------------------------------------------
66*11be35a1SLionel Sambuc * Auxiliary functions.
67*11be35a1SLionel Sambuc * --------------------------------------------------------------------- */
68*11be35a1SLionel Sambuc
69*11be35a1SLionel Sambuc static
70*11be35a1SLionel Sambuc char *
string_to_upper(const char * str)71*11be35a1SLionel Sambuc string_to_upper(const char *str)
72*11be35a1SLionel Sambuc {
73*11be35a1SLionel Sambuc char *uc;
74*11be35a1SLionel Sambuc
75*11be35a1SLionel Sambuc uc = (char *)malloc(strlen(str) + 1);
76*11be35a1SLionel Sambuc if (uc != NULL) {
77*11be35a1SLionel Sambuc char *ucptr = uc;
78*11be35a1SLionel Sambuc while (*str != '\0') {
79*11be35a1SLionel Sambuc *ucptr = toupper((int)*str);
80*11be35a1SLionel Sambuc
81*11be35a1SLionel Sambuc str++;
82*11be35a1SLionel Sambuc ucptr++;
83*11be35a1SLionel Sambuc }
84*11be35a1SLionel Sambuc *ucptr = '\0';
85*11be35a1SLionel Sambuc }
86*11be35a1SLionel Sambuc
87*11be35a1SLionel Sambuc return uc;
88*11be35a1SLionel Sambuc }
89*11be35a1SLionel Sambuc
90*11be35a1SLionel Sambuc static
91*11be35a1SLionel Sambuc void
initialize_var(struct var * var,const char * envname)92*11be35a1SLionel Sambuc initialize_var(struct var *var, const char *envname)
93*11be35a1SLionel Sambuc {
94*11be35a1SLionel Sambuc PRE(var->value == NULL);
95*11be35a1SLionel Sambuc
96*11be35a1SLionel Sambuc if (atf_env_has(envname)) {
97*11be35a1SLionel Sambuc const char *val = atf_env_get(envname);
98*11be35a1SLionel Sambuc if (strlen(val) > 0 || var->can_be_empty)
99*11be35a1SLionel Sambuc var->value = val;
100*11be35a1SLionel Sambuc else
101*11be35a1SLionel Sambuc var->value = var->default_value;
102*11be35a1SLionel Sambuc } else
103*11be35a1SLionel Sambuc var->value = var->default_value;
104*11be35a1SLionel Sambuc
105*11be35a1SLionel Sambuc POST(var->value != NULL);
106*11be35a1SLionel Sambuc }
107*11be35a1SLionel Sambuc
108*11be35a1SLionel Sambuc static
109*11be35a1SLionel Sambuc void
initialize(void)110*11be35a1SLionel Sambuc initialize(void)
111*11be35a1SLionel Sambuc {
112*11be35a1SLionel Sambuc struct var *var;
113*11be35a1SLionel Sambuc
114*11be35a1SLionel Sambuc PRE(!initialized);
115*11be35a1SLionel Sambuc
116*11be35a1SLionel Sambuc for (var = vars; var->name != NULL; var++) {
117*11be35a1SLionel Sambuc char *envname;
118*11be35a1SLionel Sambuc
119*11be35a1SLionel Sambuc envname = string_to_upper(var->name);
120*11be35a1SLionel Sambuc initialize_var(var, envname);
121*11be35a1SLionel Sambuc free(envname);
122*11be35a1SLionel Sambuc }
123*11be35a1SLionel Sambuc
124*11be35a1SLionel Sambuc initialized = true;
125*11be35a1SLionel Sambuc }
126*11be35a1SLionel Sambuc
127*11be35a1SLionel Sambuc /* ---------------------------------------------------------------------
128*11be35a1SLionel Sambuc * Free functions.
129*11be35a1SLionel Sambuc * --------------------------------------------------------------------- */
130*11be35a1SLionel Sambuc
131*11be35a1SLionel Sambuc const char *
atf_config_get(const char * name)132*11be35a1SLionel Sambuc atf_config_get(const char *name)
133*11be35a1SLionel Sambuc {
134*11be35a1SLionel Sambuc const struct var *var;
135*11be35a1SLionel Sambuc const char *value;
136*11be35a1SLionel Sambuc
137*11be35a1SLionel Sambuc if (!initialized) {
138*11be35a1SLionel Sambuc initialize();
139*11be35a1SLionel Sambuc INV(initialized);
140*11be35a1SLionel Sambuc }
141*11be35a1SLionel Sambuc
142*11be35a1SLionel Sambuc value = NULL;
143*11be35a1SLionel Sambuc for (var = vars; value == NULL && var->name != NULL; var++)
144*11be35a1SLionel Sambuc if (strcmp(var->name, name) == 0)
145*11be35a1SLionel Sambuc value = var->value;
146*11be35a1SLionel Sambuc INV(value != NULL);
147*11be35a1SLionel Sambuc
148*11be35a1SLionel Sambuc return value;
149*11be35a1SLionel Sambuc }
150*11be35a1SLionel Sambuc
151*11be35a1SLionel Sambuc void
__atf_config_reinit(void)152*11be35a1SLionel Sambuc __atf_config_reinit(void)
153*11be35a1SLionel Sambuc {
154*11be35a1SLionel Sambuc struct var *var;
155*11be35a1SLionel Sambuc
156*11be35a1SLionel Sambuc initialized = false;
157*11be35a1SLionel Sambuc
158*11be35a1SLionel Sambuc for (var = vars; var->name != NULL; var++)
159*11be35a1SLionel Sambuc var->value = NULL;
160*11be35a1SLionel Sambuc }
161