[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

g_spawn & g_io_channel: part II




    Good day,
    Developers. I tried looking at the Anjuta source, I tried reading 
the docs, and tried some googleing around. But, when I tried to do it, I 
realized: I'm DUMB ^_^
    There's a code in attachment, if someone can give me a hint: all I 
want is to start a shell command and print out its stdout and stderr values.

    Thank You,
    Vladimir.

-- 
"This is it... This is where I belong... "

#include <glib.h>

static gboolean stdout_read( GIOChannel *source, GIOCondition condition, gpointer data );
static gboolean stderr_read( GIOChannel *source, GIOCondition condition, gpointer data );

int main( int argc, char *argv[] )
{
	const char *cmd[20];
	int i = 0;
	int stdout_pipe;
	int stderr_pipe;
	int pid;
	GError *error;
	GMainLoop *loop;
	guint stdout_tag, stderr_tag;
	GIOChannel *channel;
	
	gtk_init( &argc, &argv );
		
	i = 0;
	cmd[i++] = "ls";
	cmd[i++] = "-lh";
	cmd[i++] = "/var/log/packages";
	cmd[i++] = NULL;
	
	error = NULL;
	
	if ( !g_spawn_async_with_pipes( NULL,
					(char **)cmd,
					NULL,
					G_SPAWN_SEARCH_PATH,
					NULL, NULL,
					&pid,
					NULL,
					&stdout_pipe,
					&stderr_pipe,
					&error ) ) {
		
		g_warning( "command failed: %s\n", error->message );
		g_error_free( error );
	} else {
		loop = g_main_loop_new( NULL, FALSE );
		
		channel = g_io_channel_unix_new( stdout_pipe );
		/*g_io_channel_set_encoding( channel, NULL, NULL );*/
		stdout_tag = g_io_add_watch( channel,
						( G_IO_IN | G_IO_HUP | G_IO_ERR ),
						stdout_read,
						NULL );
		
		g_io_channel_unref( channel );
		
		channel = g_io_channel_unix_new( stderr_pipe );
		/*g_io_channel_set_encoding( channel, NULL, NULL );*/
		stderr_tag = g_io_add_watch ( channel,
						( G_IO_IN | G_IO_HUP | G_IO_ERR ),
						stderr_read,
						NULL );
		g_io_channel_unref( channel );
		
		/*g_main_loop_run( loop );
		g_main_loop_unref( loop );
		g_source_remove( stdout_tag );
		g_source_remove( stderr_tag );*/
	}
	
	gtk_main();
	
	return 0;
}

static gboolean stdout_read( GIOChannel *source, GIOCondition condition, gpointer data )
{
	char *line;
	GIOStatus status;
	
	status = g_io_channel_read_line( source,
					&line, NULL, NULL, NULL);
	
	if ( status == G_IO_STATUS_NORMAL ) {
		
		g_free( line );
	}
	g_print( "STDOUT: %s\n", line );
	
	return TRUE;
}

static gboolean stderr_read( GIOChannel *source, GIOCondition condition, gpointer data )
{
	char *line;
	GIOStatus status;
	
	status = g_io_channel_read_line( source,
					&line, NULL, NULL, NULL);
	
	if ( status == G_IO_STATUS_NORMAL ) {
		
		g_free( line );
	}
	g_print( "STDERR: %s\n", line );
	
	return TRUE;
}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]