From f0dfe2d1a30f496a0c008bb54d4eaff9e62ac5a7 Mon Sep 17 00:00:00 2001 From: ac-stef Date: Wed, 31 Aug 2016 18:53:58 +0200 Subject: [PATCH] pass arguments to and deliver results from script files When a script file is executed, any additional arguments are passed as execarg1..execargX to the script. The number of arguments is in execnumargs and if the script in the file sets the value of execresult, the exec command returns that value. Example: If there is a file testscript.cfg with this content: execresult = (* $execarg1 $execarg2) then the command echo (exec testscript.cfg 6 7) will output "42". DOCREF exec --- source/src/command.cpp | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/source/src/command.cpp b/source/src/command.cpp index fbd21762..e8f4a542 100644 --- a/source/src/command.cpp +++ b/source/src/command.cpp @@ -1060,7 +1060,28 @@ void exec(const char *cfgfile) { if(!execfile(cfgfile)) conoutf("could not read \"%s\"", cfgfile); } -COMMAND(exec, "s"); + +COMMANDF(exec, "v", (char **args, int numargs) +{ + defformatstring(buf)("%d", numargs - 1); + push("execnumargs", buf); + for(int i = 1; i < 24; i++) // MAXWORDS - 1 + { + formatstring(buf)("execarg%d", i); + push(buf, i < numargs ? args[i] : ""); + } + push("execresult", ""); + exec(numargs > 0 ? args[0] : ""); + const char *res = getalias("execresult"); + if(res) result(res); + pop("execresult"); + for(int i = 23; i > 0; i--) + { + formatstring(buf)("execarg%d", i); + pop(buf); + } + pop("execnumargs"); +}); COMMANDF(execute, "s", (char *s) { intret(execute(s)); });