From 3e10f0084bc7f64cf9350cab97025fc9c99a3786 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Sun, 22 Nov 2015 09:55:29 -0600 Subject: [PATCH] debugging system/hexed. Still does not work --- system/hexed/include/bfile.h | 6 ++ system/hexed/include/cmdargs.h | 23 ++++--- system/hexed/include/hexed.h | 8 +-- system/hexed/src/bfile.c | 74 ++++++++++++++------ system/hexed/src/cmdargs.c | 117 +++++++++++++++++-------------- system/hexed/src/hexcopy.c | 15 ++-- system/hexed/src/hexed.c | 122 +++++++++++++++++---------------- system/hexed/src/hexenter.c | 20 ++++-- system/hexed/src/hexhelp.c | 27 ++++++-- system/hexed/src/hexinsert.c | 13 ++-- system/hexed/src/hexmove.c | 15 ++-- system/hexed/src/hexremove.c | 12 ++-- system/hexed/src/hexword.c | 12 ++-- 13 files changed, 281 insertions(+), 183 deletions(-) diff --git a/system/hexed/include/bfile.h b/system/hexed/include/bfile.h index 65c7241ab..16ebc5749 100644 --- a/system/hexed/include/bfile.h +++ b/system/hexed/include/bfile.h @@ -71,6 +71,12 @@ struct bfile_s FAR char *buf; }; +/**************************************************************************** + * Public Data + ****************************************************************************/ + +extern int g_last_error; + /**************************************************************************** * Public Function Prototypes ****************************************************************************/ diff --git a/system/hexed/include/cmdargs.h b/system/hexed/include/cmdargs.h index cae666d1f..a6bc73df0 100644 --- a/system/hexed/include/cmdargs.h +++ b/system/hexed/include/cmdargs.h @@ -54,11 +54,14 @@ #define CMDARGS_FL_RESET (CMDARGS_FL_SINGLE | CMDARGS_FL_SETARG) -/* Flags set by the caller */ +/* Flags set by the caller + * + * CMDARGS_FL_OPT - Options accepted + * CMDARGS_FL_OPTREQ - Options required + */ -#define CMDARGS_FL_OPT 0x00010000 /* options accepted */ -#define CMDARGS_FL_OPTREQ (0x00020000 | CMDARGS_FL_OPT) /* opts */ - /* required */ +#define CMDARGS_FL_OPT 0x00010000 +#define CMDARGS_FL_OPTREQ (0x00020000 | CMDARGS_FL_OPT) #define CMDARGS_FL_CALLMASK(fl) (fl & 0xffff0000) @@ -78,18 +81,18 @@ struct arglist_s struct cmdargs_s { - int idx; /* last argument parsed */ - int flags; /* argument flags */ - int argid; /* argument id in struct arglist_s */ - FAR char *arg; /* argument string */ - FAR char *opt; /* options string */ + int idx; /* Last argument parsed */ + int flags; /* Argument flags */ + int argid; /* Argument id in struct arglist_s */ + FAR char *arg; /* Argument string */ + FAR char *opt; /* Options string */ }; /**************************************************************************** * Public Data ****************************************************************************/ -extern FAR struct cmdargs_s *cmdargs; +extern FAR struct cmdargs_s *g_cmdargs; /**************************************************************************** * Public Function Prototypes diff --git a/system/hexed/include/hexed.h b/system/hexed/include/hexed.h index 579552272..83801e6b3 100644 --- a/system/hexed/include/hexed.h +++ b/system/hexed/include/hexed.h @@ -57,7 +57,6 @@ /* hexed constants */ #define PNAME "hexed" /* Program name */ -#define TEMP_FILE "~hexed.tmp" /* Temp file name */ #define CMD_MAX_CNT 0x10 /* Command table max count */ #define OPT_BUF_SZ 0x40 /* Option buffer size */ @@ -74,12 +73,6 @@ #define CMD_FL_QUIT 0x00000002 /* Quit after command line */ #define CMD_FL_OVERFLOW 0x00000003 /* Command overflow */ -#define RETURN_ERR(err) \ -{ \ - set_errno(err); \ - return -err; \ -} - /**************************************************************************** * Public Types ****************************************************************************/ @@ -150,6 +143,7 @@ int hexinsert(FAR struct command_s *cmd, int optc, FAR char *opt); int hexmove(FAR struct command_s *cmd, int optc, FAR char *opt); int hexremove(FAR struct command_s *cmd, int optc, FAR char *opt); int hexword(FAR struct command_s *cmd, int optc, FAR char *opt); +int show_usage(void); void printhex(uint64_t i, int size); #endif /* __APPS_SYSTEM_HEXED_INCLUDE_HEXED_H */ diff --git a/system/hexed/src/bfile.c b/system/hexed/src/bfile.c index e908308ff..83032b413 100644 --- a/system/hexed/src/bfile.c +++ b/system/hexed/src/bfile.c @@ -36,23 +36,30 @@ * Included Files ****************************************************************************/ -#include #include #include #include +#include + #include "bfile.h" +/**************************************************************************** + * Public Data + ****************************************************************************/ + +int g_last_error; + /**************************************************************************** * Private Functions ****************************************************************************/ /* Allocate file buffer */ -static void *bfallocbuf(FAR struct bfile_s *bf, long sz) +static FAR void *bfallocbuf(FAR struct bfile_s *bf, long sz) { if (bf == NULL) { - errno = EBADF; + g_last_error = EBADF; return NULL; } @@ -61,6 +68,7 @@ static void *bfallocbuf(FAR struct bfile_s *bf, long sz) sz = BFILE_BUF_ALIGN(sz); if ((bf->buf = realloc(bf->buf, sz)) == NULL) { + g_last_error = ENOMEM; return NULL; } @@ -81,7 +89,7 @@ static int bffree(FAR struct bfile_s *bf) { if (bf == NULL) { - errno = EBADF; + g_last_error = EBADF; return -EBADF; } @@ -115,7 +123,7 @@ long fsize(FILE * fp) if (fp == NULL) { - errno = EBADF; + g_last_error = EBADF; return 0; } @@ -132,7 +140,7 @@ long bftruncate(FAR struct bfile_s *bf, long sz) { if (bf == NULL) { - errno = EBADF; + g_last_error = EBADF; return -EBADF; } @@ -146,9 +154,12 @@ long bftruncate(FAR struct bfile_s *bf, long sz) int bfflush(FAR struct bfile_s *bf) { + ssize_t nread; + int ret = OK; + if (bf == NULL) { - errno = EBADF; + g_last_error = EBADF; return -EBADF; } @@ -161,15 +172,28 @@ int bfflush(FAR struct bfile_s *bf) /* Write file */ - set_errno(0); fseek(bf->fp, 0, SEEK_SET); - if (fwrite(bf->buf, 1, bf->size, bf->fp) == bf->size) + nread = fwrite(bf->buf, 1, bf->size, bf->fp); + if (nread < 0) + { + g_last_error = errno; + fprintf(stderr, "ERROR: Write to file failed: %d\n", g_last_error); + ret = -g_last_error; + } + else if (fwrite(bf->buf, 1, bf->size, bf->fp) == bf->size) + { + fprintf(stderr, "ERROR: Bad write size\n"); + g_last_error = EIO; + ret = -EIO; + } + else { bf->flags &= ~BFILE_FL_DIRTY; + ret = OK; } fflush(bf->fp); - return errno; + return ret; } /* Opens a Buffered File */ @@ -182,7 +206,7 @@ FAR struct bfile_s *bfopen(char *name, char *mode) if (name == NULL) { - errno = EINVAL; + g_last_error = EINVAL; return NULL; } @@ -190,6 +214,7 @@ FAR struct bfile_s *bfopen(char *name, char *mode) if ((bf = malloc(sizeof(struct bfile_s))) == NULL) { + g_last_error = ENOMEM; return NULL; } @@ -200,6 +225,7 @@ FAR struct bfile_s *bfopen(char *name, char *mode) if ((bf->name = malloc(strlen(name) + 1)) == NULL) { bffree(bf); + g_last_error = ENOMEM; return NULL; } @@ -209,6 +235,7 @@ FAR struct bfile_s *bfopen(char *name, char *mode) if ((bf->fp = fopen(bf->name, mode)) == NULL) { + g_last_error = errno; bffree(bf); return NULL; } @@ -233,7 +260,7 @@ int bfclose(FAR struct bfile_s *bf) if (bf == NULL) { - errno = EBADF; + g_last_error = EBADF; return -EBADF; } @@ -258,7 +285,7 @@ long bfclip(FAR struct bfile_s *bf, long off, long sz) if (bf == NULL) { - errno = EBADF; + g_last_error = EBADF; return EOF; } @@ -266,6 +293,7 @@ long bfclip(FAR struct bfile_s *bf, long off, long sz) if (off < 0 || sz <= 0) { + g_last_error = ERANGE; return EOF; } @@ -273,6 +301,7 @@ long bfclip(FAR struct bfile_s *bf, long off, long sz) if (off > bf->size) { + g_last_error = ENFILE; return EOF; } @@ -287,8 +316,10 @@ long bfclip(FAR struct bfile_s *bf, long off, long sz) cnt = bf->size - (off + sz); memmove(bf->buf + off, bf->buf + off + sz, cnt); - bf->size -= sz; + + bf->size -= sz; bf->flags |= BFILE_FL_DIRTY; + memset(bf->buf + bf->size, 0, sz); return cnt; } @@ -305,7 +336,7 @@ long bfinsert(FAR struct bfile_s *bf, long off, void *mem, long sz) if (bf == NULL) { - errno = EBADF; + g_last_error = EBADF; return EOF; } @@ -313,6 +344,7 @@ long bfinsert(FAR struct bfile_s *bf, long off, void *mem, long sz) if (off < 0 || sz <= 0) { + g_last_error = ERANGE; return EOF; } @@ -351,7 +383,7 @@ long bfcopy(FAR struct bfile_s *bf, long off, long src, long sz) { if (bf == NULL) { - errno = EBADF; + g_last_error = EBADF; return EOF; } @@ -359,6 +391,7 @@ long bfcopy(FAR struct bfile_s *bf, long off, long src, long sz) if (src > bf->size) { + g_last_error = ENFILE; return EOF; } @@ -398,7 +431,7 @@ long bfmove(FAR struct bfile_s *bf, long off, long src, long sz) if (bf == NULL) { - errno = EBADF; + g_last_error = EBADF; return EOF; } @@ -406,6 +439,7 @@ long bfmove(FAR struct bfile_s *bf, long off, long src, long sz) if (src > bf->size) { + g_last_error = ENFILE; return EOF; } @@ -462,7 +496,7 @@ long bfread(FAR struct bfile_s *bf) if (bf == NULL) { - errno = EBADF; + g_last_error = EBADF; return EOF; } @@ -495,11 +529,9 @@ long bfread(FAR struct bfile_s *bf) long bfwrite(FAR struct bfile_s *bf, long off, void *mem, long sz) { - long r; - if (bf == NULL) { - errno = EBADF; + g_last_error = EBADF; return EOF; } diff --git a/system/hexed/src/cmdargs.c b/system/hexed/src/cmdargs.c index 0e9435c8d..76db7094b 100644 --- a/system/hexed/src/cmdargs.c +++ b/system/hexed/src/cmdargs.c @@ -37,90 +37,106 @@ ****************************************************************************/ #include +#include #include + #include "cmdargs.h" /**************************************************************************** * Public Data ****************************************************************************/ -FAR struct cmdargs_s *cmdargs = NULL; +FAR struct cmdargs_s *g_cmdargs = NULL; /**************************************************************************** * Private Data ****************************************************************************/ -static struct cmdargs_s _cmdargs; +static struct cmdargs_s g_cmdargs_storage; /**************************************************************************** * Public Functions ****************************************************************************/ -/* Parse the command line arguments */ +/**************************************************************************** + * Name: parsecmdargs + * + * Description: + * Parse the command line arguments + * + * Input parmeters: + * argv - The command line arguments + * arglist - The array or supported arguments + * + * Returned Value: + * One success, the argument ID is returned. EOF is returned on any + * failure. + * + ****************************************************************************/ int parsecmdargs(FAR char *argv[], FAR struct arglist_s *arglist) { + FAR char *arg; int i; - char *arg; - /* Set cmdargs */ + /* Set cmdargs global reference (if already not set) */ - if (cmdargs != &_cmdargs) + if (g_cmdargs != &g_cmdargs_storage) { - cmdargs = &_cmdargs; - memset(cmdargs, 0, sizeof(struct cmdargs_s)); + g_cmdargs = &g_cmdargs_storage; + memset(g_cmdargs, 0, sizeof(struct cmdargs_s)); } - cmdargs->argid = 0; + g_cmdargs->argid = 0; /* Get next arg */ - if ((cmdargs->flags & CMDARGS_FL_SINGLE) == 0) + if ((g_cmdargs->flags & CMDARGS_FL_SINGLE) == 0) { - cmdargs->idx++; - arg = argv[cmdargs->idx]; - cmdargs->flags = 0; - - /* Single step through this arg */ + g_cmdargs->idx++; + arg = argv[g_cmdargs->idx]; + g_cmdargs->flags = 0; } else { - arg = cmdargs->arg + 1; - cmdargs->flags &= CMDARGS_FL_RESET; + /* Skip over this argument */ + + arg = g_cmdargs->arg + 1; + g_cmdargs->flags &= CMDARGS_FL_RESET; } - /* Error or end of command line */ + /* End of command line */ - if (argv == NULL || arglist == NULL || argv[cmdargs->idx] == NULL) + if (argv[g_cmdargs->idx] == NULL) { - memset(cmdargs, 0, sizeof(struct cmdargs_s)); - return -1; + memset(g_cmdargs, 0, sizeof(struct cmdargs_s)); + return EOF; } /* Check for argument */ - cmdargs->arg = arg; + g_cmdargs->arg = arg; if (*arg == '-') { - cmdargs->flags |= CMDARGS_FL_SETARG; + g_cmdargs->flags |= CMDARGS_FL_SETARG; arg++; if (*arg == '-') { arg++; - cmdargs->flags |= CMDARGS_FL_LONGARG; + g_cmdargs->flags |= CMDARGS_FL_LONGARG; } } /* Scan arglist */ - if ((cmdargs->flags & CMDARGS_FL_SETARG) != 0) + if ((g_cmdargs->flags & CMDARGS_FL_SETARG) != 0) { for (i = 1; arglist->name != NULL; i++, arglist++) { /* Skip long args in single step mode */ - if ((cmdargs->flags & CMDARGS_FL_SINGLE) != 0 - && strlen(arglist->name) > 1) + if ((g_cmdargs->flags & CMDARGS_FL_SINGLE) != 0 && + strlen(arglist->name) > 1) { continue; } @@ -132,12 +148,12 @@ int parsecmdargs(FAR char *argv[], FAR struct arglist_s *arglist) /* The long arg flag is set but we've found a single arg match */ if (strlen(arglist->name) == 1 && - (cmdargs->flags & CMDARGS_FL_LONGARG) != 0) + (g_cmdargs->flags & CMDARGS_FL_LONGARG) != 0) { break; } - cmdargs->argid = i; + g_cmdargs->argid = i; break; } } @@ -145,16 +161,16 @@ int parsecmdargs(FAR char *argv[], FAR struct arglist_s *arglist) /* Found argument */ - if (cmdargs->argid > 0) + if (g_cmdargs->argid > 0) { - cmdargs->arg = arg; - cmdargs->opt = NULL; - cmdargs->flags |= CMDARGS_FL_CALLMASK(arglist->flags); + g_cmdargs->arg = arg; + g_cmdargs->opt = NULL; + g_cmdargs->flags |= CMDARGS_FL_CALLMASK(arglist->flags); arg += strlen(arglist->name); /* Argument accepts options */ - if ((cmdargs->flags & CMDARGS_FL_OPT) != 0) + if ((g_cmdargs->flags & CMDARGS_FL_OPT) != 0) { /* Option in same arg */ @@ -165,46 +181,47 @@ int parsecmdargs(FAR char *argv[], FAR struct arglist_s *arglist) if (strchr(":=", *arg)) { arg++; - cmdargs->opt = arg; + g_cmdargs->opt = arg; /* Error setting arg/option */ } else { - cmdargs->opt = NULL; + g_cmdargs->opt = NULL; } /* Option in next arg */ } - else if (argv[cmdargs->idx + 1] != NULL && - *argv[cmdargs->idx + 1] != '\0' && - *argv[cmdargs->idx + 1] != '-') + else if (argv[g_cmdargs->idx + 1] != NULL && + *argv[g_cmdargs->idx + 1] != '\0' && + *argv[g_cmdargs->idx + 1] != '-') { - cmdargs->idx++; - cmdargs->opt = argv[cmdargs->idx]; + g_cmdargs->idx++; + g_cmdargs->opt = argv[g_cmdargs->idx]; } } /* Single step through this arg */ if (strlen(arglist->name) == 1 && *arg != '\0' && - (cmdargs->flags & CMDARGS_FL_OPTREQ) != CMDARGS_FL_OPTREQ && - cmdargs->opt == NULL) + (g_cmdargs->flags & CMDARGS_FL_OPTREQ) != CMDARGS_FL_OPTREQ && + g_cmdargs->opt == NULL) { - cmdargs->flags |= CMDARGS_FL_SINGLE; + g_cmdargs->flags |= CMDARGS_FL_SINGLE; } else { - cmdargs->flags &= ~CMDARGS_FL_SINGLE; + g_cmdargs->flags &= ~CMDARGS_FL_SINGLE; } - - /* No valid argument found */ } + + /* No valid argument found */ + else { - cmdargs->flags = 0; - cmdargs->opt = NULL; + g_cmdargs->flags = 0; + g_cmdargs->opt = NULL; } - return cmdargs->argid; + return g_cmdargs->argid; } diff --git a/system/hexed/src/hexcopy.c b/system/hexed/src/hexcopy.c index 39991d6b4..71ad9cae6 100644 --- a/system/hexed/src/hexcopy.c +++ b/system/hexed/src/hexcopy.c @@ -81,7 +81,8 @@ static int runcopy(FAR struct command_s *cmd) if (cmd->opts.src > g_hexfile->size) { - RETURN_ERR(EINVAL); + g_last_error = EINVAL; + return -EINVAL; } switch (cmd->id) @@ -115,7 +116,8 @@ static int setcopy(FAR struct command_s *cmd, int optc, char *opt) if (opt == NULL) { - RETURN_ERR(EFAULT); + g_last_error = EFAULT; + return -EFAULT; } v = strtoll(opt, &s, 0x10); @@ -124,7 +126,8 @@ static int setcopy(FAR struct command_s *cmd, int optc, char *opt) if (s == opt) { - RETURN_ERR(EINVAL); + g_last_error = EINVAL; + return -EINVAL; } /* Set options */ @@ -156,7 +159,8 @@ static int setcopy(FAR struct command_s *cmd, int optc, char *opt) default: /* Too many options specified */ - RETURN_ERR(E2BIG); + g_last_error = E2BIG; + return -E2BIG; } return optc; @@ -174,7 +178,8 @@ int hexcopy(FAR struct command_s *cmd, int optc, char *opt) if (cmd == NULL || (cmd->id != CMD_COPY && cmd->id != CMD_COPY_OVER)) { - RETURN_ERR(EINVAL); + g_last_error = EINVAL; + return -EINVAL; } /* Set/run copy */ diff --git a/system/hexed/src/hexed.c b/system/hexed/src/hexed.c index 05ac5a312..211497c80 100644 --- a/system/hexed/src/hexed.c +++ b/system/hexed/src/hexed.c @@ -36,12 +36,13 @@ * Included Files ****************************************************************************/ -#include #include #include #include #include #include +#include + #include "bfile.h" #include "cmdargs.h" #include "hexed.h" @@ -50,8 +51,8 @@ * Public Data ****************************************************************************/ -int g_wordsize; FAR struct bfile_s *g_hexfile = NULL; +int g_wordsize; /**************************************************************************** * Private Data @@ -64,17 +65,17 @@ static FAR struct command_s *g_cmd; static struct arglist_s g_arglist[] = { - {"?", 0}, + {"h", 0}, {"co", CMDARGS_FL_OPTREQ}, - {"c", CMDARGS_FL_OPTREQ}, - {"d", CMDARGS_FL_OPTREQ}, - {"e", CMDARGS_FL_OPTREQ}, - {"f", CMDARGS_FL_OPTREQ}, - {"i", CMDARGS_FL_OPTREQ}, + {"c", CMDARGS_FL_OPTREQ}, + {"d", CMDARGS_FL_OPTREQ}, + {"e", CMDARGS_FL_OPTREQ}, + {"f", CMDARGS_FL_OPTREQ}, + {"i", CMDARGS_FL_OPTREQ}, {"mo", CMDARGS_FL_OPTREQ}, - {"m", CMDARGS_FL_OPTREQ}, - {"r", CMDARGS_FL_OPTREQ}, - {"w", CMDARGS_FL_OPTREQ}, + {"m", CMDARGS_FL_OPTREQ}, + {"r", CMDARGS_FL_OPTREQ}, + {"w", CMDARGS_FL_OPTREQ}, {NULL, 0} }; @@ -84,7 +85,7 @@ static struct arglist_s g_arglist[] = /* Print hexed_error msg and exit */ -void hexed_error(int eno, const char *fmt, ...) +void hexed_error(int eno, FAR const char *fmt, ...) { va_list va; @@ -101,39 +102,38 @@ void printhex(uint64_t i, int size) switch (size) { case WORD_64: - printf("%016llx", (unsigned long long) i); + printf("%016llx", (unsigned long long)i); break; case WORD_32: - printf("%08lx", (unsigned long) i); + printf("%08lx", (unsigned long)i); break; case WORD_16: - printf("%04x", (unsigned int) i); + printf("%04x", (unsigned int)i); break; case WORD_8: - printf("%02x", (unsigned int) i); + printf("%02x", (unsigned int)i); break; } } /* Load a Buffered File hexfile */ -int loadfile(char *name) +int loadfile(FAR char *name) { /* No file name */ if (name == NULL) { - errno = ENOENT; - return -errno; + return -ENOENT; } /* Open file */ g_hexfile = bfopen(name, "r+b"); - if (g_hexfile == NULL && errno == ENOENT) + if (g_hexfile == NULL) { /* Create file */ @@ -144,7 +144,8 @@ int loadfile(char *name) if (g_hexfile == NULL) { - return -errno; + fprintf(stderr, "ERROR: Failed to open %s: %d\n", name, g_last_error); + return -g_last_error; } bfread(g_hexfile); @@ -157,8 +158,7 @@ int savefile(FAR struct bfile_s *file) { if (file == NULL || file->fp == NULL) { - errno = EBADF; - return -errno; + return -EBADF; } bftruncate(file, file->size); @@ -167,7 +167,7 @@ int savefile(FAR struct bfile_s *file) /* Set or run a command */ -int hexcmd(FAR struct command_s *cmd, int optc, char *opt) +int hexcmd(FAR struct command_s *cmd, int optc, FAR char *opt) { switch (cmd->id) { @@ -216,7 +216,8 @@ int hexcmd(FAR struct command_s *cmd, int optc, char *opt) break; default: - RETURN_ERR(EINVAL); + g_last_error = EINVAL; + return -EINVAL; } return optc; @@ -232,56 +233,60 @@ FAR struct command_s *newcmd(int cmdno) if (cmdno >= CMD_MAX_CNT) { - errno = E2BIG; - perror(PNAME); - exit(-errno); + fprintf(stderr, "ERROR: Command buffer overflow\n"); + exit(EXIT_FAILURE); } /* Set defaults */ cmd = &g_cmdtbl[cmdno]; memset(cmd, sizeof(struct command_s), 0); - cmd->id = cmdargs->argid; - cmd->cmd = cmdargs->arg; - cmd->flags = CMD_FL_CMDLINE; + + cmd->id = g_cmdargs->argid; + cmd->cmd = g_cmdargs->arg; + cmd->flags = CMD_FL_CMDLINE; cmd->opts.word = g_wordsize; return cmd; } /* Parse the command line arguments */ -int parseargs(char *argv[]) +int parseargs(FAR char *argv[]) { FAR char *fname; FAR char *opt; int cmdcnt; int optc; - fname = NULL; - g_cmd = g_cmdtbl; /* 1st command is reserved */ - cmdcnt = 0; + fname = NULL; + cmdcnt = 0; + g_cmd = g_cmdtbl; + g_hexfile = NULL; + memset(g_cmd, sizeof(struct command_s), 0); - while (parsecmdargs(argv, g_arglist) != -1) + + while (parsecmdargs(argv, g_arglist) != EOF) { /* Set new command */ - if (cmdargs->argid > 0) + if (g_cmdargs->argid > 0) { cmdcnt++; - optc = 0; - opt = cmdargs->opt; + optc = 0; + opt = g_cmdargs->opt; g_cmd = newcmd(cmdcnt); - - /* Unknown arg */ } else { - opt = cmdargs->arg; + /* Unknown arg */ + + opt = g_cmdargs->arg; } /* Set command options */ - if ((optc = hexcmd(g_cmd, optc, opt)) < 0) + optc = hexcmd(g_cmd, optc, opt); + if (optc < 0) { /* End of range option? */ @@ -293,16 +298,18 @@ int parseargs(char *argv[]) { hexed_error(EINVAL, "Unknown option: %s\n", opt); } - - /* Might be file name */ } + + /* Might be file name */ + else if (fname == NULL) { fname = opt; loadfile(fname); - - /* Unexpected option */ } + + /* Unexpected option */ + else { hexed_error(EINVAL, "Unexpected option: %s\n", opt); @@ -330,7 +337,7 @@ int runargs(void) /* Scan for help command */ - quit = 0; + quit = 0; g_cmd = &g_cmdtbl[1]; /* Skip 1st reserved command */ for (i = 1; i < CMD_MAX_CNT && g_cmd->id > 0; i++, g_cmd++) @@ -361,7 +368,9 @@ int runargs(void) /* Save file */ - if (quit == CMD_QUIT && g_hexfile->flags == BFILE_FL_DIRTY) + if (quit == CMD_QUIT && + g_hexfile != NULL && + g_hexfile->flags == BFILE_FL_DIRTY) { savefile(g_hexfile); } @@ -383,17 +392,14 @@ int hexed_main(int argc, char *argv[]) parseargs(argv); - /* Open file */ - - if (g_hexfile == NULL) - { - loadfile(TEMP_FILE); - } - /* Run commands */ runargs(); - bfclose(g_hexfile); - (void)unlink(TEMP_FILE); + if (g_hexfile) + { + bfclose(g_hexfile); + g_hexfile = NULL; + } + return 0; } diff --git a/system/hexed/src/hexenter.c b/system/hexed/src/hexenter.c index b9f27eaab..43ce88b3a 100644 --- a/system/hexed/src/hexenter.c +++ b/system/hexed/src/hexenter.c @@ -40,6 +40,7 @@ #include #include #include + #include "bfile.h" #include "hexed.h" @@ -75,7 +76,8 @@ static int setenter(FAR struct command_s *cmd, int optc, FAR char *opt) if (opt == NULL) { - RETURN_ERR(EINVAL); + g_last_error = EINVAL; + return -EINVAL; } v = strtoll(opt, &s, 0x10); @@ -84,7 +86,8 @@ static int setenter(FAR struct command_s *cmd, int optc, FAR char *opt) if (s == opt) { - RETURN_ERR(EINVAL); + g_last_error = EINVAL; + return -EINVAL; } /* Set destination */ @@ -120,16 +123,18 @@ static int setenter(FAR struct command_s *cmd, int optc, FAR char *opt) default: break; } + cmd->opts.cnt++; cmd->opts.bytes = cmd->opts.cnt * cmd->opts.word; optc++; - - /* Buffer overflow */ } + + /* Buffer overflow */ + else { - error(E2BIG, "Enter error: too many values set\n"); - /* RETURN_ERR(E2BIG); */ + hexed_error(E2BIG, "Enter error: too many values set\n"); + g_last_error = E2BIG; } return optc; @@ -147,7 +152,8 @@ int hexenter(FAR struct command_s *cmd, int optc, char *opt) if (cmd == NULL || cmd->id != CMD_ENTER) { - RETURN_ERR(EINVAL); + g_last_error = EINVAL; + return -EINVAL; } /* Set/run enter */ diff --git a/system/hexed/src/hexhelp.c b/system/hexed/src/hexhelp.c index 4dbebdf05..bb73a98e5 100644 --- a/system/hexed/src/hexhelp.c +++ b/system/hexed/src/hexhelp.c @@ -49,17 +49,17 @@ static const char *helpmsg[] = { NULL, - " -? Shows this help screen\n", + " -h Shows this help screen\n", " -co [src] [dest] [len] Copy data from src overwriting dest for len\n" - " words\n", + " words\n", " -c [src] [dest] [len] Copy data from src to dest for len words\n", " -d [src] [len] Display data from src for len words\n", " -e [dest] [...] Enter hex values [...] at dest\n", NULL, " -i [dest] [cnt] [...] Insert hex values [...] at dest repeating cnt" - "\n times. Defaults to 0 for empty hex values.\n", + "\n times. Defaults to 0 for empty hex values.\n", " -mo [src] [dest] [len] Move data from src overwriting dest for len\n" - " words\n", + " words\n", " -m [src] [dest] [len] Move data from src to dest for len words\n", " -r [src] [len] Remove data from src for len words\n", " -w [bytes] Set the word size in bytes: 1 = 8 bits\n" @@ -71,7 +71,13 @@ static const char *helpmsg[] = * Private Functions ****************************************************************************/ -/* Display the help page */ +/**************************************************************************** + * Name: runhelp + * + * Description: + * Display the help page + * + ****************************************************************************/ static int runhelp(FAR struct command_s *cmd) { @@ -99,7 +105,13 @@ static int runhelp(FAR struct command_s *cmd) * Public Functions ****************************************************************************/ -/* Help command */ +/**************************************************************************** + * Name: hexhelp + * + * Description: + * Help command + * + ****************************************************************************/ int hexhelp(FAR struct command_s *cmd, int optc, char *opt) { @@ -107,7 +119,8 @@ int hexhelp(FAR struct command_s *cmd, int optc, char *opt) if (cmd == NULL || cmd->id != CMD_HELP) { - RETURN_ERR(EINVAL); + g_last_error = EINVAL; + return -EINVAL; } /* Run help */ diff --git a/system/hexed/src/hexinsert.c b/system/hexed/src/hexinsert.c index 149054d99..5e407244a 100644 --- a/system/hexed/src/hexinsert.c +++ b/system/hexed/src/hexinsert.c @@ -91,7 +91,8 @@ static int setinsert(FAR struct command_s *cmd, int optc, char *opt) if (opt == NULL) { - RETURN_ERR(EINVAL); + g_last_error = EINVAL; + return -EINVAL; } v = strtoll(opt, &s, 0x10); @@ -100,7 +101,8 @@ static int setinsert(FAR struct command_s *cmd, int optc, char *opt) if (s == opt) { - RETURN_ERR(EINVAL); + g_last_error = EINVAL; + return -EINVAL; } /* Set options */ @@ -155,8 +157,8 @@ static int setinsert(FAR struct command_s *cmd, int optc, char *opt) } else { - error(E2BIG, "Insert error: too many values set\n"); - /* RETURN_ERR(E2BIG); */ + hexed_error(E2BIG, "Insert error: too many values set\n"); + g_last_error = E2BIG; } } @@ -175,7 +177,8 @@ int hexinsert(FAR struct command_s *cmd, int optc, char *opt) if (cmd == NULL || cmd->id != CMD_INSERT) { - RETURN_ERR(EINVAL); + g_last_error = EINVAL; + return -EINVAL; } /* Set/run insert */ diff --git a/system/hexed/src/hexmove.c b/system/hexed/src/hexmove.c index 1c450dd95..6a450c5ac 100644 --- a/system/hexed/src/hexmove.c +++ b/system/hexed/src/hexmove.c @@ -112,7 +112,8 @@ static int runmove(FAR struct command_s *cmd) if (cmd->opts.src > g_hexfile->size) { - RETURN_ERR(EINVAL); + g_last_error = EINVAL; + return -EINVAL; } switch (cmd->id) @@ -146,7 +147,8 @@ static int setmove(FAR struct command_s *cmd, int optc, char *opt) if (opt == NULL) { - RETURN_ERR(EFAULT); + g_last_error = EFAULT; + return -EFAULT; } v = strtoll(opt, &s, 0x10); @@ -155,7 +157,8 @@ static int setmove(FAR struct command_s *cmd, int optc, char *opt) if (s == opt) { - RETURN_ERR(EINVAL); + g_last_error = EINVAL; + return -EINVAL; } /* Set options */ @@ -188,7 +191,8 @@ static int setmove(FAR struct command_s *cmd, int optc, char *opt) default: /* Too many options specified */ - RETURN_ERR(E2BIG); + g_last_error = E2BIG; + return -E2BIG; } return optc; @@ -206,7 +210,8 @@ int hexmove(FAR struct command_s *cmd, int optc, char *opt) if (cmd == NULL || (cmd->id != CMD_MOVE && cmd->id != CMD_MOVE_OVER)) { - RETURN_ERR(EINVAL); + g_last_error = EINVAL; + return -EINVAL; } /* Ret/run move */ diff --git a/system/hexed/src/hexremove.c b/system/hexed/src/hexremove.c index c3a203e70..d66b9b0b4 100644 --- a/system/hexed/src/hexremove.c +++ b/system/hexed/src/hexremove.c @@ -75,7 +75,8 @@ static int setremove(FAR struct command_s *cmd, int optc, char *opt) if (opt == NULL) { - RETURN_ERR(EINVAL); + g_last_error = EINVAL; + return -EINVAL; } v = strtoll(opt, &s, 0x10); @@ -84,7 +85,8 @@ static int setremove(FAR struct command_s *cmd, int optc, char *opt) if (s == opt) { - RETURN_ERR(EINVAL); + g_last_error = EINVAL; + return -EINVAL; } /* Set options */ @@ -109,7 +111,8 @@ static int setremove(FAR struct command_s *cmd, int optc, char *opt) default: /* Too many options specified */ - RETURN_ERR(E2BIG); + g_last_error = E2BIG; + return -E2BIG; } return optc; @@ -127,7 +130,8 @@ int hexremove(FAR struct command_s *cmd, int optc, char *opt) if (cmd == NULL || cmd->id != CMD_REMOVE) { - RETURN_ERR(EINVAL); + g_last_error = EINVAL; + return -EINVAL; } /* Set/run remove */ diff --git a/system/hexed/src/hexword.c b/system/hexed/src/hexword.c index 1108cd12e..ddb8950e8 100644 --- a/system/hexed/src/hexword.c +++ b/system/hexed/src/hexword.c @@ -65,7 +65,8 @@ static int setword(FAR struct command_s *cmd, int optc, char *opt) if (opt == NULL) { - RETURN_ERR(EFAULT); + g_last_error = EFAULT; + return -EFAULT; } v = strtoll(opt, &s, 0x10); @@ -74,7 +75,8 @@ static int setword(FAR struct command_s *cmd, int optc, char *opt) if (s == opt) { - RETURN_ERR(EINVAL); + g_last_error = EINVAL; + return -EINVAL; } /* Set global word size */ @@ -104,7 +106,8 @@ static int setword(FAR struct command_s *cmd, int optc, char *opt) { /* Too many options specified */ - RETURN_ERR(E2BIG); + g_last_error = E2BIG; + return -E2BIG; } return optc; @@ -122,7 +125,8 @@ int hexword(FAR struct command_s *cmd, int optc, char *opt) if (cmd == NULL || cmd->id != CMD_WORD) { - RETURN_ERR(EINVAL); + g_last_error = EINVAL; + return -EINVAL; } /* Set word size */