forked from VirusTotal/yara
-
Notifications
You must be signed in to change notification settings - Fork 0
/
args.c
executable file
·278 lines (217 loc) · 5.86 KB
/
args.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
/*
Copyright (c) 2014. The YARA Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include "args.h"
#define args_is_long_arg(arg) \
(arg[0] == '-' && arg[1] == '-' && arg[2] != '\0')
#define args_is_short_arg(arg) \
(arg[0] == '-' && arg[1] != '-' && arg[1] != '\0')
args_option_t* args_get_short_option(
args_option_t *options,
const char opt)
{
while (options->type != ARGS_OPT_END)
{
if (opt == options->short_name)
return options;
options++;
}
return NULL;
}
args_option_t* args_get_long_option(
args_option_t *options,
const char* arg)
{
arg += 2; // skip starting --
while (options->type != ARGS_OPT_END)
{
if (options->long_name != NULL)
{
size_t l = strlen(options->long_name);
if ((arg[l] == '\0' || arg[l] == '=') &&
strstr(arg, options->long_name) == arg)
{
return options;
}
}
options++;
}
return NULL;
}
args_error_type_t args_parse_option(
args_option_t* opt,
const char* opt_arg,
int* opt_arg_was_used)
{
char *endptr = NULL;
if (opt_arg_was_used != NULL)
*opt_arg_was_used = 0;
if (opt->count == opt->max_count)
return ARGS_ERROR_TOO_MANY;
switch (opt->type)
{
case ARGS_OPT_BOOLEAN:
*(int*) opt->value = 1;
break;
case ARGS_OPT_INTEGER:
if (opt_arg == NULL)
return ARGS_ERROR_REQUIRED_INTEGER_ARG;
*(int*) opt->value = strtol(opt_arg, &endptr, 0);
if (*endptr != '\0')
return ARGS_ERROR_REQUIRED_INTEGER_ARG;
if (opt_arg_was_used != NULL)
*opt_arg_was_used = 1;
break;
case ARGS_OPT_STRING:
if (opt_arg == NULL)
return ARGS_ERROR_REQUIRED_STRING_ARG;
if (opt->max_count > 1)
((const char**)opt->value)[opt->count] = opt_arg;
else
*(const char**) opt->value = opt_arg;
if (opt_arg_was_used != NULL)
*opt_arg_was_used = 1;
break;
default:
assert(0);
}
opt->count++;
return ARGS_ERROR_OK;
}
void args_print_error(
args_error_type_t error,
const char* option)
{
switch(error)
{
case ARGS_ERROR_UKNOWN_OPT:
fprintf(stderr, "unknown option `%s`\n", option);
break;
case ARGS_ERROR_TOO_MANY:
fprintf(stderr, "too many `%s` options\n", option);
break;
case ARGS_ERROR_REQUIRED_INTEGER_ARG:
fprintf(stderr, "option `%s` requieres an integer argument\n", option);
break;
case ARGS_ERROR_REQUIRED_STRING_ARG:
fprintf(stderr, "option `%s` requieres a string argument\n", option);
break;
case ARGS_ERROR_UNEXPECTED_ARG:
fprintf(stderr, "option `%s` doesn't expect an argument\n", option);
break;
default:
return;
}
}
int args_parse(
args_option_t *options,
int argc,
const char **argv)
{
args_error_type_t error = ARGS_ERROR_OK;
int i = 1; // start with i = 1, argv[0] is the program name
int o = 0;
while (i < argc)
{
const char* arg = argv[i];
if (args_is_long_arg(arg))
{
args_option_t* opt = args_get_long_option(options, arg);
if (opt != NULL)
{
const char* equal = strchr(arg, '=');
if (equal)
error = args_parse_option(opt, equal + 1, NULL);
else
error = args_parse_option(opt, NULL, NULL);
}
else
{
error = ARGS_ERROR_UKNOWN_OPT;
}
}
else if (args_is_short_arg(arg))
{
for (int j = 1; arg[j] != '\0'; j++)
{
args_option_t* opt = args_get_short_option(options, arg[j]);
if (opt != NULL)
{
if (arg[j + 1] == '\0')
{
int arg_used;
// short option followed by a space, argv[i + 1] could be
// an argument for the option (i.e: -a <arg>)
error = args_parse_option(opt, argv[i + 1], &arg_used);
// argv[i + 1] was actually an argument to the option, skip it.
if (arg_used)
i++;
}
else
{
// short option followed by another option (i.e: -ab), no
// argument for this option
error = args_parse_option(opt, NULL, NULL);
}
}
else
{
error = ARGS_ERROR_UKNOWN_OPT;
}
if (error != ARGS_ERROR_OK)
break;
}
}
else
{
argv[o++] = arg;
}
if (error != ARGS_ERROR_OK)
{
args_print_error(error, arg);
exit(1);
}
i++;
}
return o;
}
void args_print_usage(
args_option_t *options,
int help_alignment)
{
char buffer[128];
for (; options->type != ARGS_OPT_END; options++)
{
int len = sprintf(buffer, " ");
if (options->short_name != '\0')
len += sprintf(buffer + len, "-%c", options->short_name);
else
len += sprintf(buffer + len, " ");
if (options->short_name != '\0' && options->long_name != NULL)
len += sprintf(buffer + len, ", ");
if (options->long_name != NULL)
len += sprintf(buffer + len, "--%s", options->long_name);
if (options->type == ARGS_OPT_STRING ||
options->type == ARGS_OPT_INTEGER)
{
len += sprintf(
buffer + len,
"%s%s",
(options->long_name != NULL) ? "=" : " ",
options->type_help);
}
printf("%-*s%s\n", help_alignment, buffer, options->help);
}
}