36 #include <sys/types.h>
39 #if defined (HAVE_TERMIOS_H)
41 #elif defined (HAVE_TERMIO_H)
43 #elif defined (HAVE_SGTTY_H)
47 #if defined (HAVE_CONIO_H)
51 #if defined (HAVE_SYS_IOCTL_H)
52 #include <sys/ioctl.h>
55 #if defined (HAVE_FLOATINGPOINT_H)
56 #include <floatingpoint.h>
59 #if defined (HAVE_IEEEFP_H)
88 #define STDIN_FILENO 1
91 #if defined (__386BSD__) || defined (__FreeBSD__) || defined (__NetBSD__)
95 #if defined (HAVE_FLOATINGPOINT_H)
100 fpsetmask (~(FP_X_OFL|FP_X_INV|FP_X_DZ|FP_X_DNML|FP_X_UFL|FP_X_IMP));
105 #if defined (__WIN32__) && ! defined (_POSIX_VERSION)
107 #define WIN32_LEAN_AND_MEAN
108 #include <tlhelp32.h>
112 w32_set_octave_home (
void)
116 HANDLE h = CreateToolhelp32Snapshot (TH32CS_SNAPMODULE
117 #ifdef TH32CS_SNAPMODULE32
118 | TH32CS_SNAPMODULE32
122 if (h != INVALID_HANDLE_VALUE)
124 MODULEENTRY32 mod_info;
126 ZeroMemory (&mod_info,
sizeof (mod_info));
127 mod_info.dwSize =
sizeof (mod_info);
129 if (Module32First (h, &mod_info))
133 std::string mod_name (mod_info.szModule);
135 if (mod_name.find (
"octinterp") != std::string::npos)
137 bin_dir = mod_info.szExePath;
138 if (bin_dir[bin_dir.length () - 1] !=
'\\')
139 bin_dir.append (1,
'\\');
143 while (Module32Next (h, &mod_info));
149 if (! bin_dir.empty ())
151 size_t pos = bin_dir.rfind (
"\\bin\\");
153 if (pos != std::string::npos)
165 SetProcessShutdownParameters (0x280, SHUTDOWN_NORETRY);
169 MINGW_signal_cleanup (
void)
177 w32_set_octave_home ();
183 w32_shell_execute (
const std::string& file)
188 DEFUN (__open_with_system_app__, args, ,
190 @deftypefn {Loadable Function} {} __open_with_system_app__ (@var{file})\n\
191 Undocumented internal function.\n\
196 if (args.length () == 1)
202 #if defined (__WIN32__) && ! defined (_POSIX_VERSION)
203 HINSTANCE status = ShellExecute (0, 0, file.c_str (), 0, 0,
207 retval = (
reinterpret_cast<ptrdiff_t
> (status) > 32);
210 =
Fsystem (
ovl (
"xdg-open " + file +
" 2> /dev/null",
214 retval = (tmp(0).double_value () == 0);
218 error (
"__open_with_system_app__: argument must be a file name");
226 #if defined (__MINGW32__)
234 #if defined (_MSC_VER)
248 #ifdef OCTAVE_USE_WINDOWS_API
252 const char *f1 = file1.c_str ();
253 const char *f2 = file2.c_str ();
255 bool f1_is_dir = GetFileAttributes (f1) & FILE_ATTRIBUTE_DIRECTORY;
256 bool f2_is_dir = GetFileAttributes (f2) & FILE_ATTRIBUTE_DIRECTORY;
261 DWORD share = FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE;
264 = CreateFile (f1, 0, share, 0, OPEN_EXISTING,
265 f1_is_dir ? FILE_FLAG_BACKUP_SEMANTICS : 0, 0);
267 if (hfile1 != INVALID_HANDLE_VALUE)
270 = CreateFile (f2, 0, share, 0, OPEN_EXISTING,
271 f2_is_dir ? FILE_FLAG_BACKUP_SEMANTICS : 0, 0);
273 if (hfile2 != INVALID_HANDLE_VALUE)
275 BY_HANDLE_FILE_INFORMATION hfi1;
276 BY_HANDLE_FILE_INFORMATION hfi2;
278 if (GetFileInformationByHandle (hfile1, &hfi1)
279 && GetFileInformationByHandle (hfile2, &hfi2))
281 retval = (hfi1.dwVolumeSerialNumber == hfi2.dwVolumeSerialNumber
282 && hfi1.nFileIndexHigh == hfi2.nFileIndexHigh
283 && hfi1.nFileIndexLow == hfi2.nFileIndexLow);
286 CloseHandle (hfile2);
289 CloseHandle (hfile1);
301 return (fs_file1 && fs_file2
302 && fs_file1.
ino () == fs_file2.
ino ()
303 && fs_file1.
dev () == fs_file2.
dev ());
311 #if defined (__386BSD__) || defined (__FreeBSD__) || defined (__NetBSD__)
313 #elif defined (__MINGW32__)
315 #elif defined (_MSC_VER)
341 static bool curr_on =
false;
343 int tty_fd = STDIN_FILENO;
344 if (! gnulib::isatty (tty_fd))
347 error (
"stdin is not a tty!");
354 #if defined (HAVE_TERMIOS_H)
357 static struct termios save_term;
363 tcgetattr (tty_fd, &s);
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;
395 tcsetattr (tty_fd, wait ? TCSAFLUSH : TCSADRAIN, &s);
397 #elif defined (HAVE_TERMIO_H)
400 static struct termio save_term;
406 ioctl (tty_fd, TCGETA, &s);
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;
437 ioctl (tty_fd, TCSETAW, &s);
439 #elif defined (HAVE_SGTTY_H)
442 static struct sgttyb save_term;
448 ioctl (tty_fd, TIOCGETP, &s);
459 s.sg_flags |= CBREAK;
460 s.sg_flags &= ~(
ECHO);
469 ioctl (tty_fd, TIOCSETN, &s);
472 warning (
"no support for raw mode console I/O on this system");
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);
504 #if defined (__MINGW32__) || defined (_MSC_VER)
517 int c = (! wait && ! _kbhit ()) ? 0 : std::cin.
get ();
530 int c = std::cin.get ();
532 if (std::cin.fail () || std::cin.eof ())
547 #if defined (__WIN32__) && ! defined (_POSIX_VERSION)
551 #if defined (P_tmpdir)
559 if (retval.empty () || retval ==
"\\")
572 #elif defined (P_tmpdir)
585 @deftypefn {Built-in Function} {} clc ()\n\
586 @deftypefnx {Built-in Function} {} home ()\n\
587 Clear the terminal screen and move the cursor to the upper left corner.\n\
590 bool skip_redisplay =
true;
599 DEFUN (getenv, args, ,
601 @deftypefn {Built-in Function} {} getenv (@var{var})\n\
602 Return the value of the environment variable @var{var}.\n\
611 returns a string containing the value of your path.\n\
612 @seealso{setenv, unsetenv}\n\
617 int nargin = args.
length ();
621 std::string name = args(0).string_value ();
636 DEFUN (setenv, args, ,
638 @deftypefn {Built-in Function} {} setenv (@var{var}, @var{value})\n\
639 @deftypefnx {Built-in Function} {} setenv (@var{var})\n\
640 @deftypefnx {Built-in Function} {} putenv (@dots{})\n\
641 Set the value of the environment variable @var{var} to @var{value}.\n\
643 If no @var{value} is specified then the variable will be assigned the null\n\
645 @seealso{unsetenv, getenv}\n\
650 int nargin = args.
length ();
652 if (nargin == 2 || nargin == 1)
654 if (args(0).is_string ())
656 std::string var = args(0).string_value ();
658 std::string val = (nargin == 2
659 ? args(1).string_value () : std::string ());
664 error (
"setenv: VALUE must be a string");
667 error (
"setenv: VAR must be a string");
685 DEFUN (unsetenv, args, ,
687 @deftypefn {Built-in Function} {@var{status} =} unsetenv (@var{var})\n\
688 Delete the environment variable @var{var}.\n\
690 Return 0 if the variable was deleted, or did not exist, and -1 if an error\n\
692 @seealso{setenv, getenv}\n\
697 int nargin = args.
length ();
701 std::string tmp = args(0).string_value ();
705 int status = gnulib::unsetenv (tmp.c_str ());
721 DEFUN (kbhit, args, ,
723 @deftypefn {Built-in Function} {} kbhit ()\n\
724 @deftypefnx {Built-in Function} {} kbhit (1)\n\
725 Read a single keystroke from the keyboard.\n\
727 If called with an argument, don't wait for a keypress.\n\
736 will set @var{x} to the next character typed at the keyboard as soon as\n\
744 is identical to the above example, but doesn't wait for a keypress,\n\
745 returning the empty string if no key is available.\n\
746 @seealso{input, pause}\n\
762 char s[2] = {
static_cast<char> (c),
'\0' };
770 DEFUN (pause, args, ,
772 @deftypefn {Built-in Function} {} pause ()\n\
773 @deftypefnx {Built-in Function} {} pause (@var{n})\n\
774 Suspend the execution of the program for @var{n} seconds.\n\
776 @var{n} is a positive real value and may be a fraction of a second.\n\
778 If invoked without an input arguments then the program is suspended until a\n\
779 character is typed.\n\
781 The following example prints a message and then waits 5 seconds before\n\
782 clearing the screen.\n\
786 fprintf (stderr, \"wait please...\\n\");\n\
791 @seealso{kbhit, sleep}\n\
796 int nargin = args.
length ();
798 if (! (nargin == 0 || nargin == 1))
806 double dval = args(0).double_value ();
823 warning (
"pause: NaN is an invalid delay");
843 DEFUN (sleep, args, ,
845 @deftypefn {Built-in Function} {} sleep (@var{seconds})\n\
846 Suspend the execution of the program for the given number of seconds.\n\
847 @seealso{usleep, pause}\n\
852 if (args.length () == 1)
854 double dval = args(0).double_value ();
859 warning (
"sleep: NaN is an invalid delay");
881 DEFUN (usleep, args, ,
883 @deftypefn {Built-in Function} {} usleep (@var{microseconds})\n\
884 Suspend the execution of the program for the given number of\n\
887 On systems where it is not possible to sleep for periods of time less than\n\
888 one second, @code{usleep} will pause the execution for @code{round\n\
889 (@var{microseconds} / 1e6)} seconds.\n\
890 @seealso{sleep, pause}\n\
895 if (args.length () == 1)
897 double dval = args(0).double_value ();
902 warning (
"usleep: NaN is an invalid delay");
907 int delay =
NINT (dval);
933 @deftypefn {Built-in Function} {} isieee ()\n\
934 Return true if your computer @emph{claims} to conform to the IEEE standard\n\
935 for floating point calculations.\n\
937 No actual tests are performed.\n\
950 DEFUN (native_float_format, , ,
952 @deftypefn {Built-in Function} {} native_float_format ()\n\
953 Return the native floating point format as a string.\n\
965 DEFUN (tilde_expand, args, ,
967 @deftypefn {Built-in Function} {} tilde_expand (@var{string})\n\
968 Perform tilde expansion on @var{string}.\n\
970 If @var{string} begins with a tilde character, (@samp{~}), all of the\n\
971 characters preceding the first slash (or all characters, if there is no\n\
972 slash) are treated as a possible user name, and the tilde and the following\n\
973 characters up to the slash are replaced by the home directory of the named\n\
974 user. If the tilde is followed immediately by a slash, the tilde is\n\
975 replaced by the home directory of the user running Octave.\n\
981 tilde_expand (\"~joeuser/bin\")\n\
982 @result{} \"/home/joeuser/bin\"\n\
983 tilde_expand (\"~/bin\")\n\
984 @result{} \"/home/jwe/bin\"\n\
991 int nargin = args.
length ();
1009 error (
"tilde_expand: expecting argument to be char or cellstr object");
1025 DEFUN (get_home_directory, , ,
1027 @deftypefn {Built-in Function} {@var{homedir} =} get_home_directory ()\n\
1028 Return the current home directory.\n\
1030 On most systems, this is equivalent to @code{getenv (\"HOME\")}. On Windows\n\
1031 systems, if the environment variable @env{HOME} is not set then it is\n\
1033 @code{fullfile (getenv (\"HOMEDRIVE\"), getenv (\"HOMEPATH\"))}\n\
1051 DEFUN (have_window_system, , ,
1053 @deftypefn {Built-in Function} {} have_window_system ()\n\
1054 Return true if a window system is available (X11, Windows, or Apple OS X)\n\
1055 and false otherwise.\n\
1056 @seealso{isguirunning}\n\
octave_interrupt_handler octave_set_interrupt_handler(const volatile octave_interrupt_handler &h, bool restart_syscalls)
FILE * octave_popen(const char *command, const char *mode)
static std::string float_format_as_string(float_format)
void raw_mode(bool on, bool wait)
int octave_pclose(FILE *f)
OCTINTERP_API void print_usage(void)
static void putenv(const std::string &name, const std::string &value)
octave_idx_type length(void) const
void w32_set_quiet_shutdown(void)
int octave_kbhit(bool wait)
OCTINTERP_API octave_value_list Fdrawnow(const octave_value_list &=octave_value_list(), int=0)
#define DEFUN(name, args_name, nargout_name, doc)
void error(const char *fmt,...)
void octave_sleep(unsigned int seconds)
void sysdep_cleanup(void)
string_vector all_strings(bool pad=false) const
static std::string tilde_expand(const std::string &)
OCTINTERP_API octave_value_list Fsystem(const octave_value_list &=octave_value_list(), int=0)
#define DEFALIAS(alias, name)
F77_RET_T const double const double * f
#define MINGW_SIGNAL_CLEANUP()
bool same_file_internal(const std::string &file1, const std::string &file2)
std::string string_value(bool force=false) const
static std::string getenv(const std::string &name)
bool is_cellstr(void) const
octave_idx_type length(void) const
dim_vector dims(void) const
octave_interrupt_handler octave_ignore_interrupts(void)
void warning(const char *fmt,...)
static void clear_screen(bool skip_redisplay=false)
octave_value_list ovl(const octave_value &a0)
static float_format native_float_format(void)
static bool display_available(void)
std::string get_P_tmpdir(void)
static bool prefer_env_winsize(bool)
void octave_usleep(unsigned int useconds)
return octave_value(v1.char_array_value().concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string())? '\'': '"'))
static std::string get_home_directory(void)