# HG changeset patch # User Mychaela Falconia # Date 1611860194 0 # Node ID 6c4567dd89461ccf4b3b3287205e8142feab303a # Parent b391204d3cd5752eb04e143f203eca6efa489a14 fc-simtool: add non-interactive one-shot command (or script) mode diff -r b391204d3cd5 -r 6c4567dd8946 simtool/Makefile --- 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 diff -r b391204d3cd5 -r 6c4567dd8946 simtool/dispatch.c --- 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 #include #include #include #include #include -#include -#include -#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); +} diff -r b391204d3cd5 -r 6c4567dd8946 simtool/exit.c --- /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 +#include +#include +#include +#include +#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); +} diff -r b391204d3cd5 -r 6c4567dd8946 simtool/main.c --- 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); } }