26#if defined (HAVE_CONFIG_H)
36#if defined (HAVE_TERMIOS_H)
38#elif defined (HAVE_TERMIO_H)
40#elif defined (HAVE_SGTTY_H)
44#if defined (HAVE_CONIO_H)
48#if defined (HAVE_SYS_IOCTL_H)
49# include <sys/ioctl.h>
52#if defined (HAVE_FLOATINGPOINT_H)
53# include <floatingpoint.h>
56#if defined (HAVE_IEEEFP_H)
60#if defined (HAVE_OMP_H)
73#include "builtin-defun-decls.h"
91#if ! defined (STDIN_FILENO)
92# define STDIN_FILENO 1
95#if defined (__MINGW32__) || defined (_MSC_VER)
97#define WIN32_LEAN_AND_MEAN
106OCTAVE_NAMESPACE_BEGIN
108#if defined (__386BSD__) || defined (__FreeBSD__) || defined (__NetBSD__)
113# if defined (HAVE_FLOATINGPOINT_H)
115# if ! defined (FP_X_DNML)
118 fpsetmask (~(FP_X_OFL|FP_X_INV|FP_X_DZ|FP_X_DNML|FP_X_UFL|FP_X_IMP));
124#if defined (__MINGW32__) || defined (_MSC_VER)
127 w32_set_octave_home (
void)
131 HANDLE h = CreateToolhelp32Snapshot (TH32CS_SNAPMODULE
132#
if defined (TH32CS_SNAPMODULE32)
133 | TH32CS_SNAPMODULE32
137 if (h != INVALID_HANDLE_VALUE)
139 MODULEENTRY32W mod_info;
141 ZeroMemory (&mod_info,
sizeof (mod_info));
142 mod_info.dwSize =
sizeof (mod_info);
144 if (Module32FirstW (h, &mod_info))
150 if (mod_name.find (
"octinterp") != std::string::npos)
158 while (Module32NextW (h, &mod_info));
166 std::size_t pos =
bin_dir.rfind (R
"(\bin\)");
168 if (pos != std::string::npos)
169 sys::env::putenv (
"OCTAVE_HOME",
bin_dir.substr (0, pos));
176 w32_set_octave_home ();
178 command_editor::prefer_env_winsize (
true);
185#if defined (__MINGW32__) || defined (_MSC_VER)
187 SetCurrentProcessExplicitAppUserModelID (L
"gnu.octave." VERSION);
192DEFUN (__open_with_system_app__, args, ,
198 if (args.length () != 1)
201 std::string file = args(0).xstring_value (
"__open_with_system_app__: argument must be a filename");
203#if defined (OCTAVE_USE_WINDOWS_API)
205 HINSTANCE status = ShellExecuteW (0, 0, wfile.c_str (), 0, 0, SW_SHOWNORMAL);
208 return octave_value (
reinterpret_cast<std::ptrdiff_t
> (status) > 32);
211 file =
'"' + file +
'"';
213# if defined (__APPLE__)
214# define FSYSTEM_OPEN_STR "open "
216# define FSYSTEM_OPEN_STR "xdg-open "
222# undef FSYSTEM_OPEN_STR
230DEFUN (__is_elevated_process__, args, ,
240#if defined (OCTAVE_USE_WINDOWS_API)
241 if (args.length () != 0)
245 HANDLE h_token =
nullptr;
247 if (OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &h_token))
249 TOKEN_ELEVATION elevation;
250 DWORD return_length =
sizeof (TOKEN_ELEVATION);
251 if (GetTokenInformation (h_token, TokenElevation, &elevation,
252 sizeof (elevation), &return_length))
253 retval = elevation.TokenIsElevated;
257 CloseHandle (h_token);
262 octave_unused_parameter (args);
263 error (
"__is_elevated_process__: "
264 "Function is only supported on Windows platforms.");
268DEFUN (__wmemory__, args, ,
276#if defined (OCTAVE_USE_WINDOWS_API)
277 if (args.length () != 0)
283 HANDLE h_proc = GetCurrentProcess ();
284 if (h_proc ==
nullptr)
285 error (
"__wmemory__: Couldn't open handle to own process");
287 PROCESS_MEMORY_COUNTERS proc_mem_count;
288 if (GetProcessMemoryInfo (h_proc, &proc_mem_count,
sizeof (proc_mem_count)))
290 proc_struct.
setfield (
"PageFaultCount",
291 proc_mem_count.PageFaultCount);
292 proc_struct.
setfield (
"PeakWorkingSetSize",
293 proc_mem_count.PeakWorkingSetSize);
294 proc_struct.
setfield (
"WorkingSetSize",
295 proc_mem_count.WorkingSetSize);
296 proc_struct.
setfield (
"QuotaPeakPagedPoolUsage",
297 proc_mem_count.QuotaPeakPagedPoolUsage);
298 proc_struct.
setfield (
"QuotaPagedPoolUsage",
299 proc_mem_count.QuotaPagedPoolUsage);
300 proc_struct.
setfield (
"QuotaPeakNonPagedPoolUsage",
301 proc_mem_count.QuotaPeakNonPagedPoolUsage);
302 proc_struct.
setfield (
"QuotaNonPagedPoolUsage",
303 proc_mem_count.QuotaNonPagedPoolUsage);
304 proc_struct.
setfield (
"PagefileUsage",
305 proc_mem_count.PagefileUsage);
306 proc_struct.
setfield (
"PeakPagefileUsage",
307 proc_mem_count.PeakPagefileUsage);
311 proc_struct.
setfield (
"PageFaultCount", 0);
312 proc_struct.
setfield (
"PeakWorkingSetSize", 0);
313 proc_struct.
setfield (
"WorkingSetSize", 0);
314 proc_struct.
setfield (
"QuotaPeakPagedPoolUsage", 0);
315 proc_struct.
setfield (
"QuotaPagedPoolUsage", 0);
316 proc_struct.
setfield (
"QuotaPeakNonPagedPoolUsage", 0);
317 proc_struct.
setfield (
"QuotaNonPagedPoolUsage", 0);
318 proc_struct.
setfield (
"PagefileUsage", 0);
319 proc_struct.
setfield (
"PeakPagefileUsage", 0);
322 CloseHandle (h_proc);
327 MEMORYSTATUSEX mem_stat;
329 mem_stat.dwLength =
sizeof (mem_stat);
331 if (GlobalMemoryStatusEx (&mem_stat))
333 sys_struct.
setfield (
"MemoryLoad", mem_stat.dwMemoryLoad);
334 sys_struct.
setfield (
"TotalPhys", mem_stat.ullTotalPhys);
335 sys_struct.
setfield (
"AvailPhys", mem_stat.ullAvailPhys);
336 sys_struct.
setfield (
"TotalPageFile", mem_stat.ullTotalPageFile);
337 sys_struct.
setfield (
"AvailPageFile", mem_stat.ullAvailPageFile);
338 sys_struct.
setfield (
"TotalVirtual", mem_stat.ullTotalVirtual);
339 sys_struct.
setfield (
"AvailVirtual", mem_stat.ullAvailVirtual);
340 sys_struct.
setfield (
"AvailExtendedVirtual",
341 mem_stat.ullAvailExtendedVirtual);
345 sys_struct.
setfield (
"MemoryLoad", 0);
346 sys_struct.
setfield (
"TotalPhys", 0);
347 sys_struct.
setfield (
"AvailPhys", 0);
348 sys_struct.
setfield (
"TotalPageFile", 0);
349 sys_struct.
setfield (
"AvailPageFile", 0);
350 sys_struct.
setfield (
"TotalVirtual", 0);
351 sys_struct.
setfield (
"AvailVirtual", 0);
352 sys_struct.
setfield (
"AvailExtendedVirtual", 0);
355 return ovl (proc_struct, sys_struct);
358 octave_unused_parameter (args);
359 error (
"__wmemory__: Function is only supported on Windows platforms");
363#if defined (__MINGW32__)
373#if defined (_MSC_VER)
387#if defined (OCTAVE_USE_WINDOWS_API)
396 const wchar_t *f1 = file1w.c_str ();
397 const wchar_t *f2 = file2w.c_str ();
399 bool f1_is_dir = GetFileAttributesW (f1) & FILE_ATTRIBUTE_DIRECTORY;
400 bool f2_is_dir = GetFileAttributesW (f2) & FILE_ATTRIBUTE_DIRECTORY;
405 DWORD share = FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE;
408 = CreateFileW (f1, 0, share, 0, OPEN_EXISTING,
409 f1_is_dir ? FILE_FLAG_BACKUP_SEMANTICS : 0, 0);
411 if (hfile1 != INVALID_HANDLE_VALUE)
414 = CreateFileW (f2, 0, share, 0, OPEN_EXISTING,
415 f2_is_dir ? FILE_FLAG_BACKUP_SEMANTICS : 0, 0);
417 if (hfile2 != INVALID_HANDLE_VALUE)
419 BY_HANDLE_FILE_INFORMATION hfi1;
420 BY_HANDLE_FILE_INFORMATION hfi2;
422 if (GetFileInformationByHandle (hfile1, &hfi1)
423 && GetFileInformationByHandle (hfile2, &hfi2))
425 retval = (hfi1.dwVolumeSerialNumber == hfi2.dwVolumeSerialNumber
426 && hfi1.nFileIndexHigh == hfi2.nFileIndexHigh
427 && hfi1.nFileIndexLow == hfi2.nFileIndexLow
428 && hfi1.nFileSizeHigh == hfi2.nFileSizeHigh
429 && hfi1.nFileSizeLow == hfi2.nFileSizeLow
430 && hfi1.ftLastWriteTime.dwLowDateTime
431 == hfi2.ftLastWriteTime.dwLowDateTime
432 && hfi1.ftLastWriteTime.dwHighDateTime
433 == hfi2.ftLastWriteTime.dwHighDateTime);
436 CloseHandle (hfile2);
439 CloseHandle (hfile1);
448 sys::file_stat fs_file1 (file1);
449 sys::file_stat fs_file2 (file2);
451 return (fs_file1 && fs_file2
452 && fs_file1.ino () == fs_file2.ino ()
453 && fs_file1.dev () == fs_file2.dev ());
462#if defined (OCTAVE_USE_WINDOWS_API)
463 std::size_t
len =
name.length ();
464 bool candidate =
false;
466 && (
len == 2 || (
len == 3 &&
name[2] ==
'\\')))
471 std::size_t next_slash =
name.find (
'\\', 3);
472 if (next_slash != std::string::npos &&
len > next_slash+1)
475 std::size_t last_slash =
name.find (
'\\', next_slash+1);
476 if (last_slash == std::string::npos
477 || (
len > next_slash+2 && last_slash ==
len-1))
487 = CreateFileW (wname.c_str (), FILE_READ_ATTRIBUTES,
488 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
489 nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS,
491 if (h != INVALID_HANDLE_VALUE)
502 octave_unused_parameter (
name);
514#if defined (HAVE_OMP_GET_NUM_THREADS)
515 omp_get_num_threads ();
518#if defined (__386BSD__) || defined (__FreeBSD__) || defined (__NetBSD__)
520#elif defined (__MINGW32__)
522#elif defined (_MSC_VER)
529#if defined (OCTAVE_USE_WINDOWS_API)
531 SetProcessShutdownParameters (0x280, SHUTDOWN_NORETRY);
549 static bool curr_on =
false;
557 error (
"stdin is not a tty!");
563#if defined (HAVE_TERMIOS_H)
566 static struct termios save_term;
572 tcgetattr (tty_fd, &s);
583 s.c_lflag &= ~(ICANON |
ECHO | ECHOE | ECHOK | ECHONL);
584 s.c_oflag |= (OPOST | ONLCR);
586 s.c_oflag &= ~(OCRNL);
589 s.c_oflag &= ~(ONOCR);
592 s.c_oflag &= ~(ONLRET);
594 s.c_cc[VMIN] = (wait ? 1 : 0);
604 tcsetattr (tty_fd, wait ? TCSAFLUSH : TCSADRAIN, &s);
606#elif defined (HAVE_TERMIO_H)
609 static struct termio save_term;
615 ioctl (tty_fd, TCGETA, &s);
626 s.c_lflag &= ~(ICANON |
ECHO | ECHOE | ECHOK | ECHONL);
627 s.c_oflag |= (OPOST | ONLCR);
629 s.c_oflag &= ~(OCRNL);
632 s.c_oflag &= ~(ONOCR);
635 s.c_oflag &= ~(ONLRET);
637 s.c_cc[VMIN] = (wait ? 1 : 0);
646 ioctl (tty_fd, TCSETAW, &s);
648#elif defined (HAVE_SGTTY_H)
650 octave_unused_parameter (wait);
653 static struct sgttyb save_term;
659 ioctl (tty_fd, TIOCGETP, &s);
670 s.sg_flags |= CBREAK;
671 s.sg_flags &= ~(
ECHO);
680 ioctl (tty_fd, TIOCSETN, &s);
684 octave_unused_parameter (wait);
695 FILE *
popen (
const char *command,
const char *mode)
697#if defined (__MINGW32__) || defined (_MSC_VER)
702 if (wmode.length () < 2)
705 return _wpopen (wcommand.c_str (), wmode.c_str ());
713#if defined (__MINGW32__) || defined (_MSC_VER)
714 return ::_pclose (
f);
724#if defined (HAVE__KBHIT) && defined (HAVE__GETCH)
728 static constexpr int eof = std::istream::traits_type::eof ();
735 c = (! _kbhit ()) ? eof : _getch ();
749 int c = std::cin.get ();
751 if (std::cin.fail () || std::cin.eof ())
768#if defined (OCTAVE_USE_WINDOWS_API)
772#if defined (P_tmpdir)
780 if (retval.empty () || retval == R
"(\)")
782 retval = sys::env::getenv ("TEMP");
785 retval = sys::env::getenv (
"TMP");
788 retval = R
"(c:\temp)";
793#elif defined (P_tmpdir)
811 bool skip_redisplay =
true;
813 command_editor::clear_screen (skip_redisplay);
820DEFUN (getenv, args, ,
836 if (args.length () != 1)
839 std::string
name = args(0).string_value ();
841 return ovl (sys::env::getenv (
name));
848DEFUN (setenv, args, ,
860 int nargin = args.length ();
862 if (nargin < 1 || nargin > 2)
865 std::string var = args(0).xstring_value (
"setenv: VAR must be a string");
867 std::string val = (nargin == 2
868 ? args(1).xstring_value (
"setenv: VALUE must be a string")
871 sys::env::putenv (var, val);
886DEFUN (unsetenv, args, ,
896 if (args.length () != 1)
899 std::string tmp = args(0).string_value ();
908#if defined (OCTAVE_USE_WINDOWS_API)
911 reg_close_key_wrapper (HKEY key)
920 get_regkey_value (HKEY h_rootkey,
const std::string subkey,
927 result = RegOpenKeyExW (h_rootkey, wsubkey.c_str (), 0, KEY_READ,
930 if (result != ERROR_SUCCESS)
933 unwind_action restore_keys ([=] () { reg_close_key_wrapper (h_subkey); });
937 result = RegQueryValueExW (h_subkey, wname.c_str (),
nullptr,
nullptr,
939 if (result != ERROR_SUCCESS)
944 result = RegQueryValueExW (h_subkey, wname.c_str (),
nullptr, &type,
946 if (result != ERROR_SUCCESS)
949 if (type == REG_DWORD)
950 value =
octave_int32 (*(
reinterpret_cast<DWORD *
> (data)));
951 else if (type == REG_SZ || type == REG_EXPAND_SZ)
954 wchar_t *dataw =
reinterpret_cast<wchar_t *
> (data);
955 DWORD lengthw = length /
sizeof (wchar_t);
956 std::wstring reg_string
957 = std::wstring (dataw, lengthw - (dataw[lengthw-1]==0));
965 get_regkey_names (HKEY h_rootkey,
const std::string subkey,
966 std::list<std::string> &fields)
974 retval = RegOpenKeyExW (h_rootkey, wsubkey.c_str (), 0, KEY_READ,
976 if (retval != ERROR_SUCCESS)
980 const int MAX_VALUE_NAME_SIZE = 32766;
981 wchar_t value_name[MAX_VALUE_NAME_SIZE+1];
982 DWORD value_name_size = MAX_VALUE_NAME_SIZE;
986 retval = RegEnumValueW (h_subkey, idx, value_name, &value_name_size,
987 nullptr,
nullptr,
nullptr,
nullptr);
988 if (retval != ERROR_SUCCESS)
991 value_name_size = MAX_VALUE_NAME_SIZE;
995 if (retval == ERROR_NO_MORE_ITEMS)
996 retval = ERROR_SUCCESS;
998 RegCloseKey (h_subkey);
1005DEFUN (winqueryreg, args, ,
1076#if defined (OCTAVE_USE_WINDOWS_API)
1077 if ((args.length () < 2) || (args.length () > 3))
1081 std::string rootkey_name
1082 = args(0).xstring_value (
"winqueryreg: the first argument must be 'name' "
1083 "or a valid ROOTKEY identifier");
1084 std::string subkey_name =
"";
1085 std::string value_name =
"";
1086 bool get_names =
false;
1087 if (rootkey_name.compare (
"name") == 0)
1089 if (args.length () < 3)
1090 error (
"winqueryreg: if the first argument is 'name', "
1091 "ROOTKEY and SUBKEY must be given");
1094 = args(1).xstring_value (
"winqueryreg: ROOTKEY must be a string");
1096 = args(2).xstring_value (
"winqueryreg: SUBKEY must be a string");
1101 = args(1).xstring_value (
"winqueryreg: SUBKEY must be a string");
1103 if (args.length () == 3)
1105 = args(2).xstring_value (
"winqueryreg: VALUENAME must be a string");
1110 if (rootkey_name ==
"HKEY_CLASSES_ROOT" || rootkey_name ==
"HKCR")
1111 h_rootkey = HKEY_CLASSES_ROOT;
1112 else if (rootkey_name ==
"HKEY_CURRENT_CONFIG")
1113 h_rootkey = HKEY_CURRENT_CONFIG;
1114 else if (rootkey_name ==
"HKEY_CURRENT_USER" || rootkey_name ==
"HKCU")
1115 h_rootkey = HKEY_CURRENT_USER;
1116 else if (rootkey_name ==
"HKEY_LOCAL_MACHINE" || rootkey_name ==
"HKLM")
1117 h_rootkey = HKEY_LOCAL_MACHINE;
1118 else if (rootkey_name ==
"HKEY_PERFORMANCE_DATA")
1119 h_rootkey = HKEY_PERFORMANCE_DATA;
1120 else if (rootkey_name ==
"HKEY_USERS" || rootkey_name ==
"HKU")
1121 h_rootkey = HKEY_USERS;
1123 error (
"winqueryreg: ROOTKEY is not a valid root key identifier");
1127 std::list<std::string> fields;
1129 LONG retval = get_regkey_names (h_rootkey, subkey_name, fields);
1130 if (retval != ERROR_SUCCESS)
1131 error (
"winqueryreg: error %ld reading names from registry", retval);
1135 std::list<std::string>::const_iterator it;
1136 for (i = 0, it = fields.begin (); it != fields.end (); ++it, ++i)
1137 fieldnames(i) = *it;
1139 return ovl (fieldnames);
1144 LONG retval = get_regkey_value (h_rootkey, subkey_name,
1145 value_name, key_val);
1146 if (retval == ERROR_FILE_NOT_FOUND)
1147 error (
"winqueryreg: no value found for '%s' at %s\\%s",
1148 value_name.c_str (), rootkey_name.c_str (),
1149 subkey_name.c_str ());
1150 if (retval != ERROR_SUCCESS)
1151 error (
"winqueryreg: error %ld reading the specified key", retval);
1153 return ovl (key_val);
1157 octave_unused_parameter (args);
1159 error (
"winqueryreg: function is only supported on Windows platforms");
1242 int c =
kbhit (args.length () == 0);
1247 char s[2] = {
static_cast<char> (c),
'\0' };
1309 int nargin = args.
length ();
1314 if (nargin == 1 && args(0).is_string ())
1317 std::string
state = args(0).string_value ();
1321 else if (
state ==
"off")
1323 else if (
state ==
"query")
1326 error (R
"(pause: first argument must be "on", "off", or "query")");
1328 if (nargout > 0 ||
state ==
"query")
1329 retval.
append (saved_state ?
"on" :
"off");
1338 dval = args(0).xdouble_value (
"pause: N must be a scalar real value");
1341 warning (
"pause: NaN is an invalid delay");
1425 if (args.length () != 1)
1448DEFUN (get_home_directory, , ,
1460 return ovl (sys::env::get_home_directory ());
1470DEFUN (__blas_version__, , ,
1479DEFUN (__lapack_version__, , ,
static bool forced_interactive(void)
Vector representing the dimensions (size) of an Array.
bool interactive(void) const
void setfield(const std::string &key, const octave_value &val)
octave_value_list & append(const octave_value &val)
octave_idx_type length(void) const
bool iscellstr(void) const
OCTINTERP_API string_vector xstring_vector_value(const char *fmt,...) const
dim_vector dims(void) const
OCTINTERP_API void print_usage(void)
#define DEFMETHOD(name, interp_name, args_name, nargout_name, doc)
Macro to define a builtin method.
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
#define DEFALIAS(alias, name)
Macro to define an alias for another existing function name.
void warning(const char *fmt,...)
void error(const char *fmt,...)
void warn_disabled_feature(const std::string &fcn, const std::string &feature, const std::string &pkg)
OCTAVE_EXPORT octave_value_list Fdrawnow(octave::interpreter &interp, const octave_value_list &args, int)
F77_RET_T const F77_DBLE const F77_DBLE * f
std::string bin_dir(void)
float_format native_float_format(void)
@ flt_fmt_ieee_big_endian
@ flt_fmt_ieee_little_endian
std::string float_format_as_string(float_format flt_fmt)
std::string tilde_expand(const std::string &name)
std::string u8_from_wstring(const std::wstring &wchar_string)
int unsetenv_wrapper(const std::string &name)
std::string lapack_version(void)
std::wstring u8_to_wstring(const std::string &utf8_string)
std::string blas_version(void)
static uint32_t state[624]
interpreter & __get_interpreter__(const std::string &who)
octave_int< int32_t > octave_int32
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
interrupt_handler set_interrupt_handler(const volatile interrupt_handler &h, bool restart_syscalls)
interrupt_handler ignore_interrupts(void)
bool same_file_internal(const std::string &file1, const std::string &file2)
void sysdep_cleanup(void)
OCTAVE_NAMESPACE_BEGIN void set_application_id(void)
FILE * popen(const char *command, const char *mode)
void raw_mode(bool on, bool wait)
static bool Vpause_enabled
std::string get_P_tmpdir(void)
bool drive_or_unc_share(const std::string &name)
OCTAVE_EXPORT octave_value_list Fsystem(const octave_value_list &args, int nargout)
int octave_isatty_wrapper(int fd)
void sleep(double seconds, bool do_graphics_events)