python.cpp: Indentation consistency and misc formatting fixes

This commit is contained in:
gergely.erdelyi 2009-05-22 15:59:38 +00:00
parent 260c813f05
commit ef9921587b

View File

@ -147,16 +147,13 @@ static error_t idaapi idc_runpythonstatement(idc_value_t *argv, idc_value_t *res
return eOk; return eOk;
} }
/* QuickFix for the FILE* incompatibility problem */ /* QuickFix for the FILE* incompatibility problem */
int ExecFile(const char *FileName) int ExecFile(const char *FileName)
{ {
PyObject* PyFileObject = PyFile_FromString((char*)FileName, "r"); PyObject* PyFileObject = PyFile_FromString((char*)FileName, "r");
if (!PyFileObject) if (!PyFileObject)
{
return 0; return 0;
}
if (PyRun_SimpleFile(PyFile_AsFile(PyFileObject), FileName) == 0) if (PyRun_SimpleFile(PyFile_AsFile(PyFileObject), FileName) == 0)
{ {
@ -203,9 +200,7 @@ void IDAPython_RunScript(char *script)
int i; int i;
if (script) if (script)
{
scriptpath = script; scriptpath = script;
}
else else
{ {
scriptpath = askfile_c(0, "*.py", "Python file to run"); scriptpath = askfile_c(0, "*.py", "Python file to run");
@ -220,15 +215,10 @@ void IDAPython_RunScript(char *script)
for (i=0; scriptpath[i]; i++) for (i=0; scriptpath[i]; i++)
{ {
if (scriptpath[i] == '\\') if (scriptpath[i] == '\\')
{
slashpath[i] = '/'; slashpath[i] = '/';
}
else else
{
slashpath[i] = scriptpath[i]; slashpath[i] = scriptpath[i];
} }
}
slashpath[i] = '\0'; slashpath[i] = '\0';
/* Add the script't path to sys.path */ /* Add the script't path to sys.path */
@ -239,13 +229,10 @@ void IDAPython_RunScript(char *script)
/* Error handling */ /* Error handling */
if (PyErr_Occurred()) if (PyErr_Occurred())
{
PyErr_Print(); PyErr_Print();
}
} }
/* Execute Python statement(s) from an editor window */ /* Execute Python statement(s) from an editor window */
/* Default hotkey: Alt-8 */ /* Default hotkey: Alt-8 */
void IDAPython_RunStatement(void) void IDAPython_RunStatement(void)
@ -258,9 +245,7 @@ void IDAPython_RunStatement(void)
/* Fetch the previous statement */ /* Fetch the previous statement */
if (history.supval(IDAPYTHON_DATA_STATEMENT, statement, sizeof(statement)) == -1) if (history.supval(IDAPYTHON_DATA_STATEMENT, statement, sizeof(statement)) == -1)
{
statement[0] = '\0'; statement[0] = '\0';
}
if (asktext(sizeof(statement), statement, statement, "Enter Python expressions")) if (asktext(sizeof(statement), statement, statement, "Enter Python expressions"))
{ {
@ -311,10 +296,8 @@ void IDAPython_ScriptBox(void)
{ {
/* Print the exception info */ /* Print the exception info */
if (PyErr_Occurred()) if (PyErr_Occurred())
{
PyErr_Print(); PyErr_Print();
} }
}
} }
bool idaapi IDAPython_Menu_Callback(void *ud) bool idaapi IDAPython_Menu_Callback(void *ud)
@ -345,10 +328,8 @@ static void handle_python_error(char *errbuf, size_t errbufsize)
Py_XDECREF(ptraceback); Py_XDECREF(ptraceback);
} }
else else
{
PyErr_Print(); PyErr_Print();
} }
}
} }
/* Convert return value from Python to IDC or report about an error */ /* Convert return value from Python to IDC or report about an error */
@ -378,9 +359,7 @@ static bool return_python_result(idc_value_t *rv,
{ {
rv->str = (char *)qalloc(PyString_Size(result)+1); rv->str = (char *)qalloc(PyString_Size(result)+1);
if (!rv->str) if (!rv->str)
{
return false; return false;
}
qstrncpy(rv->str, PyString_AsString(result), MAXSTR); qstrncpy(rv->str, PyString_AsString(result), MAXSTR);
rv->vtype = VT_STR; rv->vtype = VT_STR;
Py_XDECREF(result); Py_XDECREF(result);
@ -425,7 +404,7 @@ bool idaapi IDAPython_extlang_compile(const char *name,
PyObject *func = PyFunction_New((PyObject *)code, globals); PyObject *func = PyFunction_New((PyObject *)code, globals);
if (func == NULL) if (func == NULL)
{ {
ERR: ERR:
handle_python_error(errbuf, errbufsize); handle_python_error(errbuf, errbufsize);
Py_XDECREF(code); Py_XDECREF(code);
return false; return false;
@ -458,16 +437,13 @@ bool idaapi IDAPython_extlang_run(const char *name,
case VT_LONG: case VT_LONG:
pa = PyInt_FromLong(args[i].num); pa = PyInt_FromLong(args[i].num);
break; break;
case VT_STR: case VT_STR:
pa = PyString_FromString(args[i].str); pa = PyString_FromString(args[i].str);
break; break;
case VT_FLOAT: case VT_FLOAT:
ieee_realcvt(&dresult, (ushort *)args[i].e, 013); ieee_realcvt(&dresult, (ushort *)args[i].e, 013);
pa = PyFloat_FromDouble(dresult); pa = PyFloat_FromDouble(dresult);
break; break;
default: default:
qsnprintf(errbuf, errbufsize, "arg#%d has wrong type %d", i, args[i].vtype); qsnprintf(errbuf, errbufsize, "arg#%d has wrong type %d", i, args[i].vtype);
return false; return false;
@ -537,7 +513,7 @@ bool idaapi IDAPython_extlang_calcexpr(ea_t /*current_ea*/,
} }
extlang_t extlang_python = extlang_t extlang_python =
{ {
sizeof(extlang_t), sizeof(extlang_t),
0, 0,
"Python", "Python",
@ -546,7 +522,7 @@ extlang_t extlang_python =
IDAPython_extlang_calcexpr, IDAPython_extlang_calcexpr,
IDAPython_extlang_compile_file, IDAPython_extlang_compile_file,
"py" "py"
}; };
void enable_extlang_python(bool enable) void enable_extlang_python(bool enable)
{ {
@ -569,7 +545,7 @@ bool idaapi IDAPython_cli_execute_line(const char *line)
} }
cli_t cli_python = cli_t cli_python =
{ {
sizeof(cli_t), sizeof(cli_t),
0, 0,
"Python", "Python",
@ -578,19 +554,15 @@ cli_t cli_python =
IDAPython_cli_execute_line, IDAPython_cli_execute_line,
NULL, NULL,
NULL NULL
}; };
/* Control the Python CLI status */ /* Control the Python CLI status */
void enable_python_cli(bool enable) void enable_python_cli(bool enable)
{ {
if (enable) if (enable)
{
install_command_interpreter(&cli_python); install_command_interpreter(&cli_python);
}
else else
{
remove_command_interpreter(&cli_python); remove_command_interpreter(&cli_python);
}
} }
#endif #endif
@ -604,9 +576,7 @@ bool IDAPython_Init(void)
/* Already initialized? */ /* Already initialized? */
if (initialized == 1) if (initialized == 1)
{
return true; return true;
}
/* Check for the presence of essential files */ /* Check for the presence of essential files */
initialized = 0; initialized = 0;
@ -615,11 +585,8 @@ bool IDAPython_Init(void)
result &= CheckFile("init.py"); result &= CheckFile("init.py");
result &= CheckFile("idaapi.py"); result &= CheckFile("idaapi.py");
result &= CheckFile("idautils.py"); result &= CheckFile("idautils.py");
if (!result) if (!result)
{
return false; return false;
}
#ifdef __LINUX__ #ifdef __LINUX__
/* Export symbols from libpython to resolve imported module deps */ /* Export symbols from libpython to resolve imported module deps */
@ -651,7 +618,6 @@ bool IDAPython_Init(void)
VER_PATCH, VER_PATCH,
VER_STATUS, VER_STATUS,
VER_SERIAL); VER_SERIAL);
PyRun_SimpleString(tmp); PyRun_SimpleString(tmp);
#if IDP_INTERFACE_VERSION >= 75 #if IDP_INTERFACE_VERSION >= 75
@ -676,11 +642,8 @@ bool IDAPython_Init(void)
/* Batch-mode operation: */ /* Batch-mode operation: */
/* A script specified on the command line is run */ /* A script specified on the command line is run */
options = (char *)get_plugin_options("IDAPython"); options = (char *)get_plugin_options("IDAPython");
if (options) if (options)
{
IDAPython_RunScript(options); IDAPython_RunScript(options);
}
/* Add menu items for all the functions */ /* Add menu items for all the functions */
/* Different paths are used for the GUI version */ /* Different paths are used for the GUI version */
@ -689,31 +652,27 @@ bool IDAPython_Init(void)
(menu_item_callback_t *)IDAPython_Menu_Callback, (menu_item_callback_t *)IDAPython_Menu_Callback,
(void *)IDAPYTHON_RUNSTATEMENT); (void *)IDAPYTHON_RUNSTATEMENT);
/* Add Load Python file menu item*/
result = add_menu_item("File/Load file/IDC file...", "P~y~thon file...", result = add_menu_item("File/Load file/IDC file...", "P~y~thon file...",
"Alt-9", SETMENU_APP, "Alt-9", SETMENU_APP,
(menu_item_callback_t *)IDAPython_Menu_Callback, (menu_item_callback_t *)IDAPython_Menu_Callback,
(void *)IDAPYTHON_RUNFILE); (void *)IDAPYTHON_RUNFILE);
if (!result) if (!result)
{
add_menu_item("File/IDC command...", "P~y~thon file...", add_menu_item("File/IDC command...", "P~y~thon file...",
"Alt-9", SETMENU_APP, "Alt-9", SETMENU_APP,
(menu_item_callback_t *)IDAPython_Menu_Callback, (menu_item_callback_t *)IDAPython_Menu_Callback,
(void *)IDAPYTHON_RUNFILE); (void *)IDAPYTHON_RUNFILE);
}
/* Add View Python Scripts menu item*/
result = add_menu_item("View/Open subviews/Show strings", "Python S~c~ripts", result = add_menu_item("View/Open subviews/Show strings", "Python S~c~ripts",
"Alt-7", SETMENU_APP, "Alt-7", SETMENU_APP,
(menu_item_callback_t *)IDAPython_Menu_Callback, (menu_item_callback_t *)IDAPython_Menu_Callback,
(void *)IDAPYTHON_SCRIPTBOX); (void *)IDAPYTHON_SCRIPTBOX);
if (!result) if (!result)
{
add_menu_item("View/Open subviews/Problems", "Python S~c~ripts", add_menu_item("View/Open subviews/Problems", "Python S~c~ripts",
"Alt-7", SETMENU_APP, "Alt-7", SETMENU_APP,
(menu_item_callback_t *)IDAPython_Menu_Callback, (menu_item_callback_t *)IDAPython_Menu_Callback,
(void *)IDAPYTHON_SCRIPTBOX); (void *)IDAPYTHON_SCRIPTBOX);
}
/* Register a RunPythonStatement() function for IDC */ /* Register a RunPythonStatement() function for IDC */
set_idc_func("RunPythonStatement", idc_runpythonstatement, idc_runpythonstatement_args); set_idc_func("RunPythonStatement", idc_runpythonstatement, idc_runpythonstatement_args);
@ -821,22 +780,17 @@ char wanted_hotkey[] = "Alt-9";
extern "C" extern "C"
{ {
plugin_t PLUGIN = { plugin_t PLUGIN = {
IDP_INTERFACE_VERSION, IDP_INTERFACE_VERSION,
0, // plugin flags 0, // plugin flags
init, // initialize init, // initialize
term, // terminate. this pointer may be NULL. term, // terminate. this pointer may be NULL.
run, // invoke plugin run, // invoke plugin
comment, // long comment about the plugin comment, // long comment about the plugin
// it could appear in the status line // it could appear in the status line
// or as a hint // or as a hint
help, // multiline help about the plugin help, // multiline help about the plugin
wanted_name, // the preferred short name of the plugin wanted_name, // the preferred short name of the plugin
wanted_hotkey // the preferred hotkey to run the plugin wanted_hotkey // the preferred hotkey to run the plugin
}; };
} }