diff --git a/include/gargoyle.h b/include/gargoyle.h index 2ecb121..63bf53d 100644 --- a/include/gargoyle.h +++ b/include/gargoyle.h @@ -2,12 +2,10 @@ #define __GARGOYLE_H_ #include -#include -#include +#include #include -const uint8_t GARGOYLE_DIGEST_ALL = 1 << 0; - -uint8_t gargoyle_digest_args(uint16_t optc, struct gargoyle_opt *optv, int *argc, char ***argv, uint8_t flags); +uint8_t gargoyle_digest_args(uint16_t optc, struct gargoyle_opt *optv, int *argc, char ***argv, gargoyle_flag_type flags); +uint8_t gargoyle_digest_envh(/* ... */); #endif diff --git a/include/gargoyle/codex.h b/include/gargoyle/codex.h index 38cc42d..c2172f6 100644 --- a/include/gargoyle/codex.h +++ b/include/gargoyle/codex.h @@ -15,4 +15,18 @@ static const uint8_t GARGOYLE_ERR_INVALID_UINT = 3; static const uint8_t GARGOYLE_ERR_INVALID_DBLE = 4; static const uint8_t GARGOYLE_ERR_UNKNOWN_TYPE = 5; +typedef uint8_t gargoyle_flag_type; + +static const gargoyle_flag_type GARGOYLE_FLG_BCASE = 1 << 0; +static const gargoyle_flag_type GARGOYLE_FLG_ECASE = 1 << 1; +static const gargoyle_flag_type GARGOYLE_FLG_ICASE = 1 << 2; +static const gargoyle_flag_type GARGOYLE_FLG_SYMBL = 1 << 3; +static const gargoyle_flag_type GARGOYLE_FLG_GREED = 1 << 4; +static const gargoyle_flag_type GARGOYLE_FLG_FILL0 = 1 << 5; + +static const gargoyle_flag_type GARGOYLE_FLG_FLXBL = GARGOYLE_FLG_BCASE | + GARGOYLE_FLG_ECASE | + GARGOYLE_FLG_ICASE | + GARGOYLE_FLG_SYMBL; + #endif diff --git a/include/gargoyle/privledge.h b/include/gargoyle/privledge.h new file mode 100644 index 0000000..559bae7 --- /dev/null +++ b/include/gargoyle/privledge.h @@ -0,0 +1,17 @@ +#ifndef __GARGOYLE_PRIVLEDGE_H_ +#define __GARGOYLE_PRIVLEDGE_H_ + +struct gargoyle_opt { + const char *brand; + uint16_t brand_sz; + const char emblem; + void *val; + uint16_t val_sz; + uint8_t type; +}; + +#define GARGOYLE_MK_OPT(brand) brand, (sizeof(brand) - 1) +#define GARGOYLE_EZ_OPT(brand, val) GARGOYLE_MK_OPT(brand), 0[brand], &val, sizeof(val) +#define GARGOYLE_CS_OPT(brand, val) GARGOYLE_MK_OPT(brand), 0[brand], &val[0], sizeof(val) + +#endif diff --git a/include/gargoyle/scribe.h b/include/gargoyle/scribe.h index daa6c90..f3e5fab 100644 --- a/include/gargoyle/scribe.h +++ b/include/gargoyle/scribe.h @@ -4,7 +4,7 @@ #include #include -uint8_t gargoyle_from_bool(struct gargoyle_opt *opt, const char *brand); -uint8_t gargoyle_from_rope(struct gargoyle_opt *opt, const char *brand_val); +uint8_t gargoyle_from_bool(struct gargoyle_opt *opt, const char *brand, gargoyle_flag_type flags); +uint8_t gargoyle_from_rope(struct gargoyle_opt *opt, const char *brand_val, gargoyle_flag_type flags); #endif diff --git a/include/gargoyle/sleuth.h b/include/gargoyle/sleuth.h index 684637e..d2bf636 100644 --- a/include/gargoyle/sleuth.h +++ b/include/gargoyle/sleuth.h @@ -1,22 +1,11 @@ #ifndef __GARGOYLE_SLEUTH_H_ #define __GARGOYLE_SLEUTH_H_ +#include +#include #include -struct gargoyle_opt { - const char *brand; - uint16_t brand_sz; - const char emblem; - void *val; - uint16_t val_sz; - uint8_t type; -}; - -#define GARGOYLE_MK_OPT(brand) brand, (sizeof(brand) - 1) -#define GARGOYLE_EZ_OPT(brand, val) GARGOYLE_MK_OPT(brand), 0[brand], &val, sizeof(val) -#define GARGOYLE_CS_OPT(brand, val) GARGOYLE_MK_OPT(brand), 0[brand], &val[0], sizeof(val) - -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); +struct gargoyle_opt *gargoyle_find_brand(uint16_t optc, struct gargoyle_opt *optv, const char *brand, gargoyle_flag_type flags); +struct gargoyle_opt *gargoyle_find_emblem(uint16_t optc, struct gargoyle_opt *optv, const char emblem, gargoyle_flag_type flags); #endif diff --git a/include/gargoyle/twine.h b/include/gargoyle/twine.h index dc814cb..fcd5976 100644 --- a/include/gargoyle/twine.h +++ b/include/gargoyle/twine.h @@ -4,7 +4,8 @@ #include uint8_t is_sep(char tok); -uint8_t gargoyle_cmp(const char *s1, const char *s2, uint16_t len, uint8_t flags); -char *gargoyle_cpy(char *dst, const char *src, uint16_t len); +uint8_t is_eql(char lhs, char rhs); +uint8_t gargoyle_cmp(const char *s1, const char *s2, uint16_t len, gargoyle_flag_type flags); +char *gargoyle_cpy(char *dst, const char *src, uint16_t len, gargoyle_flag_type flags); #endif diff --git a/src/gargoyle.c b/src/gargoyle.c index 2e5e26f..36e8839 100644 --- a/src/gargoyle.c +++ b/src/gargoyle.c @@ -1,12 +1,18 @@ #include +#include +#include #include -uint8_t gargoyle_digest_args(uint16_t optc, struct gargoyle_opt *optv, int *argc, char ***argv, uint8_t flags) { - if(!(flags & GARGOYLE_DIGEST_ALL)) { +uint8_t gargoyle_digest_args(uint16_t optc, struct gargoyle_opt *optv, int *argc, char ***argv, gargoyle_flag_type flags) { + if(!(flags & GARGOYLE_FLG_GREED)) { *argc -= 1; *argv += 1; } + if(flags & GARGOYLE_FLG_BCASE) { + flags |= GARGOYLE_FLG_ICASE; + } + while(1) { const char *arg = **argv; @@ -22,7 +28,7 @@ uint8_t gargoyle_digest_args(uint16_t optc, struct gargoyle_opt *optv, int *argc if(*(arg + 1) == '-') { const char *brand = arg + 2; - opt = gargoyle_find_brand(optc, optv, brand); + opt = gargoyle_find_brand(optc, optv, brand, flags); if(!opt) { return GARGOYLE_ERR_UNKNOWN_OPT; @@ -31,9 +37,9 @@ uint8_t gargoyle_digest_args(uint16_t optc, struct gargoyle_opt *optv, int *argc uint8_t res = 0; if(opt->type & GARGOYLE_TYPE_BOOL) { - res = gargoyle_from_bool(opt, brand); + res = gargoyle_from_bool(opt, brand, flags); } else if(*(brand + opt->brand_sz) == '=') { - res = gargoyle_from_rope(opt, brand + opt->brand_sz + 1); + res = gargoyle_from_rope(opt, brand + opt->brand_sz + 1, flags); } else { if(!*(*argv + 1)) { return GARGOYLE_ERR_VALUE_REQUIRED; @@ -41,7 +47,7 @@ uint8_t gargoyle_digest_args(uint16_t optc, struct gargoyle_opt *optv, int *argc *argc -= 1; *argv += 1; - res = gargoyle_from_rope(opt, **argv); + res = gargoyle_from_rope(opt, **argv, flags); } if(res) { @@ -51,7 +57,7 @@ uint8_t gargoyle_digest_args(uint16_t optc, struct gargoyle_opt *optv, int *argc const char *idx = arg + 1; for(; *idx; idx += 1) { - opt = gargoyle_find_emblem(optc, optv, *idx); + opt = gargoyle_find_emblem(optc, optv, *idx, flags); if(!opt) { return GARGOYLE_ERR_UNKNOWN_OPT; @@ -65,3 +71,7 @@ uint8_t gargoyle_digest_args(uint16_t optc, struct gargoyle_opt *optv, int *argc return GARGOYLE_ERR_SUCCESS; } + +uint8_t gargoyle_digest_envh(/* ... */) { + return 0; +} diff --git a/src/scribe.c b/src/scribe.c index 84400a9..4dfbeb4 100644 --- a/src/scribe.c +++ b/src/scribe.c @@ -3,16 +3,16 @@ #include #include -uint8_t gargoyle_from_bool(struct gargoyle_opt *opt, const char *brand) { +uint8_t gargoyle_from_bool(struct gargoyle_opt *opt, const char *brand, gargoyle_flag_type flags) { if(opt->val) { uint8_t *val = opt->val; - *val = !gargoyle_cmp(brand, "no-", 3, GARGOYLE_CMP_ICASE); + *val = !gargoyle_cmp(brand, "no-", 3, flags); } return 0; } -uint8_t gargoyle_from_rope(struct gargoyle_opt *opt, const char *brand_val) { +uint8_t gargoyle_from_rope(struct gargoyle_opt *opt, const char *brand_val, gargoyle_flag_type flags) { if(opt->type & GARGOYLE_TYPE_UINT) { char *end = NULL; uint64_t *val = opt->val; @@ -31,8 +31,7 @@ uint8_t gargoyle_from_rope(struct gargoyle_opt *opt, const char *brand_val) { } } else if(opt->type & GARGOYLE_TYPE_ROPE) { char *val = opt->val; - char *end = gargoyle_cpy(val, brand_val, opt->val_sz - 1); - *end = '\0'; + gargoyle_cpy(val, brand_val, opt->val_sz, flags | GARGOYLE_FLG_FILL0); } else { return GARGOYLE_ERR_UNKNOWN_TYPE; } diff --git a/src/sleuth.c b/src/sleuth.c index d4d6eff..717352c 100644 --- a/src/sleuth.c +++ b/src/sleuth.c @@ -1,20 +1,25 @@ +#include #include #include #include #include -struct gargoyle_opt *gargoyle_find_brand(uint16_t optc, struct gargoyle_opt *optv, const char *brand) { +struct gargoyle_opt *gargoyle_find_brand(uint16_t optc, struct gargoyle_opt *optv, const char *brand, gargoyle_flag_type flags) { + if(flags & GARGOYLE_FLG_BCASE) { + flags |= GARGOYLE_FLG_ICASE; + } + for(; optc; optc -= 1, optv += 1) { const char *idx = brand; - if(optv->type & GARGOYLE_TYPE_BOOL && gargoyle_cmp(idx, "no-", 3, GARGOYLE_CMP_ICASE)) { + if(optv->type & GARGOYLE_TYPE_BOOL && gargoyle_cmp(idx, "no-", 3, flags)) { idx += 3; } uint8_t end = !*(idx + optv->brand_sz); uint8_t eql = *(idx + optv->brand_sz) == '='; - if(gargoyle_cmp(idx, optv->brand, optv->brand_sz, GARGOYLE_CMP_ICASE) && (end || eql)) { + if(gargoyle_cmp(idx, optv->brand, optv->brand_sz, flags) && (end || eql)) { return optv; } } @@ -22,10 +27,12 @@ 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) { +struct gargoyle_opt *gargoyle_find_emblem(uint16_t optc, struct gargoyle_opt *optv, const char emblem, gargoyle_flag_type flags) { for(; optc; optc -= 1, optv += 1) { if(emblem == optv->emblem) { return optv; + } else if(flags & GARGOYLE_FLG_ECASE && is_eql(emblem, optv->emblem)) { + return optv; } } diff --git a/src/twine.c b/src/twine.c index a239f39..45923e7 100644 --- a/src/twine.c +++ b/src/twine.c @@ -1,15 +1,20 @@ #include +#include #include uint8_t is_sep(char tok) { return (tok == '_' || tok == '-'); } -uint8_t gargoyle_cmp(const char *s1, const char *s2, uint16_t len, uint8_t flags) { +uint8_t is_eql(char lhs, char rhs) { + return lhs == rhs; +} + +uint8_t gargoyle_cmp(const char *s1, const char *s2, uint16_t len, gargoyle_flag_type flags) { while(*s1 && *s2 && len) { - if(flags & GARGOYLE_CMP_ICASE && isalpha(*s1) && tolower(*s1) == tolower(*s2)) { + if(flags & GARGOYLE_FLG_ICASE && isalpha(*s1) && is_eql(*s1, *s2)) { s1 += 1, s2 += 1; - } else if(flags & GARGOYLE_CMP_SYMBL && is_sep(*s1) && is_sep(*s2)) { + } else if(flags & GARGOYLE_FLG_SYMBL && is_sep(*s1) && is_sep(*s2)) { s1 += 1, s2 += 1; } else if(*s1 == *s2) { s1 += 1, s2 += 1; @@ -23,10 +28,13 @@ uint8_t gargoyle_cmp(const char *s1, const char *s2, uint16_t len, uint8_t flags return 1; } -char *gargoyle_cpy(char *dst, const char *src, uint16_t len) { - uint16_t idx = 1; +char *gargoyle_cpy(char *dst, const char *src, uint16_t len, gargoyle_flag_type flags) { + if(flags & GARGOYLE_FLG_FILL0) { + *(dst + len - 1) = '\0'; + len -= 1; + } - for(; len; dst += 1, len -= 1, idx += 1) { + for(; len; dst += 1, len -= 1) { if(*src) { *dst = *src; src += 1;