]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdscript.c
Change USB Configuration to "Bus Powered"
[proxmark3-svn] / client / cmdscript.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2013 m h swende <martin at swende.se>
3 //
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
6 // the license.
7 //-----------------------------------------------------------------------------
8 // Some lua scripting glue to proxmark core.
9 //-----------------------------------------------------------------------------
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <limits.h>
15 #include <sys/types.h>
16 #include <dirent.h>
17
18 #include "proxmark3.h"
19 #include "scripting.h"
20 #include "data.h"
21 #include "ui.h"
22 #include "graph.h"
23 #include "cmdparser.h"
24 #include "cmdmain.h"
25 #include "cmdscript.h"
26 #include "cmdhfmf.h"
27 #include "pm3_binlib.h"
28 #include "pm3_bitlib.h"
29 #include <lua.h>
30 #include <lualib.h>
31 #include <lauxlib.h>
32
33 static int CmdHelp(const char *Cmd);
34 static int CmdList(const char *Cmd);
35 static int CmdRun(const char *Cmd);
36
37 command_t CommandTable[] =
38 {
39 {"help", CmdHelp, 1, "This help"},
40 {"list", CmdList, 1, "List available scripts"},
41 {"run", CmdRun, 1, "<name> -- Execute a script"},
42 {NULL, NULL, 0, NULL}
43 };
44
45 int str_ends_with(const char * str, const char * suffix) {
46
47 if( str == NULL || suffix == NULL )
48 return 0;
49
50 size_t str_len = strlen(str);
51 size_t suffix_len = strlen(suffix);
52
53 if(suffix_len > str_len)
54 return 0;
55
56 return 0 == strncmp( str + str_len - suffix_len, suffix, suffix_len );
57 }
58 /**
59 * Shows some basic help
60 * @brief CmdHelp
61 * @param Cmd
62 * @return
63 */
64 int CmdHelp(const char * Cmd)
65 {
66 PrintAndLog("This is a feature to run Lua-scripts. You can place lua-scripts within the scripts/-folder. ");
67 return 0;
68 }
69
70 /**
71 * Generate list of available commands, what it does is
72 * generate a file listing of the script-directory for files
73 * ending with .lua
74 */
75 int CmdList(const char *Cmd)
76 {
77 DIR *dp;
78 struct dirent *ep;
79 char script_directory_path[strlen(get_my_executable_directory()) + strlen(LUA_SCRIPTS_DIRECTORY) + 1];
80 strcpy(script_directory_path, get_my_executable_directory());
81 strcat(script_directory_path, LUA_SCRIPTS_DIRECTORY);
82 dp = opendir(script_directory_path);
83
84 if (dp != NULL)
85 {
86 while ((ep = readdir (dp)) != NULL)
87 {
88 if(str_ends_with(ep->d_name, ".lua"))
89 PrintAndLog("%-16s %s", ep->d_name, "A script file");
90 }
91 (void) closedir (dp);
92 }
93 else
94 PrintAndLog ("Couldn't open the scripts-directory");
95 return 0;
96 }
97 /**
98 * Finds a matching script-file
99 * @brief CmdScript
100 * @param Cmd
101 * @return
102 */
103 int CmdScript(const char *Cmd)
104 {
105 CmdsParse(CommandTable, Cmd);
106 return 0;
107 }
108 /**
109 * Utility to check the ending of a string (used to check file suffix)
110 */
111 bool endsWith (char* base, char* str) {
112 int blen = strlen(base);
113 int slen = strlen(str);
114 return (blen >= slen) && (0 == strcmp(base + blen - slen, str));
115 }
116
117 /**
118 * @brief CmdRun - executes a script file.
119 * @param argc
120 * @param argv
121 * @return
122 */
123 int CmdRun(const char *Cmd)
124 {
125 // create new Lua state
126 lua_State *lua_state;
127 lua_state = luaL_newstate();
128
129 // load Lua libraries
130 luaL_openlibs(lua_state);
131
132 //Sets the pm3 core libraries, that go a bit 'under the hood'
133 set_pm3_libraries(lua_state);
134
135 //Add the 'bin' library
136 set_bin_library(lua_state);
137
138 //Add the 'bit' library
139 set_bit_library(lua_state);
140
141 char script_name[128] = {0};
142 char arguments[256] = {0};
143
144 int name_len = 0;
145 int arg_len = 0;
146 sscanf(Cmd, "%127s%n %255[^\n\r]%n", script_name,&name_len, arguments, &arg_len);
147
148 char *suffix = "";
149 if(!endsWith(script_name,".lua"))
150 {
151 suffix = ".lua";
152 }
153
154 char script_path[strlen(get_my_executable_directory()) + strlen(LUA_SCRIPTS_DIRECTORY) + strlen(script_name) + strlen(suffix) + 1];
155 strcpy(script_path, get_my_executable_directory());
156 strcat(script_path, LUA_SCRIPTS_DIRECTORY);
157 strcat(script_path, script_name);
158 strcat(script_path, suffix);
159
160 printf("--- Executing: %s%s, args '%s'\n", script_name, suffix, arguments);
161
162
163
164 // run the Lua script
165
166 int error = luaL_loadfile(lua_state, script_path);
167 if(!error)
168 {
169
170 lua_pushstring(lua_state, arguments);
171 lua_setglobal(lua_state, "args");
172
173 //Call it with 0 arguments
174 error = lua_pcall(lua_state, 0, LUA_MULTRET, 0); // once again, returns non-0 on error,
175 }
176 if(error) // if non-0, then an error
177 {
178 // the top of the stack should be the error string
179 if (!lua_isstring(lua_state, lua_gettop(lua_state)))
180 printf( "Error - but no error (?!)");
181
182 // get the top of the stack as the error and pop it off
183 const char * str = lua_tostring(lua_state, lua_gettop(lua_state));
184 lua_pop(lua_state, 1);
185 puts(str);
186 }
187
188 //luaL_dofile(lua_state, buf);
189 // close the Lua state
190 lua_close(lua_state);
191 printf("\n-----Finished\n");
192 return 0;
193 }
194
Impressum, Datenschutz