File src/engine/variables.c changed (mode: 100644) (index faad52afb..4d0da68bc) |
... |
... |
typedef enum |
59 |
59 |
} |
} |
60 |
60 |
VariableOperation; |
VariableOperation; |
61 |
61 |
|
|
62 |
|
/* Description of a builtin variable. */ |
|
|
62 |
|
/* Description of a variable. */ |
63 |
63 |
typedef struct |
typedef struct |
64 |
64 |
{ |
{ |
65 |
65 |
char *name; /* Name of the variable (including "v:" prefix). */ |
char *name; /* Name of the variable (including "v:" prefix). */ |
66 |
66 |
var_t val; /* Current value of the variable. */ |
var_t val; /* Current value of the variable. */ |
67 |
67 |
} |
} |
68 |
|
builtinvar_t; |
|
|
68 |
|
var_info_t; |
69 |
69 |
|
|
70 |
70 |
typedef struct |
typedef struct |
71 |
71 |
{ |
{ |
|
... |
... |
const char ENV_VAR_NAME_CHARS[] = "abcdefghijklmnopqrstuvwxyz" |
83 |
83 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"; |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"; |
84 |
84 |
|
|
85 |
85 |
static void init_var(const char name[], const char value[]); |
static void init_var(const char name[], const char value[]); |
86 |
|
static void clear_builtinvars(void); |
|
|
86 |
|
static void clear_internal_vars(void); |
87 |
87 |
static int extract_name(const char **in, VariableType *type, size_t buf_len, |
static int extract_name(const char **in, VariableType *type, size_t buf_len, |
88 |
88 |
char buf[]); |
char buf[]); |
89 |
89 |
static int extract_op(const char **in, VariableOperation *vo); |
static int extract_op(const char **in, VariableOperation *vo); |
|
... |
... |
static envvar_t * find_record(const char *name); |
103 |
103 |
static void free_record(envvar_t *record); |
static void free_record(envvar_t *record); |
104 |
104 |
static void clear_record(envvar_t *record); |
static void clear_record(envvar_t *record); |
105 |
105 |
static void complete_envvars(const char var[], const char **start); |
static void complete_envvars(const char var[], const char **start); |
106 |
|
static void complete_builtinvars(const char var[], const char **start); |
|
|
106 |
|
static void complete_internal_vars(const char var[], const char **start); |
107 |
107 |
|
|
108 |
108 |
static int initialized; |
static int initialized; |
109 |
109 |
static envvar_t *env_vars; |
static envvar_t *env_vars; |
110 |
110 |
static size_t env_var_count; |
static size_t env_var_count; |
111 |
|
/* List of builtin variables. */ |
|
112 |
|
static builtinvar_t *builtin_vars; |
|
113 |
|
/* Number of builtin variables. */ |
|
114 |
|
static size_t nbuiltins; |
|
|
111 |
|
/* List of internal variables. */ |
|
112 |
|
static var_info_t *internal_vars; |
|
113 |
|
/* Number of internal variables. */ |
|
114 |
|
static size_t internal_var_count; |
115 |
115 |
|
|
116 |
116 |
void |
void |
117 |
117 |
init_variables(void) |
init_variables(void) |
|
... |
... |
clear_variables(void) |
186 |
186 |
{ |
{ |
187 |
187 |
assert(initialized); |
assert(initialized); |
188 |
188 |
|
|
189 |
|
clear_builtinvars(); |
|
|
189 |
|
clear_internal_vars(); |
190 |
190 |
clear_envvars(); |
clear_envvars(); |
191 |
191 |
} |
} |
192 |
192 |
|
|
193 |
|
/* Clears the list of builtin variables. */ |
|
|
193 |
|
/* Clears the list of internal variables. */ |
194 |
194 |
static void |
static void |
195 |
|
clear_builtinvars(void) |
|
|
195 |
|
clear_internal_vars(void) |
196 |
196 |
{ |
{ |
197 |
197 |
size_t i; |
size_t i; |
198 |
|
for(i = 0U; i < nbuiltins; ++i) |
|
|
198 |
|
for(i = 0U; i < internal_var_count; ++i) |
199 |
199 |
{ |
{ |
200 |
|
free(builtin_vars[i].name); |
|
201 |
|
var_free(builtin_vars[i].val); |
|
|
200 |
|
free(internal_vars[i].name); |
|
201 |
|
var_free(internal_vars[i].val); |
202 |
202 |
} |
} |
203 |
|
nbuiltins = 0U; |
|
204 |
|
free(builtin_vars); |
|
205 |
|
builtin_vars = NULL; |
|
|
203 |
|
internal_var_count = 0U; |
|
204 |
|
free(internal_vars); |
|
205 |
|
internal_vars = NULL; |
206 |
206 |
} |
} |
207 |
207 |
|
|
208 |
208 |
void |
void |
|
... |
... |
complete_variables(const char var[], const char **start) |
745 |
745 |
} |
} |
746 |
746 |
else if(var[0] == 'v' && var[1] == ':') |
else if(var[0] == 'v' && var[1] == ':') |
747 |
747 |
{ |
{ |
748 |
|
complete_builtinvars(var, start); |
|
|
748 |
|
complete_internal_vars(var, start); |
749 |
749 |
} |
} |
750 |
750 |
else |
else |
751 |
751 |
{ |
{ |
|
... |
... |
complete_envvars(const char var[], const char **start) |
780 |
780 |
vle_compl_add_last_match(var); |
vle_compl_add_last_match(var); |
781 |
781 |
} |
} |
782 |
782 |
|
|
783 |
|
/* Completes builtin variable name. var should point to "v:...". *start is |
|
784 |
|
* set to completion insertion position in var. */ |
|
|
783 |
|
/* Completes a variable name. var should point to "v:...". *start is set to |
|
784 |
|
* completion insertion position in var. */ |
785 |
785 |
static void |
static void |
786 |
|
complete_builtinvars(const char var[], const char **start) |
|
|
786 |
|
complete_internal_vars(const char var[], const char **start) |
787 |
787 |
{ |
{ |
788 |
788 |
size_t i; |
size_t i; |
789 |
789 |
size_t len; |
size_t len; |
|
... |
... |
complete_builtinvars(const char var[], const char **start) |
792 |
792 |
|
|
793 |
793 |
/* Add all variables that start with given beginning. */ |
/* Add all variables that start with given beginning. */ |
794 |
794 |
len = strlen(var); |
len = strlen(var); |
795 |
|
for(i = 0U; i < nbuiltins; ++i) |
|
|
795 |
|
for(i = 0U; i < internal_var_count; ++i) |
796 |
796 |
{ |
{ |
797 |
|
if(strncmp(builtin_vars[i].name, var, len) == 0) |
|
|
797 |
|
if(strncmp(internal_vars[i].name, var, len) == 0) |
798 |
798 |
{ |
{ |
799 |
|
char *const str_val = var_to_str(builtin_vars[i].val); |
|
800 |
|
vle_compl_add_match(builtin_vars[i].name, str_val); |
|
|
799 |
|
char *const str_val = var_to_str(internal_vars[i].val); |
|
800 |
|
vle_compl_add_match(internal_vars[i].name, str_val); |
801 |
801 |
free(str_val); |
free(str_val); |
802 |
802 |
} |
} |
803 |
803 |
} |
} |
|
... |
... |
var_t |
809 |
809 |
getvar(const char varname[]) |
getvar(const char varname[]) |
810 |
810 |
{ |
{ |
811 |
811 |
size_t i; |
size_t i; |
812 |
|
for(i = 0U; i < nbuiltins; ++i) |
|
|
812 |
|
for(i = 0U; i < internal_var_count; ++i) |
813 |
813 |
{ |
{ |
814 |
|
if(strcmp(builtin_vars[i].name, varname) == 0) |
|
|
814 |
|
if(strcmp(internal_vars[i].name, varname) == 0) |
815 |
815 |
{ |
{ |
816 |
|
return builtin_vars[i].val; |
|
|
816 |
|
return internal_vars[i].val; |
817 |
817 |
} |
} |
818 |
818 |
} |
} |
819 |
819 |
|
|
|
... |
... |
getvar(const char varname[]) |
823 |
823 |
int |
int |
824 |
824 |
setvar(const char varname[], var_t val) |
setvar(const char varname[], var_t val) |
825 |
825 |
{ |
{ |
826 |
|
builtinvar_t new_var; |
|
|
826 |
|
var_info_t new_var; |
827 |
827 |
size_t i; |
size_t i; |
828 |
828 |
void *p; |
void *p; |
829 |
829 |
|
|
|
... |
... |
setvar(const char varname[], var_t val) |
843 |
843 |
} |
} |
844 |
844 |
|
|
845 |
845 |
/* Search for existing variable. */ |
/* Search for existing variable. */ |
846 |
|
for(i = 0U; i < nbuiltins; ++i) |
|
|
846 |
|
for(i = 0U; i < internal_var_count; ++i) |
847 |
847 |
{ |
{ |
848 |
|
if(strcmp(builtin_vars[i].name, varname) == 0) |
|
|
848 |
|
if(strcmp(internal_vars[i].name, varname) == 0) |
849 |
849 |
{ |
{ |
850 |
|
free(builtin_vars[i].name); |
|
851 |
|
var_free(builtin_vars[i].val); |
|
852 |
|
builtin_vars[i] = new_var; |
|
|
850 |
|
free(internal_vars[i].name); |
|
851 |
|
var_free(internal_vars[i].val); |
|
852 |
|
internal_vars[i] = new_var; |
853 |
853 |
return 0; |
return 0; |
854 |
854 |
} |
} |
855 |
855 |
} |
} |
856 |
856 |
|
|
857 |
857 |
/* Try to reallocate list of variables. */ |
/* Try to reallocate list of variables. */ |
858 |
|
p = realloc(builtin_vars, sizeof(*builtin_vars)*(nbuiltins + 1U)); |
|
|
858 |
|
p = realloc(internal_vars, sizeof(*internal_vars)*(internal_var_count + 1U)); |
859 |
859 |
if(p == NULL) |
if(p == NULL) |
860 |
860 |
{ |
{ |
861 |
861 |
free(new_var.name); |
free(new_var.name); |
862 |
862 |
var_free(new_var.val); |
var_free(new_var.val); |
863 |
863 |
return 1; |
return 1; |
864 |
864 |
} |
} |
865 |
|
builtin_vars = p; |
|
866 |
|
builtin_vars[nbuiltins] = new_var; |
|
867 |
|
++nbuiltins; |
|
|
865 |
|
internal_vars = p; |
|
866 |
|
internal_vars[internal_var_count] = new_var; |
|
867 |
|
++internal_var_count; |
868 |
868 |
|
|
869 |
869 |
return 0; |
return 0; |
870 |
870 |
} |
} |