From 0ec7c3edfd6ec32aaf6cd37e745568a82bf9e565 Mon Sep 17 00:00:00 2001 From: Peng Wu Date: Tue, 20 Apr 2010 15:48:43 +0800 Subject: add lua-ext-console.c --- lua/Makefile.am | 14 ++++++ lua/lua-ext-console.c | 128 ++++++++++++++++++++++++++++++++++++++++++++++++++ lua/lua-plugin.c | 6 +-- lua/lua-plugin.h | 2 +- 4 files changed, 146 insertions(+), 4 deletions(-) create mode 100644 lua/lua-ext-console.c (limited to 'lua') diff --git a/lua/Makefile.am b/lua/Makefile.am index 4700bf2..8b6ae53 100644 --- a/lua/Makefile.am +++ b/lua/Makefile.am @@ -43,6 +43,7 @@ TESTS = \ $(NULL) noinst_PROGRAMS = \ + lua-ext-console $(TESTS) \ $(NULL) @@ -58,3 +59,16 @@ test_lua_plugin_CFLAGS = \ test_lua_plugin_LDADD = \ libpylua.la \ $(NULL) + +lua_ext_console_SOURCES = \ + lua-ext-console.c \ + $(NULL) + +lua_ext_console_CFLAGS = \ + @IBUS_CFLAGS@ \ + @LUA_CFLAGS@ \ + $(NULL) + +lua_ext_console_LDADD = \ + libpylua.la \ + $(NULL) diff --git a/lua/lua-ext-console.c b/lua/lua-ext-console.c new file mode 100644 index 0000000..6f61f25 --- /dev/null +++ b/lua/lua-ext-console.c @@ -0,0 +1,128 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include "lua-plugin.h" + +void print_help(){ + printf("Usage: lua_ext_console [SCRIPT_FILE] ...\n"); + printf("Loads one or more script files then evaluates lua extension modes in an interactive shell.\n"); +} + +void print_interactive_help(){ + printf("i \t\t\t - lists all commands.\n"); + printf("i [COMMAND] \t\t - evaluates command without argument. \n"); + printf("i [COMMAND] [ARGUMENT] \t evaluates command with argument. \n"); + /* printf("g [TRIGGER_STRING] \t\t - tests a trigger string, fire trigger if hit.\n"); */ + printf("quit \t\t\t - quit the shell.\n"); + printf("help \t\t\t - show this message.\n"); +} + +void list_all_commands(IBusEnginePlugin * plugin){ + const GArray * commands = ibus_engine_plugin_get_available_commands(plugin); + size_t i; + for ( i = 0; i < commands->len; ++i ){ + lua_command_t * command = &g_array_index(commands, lua_command_t, i); + printf("%s.%s >\t", command->command_name, + command->description); + } + printf("\n"); +} + +int print_lua_call_result(IBusEnginePlugin * plugin, size_t num){ + if ( 1 == num ) { + const char * result = ibus_engine_plugin_get_retval(plugin); + printf("result: %s.\n", result); + } + if ( num > 1) { + GArray * results = ibus_engine_plugin_get_retvals(plugin); + size_t i; + for ( i = 0; i < results->len; ++i) { + const char * result = g_array_index(results, const char *, i); + printf("%d.%s >\t", i, result); + } + printf("\n"); + } +} + +int do_lua_call(IBusEnginePlugin * plugin, const char * command_name, const char * argument){ + lua_command_t * command; + size_t num; + + g_return_if_fail(2 == strlen(command_name)); + command = ibus_engine_plugin_lookup_command(plugin, command_name); + if ( NULL == command) { + fprintf(stderr, "command %s doesn't exist.\n", command); + return 1; + } + + num = ibus_engine_plugin_call(plugin, command->lua_function_name, argument); + print_lua_call_result(plugin, num); + return 0; +} + +int main(int argc, char * argv[]){ + char * line = NULL; + size_t len = 0; + ssize_t read; + int i; + + if ( 1 == argc ){ + print_help(); + exit(1); + } + + g_type_init(); + + IBusEnginePlugin * plugin = ibus_engine_plugin_new(); + + for ( i = 1; i < argc; ++i){ + ibus_engine_plugin_load_lua_script(plugin, argv[i]); + } + + printf("Lua Plugin Console for ibus-pinyin.\n"); + printf("Type ? for more information.\n"); + printf("> "); + + while ((read = getline(&line, &len, stdin)) != -1) { + line[read - 1] = '\0'; + gchar ** strs = g_strsplit_set(line, " \t", 0); + size_t len = g_strv_length(strs); + switch (len){ + case 0: + print_interactive_help(); + break; + case 1: + if ( 0 == strcmp("quit", strs[0]) ) + exit(EXIT_SUCCESS); + if ( 0 == strcmp("help", strs[0]) || 0 == strcmp("?", strs[0]) ) + print_interactive_help(); + if ( 0 == strcmp("i", strs[0]) ) + list_all_commands(plugin); + break; + case 2: + if ( 0 == strcmp("i", strs[0])) + do_lua_call(plugin, strs[1], NULL); + if ( 0 == strcmp("g", strs[0])) + fprintf(stderr, "ime trigger unimplemented."); + break; + case 3: + if ( 0 == strcmp("i", strs[0])) + do_lua_call(plugin, strs[1], strs[2]); + break; + default: + fprintf(stderr, "wrong arguments."); + break; + } + g_strfreev(strs); + printf("> "); + } + + if (line) + free(line); + + return EXIT_SUCCESS; +} diff --git a/lua/lua-plugin.c b/lua/lua-plugin.c index d256fd1..5abe4d9 100644 --- a/lua/lua-plugin.c +++ b/lua/lua-plugin.c @@ -17,7 +17,7 @@ G_DEFINE_TYPE (IBusEnginePlugin, ibus_engine_plugin, G_TYPE_OBJECT); static void lua_command_clone(lua_command_t * command, lua_command_t * new_command){ new_command->command_name = g_strdup(command->command_name); - new_command->lua_function_name = g_strdup(command->command_name); + new_command->lua_function_name = g_strdup(command->lua_function_name); new_command->description = g_strdup(command->description); new_command->leading = g_strdup(command->leading); new_command->help = g_strdup(command->help); @@ -155,10 +155,10 @@ gboolean ibus_engine_plugin_add_command(IBusEnginePlugin * plugin, lua_command_t return TRUE; } -lua_command_t * ibus_engine_plugin_lookup_command(IBusEnginePlugin * plugin, const char * command){ +lua_command_t * ibus_engine_plugin_lookup_command(IBusEnginePlugin * plugin, const char * command_name){ IBusEnginePluginPrivate * priv = IBUS_ENGINE_PLUGIN_GET_PRIVATE(plugin); GArray * lua_commands = priv->lua_commands; - lua_command_t lookup_command = {.command_name = command, }; + lua_command_t lookup_command = {.command_name = command_name, }; lua_command_t * result = bsearch(&lookup_command, lua_commands->data, lua_commands->len, sizeof(lua_command_t), compare_command); return result; diff --git a/lua/lua-plugin.h b/lua/lua-plugin.h index 6113d69..df2cb74 100644 --- a/lua/lua-plugin.h +++ b/lua/lua-plugin.h @@ -85,7 +85,7 @@ const GArray * ibus_engine_plugin_get_available_commands(IBusEnginePlugin * plug * command must be an 2-char long string. * return the matched command. */ -lua_command_t * ibus_engine_plugin_lookup_command(IBusEnginePlugin * plugin, const char * command); +lua_command_t * ibus_engine_plugin_lookup_command(IBusEnginePlugin * plugin, const char * command_name); /** * retval int: returns the number of results, -- cgit