GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Tue Dec 23, 2014 12:11 am

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: g_spawn_async_with_pipes issues
PostPosted: Fri Dec 28, 2007 9:43 pm 
Offline
Familiar Face

Joined: Tue Dec 25, 2007 4:51 pm
Posts: 10
Ok, I've searched as much as I can on this one, but can't seem to find the exact method to get this function working. I'll try and give as much information as possible, but attempt to keep it brief. So please keep in mind, this is just a snippet of code, but if you can't see what I'm trying to do, I can post the complete thing:

Code:
MyWidgets *widgets;
const gchar *host;
gchar   *command;
GPid    pid;
GError   *error = NULL; 
gulong   flags = G_SPAWN_SEARCH_PATH|G_SPAWN_STDOUT_TO_DEV_NULL|G_SPAWN_STDERR_TO_DEV_NULL;

widgets = (MyWidgets *)user_data;
host = gtk_entry_get_text (GTK_ENTRY (widgets->host_entry));
const gchar *user = g_get_home_dir();
gchar *appdir = g_build_path("/", user, ".connmgr", NULL);   

const gchar    *sshport;
const gchar    *sshuser;
const gchar    *sshoptions;

sshport = gtk_entry_get_text (GTK_ENTRY (widgets->ssh_port_sbtn));
sshuser = gtk_entry_get_text (GTK_ENTRY (widgets->ssh_user_entry));
sshoptions = gtk_entry_get_text (GTK_ENTRY (widgets->ssh_options_entry));
          
command = g_strconcat ("gnome-terminal --geometry 110x25 -e \"ssh -p ",
sshport, " ",
sshoptions, " ",
sshuser, "@",
host, "\"",
NULL);

g_spawn_async_with_pipes( appdir, // const gchar *working_directory
command,    // gchar **argv
NULL,       // gchar **envp
flags,       // GSpawnFlags flags
NULL,       // GSpawnChildSetupFunc child_setup
NULL,       // gpointer user_data
&pid,       // GPid *child_pid
NULL,       // gint *standard_input
NULL,   // gint *standard_output
NULL,       // gint *standard_error
&error);    // GError **error


Trying to compile the above using: gcc -o connmgr src/connmgr.c `pkg-config --cflags --libs libglade-2.0`-export-dynamic -Wall

It fails with the following:
    src/connmgr.c:160: warning: passing argument 2 of 'g_spawn_async_with_pipes' from incompatible pointer type


What's the proper method of doing what I'm trying to do here?

Thanks,
-Lup


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 28, 2007 10:06 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
What that error is telling you is that you are passing the wrong type of pointer to that function as the second argument.

The prototype (as you have shown in your comment) is gchar**, yet you're passing it a gchar*. See the difference? One is an array of gchars, a single string. The other is an array or arrays of gchars or, put another way, an array of strings.

So, instead of passing it one long string (as you are doing using g_strconcat) you should pass it an array of strings. Something more like:

Code:
char *command[] = { "gnome-terminal", "110x25", NULL };


So instead of
[code=]command -> "some command\0"[/code]

You need
[code=]command -> 0 -> "some\0"
-> 1 -> "command\0"
-> NULL[/code]

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 28, 2007 10:56 pm 
Offline
Familiar Face

Joined: Tue Dec 25, 2007 4:51 pm
Posts: 10
Ok, thanks....so a way around the above would be to add/alter the code to do this:

Code:
command = g_strconcat ("gnome-terminal --geometry 110x25 -e \"ssh -p ",
sshport, " ",
sshoptions, " ",
sshuser, "@",
host, "\"",
NULL);

char *runcommand[] = {command,   NULL};
                              
g_spawn_async_with_pipes(appdir,    // const gchar *working_directory
runcommand,    // gchar **argv
NULL,       // gchar **envp
flags,       // GSpawnFlags flags
NULL,       // GSpawnChildSetupFunc child_setup
NULL,       // gpointer user_data
&pid,       // GPid *child_pid
NULL,       // gint *standard_input
NULL,   // gint *standard_output
NULL,       // gint *standard_error
&error);    // GError **error
...this seems to work, although I'm getting a different error. But I'll work that for now, as it has nothing to do with this particular string/array issue.

Thanks,
-Lup


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 29, 2007 1:15 am 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
Well, not really. The function takes program argument vector...

You could pass it through g_shell_parse_argv() first though.

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 29, 2007 2:15 am 
Offline
Familiar Face

Joined: Tue Dec 25, 2007 4:51 pm
Posts: 10
Ya know, you keep giving me these same day answers, and I'm going to start expecting them from you consistently ;-)

Seriously though, I appreciate it. I almost found the "best" way of doing it. After researching your suggestions, here is the updated code snippet:

Code:
MyWidgets *widgets;
const gchar *host;
gchar *command;
gint argc;
gchar **argv = NULL;
GPid pid;
GError *error = NULL; 
gulong flags = G_SPAWN_SEARCH_PATH|G_SPAWN_STDOUT_TO_DEV_NULL|G_SPAWN_STDERR_TO_DEV_NULL;

const gchar    *sshport;
const gchar    *sshuser;
const gchar    *sshoptions;
   
sshport = gtk_entry_get_text (GTK_ENTRY (widgets->ssh_port_sbtn));
sshuser = gtk_entry_get_text (GTK_ENTRY (widgets->ssh_user_entry));
sshoptions = gtk_entry_get_text (GTK_ENTRY (widgets->ssh_options_entry));

command = g_strdup_printf ("gnome-terminal --geometry 110x25 -e \"ssh -p %s %s %s@%s\"",
sshport,
sshoptions,
sshuser,
host);

g_shell_parse_argv(command,
&argc,
&argv,
NULL);

g_spawn_async_with_pipes( appdir,    // const gchar *working_directory
argv,    // gchar **argv
NULL,       // gchar **envp
flags,       // GSpawnFlags flags
NULL,       // GSpawnChildSetupFunc child_setup
NULL,       // gpointer user_data
&pid,       // GPid *child_pid
NULL,       // gint *standard_input
NULL,      // gint *standard_output
NULL,       // gint *standard_error
&error);    // GError **error

g_strfreev(argv);
g_free(command);
This is launching the command as expected. Appreciate it!

However, the original problem that started me down the g_spawn_async_with_pipes path is back. If I have a pipe "|" in the command, it doesn't launch. Meaning, if I were to replace that specified command with something like this:
Code:
command = g_strdup_printf ("echo %s | vncviewer -autopass -quality %s -compresslevel %s %s::%s",
vncpasswd,
vncquality,
vnccompresslevel,
host,
vncport);
...this doesn't work. If I remove the pipe (and thus alter the command a little), then it does. Looks like I need to look into g_spawn_async_with_pipes a little more, because I'm obviously missing something simple.

Thanks,
-Lup


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 30, 2007 12:45 am 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
Can't say I know the answer to this one off the top of my head. I've never had to execute commands with pipes in my own GTK+ applications. I'd be curious to tinker around but don't have time at the moment...

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group