Hi, I've just made a small glib code that read commands from from a command line interface prompt. It uses g_main_loop and a g_io_channel. I've attached the code. First: It nearly works as expected. The callback is called each time I enter a command, and it's parsed to the command handler. However, I want to expand this to system to use automatic string completion, (the system provided in glib), for all available commands. Now it only handles command as I press enter. I guess I need a extra callback for each keypress. This callback can check if I press <TAB> and then call g_completion_complete(), but how do I attach such a signal to the main loop? Then: Can this be a bug? It work fine unless I use a special scandinavian letter in my commands. Do I have to convert my string at any time? How? (I'm using win32 command prompt window) Example: myprompt> hello You typed: hello myprompt> Æsop ** (gnubg.exe:3128): WARNING **: Something went wrong Invalid byte sequence in conversion input -Øystein
#include <glib.h>
#include <glib/gprintf.h>
static void
prompt(){
g_printf("myprompt> ");
}
static void
handlecommand( gchar *sz ){
g_printf( "You typed: %s", sz );
prompt();
}
static gboolean
mycallback( GIOChannel *channel, GIOCondition cond, gpointer data)
{
gchar *str_return;
gsize length, terminator_pos;
GError *error = NULL;
if( g_io_channel_read_line( channel, &str_return, &length, &terminator_pos, &error) == G_IO_STATUS_ERROR )
g_warning("Something went wrong");
if ( error != NULL ) {
g_printf(error->message);
exit(1);
}
handlecommand( str_return );
g_free( str_return );
return TRUE;
}
int
main(int argc, char* argv[])
{
GMainLoop *mainloop = g_main_loop_new( NULL, FALSE);
#ifdef G_OS_WIN32
GIOChannel *channel = g_io_channel_win32_new_fd( STDIN_FILENO );
#else
GIOChannel *channel = g_io_channel_unix_new( STDIN_FILENO );
#endif
prompt();
g_io_add_watch( channel, G_IO_IN, mycallback, NULL );
g_main_loop_run(mainloop);
return 0;
}
Attachment:
signature.asc
Description: OpenPGP digital signature