2 * The new sysinstall program.
4 * This is probably the last program in the `sysinstall' line - the next
5 * generation being essentially a complete rewrite.
7 * $FreeBSD: src/release/sysinstall/variable.c,v 1.30.2.3 2001/09/27 06:57:45 murray Exp $
8 * $DragonFly: src/release/sysinstall/Attic/variable.c,v 1.2 2003/06/17 04:27:21 dillon Exp $
11 * Jordan Hubbard. All rights reserved.
13 * Murray Stokely. All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer,
20 * verbatim and that no modifications are made prior to this
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
26 * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #include "sysinstall.h"
42 /* Routines for dealing with variable lists */
45 make_variable(char *var, char *value, int dirty)
49 /* Trim leading and trailing whitespace */
50 var = string_skipwhite(string_prune(var));
56 /* Now search to see if it's already in the list */
57 for (vp = VarHead; vp; vp = vp->next) {
58 if (!strcmp(vp->name, var)) {
59 if (vp->dirty && !dirty)
61 setenv(var, value, 1);
63 vp->value = strdup(value);
70 setenv(var, value, 1);
71 /* No? Create a new one */
72 vp = (Variable *)safe_malloc(sizeof(Variable));
73 vp->name = strdup(var);
74 vp->value = strdup(value);
83 variable_set(char *var, int dirty)
88 msgFatal("NULL variable name & value passed.");
90 msgDebug("Warning: Zero length name & value passed to variable_set()\n");
91 SAFE_STRCPY(tmp, var);
92 if ((cp = index(tmp, '=')) == NULL)
93 msgFatal("Invalid variable format: %s", var);
95 make_variable(tmp, string_skipwhite(cp), dirty);
99 variable_set2(char *var, char *value, int dirty)
102 msgFatal("Null name or value passed to set_variable2(%s) = %s!",
103 var ? var : "", value ? value : "");
104 else if (!*var || !*value)
105 msgDebug("Warning: Zero length name or value passed to variable_set2(%s) = %s\n",
107 make_variable(var, value, dirty);
111 variable_get(char *var)
117 variable_cmp(char *var, char *value)
121 if ((val = variable_get(var)))
122 return strcmp(val, value);
127 variable_unset(char *var)
132 if ((cp = index(var, '=')) != NULL)
133 sstrncpy(name, var, cp - var);
135 SAFE_STRCPY(name, var);
137 /* Now search to see if it's in our list, if we have one.. */
140 else if (!VarHead->next && !strcmp(VarHead->name, name)) {
141 safe_free(VarHead->name);
142 safe_free(VarHead->value);
147 for (vp = VarHead; vp; vp = vp->next) {
148 if (!strcmp(vp->name, name)) {
149 Variable *save = vp->next;
152 safe_free(vp->value);
161 /* Prompt user for the name of a variable */
163 variable_get_value(char *var, char *prompt, int dirty)
167 cp = variable_get(var);
168 if (cp && variable_get(VAR_NONINTERACTIVE))
170 else if ((cp = msgGetInput(cp, "%s", prompt)) != NULL)
171 variable_set2(var, cp, dirty);
177 /* Check if value passed in data (in the form "variable=value") is
178 equal to value of variable stored in env */
180 variable_check(char *data)
182 char *cp, *cp2, *cp3, tmp[256];
186 SAFE_STRCPY(tmp, data);
187 if ((cp = index(tmp, '=')) != NULL) {
189 if (*cp == '"') { /* smash quotes if present */
191 if ((cp3 = index(cp, '"')) != NULL)
194 else if ((cp3 = index(cp, ',')) != NULL)
196 cp2 = variable_get(tmp);
201 return !strcmp(cp, cp2);
207 return variable_get(tmp) ? TRUE : FALSE;
211 dump_variables(dialogMenuItem *unused)
217 msgDebug("Writing sysinstall variables to file..");
219 fp = fopen("/etc/sysinstall.vars", "w");
221 msgConfirm("Unable to write to /etc/sysinstall.vars: %s",
223 return DITEM_FAILURE;
226 for (vp = VarHead; vp; vp = vp->next)
227 fprintf(fp, "%s=\"%s\" (%d)\n", vp->name, vp->value, vp->dirty);
231 return DITEM_SUCCESS;
234 /* Free all of the variables, useful to really start over as when the
235 user selects "restart" from the interrupt menu. */
241 /* Free the variables from our list, if we have one.. */
244 else if (!VarHead->next) {
245 unsetenv(VarHead->name);
246 safe_free(VarHead->name);
247 safe_free(VarHead->value);
252 for (vp = VarHead; vp; ) {
256 safe_free(vp->value);
265 * Persistent variables. The variables modified by these functions
266 * are not cleared between invocations of sysinstall. This is useful
267 * to allow the user to completely restart sysinstall, without having
268 * it load all of the modules again from the installation media which
269 * are still in memory.
273 pvariable_set(char *var)
278 msgFatal("NULL variable name & value passed.");
280 msgDebug("Warning: Zero length name & value passed to variable_set()\n");
281 /* Add a trivial namespace to whatever name the caller chooses. */
282 SAFE_STRCPY(tmp, "SYSINSTALL_PVAR");
283 if (index(var, '=') == NULL)
284 msgFatal("Invalid variable format: %s", var);
285 strlcat(tmp, var, 1024);
290 pvariable_get(char *var)
294 SAFE_STRCPY(tmp, "SYSINSTALL_PVAR");
295 strlcat(tmp, var, 1024);