# HG changeset patch
# User Pantxo Diribarne <pantxo.diribarne@gmail.com>
# Date 1566939063 -7200
#      Tue Aug 27 22:51:03 2019 +0200
# Node ID 996229be0045fff0dfbebc8dc0c54055083de518
# 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, epstoo): Quote binary and file path
* __ghostscript__.m: Ditto.
* __opengl_print__.m: Ditto.

diff -r 8aa544cc2e17 -r 996229be0045 libinterp/corefcn/sysdep.cc
--- a/libinterp/corefcn/sysdep.cc	Tue Aug 20 19:12:07 2019 -0400
+++ b/libinterp/corefcn/sysdep.cc	Tue Aug 27 22:51:03 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 996229be0045 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	Tue Aug 27 22:51:03 2019 +0200
@@ -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 996229be0045 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	Tue Aug 27 22:51:03 2019 +0200
@@ -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 996229be0045 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	Tue Aug 27 22:51:03 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"};
@@ -85,7 +85,7 @@
       if (! isempty (svgcmd))
         pipeline = {sprintf(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
@@ -124,7 +124,7 @@
       opts.unlink = [opts.unlink tmp];
       cmd = sprintf (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)
