-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
bools.c
373 lines (333 loc) · 7.89 KB
/
bools.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/* $Id: bools.c,v 1.4 2008-02-28 03:05:43 icculus Exp $ */
/*
Manage global installer booleans.
Author: Stephane Peter
*/
#include "config.h"
#include "install.h"
#include "bools.h"
#include "install_log.h"
#include "detect.h"
#include "arch.h"
#include <sys/types.h>
#include <ctype.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
/* The maximum length of a boolean variable name */
#define MAX_VARNAME 30
/* Use a N-tree to represent the expressions */
struct _setup_expression
{
enum { OP_VARIABLE = 1, OP_OR, OP_AND, OP_XOR } type;
int negate;
setup_bool *var;
struct _setup_expression *list, *next;
};
setup_bool *setup_booleans = NULL;
static install_info *cur_info = NULL;
static setup_bool *setup_false = NULL, *setup_true = NULL;
/* Fill up with standard booleans */
void setup_init_bools(install_info *info)
{
char buf[80];
cur_info = info;
setup_booleans = NULL;
/* Basic bools */
setup_true = setup_add_bool("true", 1);
setup_false = setup_add_bool("false", 0);
setup_add_bool("is-root", geteuid()==0);
setup_add_bool("reinstalling", info->options.reinstalling);
#ifdef __linux
setup_add_bool("selinux", have_selinux);
#endif
#ifdef HAVE_BZIP2_SUPPORT
setup_add_bool("bzip2", 1);
#endif
#ifdef RPM_SUPPORT
setup_add_bool("rpm-support", 1);
# if RPM_SUPPORT == 3
setup_add_bool("rpm3-support", 1);
# endif
#endif
/* We must define this here instead of in the backend in case we use a non-GTK installer for
a GTK app that uses these flags */
#ifdef ENABLE_GTK2
setup_add_bool("gtk2", 1);
#else
setup_add_bool("gtk1", 1);
#endif
/* Add arch, glibc, and os bools */
setup_add_bool(detect_os(), 1);
setup_add_bool(info->arch, 1);
setup_add_bool(info->libc, 1);
setup_add_bool(distribution_symbol[info->distro], 1);
snprintf(buf, sizeof(buf), "distro-major-%d", info->distro_maj);
setup_add_bool(buf, 1);
snprintf(buf, sizeof(buf), "distro-minor-%d", info->distro_min);
setup_add_bool(buf, 1);
SetLocaleBools();
}
/* Create and free booleans */
setup_bool *setup_new_bool(const char *name)
{
setup_bool *ret = malloc(sizeof(setup_bool));
if ( ret ) {
memset(ret, 0, sizeof(*ret));
ret->name = strdup(name);
ret->next = setup_booleans;
setup_booleans = ret;
}
return ret;
}
void setup_free_bool(setup_bool *b)
{
if (b) {
free(b->name);
free(b->script);
free(b->envvar);
free(b);
}
}
/* Easy add of simple new bools */
setup_bool *setup_add_bool(const char *name, unsigned value)
{
setup_bool *ret = setup_new_bool(name);
setup_set_bool(ret, value);
ret->once = 1;
log_debug("New bool: %s = %s", name, value ? "TRUE" : "FALSE");
return ret;
}
setup_bool *setup_find_bool(const char *name)
{
setup_bool *ret;
for ( ret = setup_booleans; ret; ret = ret->next ) {
if ( !strcmp(ret->name, name) ) {
return ret;
}
}
return NULL;
}
int setup_get_bool(setup_bool *b)
{
if ( b ) {
if ( b->once ) {
return b->inited ? b->value : 0;
} else if (b->script) { /* Run the script to determine the value */
b->value = (run_script(cur_info, b->script, 0, 0) == 0);
b->inited = 1; /* Keep track of the last value */
return b->value;
}
}
return 0;
}
void setup_set_bool(setup_bool *b, unsigned value)
{
if (b) {
b->value = value;
b->inited = 1;
}
}
/* Return the number of characters parsed */
static setup_expression *parse_token(const char *str, int *len)
{
if ( *str ) {
setup_expression *exp = malloc(sizeof(setup_expression));
if ( exp ) {
int count = 0, cv;
char var[MAX_VARNAME+1], *v;
memset(exp, 0, sizeof(setup_expression));
if ( *str == '!' ) {
exp->negate = 1;
str ++;
count ++;
}
switch (*str) {
case '+': /* & creates problems with XML parsers */
log_debug("Parsing AND: %s", str);
exp->type = OP_AND;
str ++; count ++;
break;
case '|':
log_debug("Parsing OR: %s", str);
exp->type = OP_OR;
str ++; count ++;
break;
case '^':
log_debug("Parsing XOR: %s", str);
exp->type = OP_XOR;
str ++; count ++;
break;
default: /* Variable ? */
log_debug("Parsing VARIABLE: %s", str);
if ( isalnum(*str) ) {
exp->type = OP_VARIABLE;
v = var;
cv = 0;
for (;;) {
if (cv == MAX_VARNAME) {
*v = '\0';
log_warning("Variable '%s' name too long.", var);
break;
}
if ( *str == ')' || *str=='\0' ) /* Let the parent handle the closing parenthesis */
break;
cv ++;
if ( *str==' ' || *str=='\t' || *str==',' ) {
break;
}
*v ++ = *str ++;
}
count += cv;
*v ++ = '\0';
exp->var = setup_find_bool(var);
if ( !exp->var ) {
log_debug("Boolean '%s' is undefined - assuming false.", var);
exp->var = setup_false;
}
} else {
log_warning("Syntax error in expression: '%c' is not alnum. (%s)", *str, str);
free(exp);
exp = NULL;
}
}
if ( exp && exp->type!=OP_VARIABLE ) { /* Look for () */
if ( *str == '(' ) {
setup_expression *sub;
int sublen = 0;
str ++; count ++;
/* Recurse for sub-expressions */
for (;;) {
sub = parse_token(str, &sublen);
if ( sub ) {
sub->next = exp->list; /* This keeps items in the reverse order */
exp->list = sub;
count += sublen;
str += sublen;
} else
break;
if ( ! *str ) {
log_warning("Syntax error in expression: missing ')' before end of string");
}
if (*str == ')') {
str ++; count ++;
break;
}
}
/* Skip any following extraneous character */
if ( *str==' ' || *str=='\t' || *str==',' ) {
str ++; count ++;
}
if ( !exp->list ) {
log_warning("Operator didn't refer to any operands.");
free(exp);
exp = NULL;
}
} else {
log_warning("Syntax error in expression: '%c' instead of '('", *str);
free(exp);
exp = NULL;
}
}
if ( len )
*len = count;
return exp;
} else {
log_fatal("Failed to allocate expression token");
return NULL;
}
} else { /* Empty string */
*len = 0;
return NULL;
}
}
/* Handle expressions */
setup_expression *setup_parse_expression(const char *expr)
{
/* Break down the string into a binary tree */
return parse_token(expr, NULL);
}
void setup_free_expression(setup_expression *expr)
{
if (expr) {
setup_expression *next;
/* Start by freeing the children */
while ( expr->list ) {
next = expr->list->next;
setup_free_expression(expr->list);
expr->list = next;
}
free(expr);
}
}
/* Evaluate the expression - returns TRUE or FALSE */
int setup_evaluate(setup_expression *expr)
{
int ret = 0;
if ( expr ) {
setup_expression *sub = expr->list;
switch (expr->type) {
case OP_VARIABLE:
ret = setup_get_bool(expr->var);
break;
case OP_AND:
ret = 1;
while (sub) {
ret = ret && setup_evaluate(sub);
if ( !ret )
break; /* Exit early */
sub = sub->next;
}
break;
case OP_OR:
while (sub) {
ret = ret || setup_evaluate(sub);
if ( ret )
break; /* Exit early */
sub = sub->next;
}
break;
case OP_XOR: /* Only one of the operands must be true */
ret = 0;
while (sub) {
ret += setup_evaluate(sub);
sub = sub->next;
}
if ( ret > 1 )
ret = 0;
break;
}
if ( expr->negate )
ret = !ret;
}
return ret;
}
/* Easy shortcut to evaluate an expression string */
int match_condition(const char *expr)
{
int ret = 1; /* Default to TRUE so that empty strings still match */
if ( expr ) {
setup_expression *xp = setup_parse_expression(expr);
if ( xp ) {
ret = setup_evaluate(xp);
setup_free_expression(xp);
} else {
ret = 0;
}
log_debug("Expression '%s' evaluated to %s", expr, ret ? "TRUE" : "FALSE");
}
return ret;
}
/* Free up all memory */
void setup_exit_bools(void)
{
setup_bool *b;
while ( setup_booleans ) {
b = setup_booleans->next;
setup_free_bool(setup_booleans);
setup_booleans = b;
}
cur_info = NULL;
setup_booleans = NULL;
}