[RFC PATCH 1/2] actions.c: make spawn() parse the arguments when raw is 1

Ali Gholami Rudi <[email protected]>
Newsgroups gmane.comp.window-managers.ratpoison.devel
Message-ID <20081119082218.GE4894@lilem>
Currently when raw argument is set, spawn fails if cmd contains
arguments.  This patch parses the arguments but doesn't handle escaped
spaces and quoted strings.
---
 src/actions.c |   36 ++++++++++++++++++++++++++++++++++--
 1 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/src/actions.c b/src/actions.c
index ab5162e..12eddd3 100644
--- a/src/actions.c
+++ b/src/actions.c
@@ -2574,11 +2574,39 @@ cmd_execf (int interactive, struct cmdarg **args)
   return cmdret_new (RET_SUCCESS, NULL);
 }
 
+#define MAXARGCNT	16
+#define MAXARGLEN	64
+
+static char args_cache[MAXARGCNT][MAXARGLEN];
+static char *args_result[MAXARGCNT + 1];
+
+static char **
+mkexecargs(char *str)
+{
+  int i;
+  for (i = 0; i < MAXARGCNT - 1; i++)
+    {
+      char *to = args_cache[i];
+      char *end = args_cache[i] + MAXARGLEN - 1;
+      while (isspace (*str))
+	str++;
+      if (!*str)
+	break;
+      while (*str && to < end && !isspace (*str))
+	*to++ = *str++;
+      *to = '\0';
+      args_result[i] = args_cache[i];
+    }
+  args_result[i] = NULL;
+  return args_result;
+}
+
 int
 spawn(char *cmd, int raw, rp_frame *frame)
 {
   rp_child_info *child;
   int pid;
+  char **execargs;
 
   pid = fork();
   if (pid == 0)
@@ -2596,8 +2624,12 @@ spawn(char *cmd, int raw, rp_frame *frame)
 #endif
       /* raw means don't run it through sh.  */
       if (raw)
-        execl (cmd, cmd, NULL);
-      execl("/bin/sh", "sh", "-c", cmd, NULL);
+	{
+	  execargs = mkexecargs (cmd);
+	  execvp (execargs[0], execargs);
+	}
+      else
+	execl ("/bin/sh", "sh", "-c", cmd, NULL);
       _exit(EXIT_FAILURE);
     }
 
-- 
1.6.0.2.GIT
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.