hexed: Minor improvements

This commit is contained in:
Gregory Nutt 2015-11-22 12:56:19 -06:00
parent b1ef1169b9
commit 37401b9889
9 changed files with 55 additions and 77 deletions

View File

@ -98,6 +98,6 @@ extern FAR struct cmdargs_s *g_cmdargs;
* Public Function Prototypes * Public Function Prototypes
****************************************************************************/ ****************************************************************************/
int parsecmdargs(FAR char *argv[], FAR struct arglist_s *arglist); int parsecmdargs(FAR char *argv[], FAR const struct arglist_s *arglist);
#endif /* __APPS_SYSTEM_HEXED_INCLUDE_CMDARGS_H */ #endif /* __APPS_SYSTEM_HEXED_INCLUDE_CMDARGS_H */

View File

@ -127,8 +127,8 @@ struct command_s
* Public Data * Public Data
****************************************************************************/ ****************************************************************************/
extern int g_wordsize;
extern FAR struct bfile_s *g_hexfile; extern FAR struct bfile_s *g_hexfile;
extern int g_wordsize;
/**************************************************************************** /****************************************************************************
* Public Function Prototypes * Public Function Prototypes

View File

@ -52,8 +52,6 @@ FAR struct cmdargs_s *g_cmdargs = NULL;
* Private Data * Private Data
****************************************************************************/ ****************************************************************************/
static struct cmdargs_s g_cmdargs_storage;
/**************************************************************************** /****************************************************************************
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
@ -74,23 +72,14 @@ static struct cmdargs_s g_cmdargs_storage;
* *
****************************************************************************/ ****************************************************************************/
int parsecmdargs(FAR char *argv[], FAR struct arglist_s *arglist) int parsecmdargs(FAR char *argv[], FAR const struct arglist_s *arglist)
{ {
FAR char *arg; FAR char *arg;
int i; int i;
/* Set cmdargs global reference (if already not set) */
if (g_cmdargs != &g_cmdargs_storage)
{
g_cmdargs = &g_cmdargs_storage;
memset(g_cmdargs, 0, sizeof(struct cmdargs_s));
}
g_cmdargs->argid = 0;
/* Get next arg */ /* Get next arg */
g_cmdargs->argid = 0;
if ((g_cmdargs->flags & CMDARGS_FL_SINGLE) == 0) if ((g_cmdargs->flags & CMDARGS_FL_SINGLE) == 0)
{ {
g_cmdargs->idx++; g_cmdargs->idx++;

View File

@ -190,15 +190,6 @@ int hexcopy(FAR struct command_s *cmd, int optc, char *opt)
} }
else else
{ {
/* We need to have a file name for this command */
if (g_hexfile == NULL)
{
fprintf(stderr, "ERROR: Copy command requires a filename\n");
g_last_error = EINVAL;
return -EINVAL;
}
optc = runcopy(cmd); optc = runcopy(cmd);
} }

View File

@ -254,15 +254,6 @@ int hexdump(FAR struct command_s *cmd, int optc, char *opt)
} }
else else
{ {
/* We need to have a file name for this command */
if (g_hexfile == NULL)
{
fprintf(stderr, "ERROR: Dump command requires a filename\n");
g_last_error = EINVAL;
return -EINVAL;
}
optc = rundump(cmd); optc = rundump(cmd);
} }

View File

@ -59,11 +59,11 @@ int g_wordsize;
****************************************************************************/ ****************************************************************************/
static FAR struct command_s g_cmdtbl[CMD_MAX_CNT]; static FAR struct command_s g_cmdtbl[CMD_MAX_CNT];
static FAR struct command_s *g_cmd; static int g_ncmds;
/* Valid args */ /* Valid args */
static struct arglist_s g_arglist[] = static const struct arglist_s g_arglist[] =
{ {
{"h", 0}, {"h", 0},
{"co", CMDARGS_FL_OPTREQ}, {"co", CMDARGS_FL_OPTREQ},
@ -216,8 +216,6 @@ FAR struct command_s *newcmd(int cmdno)
/* Set defaults */ /* Set defaults */
cmd = &g_cmdtbl[cmdno]; cmd = &g_cmdtbl[cmdno];
memset(cmd, sizeof(struct command_s), 0);
cmd->id = g_cmdargs->argid; cmd->id = g_cmdargs->argid;
cmd->cmd = g_cmdargs->arg; cmd->cmd = g_cmdargs->arg;
cmd->flags = CMD_FL_CMDLINE; cmd->flags = CMD_FL_CMDLINE;
@ -229,17 +227,17 @@ FAR struct command_s *newcmd(int cmdno)
int parseargs(FAR char *argv[]) int parseargs(FAR char *argv[])
{ {
FAR struct command_s *cmd;
FAR char *fname; FAR char *fname;
FAR char *opt; FAR char *opt;
int cmdcnt;
int optc; int optc;
fname = NULL; fname = NULL;
cmdcnt = 0; cmd = g_cmdtbl;
g_cmd = g_cmdtbl; g_ncmds = 0;
g_hexfile = NULL; g_hexfile = NULL;
memset(g_cmd, sizeof(struct command_s), 0); memset(g_cmdtbl, 0, CMD_MAX_CNT * sizeof(struct command_s));
while (parsecmdargs(argv, g_arglist) != EOF) while (parsecmdargs(argv, g_arglist) != EOF)
{ {
@ -247,10 +245,10 @@ int parseargs(FAR char *argv[])
if (g_cmdargs->argid > 0) if (g_cmdargs->argid > 0)
{ {
cmdcnt++; g_ncmds++;
optc = 0; optc = 0;
opt = g_cmdargs->opt; opt = g_cmdargs->opt;
g_cmd = newcmd(cmdcnt); cmd = newcmd(g_ncmds);
} }
else else
{ {
@ -261,7 +259,7 @@ int parseargs(FAR char *argv[])
/* Set command options */ /* Set command options */
optc = hexcmd(g_cmd, optc, opt); optc = hexcmd(cmd, optc, opt);
if (optc < 0) if (optc < 0)
{ {
/* End of range option? */ /* End of range option? */
@ -296,7 +294,7 @@ int parseargs(FAR char *argv[])
if (optc <= 0) if (optc <= 0)
{ {
g_cmd = g_cmdtbl; cmd = g_cmdtbl;
optc = 0; optc = 0;
} }
} }
@ -308,19 +306,27 @@ int parseargs(FAR char *argv[])
int runargs(void) int runargs(void)
{ {
FAR struct command_s *cmd;
int quit; int quit;
int i; int i;
/* Check for no commands */
if (g_ncmds == 0)
{
return CMD_QUIT;
}
/* Scan for help command */ /* Scan for help command */
quit = 0; quit = 0;
g_cmd = &g_cmdtbl[1]; /* Skip 1st reserved command */ cmd = &g_cmdtbl[1]; /* Skip 1st reserved command */
for (i = 1; i < CMD_MAX_CNT && g_cmd->id > 0; i++, g_cmd++) for (i = 1; i < g_ncmds + 1; i++, cmd++)
{ {
if (g_cmd->id == CMD_HELP) if (cmd->id == CMD_HELP)
{ {
hexcmd(g_cmd, -1, NULL); hexcmd(cmd, -1, NULL);
quit = CMD_QUIT; quit = CMD_QUIT;
break; break;
} }
@ -330,12 +336,23 @@ int runargs(void)
if (quit != CMD_QUIT) if (quit != CMD_QUIT)
{ {
g_cmd = &g_cmdtbl[1]; /* Skip 1st reserved command */ /* All other commands require a filename arguement */
for (i = 1; i < CMD_MAX_CNT && g_cmd->id > 0; i++, g_cmd++)
{
hexcmd(g_cmd, -1, NULL);
if ((g_cmd->flags & CMD_FL_QUIT) != 0) if (g_hexfile == NULL)
{
fprintf(stderr, "ERROR: Missing filename\n");
g_last_error = EINVAL;
return -EINVAL;
}
/* Process every command on the command line */
cmd = &g_cmdtbl[1]; /* Skip 1st reserved command */
for (i = 1; i < g_ncmds + 1; i++, cmd++)
{
hexcmd(cmd, -1, NULL);
if ((cmd->flags & CMD_FL_QUIT) != 0)
{ {
quit = CMD_QUIT; quit = CMD_QUIT;
} }
@ -362,10 +379,18 @@ int main(int argc, FAR char *argv[])
int hexed_main(int argc, char *argv[]) int hexed_main(int argc, char *argv[])
#endif #endif
{ {
g_wordsize = WORD_8; struct cmdargs_s args;
/* Set cmdargs global reference (if already not set) */
memset(&args, 0, sizeof(struct cmdargs_s));
g_cmdargs = &args;
/* Check command line arguments */ /* Check command line arguments */
g_wordsize = WORD_8;
g_hexfile = NULL;
parseargs(argv); parseargs(argv);
/* Run commands */ /* Run commands */

View File

@ -222,15 +222,6 @@ int hexmove(FAR struct command_s *cmd, int optc, char *opt)
} }
else else
{ {
/* We need to have a file name for this command */
if (g_hexfile == NULL)
{
fprintf(stderr, "ERROR: Move command requires a filename\n");
g_last_error = EINVAL;
return -EINVAL;
}
optc = runmove(cmd); optc = runmove(cmd);
} }

View File

@ -142,15 +142,6 @@ int hexremove(FAR struct command_s *cmd, int optc, char *opt)
} }
else else
{ {
/* We need to have a file name for this command */
if (g_hexfile == NULL)
{
fprintf(stderr, "ERROR: Remove command requires a filename\n");
g_last_error = EINVAL;
return -EINVAL;
}
optc = runremove(cmd); optc = runremove(cmd);
} }