]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdscript.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2013 m h swende <martin at swende.se>
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // Some lua scripting glue to proxmark core.
9 //-----------------------------------------------------------------------------
15 #include <sys/types.h>
18 #include "proxmark3.h"
19 #include "scripting.h"
23 #include "cmdparser.h"
25 #include "cmdscript.h"
27 #include "pm3_binlib.h"
28 #include "pm3_bitlib.h"
34 static int CmdHelp(const char *Cmd
);
35 static int CmdList(const char *Cmd
);
36 static int CmdRun(const char *Cmd
);
38 command_t CommandTable
[] =
40 {"help", CmdHelp
, 1, "This help"},
41 {"list", CmdList
, 1, "List available scripts"},
42 {"run", CmdRun
, 1, "<name> -- Execute a script"},
46 int str_ends_with(const char * str
, const char * suffix
) {
48 if( str
== NULL
|| suffix
== NULL
)
51 size_t str_len
= strlen(str
);
52 size_t suffix_len
= strlen(suffix
);
54 if(suffix_len
> str_len
)
57 return 0 == strncmp( str
+ str_len
- suffix_len
, suffix
, suffix_len
);
60 * Shows some basic help
65 int CmdHelp(const char * Cmd
)
67 PrintAndLog("This is a feature to run Lua-scripts. You can place lua-scripts within the ´client/scripts/´ folder.");
72 * Generate list of available commands, what it does is
73 * generate a file listing of the script-directory for files
77 int CmdList(const char *Cmd
)
80 DIR *dp
= opendir ("./scripts/");
82 PrintAndLog ("Couldn't open the scripts-directory");
86 while ((ep
= readdir (dp
)) != NULL
)
88 if(str_ends_with(ep
->d_name
, ".lua"))
89 PrintAndLog("%-21s %s", ep
->d_name
, "A script file");
97 * Finds a matching script-file
102 int CmdScript(const char *Cmd
) {
103 clearCommandBuffer();
104 CmdsParse(CommandTable
, Cmd
);
108 * Utility to check the ending of a string (used to check file suffix)
110 bool endsWith (char* base
, char* str
) {
111 int blen
= strlen(base
);
112 int slen
= strlen(str
);
113 return (blen
>= slen
) && (0 == strcmp(base
+ blen
- slen
, str
));
117 * @brief CmdRun - executes a script file.
122 int CmdRun(const char *Cmd
)
124 // create new Lua state
125 lua_State
*lua_state
;
126 lua_state
= luaL_newstate();
128 // load Lua libraries
129 luaL_openlibs(lua_state
);
131 //Sets the pm3 core libraries, that go a bit 'under the hood'
132 set_pm3_libraries(lua_state
);
134 //Add the 'bin' library
135 set_bin_library(lua_state
);
137 //Add the 'bit' library
138 set_bit_library(lua_state
);
140 char script_name
[128] = {0};
141 char arguments
[256] = {0};
145 sscanf(Cmd
, "%127s%n %255[^\n\r]%n", script_name
,&name_len
, arguments
, &arg_len
);
148 if(!endsWith(script_name
,".lua"))
154 snprintf(buf
, sizeof buf
, "./scripts/%s%s", script_name
, suffix
);
156 printf("--- Executing: %s, args'%s'\n", buf
, arguments
);
158 // run the Lua script
160 int error
= luaL_loadfile(lua_state
, buf
);
163 lua_pushstring(lua_state
, arguments
);
164 lua_setglobal(lua_state
, "args");
166 //Call it with 0 arguments
167 error
= lua_pcall(lua_state
, 0, LUA_MULTRET
, 0); // once again, returns non-0 on error,
169 if(error
) // if non-0, then an error
171 // the top of the stack should be the error string
172 if (!lua_isstring(lua_state
, lua_gettop(lua_state
)))
173 printf( "Error - but no error (?!)");
175 // get the top of the stack as the error and pop it off
176 const char * str
= lua_tostring(lua_state
, lua_gettop(lua_state
));
177 lua_pop(lua_state
, 1);
181 //luaL_dofile(lua_state, buf);
182 // close the Lua state
183 lua_close(lua_state
);
184 printf("\n-----Finished\n");