summaryrefslogtreecommitdiffstats
path: root/tools/lib/h5tools_utils.c
diff options
context:
space:
mode:
authorDana Robinson <43805+derobins@users.noreply.github.com>2021-06-29 20:06:48 (GMT)
committerGitHub <noreply@github.com>2021-06-29 20:06:48 (GMT)
commit2090a527c13a13c0ea5b59b319b3402d2ef8f578 (patch)
treeb9d6a7744894956cba1e99630f1803c42d2298e4 /tools/lib/h5tools_utils.c
parent4ff544f9977da3bd54eac03cc124961eea7c4daf (diff)
downloadhdf5-2090a527c13a13c0ea5b59b319b3402d2ef8f578.zip
hdf5-2090a527c13a13c0ea5b59b319b3402d2ef8f578.tar.gz
hdf5-2090a527c13a13c0ea5b59b319b3402d2ef8f578.tar.bz2
Brings the tools getopt(3) replacement into the main library (#803)
* Moves get_option from the tools library to the C library * Adds H5 prefix to get_option call and variables * Renames the H5_get_option long options struct and enum
Diffstat (limited to 'tools/lib/h5tools_utils.c')
-rw-r--r--tools/lib/h5tools_utils.c169
1 files changed, 2 insertions, 167 deletions
diff --git a/tools/lib/h5tools_utils.c b/tools/lib/h5tools_utils.c
index 6aec6b8..f2407bf 100644
--- a/tools/lib/h5tools_utils.c
+++ b/tools/lib/h5tools_utils.c
@@ -25,12 +25,8 @@
#include "H5FDros3.h"
#endif
-/* global variables */
-unsigned h5tools_nCols = 80;
-/* ``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) */
+/* Global variables */
+unsigned h5tools_nCols = 80;
static int h5tools_d_status = 0;
static const char *h5tools_progname = "h5tools";
@@ -162,167 +158,6 @@ 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 **argv, const char *opts, const struct long_options *l_opts)
-{
- static int sp = 1; /* character index in current token */
- int opt_opt = '?'; /* 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 */
- opt_opt = 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);
-
- opt_opt = '?';
- }
- }
- }
- else {
- if (opt_arg) {
- if (opt_err)
- HDfprintf(rawerrorstream, "%s: no option required for \"%s\" flag\n", argv[0],
- arg);
-
- opt_opt = '?';
- }
- }
- 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);
-
- opt_opt = '?';
- }
-
- opt_ind++;
- sp = 1;
-
- HDfree(arg);
- }
- else {
- register char *cp; /* pointer into current token */
-
- /* short command line option */
- opt_opt = argv[opt_ind][sp];
-
- if (opt_opt == ':' || (cp = HDstrchr(opts, opt_opt)) == 0) {
- if (opt_err)
- HDfprintf(rawerrorstream, "%s: unknown option \"%c\"\n", argv[0], opt_opt);
-
- /* 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], opt_opt);
-
- opt_opt = '?';
- }
- 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 opt_opt;
-}
-
/*****************************************************************************
*
* Function: parse_tuple()