]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - release/sysinstall/variable.c
Oh bollocks, I really screwed up the "auto" check here. Time
[FreeBSD/FreeBSD.git] / release / sysinstall / variable.c
1 /*
2  * The new sysinstall program.
3  *
4  * This is probably the last program in the `sysinstall' line - the next
5  * generation being essentially a complete rewrite.
6  *
7  * $FreeBSD$
8  *
9  * Copyright (c) 1995
10  *      Jordan Hubbard.  All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
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
18  *    point in the file.
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.
22  *
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
33  * SUCH DAMAGE.
34  *
35  */
36
37 #include "sysinstall.h"
38
39 /* Routines for dealing with variable lists */
40
41 static void
42 make_variable(char *var, char *value, int dirty)
43 {
44     Variable *vp;
45
46     /* Trim leading and trailing whitespace */
47     var = string_skipwhite(string_prune(var));
48
49     if (!var || !*var)
50         return;
51
52
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)
57                 return;
58             setenv(var, value, 1);
59             free(vp->value);
60             vp->value = strdup(value);
61             if (dirty != -1)
62                 vp->dirty = dirty;
63             return;
64         }
65     }
66
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);
72     if (dirty == -1)
73         dirty = 0;
74     vp->dirty = dirty;
75     vp->next = VarHead;
76     VarHead = vp;
77 }
78
79 void
80 variable_set(char *var, int dirty)
81 {
82     char tmp[1024], *cp;
83
84     if (!var)
85         msgFatal("NULL variable name & value passed.");
86     else if (!*var)
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);
91     *(cp++) = '\0';
92     make_variable(tmp, string_skipwhite(cp), dirty);
93 }
94
95 void
96 variable_set2(char *var, char *value, int dirty)
97 {
98     if (!var || !value)
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);
103 }
104
105 char *
106 variable_get(char *var)
107 {
108     return getenv(var);
109 }
110
111 int
112 variable_cmp(char *var, char *value)
113 {
114     char *val;
115
116     if ((val = variable_get(var)))
117         return strcmp(val, value);
118     return -1;
119 }
120
121 void
122 variable_unset(char *var)
123 {
124     Variable *vp;
125     char name[512], *cp;
126
127     if ((cp = index(var, '=')) != NULL)
128         sstrncpy(name, var, cp - var);
129     else
130         SAFE_STRCPY(name, var);
131     unsetenv(name);
132     /* Now search to see if it's in our list, if we have one.. */
133     if (!VarHead)
134         return;
135     else if (!VarHead->next && !strcmp(VarHead->name, name)) {
136         safe_free(VarHead->name);
137         safe_free(VarHead->value);
138         free(VarHead);
139         VarHead = NULL;
140     }
141     else {
142         for (vp = VarHead; vp; vp = vp->next) {
143             if (!strcmp(vp->name, name)) {
144                 Variable *save = vp->next;
145
146                 safe_free(vp->name);
147                 safe_free(vp->value);
148                 *vp = *save;
149                 safe_free(save);
150                 break;
151             }
152         }
153     }
154 }
155
156 /* Prompt user for the name of a variable */
157 char *
158 variable_get_value(char *var, char *prompt, int dirty)
159 {
160     char *cp;
161
162     cp = variable_get(var);
163     if (cp && variable_get(VAR_NONINTERACTIVE))
164         return cp;
165     else if ((cp = msgGetInput(cp, prompt)) != NULL)
166         variable_set2(var, cp, dirty);
167     else
168         cp = NULL;
169     return cp;
170 }
171
172 /* Check if value passed in data (in the form "variable=value") is equal to value of 
173    variable stored in env */
174 int
175 variable_check(char *data)
176 {
177     char *cp, *cp2, *cp3, tmp[256];
178
179     if (!data)
180         return FALSE;
181     SAFE_STRCPY(tmp, data);
182     if ((cp = index(tmp, '=')) != NULL) {
183         *(cp++) = '\0';
184         if (*cp == '"') {       /* smash quotes if present */
185             ++cp;
186             if ((cp3 = index(cp, '"')) != NULL)
187                 *cp3 = '\0';
188         }
189         else if ((cp3 = index(cp, ',')) != NULL)
190             *cp3 = '\0';
191         cp2 = getenv(tmp);
192         if (cp2) {
193             if (!*cp)
194                 return TRUE;
195             else
196                 return !strcmp(cp, cp2);
197         }
198         else
199             return FALSE;
200     }
201     else
202         return getenv(tmp) ? TRUE : FALSE;
203
204
205 int
206 dump_variables(dialogMenuItem *unused)
207 {
208     FILE *fp;
209     Variable *vp;
210
211     if (isDebug())
212         msgDebug("Writing sysinstall variables to file..");
213
214     fp = fopen("/etc/sysinstall.vars", "w");
215     if (!fp) {
216         msgConfirm("Unable to write to /etc/sysinstall.vars: %s",
217                    strerror(errno));
218         return DITEM_FAILURE;
219     }
220
221     for (vp = VarHead; vp; vp = vp->next)
222         fprintf(fp, "%s=\"%s\" (%d)\n", vp->name, vp->value, vp->dirty);
223
224     fclose(fp);
225
226     return DITEM_SUCCESS;
227 }