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