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
100 #include <shellapi.h>
106 #if defined (__386BSD__) || defined (__FreeBSD__) || defined (__NetBSD__)
111 # if defined (HAVE_FLOATINGPOINT_H)
113 # if ! defined (FP_X_DNML)
116 fpsetmask (~(FP_X_OFL|FP_X_INV|FP_X_DZ|FP_X_DNML|FP_X_UFL|FP_X_IMP));
122 #if defined (__MINGW32__) || defined (_MSC_VER)
125 w32_set_octave_home (
void)
129 HANDLE h = CreateToolhelp32Snapshot (TH32CS_SNAPMODULE
130 #
if defined (TH32CS_SNAPMODULE32)
131 | TH32CS_SNAPMODULE32
135 if (h != INVALID_HANDLE_VALUE)
137 MODULEENTRY32W mod_info;
139 ZeroMemory (&mod_info,
sizeof (mod_info));
140 mod_info.dwSize =
sizeof (mod_info);
142 if (Module32FirstW (h, &mod_info))
148 if (mod_name.find (
"octinterp") != std::string::npos)
156 while (Module32NextW (h, &mod_info));
164 size_t pos =
bin_dir.rfind (R
"(\bin\)");
166 if (pos != std::string::npos)
174 w32_set_octave_home ();
188 #if defined (__MINGW32__) || defined (_MSC_VER)
190 typedef HRESULT (WINAPI *SETCURRENTAPPID)(PCWSTR AppID);
192 HMODULE hShell = LoadLibrary (
"shell32.dll");
196 SETCURRENTAPPID pfnSetCurrentProcessExplicitAppUserModelID
197 =
reinterpret_cast<SETCURRENTAPPID
> (GetProcAddress (hShell,
"SetCurrentProcessExplicitAppUserModelID"));
199 if (pfnSetCurrentProcessExplicitAppUserModelID)
200 pfnSetCurrentProcessExplicitAppUserModelID (L
"gnu.octave." VERSION);
202 FreeLibrary (hShell);
209 DEFUN (__open_with_system_app__, args, ,
215 if (args.length () != 1)
218 std::string file = args(0).xstring_value (
"__open_with_system_app__: argument must be a filename");
220 #if defined (OCTAVE_USE_WINDOWS_API)
222 HINSTANCE status = ShellExecuteW (0, 0, wfile.c_str (), 0, 0, SW_SHOWNORMAL);
225 return octave_value (
reinterpret_cast<ptrdiff_t
> (status) > 32);
228 file =
"\"" + file +
"\"";
230 # if defined (__APPLE__)
231 # define FSYSTEM_OPEN_STR "open "
233 # define FSYSTEM_OPEN_STR "xdg-open "
239 # undef FSYSTEM_OPEN_STR
247 DEFUN (__is_elevated_process__, args, ,
257 #if defined (OCTAVE_USE_WINDOWS_API)
258 if (args.length () != 0)
262 HANDLE h_token =
nullptr;
264 if (OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &h_token))
266 TOKEN_ELEVATION elevation;
267 DWORD return_length =
sizeof (TOKEN_ELEVATION);
268 if (GetTokenInformation (h_token, TokenElevation, &elevation,
269 sizeof (elevation), &return_length))
270 retval = elevation.TokenIsElevated;
274 CloseHandle (h_token);
279 octave_unused_parameter (args);
280 error (
"__is_elevated_process__: "
281 "Function is only supported on Windows platforms.");
287 #if defined (__MINGW32__)
297 #if defined (_MSC_VER)
311 #if defined (OCTAVE_USE_WINDOWS_API)
320 const wchar_t *f1 = file1w.c_str ();
321 const wchar_t *f2 = file2w.c_str ();
323 bool f1_is_dir = GetFileAttributesW (f1) & FILE_ATTRIBUTE_DIRECTORY;
324 bool f2_is_dir = GetFileAttributesW (f2) & FILE_ATTRIBUTE_DIRECTORY;
329 DWORD share = FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE;
332 = CreateFileW (f1, 0, share, 0, OPEN_EXISTING,
333 f1_is_dir ? FILE_FLAG_BACKUP_SEMANTICS : 0, 0);
335 if (hfile1 != INVALID_HANDLE_VALUE)
338 = CreateFileW (f2, 0, share, 0, OPEN_EXISTING,
339 f2_is_dir ? FILE_FLAG_BACKUP_SEMANTICS : 0, 0);
341 if (hfile2 != INVALID_HANDLE_VALUE)
343 BY_HANDLE_FILE_INFORMATION hfi1;
344 BY_HANDLE_FILE_INFORMATION hfi2;
346 if (GetFileInformationByHandle (hfile1, &hfi1)
347 && GetFileInformationByHandle (hfile2, &hfi2))
349 retval = (hfi1.dwVolumeSerialNumber == hfi2.dwVolumeSerialNumber
350 && hfi1.nFileIndexHigh == hfi2.nFileIndexHigh
351 && hfi1.nFileIndexLow == hfi2.nFileIndexLow
352 && hfi1.nFileSizeHigh == hfi2.nFileSizeHigh
353 && hfi1.nFileSizeLow == hfi2.nFileSizeLow
354 && hfi1.ftLastWriteTime.dwLowDateTime
355 == hfi2.ftLastWriteTime.dwLowDateTime
356 && hfi1.ftLastWriteTime.dwHighDateTime
357 == hfi2.ftLastWriteTime.dwHighDateTime);
360 CloseHandle (hfile2);
363 CloseHandle (hfile1);
375 return (fs_file1 && fs_file2
376 && fs_file1.
ino () == fs_file2.
ino ()
377 && fs_file1.
dev () == fs_file2.
dev ());
386 #if defined (OCTAVE_USE_WINDOWS_API)
388 bool candidate =
false;
390 && (
len == 2 || (
len == 3 &&
name[2] ==
'\\')))
395 size_t next_slash =
name.find (
"\\", 3);
396 if (next_slash != std::string::npos &&
len > next_slash+1)
399 size_t last_slash =
name.find (
"\\", next_slash+1);
400 if (last_slash == std::string::npos
401 || (
len > next_slash+2 && last_slash ==
len-1))
411 = CreateFileW (wname.c_str (), FILE_READ_ATTRIBUTES,
412 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
413 nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS,
415 if (h != INVALID_HANDLE_VALUE)
426 octave_unused_parameter (
name);
438 #if defined (HAVE_OMP_GET_NUM_THREADS)
439 omp_get_num_threads ();
442 #if defined (__386BSD__) || defined (__FreeBSD__) || defined (__NetBSD__)
444 #elif defined (__MINGW32__)
446 #elif defined (_MSC_VER)
453 #if defined (OCTAVE_USE_WINDOWS_API)
455 SetProcessShutdownParameters (0x280, SHUTDOWN_NORETRY);
473 static bool curr_on =
false;
481 error (
"stdin is not a tty!");
487 #if defined (HAVE_TERMIOS_H)
490 static struct termios save_term;
496 tcgetattr (tty_fd, &s);
507 s.c_lflag &= ~(ICANON |
ECHO | ECHOE | ECHOK | ECHONL);
508 s.c_oflag |= (OPOST | ONLCR);
510 s.c_oflag &= ~(OCRNL);
513 s.c_oflag &= ~(ONOCR);
516 s.c_oflag &= ~(ONLRET);
518 s.c_cc[VMIN] = (wait ? 1 : 0);
528 tcsetattr (tty_fd, wait ? TCSAFLUSH : TCSADRAIN, &s);
530 #elif defined (HAVE_TERMIO_H)
533 static struct termio save_term;
539 ioctl (tty_fd, TCGETA, &s);
550 s.c_lflag &= ~(ICANON |
ECHO | ECHOE | ECHOK | ECHONL);
551 s.c_oflag |= (OPOST | ONLCR);
553 s.c_oflag &= ~(OCRNL);
556 s.c_oflag &= ~(ONOCR);
559 s.c_oflag &= ~(ONLRET);
561 s.c_cc[VMIN] = (wait ? 1 : 0);
570 ioctl (tty_fd, TCSETAW, &s);
572 #elif defined (HAVE_SGTTY_H)
574 octave_unused_parameter (wait);
577 static struct sgttyb save_term;
583 ioctl (tty_fd, TIOCGETP, &s);
594 s.sg_flags |= CBREAK;
595 s.sg_flags &= ~(
ECHO);
604 ioctl (tty_fd, TIOCSETN, &s);
608 octave_unused_parameter (wait);
619 FILE *
popen (
const char *command,
const char *mode)
621 #if defined (__MINGW32__) || defined (_MSC_VER)
626 frame.
add_fcn (::
free,
static_cast<void *
> (wcommand));
627 frame.
add_fcn (::
free,
static_cast<void *
> (wmode));
629 if (wmode && wmode[0] && ! wmode[1])
632 wchar_t tmode[3] = {wmode[0], L
'b', L
'\0'};
634 return _wpopen (wcommand, tmode);
637 return _wpopen (wcommand, wmode);
645 #if defined (__MINGW32__) || defined (_MSC_VER)
646 return ::_pclose (
f);
656 #if defined (HAVE__KBHIT) && defined (HAVE__GETCH)
660 static constexpr
int eof = std::istream::traits_type::eof ();
667 c = (! _kbhit ()) ? eof : _getch ();
681 int c = std::cin.get ();
683 if (std::cin.fail () || std::cin.eof ())
700 #if defined (OCTAVE_USE_WINDOWS_API)
704 #if defined (P_tmpdir)
725 #elif defined (P_tmpdir)
744 bool skip_redisplay =
true;
753 DEFUN (getenv, args, ,
769 if (args.length () != 1)
772 std::string
name = args(0).string_value ();
781 DEFUN (setenv, args, ,
793 int nargin = args.length ();
795 if (nargin < 1 || nargin > 2)
798 std::string var = args(0).xstring_value (
"setenv: VAR must be a string");
800 std::string val = (nargin == 2
801 ? args(1).xstring_value (
"setenv: VALUE must be a string")
819 DEFUN (unsetenv, args, ,
829 if (args.length () != 1)
832 std::string tmp = args(0).string_value ();
841 #if defined (OCTAVE_USE_WINDOWS_API)
846 reg_close_key_wrapper (HKEY key)
855 get_regkey_value (HKEY h_rootkey,
const std::string subkey,
862 result = RegOpenKeyExW (h_rootkey, wsubkey.c_str (), 0, KEY_READ,
865 if (result != ERROR_SUCCESS)
868 unwind_protect frame;
870 frame.add_fcn (reg_close_key_wrapper, h_subkey);
874 result = RegQueryValueExW (h_subkey, wname.c_str (),
nullptr,
nullptr,
876 if (result != ERROR_SUCCESS)
881 result = RegQueryValueExW (h_subkey, wname.c_str (),
nullptr, &
type,
883 if (result != ERROR_SUCCESS)
886 if (
type == REG_DWORD)
887 value =
octave_int32 (*(
reinterpret_cast<DWORD *
> (data)));
888 else if (
type == REG_SZ ||
type == REG_EXPAND_SZ)
895 get_regkey_names (HKEY h_rootkey,
const std::string subkey,
896 std::list<std::string> &fields)
904 retval = RegOpenKeyExW (h_rootkey, wsubkey.c_str (), 0, KEY_READ,
906 if (
retval != ERROR_SUCCESS)
910 const int MAX_VALUE_NAME_SIZE = 32766;
911 wchar_t value_name[MAX_VALUE_NAME_SIZE+1];
912 DWORD value_name_size = MAX_VALUE_NAME_SIZE;
916 retval = RegEnumValueW (h_subkey, idx, value_name, &value_name_size,
917 nullptr,
nullptr,
nullptr,
nullptr);
918 if (
retval != ERROR_SUCCESS)
921 value_name_size = MAX_VALUE_NAME_SIZE;
925 if (
retval == ERROR_NO_MORE_ITEMS)
928 RegCloseKey (h_subkey);
936 DEFUN (winqueryreg, args, ,
1006 #if defined (OCTAVE_USE_WINDOWS_API)
1007 if ((args.length () < 2) || (args.length () > 3))
1011 std::string rootkey_name
1012 = args(0).xstring_value (
"winqueryreg: the first argument must be 'name' "
1013 "or a valid ROOTKEY identifier");
1014 std::string subkey_name =
"";
1015 std::string value_name =
"";
1016 bool get_names =
false;
1017 if (rootkey_name.compare (
"name") == 0)
1019 if (args.length () < 3)
1020 error (
"winqueryreg: if the first argument is 'name', "
1021 "ROOTKEY and SUBKEY must be given");
1024 = args(1).xstring_value (
"winqueryreg: ROOTKEY must be a string");
1026 = args(2).xstring_value (
"winqueryreg: SUBKEY must be a string");
1031 = args(1).xstring_value (
"winqueryreg: SUBKEY must be a string");
1033 if (args.length () == 3)
1035 = args(2).xstring_value (
"winqueryreg: VALUENAME must be a string");
1040 if (rootkey_name ==
"HKEY_CLASSES_ROOT" || rootkey_name ==
"HKCR")
1041 h_rootkey = HKEY_CLASSES_ROOT;
1042 else if (rootkey_name ==
"HKEY_CURRENT_CONFIG")
1043 h_rootkey = HKEY_CURRENT_CONFIG;
1044 else if (rootkey_name ==
"HKEY_CURRENT_USER" || rootkey_name ==
"HKCU")
1045 h_rootkey = HKEY_CURRENT_USER;
1046 else if (rootkey_name ==
"HKEY_LOCAL_MACHINE" || rootkey_name ==
"HKLM")
1047 h_rootkey = HKEY_LOCAL_MACHINE;
1048 else if (rootkey_name ==
"HKEY_PERFORMANCE_DATA")
1049 h_rootkey = HKEY_PERFORMANCE_DATA;
1050 else if (rootkey_name ==
"HKEY_USERS" || rootkey_name ==
"HKU")
1051 h_rootkey = HKEY_USERS;
1053 error (
"winqueryreg: ROOTKEY is not a valid root key identifier");
1057 std::list<std::string> fields;
1059 LONG
retval = octave::get_regkey_names (h_rootkey, subkey_name, fields);
1060 if (
retval != ERROR_SUCCESS)
1061 error (
"winqueryreg: error %ld reading names from registry",
retval);
1065 std::list<std::string>::const_iterator it;
1066 for (i = 0, it = fields.begin (); it != fields.end (); ++it, ++i)
1067 fieldnames(i) = *it;
1069 return ovl (fieldnames);
1074 LONG
retval = octave::get_regkey_value (h_rootkey, subkey_name,
1075 value_name, key_val);
1076 if (
retval == ERROR_FILE_NOT_FOUND)
1077 error (
"winqueryreg: no value found for '%s' at %s\\%s.",
1078 value_name.c_str (), rootkey_name.c_str (),
1079 subkey_name.c_str ());
1080 if (
retval != ERROR_SUCCESS)
1081 error (
"winqueryreg: error %ld reading the specified key",
retval);
1083 return ovl (key_val);
1087 octave_unused_parameter (args);
1089 error (
"winqueryreg: function is only supported on Windows platforms");
1177 char s[2] = {
static_cast<char> (c),
'\0' };
1185 DEFMETHOD (pause, interp, args, nargout,
1239 int nargin = args.length ();
1244 if (nargin == 1 && args(0).is_string ())
1247 std::string
state = args(0).string_value ();
1251 else if (
state ==
"off")
1253 else if (
state ==
"query")
1256 error (
"pause: first argument must be \"on\", \"off\" or \"query\"");
1258 if (nargout > 0 ||
state ==
"query")
1259 retval.append (saved_state ?
"on" :
"off");
1268 dval = args(0).xdouble_value (
"pause: N must be a scalar real value");
1271 warning (
"pause: NaN is an invalid delay");
1357 if (args.length () != 1)
1380 DEFUN (get_home_directory, , ,
1402 DEFUN (__blas_version__, , ,
1411 DEFUN (__lapack_version__, , ,
Vector representing the dimensions (size) of an Array.
void add_fcn(void(*fcn)(Params...), Args &&... args)
static bool forced_interactive(void)
static void clear_screen(bool skip_redisplay=false)
static bool prefer_env_winsize(bool)
bool interactive(void) const
static std::string getenv(const std::string &name)
static std::string get_home_directory(void)
static void putenv(const std::string &name, const std::string &value)
string_vector xstring_vector_value(const char *fmt,...) const
bool iscellstr(void) 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)
std::string bin_dir(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
std::string tilde_expand(const std::string &name)
std::string blas_version(void)
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)
void set_application_id(void)
bool drive_or_unc_share(const std::string &name)
static uint32_t state[624]
interrupt_handler set_interrupt_handler(const volatile interrupt_handler &h, bool restart_syscalls)
static double f(double k, double l_nu, double c_pm)
interpreter & __get_interpreter__(const std::string &who)
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 sleep(double seconds, bool do_graphics_events)
void sysdep_cleanup(void)
std::string get_P_tmpdir(void)
void raw_mode(bool on, bool wait)
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::octave_value(const Array< char > &chm, char type) return retval
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
static bool Vpause_enabled
OCTAVE_EXPORT octave_value_list Fsystem(const octave_value_list &args, int nargout)
wchar_t * u8_to_wchar(const char *u8)
int octave_isatty_wrapper(int fd)