From c4f30d1e51ad98edefd737e67df25e115b5233ad Mon Sep 17 00:00:00 2001
From: Jaroslav Kysela <perex@perex.cz>
Date: Mon, 17 Nov 2014 14:36:40 +0100
Subject: [PATCH] iptv pipe: fix url / arg parsing bug, add ${service_name}
 substitution

---
 docs/html/config_muxes.html       |  4 +++-
 src/input/mpegts/iptv/iptv_pipe.c | 17 ++++++++++++++---
 src/spawn.c                       |  2 +-
 3 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/docs/html/config_muxes.html b/docs/html/config_muxes.html
index 3c616b23..fb23d2a1 100644
--- a/docs/html/config_muxes.html
+++ b/docs/html/config_muxes.html
@@ -94,7 +94,9 @@
 				    directories specified by PATH.
 				    Additional arguments may be separated
 				    using spaces. A raw MPEG-TS stream is
-				    expected.
+				    expected. The string ${service_name}
+				    is substituted with the service name
+				    field contents.
 
 			</dl>
                 
diff --git a/src/input/mpegts/iptv/iptv_pipe.c b/src/input/mpegts/iptv/iptv_pipe.c
index 2c676640..5c0484f8 100644
--- a/src/input/mpegts/iptv/iptv_pipe.c
+++ b/src/input/mpegts/iptv/iptv_pipe.c
@@ -33,7 +33,7 @@
 static int
 iptv_pipe_start ( iptv_mux_t *im, const char *_raw, const url_t *url )
 {
-  char *argv[32], *f, *raw, *s;
+  char *argv[32], *f, *raw, *s, *p, *a;
   int i = 1, rd;
 
   if (strncmp(_raw, "pipe://", 7))
@@ -55,9 +55,20 @@ iptv_pipe_start ( iptv_mux_t *im, const char *_raw, const url_t *url )
     while (*s && *s != ' ')
       s++;
     if (f != s) {
-      *(char *)s = '\0';
+      if (*s) {
+        *(char *)s = '\0';
+        s++;
+      }
+      p = strstr(f, "${service_name}");
+      if (p) {
+        a = alloca(strlen(f) + strlen(im->mm_iptv_svcname ?: ""));
+        *p = '\0';
+        strcpy(a, f);
+        strcat(a, im->mm_iptv_svcname ?: "");
+        strcat(a, p + 15);
+        f = a;
+      }
       argv[i++] = f;
-      s++;
     }
   }
   argv[i] = NULL;
diff --git a/src/spawn.c b/src/spawn.c
index 419fa459..a770351f 100644
--- a/src/spawn.c
+++ b/src/spawn.c
@@ -381,7 +381,7 @@ spawn_and_give_stdout(const char *prog, char *argv[], int *rd, int redir_stderr)
     prog = bin;
   }
 
-  if(!argv) argv = (void *)local_argv;
+  if (!argv) argv = (void *)local_argv;
   if (!argv[0]) argv[0] = (char*)prog;
 
   pthread_mutex_lock(&fork_lock);