summaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorFurquan Shaikh <furquan@google.com>2020-04-14 00:14:44 -0700
committerPatrick Georgi <pgeorgi@google.com>2020-04-20 06:08:29 +0000
commitc42cf911ad787364aea359deb7c5a1c24f9bdd35 (patch)
tree7ab5e79394220424e61d4a7b619c2df3cf8360f8 /util
parent8e66b23b350e59601c8e9201c121f484856487bb (diff)
downloadcoreboot-c42cf911ad787364aea359deb7c5a1c24f9bdd35.tar.xz
util/cbfstool: Allow use of non-ASCII longopt
CB:29744 ("util/cbfstool: Add optional argument ibb") added support for non-ASCII characters for long_options. However, there is a check later on which errors out since this character is not one of the commands[i].optstring. This change adds a function valid_opt() which does the following things: 1. Checks if the returned optchar is among the list of optstring supported by the command. 2. Checks if the returned optchar is a valid non-ASCII option. Currently, we do not maintain a list of non-ASCII options supported by each command. So, this function returns true if the optchar returned by getopt_long falls within the allowed range. Signed-off-by: Furquan Shaikh <furquan@google.com> Change-Id: I27a4f9af9850e4c892573202904fa9e5fbb64df6 Reviewed-on: https://review.coreboot.org/c/coreboot/+/40375 Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: Julius Werner <jwerner@chromium.org> Reviewed-by: Aaron Durbin <adurbin@chromium.org>
Diffstat (limited to 'util')
-rw-r--r--util/cbfstool/cbfstool.c26
1 files changed, 22 insertions, 4 deletions
diff --git a/util/cbfstool/cbfstool.c b/util/cbfstool/cbfstool.c
index 65c5e08871..f15c65b6e7 100644
--- a/util/cbfstool/cbfstool.c
+++ b/util/cbfstool/cbfstool.c
@@ -1295,7 +1295,9 @@ static const struct command commands[] = {
enum {
/* begin after ASCII characters */
- LONGOPT_IBB = 256,
+ LONGOPT_START = 256,
+ LONGOPT_IBB = LONGOPT_START,
+ LONGOPT_END,
};
static struct option long_options[] = {
@@ -1491,6 +1493,23 @@ static void usage(char *name)
);
}
+static bool valid_opt(size_t i, int c)
+{
+ /* Check if it is one of the optstrings supported by the command. */
+ if (strchr(commands[i].optstring, c))
+ return true;
+
+ /*
+ * Check if it is one of the non-ASCII characters. Currently, the
+ * non-ASCII characters are only checked against the valid list
+ * irrespective of the command.
+ */
+ if (c >= LONGOPT_START && c < LONGOPT_END)
+ return true;
+
+ return false;
+}
+
int main(int argc, char **argv)
{
size_t i;
@@ -1525,9 +1544,8 @@ int main(int argc, char **argv)
}
/* Filter out illegal long options */
- if (strchr(commands[i].optstring, c) == NULL) {
- /* TODO maybe print actual long option instead */
- ERROR("%s: invalid option -- '%c'\n",
+ if (!valid_opt(i, c)) {
+ ERROR("%s: invalid option -- '%d'\n",
argv[0], c);
c = '?';
}