Author | Jamozed <[email protected]> |
Date | 2020-08-28 11:41:53 |
Commit | 40437ba6c47de4c4528d39a3efff523ff9ac0142 |
Parent | 307b54317d79431c36c8d79501b88d8932e2cf5d |
lib: Update optget to version 1.6.0
Diffstat
M | CMakeLists.txt | | | 2 | -- |
R | ext/optget.h -> src/lib/optget.c | | | 91 | +++++++++++++++++++------------------------------------------------------------ |
M | src/alder32.c | | | 18 | +++++++++--------- |
M | src/crc32.c | | | 18 | +++++++++--------- |
A | src/lib/optget.h | | | 55 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
5 files changed, 95 insertions, 89 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index ace588c..adbfbca 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,8 +8,6 @@ SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib) SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin) SET(CMAKE_STATIC_LIBRARY_PREFIX "") -INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/ext) - FILE(GLOB LIBSRC ${PROJECT_SOURCE_DIR}/src/lib/*) ADD_LIBRARY(lib STATIC ${LIBSRC}) diff --git a/src/alder32.c b/src/alder32.c index 0f9e468..3956c16 100644 --- a/src/alder32.c +++ b/src/alder32.c @@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. */ #include "lib/error.c" -#include "optget.h" +#include "lib/optget.h" #include <stdbool.h> #include <stdint.h> @@ -39,20 +39,20 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. #define VERSION "1.0.0" +static struct lop lops[] = { + { "help", ARG_NUL, 256 }, + { "version", ARG_NUL, 257 }, + { NULL, 0, 0 } +}; + static inline int alder32(const char *file); static void hlp(void); static void ver(void); int main(int ac, char *av[]) { A0 = av[0]; - lop_t lops[] = { - { "help", ARG_NUL, 256 }, - { "version", ARG_NUL, 257 }, - { NULL, 0, 0 } - }; - - opt_t opt = OPTGET_INIT; opt.str = ""; opt.lops = lops; int o; - while((o = optget(&opt, av, 1)) != -1) switch (o) { + struct opt opt = OPTGET_INIT; opt.str = ""; opt.lops = lops; + for (int o; (o = optget(&opt, av, 1)) != -1;) switch (o) { case 256: { hlp(); return 0; } case 257: { ver(); return 0; } default: { return 1; } diff --git a/src/crc32.c b/src/crc32.c index 426c064..9a145cb 100644 --- a/src/crc32.c +++ b/src/crc32.c @@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. */ #include "lib/error.c" -#include "optget.h" +#include "lib/optget.h" #include <stdbool.h> #include <stdint.h> @@ -39,6 +39,12 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. #define VERSION "1.0.6" +static struct lop lops[] = { + { "help", ARG_NUL, 256 }, + { "version", ARG_NUL, 257 }, + { NULL, 0, 0 } +}; + static inline int crc32(const char *file); static void hlp(void); @@ -47,14 +53,8 @@ static void ver(void); static const uint32_t CRC32[]; int main(int ac, char *av[]) { A0 = av[0]; - lop_t lops[] = { - { "help", ARG_NUL, 256 }, - { "version", ARG_NUL, 257 }, - { NULL, 0, 0 } - }; - - opt_t opt = OPTGET_INIT; opt.str = ""; opt.lops = lops; int o; - while((o = optget(&opt, av, 1)) != -1) switch (o) { + struct opt opt = OPTGET_INIT; opt.str = ""; opt.lops = lops; + for (int o; (o = optget(&opt, av, 1)) != -1;) switch (o) { case 256: { hlp(); return 0; } case 257: { ver(); return 0; } default: { return 1; } diff --git a/ext/optget.h b/src/lib/optget.c similarity index 65% rename from ext/optget.h rename to src/lib/optget.c index cc9ed90..da61ebd 100644 --- a/ext/optget.h +++ b/src/lib/optget.c @@ -1,14 +1,11 @@ -// optget.h, version 1.5.1 -// OMKOV optget library +// optget.h, version 1.6.0 +// optget source file for OMKOV lib // Copyright (C) 2020, Jakob Wakeling // All rights reserved. /* OMKOV Permissive Licence, version 1.0 -Copyright (C) 2020, Jakob Wakeling -All rights reserved. - Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal with the Software without restriction, including without limitation the rights to @@ -33,41 +30,20 @@ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. */ -#ifndef OMKOV_OPTGET_H_W3LIZK1S -#define OMKOV_OPTGET_H_W3LIZK1S - -#ifdef __cplusplus -extern "C" { -#endif // __cplusplus +#include "error.h" +#include "optget.h" +#include <stddef.h> #include <stdio.h> #include <string.h> -#define ARG_NUL 0 -#define ARG_REQ 1 -#define ARG_OPT 2 - -typedef struct { - char *str; - int arg, val; -} lop_t; - -typedef struct { - int ind, opt, pos, nop; - char *arg, *lop, *str; - lop_t *lops; -} opt_t; +#define cur av[opt->ind] -static const opt_t OPTGET_INIT = { 1, 0, 1, 0, NULL, NULL, NULL, NULL }; +const struct opt OPTGET_INIT = { 1, 0, 1, 0, NULL, NULL, NULL, NULL }; -static inline void _permute(char *argv[], int ind, int n); +static inline void permute(char **av, int i, int n); -#define cur argv[opt->ind] - -/* - Parse the next command line argument -*/ -static inline int optget(opt_t *opt, char *argv[], int flags) { +int optget(struct opt *opt, char *av[], int flags) { if (flags & 1) { for (; cur && (cur[0] != '-' || cur[1] == 0); ++opt->ind, ++opt->nop); if (!cur) { opt->ind -= opt->nop; opt->nop = 0; return -1; } @@ -78,27 +54,26 @@ static inline int optget(opt_t *opt, char *argv[], int flags) { if (cur[1] == '-') { if (cur[2] == 0) { if (opt->nop) { - _permute(argv, opt->ind++, opt->nop); + permute(av, opt->ind++, opt->nop); opt->ind -= opt->nop; opt->nop = 0; } else { ++opt->ind; } return -1; } int optend, lop; optret = '?'; opt->opt = 0; opt->lop = cur; if (!opt->lops) { goto nol; } - for (optend = 2; cur[optend] != '=' && cur[optend] != 0; ++optend) {} + for (optend = 2; cur[optend] != '=' && cur[optend] != 0; ++optend); for (lop = 0; opt->lops[lop].str; ++lop) { if (strncmp(&cur[2], opt->lops[lop].str, (size_t)optend - 2) == 0) { if (!opt->lops[lop].str[optend - 2]) { - optret = opt->opt = opt->lops[lop].val; - break; + optret = opt->opt = opt->lops[lop].val; break; } } } if (opt->lops[lop].arg > ARG_NUL) { if (cur[optend]) { opt->arg = &cur[optend + 1]; } - else if (argv[opt->ind + 1]) { opt->arg = argv[++opt->ind]; } + else if (av[opt->ind + 1]) { opt->arg = av[++opt->ind]; } else { if (opt->lops[lop].arg == ARG_REQ) { optret = ':'; } opt->arg = NULL; @@ -115,7 +90,7 @@ nol: opt->pos = 0; if (!optchr) { optret = '?'; } else if (optchr[1] == ':') { if (cur[opt->pos]) { opt->arg = &cur[opt->pos]; } - else if (argv[opt->ind + 1]) { opt->arg = argv[++opt->ind]; } + else if (av[opt->ind + 1]) { opt->arg = av[++opt->ind]; } else { opt->arg = NULL; optret = ':'; } opt->pos = 0; } @@ -125,45 +100,23 @@ nol: opt->pos = 0; if (!opt->pos || !cur[opt->pos]) { ++opt->ind; opt->pos = 1; if (opt->nop) for (; optind < opt->ind; ++optind) { - _permute(argv, optind, opt->nop); + permute(av, optind, opt->nop); } } if (optret == '?' && opt->str[0] != ':') { - if (opt->opt) { - fprintf(stderr, "%s: invalid option -- '%c'\n", argv[0], opt->opt); - } - else if (opt->lop) { - fprintf(stderr, "%s: invalid option '%s'\n", argv[0], opt->lop); - } + if (opt->opt) { warn("%c: invalid option", opt->opt); } + else if (opt->lop) { warn("%s: invalid option", opt->lop); } } if (optret == ':' && opt->str[0] != ':') { - if (opt->opt) { - fprintf(stderr, "%s: option requires argument -- '%c'\n", argv[0], - opt->opt); - } - else if (opt->lop) { - fprintf(stderr, "%s: option requires argument '%s'\n", argv[0], - opt->lop); - } + if (opt->opt) { warn("%c: option requires argument", opt->opt); } + else if (opt->lop) { warn("%s: option requires argument", opt->lop); } } return optret; } -/* - Permute arguments -*/ -static inline void _permute(char *argv[], int ind, int n) { - char *arg = argv[ind]; - for (int i = ind; i > ind - n; --i) { argv[i] = argv[i - 1]; } - argv[ind - n] = arg; - return; -} - -#undef cur // argv[opt->ind] - -#ifdef __cplusplus +static inline void permute(char **av, int i, int n) { + char *a = av[i]; memmove(&av[i - n + 1], &av[i - n], n * sizeof (av)); + av[i - n] = a; return; } -#endif // __cplusplus -#endif // OMKOV_OPTGET_H_W3LIZK1S diff --git a/src/lib/optget.h b/src/lib/optget.h new file mode 100644 index 0000000..f83d132 --- /dev/null +++ b/src/lib/optget.h @@ -0,0 +1,55 @@ +// optget.h, version 1.6.0 +// optget header file for OMKOV lib +// Copyright (C) 2020, Jakob Wakeling +// All rights reserved. + +/* +OMKOV Permissive Licence, version 1.0 + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal with +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimers. +* Redistributions in binary form must reproduce the above copyright notice, this + list of conditions and the following disclaimers in the documentation and/or + other materials provided with the distribution. +* Neither the names of the copyright holders, nor the names of its contributors + may be used to endorse or promote products derived from this Software without + specific prior written permission. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT +HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. +*/ + +#ifndef OMKOV_LIB_OPTGET_H_W3LIZK1S +#define OMKOV_LIB_OPTGET_H_W3LIZK1S + +#define ARG_NUL 0 +#define ARG_REQ 1 +#define ARG_OPT 2 + +struct lop { + char *str; + int arg, val; +}; + +struct opt { + int ind, opt, pos, nop; + char *arg, *lop, *str; + struct lop *lops; +}; + +extern const struct opt OPTGET_INIT; + +extern int optget(struct opt *opt, char *av[], int flags); + +#endif // OMKOV_LIB_OPTGET_H_W3LIZK1S