xref: /netbsd-src/external/bsd/atf/dist/atf-c/config.c (revision 10ad5ffa714ce1a679dcc9dd8159648df2d67b5a)
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 <stdbool.h>
31 #include <string.h>
32 
33 #include "atf-c/config.h"
34 #include "atf-c/env.h"
35 #include "atf-c/sanity.h"
36 
37 static bool initialized = false;
38 static const char *atf_arch = NULL;
39 static const char *atf_confdir = NULL;
40 static const char *atf_libexecdir = NULL;
41 static const char *atf_machine = NULL;
42 static const char *atf_pkgdatadir = NULL;
43 static const char *atf_shell = NULL;
44 static const char *atf_workdir = NULL;
45 
46 /* Only used for unit testing, so this prototype is private. */
47 void __atf_config_reinit(void);
48 
49 /* ---------------------------------------------------------------------
50  * Auxiliary functions.
51  * --------------------------------------------------------------------- */
52 
53 static
54 void
55 initialize_var(char const **valptr, const char *name, const char *def)
56 {
57     PRE(*valptr == NULL);
58 
59     if (atf_env_has(name)) {
60         const char *val = atf_env_get(name);
61         if (strlen(val) > 0)
62             *valptr = val;
63         else
64             *valptr = def;
65     } else
66         *valptr = def;
67 
68     POST(*valptr != NULL);
69 }
70 
71 static
72 void
73 initialize(void)
74 {
75     PRE(!initialized);
76 
77     initialize_var(&atf_arch,       "ATF_ARCH",       ATF_ARCH);
78     initialize_var(&atf_confdir,    "ATF_CONFDIR",    ATF_CONFDIR);
79     initialize_var(&atf_libexecdir, "ATF_LIBEXECDIR", ATF_LIBEXECDIR);
80     initialize_var(&atf_machine,    "ATF_MACHINE",    ATF_MACHINE);
81     initialize_var(&atf_pkgdatadir, "ATF_PKGDATADIR", ATF_PKGDATADIR);
82     initialize_var(&atf_shell,      "ATF_SHELL",      ATF_SHELL);
83     initialize_var(&atf_workdir,    "ATF_WORKDIR",    ATF_WORKDIR);
84 
85     initialized = true;
86 }
87 
88 /* ---------------------------------------------------------------------
89  * Free functions.
90  * --------------------------------------------------------------------- */
91 
92 const char *
93 atf_config_get(const char *var)
94 {
95     const char *value;
96 
97     if (!initialized) {
98         initialize();
99         INV(initialized);
100     }
101 
102     if (strcmp(var, "atf_arch") == 0)
103         value = atf_arch;
104     else if (strcmp(var, "atf_confdir") == 0)
105         value = atf_confdir;
106     else if (strcmp(var, "atf_libexecdir") == 0)
107         value = atf_libexecdir;
108     else if (strcmp(var, "atf_machine") == 0)
109         value = atf_machine;
110     else if (strcmp(var, "atf_pkgdatadir") == 0)
111         value = atf_pkgdatadir;
112     else if (strcmp(var, "atf_shell") == 0)
113         value = atf_shell;
114     else if (strcmp(var, "atf_workdir") == 0)
115         value = atf_workdir;
116     else {
117         UNREACHABLE;
118         value = NULL;
119     }
120 
121     return value;
122 }
123 
124 void
125 __atf_config_reinit(void)
126 {
127     initialized = false;
128     atf_arch = NULL;
129     atf_confdir = NULL;
130     atf_libexecdir = NULL;
131     atf_machine = NULL;
132     atf_pkgdatadir = NULL;
133     atf_shell = NULL;
134     atf_workdir = NULL;
135 }
136