changeset 21927:6f62bd248919

use wrapper functions for mkoctfile and main * unistd-wrappers.c, unistd-wrappers.h (octave_ctermid_wrapper, octave_setsid_wrapper): New functions. * main.in.cc, mkoctfile.in.cc: Use unistd and wait wrappers. * src/module.mk (src_mkoctfile_LDADD): Also link with libwrappers.la.
author John W. Eaton <jwe@octave.org>
date Thu, 16 Jun 2016 10:43:27 -0400
parents 24215a16f3b2
children 315f4ba604c8
files liboctave/wrappers/unistd-wrappers.c liboctave/wrappers/unistd-wrappers.h src/main.in.cc src/mkoctfile.in.cc src/module.mk
diffstat 5 files changed, 43 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/wrappers/unistd-wrappers.c	Thu Jun 16 10:02:49 2016 -0400
+++ b/liboctave/wrappers/unistd-wrappers.c	Thu Jun 16 10:43:27 2016 -0400
@@ -76,6 +76,16 @@
   return close (fd);
 }
 
+const char *
+octave_ctermid_wrapper (void)
+{
+#if defined (HAVE_CTERMID)
+  return ctermid (0);
+#else
+  return "/dev/tty";
+#endif
+}
+
 int
 octave_dup2_wrapper (int fd1, int fd2)
 {
@@ -83,6 +93,12 @@
 }
 
 int
+octave_execv_wrapper (const char *file, char *const *argv)
+{
+  return execv (file, argv);
+}
+
+int
 octave_execvp_wrapper (const char *file, char *const *argv)
 {
   return execvp (file, argv);
@@ -178,6 +194,12 @@
   return rmdir (nm);
 }
 
+pid_t
+octave_setsid_wrapper (void)
+{
+  return setsid ();
+}
+
 int
 octave_stdin_fileno (void)
 {
--- a/liboctave/wrappers/unistd-wrappers.h	Thu Jun 16 10:02:49 2016 -0400
+++ b/liboctave/wrappers/unistd-wrappers.h	Thu Jun 16 10:43:27 2016 -0400
@@ -41,8 +41,12 @@
 
 extern int octave_close_wrapper (int fd);
 
+extern const char *octave_ctermid_wrapper (void);
+
 extern int octave_dup2_wrapper (int fd1, int fd2);
 
+extern int octave_execv_wrapper (const char *file, char *const *argv);
+
 extern int octave_execvp_wrapper (const char *file, char *const *argv);
 
 extern pid_t octave_fork_wrapper (void);
@@ -75,6 +79,8 @@
 
 extern int octave_rmdir_wrapper (const char *nm);
 
+extern pid_t octave_setsid_wrapper (void);
+
 extern int octave_stdin_fileno (void);
 
 extern int octave_stdout_fileno (void);
--- a/src/main.in.cc	Thu Jun 16 10:02:49 2016 -0400
+++ b/src/main.in.cc	Thu Jun 16 10:43:27 2016 -0400
@@ -39,9 +39,8 @@
 #include <iostream>
 #include <string>
 
-#include <sys/types.h>
-#include <unistd.h>
-
+#include "fcntl-wrappers.h"
+#include "unistd-wrappers.h"
 #include "wait-wrappers.h"
 
 #if ! defined (OCTAVE_VERSION)
@@ -63,13 +62,10 @@
 #include "display-available.h"
 #include "shared-fcns.h"
 
-#include <cstdlib>
-
 #if (defined (HAVE_OCTAVE_QT_GUI) \
      && ! defined (__WIN32__) || defined (__CYGWIN__))
 
 #include <signal.h>
-#include <fcntl.h>
 
 typedef void sig_handler (int);
 
@@ -228,17 +224,13 @@
 {
   int retval = false;
 
-#if defined (HAVE_CTERMID)
-  const char *ctty = ctermid (0);
-#else
-  const char *ctty = "/dev/tty";
-#endif
+  const char *ctty = octave_ctermid_wrapper ();
 
-  int fd = gnulib::open (ctty, O_RDWR, 0);
+  int fd = octave_open_wrapper (ctty, octave_o_rdwr_wrapper (), 0);
 
   if (fd >= 0)
     {
-      gnulib::close (fd);
+      octave_close_wrapper (fd);
 
       retval = true;
     }
@@ -400,7 +392,7 @@
   argv = prepare_spawn (argv);
   return _spawnv (_P_WAIT, file.c_str (), argv);
 #else
-  execv (file.c_str (), argv);
+  octave_execv_wrapper (file.c_str (), argv);
 
   std::cerr << "octave: failed to exec '" << file << "'" << std::endl;
 
@@ -540,7 +532,7 @@
     {
       install_signal_handlers ();
 
-      gui_pid = fork ();
+      gui_pid = octave_fork_wrapper ();
 
       if (gui_pid < 0)
         {
@@ -552,7 +544,7 @@
         {
           // Child.
 
-          if (setsid () < 0)
+          if (octave_setsid_wrapper () < 0)
             {
               std::cerr << "octave: error calling setsid!" << std::endl;
 
--- a/src/mkoctfile.in.cc	Thu Jun 16 10:02:49 2016 -0400
+++ b/src/mkoctfile.in.cc	Thu Jun 16 10:43:27 2016 -0400
@@ -35,24 +35,8 @@
 #include <vector>
 #include <cstdlib>
 
-#include <sys/types.h>
-#include <unistd.h>
-
-// This mess suggested by the autoconf manual.
-
-#include <sys/types.h>
-
-#if defined HAVE_SYS_WAIT_H
-#  include <sys/wait.h>
-#endif
-
-#if ! defined (WIFEXITED)
-#  define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
-#endif
-
-#if ! defined (WEXITSTATUS)
-#  define WEXITSTATUS(stat_val) (static_cast<unsigned> (stat_val) >> 8)
-#endif
+#include "unistd-wrappers.h"
+#include "wait-wrappers.h"
 
 static std::map<std::string, std::string> vars;
 
@@ -364,8 +348,8 @@
 
   int result = system (cmd.c_str ());
 
-  if (WIFEXITED (result))
-    result = WEXITSTATUS (result);
+  if (octave_wifexited_wrapper (result))
+    result = octave_wexitstatus_wrapper (result);
 
   return result;
 }
@@ -593,7 +577,7 @@
         {
           std::string f = *it, dfile = basename (f, true) + ".d", line;
 
-          gnulib::unlink (dfile.c_str ());
+          octave_unlink_wrapper (dfile.c_str ());
           std::string cmd = vars["CC"] + " "
                             + vars["DEPEND_FLAGS"] + " "
                             + vars["CPPFLAGS"] + " "
@@ -627,7 +611,7 @@
         {
           std::string f = *it, dfile = basename (f, true) + ".d", line;
 
-          gnulib::unlink (dfile.c_str ());
+          octave_unlink_wrapper (dfile.c_str ());
           std::string cmd = vars["CC"] + " "
                             + vars["DEPEND_FLAGS"] + " "
                             + vars["CPPFLAGS"] + " "
--- a/src/module.mk	Thu Jun 16 10:02:49 2016 -0400
+++ b/src/module.mk	Thu Jun 16 10:43:27 2016 -0400
@@ -139,6 +139,7 @@
 nodist_src_mkoctfile_SOURCES = src/mkoctfile.cc
 
 src_mkoctfile_LDADD = \
+  liboctave/wrappers/libwrappers.la \
   libgnu/libgnu.la $(LIBS)
 
 src_mkoctfile_CPPFLAGS = \