aboutsummaryrefslogtreecommitdiff
path: root/src/opt.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/opt.c')
-rw-r--r--src/opt.c151
1 files changed, 93 insertions, 58 deletions
diff --git a/src/opt.c b/src/opt.c
index ddc78362..dada7442 100644
--- a/src/opt.c
+++ b/src/opt.c
@@ -3,7 +3,7 @@
*
* SPDX-License-Identifier: BSD-2-Clause
*
- * Copyright (c) 2018-2021 Gavin D. Howard and contributors.
+ * Copyright (c) 2018-2023 Gavin D. Howard and contributors.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -53,7 +53,9 @@
* @param i The index to test.
* @return True if @a i is the last index, false otherwise.
*/
-static inline bool bc_opt_longoptsEnd(const BcOptLong *longopts, size_t i) {
+static inline bool
+bc_opt_longoptsEnd(const BcOptLong* longopts, size_t i)
+{
return !longopts[i].name && !longopts[i].val;
}
@@ -63,17 +65,21 @@ static inline bool bc_opt_longoptsEnd(const BcOptLong *longopts, size_t i) {
* @param c The character to match against.
* @return The name of the long option that matches @a c, or "NULL".
*/
-static const char* bc_opt_longopt(const BcOptLong *longopts, int c) {
-
+static const char*
+bc_opt_longopt(const BcOptLong* longopts, int c)
+{
size_t i;
- for (i = 0; !bc_opt_longoptsEnd(longopts, i); ++i) {
+ for (i = 0; !bc_opt_longoptsEnd(longopts, i); ++i)
+ {
if (longopts[i].val == c) return longopts[i].name;
}
BC_UNREACHABLE
+#if !BC_CLANG
return "NULL";
+#endif // !BC_CLANG
}
/**
@@ -85,12 +91,13 @@ static const char* bc_opt_longopt(const BcOptLong *longopts, int c) {
* @param use_short True if the short option should be used for error printing,
* false otherwise.
*/
-static void bc_opt_error(BcErr err, int c, const char *str, bool use_short) {
-
- if (err == BC_ERR_FATAL_OPTION) {
-
- if (use_short) {
-
+static void
+bc_opt_error(BcErr err, int c, const char* str, bool use_short)
+{
+ if (err == BC_ERR_FATAL_OPTION)
+ {
+ if (use_short)
+ {
char short_str[2];
short_str[0] = (char) c;
@@ -109,13 +116,17 @@ static void bc_opt_error(BcErr err, int c, const char *str, bool use_short) {
* @param c The character to match against.
* @return The type of the long option as an integer, or -1 if none.
*/
-static int bc_opt_type(const BcOptLong *longopts, char c) {
-
+static int
+bc_opt_type(const BcOptLong* longopts, char c)
+{
size_t i;
if (c == ':') return -1;
- for (i = 0; !bc_opt_longoptsEnd(longopts, i) && longopts[i].val != c; ++i);
+ for (i = 0; !bc_opt_longoptsEnd(longopts, i) && longopts[i].val != c; ++i)
+ {
+ continue;
+ }
if (bc_opt_longoptsEnd(longopts, i)) return -1;
@@ -128,11 +139,12 @@ static int bc_opt_type(const BcOptLong *longopts, char c) {
* @param longopts The long options array.
* @return The character for the short option, or -1 if none left.
*/
-static int bc_opt_parseShort(BcOpt *o, const BcOptLong *longopts) {
-
+static int
+bc_opt_parseShort(BcOpt* o, const BcOptLong* longopts)
+{
int type;
- char *next;
- char *option = o->argv[o->optind];
+ char* next;
+ char* option = o->argv[o->optind];
int ret = -1;
// Make sure to clear these.
@@ -147,8 +159,8 @@ static int bc_opt_parseShort(BcOpt *o, const BcOptLong *longopts) {
type = bc_opt_type(longopts, option[0]);
next = o->argv[o->optind + 1];
- switch (type) {
-
+ switch (type)
+ {
case -1:
case BC_OPT_BC_ONLY:
case BC_OPT_DC_ONLY:
@@ -157,23 +169,24 @@ static int bc_opt_parseShort(BcOpt *o, const BcOptLong *longopts) {
if (type == -1 || (type == BC_OPT_BC_ONLY && BC_IS_DC) ||
(type == BC_OPT_DC_ONLY && BC_IS_BC))
{
- char str[2] = {0, 0};
+ char str[2] = { 0, 0 };
str[0] = option[0];
o->optind += 1;
bc_opt_error(BC_ERR_FATAL_OPTION, option[0], str, true);
}
+
+ // Fallthrough.
+ BC_FALLTHROUGH
}
- // Fallthrough.
- BC_FALLTHROUGH
case BC_OPT_NONE:
{
// If there is something else, update the suboption.
if (option[1]) o->subopt += 1;
- else {
-
+ else
+ {
// Go to the next argument.
o->subopt = 0;
o->optind += 1;
@@ -186,12 +199,17 @@ static int bc_opt_parseShort(BcOpt *o, const BcOptLong *longopts) {
case BC_OPT_REQUIRED_BC_ONLY:
{
+#if DC_ENABLED
if (BC_IS_DC)
+ {
bc_opt_error(BC_ERR_FATAL_OPTION, option[0],
bc_opt_longopt(longopts, option[0]), true);
+ }
+#endif // DC_ENABLED
+
+ // Fallthrough
+ BC_FALLTHROUGH
}
- // Fallthrough
- BC_FALLTHROUGH
case BC_OPT_REQUIRED:
{
@@ -201,16 +219,18 @@ static int bc_opt_parseShort(BcOpt *o, const BcOptLong *longopts) {
// Use the next characters, if they exist.
if (option[1]) o->optarg = option + 1;
- else if (next != NULL) {
-
+ else if (next != NULL)
+ {
// USe the next.
o->optarg = next;
o->optind += 1;
}
// No argument, barf.
- else bc_opt_error(BC_ERR_FATAL_OPTION_NO_ARG, option[0],
- bc_opt_longopt(longopts, option[0]), true);
-
+ else
+ {
+ bc_opt_error(BC_ERR_FATAL_OPTION_NO_ARG, option[0],
+ bc_opt_longopt(longopts, option[0]), true);
+ }
ret = (int) option[0];
@@ -228,15 +248,18 @@ static int bc_opt_parseShort(BcOpt *o, const BcOptLong *longopts) {
* @param option The command-line argument.
* @return True if @a option matches @a name, false otherwise.
*/
-static bool bc_opt_longoptsMatch(const char *name, const char *option) {
-
- const char *a = option, *n = name;
+static bool
+bc_opt_longoptsMatch(const char* name, const char* option)
+{
+ const char* a = option;
+ const char* n = name;
// Can never match a NULL name.
if (name == NULL) return false;
- // Loop through
- for (; *a && *n && *a != '='; ++a, ++n) {
+ // Loop through.
+ for (; *a && *n && *a != '='; ++a, ++n)
+ {
if (*a != *n) return false;
}
@@ -250,35 +273,40 @@ static bool bc_opt_longoptsMatch(const char *name, const char *option) {
* @param option The option to find the argument of.
* @return A pointer to the argument of the option, or NULL if none.
*/
-static char* bc_opt_longoptsArg(char *option) {
-
+static char*
+bc_opt_longoptsArg(char* option)
+{
// Find the end or equals sign.
- for (; *option && *option != '='; ++option);
+ for (; *option && *option != '='; ++option)
+ {
+ continue;
+ }
if (*option == '=') return option + 1;
else return NULL;
}
-int bc_opt_parse(BcOpt *o, const BcOptLong *longopts) {
-
+int
+bc_opt_parse(BcOpt* o, const BcOptLong* longopts)
+{
size_t i;
- char *option;
+ char* option;
bool empty;
// This just eats empty options.
- do {
-
+ do
+ {
option = o->argv[o->optind];
if (option == NULL) return -1;
empty = !strcmp(option, "");
o->optind += empty;
-
- } while (empty);
+ }
+ while (empty);
// If the option is just a "--".
- if (BC_OPT_ISDASHDASH(option)) {
-
+ if (BC_OPT_ISDASHDASH(option))
+ {
// Consume "--".
o->optind += 1;
return -1;
@@ -297,14 +325,14 @@ int bc_opt_parse(BcOpt *o, const BcOptLong *longopts) {
o->optind += 1;
// Loop through the valid long options.
- for (i = 0; !bc_opt_longoptsEnd(longopts, i); i++) {
-
- const char *name = longopts[i].name;
+ for (i = 0; !bc_opt_longoptsEnd(longopts, i); i++)
+ {
+ const char* name = longopts[i].name;
// If we have a match...
- if (bc_opt_longoptsMatch(name, option)) {
-
- char *arg;
+ if (bc_opt_longoptsMatch(name, option))
+ {
+ char* arg;
// Get the option char and the argument.
o->optopt = longopts[i].val;
@@ -335,8 +363,11 @@ int bc_opt_parse(BcOpt *o, const BcOptLong *longopts) {
// All's good if it exists; otherwise, barf.
if (o->optarg != NULL) o->optind += 1;
- else bc_opt_error(BC_ERR_FATAL_OPTION_NO_ARG,
- o->optopt, name, false);
+ else
+ {
+ bc_opt_error(BC_ERR_FATAL_OPTION_NO_ARG, o->optopt, name,
+ false);
+ }
}
return o->optopt;
@@ -348,10 +379,14 @@ int bc_opt_parse(BcOpt *o, const BcOptLong *longopts) {
BC_UNREACHABLE
+#if !BC_CLANG
return -1;
+#endif // !BC_CLANG
}
-void bc_opt_init(BcOpt *o, char *argv[]) {
+void
+bc_opt_init(BcOpt* o, char* argv[])
+{
o->argv = argv;
o->optind = 1;
o->subopt = 0;