changeset 124:6c4567dd8946

fc-simtool: add non-interactive one-shot command (or script) mode
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 28 Jan 2021 18:56:34 +0000
parents b391204d3cd5
children ddff76088d12
files simtool/Makefile simtool/dispatch.c simtool/exit.c simtool/main.c
diffstat 4 files changed, 56 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/simtool/Makefile	Thu Jan 28 18:42:24 2021 +0000
+++ b/simtool/Makefile	Thu Jan 28 18:56:34 2021 +0000
@@ -2,7 +2,7 @@
 CFLAGS=	-O2 -I/usr/include/PCSC
 PROG=	fc-simtool
 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 \
+	dispatch.o exit.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 script.o select.o sysmo.o telsum.o writecmd.o writeops.o
 INSTBIN=/opt/freecalypso/bin
--- a/simtool/dispatch.c	Thu Jan 28 18:42:24 2021 +0000
+++ b/simtool/dispatch.c	Thu Jan 28 18:56:34 2021 +0000
@@ -2,15 +2,11 @@
  * This module implements the command dispatch for fc-simtool
  */
 
-#include <sys/types.h>
 #include <ctype.h>
 #include <stdio.h>
 #include <string.h>
 #include <strings.h>
 #include <stdlib.h>
-#include <pcsclite.h>
-#include <winscard.h>
-#include "globals.h"
 
 extern int cmd_change_chv();
 extern int cmd_disable_chv();
@@ -43,13 +39,7 @@
 extern int cmd_verify_chv();
 
 extern int display_sim_resp_in_hex();
-
-cmd_exit()
-{
-	SCardDisconnect(hCard, SCARD_UNPOWER_CARD);
-	SCardReleaseContext(hContext);
-	exit(0);
-}
+extern int good_exit();
 
 static struct cmdtab {
 	char *cmd;
@@ -66,7 +56,7 @@
 	{"enable-chv", 1, 1, cmd_enable_chv},
 	{"enable-pin", 1, 1, cmd_enable_chv},
 	{"exec", 1, 1, cmd_exec},
-	{"exit", 0, 0, cmd_exit},
+	{"exit", 0, 0, good_exit},
 	{"fix-sysmo-msisdn", 0, 0, cmd_fix_sysmo_msisdn},
 	{"iccid", 0, 0, cmd_iccid},
 	{"imsi", 0, 0, cmd_imsi},
@@ -78,7 +68,7 @@
 	{"pb-update", 2, 2, cmd_pb_update},
 	{"pb-update-imm", 3, 4, cmd_pb_update_imm},
 	{"pb-update-imm-hex", 4, 4, cmd_pb_update_imm_hex},
-	{"quit", 0, 0, cmd_exit},
+	{"quit", 0, 0, good_exit},
 	{"readbin", 2, 2, cmd_readbin},
 	{"readef", 1, 1, cmd_readef},
 	{"readrec", 1, 2, cmd_readrec},
@@ -169,3 +159,26 @@
 	*ap = 0;
 	return tp->func(ap - argv, argv);
 }
+
+dispatch_ready_argv(argc, argv)
+	char **argv;
+{
+	struct cmdtab *tp;
+
+	for (tp = cmdtab; tp->cmd; tp++)
+		if (!strcmp(tp->cmd, argv[0]))
+			break;
+	if (!tp->func) {
+		fprintf(stderr, "error: no such command\n");
+		return(-1);
+	}
+	if (argc - 1 > tp->maxargs) {
+		fprintf(stderr, "error: too many arguments\n");
+		return(-1);
+	}
+	if (argc - 1 < tp->minargs) {
+		fprintf(stderr, "error: too few arguments\n");
+		return(-1);
+	}
+	return tp->func(argc, argv);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/simtool/exit.c	Thu Jan 28 18:56:34 2021 +0000
@@ -0,0 +1,20 @@
+#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <pcsclite.h>
+#include <winscard.h>
+#include "globals.h"
+
+good_exit()
+{
+	SCardDisconnect(hCard, SCARD_UNPOWER_CARD);
+	SCardReleaseContext(hContext);
+	exit(0);
+}
+
+error_exit()
+{
+	SCardDisconnect(hCard, SCARD_UNPOWER_CARD);
+	SCardReleaseContext(hContext);
+	exit(1);
+}
--- a/simtool/main.c	Thu Jan 28 18:42:24 2021 +0000
+++ b/simtool/main.c	Thu Jan 28 18:56:34 2021 +0000
@@ -9,19 +9,27 @@
 	char **argv;
 {
 	char command[512];
+	int rc;
 
 	setup_pcsc_context();
 	get_reader_name();
 	printf("Card reader name: %s\n", reader_name_buf);
 	connect_to_card();
 	retrieve_atr();
+	if (argc >= 2) {
+		rc = dispatch_ready_argv(argc - 1, argv + 1);
+		if (rc)
+			error_exit();
+		else
+			good_exit();
+	}
 	for (;;) {
 		if (isatty(0)) {
 			fputs("simtool> ", stdout);
 			fflush(stdout);
 		}
 		if (!fgets(command, sizeof command, stdin))
-			cmd_exit();
+			good_exit();
 		simtool_dispatch_cmd(command, 0);
 	}
 }