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
110#if defined (__386BSD__) || defined (__FreeBSD__) || defined (__NetBSD__)
115# if defined (HAVE_FLOATINGPOINT_H)
117# if ! defined (FP_X_DNML)
120 fpsetmask (~(FP_X_OFL|FP_X_INV|FP_X_DZ|FP_X_DNML|FP_X_UFL|FP_X_IMP));
126#if defined (__MINGW32__) || defined (_MSC_VER)
129w32_set_octave_home ()
133 HANDLE h = CreateToolhelp32Snapshot (TH32CS_SNAPMODULE
134#
if defined (TH32CS_SNAPMODULE32)
135 | TH32CS_SNAPMODULE32
139 if (h != INVALID_HANDLE_VALUE)
141 MODULEENTRY32W mod_info;
143 ZeroMemory (&mod_info,
sizeof (mod_info));
144 mod_info.dwSize =
sizeof (mod_info);
146 if (Module32FirstW (h, &mod_info))
150 std::string mod_name (sys::u8_from_wstring (mod_info.szModule));
152 if (mod_name.find (
"octinterp") != std::string::npos)
154 bin_dir = sys::u8_from_wstring (mod_info.szExePath);
160 while (Module32NextW (h, &mod_info));
168 std::size_t pos =
bin_dir.rfind (R
"(\bin\)");
170 if (pos != std::string::npos)
171 sys::env::putenv (
"OCTAVE_HOME",
bin_dir.substr (0, pos));
178 w32_set_octave_home ();
188#if defined (__MINGW32__) || defined (_MSC_VER)
190 SetCurrentProcessExplicitAppUserModelID (L
"gnu.octave." VERSION);
195DEFUN (__open_with_system_app__, args, ,
201 if (args.length () != 1)
204 std::string file = args(0).xstring_value (
"__open_with_system_app__: argument must be a filename");
206#if defined (OCTAVE_USE_WINDOWS_API)
207 std::wstring wfile = sys::u8_to_wstring (file);
208 HINSTANCE status = ShellExecuteW (0, 0, wfile.c_str (), 0, 0, SW_SHOWNORMAL);
211 return octave_value (
reinterpret_cast<std::ptrdiff_t
> (status) > 32);
214 file =
'"' + file +
'"';
216# if defined (__APPLE__)
217# define FSYSTEM_OPEN_STR "open "
219# define FSYSTEM_OPEN_STR "xdg-open "
225# undef FSYSTEM_OPEN_STR
233DEFUN (__is_elevated_process__, args, ,
243#if defined (OCTAVE_USE_WINDOWS_API)
244 if (args.length () != 0)
248 HANDLE h_token =
nullptr;
250 if (OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &h_token))
252 TOKEN_ELEVATION elevation;
253 DWORD return_length =
sizeof (TOKEN_ELEVATION);
254 if (GetTokenInformation (h_token, TokenElevation, &elevation,
255 sizeof (elevation), &return_length))
256 retval = elevation.TokenIsElevated;
260 CloseHandle (h_token);
265 octave_unused_parameter (args);
266 error (
"__is_elevated_process__: "
267 "Function is only supported on Windows platforms.");
271DEFUN (__wmemory__, args, ,
279#if defined (OCTAVE_USE_WINDOWS_API)
280 if (args.length () != 0)
286 HANDLE h_proc = GetCurrentProcess ();
287 if (h_proc ==
nullptr)
288 error (
"__wmemory__: Couldn't open handle to own process");
290 PROCESS_MEMORY_COUNTERS proc_mem_count;
291 if (GetProcessMemoryInfo (h_proc, &proc_mem_count,
sizeof (proc_mem_count)))
293 proc_struct.
setfield (
"PageFaultCount",
294 proc_mem_count.PageFaultCount);
295 proc_struct.
setfield (
"PeakWorkingSetSize",
296 proc_mem_count.PeakWorkingSetSize);
297 proc_struct.
setfield (
"WorkingSetSize",
298 proc_mem_count.WorkingSetSize);
299 proc_struct.
setfield (
"QuotaPeakPagedPoolUsage",
300 proc_mem_count.QuotaPeakPagedPoolUsage);
301 proc_struct.
setfield (
"QuotaPagedPoolUsage",
302 proc_mem_count.QuotaPagedPoolUsage);
303 proc_struct.
setfield (
"QuotaPeakNonPagedPoolUsage",
304 proc_mem_count.QuotaPeakNonPagedPoolUsage);
305 proc_struct.
setfield (
"QuotaNonPagedPoolUsage",
306 proc_mem_count.QuotaNonPagedPoolUsage);
307 proc_struct.
setfield (
"PagefileUsage",
308 proc_mem_count.PagefileUsage);
309 proc_struct.
setfield (
"PeakPagefileUsage",
310 proc_mem_count.PeakPagefileUsage);
314 proc_struct.
setfield (
"PageFaultCount", 0);
315 proc_struct.
setfield (
"PeakWorkingSetSize", 0);
316 proc_struct.
setfield (
"WorkingSetSize", 0);
317 proc_struct.
setfield (
"QuotaPeakPagedPoolUsage", 0);
318 proc_struct.
setfield (
"QuotaPagedPoolUsage", 0);
319 proc_struct.
setfield (
"QuotaPeakNonPagedPoolUsage", 0);
320 proc_struct.
setfield (
"QuotaNonPagedPoolUsage", 0);
321 proc_struct.
setfield (
"PagefileUsage", 0);
322 proc_struct.
setfield (
"PeakPagefileUsage", 0);
325 CloseHandle (h_proc);
330 MEMORYSTATUSEX mem_stat;
332 mem_stat.dwLength =
sizeof (mem_stat);
334 if (GlobalMemoryStatusEx (&mem_stat))
336 sys_struct.
setfield (
"MemoryLoad", mem_stat.dwMemoryLoad);
337 sys_struct.
setfield (
"TotalPhys", mem_stat.ullTotalPhys);
338 sys_struct.
setfield (
"AvailPhys", mem_stat.ullAvailPhys);
339 sys_struct.
setfield (
"TotalPageFile", mem_stat.ullTotalPageFile);
340 sys_struct.
setfield (
"AvailPageFile", mem_stat.ullAvailPageFile);
341 sys_struct.
setfield (
"TotalVirtual", mem_stat.ullTotalVirtual);
342 sys_struct.
setfield (
"AvailVirtual", mem_stat.ullAvailVirtual);
343 sys_struct.
setfield (
"AvailExtendedVirtual",
344 mem_stat.ullAvailExtendedVirtual);
348 sys_struct.
setfield (
"MemoryLoad", 0);
349 sys_struct.
setfield (
"TotalPhys", 0);
350 sys_struct.
setfield (
"AvailPhys", 0);
351 sys_struct.
setfield (
"TotalPageFile", 0);
352 sys_struct.
setfield (
"AvailPageFile", 0);
353 sys_struct.
setfield (
"TotalVirtual", 0);
354 sys_struct.
setfield (
"AvailVirtual", 0);
355 sys_struct.
setfield (
"AvailExtendedVirtual", 0);
358 return ovl (proc_struct, sys_struct);
361 octave_unused_parameter (args);
362 error (
"__wmemory__: Function is only supported on Windows platforms");
366#if defined (__MINGW32__)
376#if defined (_MSC_VER)
391#if defined (OCTAVE_USE_WINDOWS_API)
392 std::size_t
len = name.length ();
393 bool candidate =
false;
394 if (
len > 1 && isalpha(name[0]) && name[1]==
':'
395 && (
len == 2 || (
len == 3 && name[2] ==
'\\')))
397 if (
len > 4 && name[0] ==
'\\' && name[1] ==
'\\')
400 std::size_t next_slash = name.find (
'\\', 3);
401 if (next_slash != std::string::npos &&
len > next_slash+1)
404 std::size_t last_slash = name.find (
'\\', next_slash+1);
405 if (last_slash == std::string::npos
406 || (
len > next_slash+2 && last_slash ==
len-1))
414 std::wstring wname = sys::u8_to_wstring (name);
416 = CreateFileW (wname.c_str (), FILE_READ_ATTRIBUTES,
417 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
418 nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS,
420 if (h != INVALID_HANDLE_VALUE)
431 octave_unused_parameter (name);
441 static bool initialized =
false;
449#if defined (HAVE_OMP_GET_NUM_THREADS)
450 omp_get_num_threads ();
453#if defined (__386BSD__) || defined (__FreeBSD__) || defined (__NetBSD__)
455#elif defined (__MINGW32__)
457#elif defined (_MSC_VER)
467#if defined (OCTAVE_USE_WINDOWS_API)
469 SetProcessShutdownParameters (0x280, SHUTDOWN_NORETRY);
488 static bool curr_on =
false;
496 error (
"stdin is not a tty!");
502#if defined (HAVE_TERMIOS_H)
505 static struct termios save_term;
511 tcgetattr (tty_fd, &s);
522 s.c_lflag &= ~(ICANON |
ECHO | ECHOE | ECHOK | ECHONL);
523 s.c_oflag |= (OPOST | ONLCR);
525 s.c_oflag &= ~(OCRNL);
528 s.c_oflag &= ~(ONOCR);
531 s.c_oflag &= ~(ONLRET);
533 s.c_cc[VMIN] = (wait ? 1 : 0);
543 tcsetattr (tty_fd, wait ? TCSAFLUSH : TCSADRAIN, &s);
545#elif defined (HAVE_TERMIO_H)
548 static struct termio save_term;
554 ioctl (tty_fd, TCGETA, &s);
565 s.c_lflag &= ~(ICANON |
ECHO | ECHOE | ECHOK | ECHONL);
566 s.c_oflag |= (OPOST | ONLCR);
568 s.c_oflag &= ~(OCRNL);
571 s.c_oflag &= ~(ONOCR);
574 s.c_oflag &= ~(ONLRET);
576 s.c_cc[VMIN] = (wait ? 1 : 0);
585 ioctl (tty_fd, TCSETAW, &s);
587#elif defined (HAVE_SGTTY_H)
589 octave_unused_parameter (wait);
592 static struct sgttyb save_term;
598 ioctl (tty_fd, TIOCGETP, &s);
609 s.sg_flags |= CBREAK;
610 s.sg_flags &= ~(
ECHO);
619 ioctl (tty_fd, TIOCSETN, &s);
623 octave_unused_parameter (wait);
634#if defined (OCTAVE_USE_WINDOWS_API)
636std::map<FILE *, HANDLE> pipe_handle_map;
640popen (
const char *command,
const char *mode)
642#if defined (OCTAVE_USE_WINDOWS_API)
647 bool is_write = (mode[0] ==
'w');
649 bool is_binary = ((
strlen (mode) == 1) || mode[1] ==
'b');
653 SECURITY_ATTRIBUTES sa {};
654 sa.nLength =
sizeof (sa);
655 sa.bInheritHandle = TRUE;
657 if (! CreatePipe (&h_read, &h_write, &sa, 0))
660 HANDLE parent_end = is_write ? h_write : h_read;
661 HANDLE child_end = is_write ? h_read : h_write;
664 SetHandleInformation (parent_end, HANDLE_FLAG_INHERIT, 0);
669 si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
670 si.wShowWindow = SW_HIDE;
672 si.hStdInput = child_end;
675 si.hStdOutput = child_end;
676 si.hStdError = child_end;
678 PROCESS_INFORMATION pi {};
680 std::wstring wcommand {L
"cmd.exe /C \""};
681 wcommand.append (sys::u8_to_wstring (command));
682 wcommand.append (L
"\"");
683 BOOL ok = CreateProcessW (
nullptr, &wcommand[0],
nullptr,
nullptr, TRUE,
684 CREATE_NEW_CONSOLE,
nullptr,
nullptr, &si, &pi);
687 CloseHandle (h_read);
688 CloseHandle (h_write);
692 CloseHandle (pi.hThread);
695 CloseHandle (child_end);
698 int flags = (is_write ? _O_WRONLY : _O_RDONLY);
702 int fd = _open_osfhandle (
reinterpret_cast<intptr_t
> (parent_end), flags);
706 CloseHandle (parent_end);
707 CloseHandle (pi.hProcess);
711 FILE *
f = _fdopen (fd, mode);
715 CloseHandle (pi.hProcess);
719 pipe_handle_map[
f] = pi.hProcess;
724 return ::popen (command, mode);
731#if defined (OCTAVE_USE_WINDOWS_API)
732 auto it = pipe_handle_map.find (
f);
733 if (it != pipe_handle_map.end ())
735 HANDLE h_process = it->second;
736 pipe_handle_map.erase (it);
742 WaitForSingleObject (h_process, INFINITE);
744 if (! GetExitCodeProcess (h_process, &exitcode))
748 CloseHandle (h_process);
750 return static_cast<int> (exitcode);
766#if defined (HAVE__KBHIT) && defined (HAVE__GETCH)
770 static constexpr int eof = std::istream::traits_type::eof ();
777 c = (! _kbhit ()) ? eof : _getch ();
791 int c = std::cin.get ();
793 if (std::cin.fail () || std::cin.eof ())
811#if defined (OCTAVE_USE_WINDOWS_API)
815#if defined (P_tmpdir)
823 if (retval.empty () || retval == R
"(\)")
825 retval = sys::env::getenv ("TEMP");
828 retval = sys::env::getenv (
"TMP");
831 retval = R
"(c:\temp)";
836#elif defined (P_tmpdir)
857 bool skip_redisplay =
true;
866DEFUN (getenv, args, ,
882 if (args.length () != 1)
885 std::string name = args(0).string_value ();
887 return ovl (sys::env::getenv (name));
912 if (args.length () != 1)
915 std::string name = args(0).xstring_value (
"isenv: VAR must be a string");
917 return ovl (sys::env::isenv (name));
930DEFUN (setenv, args, ,
945 int nargin = args.length ();
947 if (nargin < 1 || nargin > 2)
950 std::string var = args(0).xstring_value (
"setenv: VAR must be a string");
952 std::string val = (nargin == 2
953 ? args(1).xstring_value (
"setenv: VALUE must be a string")
956 sys::env::putenv (var, val);
971DEFUN (unsetenv, args, ,
982 if (args.length () != 1)
985 std::string tmp = args(0).string_value ();
987 return ovl (sys::unsetenv_wrapper (tmp));
994#if defined (OCTAVE_USE_WINDOWS_API)
997reg_close_key_wrapper (HKEY key)
1006get_regkey_value (HKEY h_rootkey,
const std::string subkey,
1012 std::wstring wsubkey = sys::u8_to_wstring (subkey);
1013 result = RegOpenKeyExW (h_rootkey, wsubkey.c_str (), 0, KEY_READ,
1016 if (result != ERROR_SUCCESS)
1019 unwind_action restore_keys ([h_subkey] () { reg_close_key_wrapper (h_subkey); });
1021 std::wstring wname = sys::u8_to_wstring (name);
1023 result = RegQueryValueExW (h_subkey, wname.c_str (),
nullptr,
nullptr,
1025 if (result != ERROR_SUCCESS)
1030 result = RegQueryValueExW (h_subkey, wname.c_str (),
nullptr, &type,
1032 if (result != ERROR_SUCCESS)
1035 if (type == REG_DWORD)
1036 value =
octave_int32 (*(
reinterpret_cast<DWORD *
> (data)));
1037 else if (type == REG_SZ || type == REG_EXPAND_SZ)
1040 wchar_t *dataw =
reinterpret_cast<wchar_t *
> (data);
1041 DWORD lengthw = length /
sizeof (wchar_t);
1042 std::wstring reg_string
1043 = std::wstring (dataw, lengthw - (dataw[lengthw-1]==0));
1051get_regkey_names (HKEY h_rootkey,
const std::string subkey,
1052 std::list<std::string>& fields)
1059 std::wstring wsubkey = sys::u8_to_wstring (subkey);
1060 retval = RegOpenKeyExW (h_rootkey, wsubkey.c_str (), 0, KEY_READ,
1062 if (retval != ERROR_SUCCESS)
1066 const int MAX_VALUE_NAME_SIZE = 32766;
1067 wchar_t value_name[MAX_VALUE_NAME_SIZE+1];
1068 DWORD value_name_size = MAX_VALUE_NAME_SIZE;
1072 retval = RegEnumValueW (h_subkey, idx, value_name, &value_name_size,
1073 nullptr,
nullptr,
nullptr,
nullptr);
1074 if (retval != ERROR_SUCCESS)
1076 fields.push_back (sys::u8_from_wstring (value_name));
1077 value_name_size = MAX_VALUE_NAME_SIZE;
1081 if (retval == ERROR_NO_MORE_ITEMS)
1082 retval = ERROR_SUCCESS;
1084 RegCloseKey (h_subkey);
1091DEFUN (winqueryreg, args, ,
1162#if defined (OCTAVE_USE_WINDOWS_API)
1163 if ((args.length () < 2) || (args.length () > 3))
1167 std::string rootkey_name
1168 = args(0).xstring_value (
"winqueryreg: the first argument must be 'name' "
1169 "or a valid ROOTKEY identifier");
1170 std::string subkey_name =
"";
1171 std::string value_name =
"";
1172 bool get_names =
false;
1173 if (rootkey_name.compare (
"name") == 0)
1175 if (args.length () < 3)
1176 error (
"winqueryreg: if the first argument is 'name', "
1177 "ROOTKEY and SUBKEY must be given");
1180 = args(1).xstring_value (
"winqueryreg: ROOTKEY must be a string");
1182 = args(2).xstring_value (
"winqueryreg: SUBKEY must be a string");
1187 = args(1).xstring_value (
"winqueryreg: SUBKEY must be a string");
1189 if (args.length () == 3)
1191 = args(2).xstring_value (
"winqueryreg: VALUENAME must be a string");
1196 if (rootkey_name ==
"HKEY_CLASSES_ROOT" || rootkey_name ==
"HKCR")
1197 h_rootkey = HKEY_CLASSES_ROOT;
1198 else if (rootkey_name ==
"HKEY_CURRENT_CONFIG")
1199 h_rootkey = HKEY_CURRENT_CONFIG;
1200 else if (rootkey_name ==
"HKEY_CURRENT_USER" || rootkey_name ==
"HKCU")
1201 h_rootkey = HKEY_CURRENT_USER;
1202 else if (rootkey_name ==
"HKEY_LOCAL_MACHINE" || rootkey_name ==
"HKLM")
1203 h_rootkey = HKEY_LOCAL_MACHINE;
1204 else if (rootkey_name ==
"HKEY_PERFORMANCE_DATA")
1205 h_rootkey = HKEY_PERFORMANCE_DATA;
1206 else if (rootkey_name ==
"HKEY_USERS" || rootkey_name ==
"HKU")
1207 h_rootkey = HKEY_USERS;
1209 error (
"winqueryreg: ROOTKEY is not a valid root key identifier");
1213 std::list<std::string> fields;
1215 LONG retval = get_regkey_names (h_rootkey, subkey_name, fields);
1216 if (retval != ERROR_SUCCESS)
1217 error (
"winqueryreg: error %ld reading names from registry", retval);
1221 for (
const auto& it : fields)
1222 fieldnames(i++) = it;
1224 return ovl (fieldnames);
1229 LONG retval = get_regkey_value (h_rootkey, subkey_name,
1230 value_name, key_val);
1231 if (retval == ERROR_FILE_NOT_FOUND)
1232 error (
"winqueryreg: no value found for '%s' at %s\\%s",
1233 value_name.c_str (), rootkey_name.c_str (),
1234 subkey_name.c_str ());
1235 if (retval != ERROR_SUCCESS)
1236 error (
"winqueryreg: error %ld reading the specified key", retval);
1238 return ovl (key_val);
1242 octave_unused_parameter (args);
1244 error (
"winqueryreg: function is only supported on Windows platforms");
1329 int c =
kbhit (args.length () == 0);
1334 char s[2] = {
static_cast<char> (c),
'\0' };
1340static bool Vpause_enabled =
true;
1396 int nargin = args.
length ();
1401 if (nargin == 1 && args(0).is_string ())
1403 bool saved_state = Vpause_enabled;
1404 std::string state = args(0).string_value ();
1407 Vpause_enabled =
true;
1408 else if (state ==
"off")
1409 Vpause_enabled =
false;
1410 else if (state ==
"query")
1413 error (R
"(pause: first argument must be "on", "off", or "query")");
1415 if (nargout > 0 || state ==
"query")
1416 retval.
append (saved_state ?
"on" :
"off");
1418 else if (Vpause_enabled)
1425 dval = args(0).xdouble_value (
"pause: N must be a scalar real value");
1427 if (math::isnan (dval))
1428 warning (
"pause: NaN is an invalid delay");
1459 mach_info::float_format flt_fmt = mach_info::native_float_format ();
1461 return ovl (flt_fmt == mach_info::flt_fmt_ieee_little_endian
1462 || flt_fmt == mach_info::flt_fmt_ieee_big_endian);
1475 mach_info::float_format flt_fmt = mach_info::native_float_format ();
1477 return ovl (mach_info::float_format_as_string (flt_fmt));
1512 if (args.length () != 1)
1520 sv = sys::file_ops::tilde_expand (sv);
1536DEFUN (get_home_directory, , ,
1548 return ovl (sys::env::get_home_directory ());
1566DEFUN (__blas_version__, , ,
1572 return ovl (sys::blas_version ());
1575DEFUN (__lapack_version__, , ,
1581 return ovl (sys::lapack_version ());
1584OCTAVE_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__()
float_format native_float_format()
octave_int< int32_t > octave_int32
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
T::size_type strlen(const typename T::value_type *str)
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)
F77_RET_T const F77_DBLE const F77_DBLE * f
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)