From e7d0b2c114966ff5b60328efe14c21f8206f6064 Mon Sep 17 00:00:00 2001 From: Nick Chambers Date: Sun, 19 Jun 2022 00:56:39 -0500 Subject: [PATCH] Find the appropriate entry for short options --- include/gargoyle/codex.h | 3 +-- include/gargoyle/sleuth.h | 1 + src/gargoyle.c | 31 +++++++++++++++++-------------- src/sleuth.c | 10 ++++++++++ 4 files changed, 29 insertions(+), 16 deletions(-) diff --git a/include/gargoyle/codex.h b/include/gargoyle/codex.h index 00a187e..0a12102 100644 --- a/include/gargoyle/codex.h +++ b/include/gargoyle/codex.h @@ -4,7 +4,6 @@ #include const uint8_t GARGOYLE_ERR_SUCCESS = 0; -const uint8_t GARGOYLE_ERR_RESERVED = 1; -const uint8_t GARGOYLE_ERR_UNKNOWN_OPT = 2; +const uint8_t GARGOYLE_ERR_UNKNOWN_OPT = 1; #endif diff --git a/include/gargoyle/sleuth.h b/include/gargoyle/sleuth.h index b88dbd3..8e9ffc8 100644 --- a/include/gargoyle/sleuth.h +++ b/include/gargoyle/sleuth.h @@ -22,5 +22,6 @@ struct gargoyle_opt { #define GARGOYLE_EZ_OPT(brand) brand, (sizeof(brand) - 1), 0[brand] // >:) struct gargoyle_opt *gargoyle_find_brand(uint16_t optc, struct gargoyle_opt *optv, const char *brand); +struct gargoyle_opt *gargoyle_find_emblem(uint16_t optc, struct gargoyle_opt *optv, const char emblem); #endif diff --git a/src/gargoyle.c b/src/gargoyle.c index 298e4d5..329caf4 100644 --- a/src/gargoyle.c +++ b/src/gargoyle.c @@ -2,37 +2,40 @@ #include uint8_t gargoyle_digest(uint16_t optc, struct gargoyle_opt *optv, int *argc, char ***argv) { - uint8_t status = 1; - - while(status) { + while(1) { const char *arg = **argv; if(!arg || *arg != '-' || !*(arg + 1)) { - status = 0; - break; + return GARGOYLE_ERR_SUCCESS; } else if(*arg == '-' && *(arg + 1) == '-' && !*(arg + 2)) { *argc -= 1; *argv += 1; - status = 0; - break; + return GARGOYLE_ERR_SUCCESS; } struct gargoyle_opt *opt = NULL; if(*(arg + 1) == '-') { opt = gargoyle_find_brand(optc, optv, arg + 2); - } else { - /* loop through short opts */ - } - if(!opt) { - status = GARGOYLE_ERR_UNKNOWN_OPT; - break; + if(!opt) { + return GARGOYLE_ERR_UNKNOWN_OPT; + } + } else { + const char *idx = arg + 1; + + for(; *idx; idx += 1) { + opt = gargoyle_find_emblem(optc, optv, *idx); + + if(!opt) { + return GARGOYLE_ERR_UNKNOWN_OPT; + } + } } *argc -= 1; *argv += 1; } - return status; + return GARGOYLE_ERR_SUCCESS; } diff --git a/src/sleuth.c b/src/sleuth.c index a42267b..31d0c33 100644 --- a/src/sleuth.c +++ b/src/sleuth.c @@ -11,3 +11,13 @@ struct gargoyle_opt *gargoyle_find_brand(uint16_t optc, struct gargoyle_opt *opt return NULL; } + +struct gargoyle_opt *gargoyle_find_emblem(uint16_t optc, struct gargoyle_opt *optv, const char emblem) { + for(; optc; optc -= 1, optv += 1) { + if(emblem == optv->emblem) { + return optv; + } + } + + return NULL; +}