Added native output grabbing for Python and Lua: less hacky than output_grabber.py, should work on ProxSpace as well

This commit is contained in:
Philippe Teuwen committed 2024-08-14 13:53:20 +02:00
1 parent 83f6e2b56b
commit 369c5bb5db
23 files changed
+387 -198

No files matched your search

+16 -2
View File
@@ -53,18 +53,32 @@ void pm3_close(pm3_device_t *dev) {
msleep(100); // Make sure command is sent before killing client
CloseProxmark(dev);
}
free_grabber();
}
int pm3_console(pm3_device_t *dev, const char *cmd) {
int pm3_console(pm3_device_t *dev, const char *cmd, bool passthru) {
// For now, there is no real device context:
(void) dev;
return CommandReceived(cmd);
uint8_t prev_printAndLog = g_printAndLog;
if (! passthru) {
g_printAndLog |= PRINTANDLOG_GRAB;
g_printAndLog &= ~PRINTANDLOG_PRINT;
}
int ret = CommandReceived(cmd);
g_printAndLog = prev_printAndLog;
return ret;
}
const char *pm3_name_get(pm3_device_t *dev) {
return dev->g_conn->serial_port_name;
}
const char *pm3_grabbed_output_get(pm3_device_t *dev) {
char *tmp = g_grabbed_output.ptr;
g_grabbed_output.idx = 0;
return tmp;
}
pm3_device_t *pm3_get_current_dev(void) {
return g_session.current_device;
}
+9 -1
View File
@@ -8,6 +8,13 @@
/* Strip "pm3_" from API functions for SWIG */
%rename("%(strip:[pm3_])s") "";
%feature("immutable","1") pm3_current_dev;
#ifdef PYWRAP
#include <Python.h>
%typemap(default) bool passthru {
$1 = Py_False;
}
#endif
typedef struct {
%extend {
pm3() {
@@ -30,8 +37,9 @@ typedef struct {
pm3_close($self);
}
}
int console(char *cmd);
int console(char *cmd, bool passthru = false);
char const * const name;
char const * const grabbed_output;
}
} pm3;
//%nodefaultctor device;
+32 -2
View File
@@ -2768,18 +2768,23 @@ static int _wrap_pm3_console(lua_State *L) {
int SWIG_arg = 0;
pm3 *arg1 = (pm3 *) 0 ;
char *arg2 = (char *) 0 ;
bool arg3 = (bool) false ;
int result;
SWIG_check_num_args("pm3::console", 2, 2)
SWIG_check_num_args("pm3::console", 2, 3)
if (!SWIG_isptrtype(L, 1)) SWIG_fail_arg("pm3::console", 1, "pm3 *");
if (!SWIG_lua_isnilstring(L, 2)) SWIG_fail_arg("pm3::console", 2, "char *");
if (lua_gettop(L) >= 3 && !lua_isboolean(L, 3)) SWIG_fail_arg("pm3::console", 3, "bool");
if (!SWIG_IsOK(SWIG_ConvertPtr(L, 1, (void **)&arg1, SWIGTYPE_p_pm3, 0))) {
SWIG_fail_ptr("pm3_console", 1, SWIGTYPE_p_pm3);
}
arg2 = (char *)lua_tostring(L, 2);
result = (int)pm3_console(arg1, arg2);
if (lua_gettop(L) >= 3) {
arg3 = (lua_toboolean(L, 3) != 0);
}
result = (int)pm3_console(arg1, arg2, arg3);
lua_pushnumber(L, (lua_Number) result);
SWIG_arg++;
return SWIG_arg;
@@ -2815,6 +2820,30 @@ fail:
}
static int _wrap_pm3_grabbed_output_get(lua_State *L) {
int SWIG_arg = 0;
pm3 *arg1 = (pm3 *) 0 ;
char *result = 0 ;
SWIG_check_num_args("pm3::grabbed_output", 1, 1)
if (!SWIG_isptrtype(L, 1)) SWIG_fail_arg("pm3::grabbed_output", 1, "pm3 *");
if (!SWIG_IsOK(SWIG_ConvertPtr(L, 1, (void **)&arg1, SWIGTYPE_p_pm3, 0))) {
SWIG_fail_ptr("pm3_grabbed_output_get", 1, SWIGTYPE_p_pm3);
}
result = (char *)pm3_grabbed_output_get(arg1);
lua_pushstring(L, (const char *)result);
SWIG_arg++;
return SWIG_arg;
fail:
SWIGUNUSED;
lua_error(L);
return 0;
}
static void swig_delete_pm3(void *obj) {
pm3 *arg1 = (pm3 *) obj;
delete_pm3(arg1);
@@ -2829,6 +2858,7 @@ static int _proxy__wrap_new_pm3(lua_State *L) {
}
static swig_lua_attribute swig_pm3_attributes[] = {
{ "name", _wrap_pm3_name_get, SWIG_Lua_set_immutable },
{ "grabbed_output", _wrap_pm3_grabbed_output_get, SWIG_Lua_set_immutable },
{0, 0, 0}
};
static swig_lua_method swig_pm3_methods[] = {
+181 -3
View File
@@ -3270,6 +3270,149 @@ SWIGINTERN void delete_pm3(pm3 *self) {
}
}
SWIGINTERN int
SWIG_AsVal_double(PyObject *obj, double *val) {
int res = SWIG_TypeError;
if (PyFloat_Check(obj)) {
if (val) *val = PyFloat_AsDouble(obj);
return SWIG_OK;
#if PY_VERSION_HEX < 0x03000000
} else if (PyInt_Check(obj)) {
if (val) *val = (double) PyInt_AsLong(obj);
return SWIG_OK;
#endif
} else if (PyLong_Check(obj)) {
double v = PyLong_AsDouble(obj);
if (!PyErr_Occurred()) {
if (val) *val = v;
return SWIG_OK;
} else {
PyErr_Clear();
}
}
#ifdef SWIG_PYTHON_CAST_MODE
{
int dispatch = 0;
double d = PyFloat_AsDouble(obj);
if (!PyErr_Occurred()) {
if (val) *val = d;
return SWIG_AddCast(SWIG_OK);
} else {
PyErr_Clear();
}
if (!dispatch) {
long v = PyLong_AsLong(obj);
if (!PyErr_Occurred()) {
if (val) *val = v;
return SWIG_AddCast(SWIG_AddCast(SWIG_OK));
} else {
PyErr_Clear();
}
}
}
#endif
return res;
}
#include <float.h>
#include <math.h>
SWIGINTERNINLINE int
SWIG_CanCastAsInteger(double *d, double min, double max) {
double x = *d;
if ((min <= x && x <= max)) {
double fx, cx, rd;
errno = 0;
fx = floor(x);
cx = ceil(x);
rd = ((x - fx) < 0.5) ? fx : cx; /* simple rint */
if ((errno == EDOM) || (errno == ERANGE)) {
errno = 0;
} else {
double summ, reps, diff;
if (rd < x) {
diff = x - rd;
} else if (rd > x) {
diff = rd - x;
} else {
return 1;
}
summ = rd + x;
reps = diff / summ;
if (reps < 8 * DBL_EPSILON) {
*d = rd;
return 1;
}
}
}
return 0;
}
SWIGINTERN int
SWIG_AsVal_long(PyObject *obj, long *val) {
#if PY_VERSION_HEX < 0x03000000
if (PyInt_Check(obj)) {
if (val) *val = PyInt_AsLong(obj);
return SWIG_OK;
} else
#endif
if (PyLong_Check(obj)) {
long v = PyLong_AsLong(obj);
if (!PyErr_Occurred()) {
if (val) *val = v;
return SWIG_OK;
} else {
PyErr_Clear();
return SWIG_OverflowError;
}
}
#ifdef SWIG_PYTHON_CAST_MODE
{
int dispatch = 0;
long v = PyInt_AsLong(obj);
if (!PyErr_Occurred()) {
if (val) *val = v;
return SWIG_AddCast(SWIG_OK);
} else {
PyErr_Clear();
}
if (!dispatch) {
double d;
int res = SWIG_AddCast(SWIG_AsVal_double(obj, &d));
// Largest double not larger than LONG_MAX (not portably calculated easily)
// Note that double(LONG_MAX) is stored in a double rounded up by one (for 64-bit long)
// 0x7ffffffffffffc00LL == (int64_t)std::nextafter(double(__uint128_t(LONG_MAX)+1), double(0))
const double long_max = sizeof(long) == 8 ? 0x7ffffffffffffc00LL : LONG_MAX;
// No equivalent needed for 64-bit double(LONG_MIN) is exactly LONG_MIN
if (SWIG_IsOK(res) && SWIG_CanCastAsInteger(&d, LONG_MIN, long_max)) {
if (val) *val = (long)(d);
return res;
}
}
}
#endif
return SWIG_TypeError;
}
SWIGINTERN int
SWIG_AsVal_bool(PyObject *obj, bool *val) {
int r;
if (!PyBool_Check(obj))
return SWIG_ERROR;
r = PyObject_IsTrue(obj);
if (r == -1)
return SWIG_ERROR;
if (val) *val = r ? true : false;
return SWIG_OK;
}
SWIGINTERNINLINE PyObject *
SWIG_From_int(int value) {
return PyInt_FromLong((long) value);
@@ -3403,16 +3546,19 @@ SWIGINTERN PyObject *_wrap_pm3_console(PyObject *self, PyObject *args) {
PyObject *resultobj = 0;
pm3 *arg1 = (pm3 *) 0 ;
char *arg2 = (char *) 0 ;
bool arg3 = (bool) false ;
void *argp1 = 0 ;
int res1 = 0 ;
int res2 ;
char *buf2 = 0 ;
int alloc2 = 0 ;
PyObject *swig_obj[2] ;
bool val3 ;
int ecode3 = 0 ;
PyObject *swig_obj[3] ;
int result;
(void)self;
if (!SWIG_Python_UnpackTuple(args, "pm3_console", 2, 2, swig_obj)) SWIG_fail;
if (!SWIG_Python_UnpackTuple(args, "pm3_console", 2, 3, swig_obj)) SWIG_fail;
res1 = SWIG_ConvertPtr(swig_obj[0], &argp1, SWIGTYPE_p_pm3, 0 | 0);
if (!SWIG_IsOK(res1)) {
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "pm3_console" "', argument " "1"" of type '" "pm3 *""'");
@@ -3423,7 +3569,14 @@ SWIGINTERN PyObject *_wrap_pm3_console(PyObject *self, PyObject *args) {
SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "pm3_console" "', argument " "2"" of type '" "char *""'");
}
arg2 = (char *)(buf2);
result = (int)pm3_console(arg1, arg2);
if (swig_obj[2]) {
ecode3 = SWIG_AsVal_bool(swig_obj[2], &val3);
if (!SWIG_IsOK(ecode3)) {
SWIG_exception_fail(SWIG_ArgError(ecode3), "in method '" "pm3_console" "', argument " "3"" of type '" "bool""'");
}
arg3 = (bool)(val3);
}
result = (int)pm3_console(arg1, arg2, arg3);
resultobj = SWIG_From_int((int)(result));
if (alloc2 == SWIG_NEWOBJ) free((char *)buf2);
return resultobj;
@@ -3457,6 +3610,30 @@ fail:
}
SWIGINTERN PyObject *_wrap_pm3_grabbed_output_get(PyObject *self, PyObject *args) {
PyObject *resultobj = 0;
pm3 *arg1 = (pm3 *) 0 ;
void *argp1 = 0 ;
int res1 = 0 ;
PyObject *swig_obj[1] ;
char *result = 0 ;
(void)self;
if (!args) SWIG_fail;
swig_obj[0] = args;
res1 = SWIG_ConvertPtr(swig_obj[0], &argp1, SWIGTYPE_p_pm3, 0 | 0);
if (!SWIG_IsOK(res1)) {
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "pm3_grabbed_output_get" "', argument " "1"" of type '" "pm3 *""'");
}
arg1 = (pm3 *)(argp1);
result = (char *)pm3_grabbed_output_get(arg1);
resultobj = SWIG_FromCharPtr((const char *)result);
return resultobj;
fail:
return NULL;
}
SWIGINTERN PyObject *pm3_swigregister(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
PyObject *obj;
if (!SWIG_Python_UnpackTuple(args, "swigregister", 1, 1, &obj)) return NULL;
@@ -3473,6 +3650,7 @@ static PyMethodDef SwigMethods[] = {
{ "delete_pm3", _wrap_delete_pm3, METH_O, NULL},
{ "pm3_console", _wrap_pm3_console, METH_VARARGS, NULL},
{ "pm3_name_get", _wrap_pm3_name_get, METH_O, NULL},
{ "pm3_grabbed_output_get", _wrap_pm3_grabbed_output_get, METH_O, NULL},
{ "pm3_swigregister", pm3_swigregister, METH_O, NULL},
{ "pm3_swiginit", pm3_swiginit, METH_VARARGS, NULL},
{ NULL, NULL, 0, NULL }
+3 -1
View File
@@ -553,7 +553,7 @@ check_script:
if (cmd[0] != '\0') {
uint8_t old_printAndLog = g_printAndLog;
if (!printprompt) {
g_printAndLog &= PRINTANDLOG_LOG;
g_printAndLog &= ~PRINTANDLOG_PRINT;
}
char prompt[PROXPROMPT_MAX_SIZE] = {0};
prompt_compose(prompt, sizeof(prompt), prompt_ctx, prompt_dev, prompt_net, true);
@@ -1462,6 +1462,8 @@ int main(int argc, char *argv[]) {
preferences_save();
}
free_grabber();
return mainret;
}
#endif //LIBPM3
+53 -9
View File
@@ -168,6 +168,33 @@ int searchHomeFilePath(char **foundpath, const char *subdir, const char *filenam
return PM3_SUCCESS;
}
void free_grabber(void) {
free(g_grabbed_output.ptr);
g_grabbed_output.ptr = NULL;
g_grabbed_output.size = 0;
g_grabbed_output.idx = 0;
}
static void fill_grabber(const char *string) {
if (g_grabbed_output.ptr == NULL || g_grabbed_output.size - g_grabbed_output.idx < MAX_PRINT_BUFFER) {
char *tmp = realloc(g_grabbed_output.ptr, g_grabbed_output.size + MAX_PRINT_BUFFER);
if (tmp == NULL) {
// We leave current g_grabbed_output untouched
PrintAndLogEx(ERR, "Out of memory error in fill_grabber()");
return;
}
g_grabbed_output.ptr = tmp;
g_grabbed_output.size += MAX_PRINT_BUFFER;
}
int len = snprintf(g_grabbed_output.ptr + g_grabbed_output.idx, MAX_PRINT_BUFFER, "%s", string);
if (len < 0 || len > MAX_PRINT_BUFFER) {
// We leave current g_grabbed_output_len untouched
PrintAndLogEx(ERR, "snprintf error in fill_grabber()");
return;
}
g_grabbed_output.idx += len;
}
void PrintAndLogOptions(const char *str[][2], size_t size, size_t space) {
char buff[2000] = "Options:\n";
char format[2000] = "";
@@ -299,12 +326,15 @@ void PrintAndLogEx(logLevel_t level, const char *fmt, ...) {
} else {
snprintf(buffer2, sizeof(buffer2), "%s%s", prefix, buffer);
if (level == INPLACE) {
char buffer3[sizeof(buffer2)] = {0};
char buffer4[sizeof(buffer2)] = {0};
memcpy_filter_ansi(buffer3, buffer2, sizeof(buffer2), !g_session.supports_colors);
memcpy_filter_emoji(buffer4, buffer3, sizeof(buffer3), g_session.emoji_mode);
fprintf(stream, "\r%s", buffer4);
fflush(stream);
// ignore INPLACE if rest of output is grabbed
if (!(g_printAndLog & PRINTANDLOG_GRAB)) {
char buffer3[sizeof(buffer2)] = {0};
char buffer4[sizeof(buffer2)] = {0};
memcpy_filter_ansi(buffer3, buffer2, sizeof(buffer2), !g_session.supports_colors);
memcpy_filter_emoji(buffer4, buffer3, sizeof(buffer3), g_session.emoji_mode);
fprintf(stream, "\r%s", buffer4);
fflush(stream);
}
} else {
fPrintAndLog(stream, "%s", buffer2);
}
@@ -401,18 +431,32 @@ static void fPrintAndLog(FILE *stream, const char *fmt, ...) {
}
#endif
if ((g_printAndLog & PRINTANDLOG_LOG) && logging && logfile) {
if (((g_printAndLog & PRINTANDLOG_LOG) && logging && logfile) ||
(g_printAndLog & PRINTANDLOG_GRAB)) {
memcpy_filter_emoji(buffer3, buffer2, sizeof(buffer2), EMO_ALTTEXT);
if (filter_ansi) { // already done
if (!filter_ansi) {
memcpy_filter_ansi(buffer, buffer3, sizeof(buffer3), true);
}
}
if ((g_printAndLog & PRINTANDLOG_LOG) && logging && logfile) {
if (filter_ansi) {
fprintf(logfile, "%s", buffer3);
} else {
memcpy_filter_ansi(buffer, buffer3, sizeof(buffer3), true);
fprintf(logfile, "%s", buffer);
}
if (linefeed)
fprintf(logfile, "\n");
fflush(logfile);
}
if (g_printAndLog & PRINTANDLOG_GRAB) {
if (filter_ansi) {
fill_grabber(buffer3);
} else {
fill_grabber(buffer);
}
if (linefeed)
fill_grabber("\n");
}
if (flushAfterWrite)
fflush(stdout);
+1
View File
@@ -80,6 +80,7 @@ bool GetFlushAfterWrite(void);
void memcpy_filter_ansi(void *dest, const void *src, size_t n, bool filter);
void memcpy_filter_rlmarkers(void *dest, const void *src, size_t n);
void memcpy_filter_emoji(void *dest, const void *src, size_t n, emojiMode_t mode);
void free_grabber(void);
int searchHomeFilePath(char **foundpath, const char *subdir, const char *filename, bool create_home);
+3 -1
View File
@@ -36,8 +36,10 @@
#define UTIL_BUFFER_SIZE_SPRINT 8196
// global client debug variable
uint8_t g_debugMode = 0;
// global client disable logging variable
// global client enable/disable printing/logging/grabbing variable
uint8_t g_printAndLog = PRINTANDLOG_PRINT | PRINTANDLOG_LOG;
// global pointer to grabbed output
grabbed_output g_grabbed_output = {NULL, 0, 0};
// global client tell if a pending prompt is present
bool g_pendingPrompt = false;
// global CPU core count override
+8
View File
@@ -34,8 +34,16 @@ extern uint8_t g_printAndLog;
extern bool g_pendingPrompt;
extern int g_numCPUs;
typedef struct {
char *ptr;
size_t size;
size_t idx;
} grabbed_output;
extern grabbed_output g_grabbed_output;
#define PRINTANDLOG_PRINT 1
#define PRINTANDLOG_LOG 2
#define PRINTANDLOG_GRAB 4
// Return error
#define PM3_RET_ERR(err, ...) { \