|
|
| Author |
Message |
|
|
cnewbie Familiar Face
Joined: 12 Jan 2010 Posts: 7
|
Posted: Wed Jan 13, 2010 11:53 am Post subject: Warning when compiling |
|
|
This code example was a great help for exactly what I was trying to do myself.
However, I get the following warning when compiling it:
| Code: (Plaintext) | 1 2 3 4
|
example.c: In function ‘on_execute_button_clicked’:
example.c:41: warning: format not a string literal and no format arguments
|
Line 41 being error->message; :
| Code: (Plaintext) | 1 2 3 4 5 6
|
dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_OK,
[color=red]error->message);[/color]
| |
|
| Back to top |
|
 |
tadeboro Never Seen the Sunlight
Joined: 23 Jul 2008 Posts: 2114 Location: Slovenia
|
Posted: Wed Jan 13, 2010 12:33 pm Post subject: Re: Warning when compiling |
|
|
Hello and welcome to the GTK+ forums.
| cnewbie wrote: | | Code: (Plaintext) | 1 2 3 4
|
example.c: In function ‘on_execute_button_clicked’:
example.c:41: warning: format not a string literal and no format arguments
| |
This warning is caused by the fact that gtk_message_dialog_new() takes printf-like template as fourth parameter, not gchar* directly. To fix this warning, gtk_message_dialog_new() call should look something like this:
| Code: (C) | 1 2 3 4 5 6
| dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_OK,
"%s",
error->message);
| |
|
| Back to top |
|
 |
cnewbie Familiar Face
Joined: 12 Jan 2010 Posts: 7
|
Posted: Wed Jan 13, 2010 1:14 pm Post subject: |
|
|
| tadeboro wrote: | | Hello and welcome to the GTK+ forums. |
Thanks :D
| tadeboro wrote: |
This warning is caused by the fact that gtk_message_dialog_new() takes printf-like template as fourth parameter, not gchar* directly. |
Ah, ok. I should look more into those specifications. Thanks for the quick answer. |
|
| Back to top |
|
 |
cnewbie Familiar Face
Joined: 12 Jan 2010 Posts: 7
|
Posted: Fri Jan 15, 2010 3:48 pm Post subject: |
|
|
Another question:
This works fine for me with commands like "ls -la", "df" etc. However, it doesn't work with f.ex. "ping localhost". Then the application just freezes. I take it things like ping need more complex handling to execute in this manner?
Also, if I type just "ping" on the command line, I feedback on how to use the command (because it misses an argument). Is this possible to get in the gui? |
|
| Back to top |
|
 |
LGV Familiar Face
Joined: 31 Dec 2009 Posts: 5 Location: Argentina
|
Posted: Fri Jan 15, 2010 11:14 pm Post subject: |
|
|
1) You just have to convert "ping localhost" into a NULL-terminated string vector, like this:
| Code: (C) | 1 2 3 4 5 6
|
gchar* string_vector[3];
string_vector[0] = "ping";
string_vector[1] = "localhost";
string_vector[2] = NULL;
|
And pass that string as the "command_line" parameter to g_spawn_command_line_sync (reference). You can do that conversion with the g_shell_parse_argv function (reference).
2) Assuming that ping uses the standard error to output errors (wich it probably does), then yes, you can retrieve the error message trough the "standard_error" parameter of g_spawn_command_line_sync. |
|
| Back to top |
|
 |
cnewbie Familiar Face
Joined: 12 Jan 2010 Posts: 7
|
Posted: Sat Jan 16, 2010 1:05 pm Post subject: |
|
|
Hm...
I have a text which I get from a textentry widget:
| Code: (Plaintext) | 1 2 3 4
|
const gchar *adr;
adr = gtk_entry_get_text(GTK_ENTRY(widgets->adrentry));
|
Then I try to convert this for use is the string_vector:
| Code: (Plaintext) | 1 2 3 4 5 6
|
gint argc;
gchar **argv = NULL;
gchar *padr;
padr = g_shell_parse_argv(adr, &argc, &argv, NULL);
|
This gives no warnings.
But when I try inserting it into the string_vector:
| Code: (Plaintext) | 1 2 3 4 5 6
|
gchar *string_vector[3];
string_vector[0] = "ping";
string_vector[1] = padr;
string_vector[2] = NULL;
|
This gives the warning
| Code: (Plaintext) | 1 2 3
|
warning: assignment makes pointer from integer without a cast
|
For the "string_vector[1] = padr;" line.
Now, I understand this ERROR sometimes, but often I'm confused. |
|
| Back to top |
|
 |
tadeboro Never Seen the Sunlight
Joined: 23 Jul 2008 Posts: 2114 Location: Slovenia
|
Posted: Sat Jan 16, 2010 1:11 pm Post subject: |
|
|
Hello.
g_shell_parse_argv() returns gboolean, not gchar *, which makes compiler unhappy.
Tadej |
|
| Back to top |
|
 |
cnewbie Familiar Face
Joined: 12 Jan 2010 Posts: 7
|
Posted: Sat Jan 16, 2010 1:35 pm Post subject: |
|
|
Sorry, I don't get it.
- Do need to convert the text from the TextEntry before inserting it into the string_vector?
- Then, do I need to convert the string_vector before passing it to g_spawn_command_line_sync? |
|
| Back to top |
|
 |
LGV Familiar Face
Joined: 31 Dec 2009 Posts: 5 Location: Argentina
|
Posted: Sat Jan 16, 2010 6:01 pm Post subject: |
|
|
I'm sorry, I did a mistake. g_spawn_command_line_sync doesn't take a string vector as parameter, it just takes a command line; the function does the conversion from command line to string vector for you. So, you just have to call it this way:
| Code: (C) | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| #include <stdio.h>
#include <stdlib.h>
#include <glib.h>
int main(int argc, char** argv)
{
gchar* std_out = NULL;
gchar* std_err = NULL;
gint exit_code;
GError* error = NULL;
gchar* command_line = "ping localhost";
/* If fails, output errors and exit */
if(!g_spawn_command_line_sync(command_line, &std_out, &std_err, &exit_code, &error))
{
if(error)
{
g_print("Error: %s\n\n", error->message);
g_error_free(error);
error = NULL;
getchar();
exit(EXIT_FAILURE);
}
}
if(std_out)
{
g_print("std_out: %s\n\n", std_out);
g_free(std_out);
std_out = NULL;
}
if(std_err)
{
g_print("std_err: %s\n\n", std_err);
g_free(std_err);
std_err = NULL;
}
getchar();
return EXIT_SUCCESS;
} | |
|
| Back to top |
|
 |
tadeboro Never Seen the Sunlight
Joined: 23 Jul 2008 Posts: 2114 Location: Slovenia
|
Posted: Sat Jan 16, 2010 7:03 pm Post subject: |
|
|
Hello all.
Just one note. This code will not work with commands that do not terminate on their own. For example, "ping localhost" will never return from g_spawn_command_line_sync() function call and thus application will appear frozen. For commands that do not terminate or take longer time to finish, async version of spawning would be better choice.
Tadej |
|
| Back to top |
|
 |
cnewbie Familiar Face
Joined: 12 Jan 2010 Posts: 7
|
Posted: Sat Jan 16, 2010 7:08 pm Post subject: |
|
|
Thanks. However, when using the above code, I experience the exact same thing as using the way described in the beginning of this thread: The application just hangs when activating the code that runs the command, and I have to force quit it.
EDIT: Ah. tadeboro pointed that out just as I was writing my last comment.
Asyncroneous spawning seems to be what I need. My point with trying to learning this is that I want to make an application for testing SMTP server for things like Auth,, Vrfy etc., for use with a mail scanning gateway. So I figured making a "graphical pinger" would get me started. |
|
| Back to top |
|
 |
tadeboro Never Seen the Sunlight
Joined: 23 Jul 2008 Posts: 2114 Location: Slovenia
|
Posted: Sat Jan 16, 2010 7:50 pm Post subject: |
|
|
Hello.
I wrote a blog post about spawning processes using glib. You may find some interesting pointers there.
Tadej |
|
| Back to top |
|
 |
cnewbie Familiar Face
Joined: 12 Jan 2010 Posts: 7
|
Posted: Sun Jan 17, 2010 4:15 pm Post subject: |
|
|
| Ah, thanks. There are several pointers there, several of which I'm sure will at least tech me _something_. |
|
| Back to top |
|
 |
|