# HG changeset patch
# User Pantxo Diribarne <pantxo.diribarne@gmail.com>
# Date 1567452118 -7200
#      Mon Sep 02 21:21:58 2019 +0200
# Node ID 33550354d0f957bbfe3c0409cc6026d46255bd84
# Parent  8aa544cc2e170b391523b5d111b385b9d3cb9571
Allow printing with Octave installed in path with parenthesis (bug #49718)

* sysdep.cc (popen): Quote command before passing it to _wpopen.

* print.m (pstoedit, svgconvert, fig2dev, epstool): Quote binary and file path.
* __ghostscript__.m: Ditto.
* __opengl_print__.m: Ditto.

diff -r 8aa544cc2e17 -r 33550354d0f9 libinterp/corefcn/sysdep.cc
--- a/libinterp/corefcn/sysdep.cc	Tue Aug 20 19:12:07 2019 -0400
+++ b/libinterp/corefcn/sysdep.cc	Mon Sep 02 21:21:58 2019 +0200
@@ -569,7 +569,16 @@
   FILE * popen (const char *command, const char *mode)
   {
 #if defined (__MINGW32__) || defined (_MSC_VER)
-    wchar_t *wcommand = u8_to_wchar (command);
+    // On windows, if popen finds a quote at the begining and at the end of the
+    // command, it will remove both. Quote the whole command to avoid ending up
+    // with missing quotes.
+    const char *quote = "\"";
+    char qcommand[std::strlen (command) + 3];
+    strcpy (qcommand, quote);
+    strcat (qcommand, command);
+    strcat (qcommand, quote);
+
+    wchar_t *wcommand = u8_to_wchar (qcommand);
     wchar_t *wmode = u8_to_wchar (mode);
 
     unwind_protect frame;
diff -r 8aa544cc2e17 -r 33550354d0f9 scripts/plot/util/print.m
--- a/scripts/plot/util/print.m	Tue Aug 20 19:12:07 2019 -0400
+++ b/scripts/plot/util/print.m	Mon Sep 02 21:21:58 2019 +0200
@@ -805,7 +805,7 @@
              "print: 'gs' (Ghostscript) is required for specified output format, but binary is not available in PATH");
     endif
 
-    [status, devlist] = system (sprintf ("%s -h", opts.ghostscript.binary));
+    [status, devlist] = system (sprintf ('"%s" -h', opts.ghostscript.binary));
     if (isempty (strfind (devlist, "eps2write")))
       epsdevice = "epswrite";
     else
@@ -824,9 +824,9 @@
     pipein = true;
     filein = [tempname() ".eps"];
     if (dos_shell)
-      cleanup = sprintf ("& del %s ", strrep (filein, '/', '\'));
+      cleanup = sprintf ('& del "%s" ', strrep (filein, '/', '\'));
     else
-      cleanup = sprintf ("; rm %s ", filein);
+      cleanup = sprintf ('; rm "%s" ', filein);
     endif
   else
     pipein = false;
@@ -836,9 +836,9 @@
     pipeout = true;
     fileout = [tempname() ".eps"];
     if (dos_shell)
-      cleanup = [cleanup, sprintf("& del %s ", strrep (fileout, '/', '\'))];
+      cleanup = [cleanup, sprintf('& del "%s" ', strrep (fileout, '/', '\'))];
     else
-      cleanup = [cleanup, sprintf("; rm %s ", fileout)];
+      cleanup = [cleanup, sprintf('; rm "%s" ', fileout)];
     endif
   else
     pipeout = false;
@@ -965,9 +965,9 @@
   dos_shell = (ispc () && ! isunix ());
   if (dos_shell)
     ## FIXME: Is this the right thing to do for DOS?
-    cmd = sprintf ("%s -L %s 2> NUL", opts.fig2dev_binary, devopt);
+    cmd = sprintf ('"%s" -L %s 2> NUL', opts.fig2dev_binary, devopt);
   else
-    cmd = sprintf ("%s -L %s 2> /dev/null", opts.fig2dev_binary, devopt);
+    cmd = sprintf ('"%s" -L %s 2> /dev/null', opts.fig2dev_binary, devopt);
   endif
 
   if (opts.debug)
@@ -1075,18 +1075,18 @@
 
   if (! do_svg)
     if (dos_shell)
-      cmd = sprintf ("%s -f %s 2> NUL", opts.pstoedit_binary, devopt);
+      cmd = sprintf ('"%s" -f %s 2> NUL', opts.pstoedit_binary, devopt);
     else
-      cmd = sprintf ("%s -f %s 2> /dev/null", opts.pstoedit_binary, devopt);
+      cmd = sprintf ('"%s" -f %s 2> /dev/null', opts.pstoedit_binary, devopt);
       endi;f
     endif
   else
     cmd = svgconvert (opts, devopt);
     if (dos_shell)
-      cmd = sprintf ("%s & %s -ssp -f %s %%s 2> NUL", cmd, ...
+      cmd = sprintf ('%s & "%s" -ssp -f %s "%%s" 2> NUL', cmd, ...
                      opts.pstoedit_binary, devopt);
     else
-      cmd = sprintf ("%s ; %s -ssp -f %s %%s 2> /dev/null", cmd,  ...
+      cmd = sprintf ('%s ; "%s" -ssp -f %s "%%s" 2> /dev/null', cmd,  ...
                      opts.pstoedit_binary, devopt);
     endif
   endif
@@ -1116,7 +1116,7 @@
       fontdir = __octave_config_info__ ("octfontsdir");
     endif
 
-    cmd = sprintf ("%s - %%s %3.2f %s %d \"%%s\"", opts.svgconvert_binary, ...
+    cmd = sprintf ('"%s" - %%s %3.2f "%s" %d "%%s"', opts.svgconvert_binary, ...
                    get (0, "screenpixelsperinch"), ...
                    fullfile (fontdir, "FreeSans.otf"), 1);
 
diff -r 8aa544cc2e17 -r 33550354d0f9 scripts/plot/util/private/__ghostscript__.m
--- a/scripts/plot/util/private/__ghostscript__.m	Tue Aug 20 19:12:07 2019 -0400
+++ b/scripts/plot/util/private/__ghostscript__.m	Mon Sep 02 21:21:58 2019 +0200
@@ -124,9 +124,9 @@
     else
       offsetfile = [tempname() ".ps"];
       if (dos_shell)
-        cleanup_cmd = ["del " strrep(offsetfile, '/', '\')];
+        cleanup_cmd = sprintf ('del "%s"', strrep (offsetfile, '/', '\'));
       else
-        cleanup_cmd = ["rm " offsetfile];
+        cleanup_cmd = sprintf ('rm "%s"', strrep (offsetfile, '/', '\'));
       endif
     endif
     unwind_protect
@@ -149,9 +149,9 @@
   endif
 
   if (isempty (opts.output))
-    cmd = sprintf ("%s %s", opts.binary, gs_opts);
+    cmd = sprintf ('"%s" %s', opts.binary, gs_opts);
   else
-    cmd = sprintf ('%s %s -sOutputFile="%s"',
+    cmd = sprintf ('"%s" %s -sOutputFile="%s"',
                    opts.binary, gs_opts, opts.output);
   endif
   if (! isempty (opts.prepend)
@@ -176,7 +176,7 @@
     endif
   endif
   if (! isempty (offsetfile) && format_for_printer)
-    cmd = sprintf ("%s %s", cmd, offsetfile);
+    cmd = sprintf ("%s \"%s\"", cmd, offsetfile);
   endif
   if (! isempty (opts.source))
     cmd = sprintf ("%s %s", cmd, opts.source);
diff -r 8aa544cc2e17 -r 33550354d0f9 scripts/plot/util/private/__opengl_print__.m
--- a/scripts/plot/util/private/__opengl_print__.m	Tue Aug 20 19:12:07 2019 -0400
+++ b/scripts/plot/util/private/__opengl_print__.m	Mon Sep 02 21:21:58 2019 +0200
@@ -65,16 +65,16 @@
       gl2ps_device{2} = "tex";
       if (dos_shell)
         ## FIXME: this will only work on MinGW with the MSYS shell
-        pipeline = {sprintf("cat > %s-inc.%s", name, suffix)};
-        pipeline{2} = sprintf ("cat > %s.tex", name);
+        pipeline = {sprintf('cat > "%s-inc.%s"', name, suffix)};
+        pipeline{2} = sprintf ('cat > "%s.tex"', name);
       else
-        pipeline = {sprintf("cat > %s-inc.%s", name, suffix)};
-        pipeline{2} = sprintf ("cat > %s.tex", name);
+        pipeline = {sprintf('cat > "%s-inc.%s"', name, suffix)};
+        pipeline{2} = sprintf ('cat > "%s.tex"', name);
       endif
     case "tikz"
       ## format GL2PS_PGF
       gl2ps_device = {"pgf"};
-      pipeline = {sprintf("cat > %s", opts.name)};
+      pipeline = {sprintf('cat > "%s"', opts.name)};
     case "svg"
       ## format GL2PS_SVG
       gl2ps_device = {"svg"};
@@ -83,9 +83,9 @@
         svgcmd = opts.svgconvert_cmd (opts, opts.ghostscript.device);
       endif
       if (! isempty (svgcmd))
-        pipeline = {sprintf(svgcmd, "svg", opts.name)};
+        pipeline = {sprintf(undo_string_escapes  (svgcmd), "svg", opts.name)};
       else
-        pipeline = {sprintf("cat > %s", opts.name)};
+        pipeline = {sprintf('cat > "%s"', opts.name)};
       endif
     case fig2dev_devices
       cmd_fig2dev = opts.fig2dev_cmd (opts, opts.devopt);
@@ -96,12 +96,12 @@
           opts.name = opts.name(1:end-numel(ext));
         endif
         opts.name = [opts.name ".ps"];
-        cmd = sprintf ("%s | %s > %s", cmd_pstoedit, cmd_fig2dev, opts.name);
+        cmd = sprintf ('%s | %s > "%s"', cmd_pstoedit, cmd_fig2dev, opts.name);
         gl2ps_device = {"eps"};
         pipeline = {cmd};
         cmd_fig2dev = opts.fig2dev_cmd (opts, "pstex_t");
         gl2ps_device{2} = "eps";
-        pipeline{2} = sprintf ("%s | %s > %s", cmd_pstoedit,
+        pipeline{2} = sprintf ('%s | %s > "%s"', cmd_pstoedit,
                                cmd_fig2dev, strrep(opts.name, ".ps", ".tex"));
       else
         ## Using svgconvert
@@ -109,7 +109,7 @@
         opts.unlink = [opts.unlink tmp];
         cmd_pstoedit = sprintf (opts.pstoedit_cmd (opts, "fig"), ...
                                 "pdf", tmp, tmp);
-        cmd = sprintf ("%s | %s > %s", cmd_pstoedit, cmd_fig2dev, opts.name);
+        cmd = sprintf ('%s | %s > "%s"', cmd_pstoedit, cmd_fig2dev, opts.name);
         gl2ps_device = {"svg"};
         pipeline = {cmd};
       endif
@@ -122,9 +122,10 @@
     case {"dxf", "emf", "fig", "hpgl"}
       tmp = tempname ();
       opts.unlink = [opts.unlink tmp];
-      cmd = sprintf (opts.pstoedit_cmd (opts), "pdf", tmp, tmp);
+      cmd = sprintf (undo_string_escapes (opts.pstoedit_cmd (opts)), ...
+                     "pdf", tmp, tmp);
       gl2ps_device = {"svg"};
-      pipeline = {sprintf("%s > %s", cmd, opts.name)};
+      pipeline = {sprintf('%s > "%s"', cmd, opts.name)};
     case opts.ghostscript.device
       svgcmd = "";
       if (opts.svgconvert)
@@ -137,7 +138,7 @@
         tmp = tempname ();
         opts.ghostscript.source = tmp;
         opts.unlink = [opts.unlink tmp];
-        svgcmd = sprintf (svgcmd, "pdf", tmp);
+        svgcmd = sprintf (undo_string_escapes  (svgcmd), "pdf", tmp);
       endif
 
       opts.ghostscript.output = opts.name;
