-
Notifications
You must be signed in to change notification settings - Fork 0
/
argparser.c
304 lines (260 loc) · 13.2 KB
/
argparser.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
#include "argparser.h"
#include "invoke_handler.h"
#include "library_path_resolver.h"
#include "main.h"
#include "return_formatter.h"
#include "types_and_utils.h"
#include "var_map.h"
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
// Function to allocate and initialize a new ArgInfo struct
// ArgInfo* create_arg_info(ArgType type, const char* value) {
// ArgInfo* arg = malloc(sizeof(ArgInfo));
// if (!arg) return NULL;
// arg->type = type;
// // Omitting detailed value assignment for brevity
// return arg;
// }
void addArgToFunctionCallInfo(ArgInfoContainer* info, ArgInfo* arg) {
if (!info || !arg) return;
if (!info->args) {
// Allocate memory for a single ArgInfo struct
info->args = malloc(sizeof(void*));
if (!info->args) return;
// Set the first element of the array to point to the new ArgInfo struct
info->args[0] = arg;
info->arg_count = 1;
} else {
// Reallocate memory to accommodate one more ArgInfo struct
info->args = realloc(info->args, (info->arg_count + 1) * sizeof(void*));;
// Copy arg to the end of the array
info->args[info->arg_count] = arg;
info->arg_count++;
}
}
void parse_arg_type_from_flag(ArgInfo* arg, const char* argStr){
int pointer_depth = 0;
int array_value_pointer_depth = 0;
ArgType explicitType = charToType(argStr[0]); // Convert flag to type
while (explicitType == TYPE_POINTER) {
pointer_depth++;
explicitType = charToType(argStr[pointer_depth]);
}
if (explicitType == TYPE_STRUCT) {
arg->struct_info = calloc(1, sizeof(StructInfo));
arg->struct_info->is_packed = false;
if (argStr[1+pointer_depth] == 'K'){ // SK: = pacKed struct
arg->struct_info->is_packed = true;
}
if (argStr[1 + pointer_depth + arg->struct_info->is_packed] != ':') {
fprintf(stderr, "Error: Struct flag must be followed by a colon and then a list of arguments, in flags %s\n", argStr);
exit_or_restart(1);
}
}
if (explicitType == TYPE_ARRAY) {
// We'll need to figure out how to move forward argv past the array values
// For now we'll just say that the array values can't have a space in them
// So the entire array will just be one argv
// In the future we may switch to using end delimitters eg a: 3 2 1 :a
while (charToType(argStr[1+pointer_depth+array_value_pointer_depth]) == TYPE_POINTER) {
array_value_pointer_depth++;
}
if (isAllDigits(&argStr[1+pointer_depth+array_value_pointer_depth]) || argStr[1+pointer_depth+array_value_pointer_depth] == 't') {
//argStr[:2+pointer_depth] is the array flag + 'i' + argStr[2+pointer_depth:]
fprintf(stderr, "Error: Array flag must be followed by a primitive type flag, and THEN it can be followed by a size or size_t argnum, so for instance ai4 or ait1 for an array of ints, whereas you have %s\n", argStr);
exit_or_restart(1);
}
explicitType = charToType(argStr[1 + pointer_depth+array_value_pointer_depth]);
if (explicitType == TYPE_STRUCT){
fprintf(stderr, "Error: Arrays of structs are not presently supported, in flags %s\n", argStr);
exit_or_restart(1);
} else if (explicitType == TYPE_UNKNOWN) {
fprintf(stderr, "Error: Unsupported argument type flag in flags %s\n", argStr);
exit_or_restart(1);
} else if (explicitType == TYPE_ARRAY) {
fprintf(stderr, "Error: Array types cannot be nested, in flags %s\n", argStr);
exit_or_restart(1);
} else if (explicitType == TYPE_POINTER) {
fprintf(stderr, "Error: Array flag in unsupported position in flags %s. Order must be -[p[p..]][a][primitive type flag]\n", argStr);
exit_or_restart(1);
}
// see if there's a size appended to the end of the array flag
if (isAllDigits(&argStr[2 + pointer_depth+array_value_pointer_depth])){
arg->is_array = ARRAY_STATIC_SIZE;
arg->static_or_implied_size = atoi(argStr + 2 + pointer_depth+array_value_pointer_depth);
} else if (argStr[2 + pointer_depth+array_value_pointer_depth] == 't') {
// t is a flag indicating that the array size is specified as a size_t in another argument
if (isAllDigits(&argStr[3 + pointer_depth+array_value_pointer_depth])){
arg->is_array = ARRAY_SIZE_AT_ARGNUM;
arg->array_sizet_arg.argnum_of_size_t_to_be_replaced = atoi(argStr + 3 + pointer_depth + array_value_pointer_depth);
} else {
//maybe in the future we'll allow just assuming its the next arg if it's not a number
fprintf(stderr, "Error: Array size flag t must be followed by a number in flags %s\n", argStr);
exit_or_restart(1);
}
} else {
arg->is_array = ARRAY_STATIC_SIZE_UNSET;
// will be set from the values given later on if it's an argument (or will fail if it's a return)
}
}
if (explicitType == TYPE_UNKNOWN) {
fprintf(stderr, "Error: Unsupported argument type flag in flags %s\n", argStr);
exit_or_restart(1);
}
else if (/*explicitType == TYPE_ARRAY ||*/ explicitType == TYPE_POINTER) {
fprintf(stderr, "Error: Array or Pointer flag in unsupported position in flags %s. Order must be -[p[p..]][a][primitive type flag]\n", argStr);
exit_or_restart(1);
}
arg->type = explicitType;
arg->explicitType = true;
arg->pointer_depth = pointer_depth;
arg->array_value_pointer_depth = array_value_pointer_depth;
}
void parse_all_from_argvs(ArgInfoContainer* info, int argc, char* argv[], int *args_used, bool is_return, bool is_struct);
ArgInfo* parse_one_arg(int argc, char* argv[], int *args_used, bool is_return){
char* argStr = argv[0];
ArgInfo* storedVar = getVar(argStr);
if (storedVar != NULL) {
return storedVar;
}
ArgInfo* outArg = calloc(1,sizeof(ArgInfo));
outArg->value = malloc(sizeof(*outArg->value));
bool set_to_null = false;
int i = 0;
if (is_return){ // is a return type, so we don't need to parse values or check for the - flag
parse_arg_type_from_flag(outArg, argStr);
} else if (argStr[0] == '-' && !isAllDigits(argStr+1) && !isHexFormat(argStr+1)) {
parse_arg_type_from_flag(outArg, argStr+1);
if (outArg->type != TYPE_STRUCT) argStr = argv[++i]; // Set the value to one arg past the flag, and increment i to skip the value
} else if (argStr[0] == 'N'){ //for NULL
set_to_null = true;
parse_arg_type_from_flag(outArg, argStr+1);
// if (outArg->type != TYPE_STRUCT) argStr = "NULL";
} else { // no flag, so we need to infer the type from the value
infer_arg_type_from_value(outArg, argStr);
}
if (outArg->type==TYPE_STRUCT){ //parsing now in case it's a null type being used for casting a variable
StructInfo* struct_info = outArg->struct_info; // it's allocated inside parse_arg_type_from_flag
outArg->struct_info->info.return_var = calloc(1,sizeof(ArgInfo));
int struct_args_used = 0;
i++; // skip the S: open tag
parse_all_from_argvs(&struct_info->info, argc-i, argv+i, &struct_args_used, is_return || set_to_null, true);
i+=struct_args_used;
outArg->struct_info = struct_info;
// not setting a value here, that will be handled by the make_raw_value_for_struct function in the invoke handler module
if (set_to_null && !is_return){
argStr = argv[++i]; // just setting for the purpose of checking if the next string afterwards is a variable
}
else {
argStr = "";
}
// one problem with this is that we could end up with ambiguity between signatures of ( struct, var ) and ( var cast to struct )
// but we can fix that by only allowing casting to NULL structs (specified like NS:). So if its an actual struct we don't try to cast to it
}
if(!is_return && outArg->explicitType && (outArg->type!=TYPE_STRUCT || set_to_null)){
// if the value is a variable, we should cast it to the type specified in the flag and return
ArgInfo* storedVar = getVar(argStr);
if (storedVar != NULL) {
printf("Attempting cast from variable %s of type ", argStr);
format_and_print_arg_type(storedVar);
printf(" to type ");
format_and_print_arg_type(outArg);
printf("\n");
castArgValueToType(outArg, storedVar);
goto set_args_used_and_return;
}
}
if (outArg->type!=TYPE_STRUCT && (set_to_null || is_return)) { // should probably instead make another field for is_null in both parse_one_arg and parse_all_from_argvs, but this is adequate for now
set_arg_value_nullish(outArg); // I'm not sure if this causes a memory leak, but fixing memory leaks will be a project for another time
} else if (!is_return && outArg->type!=TYPE_STRUCT) {
convert_arg_value(outArg, argStr);
}
set_args_used_and_return: // this is a goto label
*args_used += i;
return outArg;
}
void parse_all_from_argvs(ArgInfoContainer* info, int argc, char* argv[], int *args_used, bool is_return, bool is_struct) {
// ArgInfoContainer* arginfo = info->type == FUNCTION_INFO ? &info->function_info->info : &info->struct_info->info;
#ifdef DEBUG
printf("Beginning to parse args (%d remaining)\n", argc);
#endif
bool hit_struct_close = false;
info->vararg_start = -1;
int i;
for (i=0; i < argc; i++) {
char* argStr = argv[i];
if (strcmp(argStr, ":S") == 0){ // this terminates a struct flag
if (!is_struct){
fprintf(stderr, "Error: Unexpected close struct flag :S\n");
exit_or_restart(1);
}
hit_struct_close = true;
break;
}
if (strcmp(argStr, "...") == 0) {
if (info->vararg_start != -1){
fprintf(stderr, "Error: Multiple varargs flags encountered\n");
exit_or_restart(1);
} else if (is_return){
fprintf(stderr, "Error: Varargs flag encountered in return type\n");
exit_or_restart(1);
} else if (is_struct){
fprintf(stderr, "Error: Varargs flag encountered in struct\n");
exit_or_restart(1);
}
info->vararg_start = info->arg_count;
continue;
}
ArgInfo* arg = parse_one_arg(argc-i, argv+i, &i, is_return);
addArgToFunctionCallInfo(info, arg);
}
if (is_struct && !hit_struct_close){
fprintf(stderr, "Error: Struct flag not closed with :S\n");
exit_or_restart(1);
}
*args_used = i;
convert_all_arrays_to_arginfo_ptr_sized_after_parsing(info);
second_pass_arginfo_ptr_sized_null_array_initialization(info);
}
FunctionCallInfo* parse_arguments(int argc, char* argv[]) {
FunctionCallInfo* info = calloc(1, sizeof(FunctionCallInfo)); // using calloc to zero out the struct
setCodeSectionForSegfaultHandler("parse_arguments : resolve library path");
// arg[1] is the library path
info->library_path = resolve_library_path(argv[0]);
if (!info->library_path) {
fprintf(stderr, "Error: Unable to resolve library path for %s\n", argv[0]);
exit_or_restart(1);
}
setCodeSectionForSegfaultHandler("parse_arguments : parse return type");
int args_used_by_return = 0;
info->info.return_var = parse_one_arg(argc-1, argv+1, &args_used_by_return, true);
argc-=args_used_by_return;
argv+=args_used_by_return;
if (info->info.return_var->type == TYPE_UNKNOWN) {
fprintf(stderr, "Error: Unknown Return Type\n");
exit_or_restart(1);
return NULL;
}
//check if return is an array without a specified size
if (info->info.return_var->is_array==ARRAY_STATIC_SIZE_UNSET) {
fprintf(stderr, "Error: Array return types must have a specified size. Put a number at the end of the flag with no spaces, eg %s4 for a static size, or t and a number to specify an argnumber that will represent size_t for it eg %st1 for the first arg, since 0=return\n", argv[1],argv[1]);
exit_or_restart(1);
}
// arg[2] is the function name
info->function_name = strdup(argv[2]);
if (!info->function_name) {
fprintf(stderr, "Error: Unable to allocate memory for function name\n");
return NULL;
}
setCodeSectionForSegfaultHandler("parse_arguments : parse function arguments");
//TODO: maybe at some point we should be able to take a hex offset instead of a function name
parse_all_from_argvs(&info->info, argc-3, argv+3, &args_used_by_return, false, false);
if (args_used_by_return != argc-3){
fprintf(stderr, "Error: Not all arguments were used in parsing\n");
exit_or_restart(1);
}
unsetCodeSectionForSegfaultHandler();
return info;
}