# HG changeset patch # User Mychaela Falconia # Date 1611859344 0 # Node ID b391204d3cd5752eb04e143f203eca6efa489a14 # Parent f18704e91393d9f65b71bf8bf5ceaa6e15079579 fc-simtool: add scripting facility in the form of exec command diff -r f18704e91393 -r b391204d3cd5 simtool/Makefile --- 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} diff -r f18704e91393 -r b391204d3cd5 simtool/dispatch.c --- 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++; diff -r f18704e91393 -r b391204d3cd5 simtool/main.c --- 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); } } diff -r f18704e91393 -r b391204d3cd5 simtool/script.c --- /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 +#include +#include +#include + +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); +}