summaryrefslogtreecommitdiffstats
path: root/tools/lib
diff options
context:
space:
mode:
authorAllen Byrne <50328838+byrnHDF@users.noreply.github.com>2022-02-21 16:37:04 (GMT)
committerGitHub <noreply@github.com>2022-02-21 16:37:04 (GMT)
commitc7d800e5e526e2dcc2fd19cd0c75d2ef92f184bc (patch)
treeaa6df876c2d6da6f35f531fd025589e60f9e2b8d /tools/lib
parent1c9f219463e236487a27cee6cb839379d670cda4 (diff)
downloadhdf5-c7d800e5e526e2dcc2fd19cd0c75d2ef92f184bc.zip
hdf5-c7d800e5e526e2dcc2fd19cd0c75d2ef92f184bc.tar.gz
hdf5-c7d800e5e526e2dcc2fd19cd0c75d2ef92f184bc.tar.bz2
1.12 Fix tools incompatibility (#1448)
* fix tools incompatibility with get_option #1443 * Fix get_option function * Committing clang-format changes Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
Diffstat (limited to 'tools/lib')
-rw-r--r--tools/lib/h5tools_utils.c165
-rw-r--r--tools/lib/h5tools_utils.h47
2 files changed, 212 insertions, 0 deletions
diff --git a/tools/lib/h5tools_utils.c b/tools/lib/h5tools_utils.c
index ea9812b..d389bba 100644
--- a/tools/lib/h5tools_utils.c
+++ b/tools/lib/h5tools_utils.c
@@ -29,6 +29,10 @@
unsigned h5tools_nCols = 80;
static int h5tools_d_status = 0;
static const char *h5tools_progname = "h5tools";
+/* ``get_option'' variables */
+int opt_err = 1; /*get_option prints errors if this is on */
+int opt_ind = 1; /*token pointer */
+const char *opt_arg; /*flag argument (or value) */
/*
* The output functions need a temporary buffer to hold a piece of the
@@ -158,6 +162,167 @@ help_ref_msg(FILE *output)
HDfprintf(output, "see the <%s> entry in the 'HDF5 Reference Manual'.\n", h5tools_getprogname());
}
+/*-------------------------------------------------------------------------
+ * Function: get_option
+ *
+ * Purpose: Determine the command-line options a user specified. We can
+ * accept both short and long type command-lines.
+ *
+ * Return: Success: The short valued "name" of the command line
+ * parameter or EOF if there are no more
+ * parameters to process.
+ *
+ * Failure: A question mark.
+ *-------------------------------------------------------------------------
+ */
+int
+get_option(int argc, const char *const *argv, const char *opts, const struct long_options *l_opts)
+{
+ static int sp = 1; /* character index in current token */
+ int optchar = '?'; /* option character passed back to user */
+
+ if (sp == 1) {
+ /* check for more flag-like tokens */
+ if (opt_ind >= argc || argv[opt_ind][0] != '-' || argv[opt_ind][1] == '\0') {
+ return EOF;
+ }
+ else if (HDstrcmp(argv[opt_ind], "--") == 0) {
+ opt_ind++;
+ return EOF;
+ }
+ }
+
+ if (sp == 1 && argv[opt_ind][0] == '-' && argv[opt_ind][1] == '-') {
+ /* long command line option */
+ int i;
+ const char ch = '=';
+ char * arg = HDstrdup(&argv[opt_ind][2]);
+ size_t arg_len = 0;
+
+ opt_arg = strchr(&argv[opt_ind][2], ch);
+ arg_len = HDstrlen(&argv[opt_ind][2]);
+ if (opt_arg) {
+ arg_len -= HDstrlen(opt_arg);
+ opt_arg++; /* skip the equal sign */
+ }
+ arg[arg_len] = 0;
+
+ for (i = 0; l_opts && l_opts[i].name; i++) {
+ if (HDstrcmp(arg, l_opts[i].name) == 0) {
+ /* we've found a matching long command line flag */
+ optchar = l_opts[i].shortval;
+
+ if (l_opts[i].has_arg != no_arg) {
+ if (opt_arg == NULL) {
+ if (l_opts[i].has_arg != optional_arg) {
+ if (opt_ind < (argc - 1))
+ if (argv[opt_ind + 1][0] != '-')
+ opt_arg = argv[++opt_ind];
+ }
+ else if (l_opts[i].has_arg == require_arg) {
+ if (opt_err)
+ HDfprintf(rawerrorstream, "%s: option required for \"--%s\" flag\n", argv[0],
+ arg);
+
+ optchar = '?';
+ }
+ }
+ }
+ else {
+ if (opt_arg) {
+ if (opt_err)
+ HDfprintf(rawerrorstream, "%s: no option required for \"%s\" flag\n", argv[0],
+ arg);
+
+ optchar = '?';
+ }
+ }
+ break;
+ }
+ }
+
+ if (l_opts[i].name == NULL) {
+ /* exhausted all of the l_opts we have and still didn't match */
+ if (opt_err)
+ HDfprintf(rawerrorstream, "%s: unknown option \"%s\"\n", argv[0], arg);
+
+ optchar = '?';
+ }
+
+ opt_ind++;
+ sp = 1;
+
+ HDfree(arg);
+ }
+ else {
+ register char *cp; /* pointer into current token */
+
+ /* short command line option */
+ optchar = argv[opt_ind][sp];
+
+ if (optchar == ':' || (cp = HDstrchr(opts, optchar)) == 0) {
+ if (opt_err)
+ HDfprintf(rawerrorstream, "%s: unknown option \"%c\"\n", argv[0], optchar);
+
+ /* if no chars left in this token, move to next token */
+ if (argv[opt_ind][++sp] == '\0') {
+ opt_ind++;
+ sp = 1;
+ }
+ return '?';
+ }
+
+ if (*++cp == ':') {
+ /* if a value is expected, get it */
+ if (argv[opt_ind][sp + 1] != '\0') {
+ /* flag value is rest of current token */
+ opt_arg = &argv[opt_ind++][sp + 1];
+ }
+ else if (++opt_ind >= argc) {
+ if (opt_err)
+ HDfprintf(rawerrorstream, "%s: value expected for option \"%c\"\n", argv[0], optchar);
+
+ optchar = '?';
+ }
+ else {
+ /* flag value is next token */
+ opt_arg = argv[opt_ind++];
+ }
+
+ sp = 1;
+ }
+ /* wildcard argument */
+ else if (*cp == '*') {
+ /* check the next argument */
+ opt_ind++;
+ /* we do have an extra argument, check if not last */
+ if ((opt_ind + 1) < argc) {
+ if (argv[opt_ind][0] != '-') {
+ opt_arg = argv[opt_ind++];
+ }
+ else {
+ opt_arg = NULL;
+ }
+ }
+ else {
+ opt_arg = NULL;
+ }
+ }
+ else {
+ /* set up to look at next char in token, next time */
+ if (argv[opt_ind][++sp] == '\0') {
+ /* no more in current token, so setup next token */
+ opt_ind++;
+ sp = 1;
+ }
+ opt_arg = NULL;
+ }
+ }
+
+ /* return the current flag character found */
+ return optchar;
+}
+
/*****************************************************************************
*
* Function: parse_tuple()
diff --git a/tools/lib/h5tools_utils.h b/tools/lib/h5tools_utils.h
index 095ad6c..f955d2b 100644
--- a/tools/lib/h5tools_utils.h
+++ b/tools/lib/h5tools_utils.h
@@ -40,6 +40,53 @@ H5TOOLS_DLLVAR FILE *overflow_file;
H5TOOLS_DLLVAR hsize_t H5TOOLS_MALLOCSIZE;
/* size of hyperslab buffer when a dataset is bigger than H5TOOLS_MALLOCSIZE */
H5TOOLS_DLLVAR hsize_t H5TOOLS_BUFSIZE;
+/*
+ * begin get_option section
+ */
+H5TOOLS_DLLVAR int opt_err; /* getoption prints errors if this is on */
+H5TOOLS_DLLVAR int opt_ind; /* token pointer */
+H5TOOLS_DLLVAR const char *opt_arg; /* flag argument (or value) */
+
+/*
+ * get_option determines which options are specified on the command line and
+ * returns a pointer to any arguments possibly associated with the option in
+ * the ``opt_arg'' variable. get_option returns the shortname equivalent of
+ * the option. The long options are specified in the following way:
+ *
+ * struct long_options foo[] = {
+ * { "filename", require_arg, 'f' },
+ * { "append", no_arg, 'a' },
+ * { "width", require_arg, 'w' },
+ * { NULL, 0, 0 }
+ * };
+ *
+ * Long named options can have arguments specified as either:
+ *
+ * ``--param=arg'' or ``--param arg''
+ *
+ * Short named options can have arguments specified as either:
+ *
+ * ``-w80'' or ``-w 80''
+ *
+ * and can have more than one short named option specified at one time:
+ *
+ * -aw80
+ *
+ * in which case those options which expect an argument need to come at the
+ * end.
+ */
+typedef struct long_options {
+ const char *name; /* name of the long option */
+ int has_arg; /* whether we should look for an arg */
+ char shortval; /* the shortname equivalent of long arg
+ * this gets returned from get_option */
+} long_options;
+
+H5TOOLS_DLL int get_option(int argc, const char *const *argv, const char *opt,
+ const struct long_options *l_opt);
+/*
+ * end get_option section
+ */
/*struct taken from the dumper. needed in table struct*/
typedef struct obj_t {