diff simtool/dispatch.c @ 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 141489d31667
line wrap: on
line diff
--- 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);
+}