Skip to content

Commit

Permalink
Clean up command handling in hdp.c/h
Browse files Browse the repository at this point in the history
* Move list of commands inside hdp.c
* Remove HDP_MASTER scheme
* Tie command string & enum value together in a struct to keep in sync
* Minor tidying
  • Loading branch information
derobins committed Sep 13, 2023
1 parent 2c3b99b commit ffeb597
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 44 deletions.
57 changes: 34 additions & 23 deletions mfhdf/dumper/hdp.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,28 @@
* help@hdfgroup.org. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#define HDP_MASTER
#define VSET_INTERFACE
#include "hdp.h"
#include "local_nc.h" /* to use some definitions */

/* Global Variables */

/* indicates Vsets have been initialized for the current file */
intn vinit_done = FALSE;

/* hdp commands (stored as (value, name) pairs to keep them in sync) */
typedef enum { HELP, LIST, DUMPSDS, DUMPRIG, DUMPVG, DUMPVD, DUMPGR, BAD_COMMAND } command_value_t;

typedef struct command_t {
const command_value_t value;
const char *name;
} command_t;

static const command_t commands[] = {{HELP, "help"}, {LIST, "list"},
{DUMPSDS, "dumpsds"}, {DUMPRIG, "dumprig"},
{DUMPVG, "dumpvg"}, {DUMPVD, "dumpvd"},
{DUMPGR, "dumpgr"}, {BAD_COMMAND, "BADNESS - not a valid command"}};

/* Print the usage message about this utility */
static void
usage(intn argc, char *argv[])
Expand Down Expand Up @@ -87,49 +104,43 @@ init_dump_opts(dump_info_t *dump_opts)
int
main(int argc, char *argv[])
{
command_t cmd; /* command to perform */
intn curr_arg; /* current cmd line argument */
dump_opt_t glob_opts; /* global options for all commands */
intn j; /* local counting variables */
command_value_t cmd = BAD_COMMAND; /* command to perform */
intn curr_arg; /* current cmd line argument */
dump_opt_t glob_opts; /* global options for all commands */
intn j; /* local counting variables */

memset(&glob_opts, 0, sizeof(dump_opt_t));

if (argc < 2) {
usage(argc, argv);
exit(1);
} /* end if */
}

curr_arg = 1;
/* printf("Argument 0: %s\n",argv[0]);
printf("Argument 1: %s\n",argv[1]);
*/
while (curr_arg < argc && (argv[curr_arg][0] == '-')) {
/* while(curr_arg<argc && (argv[curr_arg][0]=='-' || argv[curr_arg][0]=='/')) { */
switch (argv[curr_arg][1]) {
case 'H':
/* case 'h': */ /* Print help for a given command */
/* case 'h': */ /* Print help for a given command */
if (curr_arg < argc - 1) {
glob_opts.help = TRUE; /* for displaying options. */
break;
}
default:
usage(argc, argv); /* Display the general usage. */
exit(1);
} /* end switch */
}
curr_arg++;
} /* end while */
}

for (j = 0, cmd = HELP; j < (sizeof(commands) / sizeof(const char *)); j++, cmd++) {
if (HDstrcmp(argv[curr_arg], commands[j]) == 0)
for (j = 0; j < (sizeof(commands) / sizeof(command_t)); j++) {
if (HDstrcmp(argv[curr_arg], commands[j].name) == 0) {
cmd = commands[j].value;
break;
} /* end for */
}
}

/* printf("cmd=%d\n",(int)cmd);
printf("command=%s\n",argv[curr_arg]);
*/
curr_arg++;

/* must be a legit command */
switch (cmd) {
case LIST:
if (FAIL == do_list(curr_arg, argc, argv, glob_opts.help))
Expand Down Expand Up @@ -167,17 +178,17 @@ main(int argc, char *argv[])
break;

case HELP:
case NONE:
usage(argc, argv);
break;

case BAD_COMMAND:
default:
printf("Invalid command!, cmd=%d\n", (int)cmd);
exit(1);
break;
} /* end switch */
}

return (0);
return 0;
}

/* -----------------------------------------------------------------
Expand Down
25 changes: 4 additions & 21 deletions mfhdf/dumper/hdp.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,15 @@
#include "hfile.h"

/* Global Variables */
#ifndef HDP_MASTER
extern
#endif /* !HDP_MASTER */
intn vinit_done
#ifdef HDP_MASTER
= FALSE /* indicates Vsets have been init'ed for the current file */
#endif /* HDP_MASTER */
;
extern intn vinit_done;

/* Global Definitions */
#define MAXCHOICES 50

#ifndef MAXNAMELEN
#define MAXNAMELEN 100
#endif /* !MAXNAMELEN */
#endif

#define MAXCLASSLEN 100
#define MAXPERLINE 65 /* max # of chars per line in the output */
#define MAXRANK 100
Expand Down Expand Up @@ -283,18 +278,6 @@ extern
} \
}

/* Add enum and string for new commands to both of the variables below. */
/* Preserve the correct/corresponding ordering */
typedef enum { HELP, LIST, DUMPSDS, DUMPRIG, DUMPVG, DUMPVD, DUMPGR, NONE } command_t;
#ifndef HDP_MASTER
extern
#endif /* !HDP_MASTER */
const char *commands[]
#ifdef HDP_MASTER
= {"help", "list", "dumpsds", "dumprig", "dumpvg", "dumpvd", "dumpgr"}
#endif /* HDP_MASTER */
;

/* Global options structure */
typedef struct {
intn help; /* Print help on this command */
Expand Down

0 comments on commit ffeb597

Please sign in to comment.