summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGregory Nutt <gnutt@nuttx.org>2013-11-01 07:15:14 -0600
committerGregory Nutt <gnutt@nuttx.org>2013-11-01 07:15:14 -0600
commit8148ce2957824b783b7ec81919b891f220c0237f (patch)
tree87a3f75d9eb988361cac0d8bd35771e9268b85de
parentb6814d7a4a44f58992704e2b85502ccc5dd0e331 (diff)
downloadnuttx-8148ce2957824b783b7ec81919b891f220c0237f.tar.gz
nuttx-8148ce2957824b783b7ec81919b891f220c0237f.tar.bz2
nuttx-8148ce2957824b783b7ec81919b891f220c0237f.zip
Add skip= and count= options to the NSH hexdump command. From Ken Pettit
-rw-r--r--apps/ChangeLog.txt2
-rw-r--r--apps/nshlib/Kconfig4
-rw-r--r--apps/nshlib/nsh_dbgcmds.c128
-rw-r--r--apps/nshlib/nsh_parse.c4
4 files changed, 111 insertions, 27 deletions
diff --git a/apps/ChangeLog.txt b/apps/ChangeLog.txt
index f47c1b082..949ae7414 100644
--- a/apps/ChangeLog.txt
+++ b/apps/ChangeLog.txt
@@ -716,4 +716,6 @@
(2013-10-30).
* apps/platform/mikroe-stm32f4: Now supports storage of configuration
data. From Ken Pettit (2013-10-30).
+ * apps/nshlib/nsh_dbgcmds.c and others: Add skip= and count=
+ options to the hexdump command. From Ken Pettit (2013-11-1).
diff --git a/apps/nshlib/Kconfig b/apps/nshlib/Kconfig
index dcff7cf58..b549a6c2a 100644
--- a/apps/nshlib/Kconfig
+++ b/apps/nshlib/Kconfig
@@ -238,6 +238,10 @@ config NSH_CODECS_BUFSIZE
int "File buffer size used by CODEC commands"
default 128
+config NSH_CMDOPT_HEXDUMP
+ bool "hexdump: Enable 'skip' and 'count' parameters"
+ default n
+
endmenu
config NSH_FILEIOSIZE
diff --git a/apps/nshlib/nsh_dbgcmds.c b/apps/nshlib/nsh_dbgcmds.c
index 85a4ccb9c..4b8646ef7 100644
--- a/apps/nshlib/nsh_dbgcmds.c
+++ b/apps/nshlib/nsh_dbgcmds.c
@@ -369,10 +369,16 @@ int cmd_hexdump(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
{
uint8_t buffer[IOBUFFERSIZE];
char msg[32];
- int position;
+ off_t position;
int fd;
int ret = OK;
-
+#ifdef CONFIG_NSH_CMDOPT_HEXDUMP
+ off_t skip = 0;
+ off_t count = 0xfffffff;
+ off_t dumpbytes;
+ int x;
+#endif
+
/* Open the file for reading */
fd = open(argv[1], O_RDONLY);
@@ -381,33 +387,101 @@ int cmd_hexdump(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
nsh_output(vtbl, g_fmtcmdfailed, "hexdump", "open", NSH_ERRNO);
return ERROR;
}
-
+
+#ifdef CONFIG_NSH_CMDOPT_HEXDUMP
+ for (x = 2; x < argc; x++)
+ {
+ if (strncmp(argv[x], "skip=", 5) == 0)
+ {
+ skip = atoi(&argv[x][5]);
+ }
+ else if (strncmp(argv[x], "count=", 6) == 0)
+ {
+ count = atoi(&argv[x][6]);
+ }
+ }
+#endif
+
position = 0;
for (;;)
- {
- int nbytesread = read(fd, buffer, IOBUFFERSIZE);
-
- /* Check for read errors */
-
- if (nbytesread < 0)
- {
- int errval = errno;
- nsh_output(vtbl, g_fmtcmdfailed, "hexdump", "read", NSH_ERRNO_OF(errval));
- ret = ERROR;
- break;
- }
- else if (nbytesread > 0)
- {
- snprintf(msg, sizeof(msg), "%s at %08x", argv[1], position);
- nsh_dumpbuffer(vtbl, msg, buffer, nbytesread);
- position += nbytesread;
- }
- else
- {
- break; // EOF
- }
- }
-
+ {
+ int nbytesread = read(fd, buffer, IOBUFFERSIZE);
+
+ /* Check for read errors */
+
+ if (nbytesread < 0)
+ {
+ int errval = errno;
+ nsh_output(vtbl, g_fmtcmdfailed, "hexdump", "read",
+ NSH_ERRNO_OF(errval));
+ ret = ERROR;
+ break;
+ }
+ else if (nbytesread > 0)
+ {
+#ifdef CONFIG_NSH_CMDOPT_HEXDUMP
+ if (position < skip)
+ {
+ /* Skip bytes until we reach the skip point */
+
+ position += nbytesread;
+ if (position > skip)
+ {
+ dumpbytes = position - skip;
+ if (dumpbytes > count)
+ {
+ dumpbytes = count;
+ }
+
+ snprintf(msg, sizeof(msg), "%s at %08x", argv[1], skip);
+ nsh_dumpbuffer(vtbl, msg,
+ &buffer[nbytesread - (position-skip)],
+ dumpbytes);
+
+ if (count > dumpbytes)
+ {
+ count -= dumpbytes;
+ }
+ else
+ {
+ break;
+ }
+ }
+
+ /* Don't print if we are in skip mode */
+
+ continue;
+ }
+
+ /* Limit dumpbuffer to count if less than a full buffer needed */
+
+ if (nbytesread > count)
+ {
+ nbytesread = count;
+ }
+#endif
+
+ snprintf(msg, sizeof(msg), "%s at %08x", argv[1], position);
+ nsh_dumpbuffer(vtbl, msg, buffer, nbytesread);
+ position += nbytesread;
+
+#ifdef CONFIG_NSH_CMDOPT_HEXDUMP
+ if (count > nbytesread)
+ {
+ count -= nbytesread;
+ }
+ else
+ {
+ break;
+ }
+#endif
+ }
+ else
+ {
+ break; // EOF
+ }
+ }
+
(void)close(fd);
return ret;
}
diff --git a/apps/nshlib/nsh_parse.c b/apps/nshlib/nsh_parse.c
index b293f4983..a58d0b03a 100644
--- a/apps/nshlib/nsh_parse.c
+++ b/apps/nshlib/nsh_parse.c
@@ -245,7 +245,11 @@ static const struct cmdmap_s g_cmdmap[] =
#if CONFIG_NFILE_DESCRIPTORS > 0
#ifndef CONFIG_NSH_DISABLE_HEXDUMP
+#ifndef CONFIG_NSH_CMDOPT_HEXDUMP
{ "hexdump", cmd_hexdump, 2, 2, "<file or device>" },
+#else
+ { "hexdump", cmd_hexdump, 2, 4, "<file or device> [skip=<bytes>] [count=<bytes>]" },
+#endif
#endif
#endif