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
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)
127w32_set_octave_home ()
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))
148 std::string mod_name (sys::u8_from_wstring (mod_info.szModule));
150 if (mod_name.find (
"octinterp") != std::string::npos)
152 bin_dir = sys::u8_from_wstring (mod_info.szExePath);
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 ();
186#if defined (__MINGW32__) || defined (_MSC_VER)
188 SetCurrentProcessExplicitAppUserModelID (L
"gnu.octave." VERSION);
193DEFUN (__open_with_system_app__, args, ,
199 if (args.length () != 1)
202 std::string file = args(0).xstring_value (
"__open_with_system_app__: argument must be a filename");
204#if defined (OCTAVE_USE_WINDOWS_API)
205 std::wstring wfile = sys::u8_to_wstring (file);
206 HINSTANCE status = ShellExecuteW (0, 0, wfile.c_str (), 0, 0, SW_SHOWNORMAL);
209 return octave_value (
reinterpret_cast<std::ptrdiff_t
> (status) > 32);
212 file =
'"' + file +
'"';
214# if defined (__APPLE__)
215# define FSYSTEM_OPEN_STR "open "
217# define FSYSTEM_OPEN_STR "xdg-open "
223# undef FSYSTEM_OPEN_STR
231DEFUN (__is_elevated_process__, args, ,
241#if defined (OCTAVE_USE_WINDOWS_API)
242 if (args.length () != 0)
246 HANDLE h_token =
nullptr;
248 if (OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &h_token))
250 TOKEN_ELEVATION elevation;
251 DWORD return_length =
sizeof (TOKEN_ELEVATION);
252 if (GetTokenInformation (h_token, TokenElevation, &elevation,
253 sizeof (elevation), &return_length))
254 retval = elevation.TokenIsElevated;
258 CloseHandle (h_token);
263 octave_unused_parameter (args);
264 error (
"__is_elevated_process__: "
265 "Function is only supported on Windows platforms.");
269DEFUN (__wmemory__, args, ,
277#if defined (OCTAVE_USE_WINDOWS_API)
278 if (args.length () != 0)
284 HANDLE h_proc = GetCurrentProcess ();
285 if (h_proc ==
nullptr)
286 error (
"__wmemory__: Couldn't open handle to own process");
288 PROCESS_MEMORY_COUNTERS proc_mem_count;
289 if (GetProcessMemoryInfo (h_proc, &proc_mem_count,
sizeof (proc_mem_count)))
291 proc_struct.
setfield (
"PageFaultCount",
292 proc_mem_count.PageFaultCount);
293 proc_struct.
setfield (
"PeakWorkingSetSize",
294 proc_mem_count.PeakWorkingSetSize);
295 proc_struct.
setfield (
"WorkingSetSize",
296 proc_mem_count.WorkingSetSize);
297 proc_struct.
setfield (
"QuotaPeakPagedPoolUsage",
298 proc_mem_count.QuotaPeakPagedPoolUsage);
299 proc_struct.
setfield (
"QuotaPagedPoolUsage",
300 proc_mem_count.QuotaPagedPoolUsage);
301 proc_struct.
setfield (
"QuotaPeakNonPagedPoolUsage",
302 proc_mem_count.QuotaPeakNonPagedPoolUsage);
303 proc_struct.
setfield (
"QuotaNonPagedPoolUsage",
304 proc_mem_count.QuotaNonPagedPoolUsage);
305 proc_struct.
setfield (
"PagefileUsage",
306 proc_mem_count.PagefileUsage);
307 proc_struct.
setfield (
"PeakPagefileUsage",
308 proc_mem_count.PeakPagefileUsage);
312 proc_struct.
setfield (
"PageFaultCount", 0);
313 proc_struct.
setfield (
"PeakWorkingSetSize", 0);
314 proc_struct.
setfield (
"WorkingSetSize", 0);
315 proc_struct.
setfield (
"QuotaPeakPagedPoolUsage", 0);
316 proc_struct.
setfield (
"QuotaPagedPoolUsage", 0);
317 proc_struct.
setfield (
"QuotaPeakNonPagedPoolUsage", 0);
318 proc_struct.
setfield (
"QuotaNonPagedPoolUsage", 0);
319 proc_struct.
setfield (
"PagefileUsage", 0);
320 proc_struct.
setfield (
"PeakPagefileUsage", 0);
323 CloseHandle (h_proc);
328 MEMORYSTATUSEX mem_stat;
330 mem_stat.dwLength =
sizeof (mem_stat);
332 if (GlobalMemoryStatusEx (&mem_stat))
334 sys_struct.
setfield (
"MemoryLoad", mem_stat.dwMemoryLoad);
335 sys_struct.
setfield (
"TotalPhys", mem_stat.ullTotalPhys);
336 sys_struct.
setfield (
"AvailPhys", mem_stat.ullAvailPhys);
337 sys_struct.
setfield (
"TotalPageFile", mem_stat.ullTotalPageFile);
338 sys_struct.
setfield (
"AvailPageFile", mem_stat.ullAvailPageFile);
339 sys_struct.
setfield (
"TotalVirtual", mem_stat.ullTotalVirtual);
340 sys_struct.
setfield (
"AvailVirtual", mem_stat.ullAvailVirtual);
341 sys_struct.
setfield (
"AvailExtendedVirtual",
342 mem_stat.ullAvailExtendedVirtual);
346 sys_struct.
setfield (
"MemoryLoad", 0);
347 sys_struct.
setfield (
"TotalPhys", 0);
348 sys_struct.
setfield (
"AvailPhys", 0);
349 sys_struct.
setfield (
"TotalPageFile", 0);
350 sys_struct.
setfield (
"AvailPageFile", 0);
351 sys_struct.
setfield (
"TotalVirtual", 0);
352 sys_struct.
setfield (
"AvailVirtual", 0);
353 sys_struct.
setfield (
"AvailExtendedVirtual", 0);
356 return ovl (proc_struct, sys_struct);
359 octave_unused_parameter (args);
360 error (
"__wmemory__: Function is only supported on Windows platforms");
364#if defined (__MINGW32__)
374#if defined (_MSC_VER)
389#if defined (OCTAVE_USE_WINDOWS_API)
390 std::size_t
len = name.length ();
391 bool candidate =
false;
392 if (
len > 1 && isalpha(name[0]) && name[1]==
':'
393 && (
len == 2 || (
len == 3 && name[2] ==
'\\')))
395 if (
len > 4 && name[0] ==
'\\' && name[1] ==
'\\')
398 std::size_t next_slash = name.find (
'\\', 3);
399 if (next_slash != std::string::npos &&
len > next_slash+1)
402 std::size_t last_slash = name.find (
'\\', next_slash+1);
403 if (last_slash == std::string::npos
404 || (
len > next_slash+2 && last_slash ==
len-1))
412 std::wstring wname = sys::u8_to_wstring (name);
414 = CreateFileW (wname.c_str (), FILE_READ_ATTRIBUTES,
415 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
416 nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS,
418 if (h != INVALID_HANDLE_VALUE)
429 octave_unused_parameter (name);
442#if defined (HAVE_OMP_GET_NUM_THREADS)
443 omp_get_num_threads ();
446#if defined (__386BSD__) || defined (__FreeBSD__) || defined (__NetBSD__)
448#elif defined (__MINGW32__)
450#elif defined (_MSC_VER)
458#if defined (OCTAVE_USE_WINDOWS_API)
460 SetProcessShutdownParameters (0x280, SHUTDOWN_NORETRY);
479 static bool curr_on =
false;
487 error (
"stdin is not a tty!");
493#if defined (HAVE_TERMIOS_H)
496 static struct termios save_term;
502 tcgetattr (tty_fd, &s);
513 s.c_lflag &= ~(ICANON |
ECHO | ECHOE | ECHOK | ECHONL);
514 s.c_oflag |= (OPOST | ONLCR);
516 s.c_oflag &= ~(OCRNL);
519 s.c_oflag &= ~(ONOCR);
522 s.c_oflag &= ~(ONLRET);
524 s.c_cc[VMIN] = (wait ? 1 : 0);
534 tcsetattr (tty_fd, wait ? TCSAFLUSH : TCSADRAIN, &s);
536#elif defined (HAVE_TERMIO_H)
539 static struct termio save_term;
545 ioctl (tty_fd, TCGETA, &s);
556 s.c_lflag &= ~(ICANON |
ECHO | ECHOE | ECHOK | ECHONL);
557 s.c_oflag |= (OPOST | ONLCR);
559 s.c_oflag &= ~(OCRNL);
562 s.c_oflag &= ~(ONOCR);
565 s.c_oflag &= ~(ONLRET);
567 s.c_cc[VMIN] = (wait ? 1 : 0);
576 ioctl (tty_fd, TCSETAW, &s);
578#elif defined (HAVE_SGTTY_H)
580 octave_unused_parameter (wait);
583 static struct sgttyb save_term;
589 ioctl (tty_fd, TIOCGETP, &s);
600 s.sg_flags |= CBREAK;
601 s.sg_flags &= ~(
ECHO);
610 ioctl (tty_fd, TIOCSETN, &s);
614 octave_unused_parameter (wait);
626popen (
const char *command,
const char *mode)
628#if defined (__MINGW32__) || defined (_MSC_VER)
629 std::wstring wcommand = sys::u8_to_wstring (command);
630 std::wstring wmode = sys::u8_to_wstring (mode);
633 if (wmode.length () < 2)
636 return _wpopen (wcommand.c_str (), wmode.c_str ());
638 return ::popen (command, mode);
645#if defined (__MINGW32__) || defined (_MSC_VER)
646 return ::_pclose (
f);
657#if defined (HAVE__KBHIT) && defined (HAVE__GETCH)
661 static constexpr int eof = std::istream::traits_type::eof ();
668 c = (! _kbhit ()) ? eof : _getch ();
682 int c = std::cin.get ();
684 if (std::cin.fail () || std::cin.eof ())
702#if defined (OCTAVE_USE_WINDOWS_API)
706#if defined (P_tmpdir)
714 if (retval.empty () || retval == R
"(\)")
716 retval = sys::env::getenv ("TEMP");
719 retval = sys::env::getenv (
"TMP");
722 retval = R
"(c:\temp)";
727#elif defined (P_tmpdir)
748 bool skip_redisplay =
true;
757DEFUN (getenv, args, ,
773 if (args.length () != 1)
776 std::string name = args(0).string_value ();
778 return ovl (sys::env::getenv (name));
803 if (args.length () != 1)
806 std::string name = args(0).xstring_value (
"isenv: VAR must be a string");
808 return ovl (sys::env::isenv (name));
821DEFUN (setenv, args, ,
836 int nargin = args.length ();
838 if (nargin < 1 || nargin > 2)
841 std::string var = args(0).xstring_value (
"setenv: VAR must be a string");
843 std::string val = (nargin == 2
844 ? args(1).xstring_value (
"setenv: VALUE must be a string")
847 sys::env::putenv (var, val);
862DEFUN (unsetenv, args, ,
873 if (args.length () != 1)
876 std::string tmp = args(0).string_value ();
878 return ovl (sys::unsetenv_wrapper (tmp));
885#if defined (OCTAVE_USE_WINDOWS_API)
888reg_close_key_wrapper (HKEY key)
897get_regkey_value (HKEY h_rootkey,
const std::string subkey,
903 std::wstring wsubkey = sys::u8_to_wstring (subkey);
904 result = RegOpenKeyExW (h_rootkey, wsubkey.c_str (), 0, KEY_READ,
907 if (result != ERROR_SUCCESS)
910 unwind_action restore_keys ([h_subkey] () { reg_close_key_wrapper (h_subkey); });
912 std::wstring wname = sys::u8_to_wstring (name);
914 result = RegQueryValueExW (h_subkey, wname.c_str (),
nullptr,
nullptr,
916 if (result != ERROR_SUCCESS)
921 result = RegQueryValueExW (h_subkey, wname.c_str (),
nullptr, &type,
923 if (result != ERROR_SUCCESS)
926 if (type == REG_DWORD)
927 value =
octave_int32 (*(
reinterpret_cast<DWORD *
> (data)));
928 else if (type == REG_SZ || type == REG_EXPAND_SZ)
931 wchar_t *dataw =
reinterpret_cast<wchar_t *
> (data);
932 DWORD lengthw = length /
sizeof (wchar_t);
933 std::wstring reg_string
934 = std::wstring (dataw, lengthw - (dataw[lengthw-1]==0));
942get_regkey_names (HKEY h_rootkey,
const std::string subkey,
943 std::list<std::string>& fields)
950 std::wstring wsubkey = sys::u8_to_wstring (subkey);
951 retval = RegOpenKeyExW (h_rootkey, wsubkey.c_str (), 0, KEY_READ,
953 if (retval != ERROR_SUCCESS)
957 const int MAX_VALUE_NAME_SIZE = 32766;
958 wchar_t value_name[MAX_VALUE_NAME_SIZE+1];
959 DWORD value_name_size = MAX_VALUE_NAME_SIZE;
963 retval = RegEnumValueW (h_subkey, idx, value_name, &value_name_size,
964 nullptr,
nullptr,
nullptr,
nullptr);
965 if (retval != ERROR_SUCCESS)
967 fields.push_back (sys::u8_from_wstring (value_name));
968 value_name_size = MAX_VALUE_NAME_SIZE;
972 if (retval == ERROR_NO_MORE_ITEMS)
973 retval = ERROR_SUCCESS;
975 RegCloseKey (h_subkey);
982DEFUN (winqueryreg, args, ,
1053#if defined (OCTAVE_USE_WINDOWS_API)
1054 if ((args.length () < 2) || (args.length () > 3))
1058 std::string rootkey_name
1059 = args(0).xstring_value (
"winqueryreg: the first argument must be 'name' "
1060 "or a valid ROOTKEY identifier");
1061 std::string subkey_name =
"";
1062 std::string value_name =
"";
1063 bool get_names =
false;
1064 if (rootkey_name.compare (
"name") == 0)
1066 if (args.length () < 3)
1067 error (
"winqueryreg: if the first argument is 'name', "
1068 "ROOTKEY and SUBKEY must be given");
1071 = args(1).xstring_value (
"winqueryreg: ROOTKEY must be a string");
1073 = args(2).xstring_value (
"winqueryreg: SUBKEY must be a string");
1078 = args(1).xstring_value (
"winqueryreg: SUBKEY must be a string");
1080 if (args.length () == 3)
1082 = args(2).xstring_value (
"winqueryreg: VALUENAME must be a string");
1087 if (rootkey_name ==
"HKEY_CLASSES_ROOT" || rootkey_name ==
"HKCR")
1088 h_rootkey = HKEY_CLASSES_ROOT;
1089 else if (rootkey_name ==
"HKEY_CURRENT_CONFIG")
1090 h_rootkey = HKEY_CURRENT_CONFIG;
1091 else if (rootkey_name ==
"HKEY_CURRENT_USER" || rootkey_name ==
"HKCU")
1092 h_rootkey = HKEY_CURRENT_USER;
1093 else if (rootkey_name ==
"HKEY_LOCAL_MACHINE" || rootkey_name ==
"HKLM")
1094 h_rootkey = HKEY_LOCAL_MACHINE;
1095 else if (rootkey_name ==
"HKEY_PERFORMANCE_DATA")
1096 h_rootkey = HKEY_PERFORMANCE_DATA;
1097 else if (rootkey_name ==
"HKEY_USERS" || rootkey_name ==
"HKU")
1098 h_rootkey = HKEY_USERS;
1100 error (
"winqueryreg: ROOTKEY is not a valid root key identifier");
1104 std::list<std::string> fields;
1106 LONG retval = get_regkey_names (h_rootkey, subkey_name, fields);
1107 if (retval != ERROR_SUCCESS)
1108 error (
"winqueryreg: error %ld reading names from registry", retval);
1112 for (
const auto& it : fields)
1113 fieldnames(i++) = it;
1115 return ovl (fieldnames);
1120 LONG retval = get_regkey_value (h_rootkey, subkey_name,
1121 value_name, key_val);
1122 if (retval == ERROR_FILE_NOT_FOUND)
1123 error (
"winqueryreg: no value found for '%s' at %s\\%s",
1124 value_name.c_str (), rootkey_name.c_str (),
1125 subkey_name.c_str ());
1126 if (retval != ERROR_SUCCESS)
1127 error (
"winqueryreg: error %ld reading the specified key", retval);
1129 return ovl (key_val);
1133 octave_unused_parameter (args);
1135 error (
"winqueryreg: function is only supported on Windows platforms");
1220 int c =
kbhit (args.length () == 0);
1225 char s[2] = {
static_cast<char> (c),
'\0' };
1231static bool Vpause_enabled =
true;
1287 int nargin = args.
length ();
1292 if (nargin == 1 && args(0).is_string ())
1294 bool saved_state = Vpause_enabled;
1295 std::string state = args(0).string_value ();
1298 Vpause_enabled =
true;
1299 else if (state ==
"off")
1300 Vpause_enabled =
false;
1301 else if (state ==
"query")
1304 error (R
"(pause: first argument must be "on", "off", or "query")");
1306 if (nargout > 0 || state ==
"query")
1307 retval.
append (saved_state ?
"on" :
"off");
1309 else if (Vpause_enabled)
1316 dval = args(0).xdouble_value (
"pause: N must be a scalar real value");
1318 if (math::isnan (dval))
1319 warning (
"pause: NaN is an invalid delay");
1350 mach_info::float_format flt_fmt = mach_info::native_float_format ();
1352 return ovl (flt_fmt == mach_info::flt_fmt_ieee_little_endian
1353 || flt_fmt == mach_info::flt_fmt_ieee_big_endian);
1366 mach_info::float_format flt_fmt = mach_info::native_float_format ();
1368 return ovl (mach_info::float_format_as_string (flt_fmt));
1403 if (args.length () != 1)
1411 sv = sys::file_ops::tilde_expand (sv);
1427DEFUN (get_home_directory, , ,
1439 return ovl (sys::env::get_home_directory ());
1449DEFUN (__blas_version__, , ,
1455 return ovl (sys::blas_version ());
1458DEFUN (__lapack_version__, , ,
1464 return ovl (sys::lapack_version ());
1467OCTAVE_END_NAMESPACE(octave)
static bool forced_interactive()
static bool prefer_env_winsize(bool)
static void clear_screen(bool skip_redisplay=false)
Vector representing the dimensions (size) of an Array.
void setfield(const std::string &key, const octave_value &val)
octave_value_list & append(const octave_value &val)
octave_idx_type length() const
string_vector xstring_vector_value(const char *fmt,...) const
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#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_value_list Fdrawnow(octave::interpreter &interp, const octave_value_list &args, int)
interpreter & __get_interpreter__()
F77_RET_T const F77_DBLE const F77_DBLE * f
float_format native_float_format()
octave_int< int32_t > octave_int32
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
interrupt_handler ignore_interrupts()
interrupt_handler set_interrupt_handler(const interrupt_handler &h, bool restart_syscalls)
FILE * popen(const char *command, const char *mode)
std::string get_P_tmpdir()
void raw_mode(bool on, bool wait)
void set_application_id()
bool drive_or_unc_share(const std::string &name)
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)