merge in Duclares better way to escape things in run_command
authorDieter Plaetinck <dieter@plaetinck.be>
Sun, 17 May 2009 12:34:59 +0000 (14:34 +0200)
committerDieter Plaetinck <dieter@plaetinck.be>
Sun, 17 May 2009 12:34:59 +0000 (14:34 +0200)
1  2 
uzbl.c

diff --cc uzbl.c
--- 1/uzbl.c
--- 2/uzbl.c
+++ b/uzbl.c
@@@ -722,25 -720,23 +722,23 @@@ expand_template(const char *template) 
  /* --End Statusbar functions-- */
  
  
- // make sure to put "" or '' around args, so that if there is whitespace we can still keep arguments together.
- // note: if your args contain ', you must wrap them in "" (you cannot escape inside '')
- // if your args contain ", you should wrap them in "" and escape them
 -// make sure to put '' around args, so that if there is whitespace we can still keep arguments together.
++// make sure that the args string you pass can properly be interpreted (eg properly escaped against whitespace, quotes etc)
  static gboolean
  run_command (const char *command, const char *args, const gboolean sync, char **stdout) {
     //command <uzbl conf> <uzbl pid> <uzbl win id> <uzbl fifo file> <uzbl socket file> [args]
-     GString* to_execute = g_string_new ("");
+     GString *to_execute = g_string_new ("");
      GError *err = NULL;
-     gchar* cmd = g_strstrip(g_strdup(command));
+     gchar *cmd = g_strstrip(g_strdup(command));
+     gchar *qcfg = (uzbl.state.config_file ? g_shell_quote(uzbl.state.config_file) : g_strdup("''"));
+     gchar *qfifo = (uzbl.comm.fifo_path ? g_shell_quote(uzbl.comm.fifo_path) : g_strdup("''"));
+     gchar *qsock = (uzbl.comm.socket_path ? g_shell_quote(uzbl.comm.socket_path) : g_strdup("''"));
+     gchar *quri = (uzbl.state.uri ? g_shell_quote(uzbl.state.uri) : g_strdup("''"));
+     gchar *qtitle = (uzbl.gui.main_title ? g_shell_quote(uzbl.gui.main_title) : g_strdup("''"));
 -    
++
      gboolean result;
-     const char* search = "\"";
-     const char* replace = "\\\"";
-     g_string_printf (to_execute, "%s '%s' '%i' '%i' '%s' '%s'",
-                      cmd, (uzbl.state.config_file ? uzbl.state.config_file : "(null)"),
-                      (int) getpid(), (int) uzbl.xwin, uzbl.comm.fifo_path,
-                      uzbl.comm.socket_path);
-     g_string_append_printf (to_execute, " \"%s\" \"%s\"",
-                     uzbl.state.uri,
-                     str_replace(search, replace, uzbl.gui.main_title));
+     g_string_printf (to_execute, "%s %s '%i' '%i' %s %s",
+                      cmd, qcfg, (int) getpid(), (int) uzbl.xwin, qfifo, qsock);
+     g_string_append_printf (to_execute, " %s %s", quri, qtitle);
      if(args) g_string_append_printf (to_execute, " %s", args);
  
      if (sync) {
          g_printerr("error on run_command: %s\n", err->message);
          g_error_free (err);
      }
 -    
++
+     g_free (qcfg);
+     g_free (qfifo);
+     g_free (qsock);
+     g_free (quri);
+     g_free (qtitle);
      g_free (cmd);
      return result;
  }