2
2
3
3
int sap_init (sap_t * parser , int argc , char * * argv ) {
4
4
5
+
5
6
parser -> argc = argc ;
6
7
parser -> argv = argv ;
7
8
@@ -10,6 +11,9 @@ int sap_init(sap_t* parser, int argc, char** argv) {
10
11
11
12
parser -> default_command = NULL ;
12
13
14
+ parser -> arg_commands = calloc (1 , sizeof (sap_command_list_t ));
15
+ parser -> arg_options = calloc (1 , sizeof (sap_option_list_t ));
16
+
13
17
parser -> error_string = calloc (1 , 100 );
14
18
15
19
return 0 ;
@@ -25,8 +29,6 @@ int sap_add_command(sap_t* parser, char* command, command_handler handler) {
25
29
/* go through the command list and look for existing
26
30
* commands, return -1 if so. */
27
31
28
-
29
-
30
32
sap_command_t * current_command = parser -> commands .first ;
31
33
32
34
@@ -146,13 +148,14 @@ static sap_option_t* sap_parse_option(char* arg) {
146
148
* and store this string as the options label */
147
149
148
150
int len = match [1 ].rm_eo - match [1 ].rm_so ;
149
- char * label = calloc (1 , len );
151
+ char * label = calloc (1 , len + 1 );
150
152
memcpy (label , arg + match [1 ].rm_so , len );
151
153
152
154
sap_option_t * option = calloc (1 , sizeof (sap_option_t ));
153
155
option -> label = label ;
154
156
option -> value = NULL ;
155
157
option -> is_flag = 1 ;
158
+ option -> next = NULL ;
156
159
157
160
/* if there is no value assigned to the option then we mark
158
161
* this option as flag. Meaning this option is enabled.
@@ -164,7 +167,7 @@ static sap_option_t* sap_parse_option(char* arg) {
164
167
}
165
168
166
169
len = match [3 ].rm_eo - match [3 ].rm_so ;
167
- char * value = calloc (1 , len );
170
+ char * value = calloc (1 , len + 1 );
168
171
memcpy (value , arg + match [3 ].rm_so , len );
169
172
170
173
option -> value = value ;
@@ -205,34 +208,32 @@ static int sap_is_command(sap_t* parser, char* arg) {
205
208
206
209
int sap_execute (sap_t * parser ) {
207
210
208
- sap_command_list_t * commands = calloc (1 , sizeof (sap_command_list_t ));
209
- sap_option_list_t * options = calloc (1 , sizeof (sap_option_list_t ));
210
-
211
211
sap_command_t * command = parser -> default_command ;
212
212
213
213
for (unsigned int i = 1 ; i < parser -> argc ; i += 1 ) {
214
214
215
215
char * command_str = parser -> argv [i ];
216
216
217
- if (sap_is_command (parser , command_str )) {
218
-
219
- sap_command_t * new_command = calloc (1 , sizeof (sap_command_t ));
220
- new_command -> label = calloc (1 , strlen (command_str ) + 1 );
221
- strcpy (new_command -> label , command_str );
217
+ if (!sap_is_command (parser , command_str )) {
218
+ continue ;
219
+ }
222
220
223
- if (!commands -> first ) {
221
+ sap_command_t * new_command = calloc (1 , sizeof (sap_command_t ));
222
+ new_command -> label = calloc (1 , strlen (command_str ) + 1 );
223
+ strcpy (new_command -> label , command_str );
224
224
225
- commands -> first = new_command ;
226
- commands -> last = new_command ;
225
+ if (!parser -> arg_commands -> first ) {
227
226
228
- } else {
229
-
230
- commands -> last -> next = new_command ;
231
- commands -> last = new_command ;
227
+ parser -> arg_commands -> first = new_command ;
228
+ parser -> arg_commands -> last = new_command ;
232
229
233
- }
230
+ } else {
231
+
232
+ parser -> arg_commands -> last -> next = new_command ;
233
+ parser -> arg_commands -> last = new_command ;
234
234
235
235
}
236
+
236
237
}
237
238
238
239
for (unsigned int i = 1 ; i < parser -> argc ; i += 1 ) {
@@ -241,33 +242,36 @@ int sap_execute(sap_t* parser) {
241
242
242
243
/* check if current_string is option */
243
244
244
- if (sap_is_option (parser , current_string )) {
245
+ if (!sap_is_option (parser , current_string )) {
246
+ continue ;
247
+ }
245
248
246
- sap_option_t * option = sap_parse_option (current_string );
249
+ sap_option_t * option = sap_parse_option (current_string );
247
250
248
- if (!options -> first ) {
251
+ if (!option ) {
252
+ continue ;
253
+ }
249
254
250
- options -> first = option ;
251
- options -> last = option ;
255
+ if (!parser -> arg_options -> first ) {
252
256
253
- option -> next = NULL ;
257
+ parser -> arg_options -> first = option ;
258
+ parser -> arg_options -> last = option ;
254
259
255
- } else {
256
-
257
- options -> last -> next = option ;
258
- options -> last = option ;
260
+ option -> next = NULL ;
259
261
260
- option -> next = NULL ;
262
+ } else {
263
+
264
+ parser -> arg_options -> last -> next = option ;
265
+ parser -> arg_options -> last = option ;
261
266
262
- }
267
+ option -> next = NULL ;
263
268
264
- }
265
-
269
+ }
266
270
}
267
271
268
272
/* get command from the parsers commands list */
269
273
270
- sap_command_t * first_command = commands -> first ;
274
+ sap_command_t * first_command = parser -> arg_commands -> first ;
271
275
sap_command_t * current_command = parser -> commands .first ;
272
276
273
277
while (current_command ) {
@@ -286,27 +290,30 @@ int sap_execute(sap_t* parser) {
286
290
287
291
}
288
292
293
+ int retVal = -1 ;
289
294
290
295
if (current_command ) {
291
296
292
- commands -> first = commands -> first -> next ;
293
-
294
- int retVal = current_command -> handler (commands , options );
297
+ sap_command_t * first = parser -> arg_commands -> first ;
298
+ parser -> arg_commands -> first = parser -> arg_commands -> first -> next ;
295
299
296
- free ( commands -> first );
300
+ retVal = current_command -> handler ( parser -> arg_commands , parser -> arg_options );
297
301
298
- return retVal ;
302
+ free (first -> label );
303
+ free (first );
299
304
300
305
} else if (parser -> default_command ) {
301
306
302
- return parser -> default_command -> handler (commands , options );
307
+ retVal = parser -> default_command -> handler (parser -> arg_commands , parser -> arg_options );
303
308
304
309
} else {
305
310
306
- return -1 ;
311
+ retVal = -1 ;
307
312
308
313
}
309
314
315
+ return retVal ;
316
+
310
317
}
311
318
312
319
int sap_execute_ex (sap_t * parser , sap_command_list_t * commands , sap_option_list_t * options ) {
@@ -332,27 +339,30 @@ int sap_execute_ex(sap_t* parser, sap_command_list_t* commands, sap_option_list_
332
339
333
340
}
334
341
342
+ int retVal = -1 ;
335
343
336
344
if (current_command && first_command ) {
337
345
346
+ sap_command_t * first = commands -> first ;
338
347
commands -> first = commands -> first -> next ;
339
348
340
- int retVal = current_command -> handler (commands , options );
341
-
342
- free (commands -> first );
349
+ retVal = current_command -> handler (commands , options );
343
350
344
- return retVal ;
351
+ free (first -> label );
352
+ free (first );
345
353
346
354
} else if (parser -> default_command ) {
347
355
348
- return parser -> default_command -> handler (commands , options );
356
+ retVal = parser -> default_command -> handler (commands , options );
349
357
350
358
} else {
351
359
352
- return - 1 ;
360
+ retVal = 1 ;
353
361
354
362
}
355
363
364
+ return retVal ;
365
+
356
366
}
357
367
358
368
void sap_free (sap_t * parser ) {
@@ -377,6 +387,30 @@ void sap_free(sap_t* parser) {
377
387
free (tmp );
378
388
}
379
389
390
+ sap_command_t * cc = parser -> arg_commands -> first ;
391
+
392
+ while (cc ) {
393
+ sap_command_t * nc = cc -> next ;
394
+ free (cc -> label );
395
+ free (cc );
396
+ cc = nc ;
397
+ }
398
+
399
+ sap_option_t * co = parser -> arg_options -> first ;
400
+ while (co ) {
401
+ sap_option_t * no = co -> next ;
402
+ free (co -> label );
403
+ if (!co -> is_flag ) {
404
+ free (co -> value );
405
+ }
406
+ free (co );
407
+ co = no ;
408
+ }
409
+
410
+ free (parser -> arg_commands );
411
+ free (parser -> arg_options );
412
+
413
+
380
414
}
381
415
382
416
sap_option_t * sap_get_option_by_index (sap_option_list_t * options , unsigned int index ) {
@@ -389,7 +423,6 @@ sap_option_t* sap_get_option_by_index(sap_option_list_t* options, unsigned int i
389
423
390
424
sap_option_t * curOption = options -> first ;
391
425
392
-
393
426
while (curOption ) {
394
427
395
428
if (counter == index ) {
@@ -413,15 +446,11 @@ sap_option_t* sap_get_option_by_key(sap_option_list_t* options, char* key) {
413
446
414
447
sap_option_t * curOption = options -> first ;
415
448
416
-
417
- while (curOption ) {
418
-
449
+ while (curOption ) {
419
450
if (strcmp (curOption -> label , key ) == 0 ) {
420
451
return curOption ;
421
452
}
422
-
423
453
curOption = curOption -> next ;
424
-
425
454
}
426
455
427
456
return NULL ;
0 commit comments