-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwb_argeater.c
196 lines (177 loc) · 4.37 KB
/
pwb_argeater.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
#include "pwb_handle.h"
#include "pwb_argeater.h"
#include "pwb_errors.h"
#include <argeater.h>
#include <stdio.h>
#include <stdlib.h> // for strtol()
/**
* @brief Reimplement int_setter to allow using our error reporting
*/
bool pwb_argeater_int_setter(const char **target, const char *value)
{
if (*value)
{
char *end;
long val = strtol(value, &end, 10);
if (*end == '\0')
{
*(int*)target = (int)val;
return true;
}
else
(*error_sink)("Value '%s' does not convert to an integer.", value);
}
else
(*error_sink)("Integer argument was empty.");
return false;
}
/**
* @brief Reimplement int_setter to allow using our error reporting
*/
bool pwb_argeater_unsigned_int_setter(const char **target, const char *value)
{
if (*value)
{
char *end;
unsigned long val = strtoul(value, &end, 10);
if (*end == '\0')
{
*(unsigned int*)target = (unsigned int)val;
return true;
}
else
(*error_sink)("Value '%s' does not convert to an unsigned integer.", value);
}
else
(*error_sink)("Unsigned integer argument was empty.");
return false;
}
bool argeater_pwbh_setter(const char **target, const char *value)
{
SHELL_VAR *sv = find_variable(value);
if (sv)
{
if (pwbh_p(sv))
{
*(PWBH**)target = pwbh_cell(sv);
return true;
}
else
(*error_sink)("Variable '%s' is not a pwb handle.", value);
}
else
(*error_sink)("Failed to find handle variable '%s'.", value);
return false;
}
bool argeater_array_name_setter(const char **target, const char *value)
{
SHELL_VAR *sv = find_variable(value);
if (sv)
{
if (array_p(sv))
{
*target = value;
return true;
}
else
(*error_sink)("Variable '%s' is not an array.", value);
}
else
(*error_sink)("Failed to find variable '%s'.", value);
return false;
}
bool argeater_array_setter(const char **target, const char *value)
{
SHELL_VAR *sv = find_variable(value);
if (sv)
{
if (array_p(sv))
{
*(ARRAY**)target = array_cell(sv);
return true;
}
else
(*error_sink)("Variable '%s' is not a shell array.", value);
}
else
(*error_sink)("Failed to find handle variable '%s'.", value);
return false;
}
bool pwb_argeater_function_setter(const char **target, const char *value)
{
SHELL_VAR *sv = find_function(value);
if (sv)
{
if (function_p(sv))
{
*target = value;
return true;
}
else
(*error_sink)("Shell variable '%s' is not a function.\n", value);
}
else
(*error_sink)("Failed to find script function '%s'.\n", value);
return false;
}
/**
* @brief Ensures named associative array variable exists
*
* If the variable exists and is an associative array, it is
* left alone. Otherwise, The variable is created if it doesn't
* already exist. If the named variable doesn't exist, an
* associative array of the given name is created.
*/
bool pwb_argeater_assoc_ensurer(const char **target, const char *value)
{
SHELL_VAR *sv = find_variable(value);
if (sv)
{
// Clear array if proper type, otherwise dispose of it
if (assoc_p(sv))
assoc_flush(assoc_cell(sv));
else
{
dispose_variable(sv);
sv = NULL;
}
}
if (!sv)
{
if (variable_context == 0)
sv = make_new_assoc_variable((char*)value);
else
sv = make_local_assoc_variable((char*)value, variable_context);
}
if (sv)
{
*target = *(char**)&sv;
return true;
}
(*error_sink)("Failed to secure associative array %s", value);
return false;
}
bool argeater_kclass_setter(const char **target, const char *value)
{
SHELL_VAR *sv = find_variable(value);
if (sv)
{
if (specialvar_p(sv))
{
int diff = strcmp(sv->value, KEYMAP_LABEL);
++diff;
}
if (keymap_p(sv))
{
KCLASS *kclass = (KCLASS*)target;
KDATA *kdata = (KDATA*)sv->value;
PWB_RESULT result = initialize_kclass(kclass, kdata);
return result == PWB_SUCCESS;
}
else
(*error_sink)("Shell variable '%s' is not a keymap.\n", value);
}
else
(*error_sink)("Failed to find keymap variable '%s'.\n", value);
return false;
}