changeset 29189:0fc400f15e35 stable

avoid gui when octave is launched in non-interactive mode (bug #59628) * main.in.cc (main): ignore gui option if eval-option or a script file is given unless the persist-option is given at the same time; for this, add the gui option to the final call at the end when all other options are examined
author Torsten Lilge <ttl-octave@mailbox.org>
date Thu, 17 Dec 2020 07:23:49 +0100
parents 3a15c18c705f
children 7f11d59e3af8
files src/main.in.cc
diffstat 1 files changed, 37 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/main.in.cc	Tue Dec 15 17:08:13 2020 +0100
+++ b/src/main.in.cc	Thu Dec 17 07:23:49 2020 +0100
@@ -208,9 +208,13 @@
 {
   int retval = 0;
 
+  int idx_gui = -1;
   bool start_gui = false;
   bool gui_libs = true;
 
+  bool eval_code = false;
+  bool persist_octave = false;
+
   set_octave_home ();
 
   std::string octave_bindir = get_octave_bindir ();
@@ -267,8 +271,26 @@
           // If we see this option, then we fork and exec octave with
           // the --gui option, while continuing to handle signals in the
           // terminal.
+          // Do not copy the arg now, since we still not know if the
+          // gui should really be launched. Just store the index
 
+          idx_gui = i;
           start_gui = true;
+        }
+      else if (! strcmp (argv[i], "--persist"))
+        {
+          // FIXME: How can we reliably detect if this option appears
+          //        after a FILE argument. In this case octave ignores
+          //        the option but the GUI might still be launched if
+          //        --gui is also given.
+          persist_octave = true;
+
+          new_argv[k++] = argv[i];
+        }
+      else if (! strcmp (argv[i], "--eval") ||
+               (strlen (argv[i]) > 0 && argv[i][0] != '-'))
+        {
+          eval_code = true;
           new_argv[k++] = argv[i];
         }
       else if (! strcmp (argv[i], "--silent") || ! strcmp (argv[i], "--quiet"))
@@ -307,11 +329,16 @@
         new_argv[k++] = argv[i];
     }
 
-  // At this point, gui_libs and start_gui are just about options, not
-  // the environment.  Exit if they don't make sense.
+  if (start_gui && eval_code && ! persist_octave)
+    start_gui = false;
 
+  // At this point, we definitely know whether the gui has to
+  // be launched or not.
+  // gui_libs and start_gui are just about options, not
+  // the environment. Exit if they don't make sense.
   if (start_gui)
     {
+      // GUI should be started
       if (! gui_libs)
         {
           std::cerr << "octave: conflicting options: --no-gui-libs and --gui"
@@ -324,6 +351,14 @@
                 << std::endl;
       return 1;
 #endif
+
+      // Finally, add --gui to the command line options. We can not
+      // just append it since options after a given file are ignored.
+      for (int j = k; j > 1; j--)
+        new_argv[j] = new_argv[j-1];
+
+      new_argv[1] = argv[idx_gui];
+      k++;
     }
 
   new_argv[k] = nullptr;