From 96d175abd593e304e854eff915ad36ab84f07701 Mon Sep 17 00:00:00 2001
From: Piotr Grzybowski <merlin@narsil.org.pl>
Date: Sat, 16 Apr 2016 15:59:08 +0200
Subject: [PATCH] coproc name word expansion

---
 execute_cmd.c |   43 +++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 41 insertions(+), 2 deletions(-)

diff --git a/execute_cmd.c b/execute_cmd.c
index cc115a6..4e38836 100644
--- a/execute_cmd.c
+++ b/execute_cmd.c
@@ -2235,6 +2235,43 @@ execute_coproc (command, pipe_in, pipe_out, fds_to_close)
   char *tcmd;
   sigset_t set, oset;
 
+  WORD_LIST *list;
+  WORD_LIST *l;
+  WORD_DESC *w;
+
+  invert = (command->flags & CMD_INVERT_RETURN) != 0;
+
+  w = make_word(command->value.Coproc->name);
+  if (w == NULL)
+    {
+      internal_error("execute_coproc: error creating coproc.");
+      return (invert ? EXECUTION_SUCCESS: EXECUTION_FAILURE);
+    }
+
+  list = make_word_list (w, (WORD_LIST *)NULL);
+  if (list == NULL)
+    {
+      dispose_word(w);
+      internal_error("execute_coproc: error creating coproc.");
+      return (invert ? EXECUTION_SUCCESS: EXECUTION_FAILURE);
+    }
+
+  l = expand_words(list);
+  if (l == NULL || l->word == NULL) {
+      internal_error("execute_coproc: error creating coproc.");
+      dispose_words(list);
+      dispose_words(l);
+      return (invert ? EXECUTION_SUCCESS: EXECUTION_FAILURE);
+  }
+
+  if (!legal_identifier(l->word->word))
+    {
+      internal_error("execute_coproc: given name is not a valid indetifier.");
+      dispose_words(l);
+      dispose_words(list);
+      return (invert ? EXECUTION_SUCCESS: EXECUTION_FAILURE);
+    }
+
   /* XXX -- can be removed after changes to handle multiple coprocs */
 #if !MULTIPLE_COPROCS
   if (sh_coproc.c_pid != NO_PID)
@@ -2270,8 +2307,8 @@ execute_coproc (command, pipe_in, pipe_out, fds_to_close)
   close (rpipe[1]);
   close (wpipe[0]);
 
-  /* XXX - possibly run Coproc->name through word expansion? */
-  cp = coproc_alloc (command->value.Coproc->name, coproc_pid);
+  /* Coproc->name ran through word expansion at the begining. */
+  cp = coproc_alloc (l->word->word, coproc_pid);
   cp->c_rfd = rpipe[0];
   cp->c_wfd = wpipe[1];
 
@@ -2294,6 +2331,8 @@ execute_coproc (command, pipe_in, pipe_out, fds_to_close)
   DESCRIBE_PID (coproc_pid);
   run_pending_traps ();
 
+  dispose_words(l);
+  dispose_words(list);
   return (invert ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
 }
 #endif
-- 
1.7.8.3

