1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2017 Merlok
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // Command line parser core commands
9 //-----------------------------------------------------------------------------
13 #include "argtable3.h"
17 #define arg_param_begin arg_lit0("h", "help", "This help")
18 #define arg_param_end arg_end(20)
20 #define arg_getsize(a) (sizeof(a) / sizeof(a[0]))
21 #define arg_get_lit(ctx, n) (((struct arg_lit*)((ctx)->argtable)[(n)])->count)
23 #define arg_get_int_count(ctx, n) (((struct arg_int*)((ctx)->argtable)[(n)])->count)
24 #define arg_get_int(ctx, n) (((struct arg_int*)((ctx)->argtable)[(n)])->ival[0])
25 #define arg_get_int_def(ctx, n, def)(arg_get_int_count((ctx), (n)) ? (arg_get_int((ctx), (n))) : (def))
27 #define arg_get_dbl_count(ctx, n) (((struct arg_dbl*)((ctx)->argtable)[(n)])->count)
28 #define arg_get_dbl(ctx, n) (((struct arg_dbl*)((ctx)->argtable)[(n)])->dval[0])
29 #define arg_get_dbl_def(ctx, n, def)(arg_get_dbl_count((ctx), (n)) ? (arg_get_dbl((ctx), (n))) : (def))
31 #define arg_get_u32(ctx, n) (uint32_t)(((struct arg_u64*)((ctx)->argtable)[(n)])->uval[0])
32 #define arg_get_u32_def(ctx, n, def) (arg_get_u64_count((ctx), (n)) ? (arg_get_u32((ctx), (n))) : (uint32_t)(def))
34 #define arg_get_u64_count(ctx, n) (((struct arg_u64*)((ctx)->argtable)[(n)])->count)
35 #define arg_get_u64(ctx, n) (((struct arg_u64*)((ctx)->argtable)[(n)])->uval[0])
36 #define arg_get_u64_def(ctx, n, def) (arg_get_u64_count((ctx), (n)) ? (arg_get_u64((ctx), (n))) : (uint64_t)(def))
38 #define arg_get_str(ctx, n) ((struct arg_str*)((ctx)->argtable)[(n)])
39 #define arg_get_str_len(ctx, n) (strlen(((struct arg_str*)((ctx)->argtable)[(n)])->sval[0]))
41 #define arg_strx1(shortopts, longopts, datatype, glossary) (arg_strn((shortopts), (longopts), (datatype), 1, 250, (glossary)))
42 #define arg_strx0(shortopts, longopts, datatype, glossary) (arg_strn((shortopts), (longopts), (datatype), 0, 250, (glossary)))
44 #define CLIParserFree(ctx) if ((ctx)) {arg_freetable((ctx)->argtable, (ctx)->argtableLen); free((ctx)); (ctx)=NULL;}
46 #define CLIExecWithReturn(ctx, cmd, atbl, ifempty) if (CLIParserParseString((ctx), (cmd), (atbl), arg_getsize((atbl)), (ifempty))) {CLIParserFree((ctx)); return PM3_ESOFT;}
48 #define CLIGetHexBLessWithReturn(ctx, paramnum, data, datalen, delta) if (CLIParamHexToBuf(arg_get_str((ctx), (paramnum)), (data), sizeof((data)) - (delta), (datalen))) {CLIParserFree((ctx)); return PM3_ESOFT;}
50 #define CLIGetHexWithReturn(ctx, paramnum, data, datalen) if (CLIParamHexToBuf(arg_get_str((ctx), (paramnum)), (data), sizeof((data)), (datalen))) {CLIParserFree((ctx)); return PM3_ESOFT;}
52 #define CLIGetStrWithReturn(ctx, paramnum, data, datalen) if (CLIParamStrToBuf(arg_get_str((ctx), (paramnum)), (data), (*datalen), (datalen))) {CLIParserFree((ctx)); return PM3_ESOFT;}
57 const char *programName
;
58 const char *programHint
;
59 const char *programHelp
;
62 int CLIParserInit(CLIParserContext
**ctx
, const char *vprogramName
, const char *vprogramHint
, const char *vprogramHelp
);
63 void CLIParserPrintHelp(CLIParserContext
*ctx
);
64 int CLIParserParseString(CLIParserContext
*ctx
, const char *str
, void *vargtable
[], size_t vargtableLen
, bool allowEmptyExec
);
65 int CLIParserParseStringEx(CLIParserContext
*ctx
, const char *str
, void *vargtable
[], size_t vargtableLen
, bool allowEmptyExec
, bool clueData
);
66 int CLIParserParseArg(CLIParserContext
*ctx
, int argc
, char **argv
, void *vargtable
[], size_t vargtableLen
, bool allowEmptyExec
);
68 int CLIParamHexToBuf(struct arg_str
*argstr
, uint8_t *data
, int maxdatalen
, int *datalen
);
69 int CLIParamStrToBuf(struct arg_str
*argstr
, uint8_t *data
, int maxdatalen
, int *datalen
);
70 int CLIParamBinToBuf(struct arg_str
*argstr
, uint8_t *data
, int maxdatalen
, int *datalen
);
72 uint64_t arg_get_u64_hexstr_def(CLIParserContext
*ctx
, uint8_t paramnum
, uint64_t def
);
73 int arg_get_u64_hexstr_def_nlen(CLIParserContext
*ctx
, uint8_t paramnum
, uint64_t def
, uint64_t *out
, uint8_t nlen
, bool optional
);
74 int arg_get_u32_hexstr_def(CLIParserContext
*ctx
, uint8_t paramnum
, uint32_t def
, uint32_t *out
);
75 int arg_get_u32_hexstr_def_nlen(CLIParserContext
*ctx
, uint8_t paramnum
, uint32_t def
, uint32_t *out
, uint8_t nlen
, bool optional
);
77 #define CP_SUCCESS_OPTIONAL 1
79 #define CP_ENOPARAM -1
80 #define CP_WRONGLEN -2