Author | Jakob Wakeling <[email protected]> |
Date | 2020-06-30 12:12:59 |
Commit | f193bb40eef110b3908a1ff67ae5000fe30c96ef |
Parent | 799eb292b27da8dbb51a3b73cc9c86f5f7ddcbde |
Add external libraries
Diffstat
A | ext/error.h | | | 48 | ++++++++++++++++++++++++++++++++++++++++++++++++ |
A | ext/optget.h | | | 169 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 217 insertions, 0 deletions
diff --git a/ext/error.h b/ext/error.h new file mode 100644 index 0000000..91c0796 --- /dev/null +++ b/ext/error.h @@ -0,0 +1,48 @@ +// error.h, version 1.0.0 +// OMKOV error library +// Copyright (C) 2020, Jakob Wakeling +// All rights reserved. + +/* +OMKOV Public Domain Licence, version 1.0 + +Permission is hereby granted to deal with this software and its associated +documentation files without restriction. + +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_ERROR_H_38W06M3W +#define OMKOV_ERROR_H_38W06M3W + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> + +#define serrno strerror(errno) + +static void error(int status, const char *format, ...) { + va_list ap; va_start(ap, format); fflush(stdout); + vfprintf(stderr, format, ap); fputc('\n', stderr); + va_end(ap); exit(status); +} + +static void warn(const char *format, ...) { + va_list ap; va_start(ap, format); fflush(stdout); + vfprintf(stderr, format, ap); fputc('\n', stderr); + va_end(ap); return; +} + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // OMKOV_ERROR_H_38W06M3W diff --git a/ext/optget.h b/ext/optget.h new file mode 100644 index 0000000..cc9ed90 --- /dev/null +++ b/ext/optget.h @@ -0,0 +1,169 @@ +// optget.h, version 1.5.1 +// OMKOV optget library +// 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 +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_OPTGET_H_W3LIZK1S +#define OMKOV_OPTGET_H_W3LIZK1S + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +#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; + +static const opt_t OPTGET_INIT = { 1, 0, 1, 0, NULL, NULL, NULL, NULL }; + +static inline void _permute(char *argv[], int ind, int n); + +#define cur argv[opt->ind] + +/* + Parse the next command line argument +*/ +static inline int optget(opt_t *opt, char *argv[], 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; } + } + else if (!cur || (cur[0] != '-' || cur[1] == 0)) { return -1; } + + int optind = opt->ind, optret; + + if (cur[1] == '-') { + if (cur[2] == 0) { if (opt->nop) { + _permute(argv, 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 (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; + } + } + } + + 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 (opt->lops[lop].arg == ARG_REQ) { optret = ':'; } + opt->arg = NULL; + } + } + else { opt->arg = NULL; } + +nol: opt->pos = 0; + } + else { + optret = opt->opt = cur[opt->pos++]; opt->lop = NULL; + const char *optchr = strchr(opt->str, opt->opt); + + 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 { opt->arg = NULL; optret = ':'; } + opt->pos = 0; + } + else { opt->arg = NULL; } + } + + if (!opt->pos || !cur[opt->pos]) { + ++opt->ind; opt->pos = 1; + if (opt->nop) for (; optind < opt->ind; ++optind) { + _permute(argv, 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 (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); + } + } + + 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 +} +#endif // __cplusplus +#endif // OMKOV_OPTGET_H_W3LIZK1S