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