26 #if defined (HAVE_CONFIG_H)
77 const std::string& warn_for,
bool warn)
81 if (! fcn_name.empty ())
93 error (
"%s: the symbol '%s' is not valid as a function",
94 warn_for.c_str (), fcn_name.c_str ());
101 const std::string& warn_for,
bool warn)
105 std::string fcn_name;
115 error (
"%s: argument must be a string containing function name",
140 if (args.length () != 1)
143 std::string name = args(0).xstring_value (
"isglobal: NAME must be a string");
145 return ovl (interp.isglobal (name));
161 const std::string& type =
"any")
166 bool search_any = type ==
"any";
167 bool search_var = type ==
"var";
168 bool search_dir = type ==
"dir";
169 bool search_file = type ==
"file";
170 bool search_builtin = type ==
"builtin";
171 bool search_class = type ==
"class";
173 if (! (search_any || search_var || search_dir || search_file
174 || search_builtin || search_class))
175 error (R
"(exist: unrecognized type argument "%s")", type.c_str ());
177 if (search_any || search_var)
196 if (search_any || search_file || search_dir || search_class)
198 bool have_fcn_ext =
false;
200 std::string xname = name;
203 std::size_t pos = name.rfind (
'.');
205 if (pos != std::string::npos)
207 ext = name.substr (pos+1);
209 if (ext ==
"m" || ext ==
"oct" || ext ==
"mex")
211 xname = name.substr (0, pos);
216 std::string file_name;
218 if (search_any || search_file || search_class)
225 if (have_fcn_ext && ! file_name.empty ())
228 pos = file_name.rfind (
'.');
230 if (pos != std::string::npos)
232 std::string fext = file_name.substr (pos+1);
239 if (search_any && file_name.empty ())
249 if (! have_fcn_ext && file_name.empty ())
257 if (file_name.empty ())
261 std::size_t
len = file_name.length ();
263 if (
len > 0 && (search_any || search_file || search_class))
265 if (search_any || search_file)
267 if (
len > 4 && (file_name.substr (
len-4) ==
".oct"
268 || file_name.substr (
len-4) ==
".mex"))
287 if (file_name.empty ())
294 sys::file_stat fs (file_name);
298 if (search_any || search_file)
303 len = file_name.length ();
305 if (
len > 4 && (file_name.substr (
len-4) ==
".oct"
306 || file_name.substr (
len-4) ==
".mex"))
311 else if (search_dir && fs.is_dir ())
315 if (search_file || search_dir)
319 if ((search_any || search_builtin)
331 return octave::symbol_exist (interp, name, type);
335 #define GET_IDX(LEN) \
336 static_cast<int> (((LEN)-1) * static_cast<double> (rand ()) / RAND_MAX)
341 static const std::string alpha
342 =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
344 static std::size_t
len = alpha.length ();
346 std::string nm = basename + alpha[
GET_IDX (
len)];
348 std::size_t pos = nm.length ();
350 if (nm.substr (0, 2) ==
"__")
434 int nargin = args.length ();
436 if (nargin < 1 || nargin > 2)
441 if (args(0).builtin_type () !=
btyp_unknown && args(0).isempty ())
446 if (nargin == 2 && args(1).builtin_type () !=
btyp_unknown
447 && args(1).isempty ())
450 std::string name = args(0).xstring_value (
"exist: NAME must be a string");
455 = args(1).xstring_value (
"exist: TYPE must be a string");
549 if (! args(1).is_string () || args(1).string_value () !=
"local")
559 static octave::unwind_protect *
560 curr_fcn_unwind_protect_frame ()
564 return tw.curr_fcn_unwind_protect_frame ();
567 template <
typename T>
569 try_local_protect (T& var)
571 octave::unwind_protect *frame = curr_fcn_unwind_protect_frame ();
575 frame->protect_var (var);
584 int nargout,
const char *nm)
588 int nargin = args.
length ();
590 if (nargout > 0 || nargin == 0)
593 if (wants_local_change (args, nargin))
595 if (! try_local_protect (var))
596 warning (R
"("local" has no effect outside a function)");
604 bool bval = args(0).xbool_value (
"%s: argument must be a logical value", nm);
614 int nargout,
const char *nm)
618 int nargin = args.
length ();
620 if (nargout > 0 || nargin == 0)
623 if (wants_local_change (args, nargin))
625 if (! try_local_protect (var))
626 warning (R
"("local" has no effect outside a function)");
634 std::string sval = args(0).xstring_value (
"%s: argument must be a single character", nm);
636 switch (sval.length ())
647 error (
"%s: argument must be a single character", nm);
657 int nargout,
const char *nm,
658 int minval,
int maxval)
662 int nargin = args.
length ();
664 if (nargout > 0 || nargin == 0)
667 if (wants_local_change (args, nargin))
669 if (! try_local_protect (var))
670 warning (R
"("local" has no effect outside a function)");
678 int ival = args(0).xint_value (
"%s: argument must be an integer value", nm);
681 error (
"%s: arg must be greater than %d", nm, minval);
683 error (
"%s: arg must be less than or equal to %d", nm, maxval);
693 int nargout,
const char *nm,
694 double minval,
double maxval)
698 int nargin = args.
length ();
700 if (nargout > 0 || nargin == 0)
703 if (wants_local_change (args, nargin))
705 if (! try_local_protect (var))
706 warning (R
"("local" has no effect outside a function)");
714 double dval = args(0).xscalar_value (
"%s: argument must be a scalar value", nm);
717 error (
"%s: argument must be greater than %g", nm, minval);
719 error (
"%s: argument must be less than or equal to %g", nm, maxval);
729 int nargout,
const char *nm,
bool empty_ok)
733 int nargin = args.
length ();
735 if (nargout > 0 || nargin == 0)
738 if (wants_local_change (args, nargin))
740 if (! try_local_protect (var))
741 warning (R
"("local" has no effect outside a function)");
749 std::string sval = args(0).xstring_value (
"%s: first argument must be a string", nm);
751 if (! empty_ok && sval.empty ())
752 error (
"%s: value must not be empty", nm);
762 int nargout,
const char *nm,
const char **choices)
766 while (choices[nchoices] !=
nullptr)
769 int nargin = args.
length ();
773 if (nargout > 0 || nargin == 0)
774 retval = choices[var];
776 if (wants_local_change (args, nargin))
778 if (! try_local_protect (var))
779 warning (R
"("local" has no effect outside a function)");
787 std::string sval = args(0).xstring_value (
"%s: first argument must be a string", nm);
790 for (; i < nchoices; i++)
792 if (sval == choices[i])
799 error (R
"(%s: value not allowed ("%s"))", nm, sval.c_str ());
807 int nargout,
const char *nm,
const char **choices)
811 while (choices[nchoices] !=
nullptr)
814 int nargin = args.
length ();
816 if (nargout > 0 || nargin == 0)
819 if (wants_local_change (args, nargin))
821 if (! try_local_protect (var))
822 warning (R
"("local" has no effect outside a function)");
830 std::string sval = args(0).xstring_value (
"%s: first argument must be a string", nm);
833 for (; i < nchoices; i++)
835 if (sval == choices[i])
842 error (R
"(%s: value not allowed ("%s"))", nm, sval.c_str ());
884 int nargin = args.
length ();
892 = args(0).xstring_value (
"munlock: FCN must be a string");
912 int nargin = args.
length ();
920 = args(0).xstring_value (
"mislocked: FCN must be a string");
931 name_matches_any_pattern (
const std::string& nm,
const string_vector& argv,
932 int argc,
int idx,
bool have_regexp =
false)
936 for (
int k = idx; k < argc; k++)
938 std::string patstr = argv[k];
939 if (! patstr.empty ())
953 if (pattern.match (nm))
966 maybe_warn_exclusive (
bool exclusive)
969 warning (
"clear: ignoring --exclusive option");
975 bool exclusive =
false)
985 for (
const auto& name : fcns)
987 if (! name_matches_any_pattern (name, argv, argc, idx))
1002 bool exclusive =
false)
1008 for (
const auto& name : gvars)
1020 for (
const auto& name : gvars)
1022 if (! name_matches_any_pattern (name, argv, argc, idx))
1033 std::string pattern = argv[idx++];
1045 bool exclusive =
false,
bool have_regexp =
false)
1055 for (
const auto& name : lvars)
1057 if (! name_matches_any_pattern (name, argv, argc, idx,
1077 bool exclusive =
false)
1092 do_clear_variables (interp, argv, argc, idx, exclusive);
1093 do_clear_functions (interp, argv, argc, idx, exclusive);
1109 for (; idx < argc; idx++)
1115 else if (argv[idx] ==
"functions"
1118 do_clear_functions (interp, argv, argc, ++idx);
1120 else if (argv[idx] ==
"global"
1123 do_clear_globals (interp, argv, argc, ++idx);
1125 else if (argv[idx] ==
"variables"
1130 else if (argv[idx] ==
"classes"
1235 int argc = args.
length () + 1;
1241 do_clear_variables (interp, argv, argc,
true);
1251 bool clear_all =
false;
1252 bool clear_functions =
false;
1253 bool clear_globals =
false;
1254 bool clear_variables =
false;
1255 bool clear_objects =
false;
1256 bool exclusive =
false;
1257 bool have_regexp =
false;
1258 bool have_dash_option =
false;
1260 while (++idx < argc)
1262 if (argv[idx] ==
"-all" || argv[idx] ==
"-a")
1264 if (have_dash_option)
1267 have_dash_option =
true;
1270 else if (argv[idx] ==
"-exclusive" || argv[idx] ==
"-x")
1274 else if (argv[idx] ==
"-functions" || argv[idx] ==
"-f")
1276 if (have_dash_option)
1279 have_dash_option =
true;
1280 clear_functions =
true;
1282 else if (argv[idx] ==
"-global" || argv[idx] ==
"-g")
1284 if (have_dash_option)
1287 have_dash_option =
true;
1288 clear_globals =
true;
1290 else if (argv[idx] ==
"-variables" || argv[idx] ==
"-v")
1292 if (have_dash_option)
1295 have_dash_option =
true;
1296 clear_variables =
true;
1298 else if (argv[idx] ==
"-classes" || argv[idx] ==
"-c")
1300 if (have_dash_option)
1303 have_dash_option =
true;
1304 clear_objects =
true;
1306 else if (argv[idx] ==
"-regexp" || argv[idx] ==
"-r")
1308 if (have_dash_option)
1311 have_dash_option =
true;
1320 if (! have_dash_option && ! exclusive)
1321 do_matlab_compatible_clear (interp, argv, argc, idx);
1326 maybe_warn_exclusive (exclusive);
1329 warning (
"clear: ignoring extra arguments after -all");
1333 else if (have_regexp)
1335 do_clear_variables (interp, argv, argc, idx, exclusive,
true);
1337 else if (clear_functions)
1339 do_clear_functions (interp, argv, argc, idx, exclusive);
1341 else if (clear_globals)
1343 do_clear_globals (interp, argv, argc, idx, exclusive);
1345 else if (clear_variables)
1347 do_clear_variables (interp, argv, argc, idx, exclusive);
1349 else if (clear_objects)
1357 do_clear_symbols (interp, argv, argc, idx, exclusive);
1389 static std::string Vmissing_function_hook =
"__unimplemented__";
1391 DEFUN (missing_function_hook, args, nargout,
1406 "missing_function_hook");
1415 if (Vmissing_function_hook.empty ())
1418 octave::symbol_table& symtab = interp.get_symbol_table ();
1420 octave_value val = symtab.find_function (Vmissing_function_hook);
1425 octave::unwind_protect_var<std::string>
1426 restore_var (Vmissing_function_hook);
1429 const std::string fcn_name = Vmissing_function_hook;
1430 Vmissing_function_hook.clear ();
1435 if (tmp.
length () == 1 && tmp(0).is_string ())
1436 return tmp(0).string_value ();
1476 std::string name = args(0).xstring_value (
"__varval__: NAME must be a string");
1479 if (name ==
".argn.")
1486 return interp.
varval (name);
1489 static std::string Vmissing_component_hook;
1491 DEFUN (missing_component_hook, args, nargout,
1520 "missing_component_hook");
1523 OCTAVE_END_NAMESPACE(
octave)
Provides threadsafe access to octave.
octave_value varval(const std::string &name) const
void clear_variable(const std::string &name)
void mlock(bool skip_first=false) const
std::list< std::string > variable_names()
event_manager & get_event_manager()
symbol_table & get_symbol_table()
void clear_symbol_pattern(const std::string &pat)
void clear_global_variable(const std::string &name)
std::list< std::string > user_function_names()
void clear_global_variable_pattern(const std::string &pattern)
void clear_variable_regexp(const std::string &pattern)
bool mislocked(bool skip_first=false) const
tree_evaluator & get_evaluator()
std::list< std::string > global_variable_names()
void clear_variable_pattern(const std::string &pattern)
load_path & get_load_path()
void clear_function_pattern(const std::string &pat)
bool is_local_variable(const std::string &name) const
void munlock(bool skip_first=false) const
void clear_function(const std::string &name)
void clear_all(bool force=false)
void clear_functions(bool force=false)
std::string find_fcn(const std::string &fcn, std::string &dir_name, const std::string &pack_name="")
std::string find_method(const std::string &class_name, const std::string &meth, std::string &dir_name, const std::string &pack_name="")
static void clear_exemplar_map()
string_vector make_argv(const std::string &="") const
octave_idx_type length() const
bool is_function_handle() const
bool is_inline_function() const
bool is_classdef_meta() const
octave_function * function_value(bool silent=false) const
std::string string_value(bool force=false) const
bool is_anonymous_function() const
bool is_match(const std::string &buffer) const
bool is_built_in_function_name(const std::string &name)
octave_value find_function(const std::string &name, const symbol_scope &search_scope=symbol_scope::invalid())
octave_value find_cmdline_function(const std::string &name)
octave_value get_auto_fcn_var(stack_frame::auto_var_type avt) const
std::string lookup_autoload(const std::string &nm) 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.
void warning(const char *fmt,...)
void error_with_cfn(const char *fmt,...)
void() error(const char *fmt,...)
#define error_unless(cond)
interpreter & __get_interpreter__()
tree_evaluator & __get_evaluator__()
symbol_table & __get_symbol_table__()
bool iskeyword(const std::string &s)
octave_value set_internal_variable(std::string &var, const octave_value_list &args, int nargout, const char *nm, const char **choices)
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
bool drive_or_unc_share(const std::string &)
std::string file_in_path(const std::string &, const std::string &)
octave_function * is_valid_function(const std::string &fcn_name, const std::string &warn_for, bool warn)
int symbol_exist(const std::string &name, const std::string &type="any")
std::string maybe_missing_function_hook(const std::string &name)
std::string unique_symbol_name(const std::string &basename)