]>
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"
33 static int CmdHelp(const char *Cmd
);
34 static int CmdList(const char *Cmd
);
35 static int CmdRun(const char *Cmd
);
37 command_t CommandTable
[] =
39 {"help", CmdHelp
, 1, "This help"},
40 {"list", CmdList
, 1, "List available scripts"},
41 {"run", CmdRun
, 1, "<name> -- Execute a script"},
45 int str_ends_with(const char * str
, const char * suffix
) {
47 if( str
== NULL
|| suffix
== NULL
)
50 size_t str_len
= strlen(str
);
51 size_t suffix_len
= strlen(suffix
);
53 if(suffix_len
> str_len
)
56 return 0 == strncmp( str
+ str_len
- suffix_len
, suffix
, suffix_len
);
59 * Shows some basic help
64 int CmdHelp(const char * Cmd
)
66 PrintAndLog("This is a feature to run Lua-scripts. You can place lua-scripts within the scripts/-folder. ");
71 * Generate list of available commands, what it does is
72 * generate a file listing of the script-directory for files
75 int CmdList(const char *Cmd
)
79 char const * exedir
= get_my_executable_directory();
82 char script_directory_path
[strlen(exedir
) + strlen(LUA_SCRIPTS_DIRECTORY
) + 1];
83 strcpy(script_directory_path
, exedir
);
84 strcat(script_directory_path
, LUA_SCRIPTS_DIRECTORY
);
85 dp
= opendir(script_directory_path
);
89 while ((ep
= readdir (dp
)) != NULL
)
91 if(str_ends_with(ep
->d_name
, ".lua"))
92 PrintAndLog("%-16s %s", ep
->d_name
, "A script file");
97 PrintAndLog ("Couldn't open the scripts-directory");
101 * Finds a matching script-file
106 int CmdScript(const char *Cmd
)
108 CmdsParse(CommandTable
, Cmd
);
112 * Utility to check the ending of a string (used to check file suffix)
114 bool endsWith (char* base
, char* str
) {
115 int blen
= strlen(base
);
116 int slen
= strlen(str
);
117 return (blen
>= slen
) && (0 == strcmp(base
+ blen
- slen
, str
));
121 * @brief CmdRun - executes a script file.
126 int CmdRun(const char *Cmd
)
128 // create new Lua state
129 lua_State
*lua_state
;
130 lua_state
= luaL_newstate();
132 // load Lua libraries
133 luaL_openlibs(lua_state
);
135 //Sets the pm3 core libraries, that go a bit 'under the hood'
136 set_pm3_libraries(lua_state
);
138 //Add the 'bin' library
139 set_bin_library(lua_state
);
141 //Add the 'bit' library
142 set_bit_library(lua_state
);
144 char script_name
[128] = {0};
145 char arguments
[256] = {0};
149 sscanf(Cmd
, "%127s%n %255[^\n\r]%n", script_name
,&name_len
, arguments
, &arg_len
);
152 if(!endsWith(script_name
,".lua"))
157 char script_path
[strlen(get_my_executable_directory()) + strlen(LUA_SCRIPTS_DIRECTORY
) + strlen(script_name
) + strlen(suffix
) + 1];
158 strcpy(script_path
, get_my_executable_directory());
159 strcat(script_path
, LUA_SCRIPTS_DIRECTORY
);
160 strcat(script_path
, script_name
);
161 strcat(script_path
, suffix
);
163 printf("--- Executing: %s%s, args '%s'\n", script_name
, suffix
, arguments
);
167 // run the Lua script
169 int error
= luaL_loadfile(lua_state
, script_path
);
173 lua_pushstring(lua_state
, arguments
);
174 lua_setglobal(lua_state
, "args");
176 //Call it with 0 arguments
177 error
= lua_pcall(lua_state
, 0, LUA_MULTRET
, 0); // once again, returns non-0 on error,
179 if(error
) // if non-0, then an error
181 // the top of the stack should be the error string
182 if (!lua_isstring(lua_state
, lua_gettop(lua_state
)))
183 printf( "Error - but no error (?!)");
185 // get the top of the stack as the error and pop it off
186 const char * str
= lua_tostring(lua_state
, lua_gettop(lua_state
));
187 lua_pop(lua_state
, 1);
191 //luaL_dofile(lua_state, buf);
192 // close the Lua state
193 lua_close(lua_state
);
194 printf("\n-----Finished\n");