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/>.
38#ifdef HAVE_SYS_TYPES_H
44#if defined (HAVE_TERMIOS_H)
46#elif defined (HAVE_TERMIO_H)
48#elif defined (HAVE_SGTTY_H)
52#if defined (HAVE_CONIO_H)
56#if defined (HAVE_SYS_IOCTL_H)
60#if defined (HAVE_FLOATINGPOINT_H)
61#include <floatingpoint.h>
64#if defined (HAVE_IEEEFP_H)
68#if !defined (HAVE_GETHOSTNAME) && defined (HAVE_SYS_UTSNAME_H)
69#include <sys/utsname.h>
74#include "lo-mappers.h"
88#include "sighandlers.h"
98#if defined (__386BSD__) || defined (__FreeBSD__) || defined (__NetBSD__)
102#if defined (HAVE_FLOATINGPOINT_H)
103 // Disable trapping on common exceptions.
107 fpsetmask (~(FP_X_OFL|FP_X_INV|FP_X_DZ|FP_X_DNML|FP_X_UFL|FP_X_IMP));
112#if defined (__WIN32__) && ! defined (_POSIX_VERSION)
114w32_set_octave_home (void)
118 std::string bin_dir (n, '\0');
122 HMODULE hMod = GetModuleHandle ("octinterp");
123 int status = GetModuleFileName (hMod, &bin_dir[0], n);
127 bin_dir.resize (status);
137 if (! bin_dir.empty ())
139 size_t pos = bin_dir.rfind ("\\bin\\");
141 if (pos != std::string::npos)
142 octave_env::putenv ("OCTAVE_HOME", bin_dir.substr (0, pos));
147w32_set_quiet_shutdown (void)
149 // Let the user close the console window or shutdown without the
152 // FIXME -- should this be user configurable?
153 SetProcessShutdownParameters (0x280, SHUTDOWN_NORETRY);
157MINGW_signal_cleanup (void)
159 w32_set_quiet_shutdown ();
165#if defined (__MINGW32__)
169 w32_set_octave_home ();
171 // Init mutex to protect setjmp/longjmp and get main thread context
174 w32_set_quiet_shutdown ();
178#if defined (_MSC_VER)
182 w32_set_octave_home ();
184 // Init mutex to protect setjmp/longjmp and get main thread context
187 w32_set_quiet_shutdown ();
192// Return TRUE if FILE1 and FILE2 refer to the same (physical) file.
195same_file_internal (const std::string& file1, const std::string& file2)
197#ifdef OCTAVE_USE_WINDOWS_API
201 // Windows native code
202 // Reference: http://msdn2.microsoft.com/en-us/library/aa363788.aspx
204 HANDLE hfile1 = CreateFile (file1.c_str (), 0, FILE_SHARE_READ, 0,
205 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
207 if (hfile1 != INVALID_HANDLE_VALUE)
209 HANDLE hfile2 = CreateFile (file2.c_str (), 0, FILE_SHARE_READ, 0,
210 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
212 if (hfile2 != INVALID_HANDLE_VALUE)
214 BY_HANDLE_FILE_INFORMATION hfi1;
215 BY_HANDLE_FILE_INFORMATION hfi2;
217 if (GetFileInformationByHandle (hfile1, &hfi1)
218 && GetFileInformationByHandle (hfile2, &hfi2))
220 retval = (hfi1.dwVolumeSerialNumber == hfi2.dwVolumeSerialNumber
221 && hfi1.nFileIndexHigh == hfi2.nFileIndexHigh
222 && hfi1.nFileIndexLow == hfi2.nFileIndexLow);
224 CloseHandle (hfile2);
227 CloseHandle (hfile1);
236 file_stat fs_file1 (file1);
237 file_stat fs_file2 (file2);
239 return (fs_file1 && fs_file2
240 && fs_file1.ino () == fs_file2.ino ()
241 && fs_file1.dev () == fs_file2.dev ());
246#if defined (__DECCXX)
248// These don't seem to be instantiated automatically...
250template std::istream&
251std::operator >> (std::istream&, std::complex<double>&);
254std::string::append (const std::string&, size_t, size_t);
261 typedef void (*_cplus_fcn_int) (int);
262 extern void (*malloc_error (_cplus_fcn_int)) (int);
266malloc_handler (int code)
269 warning ("hopefully recoverable malloc error: freeing wild pointer");
271 panic ("probably irrecoverable malloc error: code %d", code);
277 malloc_error (malloc_handler);
284 _control87 ((EM_INVALID | EM_DENORMAL | EM_ZERODIVIDE | EM_OVERFLOW
285 | EM_UNDERFLOW | EM_INEXACT), MCW_EM);
293#if defined (HAVE_IEEEFP_H)
294 // Disable trapping on common exceptions.
295 fpsetmask (~(FP_X_OFL|FP_X_INV|FP_X_DZ|FP_X_DNML|FP_X_UFL|FP_X_IMP));
303#if defined (__386BSD__) || defined (__FreeBSD__) || defined(__NetBSD__)
305#elif defined (__MINGW32__)
307#elif defined (_MSC_VER)
311#elif defined (__EMX__)
323 MINGW_SIGNAL_CLEANUP ();
326// Set terminal in raw mode. From less-177.
328// Change terminal to "raw mode", or restore to "normal" mode.
330// 1. An outstanding read will complete on receipt of a single keystroke.
331// 2. Input is not echoed.
332// 3. On output, \n is mapped to \r\n.
333// 4. \t is NOT expanded into spaces.
334// 5. Signal-causing characters such as ctrl-C (interrupt),
335// etc. are NOT disabled.
336// It doesn't matter whether an input \n is mapped to \r, or vice versa.
339raw_mode (bool on, bool wait)
341 static bool curr_on = false;
343 int tty_fd = STDIN_FILENO;
344 if (! isatty (tty_fd))
347 error ("stdin is not a tty!");
354#if defined (HAVE_TERMIOS_H)
357 static struct termios save_term;
361 // Get terminal modes.
363 tcgetattr (tty_fd, &s);
365 // Save modes and set certain variables dependent on modes.
368// ospeed = s.c_cflag & CBAUD;
369// erase_char = s.c_cc[VERASE];
370// kill_char = s.c_cc[VKILL];
372 // Set the modes to the way we want them.
374 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL);
375 s.c_oflag |= (OPOST|ONLCR);
377 s.c_oflag &= ~(OCRNL);
380 s.c_oflag &= ~(ONOCR);
383 s.c_oflag &= ~(ONLRET);
385 s.c_cc[VMIN] = wait ? 1 : 0;
390 // Restore saved modes.
395 tcsetattr (tty_fd, wait ? TCSAFLUSH : TCSADRAIN, &s);
397#elif defined (HAVE_TERMIO_H)
400 static struct termio save_term;
404 // Get terminal modes.
406 ioctl (tty_fd, TCGETA, &s);
408 // Save modes and set certain variables dependent on modes.
411// ospeed = s.c_cflag & CBAUD;
412// erase_char = s.c_cc[VERASE];
413// kill_char = s.c_cc[VKILL];
415 // Set the modes to the way we want them.
417 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL);
418 s.c_oflag |= (OPOST|ONLCR);
420 s.c_oflag &= ~(OCRNL);
423 s.c_oflag &= ~(ONOCR);
426 s.c_oflag &= ~(ONLRET);
428 s.c_cc[VMIN] = wait ? 1 : 0;
432 // Restore saved modes.
437 ioctl (tty_fd, TCSETAW, &s);
439#elif defined (HAVE_SGTTY_H)
442 static struct sgttyb save_term;
446 // Get terminal modes.
448 ioctl (tty_fd, TIOCGETP, &s);
450 // Save modes and set certain variables dependent on modes.
453// ospeed = s.sg_ospeed;
454// erase_char = s.sg_erase;
455// kill_char = s.sg_kill;
457 // Set the modes to the way we want them.
459 s.sg_flags |= CBREAK;
460 s.sg_flags &= ~(ECHO);
464 // Restore saved modes.
469 ioctl (tty_fd, TIOCSETN, &s);
472 warning ("no support for raw mode console I/O on this system");
474 // Make sure the current mode doesn't toggle.
482octave_popen (const char *command, const char *mode)
484#if defined (__MINGW32__) || defined (_MSC_VER)
485 if (mode && mode[0] && ! mode[1])
492 return _popen (command, tmode);
495 return _popen (command, mode);
497 return popen (command, mode);
502octave_pclose (FILE *f)
504#if defined (__MINGW32__) || defined (_MSC_VER)
511// Read one character from the terminal.
514octave_kbhit (bool wait)
517 int c = (! wait && ! _kbhit ()) ? 0 : std::cin.get ();
519 raw_mode (true, wait);
521 // Get current handler.
522 octave_interrupt_handler saved_interrupt_handler
523 = octave_ignore_interrupts ();
525 // Restore it, disabling system call restarts (if possible) so the
526 // read can be interrupted.
528 octave_set_interrupt_handler (saved_interrupt_handler, false);
530 int c = std::cin.get ();
532 if (std::cin.fail () || std::cin.eof ())
535 // Restore it, enabling system call restarts (if possible).
536 octave_set_interrupt_handler (saved_interrupt_handler, true);
538 raw_mode (false, true);
546@deftypefn {Built-in Function} {} clc ()\n\
547@deftypefnx {Built-in Function} {} home ()\n\
548Clear the terminal screen and move the cursor to the upper left corner.\n\
551 command_editor::clear_screen ();
553 return octave_value_list ();
558DEFUN (getenv, args, ,
560@deftypefn {Built-in Function} {} getenv (@var{var})\n\
561Return the value of the environment variable @var{var}. For example,\n\
568returns a string containing the value of your path.\n\
573 int nargin = args.length ();
577 std::string name = args(0).string_value ();
580 retval = octave_env::getenv (name);
588DEFUN (putenv, args, ,
590@deftypefn {Built-in Function} {} putenv (@var{var}, @var{value})\n\
591@deftypefnx {Built-in Function} {} setenv (@var{var}, @var{value})\n\
592Set the value of the environment variable @var{var} to @var{value}.\n\
595 octave_value_list retval;
597 int nargin = args.length ();
599 if (nargin == 2 || nargin == 1)
601 std::string var = args(0).string_value ();
605 std::string val = (nargin == 2
606 ? args(1).string_value () : std::string ());
609 octave_env::putenv (var, val);
611 error ("putenv: second argument should be a string");
614 error ("putenv: first argument should be a string");
621DEFALIAS (setenv, putenv);
623// FIXME -- perhaps kbhit should also be able to print a prompt?
627@deftypefn {Built-in Function} {} kbhit ()\n\
628Read a single keystroke from the keyboard. If called with one\n\
629argument, don't wait for a keypress. For example,\n\
636will set @var{x} to the next character typed at the keyboard as soon as\n\
644identical to the above example, but don't wait for a keypress,\n\
645returning the empty string if no key is available.\n\
650 // FIXME -- add timeout and default value args?
652 if (interactive || forced_interactive)
656 int c = octave_kbhit (args.length () == 0);
661 char *s = new char [2];
672@deftypefn {Built-in Function} {} pause (@var{seconds})\n\
673Suspend the execution of the program. If invoked without any arguments,\n\
674Octave waits until you type a character. With a numeric argument, it\n\
675pauses for the given number of seconds. For example, the following\n\
676statement prints a message and then waits 5 seconds before clearing the\n\
681fprintf (stderr, \"wait please...\\n\");\n\
688 octave_value_list retval;
690 int nargin = args.length ();
692 if (! (nargin == 0 || nargin == 1))
700 double dval = args(0).double_value ();
710 flush_octave_stdout ();
717 warning ("pause: NaN is an invalid delay");
723 flush_octave_stdout ();
732@deftypefn {Built-in Function} {} sleep (@var{seconds})\n\
733Suspend the execution of the program for the given number of seconds.\n\
736 octave_value_list retval;
738 if (args.length () == 1)
740 double dval = args(0).double_value ();
745 warning ("sleep: NaN is an invalid delay");
759DEFUN (usleep, args, ,
761@deftypefn {Built-in Function} {} usleep (@var{microseconds})\n\
762Suspend the execution of the program for the given number of\n\
763microseconds. On systems where it is not possible to sleep for periods\n\
764of time less than one second, @code{usleep} will pause the execution for\n\
765@code{round (@var{microseconds} / 1e6)} seconds.\n\
768 octave_value_list retval;
770 if (args.length () == 1)
772 double dval = args(0).double_value ();
777 warning ("usleep: NaN is an invalid delay");
782 int delay = NINT (dval);
785 octave_usleep (delay);
795// FIXME -- maybe this should only return 1 if IEEE floating
796// point functions really work.
800@deftypefn {Built-in Function} {} isieee ()\n\
801Return 1 if your computer claims to conform to the IEEE standard for\n\
802floating point calculations.\n\
805 oct_mach_info::float_format flt_fmt = oct_mach_info::native_float_format ();
807 return octave_value (flt_fmt == oct_mach_info::flt_fmt_ieee_little_endian
808 || flt_fmt == oct_mach_info::flt_fmt_ieee_big_endian);
811DEFUN (native_float_format, , ,
813@deftypefn {Built-in Function} {} native_float_format ()\n\
814Return the native floating point format as a string\n\
817 oct_mach_info::float_format flt_fmt = oct_mach_info::native_float_format ();
819 return octave_value (oct_mach_info::float_format_as_string (flt_fmt));
822DEFUN (tilde_expand, args, ,
824@deftypefn {Built-in Function} {} tilde_expand (@var{string})\n\
825Performs tilde expansion on @var{string}. If @var{string} begins with a\n\
826tilde character, (@samp{~}), all of the characters preceding the first\n\
827slash (or all characters, if there is no slash) are treated as a\n\
828possible user name, and the tilde and the following characters up to the\n\
829slash are replaced by the home directory of the named user. If the\n\
830tilde is followed immediately by a slash, the tilde is replaced by the\n\
831home directory of the user running Octave. For example,\n\
835tilde_expand (\"~joeuser/bin\")\n\
836 @result{} \"/home/joeuser/bin\"\n\
837tilde_expand (\"~/bin\")\n\
838 @result{} \"/home/jwe/bin\"\n\
845 int nargin = args.length ();
849 octave_value arg = args(0);
851 string_vector sv = arg.all_strings ();
855 sv = file_ops::tilde_expand (sv);
857 if (arg.is_cellstr ())
858 retval = Cell (arg.dims (), sv);
863 error ("tilde_expand: expecting argument to be char or cellstr object");
871#if defined (__EMX__) && defined (OS2)
874 "extproc: ignored by Octave")
876 return octave_value_list ();
879DEFALIAS (EXTPROC, extproc);
884;;; Local Variables: ***