1
1
/*
2
- Copyright (c) 2014. The YARA Authors. All Rights Reserved.
2
+ Copyright (c) 2014-2021 . The YARA Authors. All Rights Reserved.
3
3
4
4
Redistribution and use in source and binary forms, with or without modification,
5
5
are permitted provided that the following conditions are met:
@@ -34,14 +34,16 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
34
#include <string.h>
35
35
#include <yara.h>
36
36
37
- #define args_is_long_arg (arg ) (arg[0] == '-' && arg[1] == '-' && arg[2] != '\0')
37
+ #include "args.h"
38
+ #include "common.h"
39
+ #include "unicode.h"
38
40
41
+ #define args_is_long_arg (arg ) (arg[0] == '-' && arg[1] == '-' && arg[2] != '\0')
39
42
40
43
#define args_is_short_arg (arg ) \
41
44
(arg[0] == '-' && arg[1] != '-' && arg[1] != '\0')
42
45
43
-
44
- args_option_t * args_get_short_option (args_option_t * options , const char opt )
46
+ args_option_t * args_get_short_option (args_option_t * options , const char_t opt )
45
47
{
46
48
while (options -> type != ARGS_OPT_END )
47
49
{
@@ -54,19 +56,18 @@ args_option_t* args_get_short_option(args_option_t* options, const char opt)
54
56
return NULL ;
55
57
}
56
58
57
-
58
- args_option_t * args_get_long_option (args_option_t * options , const char * arg )
59
+ args_option_t * args_get_long_option (args_option_t * options , const char_t * arg )
59
60
{
60
61
arg += 2 ; // skip starting --
61
62
62
63
while (options -> type != ARGS_OPT_END )
63
64
{
64
65
if (options -> long_name != NULL )
65
66
{
66
- size_t l = strlen (options -> long_name );
67
+ size_t l = _tcslen (options -> long_name );
67
68
68
69
if ((arg [l ] == '\0' || arg [l ] == '=' ) &&
69
- strstr (arg , options -> long_name ) == arg )
70
+ _tcsstr (arg , options -> long_name ) == arg )
70
71
{
71
72
return options ;
72
73
}
@@ -78,13 +79,12 @@ args_option_t* args_get_long_option(args_option_t* options, const char* arg)
78
79
return NULL ;
79
80
}
80
81
81
-
82
82
args_error_type_t args_parse_option (
83
83
args_option_t * opt ,
84
- const char * opt_arg ,
84
+ const char_t * opt_arg ,
85
85
int * opt_arg_was_used )
86
86
{
87
- char * endptr = NULL ;
87
+ char_t * endptr = NULL ;
88
88
89
89
if (opt_arg_was_used != NULL )
90
90
* opt_arg_was_used = 0 ;
@@ -99,11 +99,10 @@ args_error_type_t args_parse_option(
99
99
break ;
100
100
101
101
case ARGS_OPT_INTEGER :
102
-
103
102
if (opt_arg == NULL )
104
103
return ARGS_ERROR_REQUIRED_INTEGER_ARG ;
105
104
106
- * (int * ) opt -> value = strtol (opt_arg , & endptr , 0 );
105
+ * (long * ) opt -> value = _tcstol (opt_arg , & endptr , 0 );
107
106
108
107
if (* endptr != '\0' )
109
108
return ARGS_ERROR_REQUIRED_INTEGER_ARG ;
@@ -114,14 +113,20 @@ args_error_type_t args_parse_option(
114
113
break ;
115
114
116
115
case ARGS_OPT_STRING :
117
-
118
116
if (opt_arg == NULL )
119
117
return ARGS_ERROR_REQUIRED_STRING_ARG ;
120
118
119
+ #ifdef _UNICODE
120
+ if (opt -> max_count > 1 )
121
+ ((const char * * ) opt -> value )[opt -> count ] = unicode_to_ansi (opt_arg );
122
+ else
123
+ * (const char * * ) opt -> value = unicode_to_ansi (opt_arg );
124
+ #else
121
125
if (opt -> max_count > 1 )
122
126
((const char * * ) opt -> value )[opt -> count ] = opt_arg ;
123
127
else
124
128
* (const char * * ) opt -> value = opt_arg ;
129
+ #endif
125
130
126
131
if (opt_arg_was_used != NULL )
127
132
* opt_arg_was_used = 1 ;
@@ -137,33 +142,31 @@ args_error_type_t args_parse_option(
137
142
return ARGS_ERROR_OK ;
138
143
}
139
144
140
-
141
- void args_print_error (args_error_type_t error , const char * option )
145
+ void args_print_error (args_error_type_t error , const char_t * option )
142
146
{
143
147
switch (error )
144
148
{
145
149
case ARGS_ERROR_UNKNOWN_OPT :
146
- fprintf (stderr , "unknown option `%s`\n" , option );
150
+ _ftprintf (stderr , _T ( "unknown option `%s`\n" ) , option );
147
151
break ;
148
152
case ARGS_ERROR_TOO_MANY :
149
- fprintf (stderr , "too many `%s` options\n" , option );
153
+ _ftprintf (stderr , _T ( "too many `%s` options\n" ) , option );
150
154
break ;
151
155
case ARGS_ERROR_REQUIRED_INTEGER_ARG :
152
- fprintf (stderr , "option `%s` requires an integer argument\n" , option );
156
+ _ftprintf (stderr , _T ( "option `%s` requires an integer argument\n" ) , option );
153
157
break ;
154
158
case ARGS_ERROR_REQUIRED_STRING_ARG :
155
- fprintf (stderr , "option `%s` requires a string argument\n" , option );
159
+ _ftprintf (stderr , _T ( "option `%s` requires a string argument\n" ) , option );
156
160
break ;
157
161
case ARGS_ERROR_UNEXPECTED_ARG :
158
- fprintf (stderr , "option `%s` doesn't expect an argument\n" , option );
162
+ _ftprintf (stderr , _T ( "option `%s` doesn't expect an argument\n" ) , option );
159
163
break ;
160
164
default :
161
165
return ;
162
166
}
163
167
}
164
168
165
-
166
- int args_parse (args_option_t * options , int argc , const char * * argv )
169
+ int args_parse (args_option_t * options , int argc , const char_t * * argv )
167
170
{
168
171
args_error_type_t error = ARGS_ERROR_OK ;
169
172
@@ -172,15 +175,15 @@ int args_parse(args_option_t* options, int argc, const char** argv)
172
175
173
176
while (i < argc )
174
177
{
175
- const char * arg = argv [i ];
178
+ const char_t * arg = argv [i ];
176
179
177
180
if (args_is_long_arg (arg ))
178
181
{
179
182
args_option_t * opt = args_get_long_option (options , arg );
180
183
181
184
if (opt != NULL )
182
185
{
183
- const char * equal = strchr (arg , '=' );
186
+ const char_t * equal = _tcschr (arg , '=' );
184
187
185
188
if (equal )
186
189
error = args_parse_option (opt , equal + 1 , NULL );
@@ -242,38 +245,51 @@ int args_parse(args_option_t* options, int argc, const char** argv)
242
245
i ++ ;
243
246
}
244
247
248
+ // Initialize to NULL the value pointers for all options.
249
+ for (; options -> type != ARGS_OPT_END ; options ++ ) options -> value = NULL ;
250
+
245
251
return o ;
246
252
}
247
253
248
-
249
254
void args_print_usage (args_option_t * options , int help_alignment )
250
255
{
251
- char buffer [128 ];
256
+ char_t buffer [128 ];
252
257
253
258
for (; options -> type != ARGS_OPT_END ; options ++ )
254
259
{
255
- int len = sprintf (buffer , " " );
260
+ int len = _stprintf (buffer , _T ( " " ) );
256
261
257
262
if (options -> short_name != '\0' )
258
- len += sprintf (buffer + len , "-%c" , options -> short_name );
263
+ len += _stprintf (buffer + len , _T ( "-%c" ) , options -> short_name );
259
264
else
260
- len += sprintf (buffer + len , " " );
265
+ len += _stprintf (buffer + len , _T ( " " ) );
261
266
262
267
if (options -> short_name != '\0' && options -> long_name != NULL )
263
- len += sprintf (buffer + len , ", " );
268
+ len += _stprintf (buffer + len , _T ( ", " ) );
264
269
265
270
if (options -> long_name != NULL )
266
- len += sprintf (buffer + len , "--%s" , options -> long_name );
271
+ len += _stprintf (buffer + len , _T ( "--%s" ) , options -> long_name );
267
272
268
273
if (options -> type == ARGS_OPT_STRING || options -> type == ARGS_OPT_INTEGER )
269
274
{
270
- len += sprintf (
275
+ len += _stprintf (
271
276
buffer + len ,
272
- "%s%s" ,
273
- (options -> long_name != NULL ) ? "=" : " " ,
277
+ _T ( "%s%s" ) ,
278
+ (options -> long_name != NULL ) ? _T ( "=" ) : _T ( " " ) ,
274
279
options -> type_help );
275
280
}
276
281
277
- printf ("%-*s%s\n" , help_alignment , buffer , options -> help );
282
+ _tprintf (_T ("%-*s%s\n" ), help_alignment , buffer , options -> help );
283
+ }
284
+ }
285
+
286
+ void args_free (args_option_t * options )
287
+ {
288
+ for (; options -> type != ARGS_OPT_END ; options ++ )
289
+ {
290
+ if (options -> type == ARGS_OPT_STRING && options -> value != NULL )
291
+ {
292
+ free (options -> value );
293
+ }
278
294
}
279
295
}
0 commit comments