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
101 #include <shellapi.h>
102 #include <shobjidl.h>
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 ();
185 #if defined (__MINGW32__) || defined (_MSC_VER)
187 SetCurrentProcessExplicitAppUserModelID (L
"gnu.octave." VERSION);
192 DEFUN (__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
230 DEFUN (__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.");
268 DEFUN (__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;
465 if (
len > 1 && isalpha(name[0]) && name[1]==
':'
466 && (
len == 2 || (
len == 3 && name[2] ==
'\\')))
468 if (
len > 4 && name[0] ==
'\\' && name[1] ==
'\\')
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;
820 DEFUN (getenv, args, ,
836 if (args.length () != 1)
839 std::string name = args(0).string_value ();
841 return ovl (sys::env::getenv (name));
848 DEFUN (setenv, args, ,
863 int nargin = args.length ();
865 if (nargin < 1 || nargin > 2)
868 std::string var = args(0).xstring_value (
"setenv: VAR must be a string");
870 std::string val = (nargin == 2
871 ? args(1).xstring_value (
"setenv: VALUE must be a string")
874 sys::env::putenv (var, val);
889 DEFUN (unsetenv, args, ,
899 if (args.length () != 1)
902 std::string tmp = args(0).string_value ();
911 #if defined (OCTAVE_USE_WINDOWS_API)
914 reg_close_key_wrapper (HKEY key)
923 get_regkey_value (HKEY h_rootkey,
const std::string subkey,
930 result = RegOpenKeyExW (h_rootkey, wsubkey.c_str (), 0, KEY_READ,
933 if (result != ERROR_SUCCESS)
936 unwind_action restore_keys ([=] () { reg_close_key_wrapper (h_subkey); });
940 result = RegQueryValueExW (h_subkey, wname.c_str (),
nullptr,
nullptr,
942 if (result != ERROR_SUCCESS)
947 result = RegQueryValueExW (h_subkey, wname.c_str (),
nullptr, &type,
949 if (result != ERROR_SUCCESS)
952 if (type == REG_DWORD)
953 value =
octave_int32 (*(
reinterpret_cast<DWORD *
> (data)));
954 else if (type == REG_SZ || type == REG_EXPAND_SZ)
957 wchar_t *dataw =
reinterpret_cast<wchar_t *
> (data);
958 DWORD lengthw = length /
sizeof (wchar_t);
959 std::wstring reg_string
960 = std::wstring (dataw, lengthw - (dataw[lengthw-1]==0));
968 get_regkey_names (HKEY h_rootkey,
const std::string subkey,
969 std::list<std::string>& fields)
977 retval = RegOpenKeyExW (h_rootkey, wsubkey.c_str (), 0, KEY_READ,
979 if (retval != ERROR_SUCCESS)
983 const int MAX_VALUE_NAME_SIZE = 32766;
984 wchar_t value_name[MAX_VALUE_NAME_SIZE+1];
985 DWORD value_name_size = MAX_VALUE_NAME_SIZE;
989 retval = RegEnumValueW (h_subkey, idx, value_name, &value_name_size,
990 nullptr,
nullptr,
nullptr,
nullptr);
991 if (retval != ERROR_SUCCESS)
994 value_name_size = MAX_VALUE_NAME_SIZE;
998 if (retval == ERROR_NO_MORE_ITEMS)
999 retval = ERROR_SUCCESS;
1001 RegCloseKey (h_subkey);
1008 DEFUN (winqueryreg, args, ,
1079 #if defined (OCTAVE_USE_WINDOWS_API)
1080 if ((args.length () < 2) || (args.length () > 3))
1084 std::string rootkey_name
1085 = args(0).xstring_value (
"winqueryreg: the first argument must be 'name' "
1086 "or a valid ROOTKEY identifier");
1087 std::string subkey_name =
"";
1088 std::string value_name =
"";
1089 bool get_names =
false;
1090 if (rootkey_name.compare (
"name") == 0)
1092 if (args.length () < 3)
1093 error (
"winqueryreg: if the first argument is 'name', "
1094 "ROOTKEY and SUBKEY must be given");
1097 = args(1).xstring_value (
"winqueryreg: ROOTKEY must be a string");
1099 = args(2).xstring_value (
"winqueryreg: SUBKEY must be a string");
1104 = args(1).xstring_value (
"winqueryreg: SUBKEY must be a string");
1106 if (args.length () == 3)
1108 = args(2).xstring_value (
"winqueryreg: VALUENAME must be a string");
1113 if (rootkey_name ==
"HKEY_CLASSES_ROOT" || rootkey_name ==
"HKCR")
1114 h_rootkey = HKEY_CLASSES_ROOT;
1115 else if (rootkey_name ==
"HKEY_CURRENT_CONFIG")
1116 h_rootkey = HKEY_CURRENT_CONFIG;
1117 else if (rootkey_name ==
"HKEY_CURRENT_USER" || rootkey_name ==
"HKCU")
1118 h_rootkey = HKEY_CURRENT_USER;
1119 else if (rootkey_name ==
"HKEY_LOCAL_MACHINE" || rootkey_name ==
"HKLM")
1120 h_rootkey = HKEY_LOCAL_MACHINE;
1121 else if (rootkey_name ==
"HKEY_PERFORMANCE_DATA")
1122 h_rootkey = HKEY_PERFORMANCE_DATA;
1123 else if (rootkey_name ==
"HKEY_USERS" || rootkey_name ==
"HKU")
1124 h_rootkey = HKEY_USERS;
1126 error (
"winqueryreg: ROOTKEY is not a valid root key identifier");
1130 std::list<std::string> fields;
1132 LONG retval = get_regkey_names (h_rootkey, subkey_name, fields);
1133 if (retval != ERROR_SUCCESS)
1134 error (
"winqueryreg: error %ld reading names from registry", retval);
1138 std::list<std::string>::const_iterator it;
1139 for (i = 0, it = fields.begin (); it != fields.end (); ++it, ++i)
1140 fieldnames(i) = *it;
1142 return ovl (fieldnames);
1147 LONG retval = get_regkey_value (h_rootkey, subkey_name,
1148 value_name, key_val);
1149 if (retval == ERROR_FILE_NOT_FOUND)
1150 error (
"winqueryreg: no value found for '%s' at %s\\%s",
1151 value_name.c_str (), rootkey_name.c_str (),
1152 subkey_name.c_str ());
1153 if (retval != ERROR_SUCCESS)
1154 error (
"winqueryreg: error %ld reading the specified key", retval);
1156 return ovl (key_val);
1160 octave_unused_parameter (args);
1162 error (
"winqueryreg: function is only supported on Windows platforms");
1247 int c =
kbhit (args.length () == 0);
1252 char s[2] = {
static_cast<char> (c),
'\0' };
1260 DEFMETHOD (pause, interp, args, nargout,
1314 int nargin = args.
length ();
1319 if (nargin == 1 && args(0).is_string ())
1322 std::string
state = args(0).string_value ();
1326 else if (
state ==
"off")
1328 else if (
state ==
"query")
1331 error (R
"(pause: first argument must be "on", "off", or "query")");
1333 if (nargout > 0 ||
state ==
"query")
1334 retval.
append (saved_state ?
"on" :
"off");
1343 dval = args(0).xdouble_value (
"pause: N must be a scalar real value");
1346 warning (
"pause: NaN is an invalid delay");
1430 if (args.length () != 1)
1454 DEFUN (get_home_directory, , ,
1466 return ovl (sys::env::get_home_directory ());
1476 DEFUN (__blas_version__, , ,
1485 DEFUN (__lapack_version__, , ,
static bool forced_interactive(void)
static bool prefer_env_winsize(bool)
static void clear_screen(bool skip_redisplay=false)
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
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
std::string bin_dir(void)
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)
std::string tilde_expand(const std::string &name)
OCTAVE_EXPORT octave_value_list Fdrawnow(octave::interpreter &interp, const octave_value_list &args, int)
interpreter & __get_interpreter__(void)
F77_RET_T const F77_DBLE const F77_DBLE * f
std::string u8_from_wstring(const std::wstring &wchar_string)
std::wstring u8_to_wstring(const std::string &utf8_string)
int unsetenv_wrapper(const std::string &name)
std::string lapack_version(void)
std::string blas_version(void)
std::string float_format_as_string(float_format flt_fmt)
float_format native_float_format(void)
@ flt_fmt_ieee_big_endian
@ flt_fmt_ieee_little_endian
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.
static uint32_t state[624]
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)
FILE * popen(const char *command, const char *mode)
void sysdep_cleanup(void)
void raw_mode(bool on, bool wait)
static bool Vpause_enabled
std::string get_P_tmpdir(void)
void set_application_id(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)