42 #include <sys/types.h>
45 #ifndef OCTAVE_VERSION
46 #define OCTAVE_VERSION "4.0.0"
49 #ifndef OCTAVE_ARCHLIBDIR
50 #define OCTAVE_ARCHLIBDIR "/usr/local/libexec/octave/4.0.0/exec/x86_64-unknown-linux-gnu"
54 #define OCTAVE_BINDIR "/usr/local/bin"
58 #define OCTAVE_PREFIX "/usr/local"
66 #if (defined (HAVE_OCTAVE_GUI) \
67 && ! defined (__WIN32__) || defined (__CYGWIN__))
80 static pid_t gui_pid = 0;
82 static int caught_signal = -1;
85 gui_driver_sig_handler (
int sig)
94 struct sigaction act, oact;
96 act.sa_handler = handler;
99 gnulib::sigemptyset (&act.sa_mask);
100 gnulib::sigemptyset (&oact.sa_mask);
102 gnulib::sigaction (sig, &act, &oact);
104 return oact.sa_handler;
229 have_controlling_terminal (
void)
233 #if defined (HAVE_CTERMID)
234 const char *ctty = ctermid (0);
236 const char *ctty =
"/dev/tty";
239 int fd = gnulib::open (ctty, O_RDWR, 0);
281 #if defined (__WIN32__) && ! defined (__CYGWIN__)
306 #define SHELL_SPECIAL_CHARS "\"\\ \001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037"
307 #define SHELL_SPACE_CHARS " \001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037"
309 prepare_spawn (
char **argv)
316 for (argc = 0; argv[argc] != NULL; argc++)
320 new_argv =
new char* [argc + 1];
323 for (i = 0; i < argc; i++)
325 const char *
string = argv[i];
327 if (
string[0] ==
'\0')
328 new_argv[i] = strdup (
"\"\"");
329 else if (strpbrk (
string, SHELL_SPECIAL_CHARS) != NULL)
331 int quote_around = (strpbrk (
string, SHELL_SPACE_CHARS) != NULL);
333 unsigned int backslashes;
342 for (s =
string; *s !=
'\0'; s++)
346 length += backslashes + 1;
354 length += backslashes + 1;
356 quoted_string =
new char [length + 1];
362 for (s =
string; *s !=
'\0'; s++)
368 for (j = backslashes + 1; j > 0; j--)
380 for (j = backslashes; j > 0; j--)
386 new_argv[i] = quoted_string;
389 new_argv[i] = (
char *)
string;
391 new_argv[argc] = NULL;
396 #endif // __WIN32__ && ! __CYGWIN__
401 #if defined (__WIN32__) && ! defined (__CYGWIN__)
402 argv = prepare_spawn (argv);
403 return _spawnv (_P_WAIT, file.c_str (), argv);
405 execv (file.c_str (), argv);
407 std::cerr <<
"octave: failed to exec '" << file <<
"'" << std::endl;
419 int len = strlen (s);
420 char *tmp =
new char [len+1];
421 tmp = strcpy (tmp, s);
431 bool gui_libs =
true;
436 #if defined (HAVE_OCTAVE_GUI)
441 std::string file = octave_archlibdir +
dir_sep_char +
"octave-gui";
447 char **new_argv =
new char * [argc + 1];
451 bool warn_display =
true;
453 for (
int i = 1; i < argc; i++)
455 if (! strcmp (argv[i],
"--no-gui-libs"))
465 file = octave_bindir + dir_sep_char +
"octave-cli";
467 else if (! strcmp (argv[i],
"--no-gui"))
476 new_argv[k++] = argv[i];
478 else if (! strcmp (argv[i],
"--silent") || ! strcmp (argv[i],
"--quiet")
479 || ! strcmp (argv[i],
"-q"))
481 warn_display =
false;
482 new_argv[k++] = argv[i];
485 new_argv[k++] = argv[i];
490 if (gui_libs || start_gui)
501 file = octave_bindir + dir_sep_char +
"octave-cli-" OCTAVE_VERSION;
505 if (! display_check_err_msg)
506 display_check_err_msg =
"graphical display unavailable";
508 std::cerr <<
"octave: " << display_check_err_msg << std::endl;
509 std::cerr <<
"octave: disabling GUI features" << std::endl;
514 #if defined (__WIN32__) && ! defined (__CYGWIN__)
518 new_argv[0] =
strsave (file.c_str ());
520 #if (defined (HAVE_OCTAVE_GUI) \
521 && ! defined (__WIN32__) || defined (__CYGWIN__))
523 if (gui_libs && start_gui && have_controlling_terminal ())
531 std::cerr <<
"octave: fork failed!" << std::endl;
535 else if (gui_pid == 0)
541 std::cerr <<
"octave: error calling setsid!" << std::endl;
558 if (caught_signal > 0)
560 int sig = caught_signal;
573 std::cerr <<
"octave exited with signal "
574 << WTERMSIG (status) << std::endl;
static std::string subst_octave_home(const std::string &s)
static char * strsave(const char *s)
const char * display_available(int *dpy_avail)
#define WIFSIGNALLED(stat_val)
#define OCTAVE_ARCHLIBDIR
static std::string get_octave_bindir(void)
void install_signal_handlers(void)
#define WIFEXITED(stat_val)
static const char dir_sep_char
int main(int argc, char **argv)
static std::string get_octave_archlibdir(void)
static std::string octave_getenv(const std::string &name)
static int octave_exec(const std::string &file, char **argv)
#define WEXITSTATUS(stat_val)
sig_handler * octave_set_signal_handler(int sig, sig_handler *handler, bool restart_syscalls)