]> git.zerfleddert.de Git - proxmark3-svn/blame - client/cmdscript.c
chg; PRIu32 becomes %u. Lets return to whats working - %lu
[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 "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"
f057bddb 27#include "pm3_binlib.h"
77cd612f 28#include "pm3_bitlib.h"
806dc075 29#include <lua.h>
30#include <lualib.h>
31#include <lauxlib.h>
32
33
806dc075 34static int CmdHelp(const char *Cmd);
35static int CmdList(const char *Cmd);
36static int CmdRun(const char *Cmd);
37
38command_t CommandTable[] =
39{
40 {"help", CmdHelp, 1, "This help"},
a0655c45 41 {"list", CmdList, 1, "List available scripts"},
42 {"run", CmdRun, 1, "<name> -- Execute a script"},
806dc075 43 {NULL, NULL, 0, NULL}
44};
45
46int 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 */
65int CmdHelp(const char * Cmd)
66{
5def0b3c 67 PrintAndLog("This is a feature to run Lua-scripts. You can place lua-scripts within the ´client/scripts/´ folder.");
42daa759 68 return 0;
806dc075 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
bd94b978 75*
806dc075 76*/
77int CmdList(const char *Cmd)
78{
806dc075 79 struct dirent *ep;
bd94b978 80 DIR *dp = opendir ("./scripts/");
81 if ( dp == NULL ) {
82 PrintAndLog ("Couldn't open the scripts-directory");
83 return 1;
84 }
a403a559 85
bd94b978 86 while ((ep = readdir (dp)) != NULL)
87 {
88 if(str_ends_with(ep->d_name, ".lua"))
89 PrintAndLog("%-21s %s", ep->d_name, "A script file");
90 }
91 (void) closedir (dp);
806dc075 92 return 0;
93}
bd94b978 94
95
806dc075 96/**
97 * Finds a matching script-file
98 * @brief CmdScript
99 * @param Cmd
100 * @return
101 */
4c36581b 102int CmdScript(const char *Cmd) {
103 clearCommandBuffer();
104 CmdsParse(CommandTable, Cmd);
105 return 0;
806dc075 106}
a403a559 107/**
108 * Utility to check the ending of a string (used to check file suffix)
109 */
110bool 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));
114}
806dc075 115
116/**
117 * @brief CmdRun - executes a script file.
118 * @param argc
119 * @param argv
120 * @return
121 */
122int CmdRun(const char *Cmd)
123{
124 // create new Lua state
125 lua_State *lua_state;
126 lua_state = luaL_newstate();
127
128 // load Lua libraries
5a92cb52 129 luaL_openlibs(lua_state);
a0655c45 130
131 //Sets the pm3 core libraries, that go a bit 'under the hood'
132 set_pm3_libraries(lua_state);
133
f057bddb 134 //Add the 'bin' library
135 set_bin_library(lua_state);
136
77cd612f 137 //Add the 'bit' library
138 set_bit_library(lua_state);
a403a559 139
30a5d355 140 char script_name[128] = {0};
141 char arguments[256] = {0};
a403a559 142
143 int name_len = 0;
144 int arg_len = 0;
145 sscanf(Cmd, "%127s%n %255[^\n\r]%n", script_name,&name_len, arguments, &arg_len);
146
147 char *suffix = "";
148 if(!endsWith(script_name,".lua"))
149 {
150 suffix = ".lua";
151 }
806dc075 152
153 char buf[256];
a403a559 154 snprintf(buf, sizeof buf, "./scripts/%s%s", script_name, suffix);
155
5def0b3c 156 printf("--- Executing: %s, args'%s'\n", buf, arguments);
806dc075 157
806dc075 158 // run the Lua script
a0655c45 159
160 int error = luaL_loadfile(lua_state, buf);
161 if(!error)
162 {
a403a559 163 lua_pushstring(lua_state, arguments);
164 lua_setglobal(lua_state, "args");
165
166 //Call it with 0 arguments
a0655c45 167 error = lua_pcall(lua_state, 0, LUA_MULTRET, 0); // once again, returns non-0 on error,
168 }
169 if(error) // if non-0, then an error
170 {
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 (?!)");
174
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);
42daa759 178 puts(str);
a0655c45 179 }
180
181 //luaL_dofile(lua_state, buf);
806dc075 182 // close the Lua state
183 lua_close(lua_state);
96e7a3a5 184 printf("\n-----Finished\n");
42daa759 185 return 0;
806dc075 186}
187
Impressum, Datenschutz