From ce13b70c5a0fae3788dc4d551fff24dd5c001649 Mon Sep 17 00:00:00 2001
From: Lukas Fleischer <calcurse@cryptocrack.de>
Date: Wed, 16 May 2012 15:55:40 +0200
Subject: Allow passing additional parameters to shell_exec()

This allows for specifying multiple command line parameters to be passed
on to the command in a way similar to fork_exec(). This is useful if we
want to wrap editor or pager invocations in a shell.

Signed-off-by: Lukas Fleischer <calcurse@cryptocrack.de>
---
 src/calcurse.h |  2 +-
 src/day.c      |  3 ++-
 src/todo.c     |  3 ++-
 src/utils.c    | 43 ++++++++++++++++++++++++++++++++++++++++---
 4 files changed, 45 insertions(+), 6 deletions(-)

(limited to 'src')

diff --git a/src/calcurse.h b/src/calcurse.h
index 5165f25..dad9f44 100644
--- a/src/calcurse.h
+++ b/src/calcurse.h
@@ -930,7 +930,7 @@ void         str_toupper (char *);
 void         file_close (FILE *, const char *);
 void         psleep (unsigned);
 int          fork_exec (int *, int *, const char *, const char *const *);
-int          shell_exec (int *, int *, const char *);
+int          shell_exec (int *, int *, const char *, const char *const *);
 int          child_wait (int *, int *, int);
 void         press_any_key (void);
 void         print_apoint (const char *, long, struct apoint *);
diff --git a/src/day.c b/src/day.c
index 43a5aec..ab362c0 100644
--- a/src/day.c
+++ b/src/day.c
@@ -1180,6 +1180,7 @@ void
 day_pipe_item (void)
 {
   char cmd[BUFSIZ] = "";
+  char const *arg[] = { cmd, NULL };
   int pout;
   int pid;
   FILE *fpout;
@@ -1196,7 +1197,7 @@ day_pipe_item (void)
     return;
 
   wins_prepare_external ();
-  if ((pid = shell_exec (NULL, &pout, cmd)))
+  if ((pid = shell_exec (NULL, &pout, *arg, arg)))
     {
       fpout = fdopen (pout, "w");
 
diff --git a/src/todo.c b/src/todo.c
index 24bcec6..50180f4 100644
--- a/src/todo.c
+++ b/src/todo.c
@@ -469,6 +469,7 @@ void
 todo_pipe_item (void)
 {
   char cmd[BUFSIZ] = "";
+  char const *arg[] = { cmd, NULL };
   int pout;
   int pid;
   FILE *fpout;
@@ -479,7 +480,7 @@ todo_pipe_item (void)
     return;
 
   wins_prepare_external ();
-  if ((pid = shell_exec (NULL, &pout, cmd)))
+  if ((pid = shell_exec (NULL, &pout, *arg, arg)))
     {
       fpout = fdopen (pout, "w");
 
diff --git a/src/utils.c b/src/utils.c
index d9f3737..36abb30 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -1015,10 +1015,47 @@ fork_exec (int *pfdin, int *pfdout, const char *path, const char *const *arg)
 
 /* Execute an external program in a shell. */
 int
-shell_exec (int *pfdin, int *pfdout, const char *cmd)
+shell_exec (int *pfdin, int *pfdout, const char *path, const char *const *arg)
 {
-  const char *arg[] = { "/bin/sh", "-c", cmd, NULL };
-  return fork_exec (pfdin, pfdout, *arg, arg);
+  int argc, i;
+  const char **narg;
+  char *arg0 = NULL;
+  int ret;
+
+  for (argc = 0; arg[argc]; argc++)
+    ;
+
+  if (argc < 1)
+    return -1;
+
+  narg = mem_calloc (argc + 4, sizeof (const char *));
+
+  narg[0] = "sh";
+  narg[1] = "-c";
+
+  if (argc > 1)
+    {
+      arg0 = mem_malloc (strlen (path) + 6);
+      sprintf (arg0, "%s \"$@\"", path);
+      narg[2] = arg0;
+
+      for (i = 0; i < argc; i++)
+        narg[i + 3] = arg[i];
+      narg[argc + 3] = NULL;
+    }
+  else
+    {
+      narg[2] = path;
+      narg[3] = NULL;
+    }
+
+  ret = fork_exec (pfdin, pfdout, *narg, narg);
+
+  if (arg0)
+    mem_free (arg0);
+  mem_free (narg);
+
+  return ret;
 }
 
 /* Wait for a child process to terminate. */
-- 
cgit v1.2.3-70-g09d2