-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathandroid_option.c
240 lines (201 loc) · 6.35 KB
/
android_option.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
#include "android_option.h"
#include "android_debug.h"
#include "android_utils.h"
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#define _VERBOSE_TAG(x,y) { #x, VERBOSE_##x, y },
static const struct { const char* name; int flag; const char* text; }
debug_tags[] = {
VERBOSE_TAG_LIST
{ 0, 0, 0 }
};
static void parse_debug_tags( const char* tags );
static void parse_env_debug_tags( void );
typedef struct {
const char* name;
int var_offset;
int var_is_param;
int var_is_config;
} OptionInfo;
#define OPTION(_name,_type,_config) \
{ #_name, offsetof(AndroidOptions,_name), _type, _config },
static const OptionInfo option_keys[] = {
#define OPT_PARAM(_name,_template,_descr) OPTION(_name,1,0)
#define OPT_FLAG(_name,_descr) OPTION(_name,0,0)
#define CFG_PARAM(_name,_template,_descr) OPTION(_name,1,1)
#define CFG_FLAG(_name,_descr) OPTION(_name,0,1)
#include "android_options.h"
{ NULL, 0, 0, 0 }
};
int
android_parse_options( int *pargc, char** *pargv, AndroidOptions* opt )
{
int nargs = *pargc-1;
char** aread = *pargv+1;
char** awrite = aread;
memset( opt, 0, sizeof *opt );
while (nargs > 0 && aread[0][0] == '-') {
char* arg = aread[0]+1;
int nn;
/* an option cannot contain an underscore */
if (strchr(arg, '_') != NULL) {
break;
}
nargs--;
aread++;
/* for backwards compatibility with previous versions */
if (!strcmp(arg, "verbose")) {
arg = "debug-init";
}
/* special handing for -debug <tags> */
if (!strcmp(arg, "debug")) {
if (nargs == 0) {
derror( "-debug must be followed by tags (see -help-verbose)\n");
exit(1);
}
nargs--;
parse_debug_tags(*aread++);
continue;
}
/* special handling for -debug-<tag> and -debug-no-<tag> */
if (!strncmp(arg, "debug-", 6)) {
int remove = 0;
unsigned long mask = 0;
arg += 6;
if (!strncmp(arg, "no-", 3)) {
arg += 3;
remove = 1;
}
if (!strcmp(arg, "all")) {
mask = ~0;
}
for (nn = 0; debug_tags[nn].name; nn++) {
if (!strcmp(arg, debug_tags[nn].name)) {
mask = (1 << debug_tags[nn].flag);
break;
}
}
if (remove)
android_verbose &= ~mask;
else
android_verbose |= mask;
continue;
}
/* look into our table of options
*
* NOTE: the 'option_keys' table maps option names
* to field offsets into the AndroidOptions structure.
*
* however, the names stored in the table used underscores
* instead of dashes. this means that the command-line option
* '-foo-bar' will be associated to the name 'foo_bar' in
* this table, and will point to the field 'foo_bar' or
* AndroidOptions.
*
* as such, before comparing the current option to the
* content of the table, we're going to translate dashes
* into underscores.
*/
{
const OptionInfo* oo = option_keys;
char arg2[64];
int len = strlen(arg);
/* copy into 'arg2' buffer, translating dashes
* to underscores. note that we truncate to 63
* characters, which should be enough in practice
*/
if (len > sizeof(arg2)-1)
len = sizeof(arg2)-1;
memcpy(arg2, arg, len+1);
buffer_translate_char(arg2, len, '-', '_');
for ( ; oo->name; oo++ ) {
if ( !strcmp( oo->name, arg2 ) ) {
void* field = (char*)opt + oo->var_offset;
if (oo->var_is_param) {
/* parameter option */
if (nargs == 0) {
derror( "-%s must be followed by parameter (see -help-%s)",
arg, arg );
exit(1);
}
nargs--;
((char**)field)[0] = *aread++;
} else {
/* flag option */
((int*)field)[0] = 1;
}
break;
}
}
if (oo->name == NULL) { /* unknown option ? */
nargs++;
aread--;
break;
}
}
}
/* copy remaining parameters, if any, to command line */
*pargc = nargs + 1;
while (nargs > 0) {
awrite[0] = aread[0];
awrite ++;
aread ++;
nargs --;
}
awrite[0] = NULL;
return 0;
}
/* special handling of -debug option and tags */
#define ENV_DEBUG "ANDROID_DEBUG"
static void
parse_debug_tags( const char* tags )
{
char* x;
char* y;
char* x0;
if (tags == NULL)
return;
x = x0 = strdup(tags);
while (*x) {
y = strchr(x, ',');
if (y == NULL)
y = x + strlen(x);
else
*y++ = 0;
if (y > x+1) {
int nn, remove = 0;
unsigned mask = 0;
if (x[0] == '-') {
remove = 1;
x += 1;
}
if (!strcmp( "all", x ))
mask = ~0;
else {
for (nn = 0; debug_tags[nn].name != NULL; nn++) {
if ( !strcmp( debug_tags[nn].name, x ) ) {
mask |= (1 << debug_tags[nn].flag);
break;
}
}
}
if (mask == 0)
dprint( "ignoring unknown " ENV_DEBUG " item '%s'", x );
else {
if (remove)
android_verbose &= ~mask;
else
android_verbose |= mask;
}
}
x = y;
}
free(x0);
}
static void
parse_env_debug_tags( void )
{
const char* env = getenv( ENV_DEBUG );
parse_debug_tags( env );
}