changeset 29:dacf45e3d20f

loadtool: scripting functionality implemented
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sat, 04 May 2013 07:15:51 +0000
parents 768a3d012931
children af6d763badfb
files loadtools/Makefile loadtools/ltdispatch.c loadtools/ltmain.c loadtools/ltscript.c
diffstat 4 files changed, 71 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/loadtools/Makefile	Sat May 04 06:39:05 2013 +0000
+++ b/loadtools/Makefile	Sat May 04 07:15:51 2013 +0000
@@ -6,7 +6,8 @@
 		srecreader.o ttypassthru.o
 
 LOADTOOL_OBJS=	defpath.o hexdecode.o hwparam.o ltdispatch.o ltexit.o ltmain.o \
-		ltpassthru.o romload.o sercomm.o srecreader.o tpinterf.o
+		ltpassthru.o ltscript.o romload.o sercomm.o srecreader.o \
+		tpinterf.o
 
 all:	${PROGS}
 
--- a/loadtools/ltdispatch.c	Sat May 04 06:39:05 2013 +0000
+++ b/loadtools/ltdispatch.c	Sat May 04 07:15:51 2013 +0000
@@ -8,8 +8,7 @@
 #include <strings.h>
 #include <stdlib.h>
 
-extern char loadtool_command[];
-
+extern int cmd_exec();
 extern int cmd_exit();
 extern int loadtool_cmd_passthru();
 
@@ -20,6 +19,7 @@
 	int (*func)();
 } cmdtab[] = {
 	{"dump", 2, 2, loadtool_cmd_passthru},
+	{"exec", 1, 1, cmd_exec},
 	{"exit", 0, 1, cmd_exit},
 	{"quit", 0, 1, cmd_exit},
 	{"r8", 1, 1, loadtool_cmd_passthru},
@@ -31,16 +31,19 @@
 	{0, 0, 0, 0}
 };
 
-loadtool_dispatch_cmd()
+loadtool_dispatch_cmd(cmd, is_script)
+	char *cmd;
 {
 	char *argv[10];
 	char *cp, **ap;
 	struct cmdtab *tp;
 
-	for (cp = loadtool_command; isspace(*cp); cp++)
+	for (cp = cmd; isspace(*cp); cp++)
 		;
 	if (!*cp || *cp == '#')
 		return(0);
+	if (is_script)
+		printf("Script command: %s\n", cp);
 	argv[0] = cp;
 	while (*cp && !isspace(*cp))
 		cp++;
--- a/loadtools/ltmain.c	Sat May 04 06:39:05 2013 +0000
+++ b/loadtools/ltmain.c	Sat May 04 07:15:51 2013 +0000
@@ -12,17 +12,16 @@
 extern char *target_ttydev;
 extern struct srecreader iramimage;
 extern char default_loadagent_image[];
-
+extern char hw_init_script[];
 extern void (*default_exit)();
 
-char loadtool_command[512];
-
 main(argc, argv)
 	char **argv;
 {
 	extern char *optarg;
 	extern int optind;
 	int c;
+	char command[512];
 
 	while ((c = getopt(argc, argv, "a:h:H:i:")) != EOF)
 		switch (c) {
@@ -55,13 +54,18 @@
 	putchar('\n');
 	if (tpinterf_pass_output(1) < 0)
 		exit(1);
+	putchar('\n');
+	if (hw_init_script) {
+		printf("Executing init script %s\n", hw_init_script);
+		loadtool_exec_script(hw_init_script);
+	}
 	for (;;) {
 		if (isatty(0)) {
 			fputs("loadtool> ", stdout);
 			fflush(stdout);
 		}
-		if (!fgets(loadtool_command, sizeof loadtool_command, stdin))
+		if (!fgets(command, sizeof command, stdin))
 			default_exit();
-		loadtool_dispatch_cmd();
+		loadtool_dispatch_cmd(command, 0);
 	}
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/loadtools/ltscript.c	Sat May 04 07:15:51 2013 +0000
@@ -0,0 +1,53 @@
+/*
+ * This module contains the code that implements the loadtool scripting
+ * functionality: init-script setting and the exec command.
+ */
+
+#include <sys/param.h>
+#include <stdio.h>
+#include <string.h>
+#include <strings.h>
+#include <stdlib.h>
+
+extern char default_helpers_dir[];
+
+loadtool_exec_script(script_name)
+	char *script_name;
+{
+	char pathbuf[MAXPATHLEN], *openfname;
+	FILE *f;
+	char linebuf[512], *cp;
+	int lineno, retval = 0;
+
+	if (index(script_name, '/'))
+		openfname = script_name;
+	else {
+		sprintf(pathbuf, "%s/%s", default_helpers_dir, script_name);
+		openfname = pathbuf;
+	}
+	f = fopen(openfname, "r");
+	if (!f) {
+		perror(openfname);
+		return(-1);
+	}
+	for (lineno = 1; fgets(linebuf, sizeof linebuf, f); lineno++) {
+		cp = index(linebuf, '\n');
+		if (!cp) {
+			fprintf(stderr, "%s line %d: missing newline\n",
+				openfname, lineno);
+			fclose(f);
+			return(-1);
+		}
+		retval = loadtool_dispatch_cmd(linebuf, 1);
+		if (retval)
+			break;
+	}
+	fclose(f);
+	return(retval);
+}
+
+cmd_exec(argc, argv)
+	char **argv;
+{
+	return loadtool_exec_script(argv[1]);
+}