diff options
Diffstat (limited to 'common/util.c')
-rw-r--r-- | common/util.c | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/common/util.c b/common/util.c index f8a0519f17..f1c3e9a41e 100644 --- a/common/util.c +++ b/common/util.c @@ -424,3 +424,38 @@ int cond_went(cond_t *c, int val) return ret; } + +/****************************************************************************/ +/* console command parsing */ + +/** + * Parse offset and size from command line argv[shift] and argv[shift+1] + * + * Default values: If argc<=shift, leaves offset unchanged, returning error if + * *offset<0. If argc<shift+1, leaves size unchanged, returning error if + * *size<0. + */ +int parse_offset_size(int argc, char **argv, int shift, + int *offset, int *size) +{ + char *e; + int i; + + if (argc > shift) { + i = (uint32_t)strtoi(argv[shift], &e, 0); + if (*e) + return EC_ERROR_PARAM1; + *offset = i; + } else if (*offset < 0) + return EC_ERROR_PARAM_COUNT; + + if (argc > shift + 1) { + i = (uint32_t)strtoi(argv[shift + 1], &e, 0); + if (*e) + return EC_ERROR_PARAM2; + *size = i; + } else if (*size < 0) + return EC_ERROR_PARAM_COUNT; + + return EC_SUCCESS; +} |