changeset 13:f4fc449a64ea

target-utils libcommon infrastructure for interactive commands
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Fri, 03 May 2013 06:42:03 +0000
parents 165040ce4929
children f0501c4a8790
files target-utils/include/cmdtab.h target-utils/libcommon/Makefile target-utils/libcommon/cmd_r16.c target-utils/libcommon/cmd_r32.c target-utils/libcommon/cmd_r8.c target-utils/libcommon/dispatch.c target-utils/libcommon/hexarg.c target-utils/libcommon/parseargs.c
diffstat 8 files changed, 182 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/target-utils/include/cmdtab.h	Fri May 03 06:42:03 2013 +0000
@@ -0,0 +1,6 @@
+/* this structure is used for interactive command dispatch */
+
+struct cmdtab {
+	char	*cmd;
+	void	(*func)();
+};
--- a/target-utils/libcommon/Makefile	Wed May 01 20:01:41 2013 +0000
+++ b/target-utils/libcommon/Makefile	Fri May 03 06:42:03 2013 +0000
@@ -4,7 +4,8 @@
 AR=	arm-elf-ar
 RANLIB=	arm-elf-ranlib
 
-OBJS=	cmdentry.o serio.o uartsel.o
+OBJS=	cmdentry.o dispatch.o hexarg.o parseargs.o serio.o uartsel.o \
+	cmd_r8.o cmd_r16.o cmd_r32.o
 
 all:	libcommon.a
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/target-utils/libcommon/cmd_r16.c	Fri May 03 06:42:03 2013 +0000
@@ -0,0 +1,26 @@
+/*
+ * r16 hexaddr -- read a 16-bit register or memory location
+ */
+
+#include <sys/types.h>
+#include "types.h"
+
+void
+cmd_r16(argbulk)
+	char *argbulk;
+{
+	char *argv[2];
+	u_long addr;
+
+	if (parse_args(argbulk, 1, 1, argv, 0) < 0)
+		return;
+	if (parse_hexarg(argv[0], 8, &addr) < 0) {
+		printf("ERROR: argument must be a valid 32-bit hex address\n");
+		return;
+	}
+	if (addr & 1) {
+		printf("ERROR: unaligned address\n");
+		return;
+	}
+	printf("%04X\n", *(volatile u16 *)addr);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/target-utils/libcommon/cmd_r32.c	Fri May 03 06:42:03 2013 +0000
@@ -0,0 +1,26 @@
+/*
+ * r32 hexaddr -- read a 32-bit register or memory location
+ */
+
+#include <sys/types.h>
+#include "types.h"
+
+void
+cmd_r32(argbulk)
+	char *argbulk;
+{
+	char *argv[2];
+	u_long addr;
+
+	if (parse_args(argbulk, 1, 1, argv, 0) < 0)
+		return;
+	if (parse_hexarg(argv[0], 8, &addr) < 0) {
+		printf("ERROR: argument must be a valid 32-bit hex address\n");
+		return;
+	}
+	if (addr & 3) {
+		printf("ERROR: unaligned address\n");
+		return;
+	}
+	printf("%08X\n", *(volatile u32 *)addr);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/target-utils/libcommon/cmd_r8.c	Fri May 03 06:42:03 2013 +0000
@@ -0,0 +1,22 @@
+/*
+ * r8 hexaddr -- read an 8-bit register or memory location
+ */
+
+#include <sys/types.h>
+#include "types.h"
+
+void
+cmd_r8(argbulk)
+	char *argbulk;
+{
+	char *argv[2];
+	u_long addr;
+
+	if (parse_args(argbulk, 1, 1, argv, 0) < 0)
+		return;
+	if (parse_hexarg(argv[0], 8, &addr) < 0) {
+		printf("ERROR: argument must be a valid 32-bit hex address\n");
+		return;
+	}
+	printf("%02X\n", *(volatile u8 *)addr);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/target-utils/libcommon/dispatch.c	Fri May 03 06:42:03 2013 +0000
@@ -0,0 +1,32 @@
+/*
+ * This module implements the dispatch of interactively entered
+ * commands to their respective implementation functions via cmdtab.
+ */
+
+#include "cmdtab.h"
+
+extern char command[];
+extern struct cmdtab cmdtab[];
+
+void
+command_dispatch()
+{
+	char *cp, *np;
+	struct cmdtab *tp;
+
+	for (cp = command; *cp == ' '; cp++)
+		;
+	if (!*cp)
+		return;
+	for (np = cp; *cp && *cp != ' '; cp++)
+		;
+	if (*cp)
+		*cp++ = '\0';
+	for (tp = cmdtab; tp->cmd; tp++)
+		if (!strcmp(tp->cmd, np))
+			break;
+	if (tp->func)
+		tp->func(cp);
+	else
+		printf("ERROR: unknown or unimplemented command\n");
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/target-utils/libcommon/hexarg.c	Fri May 03 06:42:03 2013 +0000
@@ -0,0 +1,29 @@
+/*
+ * Many commands take hex arguments.  This module contains the parse_hexarg()
+ * function, which is a wrapper around strtoul that performs some additional
+ * checks.
+ */
+
+#include <sys/types.h>
+#include <ctype.h>
+#include <stdlib.h>
+
+parse_hexarg(arg, maxdigits, valp)
+	char *arg;
+	int maxdigits;
+	u_long *valp;
+{
+	char *cp = arg, *bp;
+	int len;
+
+	if (cp[0] == '0' && (cp[1] == 'x' || cp[1] == 'X'))
+		cp += 2;
+	for (bp = cp; *cp; cp++)
+		if (!isxdigit(*cp))
+			return(-1);
+	len = cp - bp;
+	if (len < 1 || len > maxdigits)
+		return(-1);
+	*valp = strtoul(arg, 0, 16);
+	return(0);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/target-utils/libcommon/parseargs.c	Fri May 03 06:42:03 2013 +0000
@@ -0,0 +1,39 @@
+/*
+ * This module contains the parse_args() function, which parses the "rest"
+ * part of an entered command into an argc/argv-style argument array.
+ */
+
+parse_args(unparsed, minargs, maxargs, argv, argcp)
+	char *unparsed;
+	int minargs, maxargs;
+	char **argv;
+	int *argcp;
+{
+	int argc;
+	char *cp;
+
+	argc = 0;
+	for (cp = unparsed; ; ) {
+		while (*cp == ' ')
+			cp++;
+		if (!*cp)
+			break;
+		if (argc >= maxargs) {
+			printf("ERROR: too many arguments\n");
+			return(-1);
+		}
+		argv[argc++] = cp;
+		while (*cp && *cp != ' ')
+			cp++;
+		if (*cp)
+			*cp++ = '\0';
+	}
+	if (argc < minargs) {
+		printf("ERROR: too few arguments\n");
+		return(-1);
+	}
+	argv[argc] = 0;
+	if (argcp)
+		*argcp = argc;
+	return(0);
+}