From: martin.holst@gmail.com Date: Thu, 19 Sep 2013 19:21:12 +0000 (+0000) Subject: Implemented parameter passing into script, added a minimal getop-parser, added an... X-Git-Tag: v1.0.0~80^2~3 X-Git-Url: http://git.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/commitdiff_plain/30a5d3552930ab77d489484fcd47f296b69dfe34 Implemented parameter passing into script, added a minimal getop-parser, added an example script which takes parameters --- diff --git a/client/cmdscript.c b/client/cmdscript.c index 74d2228a..e4666dbf 100644 --- a/client/cmdscript.c +++ b/client/cmdscript.c @@ -228,8 +228,8 @@ int CmdRun(const char *Cmd) // memset(cmd_name, 0, 32); // sscanf(Cmd, "%31s%n", cmd_name, &len); - char script_name[128]; - char arguments[256]; + char script_name[128] = {0}; + char arguments[256] = {0}; int name_len = 0; int arg_len = 0; @@ -244,7 +244,7 @@ int CmdRun(const char *Cmd) char buf[256]; snprintf(buf, sizeof buf, "./scripts/%s%s", script_name, suffix); - printf("---Executing: %s with arguments '%s'\n",buf,arguments); + printf("--- Executing: %s, args'%s'\n",buf,arguments); diff --git a/client/getopt.lua b/client/getopt.lua new file mode 100644 index 00000000..538357f9 --- /dev/null +++ b/client/getopt.lua @@ -0,0 +1,107 @@ + +--[[This file is an adaptation from the following source: + +https://github.com/attractivechaos/klib/blob/master/lua/klib.lua + +]] + +--[[ + The MIT License + + Copyright (c) 2011, Attractive Chaos + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +]]-- + + +local function split(txt) + local retval = {}; + for i in string.gmatch(txt, "%S+") do + table.insert(retval,i) + end + return retval +end + + +-- Description: getopt() translated from the BSD getopt(); compatible with the default Unix getopt() +--[[ Example: + for o, a in os.getopt(arg, 'a:b') do + print(o, a) + end +]]-- + +local function getopt(args, ostr) + -- Modification to handle strings instead of tables: + if type(args) == 'string' then + args = split(args) + end + + local arg, place = nil, 0; + return function () + if place == 0 then -- update scanning pointer + place = 1 + if #args == 0 + or args[1]:sub(1, 1) ~= '-' then + place = 0; return nil end + if #args[1] >= 2 then + place = place + 1 + if args[1]:sub(2, 2) == '-' then -- found "--" + place = 0 + table.remove(args, 1); + return nil; + end + end + end + local optopt = args[1]:sub(place, place); + place = place + 1; + local oli = ostr:find(optopt); + if optopt == ':' or oli == nil then -- unknown option + if optopt == '-' then return nil end + if place > #args[1] then + table.remove(args, 1); + place = 0; + end + return '?'; + end + oli = oli + 1; + if ostr:sub(oli, oli) ~= ':' then -- do not need argument + arg = nil; + if place > #args[1] then + table.remove(args, 1); + place = 0; + end + else -- need an argument + if place <= #args[1] then -- no white space + arg = args[1]:sub(place); + else + table.remove(args, 1); + if #args == 0 then -- an option requiring argument is the last one + place = 0; + if ostr:sub(1, 1) == ':' then return ':' end + return '?'; + else arg = args[1] end + end + table.remove(args, 1); + place = 0; + end + return optopt, arg; + end +end + +return { getopt = getopt } diff --git a/client/scripts/parameters.lua b/client/scripts/parameters.lua new file mode 100644 index 00000000..fa260204 --- /dev/null +++ b/client/scripts/parameters.lua @@ -0,0 +1,45 @@ + +-- The getopt-functionality is loaded from pm3/getopt.lua +-- Have a look there for further details +getopt = require('getopt') + +usage = "script run parameters.lua -a 1 -blala -c -de" +author = "Martin Holst Swende" +desc =[[ +This is an example script to demonstrate handle parameters in scripts. +For more info, check the comments in the code +]] + +local function main(args) + + print(desc) + print("These parameters were passed") + --[[ + When passing parameters, + x: means that a value should follow x + y means that 'y' is a flag, either on or off + So, the string a:b:def means that we support up to + 5 parameters; two with values and three flags. The following + should be valid: + + script run parameters.lua -a 1 -blala -c -de + + Notice two things: + 1. 'blala' works just like 'b lala', both set 'b' to 'lala' + 2. Flags can be put together, '-de' is the same as '-d -e' + 3. The format -b=lala is *not* supported + 4. The format b lala (without -) is *not* supported + --]] + + for o, a in getopt.getopt(args, 'a:b:ced') do + print(o, a) + end +end + + +--[[ +In the future, we may implement so that scripts are invoked directly +into a 'main' function, instead of being executed blindly. For future +compatibility, I have done so, but I invoke my main from here. +--]] +main(args) \ No newline at end of file