From 6d46296fbc037ca53838a6996b880fc8d3b3a937 Mon Sep 17 00:00:00 2001 From: Jiri Kalvoda Date: Thu, 15 Jul 2021 15:05:13 +0200 Subject: [PATCH] Add expansions in cmd --- teatimer.c | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/teatimer.c b/teatimer.c index b21d15d..56d78c1 100644 --- a/teatimer.c +++ b/teatimer.c @@ -25,6 +25,38 @@ static time_t alarm_time; 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) { @@ -32,13 +64,7 @@ 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; } } @@ -208,6 +234,8 @@ usage(void) fprintf(stderr, "Usage: teatimer [] []\n\n\ Options:\n\ -r, --run=\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); } -- 2.39.2