aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2024-10-03 23:11:23 +0200
committerThomas Voss <mail@thomasvoss.com> 2024-10-03 23:11:23 +0200
commit4fcb1ec318d74a454f6cfbefe018f35bbb32d6bf (patch)
treeacadeb955c33c6d6e7d37e19754e10e9cf60ffa1
parent2fdc8dba3690419f2b0dd12b3d6d1c391cc54ba2 (diff)
Typedef enums and structures
-rw-r--r--include/cli.h21
-rw-r--r--lib/cli/optparse.c16
2 files changed, 18 insertions, 19 deletions
diff --git a/include/cli.h b/include/cli.h
index 970b228..8efa2d0 100644
--- a/include/cli.h
+++ b/include/cli.h
@@ -7,7 +7,7 @@
#include "_rune.h"
#include "_uNview.h"
-struct optparser {
+typedef struct {
bool _b;
int _subopt;
char **_argv;
@@ -15,28 +15,27 @@ struct optparser {
int optind;
char errmsg[128];
u8view_t optarg;
-};
+} optparser_t;
-enum cliarg {
+typedef enum {
CLI_NONE,
CLI_OPT,
CLI_REQ,
-};
+} cli_opt_kind_t;
-struct cli_option {
+typedef struct {
rune shortopt;
u8view_t longopt;
- enum cliarg argtype;
-};
+ cli_opt_kind_t argtype;
+} cli_opt_t;
-[[nodiscard]] rune optparse(struct optparser *, const struct cli_option *,
- size_t);
+[[nodiscard]] rune optparse(optparser_t *, const cli_opt_t *, size_t);
[[_mlib_inline]]
-static inline struct optparser
+static inline optparser_t
mkoptparser(char **argv)
{
- return (struct optparser){
+ return (optparser_t){
._argv = argv,
.optind = argv[0] != nullptr,
};
diff --git a/lib/cli/optparse.c b/lib/cli/optparse.c
index 8178b85..6544b06 100644
--- a/lib/cli/optparse.c
+++ b/lib/cli/optparse.c
@@ -16,12 +16,12 @@
#define error(st, msg, x) \
_Generic((x), u8view_t: error_s, rune: error_r)((st), (msg), (x))
-static rune error_r(struct optparser *, const char *, rune);
-static rune error_s(struct optparser *, const char *, u8view_t);
-static rune shortopt(struct optparser *, const struct cli_option *, size_t);
+static rune error_r(optparser_t *, const char *, rune);
+static rune error_s(optparser_t *, const char *, u8view_t);
+static rune shortopt(optparser_t *, const cli_opt_t *, size_t);
rune
-optparse(struct optparser *st, const struct cli_option *opts, size_t nopts)
+optparse(optparser_t *st, const cli_opt_t *opts, size_t nopts)
{
st->errmsg[0] = '\0';
st->optarg = (u8view_t){};
@@ -46,7 +46,7 @@ optparse(struct optparser *st, const struct cli_option *opts, size_t nopts)
/* Skip ‘--’ */
VSHFT(&opt, 2);
- const struct cli_option *o = nullptr;
+ const cli_opt_t *o = nullptr;
const char8_t *eq_p = u8chr(opt, '=');
u8view_t opt_no_eq = {
.p = opt.p,
@@ -101,7 +101,7 @@ optparse(struct optparser *st, const struct cli_option *opts, size_t nopts)
}
rune
-shortopt(struct optparser *st, const struct cli_option *opts, size_t nopts)
+shortopt(optparser_t *st, const cli_opt_t *opts, size_t nopts)
{
rune ch;
const char8_t *opt = st->_argv[st->optind];
@@ -142,7 +142,7 @@ out:
}
rune
-error_s(struct optparser *st, const char *msg, u8view_t s)
+error_s(optparser_t *st, const char *msg, u8view_t s)
{
snprintf(st->errmsg, sizeof(st->errmsg), u8"%s — ‘%.*s’", msg,
SV_PRI_ARGS(s));
@@ -150,7 +150,7 @@ error_s(struct optparser *st, const char *msg, u8view_t s)
}
rune
-error_r(struct optparser *st, const char *msg, rune ch)
+error_r(optparser_t *st, const char *msg, rune ch)
{
char buf[U8_LEN_MAX + 1] = {};
snprintf(st->errmsg, sizeof(st->errmsg), u8"%s — ‘%.*s’", msg,