5 * Jordan Hubbard. All rights reserved.
7 * Murray Stokely. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer,
14 * verbatim and that no modifications are made prior to this
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 /* Routines for dealing with variable lists */
39 make_variable(char *var, char *value, int dirty)
43 /* Trim leading and trailing whitespace */
44 var = string_skipwhite(string_prune(var));
50 /* Now search to see if it's already in the list */
51 for (vp = VarHead; vp; vp = vp->next) {
52 if (!strcmp(vp->name, var)) {
53 if (vp->dirty && !dirty)
55 setenv(var, value, 1);
57 vp->value = strdup(value);
64 setenv(var, value, 1);
65 /* No? Create a new one */
66 vp = (Variable *)safe_malloc(sizeof(Variable));
67 vp->name = strdup(var);
68 vp->value = strdup(value);
77 variable_set(char *var, int dirty)
82 msgFatal("NULL variable name & value passed.");
84 msgDebug("Warning: Zero length name & value passed to variable_set()\n");
85 SAFE_STRCPY(tmp, var);
86 if ((cp = index(tmp, '=')) == NULL)
87 msgFatal("Invalid variable format: %s", var);
89 make_variable(tmp, string_skipwhite(cp), dirty);
93 variable_set2(char *var, char *value, int dirty)
96 msgFatal("Null name or value passed to set_variable2(%s) = %s!",
97 var ? var : "", value ? value : "");
98 else if (!*var || !*value)
99 msgDebug("Warning: Zero length name or value passed to variable_set2(%s) = %s\n",
101 make_variable(var, value, dirty);
105 variable_get(char *var)
111 variable_cmp(char *var, char *value)
115 if ((val = variable_get(var)))
116 return strcmp(val, value);
121 variable_unset(char *var)
126 if ((cp = index(var, '=')) != NULL)
127 sstrncpy(name, var, cp - var);
129 SAFE_STRCPY(name, var);
131 /* Now search to see if it's in our list, if we have one.. */
134 else if (!VarHead->next && !strcmp(VarHead->name, name)) {
135 safe_free(VarHead->name);
136 safe_free(VarHead->value);
141 for (vp = VarHead; vp; vp = vp->next) {
142 if (!strcmp(vp->name, name)) {
143 Variable *save = vp->next;
146 safe_free(vp->value);
155 /* Prompt user for the name of a variable */
157 variable_get_value(char *var, char *prompt, int dirty)
161 cp = variable_get(var);
162 if (cp && variable_get(VAR_NONINTERACTIVE))
164 else if ((cp = msgGetInput(cp, "%s", prompt)) != NULL)
165 variable_set2(var, cp, dirty);
171 /* Check if value passed in data (in the form "variable=value") is
172 * valid, and it's status compared to the value of variable stored in
175 * Possible return values :
176 * -3: Invalid line, the data string is NOT set as an env variable
177 * -2: Invalid line, the data string is set as an env variable
179 * 0: Valid line, is NOT equal to env version
180 * 1: Valid line, is equal to env version
181 * 2: Valid line, value empty - e.g. foo=""
182 * 3: Valid line, does not exist in env
185 variable_check2(char *data)
187 char *cp, *cp2, *cp3, tmp[256];
191 SAFE_STRCPY(tmp, data);
192 if ((cp = index(tmp, '=')) != NULL) {
194 if (*cp == '"') { /* smash quotes if present */
196 if ((cp3 = index(cp, '"')) != NULL)
199 else if ((cp3 = index(cp, ',')) != NULL)
201 cp2 = variable_get(tmp);
206 return strcmp(cp, cp2) == 0 ? 1 : 0;
212 return variable_get(tmp) != NULL ? -2 : -3;
215 /* Check if the value passed in data (in the form "variable=value") is
216 equal to the value of variable stored in env */
218 variable_check(char *data)
221 ret = variable_check2(data);
235 dump_variables(dialogMenuItem *unused)
241 msgDebug("Writing sysinstall variables to file..\n");
243 fp = fopen("/etc/sysinstall.vars", "w");
245 msgConfirm("Unable to write to /etc/sysinstall.vars: %s",
247 return DITEM_FAILURE;
250 for (vp = VarHead; vp; vp = vp->next)
251 fprintf(fp, "%s=\"%s\" (%d)\n", vp->name, vp->value, vp->dirty);
255 return DITEM_SUCCESS;
258 /* Free all of the variables, useful to really start over as when the
259 user selects "restart" from the interrupt menu. */
265 /* Free the variables from our list, if we have one.. */
268 else if (!VarHead->next) {
269 unsetenv(VarHead->name);
270 safe_free(VarHead->name);
271 safe_free(VarHead->value);
276 for (vp = VarHead; vp; ) {
280 safe_free(vp->value);
289 * Persistent variables. The variables modified by these functions
290 * are not cleared between invocations of sysinstall. This is useful
291 * to allow the user to completely restart sysinstall, without having
292 * it load all of the modules again from the installation media which
293 * are still in memory.
297 pvariable_set(char *var)
302 msgFatal("NULL variable name & value passed.");
304 msgDebug("Warning: Zero length name & value passed to variable_set()\n");
305 /* Add a trivial namespace to whatever name the caller chooses. */
306 SAFE_STRCPY(tmp, "SYSINSTALL_PVAR");
307 if (index(var, '=') == NULL)
308 msgFatal("Invalid variable format: %s", var);
309 strlcat(tmp, var, 1024);
314 pvariable_get(char *var)
318 SAFE_STRCPY(tmp, "SYSINSTALL_PVAR");
319 strlcat(tmp, var, 1024);