3Copyright (C) 1993, 1994, 1995, 1996, 1997, 1999, 2000, 2002, 2003,
4 2005, 2006, 2007, 2008, 2009 John W. Eaton
6This file is part of Octave.
8Octave is free software; you can redistribute it and/or modify it
9under the terms of the GNU General Public License as published by the
10Free Software Foundation; either version 3 of the License, or (at your
11option) any later version.
13Octave is distributed in the hope that it will be useful, but WITHOUT
14ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18You should have received a copy of the GNU General Public License
19along with Octave; see the file COPYING. If not, see
20<http://www.gnu.org/licenses/>.
40#if defined (HAVE_TERMIOS_H)
42#elif defined (HAVE_TERMIO_H)
44#elif defined (HAVE_SGTTY_H)
48#if defined (HAVE_CONIO_H)
52#if defined (HAVE_SYS_IOCTL_H)
56#if defined (HAVE_FLOATINGPOINT_H)
57#include <floatingpoint.h>
60#if defined (HAVE_IEEEFP_H)
66#include "lo-mappers.h"
80#include "sighandlers.h"
90#if defined (__386BSD__) || defined (__FreeBSD__) || defined (__NetBSD__)
94#if defined (HAVE_FLOATINGPOINT_H)
95 // Disable trapping on common exceptions.
99 fpsetmask (~(FP_X_OFL|FP_X_INV|FP_X_DZ|FP_X_DNML|FP_X_UFL|FP_X_IMP));
104#if defined (__WIN32__) && ! defined (_POSIX_VERSION)
106w32_set_octave_home (void)
110 std::string bin_dir (n, '\0');
114 HMODULE hMod = GetModuleHandle ("octinterp");
115 int status = GetModuleFileName (hMod, &bin_dir[0], n);
119 bin_dir.resize (status);
129 if (! bin_dir.empty ())
131 size_t pos = bin_dir.rfind ("\\bin\\");
133 if (pos != std::string::npos)
134 octave_env::putenv ("OCTAVE_HOME", bin_dir.substr (0, pos));
139w32_set_quiet_shutdown (void)
141 // Let the user close the console window or shutdown without the
144 // FIXME -- should this be user configurable?
145 SetProcessShutdownParameters (0x280, SHUTDOWN_NORETRY);
149MINGW_signal_cleanup (void)
151 w32_set_quiet_shutdown ();
157#if defined (__MINGW32__)
161 w32_set_octave_home ();
163 // Init mutex to protect setjmp/longjmp and get main thread context
166 w32_set_quiet_shutdown ();
170#if defined (_MSC_VER)
174 w32_set_octave_home ();
176 // Init mutex to protect setjmp/longjmp and get main thread context
179 w32_set_quiet_shutdown ();
184// Return TRUE if FILE1 and FILE2 refer to the same (physical) file.
187same_file_internal (const std::string& file1, const std::string& file2)
189#ifdef OCTAVE_USE_WINDOWS_API
193 // Windows native code
194 // Reference: http://msdn2.microsoft.com/en-us/library/aa363788.aspx
196 HANDLE hfile1 = CreateFile (file1.c_str (), 0, FILE_SHARE_READ, 0,
197 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
199 if (hfile1 != INVALID_HANDLE_VALUE)
201 HANDLE hfile2 = CreateFile (file2.c_str (), 0, FILE_SHARE_READ, 0,
202 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
204 if (hfile2 != INVALID_HANDLE_VALUE)
206 BY_HANDLE_FILE_INFORMATION hfi1;
207 BY_HANDLE_FILE_INFORMATION hfi2;
209 if (GetFileInformationByHandle (hfile1, &hfi1)
210 && GetFileInformationByHandle (hfile2, &hfi2))
212 retval = (hfi1.dwVolumeSerialNumber == hfi2.dwVolumeSerialNumber
213 && hfi1.nFileIndexHigh == hfi2.nFileIndexHigh
214 && hfi1.nFileIndexLow == hfi2.nFileIndexLow);
216 CloseHandle (hfile2);
219 CloseHandle (hfile1);
228 file_stat fs_file1 (file1);
229 file_stat fs_file2 (file2);
231 return (fs_file1 && fs_file2
232 && fs_file1.ino () == fs_file2.ino ()
233 && fs_file1.dev () == fs_file2.dev ());
238#if defined (__DECCXX)
240// These don't seem to be instantiated automatically...
242template std::istream&
243std::operator >> (std::istream&, std::complex<double>&);
246std::string::append (const std::string&, size_t, size_t);
253 typedef void (*_cplus_fcn_int) (int);
254 extern void (*malloc_error (_cplus_fcn_int)) (int);
258malloc_handler (int code)
261 warning ("hopefully recoverable malloc error: freeing wild pointer");
263 panic ("probably irrecoverable malloc error: code %d", code);
269 malloc_error (malloc_handler);
276 _control87 ((EM_INVALID | EM_DENORMAL | EM_ZERODIVIDE | EM_OVERFLOW
277 | EM_UNDERFLOW | EM_INEXACT), MCW_EM);
285#if defined (HAVE_IEEEFP_H)
286 // Disable trapping on common exceptions.
287 fpsetmask (~(FP_X_OFL|FP_X_INV|FP_X_DZ|FP_X_DNML|FP_X_UFL|FP_X_IMP));
295#if defined (__386BSD__) || defined (__FreeBSD__) || defined(__NetBSD__)
297#elif defined (__MINGW32__)
299#elif defined (_MSC_VER)
303#elif defined (__EMX__)
315 MINGW_SIGNAL_CLEANUP ();
318// Set terminal in raw mode. From less-177.
320// Change terminal to "raw mode", or restore to "normal" mode.
322// 1. An outstanding read will complete on receipt of a single keystroke.
323// 2. Input is not echoed.
324// 3. On output, \n is mapped to \r\n.
325// 4. \t is NOT expanded into spaces.
326// 5. Signal-causing characters such as ctrl-C (interrupt),
327// etc. are NOT disabled.
328// It doesn't matter whether an input \n is mapped to \r, or vice versa.
331raw_mode (bool on, bool wait)
333 static bool curr_on = false;
335 int tty_fd = STDIN_FILENO;
336 if (! isatty (tty_fd))
339 error ("stdin is not a tty!");
346#if defined (HAVE_TERMIOS_H)
349 static struct termios save_term;
353 // Get terminal modes.
355 tcgetattr (tty_fd, &s);
357 // Save modes and set certain variables dependent on modes.
360// ospeed = s.c_cflag & CBAUD;
361// erase_char = s.c_cc[VERASE];
362// kill_char = s.c_cc[VKILL];
364 // Set the modes to the way we want them.
366 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL);
367 s.c_oflag |= (OPOST|ONLCR);
369 s.c_oflag &= ~(OCRNL);
372 s.c_oflag &= ~(ONOCR);
375 s.c_oflag &= ~(ONLRET);
377 s.c_cc[VMIN] = wait ? 1 : 0;
382 // Restore saved modes.
387 tcsetattr (tty_fd, wait ? TCSAFLUSH : TCSADRAIN, &s);
389#elif defined (HAVE_TERMIO_H)
392 static struct termio save_term;
396 // Get terminal modes.
398 ioctl (tty_fd, TCGETA, &s);
400 // Save modes and set certain variables dependent on modes.
403// ospeed = s.c_cflag & CBAUD;
404// erase_char = s.c_cc[VERASE];
405// kill_char = s.c_cc[VKILL];
407 // Set the modes to the way we want them.
409 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL);
410 s.c_oflag |= (OPOST|ONLCR);
412 s.c_oflag &= ~(OCRNL);
415 s.c_oflag &= ~(ONOCR);
418 s.c_oflag &= ~(ONLRET);
420 s.c_cc[VMIN] = wait ? 1 : 0;
424 // Restore saved modes.
429 ioctl (tty_fd, TCSETAW, &s);
431#elif defined (HAVE_SGTTY_H)
434 static struct sgttyb save_term;
438 // Get terminal modes.
440 ioctl (tty_fd, TIOCGETP, &s);
442 // Save modes and set certain variables dependent on modes.
445// ospeed = s.sg_ospeed;
446// erase_char = s.sg_erase;
447// kill_char = s.sg_kill;
449 // Set the modes to the way we want them.
451 s.sg_flags |= CBREAK;
452 s.sg_flags &= ~(ECHO);
456 // Restore saved modes.
461 ioctl (tty_fd, TIOCSETN, &s);
464 warning ("no support for raw mode console I/O on this system");
466 // Make sure the current mode doesn't toggle.
474octave_popen (const char *command, const char *mode)
476#if defined (__MINGW32__) || defined (_MSC_VER)
477 if (mode && mode[0] && ! mode[1])
484 return _popen (command, tmode);
487 return _popen (command, mode);
489 return popen (command, mode);
494octave_pclose (FILE *f)
496#if defined (__MINGW32__) || defined (_MSC_VER)
503// Read one character from the terminal.
506octave_kbhit (bool wait)
509 int c = (! wait && ! _kbhit ()) ? 0 : std::cin.get ();
511 raw_mode (true, wait);
513 // Get current handler.
514 octave_interrupt_handler saved_interrupt_handler
515 = octave_ignore_interrupts ();
517 // Restore it, disabling system call restarts (if possible) so the
518 // read can be interrupted.
520 octave_set_interrupt_handler (saved_interrupt_handler, false);
522 int c = std::cin.get ();
524 if (std::cin.fail () || std::cin.eof ())
527 // Restore it, enabling system call restarts (if possible).
528 octave_set_interrupt_handler (saved_interrupt_handler, true);
530 raw_mode (false, true);
538@deftypefn {Built-in Function} {} clc ()\n\
539@deftypefnx {Built-in Function} {} home ()\n\
540Clear the terminal screen and move the cursor to the upper left corner.\n\
543 command_editor::clear_screen ();
545 return octave_value_list ();
550DEFUN (getenv, args, ,
552@deftypefn {Built-in Function} {} getenv (@var{var})\n\
553Return the value of the environment variable @var{var}. For example,\n\
560returns a string containing the value of your path.\n\
565 int nargin = args.length ();
569 std::string name = args(0).string_value ();
572 retval = octave_env::getenv (name);
580DEFUN (putenv, args, ,
582@deftypefn {Built-in Function} {} putenv (@var{var}, @var{value})\n\
583@deftypefnx {Built-in Function} {} setenv (@var{var}, @var{value})\n\
584Set the value of the environment variable @var{var} to @var{value}.\n\
587 octave_value_list retval;
589 int nargin = args.length ();
591 if (nargin == 2 || nargin == 1)
593 std::string var = args(0).string_value ();
597 std::string val = (nargin == 2
598 ? args(1).string_value () : std::string ());
601 octave_env::putenv (var, val);
603 error ("putenv: second argument should be a string");
606 error ("putenv: first argument should be a string");
613DEFALIAS (setenv, putenv);
615// FIXME -- perhaps kbhit should also be able to print a prompt?
619@deftypefn {Built-in Function} {} kbhit ()\n\
620Read a single keystroke from the keyboard. If called with one\n\
621argument, don't wait for a keypress. For example,\n\
628will set @var{x} to the next character typed at the keyboard as soon as\n\
636identical to the above example, but don't wait for a keypress,\n\
637returning the empty string if no key is available.\n\
642 // FIXME -- add timeout and default value args?
644 if (interactive || forced_interactive)
648 int c = octave_kbhit (args.length () == 0);
653 char *s = new char [2];
664@deftypefn {Built-in Function} {} pause (@var{seconds})\n\
665Suspend the execution of the program. If invoked without any arguments,\n\
666Octave waits until you type a character. With a numeric argument, it\n\
667pauses for the given number of seconds. For example, the following\n\
668statement prints a message and then waits 5 seconds before clearing the\n\
673fprintf (stderr, \"wait please...\\n\");\n\
680 octave_value_list retval;
682 int nargin = args.length ();
684 if (! (nargin == 0 || nargin == 1))
692 double dval = args(0).double_value ();
702 flush_octave_stdout ();
709 warning ("pause: NaN is an invalid delay");
715 flush_octave_stdout ();
724@deftypefn {Built-in Function} {} sleep (@var{seconds})\n\
725Suspend the execution of the program for the given number of seconds.\n\
728 octave_value_list retval;
730 if (args.length () == 1)
732 double dval = args(0).double_value ();
737 warning ("sleep: NaN is an invalid delay");
751DEFUN (usleep, args, ,
753@deftypefn {Built-in Function} {} usleep (@var{microseconds})\n\
754Suspend the execution of the program for the given number of\n\
755microseconds. On systems where it is not possible to sleep for periods\n\
756of time less than one second, @code{usleep} will pause the execution for\n\
757@code{round (@var{microseconds} / 1e6)} seconds.\n\
760 octave_value_list retval;
762 if (args.length () == 1)
764 double dval = args(0).double_value ();
769 warning ("usleep: NaN is an invalid delay");
774 int delay = NINT (dval);
777 octave_usleep (delay);
787// FIXME -- maybe this should only return 1 if IEEE floating
788// point functions really work.
792@deftypefn {Built-in Function} {} isieee ()\n\
793Return 1 if your computer claims to conform to the IEEE standard for\n\
794floating point calculations.\n\
797 oct_mach_info::float_format flt_fmt = oct_mach_info::native_float_format ();
799 return octave_value (flt_fmt == oct_mach_info::flt_fmt_ieee_little_endian
800 || flt_fmt == oct_mach_info::flt_fmt_ieee_big_endian);
803DEFUN (native_float_format, , ,
805@deftypefn {Built-in Function} {} native_float_format ()\n\
806Return the native floating point format as a string\n\
809 oct_mach_info::float_format flt_fmt = oct_mach_info::native_float_format ();
811 return octave_value (oct_mach_info::float_format_as_string (flt_fmt));
814DEFUN (tilde_expand, args, ,
816@deftypefn {Built-in Function} {} tilde_expand (@var{string})\n\
817Performs tilde expansion on @var{string}. If @var{string} begins with a\n\
818tilde character, (@samp{~}), all of the characters preceding the first\n\
819slash (or all characters, if there is no slash) are treated as a\n\
820possible user name, and the tilde and the following characters up to the\n\
821slash are replaced by the home directory of the named user. If the\n\
822tilde is followed immediately by a slash, the tilde is replaced by the\n\
823home directory of the user running Octave. For example,\n\
827tilde_expand (\"~joeuser/bin\")\n\
828 @result{} \"/home/joeuser/bin\"\n\
829tilde_expand (\"~/bin\")\n\
830 @result{} \"/home/jwe/bin\"\n\
837 int nargin = args.length ();
841 octave_value arg = args(0);
843 string_vector sv = arg.all_strings ();
847 sv = file_ops::tilde_expand (sv);
849 if (arg.is_cellstr ())
850 retval = Cell (arg.dims (), sv);
855 error ("tilde_expand: expecting argument to be char or cellstr object");
863#if defined (__EMX__) && defined (OS2)
866 "extproc: ignored by Octave")
868 return octave_value_list ();
871DEFALIAS (EXTPROC, extproc);