static int // return pid of new process or 0 if failed
expand_and_exec(char *cmd)
{
- GString *expanded_cmd = g_string_new("");
- if (!expanded_cmd)
- return 0;
- for (int i=0; cmd[i]; i++)
- {
- if (cmd[i]=='%' && cmd[i+1]=='%')
- {
- i++;
- g_string_append_c(expanded_cmd, '%');
- }
- else
- if (cmd[i]=='%' && cmd[i+1]=='n')
- {
- i++;
- const gchar *name = gtk_entry_get_text(GTK_ENTRY(namebox));
- g_string_append(expanded_cmd, name);
- }
- else
- g_string_append_c(expanded_cmd, cmd[i]);
- }
-
GError *err = NULL;
gint argc;
gchar ** argv = NULL;
int pid;
- g_shell_parse_argv(expanded_cmd->str, &argc, &argv, &err);
- g_string_free(expanded_cmd, 1);
+ g_shell_parse_argv(cmd, &argc, &argv, &err);
+ GString ** expanded_argv = malloc(sizeof(expanded_argv[0])*argc);
+ if (!expanded_argv)
+ return 0;
+ gchar ** expanded_argv_gchar = malloc(sizeof(expanded_argv_gchar[0])*(argc+1));
if (err)
{
- fprintf(stderr, "teatimer: Unable to run command: %s\n", err->message);
+ fprintf(stderr, "teatimer: Unable to parse command: %s\n", err->message);
g_error_free(err);
return 0;
}
- g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, &pid, &err);
+ if(!expanded_argv_gchar) return 0;
+ for (int i=0; i<argc; i++)
+ {
+ expanded_argv[i] = g_string_new("");
+ for (int j=0; argv[i][j]; j++)
+ {
+ if (argv[i][j]=='%' && argv[i][j+1]=='%')
+ {
+ j++;
+ g_string_append_c(expanded_argv[i], '%');
+ }
+ else
+ if (argv[i][j]=='%' && argv[i][j+1]=='n')
+ {
+ j++;
+ const gchar * name = gtk_entry_get_text(GTK_ENTRY(namebox));
+ g_string_append(expanded_argv[i], name);
+ }
+ else
+ g_string_append_c(expanded_argv[i], argv[i][j]);
+ }
+ expanded_argv_gchar[i] = expanded_argv[i]->str;
+ }
+ expanded_argv_gchar[argc]=NULL;
+ g_spawn_async(NULL, expanded_argv_gchar, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, &pid, &err);
g_strfreev(argv);
+ for (int i=0; i<argc; i++)
+ g_string_free(expanded_argv[i], 1);
if (err)
{
fprintf(stderr, "teatimer: Unable to run command: %s\n", err->message);