PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
anaarg.c
1/*
2# _____ ___ ____ ___ ____
3# ____| | ____| | | |____|
4# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5#-----------------------------------------------------------------------
6# Copyright ps2dev - http://www.ps2dev.org
7# Licenced under Academic Free License version 2.0
8# Review ps2sdk README & LICENSE files for further details.
9*/
10
11#include "srxfixup_internal.h"
12#include <stdint.h>
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16
17int analize_arguments(const Opttable *dopttable, int argc, char **argv)
18{
19 Opt_strings *optstr;
20 const char *opt;
21 Opttable *otp;
22 Opttable *igadd;
23 Opttable *opttable;
24 char *cp;
25 char **argvp;
26 char **nargv;
27 int nargc;
28 int i;
29 int argca;
30 char **argva;
31
32 for ( i = 0; dopttable[i].option; i += 1 )
33 {
34 ;
35 }
36 opttable = (Opttable *)__builtin_alloca((argc + i) * sizeof(Opttable));
37 memset(opttable, 0, (argc + i) * sizeof(Opttable));
38 memcpy(opttable, dopttable, i * sizeof(Opttable));
39 igadd = &opttable[i];
40 nargv = (char **)__builtin_alloca((argc + 1) * sizeof(char *));
41 memset(nargv, 0, (argc + 1) * sizeof(char *));
42 argvp = argv;
43 *nargv = *argv;
44 nargc = 1;
45 for ( argca = argc - 1, argva = argv + 1;; argca -= 1, argva += 1 )
46 {
47 if ( argca <= 0 )
48 {
49 for ( i = 0; i < nargc + 1; i += 1 )
50 {
51 argvp[i] = nargv[i];
52 }
53 for ( i = 0; opttable[i].option; i += 1 )
54 {
55 if ( opttable[i].vartype == 'l' )
56 {
57 *(SLink **)opttable[i].var = ring_to_liner(*(SLink **)opttable[i].var);
58 }
59 }
60 return nargc;
61 }
62 if ( **argva == '-' )
63 {
64 opt = 0;
65 for ( i = 0; opttable[i].option; i += 1 )
66 {
67 if ( opttable[i].havearg == ARG_HAVEARG_UNK3 )
68 {
69 if ( !strcmp(opttable[i].option, *argva) )
70 {
71 break;
72 }
73 }
74 else
75 {
76 if ( !strncmp(opttable[i].option, *argva, strlen(opttable[i].option)) )
77 {
78 break;
79 }
80 }
81 }
82 if ( !opttable[i].option )
83 {
84 return -1;
85 }
86 if ( opttable[i].havearg != ARG_HAVEARG_NONE && opttable[i].havearg != ARG_HAVEARG_UNK3 )
87 {
88 if ( opttable[i].havearg == ARG_HAVEARG_UNK4 || (*argva)[strlen(opttable[i].option)] )
89 {
90 opt = &(*argva)[strlen(opttable[i].option)];
91 }
92 else if ( argca > 1 )
93 {
94 opt = argva[1];
95 argva += 1;
96 argca -= 1;
97 }
98 }
99 if ( opttable[i].havearg != ARG_HAVEARG_REQUIRED || opt )
100 {
101 switch ( opttable[i].vartype )
102 {
103 case 'F':
104 case 'f':
105 if ( (*argva)[strlen(opttable[i].option)] )
106 {
107 *(uint32_t *)opttable[i].var = strtoul(&(*argva)[strlen(opttable[i].option)], NULL, 16);
108 }
109 else
110 {
111 *(uint32_t *)opttable[i].var = (opttable[i].vartype == 'f') ? 1 : 0;
112 }
113 break;
114 case 'h':
115 if ( opt != NULL )
116 {
117 *(uint32_t *)opttable[i].var = strtoul(opt, 0, 16);
118 }
119 break;
120 case 'i':
121 if ( opt != NULL )
122 {
123 for ( otp = igadd; opttable < otp; otp -= 1 )
124 {
125 *otp = otp[-1];
126 }
127 igadd += 1;
128 opttable->option = opt;
129 opttable->vartype = 'n';
130 opttable->havearg = ARG_HAVEARG_UNK3;
131 cp = strchr(opttable->option, ':');
132 if ( cp )
133 {
134 *cp = 0;
135 switch ( cp[1] )
136 {
137 case 'c':
138 opttable->havearg = ARG_HAVEARG_UNK4;
139 break;
140 case 'n':
141 opttable->havearg = ARG_HAVEARG_REQUIRED;
142 break;
143 case 'o':
144 opttable->havearg = ARG_HAVEARG_UNK1;
145 break;
146 default:
147 break;
148 }
149 }
150 }
151 break;
152 case 'l':
153 optstr = (Opt_strings *)calloc(1, sizeof(Opt_strings));
154 optstr->string = opt;
155 *(SLink **)opttable[i].var = add_ring_tail(*(SLink **)opttable[i].var, (SLink *)optstr);
156 break;
157 case 'n':
158 break;
159 case 's':
160 *(const char **)opttable[i].var = opt;
161 break;
162 default:
163 fprintf(stderr, "internal error\n");
164 return -1;
165 }
166 }
167 else
168 {
169 return -1;
170 }
171 }
172 else
173 {
174 nargv[nargc] = *argva;
175 nargc += 1;
176 }
177 }
178}