static char *run_cmd;
static int expired;
+static void
+expand_and_exec(char *cmd)
+{
+ GString * expanded_cmd = g_string_new("");
+ 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;
+ g_spawn_command_line_async(expanded_cmd->str, &err);
+ g_string_free(expanded_cmd, 1);
+ if (err)
+ {
+ fprintf(stderr, "teatimer: Unable to run command: %s\n", err->message);
+ g_error_free(err);
+ }
+}
+
static void
it_tolls_for_thee(void)
{
{
if (!expired)
{
- GError *err = NULL;
- g_spawn_command_line_async(run_cmd, &err);
- if (err)
- {
- fprintf(stderr, "teatimer: Unable to run command: %s\n", err->message);
- g_error_free(err);
- }
+ expand_and_exec(run_cmd);
expired = 1;
}
}
fprintf(stderr, "Usage: teatimer [<options>] [<mm:ss>]\n\n\
Options:\n\
-r, --run=<cmd>\t\tRun a given program when the tea is ready\n\
+\t\t\t\t%%d will be expanded to timer name\n\
+\t\t\t\t%%%% will be expanded to %%\n\
");
exit(1);
}