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.
10 * Jordan Hubbard. All rights reserved.
12 * Murray Stokely. All rights reserved.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer,
19 * verbatim and that no modifications are made prior to this
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
25 * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include "sysinstall.h"
41 /* Routines for dealing with variable lists */
44 make_variable(char *var, char *value, int dirty)
48 /* Trim leading and trailing whitespace */
49 var = string_skipwhite(string_prune(var));
55 /* Now search to see if it's already in the list */
56 for (vp = VarHead; vp; vp = vp->next) {
57 if (!strcmp(vp->name, var)) {
58 if (vp->dirty && !dirty)
60 setenv(var, value, 1);
62 vp->value = strdup(value);
69 setenv(var, value, 1);
70 /* No? Create a new one */
71 vp = (Variable *)safe_malloc(sizeof(Variable));
72 vp->name = strdup(var);
73 vp->value = strdup(value);
82 variable_set(char *var, int dirty)
87 msgFatal("NULL variable name & value passed.");
89 msgDebug("Warning: Zero length name & value passed to variable_set()\n");
90 SAFE_STRCPY(tmp, var);
91 if ((cp = index(tmp, '=')) == NULL)
92 msgFatal("Invalid variable format: %s", var);
94 make_variable(tmp, string_skipwhite(cp), dirty);
98 variable_set2(char *var, char *value, int dirty)
101 msgFatal("Null name or value passed to set_variable2(%s) = %s!",
102 var ? var : "", value ? value : "");
103 else if (!*var || !*value)
104 msgDebug("Warning: Zero length name or value passed to variable_set2(%s) = %s\n",
106 make_variable(var, value, dirty);
110 variable_get(char *var)
116 variable_cmp(char *var, char *value)
120 if ((val = variable_get(var)))
121 return strcmp(val, value);
126 variable_unset(char *var)
131 if ((cp = index(var, '=')) != NULL)
132 sstrncpy(name, var, cp - var);
134 SAFE_STRCPY(name, var);
136 /* Now search to see if it's in our list, if we have one.. */
139 else if (!VarHead->next && !strcmp(VarHead->name, name)) {
140 safe_free(VarHead->name);
141 safe_free(VarHead->value);
146 for (vp = VarHead; vp; vp = vp->next) {
147 if (!strcmp(vp->name, name)) {
148 Variable *save = vp->next;
151 safe_free(vp->value);
160 /* Prompt user for the name of a variable */
162 variable_get_value(char *var, char *prompt, int dirty)
166 cp = variable_get(var);
167 if (cp && variable_get(VAR_NONINTERACTIVE))
169 else if ((cp = msgGetInput(cp, "%s", prompt)) != NULL)
170 variable_set2(var, cp, dirty);
176 /* Check if value passed in data (in the form "variable=value") is
177 * valid, and it's status compared to the value of variable stored in
180 * Possible return values :
181 * -3: Invalid line, the data string is NOT set as an env variable
182 * -2: Invalid line, the data string is set as an env variable
184 * 0: Valid line, is NOT equal to env version
185 * 1: Valid line, is equal to env version
186 * 2: Valid line, value empty - e.g. foo=""
187 * 3: Valid line, does not exist in env
190 variable_check2(char *data)
192 char *cp, *cp2, *cp3, tmp[256];
196 SAFE_STRCPY(tmp, data);
197 if ((cp = index(tmp, '=')) != NULL) {
199 if (*cp == '"') { /* smash quotes if present */
201 if ((cp3 = index(cp, '"')) != NULL)
204 else if ((cp3 = index(cp, ',')) != NULL)
206 cp2 = variable_get(tmp);
211 return strcmp(cp, cp2) == 0 ? 1 : 0;
217 return variable_get(tmp) != NULL ? -2 : -3;
220 /* Check if the value passed in data (in the form "variable=value") is
221 equal to the value of variable stored in env */
223 variable_check(char *data)
226 ret = variable_check2(data);
240 dump_variables(dialogMenuItem *unused)
246 msgDebug("Writing %s variables to file..\n", ProgName);
248 fp = fopen("/etc/sysinstall.vars", "w");
250 msgConfirm("Unable to write to /etc/%s.vars: %s",
251 ProgName, strerror(errno));
252 return DITEM_FAILURE;
255 for (vp = VarHead; vp; vp = vp->next)
256 fprintf(fp, "%s=\"%s\" (%d)\n", vp->name, vp->value, vp->dirty);
260 return DITEM_SUCCESS;
263 /* Free all of the variables, useful to really start over as when the
264 user selects "restart" from the interrupt menu. */
270 /* Free the variables from our list, if we have one.. */
273 else if (!VarHead->next) {
274 unsetenv(VarHead->name);
275 safe_free(VarHead->name);
276 safe_free(VarHead->value);
281 for (vp = VarHead; vp; ) {
285 safe_free(vp->value);
294 * Persistent variables. The variables modified by these functions
295 * are not cleared between invocations of sysinstall. This is useful
296 * to allow the user to completely restart sysinstall, without having
297 * it load all of the modules again from the installation media which
298 * are still in memory.
302 pvariable_set(char *var)
308 msgFatal("NULL variable name & value passed.");
310 msgDebug("Warning: Zero length name & value passed to variable_set()\n");
311 /* Add a trivial namespace to whatever name the caller chooses. */
312 SAFE_STRCPY(tmp, "SYSINSTALL_PVAR");
313 if (index(var, '=') == NULL)
314 msgFatal("Invalid variable format: %s", var);
315 strlcat(tmp, var, 1024);
316 p = strchr(tmp, '=');
318 setenv(tmp, p + 1, 1);
322 pvariable_get(char *var)
326 SAFE_STRCPY(tmp, "SYSINSTALL_PVAR");
327 strlcat(tmp, var, 1024);