1 /*	$NetBSD: config.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 <ctype.h>
33 #include <stdbool.h>
34 #include <stdlib.h>
35 #include <string.h>
36 
37 #include "atf-c/config.h"
38 
39 #include "detail/env.h"
40 #include "detail/sanity.h"
41 
42 static bool initialized = false;
43 
44 static struct var {
45     const char *name;
46     const char *default_value;
47     const char *value;
48     bool can_be_empty;
49 } vars[] = {
50     { "atf_arch",           ATF_ARCH,           NULL, false, },
51     { "atf_build_cc",       ATF_BUILD_CC,       NULL, false, },
52     { "atf_build_cflags",   ATF_BUILD_CFLAGS,   NULL, true,  },
53     { "atf_build_cpp",      ATF_BUILD_CPP,      NULL, false, },
54     { "atf_build_cppflags", ATF_BUILD_CPPFLAGS, NULL, true,  },
55     { "atf_build_cxx",      ATF_BUILD_CXX,      NULL, false, },
56     { "atf_build_cxxflags", ATF_BUILD_CXXFLAGS, NULL, true,  },
57     { "atf_confdir",        ATF_CONFDIR,        NULL, false, },
58     { "atf_includedir",     ATF_INCLUDEDIR,     NULL, false, },
59     { "atf_libdir",         ATF_LIBDIR,         NULL, false, },
60     { "atf_libexecdir",     ATF_LIBEXECDIR,     NULL, false, },
61     { "atf_machine",        ATF_MACHINE,        NULL, false, },
62     { "atf_pkgdatadir",     ATF_PKGDATADIR,     NULL, false, },
63     { "atf_shell",          ATF_SHELL,          NULL, false, },
64     { "atf_workdir",        ATF_WORKDIR,        NULL, false, },
65     { NULL,                 NULL,               NULL, false, },
66 };
67 
68 /* Only used for unit testing, so this prototype is private. */
69 void __atf_config_reinit(void);
70 
71 /* ---------------------------------------------------------------------
72  * Auxiliary functions.
73  * --------------------------------------------------------------------- */
74 
75 static
76 char *
77 string_to_upper(const char *str)
78 {
79     char *uc;
80 
81     uc = (char *)malloc(strlen(str) + 1);
82     if (uc != NULL) {
83         char *ucptr = uc;
84         while (*str != '\0') {
85             *ucptr = toupper((int)*str);
86 
87             str++;
88             ucptr++;
89         }
90         *ucptr = '\0';
91     }
92 
93     return uc;
94 }
95 
96 static
97 void
98 initialize_var(struct var *var, const char *envname)
99 {
100     PRE(var->value == NULL);
101 
102     if (atf_env_has(envname)) {
103         const char *val = atf_env_get(envname);
104         if (strlen(val) > 0 || var->can_be_empty)
105             var->value = val;
106         else
107             var->value = var->default_value;
108     } else
109         var->value = var->default_value;
110 
111     POST(var->value != NULL);
112 }
113 
114 static
115 void
116 initialize(void)
117 {
118     struct var *var;
119 
120     PRE(!initialized);
121 
122     for (var = vars; var->name != NULL; var++) {
123         char *envname;
124 
125         envname = string_to_upper(var->name);
126         initialize_var(var, envname);
127         free(envname);
128     }
129 
130     initialized = true;
131 }
132 
133 /* ---------------------------------------------------------------------
134  * Free functions.
135  * --------------------------------------------------------------------- */
136 
137 const char *
138 atf_config_get(const char *name)
139 {
140     const struct var *var;
141     const char *value;
142 
143     if (!initialized) {
144         initialize();
145         INV(initialized);
146     }
147 
148     value = NULL;
149     for (var = vars; value == NULL && var->name != NULL; var++)
150         if (strcmp(var->name, name) == 0)
151             value = var->value;
152     INV(value != NULL);
153 
154     return value;
155 }
156 
157 void
158 __atf_config_reinit(void)
159 {
160     struct var *var;
161 
162     initialized = false;
163 
164     for (var = vars; var->name != NULL; var++)
165         var->value = NULL;
166 }
167