static void c_api_register_command(const char *filename, const char *command_name, int min_args, int max_args, char **synopsis, const char *description, char *arguments[][2], char **examples, void(*callback)(char **args)) { char *plugin_name = _c_plugin_name(filename); log_debug("Register command %s for %s", command_name, plugin_name); CommandWrapper *wrapper = malloc(sizeof(CommandWrapper)); wrapper->func = callback; api_register_command(plugin_name, command_name, min_args, max_args, synopsis, description, arguments, examples, wrapper, c_command_callback, free); free(plugin_name); }
static PyObject* python_api_register_command(PyObject *self, PyObject *args) { PyObject *command_name = NULL; int min_args = 0; int max_args = 0; PyObject *synopsis = NULL; PyObject *description = NULL; PyObject *arguments = NULL; PyObject *examples = NULL; PyObject *p_callback = NULL; if (!PyArg_ParseTuple(args, "OiiOOOOO", &command_name, &min_args, &max_args, &synopsis, &description, &arguments, &examples, &p_callback)) { Py_RETURN_NONE; } char *command_name_str = python_str_or_unicode_to_string(command_name); char *description_str = python_str_or_unicode_to_string(description); char *plugin_name = _python_plugin_name(); log_debug("Register command %s for %s", command_name_str, plugin_name); if (p_callback && PyCallable_Check(p_callback)) { Py_ssize_t len = PyList_Size(synopsis); char *c_synopsis[len == 0 ? 0 : len+1]; Py_ssize_t i = 0; for (i = 0; i < len; i++) { PyObject *item = PyList_GetItem(synopsis, i); char *c_item = python_str_or_unicode_to_string(item); c_synopsis[i] = c_item; } c_synopsis[len] = NULL; Py_ssize_t args_len = PyList_Size(arguments); char *c_arguments[args_len == 0 ? 0 : args_len+1][2]; i = 0; for (i = 0; i < args_len; i++) { PyObject *item = PyList_GetItem(arguments, i); Py_ssize_t len2 = PyList_Size(item); if (len2 != 2) { Py_RETURN_NONE; } PyObject *arg = PyList_GetItem(item, 0); char *c_arg = python_str_or_unicode_to_string(arg); c_arguments[i][0] = c_arg; PyObject *desc = PyList_GetItem(item, 1); char *c_desc = python_str_or_unicode_to_string(desc); c_arguments[i][1] = c_desc; } c_arguments[args_len][0] = NULL; c_arguments[args_len][1] = NULL; len = PyList_Size(examples); char *c_examples[len == 0 ? 0 : len+1]; i = 0; for (i = 0; i < len; i++) { PyObject *item = PyList_GetItem(examples, i); char *c_item = python_str_or_unicode_to_string(item); c_examples[i] = c_item; } c_examples[len] = NULL; allow_python_threads(); api_register_command(plugin_name, command_name_str, min_args, max_args, c_synopsis, description_str, c_arguments, c_examples, p_callback, python_command_callback, NULL); free(command_name_str); free(description_str); i = 0; while (c_synopsis[i] != NULL) { free(c_synopsis[i++]); } i = 0; while (c_arguments[i] != NULL && c_arguments[i][0] != NULL) { free(c_arguments[i][0]); free(c_arguments[i][1]); i++; } i = 0; while (c_examples[i] != NULL) { free(c_examples[i++]); } disable_python_threads(); } free(plugin_name); Py_RETURN_NONE; }