changeset 123:b391204d3cd5

fc-simtool: add scripting facility in the form of exec command
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 28 Jan 2021 18:42:24 +0000
parents f18704e91393
children 6c4567dd8946
files simtool/Makefile simtool/dispatch.c simtool/main.c simtool/script.c
diffstat 4 files changed, 44 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/simtool/Makefile	Thu Jan 28 05:12:10 2021 +0000
+++ b/simtool/Makefile	Thu Jan 28 18:42:24 2021 +0000
@@ -4,7 +4,7 @@
 OBJS=	alpha_decode.o alpha_valid.o apdu.o atr.o cardconnect.o chv.o \
 	dispatch.o globals.o hexdump.o hexread.o hlread.o main.o names.o \
 	pbcommon.o pbdump.o pberase.o pbupdate.o readcmd.o readops.o \
-	saverestore.o select.o sysmo.o telsum.o writecmd.o writeops.o
+	saverestore.o script.o select.o sysmo.o telsum.o writecmd.o writeops.o
 INSTBIN=/opt/freecalypso/bin
 
 all:	${PROG}
--- a/simtool/dispatch.c	Thu Jan 28 05:12:10 2021 +0000
+++ b/simtool/dispatch.c	Thu Jan 28 18:42:24 2021 +0000
@@ -15,6 +15,7 @@
 extern int cmd_change_chv();
 extern int cmd_disable_chv();
 extern int cmd_enable_chv();
+extern int cmd_exec();
 extern int cmd_fix_sysmo_msisdn();
 extern int cmd_iccid();
 extern int cmd_imsi();
@@ -64,6 +65,7 @@
 	{"disable-pin", 1, 1, cmd_disable_chv},
 	{"enable-chv", 1, 1, cmd_enable_chv},
 	{"enable-pin", 1, 1, cmd_enable_chv},
+	{"exec", 1, 1, cmd_exec},
 	{"exit", 0, 0, cmd_exit},
 	{"fix-sysmo-msisdn", 0, 0, cmd_fix_sysmo_msisdn},
 	{"iccid", 0, 0, cmd_iccid},
@@ -101,7 +103,7 @@
 	{0, 0, 0, 0}
 };
 
-simtool_dispatch_cmd(cmd)
+simtool_dispatch_cmd(cmd, is_script)
 	char *cmd;
 {
 	char *argv[10];
@@ -112,6 +114,8 @@
 		;
 	if (!*cp || *cp == '#')
 		return(0);
+	if (is_script)
+		printf("Script command: %s\n", cp);
 	argv[0] = cp;
 	while (*cp && !isspace(*cp))
 		cp++;
--- a/simtool/main.c	Thu Jan 28 05:12:10 2021 +0000
+++ b/simtool/main.c	Thu Jan 28 18:42:24 2021 +0000
@@ -22,6 +22,6 @@
 		}
 		if (!fgets(command, sizeof command, stdin))
 			cmd_exit();
-		simtool_dispatch_cmd(command);
+		simtool_dispatch_cmd(command, 0);
 	}
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/simtool/script.c	Thu Jan 28 18:42:24 2021 +0000
@@ -0,0 +1,37 @@
+/*
+ * This module implements the exec command, which is our scripting facility.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+
+cmd_exec(argc, argv)
+	char **argv;
+{
+	FILE *f;
+	char linebuf[512], *cp;
+	int lineno, retval = 0;
+
+	f = fopen(argv[1], "r");
+	if (!f) {
+		perror(argv[1]);
+		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",
+				argv[1], lineno);
+			fclose(f);
+			return(-1);
+		}
+		*cp = '\0';
+		retval = simtool_dispatch_cmd(linebuf, 1);
+		if (retval)
+			break;
+	}
+	fclose(f);
+	return(retval);
+}