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 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer,
17 * verbatim and that no modifications are made prior to this
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include "sysinstall.h"
39 /* Routines for dealing with variable lists */
42 make_variable(char *var, char *value, int dirty)
46 /* Trim leading and trailing whitespace */
47 var = string_skipwhite(string_prune(var));
53 /* Now search to see if it's already in the list */
54 for (vp = VarHead; vp; vp = vp->next) {
55 if (!strcmp(vp->name, var)) {
56 if (vp->dirty && !dirty)
58 setenv(var, value, 1);
60 vp->value = strdup(value);
67 setenv(var, value, 1);
68 /* No? Create a new one */
69 vp = (Variable *)safe_malloc(sizeof(Variable));
70 vp->name = strdup(var);
71 vp->value = strdup(value);
80 variable_set(char *var, int dirty)
85 msgFatal("NULL variable name & value passed.");
87 msgDebug("Warning: Zero length name & value passed to variable_set()\n");
88 SAFE_STRCPY(tmp, var);
89 if ((cp = index(tmp, '=')) == NULL)
90 msgFatal("Invalid variable format: %s", var);
92 make_variable(tmp, string_skipwhite(cp), dirty);
96 variable_set2(char *var, char *value, int dirty)
99 msgFatal("Null name or value passed to set_variable2!");
100 else if (!*var || !*value)
101 msgDebug("Warning: Zero length name or value passed to variable_set2()\n");
102 make_variable(var, value, dirty);
106 variable_get(char *var)
112 variable_cmp(char *var, char *value)
116 if ((val = variable_get(var)))
117 return strcmp(val, value);
122 variable_unset(char *var)
127 if ((cp = index(var, '=')) != NULL)
128 sstrncpy(name, var, cp - var);
130 SAFE_STRCPY(name, var);
132 /* Now search to see if it's in our list, if we have one.. */
135 else if (!VarHead->next && !strcmp(VarHead->name, name)) {
136 safe_free(VarHead->name);
137 safe_free(VarHead->value);
142 for (vp = VarHead; vp; vp = vp->next) {
143 if (!strcmp(vp->name, name)) {
144 Variable *save = vp->next;
147 safe_free(vp->value);
156 /* Prompt user for the name of a variable */
158 variable_get_value(char *var, char *prompt, int dirty)
162 cp = variable_get(var);
163 if (cp && variable_get(VAR_NONINTERACTIVE))
165 else if ((cp = msgGetInput(cp, prompt)) != NULL)
166 variable_set2(var, cp, dirty);
172 /* Check if value passed in data (in the form "variable=value") is equal to value of
173 variable stored in env */
175 variable_check(char *data)
177 char *cp, *cp2, *cp3, tmp[256];
181 SAFE_STRCPY(tmp, data);
182 if ((cp = index(tmp, '=')) != NULL) {
184 if (*cp == '"') { /* smash quotes if present */
186 if ((cp3 = index(cp, '"')) != NULL)
189 else if ((cp3 = index(cp, ',')) != NULL)
196 return !strcmp(cp, cp2);
202 return getenv(tmp) ? TRUE : FALSE;
206 dump_variables(dialogMenuItem *unused)
212 msgDebug("Writing sysinstall variables to file..");
214 fp = fopen("/etc/sysinstall.vars", "w");
216 msgConfirm("Unable to write to /etc/sysinstall.vars: %s",
218 return DITEM_FAILURE;
221 for (vp = VarHead; vp; vp = vp->next)
222 fprintf(fp, "%s=\"%s\" (%d)\n", vp->name, vp->value, vp->dirty);
226 return DITEM_SUCCESS;