forked from pspdev/prxtool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getargs.C
188 lines (170 loc) · 3.19 KB
/
getargs.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
/***************************************************************
* PRXTool : Utility for PSP executables.
* (c) TyRaNiD 2k5
*
* getargs.C - Argument parser
***************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "getargs.h"
char **GetArgs(int *argc, char **argv, struct ArgEntry *entry, int argcount)
{
int error = 0;
if((argc == NULL) || (argv == NULL) || (entry == NULL))
{
return NULL;
}
(*argc)--;
argv++;
while(*argc > 0)
{
const char *arg;
struct ArgEntry *ent;
if(*argv == NULL)
{
error = 1;
break;
}
arg = *argv;
if(arg[0] != '-')
{
break;
}
if(arg[1] == '-')
{
/* Long arg */
int i;
ent = NULL;
for(i = 0; i < argcount; i++)
{
if(entry[i].full)
{
if(strcmp(entry[i].full, &arg[2]) == 0)
{
ent = &entry[i];
}
}
}
}
else if(arg[1] != 0)
{
int i;
ent = NULL;
/* Error check, short options should be 2 characters */
if(strlen(arg) == 2)
{
for(i = 0; i < argcount; i++)
{
if(entry[i].ch == arg[1])
{
ent = &entry[i];
}
}
}
}
else
{
/* Single - means stop processing */
(*argc)--;
argv++;
break;
}
if(ent == NULL)
{
fprintf(stderr, "Invalid argument %s\n", arg);
error = 1;
break;
}
if(ent->argvoid == NULL)
{
fprintf(stderr, "Internal error processing %s\n", arg);
error = 1;
break;
}
if(ent->opt == ARG_OPT_NONE)
{
switch(ent->type)
{
case ARG_TYPE_BOOL: {
bool *argbool = (bool *) ent->argvoid;
*argbool = ent->val;
}
break;
case ARG_TYPE_INT: {
int *argint = (int *) ent->argvoid;
*argint = ent->val;
}
break;
case ARG_TYPE_FUNC: { ArgFunc argfunc = (ArgFunc) ent->argvoid;
if(argfunc(NULL) == 0)
{
fprintf(stderr, "Error processing argument for %s\n", arg);
error = 1;
}
}
break;
default: fprintf(stderr, "Invalid type for option %s\n", arg);
error = 1;
break;
};
if(error)
{
break;
}
}
else if(ent->opt == ARG_OPT_REQUIRED)
{
if(*argc <= 1)
{
fprintf(stderr, "No argument passed for %s\n", arg);
error = 1;
break;
}
(*argc)--;
argv++;
switch(ent->type)
{
case ARG_TYPE_INT: { int *argint = (int*) ent->argvoid;
*argint = strtoul(argv[0], NULL, 0);
}
break;
case ARG_TYPE_STR: { const char **argstr = (const char **) ent->argvoid;
*argstr = argv[0];
}
break;
case ARG_TYPE_FUNC: { ArgFunc argfunc = (ArgFunc) ent->argvoid;
if(argfunc(argv[0]) == 0)
{
fprintf(stderr, "Error processing argument for %s\n", arg);
error = 1;
}
}
break;
default: fprintf(stderr, "Invalid type for option %s\n", arg);
error = 1;
break;
};
if(error)
{
break;
}
}
else
{
fprintf(stderr, "Internal options error processing %s\n", arg);
error = 1;
break;
}
(*argc)--;
argv++;
}
if(error)
{
return NULL;
}
else
{
return argv;
}
}