]> mj.ucw.cz Git - teatimer.git/commitdiff
Add expansions in cmd
authorJiri Kalvoda <jirikalvoda@kam.mff.cuni.cz>
Thu, 15 Jul 2021 13:05:13 +0000 (15:05 +0200)
committerMartin Mares <mj@ucw.cz>
Fri, 13 Aug 2021 11:05:45 +0000 (13:05 +0200)
teatimer.c

index b21d15d57cb2551d96da6364db92d46002d43003..56d78c17543e9a549ebea043af3fda39434eeba9 100644 (file)
@@ -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 [<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);
 }