File src/engine/variables.c changed (mode: 100644) (index 3ef3255aa..faad52afb) |
... |
... |
static void complete_envvars(const char var[], const char **start); |
106 |
106 |
static void complete_builtinvars(const char var[], const char **start); |
static void complete_builtinvars(const char var[], const char **start); |
107 |
107 |
|
|
108 |
108 |
static int initialized; |
static int initialized; |
109 |
|
static envvar_t *vars; |
|
110 |
|
static size_t nvars; |
|
|
109 |
|
static envvar_t *env_vars; |
|
110 |
|
static size_t env_var_count; |
111 |
111 |
/* List of builtin variables. */ |
/* List of builtin variables. */ |
112 |
112 |
static builtinvar_t *builtin_vars; |
static builtinvar_t *builtin_vars; |
113 |
113 |
/* Number of builtin variables. */ |
/* Number of builtin variables. */ |
|
... |
... |
init_variables(void) |
119 |
119 |
int env_count; |
int env_count; |
120 |
120 |
char **env_lst; |
char **env_lst; |
121 |
121 |
|
|
122 |
|
if(nvars > 0) |
|
|
122 |
|
if(env_var_count > 0) |
123 |
123 |
{ |
{ |
124 |
124 |
clear_envvars(); |
clear_envvars(); |
125 |
125 |
} |
} |
|
... |
... |
init_variables(void) |
130 |
130 |
int i; |
int i; |
131 |
131 |
|
|
132 |
132 |
/* Allocate memory for environment variables. */ |
/* Allocate memory for environment variables. */ |
133 |
|
vars = reallocarray(NULL, env_count, sizeof(*vars)); |
|
134 |
|
assert(vars != NULL && "Failed to allocate memory for env vars."); |
|
|
133 |
|
env_vars = reallocarray(NULL, env_count, sizeof(*env_vars)); |
|
134 |
|
assert(env_vars != NULL && "Failed to allocate memory for env vars."); |
135 |
135 |
|
|
136 |
136 |
/* Initialize variable list. */ |
/* Initialize variable list. */ |
137 |
137 |
for(i = 0; i < env_count; ++i) |
for(i = 0; i < env_count; ++i) |
|
... |
... |
clear_envvars(void) |
212 |
212 |
assert(initialized); |
assert(initialized); |
213 |
213 |
|
|
214 |
214 |
/* Free memory. */ |
/* Free memory. */ |
215 |
|
for(i = 0U; i < nvars; ++i) |
|
|
215 |
|
for(i = 0U; i < env_var_count; ++i) |
216 |
216 |
{ |
{ |
217 |
|
if(vars[i].name == NULL) |
|
|
217 |
|
if(env_vars[i].name == NULL) |
218 |
218 |
continue; |
continue; |
219 |
219 |
|
|
220 |
|
if(vars[i].from_parent) |
|
|
220 |
|
if(env_vars[i].from_parent) |
221 |
221 |
{ |
{ |
222 |
|
set_envvar(vars[i].name, vars[i].initial); |
|
|
222 |
|
set_envvar(env_vars[i].name, env_vars[i].initial); |
223 |
223 |
} |
} |
224 |
224 |
else |
else |
225 |
225 |
{ |
{ |
226 |
|
env_remove(vars[i].name); |
|
|
226 |
|
env_remove(env_vars[i].name); |
227 |
227 |
} |
} |
228 |
|
free_record(&vars[i]); |
|
|
228 |
|
free_record(&env_vars[i]); |
229 |
229 |
} |
} |
230 |
230 |
|
|
231 |
|
nvars = 0; |
|
232 |
|
free(vars); |
|
233 |
|
vars = NULL; |
|
|
231 |
|
env_var_count = 0; |
|
232 |
|
free(env_vars); |
|
233 |
|
env_vars = NULL; |
234 |
234 |
} |
} |
235 |
235 |
|
|
236 |
236 |
int |
int |
|
... |
... |
get_record(const char *name) |
583 |
583 |
size_t i; |
size_t i; |
584 |
584 |
|
|
585 |
585 |
/* Search for existing variable. */ |
/* Search for existing variable. */ |
586 |
|
for(i = 0U; i < nvars; ++i) |
|
|
586 |
|
for(i = 0U; i < env_var_count; ++i) |
587 |
587 |
{ |
{ |
588 |
|
if(vars[i].name == NULL) |
|
589 |
|
p = &vars[i]; |
|
590 |
|
else if(strcmp(vars[i].name, name) == 0) |
|
591 |
|
return &vars[i]; |
|
|
588 |
|
if(env_vars[i].name == NULL) |
|
589 |
|
p = &env_vars[i]; |
|
590 |
|
else if(strcmp(env_vars[i].name, name) == 0) |
|
591 |
|
return &env_vars[i]; |
592 |
592 |
} |
} |
593 |
593 |
|
|
594 |
594 |
if(p == NULL) |
if(p == NULL) |
595 |
595 |
{ |
{ |
596 |
596 |
/* Try to reallocate list of variables. */ |
/* Try to reallocate list of variables. */ |
597 |
|
p = realloc(vars, sizeof(*vars)*(nvars + 1U)); |
|
|
597 |
|
p = realloc(env_vars, sizeof(*env_vars)*(env_var_count + 1U)); |
598 |
598 |
if(p == NULL) |
if(p == NULL) |
599 |
599 |
return NULL; |
return NULL; |
600 |
|
vars = p; |
|
601 |
|
p = &vars[nvars]; |
|
602 |
|
++nvars; |
|
|
600 |
|
env_vars = p; |
|
601 |
|
p = &env_vars[env_var_count]; |
|
602 |
|
++env_var_count; |
603 |
603 |
} |
} |
604 |
604 |
|
|
605 |
605 |
/* Initialize new record. */ |
/* Initialize new record. */ |
|
... |
... |
static envvar_t * |
708 |
708 |
find_record(const char *name) |
find_record(const char *name) |
709 |
709 |
{ |
{ |
710 |
710 |
size_t i; |
size_t i; |
711 |
|
for(i = 0U; i < nvars; ++i) |
|
|
711 |
|
for(i = 0U; i < env_var_count; ++i) |
712 |
712 |
{ |
{ |
713 |
|
if(vars[i].name != NULL && stroscmp(vars[i].name, name) == 0) |
|
714 |
|
return &vars[i]; |
|
|
713 |
|
if(env_vars[i].name != NULL && stroscmp(env_vars[i].name, name) == 0) |
|
714 |
|
return &env_vars[i]; |
715 |
715 |
} |
} |
716 |
716 |
return NULL; |
return NULL; |
717 |
717 |
} |
} |
|
... |
... |
complete_envvars(const char var[], const char **start) |
767 |
767 |
|
|
768 |
768 |
/* Add all variables that start with given beginning. */ |
/* Add all variables that start with given beginning. */ |
769 |
769 |
len = strlen(var); |
len = strlen(var); |
770 |
|
for(i = 0U; i < nvars; ++i) |
|
|
770 |
|
for(i = 0U; i < env_var_count; ++i) |
771 |
771 |
{ |
{ |
772 |
|
if(vars[i].name == NULL) |
|
|
772 |
|
if(env_vars[i].name == NULL) |
773 |
773 |
continue; |
continue; |
774 |
|
if(vars[i].removed) |
|
|
774 |
|
if(env_vars[i].removed) |
775 |
775 |
continue; |
continue; |
776 |
|
if(strnoscmp(vars[i].name, var, len) == 0) |
|
777 |
|
vle_compl_add_match(vars[i].name, vars[i].val); |
|
|
776 |
|
if(strnoscmp(env_vars[i].name, var, len) == 0) |
|
777 |
|
vle_compl_add_match(env_vars[i].name, env_vars[i].val); |
778 |
778 |
} |
} |
779 |
779 |
vle_compl_finish_group(); |
vle_compl_finish_group(); |
780 |
780 |
vle_compl_add_last_match(var); |
vle_compl_add_last_match(var); |