Mercurial > hg > octave-nkf
annotate src/variables.cc @ 7531:c9a476b1e664
correctly set ans for cs-lists and simplify printing them
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 26 Feb 2008 02:16:32 -0500 |
parents | 0fa079d04772 |
children | 84122fb29c75 |
rev | line source |
---|---|
1 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002, |
4 2003, 2004, 2005, 2006, 2007 John W. Eaton | |
1 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
1 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
1 | 21 |
22 */ | |
23 | |
240 | 24 #ifdef HAVE_CONFIG_H |
1192 | 25 #include <config.h> |
1 | 26 #endif |
27 | |
1468 | 28 #include <cstdio> |
1343 | 29 #include <cstring> |
605 | 30 |
7336 | 31 #include <iomanip> |
4207 | 32 #include <set> |
1728 | 33 #include <string> |
34 | |
2926 | 35 #include "file-stat.h" |
36 #include "oct-env.h" | |
4604 | 37 #include "file-ops.h" |
2926 | 38 #include "glob-match.h" |
1755 | 39 #include "str-vec.h" |
40 | |
2492 | 41 #include <defaults.h> |
4435 | 42 #include "Cell.h" |
1352 | 43 #include "defun.h" |
44 #include "dirfns.h" | |
45 #include "error.h" | |
2205 | 46 #include "gripes.h" |
1352 | 47 #include "help.h" |
3165 | 48 #include "input.h" |
1352 | 49 #include "lex.h" |
5832 | 50 #include "load-path.h" |
2926 | 51 #include "oct-map.h" |
52 #include "oct-obj.h" | |
53 #include "ov.h" | |
3933 | 54 #include "ov-usr-fcn.h" |
605 | 55 #include "pager.h" |
1352 | 56 #include "parse.h" |
2926 | 57 #include "symtab.h" |
2205 | 58 #include "toplev.h" |
1352 | 59 #include "unwind-prot.h" |
1 | 60 #include "utils.h" |
1352 | 61 #include "variables.h" |
2205 | 62 |
63 // Should Octave always check to see if function files have changed | |
64 // since they were last compiled? | |
5794 | 65 static int Vignore_function_time_stamp = 1; |
2205 | 66 |
7336 | 67 // Defines layout for the whos/who -long command |
68 static std::string Vwhos_line_format | |
69 = " %a:4; %ln:6; %cs:16:6:8:1; %rb:12; %lc:-1;\n"; | |
195 | 70 |
6068 | 71 void |
6072 | 72 clear_mex_functions (void) |
6068 | 73 { |
7336 | 74 symbol_table::clear_mex_functions (); |
75 } | |
76 | |
77 void | |
78 clear_function (const std::string& nm) | |
79 { | |
80 symbol_table::clear_function (nm); | |
81 } | |
82 | |
83 void | |
84 clear_variable (const std::string& nm) | |
85 { | |
86 symbol_table::clear_variable (nm); | |
87 } | |
88 | |
89 void | |
90 clear_symbol (const std::string& nm) | |
91 { | |
92 symbol_table::clear_symbol (nm); | |
6068 | 93 } |
94 | |
593 | 95 // Attributes of variables and functions. |
96 | |
4208 | 97 // Is this a command-style function? |
593 | 98 |
4208 | 99 static std::set <std::string> command_set; |
4207 | 100 |
7336 | 101 void |
4208 | 102 mark_as_command (const std::string& s) |
4207 | 103 { |
4208 | 104 command_set.insert (s); |
4207 | 105 } |
106 | |
107 static inline void | |
4208 | 108 unmark_command (const std::string& s) |
4207 | 109 { |
4208 | 110 command_set.erase (s); |
4207 | 111 } |
112 | |
4208 | 113 DEFCMD (mark_as_command, args, , |
4207 | 114 "-*- texinfo -*-\n\ |
4208 | 115 @deftypefn {Built-in Function} {} mark_as_command (@var{name})\n\ |
116 Enter @var{name} into the list of commands.\n\ | |
5642 | 117 @seealso{unmark_command, iscommand}\n\ |
118 @end deftypefn") | |
4207 | 119 { |
120 octave_value_list retval; | |
121 | |
7336 | 122 if (symbol_table::at_top_level ()) |
4208 | 123 { |
124 int nargin = args.length (); | |
4207 | 125 |
4208 | 126 if (nargin > 0) |
127 { | |
128 int argc = nargin + 1; | |
4207 | 129 |
4208 | 130 string_vector argv = args.make_argv ("mark_as_command"); |
4207 | 131 |
4208 | 132 if (! error_state) |
133 { | |
134 for (int i = 1; i < argc; i++) | |
135 mark_as_command (argv[i]); | |
136 } | |
4207 | 137 } |
4208 | 138 else |
5823 | 139 print_usage (); |
4207 | 140 } |
141 else | |
4208 | 142 warning ("mark_as_command: invalid use inside function body"); |
4207 | 143 |
144 return retval; | |
145 } | |
146 | |
4208 | 147 DEFCMD (unmark_command, args, , |
4207 | 148 "-*- texinfo -*-\n\ |
5102 | 149 @deftypefn {Built-in Function} {} unmark_command (@var{name})\n\ |
4208 | 150 Remove @var{name} from the list of commands.\n\ |
5642 | 151 @seealso{mark_as_command, iscommand}\n\ |
152 @end deftypefn") | |
4207 | 153 { |
154 octave_value_list retval; | |
155 | |
7336 | 156 if (symbol_table::at_top_level ()) |
4208 | 157 { |
158 int nargin = args.length (); | |
4207 | 159 |
4208 | 160 if (nargin > 0) |
161 { | |
162 int argc = nargin + 1; | |
4207 | 163 |
4208 | 164 string_vector argv = args.make_argv ("unmark_command"); |
4207 | 165 |
4208 | 166 if (! error_state) |
167 { | |
168 for (int i = 1; i < argc; i++) | |
169 unmark_command (argv[i]); | |
170 } | |
4207 | 171 } |
4208 | 172 else |
5823 | 173 print_usage (); |
4207 | 174 } |
175 else | |
4208 | 176 warning ("mark_as_command: invalid use inside function body"); |
4207 | 177 |
178 return retval; | |
179 } | |
180 | |
1827 | 181 bool |
4208 | 182 is_command_name (const std::string& s) |
593 | 183 { |
7336 | 184 return command_set.find (s) != command_set.end (); |
593 | 185 } |
186 | |
7336 | 187 |
5102 | 188 DEFCMD (iscommand, args, , |
189 "-*- texinfo -*-\n\ | |
190 @deftypefn {Built-in Function} {} iscommand (@var{name})\n\ | |
191 Return true if @var{name} is a command style function. If @var{name}\n\ | |
192 is omitted, return a list of identifiers which are marked as commands with\n\ | |
6637 | 193 @code{mark_as_command}.\n\ |
5642 | 194 @seealso{mark_as_command, unmark_command}\n\ |
195 @end deftypefn") | |
5102 | 196 { |
197 octave_value retval; | |
198 | |
199 int nargin = args.length (); | |
200 | |
201 if (nargin == 0) | |
202 { | |
203 string_vector lst (command_set.size ()); | |
204 | |
205 int i = 0; | |
206 for (std::set<std::string>::const_iterator p = command_set.begin (); | |
207 p != command_set.end (); p++) | |
208 lst[i++] = *p; | |
209 | |
210 retval = Cell (lst.qsort ()); | |
211 } | |
212 else if (nargin == 1) | |
213 { | |
214 string_vector argv = args.make_argv ("iscommand"); | |
215 | |
216 if (! error_state) | |
217 { | |
218 std::string s = argv[1]; | |
219 retval = is_command_name(s); | |
220 } | |
221 } | |
222 else | |
5823 | 223 print_usage (); |
5102 | 224 |
225 return retval; | |
226 } | |
227 | |
228 // Is this a raw input command? | |
229 | |
230 static std::set <std::string> rawcommand_set; | |
231 | |
5685 | 232 void |
5102 | 233 mark_as_rawcommand (const std::string& s) |
234 { | |
235 command_set.insert (s); | |
236 rawcommand_set.insert (s); | |
237 } | |
238 | |
5685 | 239 void |
5102 | 240 unmark_rawcommand (const std::string& s) |
241 { | |
242 rawcommand_set.erase (s); | |
243 } | |
244 | |
245 DEFCMD (mark_as_rawcommand, args, , | |
246 "-*- texinfo -*-\n\ | |
247 @deftypefn {Built-in Function} {} mark_as_rawcommand (@var{name})\n\ | |
248 Enter @var{name} into the list of raw input commands and to the list of\n\ | |
249 command style functions.\n\ | |
250 Raw input commands are like normal command style functions, but they\n\ | |
251 receive their input unprocessed (ie. strings still contain the quotes\n\ | |
252 and escapes they had when input). However, comments and continuations\n\ | |
253 are handled as usual, you cannot pass a token starting with a comment\n\ | |
254 character ('#' or '%') to your function, and the last token cannot be\n\ | |
255 a continuation token ('\\' or '...').\n\ | |
5642 | 256 @seealso{unmark_rawcommand, israwcommand, iscommand, mark_as_command}\n\ |
257 @end deftypefn") | |
5102 | 258 { |
259 octave_value_list retval; | |
260 | |
7336 | 261 if (symbol_table::at_top_level ()) |
5102 | 262 { |
263 int nargin = args.length (); | |
264 | |
265 if (nargin > 0) | |
266 { | |
267 int argc = nargin + 1; | |
268 | |
269 string_vector argv = args.make_argv ("mark_as_rawcommand"); | |
270 | |
271 if (! error_state) | |
272 { | |
273 for (int i = 1; i < argc; i++) | |
274 mark_as_rawcommand (argv[i]); | |
275 } | |
276 } | |
277 else | |
5823 | 278 print_usage (); |
5102 | 279 } |
280 else | |
281 warning ("mark_as_rawcommand: invalid use inside function body"); | |
282 | |
283 return retval; | |
284 } | |
285 | |
286 DEFCMD (unmark_rawcommand, args, , | |
287 "-*- texinfo -*-\n\ | |
288 @deftypefn {Built-in Function} {} unmark_rawcommand (@var{name})\n\ | |
289 Remove @var{name} from the list of raw input commands.\n\ | |
290 Note that this does not remove @var{name} from the list of command style\n\ | |
291 functions.\n\ | |
5642 | 292 @seealso{mark_as_rawcommand, israwcommand, iscommand, unmark_command}\n\ |
293 @end deftypefn") | |
5102 | 294 { |
295 octave_value_list retval; | |
296 | |
7336 | 297 if (symbol_table::at_top_level ()) |
5102 | 298 { |
299 int nargin = args.length (); | |
300 | |
301 if (nargin > 0) | |
302 { | |
303 int argc = nargin + 1; | |
304 | |
305 string_vector argv = args.make_argv ("unmark_rawcommand"); | |
306 | |
307 if (! error_state) | |
308 { | |
309 for (int i = 1; i < argc; i++) | |
310 unmark_rawcommand (argv[i]); | |
311 } | |
312 } | |
313 else | |
5823 | 314 print_usage (); |
5102 | 315 } |
316 else | |
317 warning ("unmark_rawcommand: invalid use inside function body"); | |
318 | |
319 return retval; | |
320 } | |
321 | |
322 bool | |
323 is_rawcommand_name (const std::string& s) | |
324 { | |
7336 | 325 return rawcommand_set.find (s) != rawcommand_set.end (); |
5102 | 326 } |
327 | |
328 DEFCMD (israwcommand, args, , | |
329 "-*- texinfo -*-\n\ | |
330 @deftypefn {Built-in Function} {} israwcommand (@var{name})\n\ | |
331 Return true if @var{name} is a raw input command function.\n\ | |
332 If @var{name} is omitted, return a list of identifiers which are marked as\n\ | |
333 raw input commands with mark_as_rawcommand.\n\ | |
5642 | 334 @seealso{mark_as_rawcommand, unmark_rawcommand}\n\ |
335 @end deftypefn") | |
5102 | 336 { |
337 octave_value retval; | |
338 | |
339 int nargin = args.length (); | |
340 | |
341 if (nargin == 0) | |
342 { | |
343 string_vector lst (rawcommand_set.size()); | |
344 | |
345 int i = 0; | |
346 for (std::set<std::string>::const_iterator p = rawcommand_set.begin (); | |
347 p != rawcommand_set.end (); | |
348 p++) | |
349 lst[i++] = *p; | |
350 | |
351 retval = Cell (lst.qsort ()); | |
352 } | |
353 else if (nargin == 1) | |
354 { | |
355 string_vector argv = args.make_argv ("israwcommand"); | |
356 | |
357 if (! error_state) | |
358 { | |
359 std::string s = argv[1]; | |
360 retval = is_rawcommand_name(s); | |
361 } | |
362 } | |
363 else | |
5823 | 364 print_usage (); |
5102 | 365 |
366 return retval; | |
367 } | |
368 | |
2086 | 369 // Is this octave_value a valid function? |
593 | 370 |
2975 | 371 octave_function * |
4345 | 372 is_valid_function (const std::string& fcn_name, |
373 const std::string& warn_for, bool warn) | |
593 | 374 { |
2975 | 375 octave_function *ans = 0; |
593 | 376 |
1755 | 377 if (! fcn_name.empty ()) |
3618 | 378 { |
7336 | 379 octave_value val = symbol_table::find_function (fcn_name); |
380 | |
381 if (val.is_defined ()) | |
382 ans = val.function_value (true); | |
3618 | 383 } |
593 | 384 |
7336 | 385 if (! ans && warn) |
386 error ("%s: the symbol `%s' is not valid as a function", | |
387 warn_for.c_str (), fcn_name.c_str ()); | |
593 | 388 |
389 return ans; | |
390 } | |
391 | |
2975 | 392 octave_function * |
4345 | 393 is_valid_function (const octave_value& arg, |
394 const std::string& warn_for, bool warn) | |
3178 | 395 { |
396 octave_function *ans = 0; | |
397 | |
3523 | 398 std::string fcn_name; |
3178 | 399 |
400 if (arg.is_string ()) | |
4700 | 401 { |
402 fcn_name = arg.string_value (); | |
3178 | 403 |
4700 | 404 if (! error_state) |
405 ans = is_valid_function (fcn_name, warn_for, warn); | |
406 else if (warn) | |
407 error ("%s: expecting function name as argument", warn_for.c_str ()); | |
408 } | |
3178 | 409 else if (warn) |
410 error ("%s: expecting function name as argument", warn_for.c_str ()); | |
411 | |
412 return ans; | |
413 } | |
414 | |
415 octave_function * | |
3523 | 416 extract_function (const octave_value& arg, const std::string& warn_for, |
417 const std::string& fname, const std::string& header, | |
418 const std::string& trailer) | |
2796 | 419 { |
2975 | 420 octave_function *retval = 0; |
2796 | 421 |
422 retval = is_valid_function (arg, warn_for, 0); | |
423 | |
424 if (! retval) | |
425 { | |
3523 | 426 std::string s = arg.string_value (); |
2796 | 427 |
3523 | 428 std::string cmd = header; |
2796 | 429 cmd.append (s); |
430 cmd.append (trailer); | |
431 | |
432 if (! error_state) | |
433 { | |
434 int parse_status; | |
435 | |
2898 | 436 eval_string (cmd, true, parse_status); |
2796 | 437 |
438 if (parse_status == 0) | |
439 { | |
440 retval = is_valid_function (fname, warn_for, 0); | |
441 | |
442 if (! retval) | |
443 { | |
444 error ("%s: `%s' is not valid as a function", | |
445 warn_for.c_str (), fname.c_str ()); | |
446 return retval; | |
447 } | |
448 } | |
449 else | |
450 error ("%s: `%s' is not valid as a function", | |
451 warn_for.c_str (), fname.c_str ()); | |
452 } | |
453 else | |
454 error ("%s: expecting first argument to be a string", | |
455 warn_for.c_str ()); | |
456 } | |
457 | |
458 return retval; | |
459 } | |
460 | |
2921 | 461 string_vector |
3523 | 462 get_struct_elts (const std::string& text) |
2921 | 463 { |
464 int n = 1; | |
465 | |
466 size_t pos = 0; | |
467 | |
468 size_t len = text.length (); | |
469 | |
470 while ((pos = text.find ('.', pos)) != NPOS) | |
471 { | |
472 if (++pos == len) | |
473 break; | |
474 | |
475 n++; | |
476 } | |
477 | |
478 string_vector retval (n); | |
479 | |
480 pos = 0; | |
481 | |
482 for (int i = 0; i < n; i++) | |
483 { | |
4587 | 484 len = text.find ('.', pos); |
2921 | 485 |
486 if (len != NPOS) | |
487 len -= pos; | |
488 | |
489 retval[i] = text.substr (pos, len); | |
490 | |
491 if (len != NPOS) | |
492 pos += len + 1; | |
493 } | |
494 | |
495 return retval; | |
496 } | |
497 | |
4179 | 498 static inline bool |
499 is_variable (const std::string& name) | |
500 { | |
501 bool retval = false; | |
502 | |
503 if (! name.empty ()) | |
504 { | |
7336 | 505 octave_value val = symbol_table::varval (name); |
506 | |
507 retval = val.is_defined (); | |
4179 | 508 } |
509 | |
510 return retval; | |
511 } | |
512 | |
2921 | 513 string_vector |
3933 | 514 generate_struct_completions (const std::string& text, |
515 std::string& prefix, std::string& hint) | |
2921 | 516 { |
517 string_vector names; | |
518 | |
519 size_t pos = text.rfind ('.'); | |
520 | |
3933 | 521 if (pos != NPOS) |
2921 | 522 { |
523 if (pos == text.length ()) | |
524 hint = ""; | |
525 else | |
526 hint = text.substr (pos+1); | |
527 | |
528 prefix = text.substr (0, pos); | |
529 | |
4179 | 530 std::string base_name = prefix; |
531 | |
532 pos = base_name.find_first_of ("{(."); | |
2921 | 533 |
4179 | 534 if (pos != NPOS) |
535 base_name = base_name.substr (0, pos); | |
4143 | 536 |
4179 | 537 if (is_variable (base_name)) |
538 { | |
539 int parse_status; | |
540 | |
541 unwind_protect::begin_frame ("generate_struct_completions"); | |
3935 | 542 |
4452 | 543 unwind_protect_int (error_state); |
544 unwind_protect_int (warning_state); | |
545 | |
4179 | 546 unwind_protect_bool (discard_error_messages); |
4452 | 547 unwind_protect_bool (discard_warning_messages); |
3935 | 548 |
4179 | 549 discard_error_messages = true; |
4452 | 550 discard_warning_messages = true; |
2921 | 551 |
4179 | 552 octave_value tmp = eval_string (prefix, true, parse_status); |
553 | |
554 unwind_protect::run_frame ("generate_struct_completions"); | |
3935 | 555 |
4179 | 556 if (tmp.is_defined () && tmp.is_map ()) |
557 names = tmp.map_keys (); | |
558 } | |
559 } | |
2921 | 560 |
561 return names; | |
562 } | |
563 | |
5775 | 564 // FIXME -- this will have to be much smarter to work |
4179 | 565 // "correctly". |
566 | |
2921 | 567 bool |
3523 | 568 looks_like_struct (const std::string& text) |
2921 | 569 { |
4604 | 570 bool retval = (! text.empty () |
571 && text != "." | |
572 && text.find_first_of (file_ops::dir_sep_chars) == NPOS | |
573 && text.find ("..") == NPOS | |
574 && text.rfind ('.') != NPOS); | |
3968 | 575 |
4179 | 576 #if 0 |
3968 | 577 symbol_record *sr = curr_sym_tab->lookup (text); |
2963 | 578 |
3968 | 579 if (sr && ! sr->is_function ()) |
580 { | |
581 int parse_status; | |
2921 | 582 |
4143 | 583 unwind_protect::begin_frame ("looks_like_struct"); |
584 | |
585 unwind_protect_bool (discard_error_messages); | |
586 unwind_protect_int (error_state); | |
587 | |
588 discard_error_messages = true; | |
589 | |
3968 | 590 octave_value tmp = eval_string (text, true, parse_status); |
591 | |
4143 | 592 unwind_protect::run_frame ("looks_like_struct"); |
593 | |
3968 | 594 retval = (tmp.is_defined () && tmp.is_map ()); |
595 } | |
4179 | 596 #endif |
3968 | 597 |
598 return retval; | |
2921 | 599 } |
2796 | 600 |
5930 | 601 static octave_value |
602 do_isglobal (const octave_value_list& args) | |
593 | 603 { |
4233 | 604 octave_value retval = false; |
593 | 605 |
712 | 606 int nargin = args.length (); |
607 | |
608 if (nargin != 1) | |
593 | 609 { |
5823 | 610 print_usage (); |
593 | 611 return retval; |
612 } | |
613 | |
3523 | 614 std::string name = args(0).string_value (); |
593 | 615 |
636 | 616 if (error_state) |
617 { | |
4028 | 618 error ("isglobal: expecting std::string argument"); |
636 | 619 return retval; |
620 } | |
621 | |
7336 | 622 return symbol_table::is_global (name); |
593 | 623 } |
624 | |
5930 | 625 DEFUN (isglobal, args, , |
626 "-*- texinfo -*-\n\ | |
627 @deftypefn {Built-in Function} {} isglobal (@var{name})\n\ | |
628 Return 1 if @var{name} is globally visible. Otherwise, return 0. For\n\ | |
629 example,\n\ | |
630 \n\ | |
631 @example\n\ | |
632 @group\n\ | |
633 global x\n\ | |
634 isglobal (\"x\")\n\ | |
635 @result{} 1\n\ | |
636 @end group\n\ | |
637 @end example\n\ | |
638 @end deftypefn") | |
639 { | |
640 return do_isglobal (args); | |
641 } | |
642 | |
643 DEFUN (is_global, args, , | |
644 "-*- texinfo -*-\n\ | |
645 @deftypefn {Built-in Function} {} isglobal (@var{name})\n\ | |
646 This function has been deprecated. Use isglobal instead.\n\ | |
647 @end deftypefn") | |
648 { | |
649 return do_isglobal (args); | |
650 } | |
651 | |
4016 | 652 int |
653 symbol_exist (const std::string& name, const std::string& type) | |
593 | 654 { |
4016 | 655 int retval = 0; |
636 | 656 |
3523 | 657 std::string struct_elts; |
658 std::string symbol_name = name; | |
1755 | 659 |
660 size_t pos = name.find ('.'); | |
661 | |
2790 | 662 if (pos != NPOS && pos > 0) |
1277 | 663 { |
1755 | 664 struct_elts = name.substr (pos+1); |
2790 | 665 symbol_name = name.substr (0, pos); |
1277 | 666 } |
667 | |
4009 | 668 // We shouldn't need to look in the global symbol table, since any |
669 // name that is visible in the current scope will be in the local | |
670 // symbol table. | |
671 | |
7336 | 672 octave_value_list evaluated_args; |
673 bool args_evaluated; | |
674 | |
675 octave_value val = symbol_table::find (symbol_name, 0, string_vector (), | |
676 evaluated_args, args_evaluated); | |
677 | |
678 if (val.is_defined ()) | |
1277 | 679 { |
4357 | 680 bool not_a_struct = struct_elts.empty (); |
7336 | 681 bool var_ok = not_a_struct /* || val.is_map_element (struct_elts) */; |
4357 | 682 |
4016 | 683 if (! retval |
4357 | 684 && var_ok |
4016 | 685 && (type == "any" || type == "var") |
7336 | 686 && val.is_constant ()) |
4009 | 687 { |
4016 | 688 retval = 1; |
4009 | 689 } |
4016 | 690 |
691 if (! retval | |
692 && (type == "any" || type == "builtin")) | |
4009 | 693 { |
7336 | 694 if (not_a_struct && val.is_builtin_function ()) |
4016 | 695 { |
696 retval = 5; | |
697 } | |
4009 | 698 } |
4016 | 699 |
700 if (! retval | |
4357 | 701 && not_a_struct |
4016 | 702 && (type == "any" || type == "file") |
7336 | 703 && (val.is_user_function () || val.is_dld_function ())) |
4009 | 704 { |
7336 | 705 octave_function *f = val.function_value (true); |
4016 | 706 std::string s = f ? f->fcn_file_name () : std::string (); |
707 | |
7336 | 708 retval = s.empty () ? 103 : (val.is_user_function () ? 2 : 3); |
4009 | 709 } |
1421 | 710 } |
4016 | 711 |
5140 | 712 if (! (type == "var" || type == "builtin")) |
593 | 713 { |
5140 | 714 if (! retval) |
593 | 715 { |
5484 | 716 std::string file_name = lookup_autoload (name); |
717 | |
718 if (file_name.empty ()) | |
5832 | 719 file_name = load_path::find_fcn (name); |
5140 | 720 |
721 size_t len = file_name.length (); | |
722 | |
5832 | 723 if (len > 0) |
4437 | 724 { |
5140 | 725 if (type == "any" || type == "file") |
726 { | |
5864 | 727 if (len > 4 && (file_name.substr (len-4) == ".oct" |
728 || file_name.substr (len-4) == ".mex")) | |
5140 | 729 retval = 3; |
730 else | |
731 retval = 2; | |
732 } | |
4437 | 733 } |
593 | 734 } |
5140 | 735 |
736 if (! retval) | |
593 | 737 { |
5140 | 738 std::string file_name = file_in_path (name, ""); |
739 | |
740 if (file_name.empty ()) | |
741 file_name = name; | |
742 | |
743 file_stat fs (file_name); | |
744 | |
745 if (fs) | |
1421 | 746 { |
5140 | 747 if ((type == "any" || type == "file") |
748 && fs.is_reg ()) | |
749 { | |
750 retval = 2; | |
751 } | |
752 else if ((type == "any" || type == "dir") | |
753 && fs.is_dir ()) | |
754 { | |
755 retval = 7; | |
756 } | |
1421 | 757 } |
593 | 758 } |
759 } | |
760 | |
761 return retval; | |
762 } | |
763 | |
4962 | 764 #define GET_IDX(LEN) \ |
765 static_cast<int> ((LEN-1) * static_cast<double> (rand ()) / RAND_MAX) | |
766 | |
4954 | 767 std::string |
768 unique_symbol_name (const std::string& basename) | |
769 { | |
4962 | 770 static const std::string alpha |
771 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
772 | |
773 static size_t len = alpha.length (); | |
774 | |
775 std::string nm = basename + alpha[GET_IDX (len)]; | |
776 | |
777 size_t pos = nm.length (); | |
778 | |
779 if (nm.substr (0, 2) == "__") | |
780 nm.append ("__"); | |
781 | |
782 while (symbol_exist (nm, "any")) | |
783 nm.insert (pos++, 1, alpha[GET_IDX (len)]); | |
784 | |
785 return nm; | |
4954 | 786 } |
787 | |
4016 | 788 DEFUN (exist, args, , |
789 "-*- texinfo -*-\n\ | |
790 @deftypefn {Built-in Function} {} exist (@var{name}, @var{type})\n\ | |
791 Return 1 if the name exists as a variable, 2 if the name (after\n\ | |
5814 | 792 appending @samp{.m}) is a function file in Octave's @code{path}, 3 if the\n\ |
5864 | 793 name is a @samp{.oct} or @samp{.mex} file in Octave's @code{path},\n\ |
794 5 if the name is a built-in function, 7 if the name is a directory, or 103\n\ | |
4016 | 795 if the name is a function not associated with a file (entered on\n\ |
796 the command line).\n\ | |
797 \n\ | |
798 Otherwise, return 0.\n\ | |
799 \n\ | |
800 This function also returns 2 if a regular file called @var{name}\n\ | |
5814 | 801 exists in Octave's search path. If you want information about\n\ |
4016 | 802 other types of files, you should use some combination of the functions\n\ |
803 @code{file_in_path} and @code{stat} instead.\n\ | |
804 \n\ | |
805 If the optional argument @var{type} is supplied, check only for\n\ | |
806 symbols of the specified type. Valid types are\n\ | |
807 \n\ | |
808 @table @samp\n\ | |
809 @item \"var\"\n\ | |
810 Check only for variables.\n\ | |
811 @item \"builtin\"\n\ | |
812 Check only for built-in functions.\n\ | |
813 @item \"file\"\n\ | |
814 Check only for files.\n\ | |
815 @item \"dir\"\n\ | |
816 Check only for directories.\n\ | |
817 @end table\n\ | |
818 @end deftypefn") | |
819 { | |
4233 | 820 octave_value retval = false; |
4016 | 821 |
822 int nargin = args.length (); | |
823 | |
824 if (nargin == 1 || nargin == 2) | |
825 { | |
826 std::string name = args(0).string_value (); | |
827 | |
828 if (! error_state) | |
829 { | |
830 std::string type | |
831 = (nargin == 2) ? args(1).string_value () : std::string ("any"); | |
832 | |
833 if (! error_state) | |
4233 | 834 retval = symbol_exist (name, type); |
4016 | 835 else |
836 error ("exist: expecting second argument to be a string"); | |
837 } | |
838 else | |
839 error ("exist: expecting first argument to be a string"); | |
840 } | |
841 else | |
5823 | 842 print_usage (); |
4016 | 843 |
844 return retval; | |
845 } | |
846 | |
2849 | 847 octave_value |
4988 | 848 lookup_function_handle (const std::string& nm) |
849 { | |
7336 | 850 octave_value val = symbol_table::varval (nm); |
851 | |
852 return val.is_function_handle () ? val : octave_value (); | |
4988 | 853 } |
854 | |
855 octave_value | |
5027 | 856 get_global_value (const std::string& nm, bool silent) |
2849 | 857 { |
7336 | 858 octave_value val = symbol_table::varval (nm, symbol_table::global_scope ()); |
859 | |
860 if (val.is_undefined () && ! silent) | |
861 error ("get_global_by_name: undefined symbol `%s'", nm.c_str ()); | |
862 | |
863 return val; | |
2849 | 864 } |
865 | |
866 void | |
3523 | 867 set_global_value (const std::string& nm, const octave_value& val) |
2849 | 868 { |
7336 | 869 symbol_table::varref (nm, symbol_table::global_scope ()) = val; |
2849 | 870 } |
871 | |
593 | 872 // Variable values. |
195 | 873 |
5791 | 874 octave_value |
875 set_internal_variable (bool& var, const octave_value_list& args, | |
5794 | 876 int nargout, const char *nm) |
5791 | 877 { |
5794 | 878 octave_value retval; |
879 | |
5800 | 880 int nargin = args.length (); |
881 | |
882 if (nargout > 0 || nargin == 0) | |
5794 | 883 retval = var; |
5791 | 884 |
885 if (nargin == 1) | |
886 { | |
887 bool bval = args(0).bool_value (); | |
888 | |
889 if (! error_state) | |
890 var = bval; | |
891 else | |
892 error ("%s: expecting arg to be a logical value", nm); | |
893 } | |
894 else if (nargin > 1) | |
5823 | 895 print_usage (); |
5791 | 896 |
897 return retval; | |
898 } | |
899 | |
900 octave_value | |
5794 | 901 set_internal_variable (char& var, const octave_value_list& args, |
902 int nargout, const char *nm) | |
5791 | 903 { |
5794 | 904 octave_value retval; |
905 | |
5800 | 906 int nargin = args.length (); |
907 | |
908 if (nargout > 0 || nargin == 0) | |
5794 | 909 retval = var; |
5791 | 910 |
911 if (nargin == 1) | |
912 { | |
913 std::string sval = args(0).string_value (); | |
914 | |
915 if (! error_state) | |
5794 | 916 { |
917 switch (sval.length ()) | |
918 { | |
919 case 1: | |
920 var = sval[0]; | |
921 break; | |
922 | |
923 case 0: | |
924 var = '\0'; | |
925 break; | |
926 | |
927 default: | |
928 error ("%s: argument must be a single character", nm); | |
929 break; | |
930 } | |
931 } | |
932 else | |
933 error ("%s: argument must be a single character", nm); | |
934 } | |
935 else if (nargin > 1) | |
5823 | 936 print_usage (); |
5794 | 937 |
938 return retval; | |
939 } | |
940 | |
941 octave_value | |
942 set_internal_variable (int& var, const octave_value_list& args, | |
943 int nargout, const char *nm, | |
944 int minval, int maxval) | |
945 { | |
946 octave_value retval; | |
947 | |
5800 | 948 int nargin = args.length (); |
949 | |
950 if (nargout > 0 || nargin == 0) | |
5794 | 951 retval = var; |
952 | |
953 if (nargin == 1) | |
954 { | |
955 int ival = args(0).int_value (); | |
956 | |
957 if (! error_state) | |
958 { | |
959 if (ival < minval) | |
960 error ("%s: expecting arg to be greater than %d", minval); | |
961 else if (ival > maxval) | |
962 error ("%s: expecting arg to be less than or equal to %d", maxval); | |
963 else | |
964 var = ival; | |
965 } | |
966 else | |
967 error ("%s: expecting arg to be an integer value", nm); | |
968 } | |
969 else if (nargin > 1) | |
5823 | 970 print_usage (); |
5794 | 971 |
972 return retval; | |
973 } | |
974 | |
975 octave_value | |
976 set_internal_variable (double& var, const octave_value_list& args, | |
977 int nargout, const char *nm, | |
978 double minval, double maxval) | |
979 { | |
980 octave_value retval; | |
981 | |
5800 | 982 int nargin = args.length (); |
983 | |
984 if (nargout > 0 || nargin == 0) | |
5794 | 985 retval = var; |
986 | |
987 if (nargin == 1) | |
988 { | |
989 double dval = args(0).scalar_value (); | |
990 | |
991 if (! error_state) | |
992 { | |
993 if (dval < minval) | |
994 error ("%s: expecting arg to be greater than %g", minval); | |
995 else if (dval > maxval) | |
996 error ("%s: expecting arg to be less than or equal to %g", maxval); | |
997 else | |
998 var = dval; | |
999 } | |
1000 else | |
1001 error ("%s: expecting arg to be a scalar value", nm); | |
1002 } | |
1003 else if (nargin > 1) | |
5823 | 1004 print_usage (); |
5794 | 1005 |
1006 return retval; | |
1007 } | |
1008 | |
1009 octave_value | |
1010 set_internal_variable (std::string& var, const octave_value_list& args, | |
1011 int nargout, const char *nm, bool empty_ok) | |
1012 { | |
1013 octave_value retval; | |
1014 | |
5800 | 1015 int nargin = args.length (); |
1016 | |
1017 if (nargout > 0 || nargin == 0) | |
5794 | 1018 retval = var; |
1019 | |
1020 if (nargin == 1) | |
1021 { | |
1022 std::string sval = args(0).string_value (); | |
1023 | |
1024 if (! error_state) | |
1025 { | |
1026 if (empty_ok || ! sval.empty ()) | |
1027 var = sval; | |
1028 else | |
1029 error ("%s: value must not be empty", nm); | |
1030 } | |
5791 | 1031 else |
1032 error ("%s: expecting arg to be a character string", nm); | |
1033 } | |
1034 else if (nargin > 1) | |
5823 | 1035 print_usage (); |
1 | 1036 |
1037 return retval; | |
1038 } | |
1039 | |
7336 | 1040 struct |
1041 symbol_record_name_compare | |
1042 { | |
1043 bool operator () (const symbol_table::symbol_record& a, | |
1044 const symbol_table::symbol_record& b) | |
1045 { | |
1046 std::string a_nm = a.name (); | |
1047 std::string b_nm = b.name (); | |
1048 | |
1049 return a_nm.compare (b_nm); | |
1050 } | |
1051 }; | |
1052 | |
1053 struct | |
1054 whos_parameter | |
195 | 1055 { |
7336 | 1056 char command; |
1057 char modifier; | |
1058 int parameter_length; | |
1059 int first_parameter_length; | |
1060 int dimensions; | |
1061 int balance; | |
1062 std::string text; | |
1063 std::string line; | |
1064 }; | |
1065 | |
1066 static void | |
1067 print_descriptor (std::ostream& os, std::list<whos_parameter> params) | |
1068 { | |
1069 // This method prints a line of information on a given symbol | |
1070 std::list<whos_parameter>::iterator i = params.begin (); | |
1071 std::ostringstream param_buf; | |
1072 | |
1073 while (i != params.end ()) | |
195 | 1074 { |
7336 | 1075 whos_parameter param = *i; |
1076 | |
1077 if (param.command != '\0') | |
1078 { | |
1079 // Do the actual printing | |
1080 switch (param.modifier) | |
1081 { | |
1082 case 'l': | |
1083 os << std::setiosflags (std::ios::left) << std::setw (param.parameter_length); | |
1084 param_buf << std::setiosflags (std::ios::left) << std::setw (param.parameter_length); | |
1085 break; | |
1086 | |
1087 case 'r': | |
1088 os << std::setiosflags (std::ios::right) << std::setw (param.parameter_length); | |
1089 param_buf << std::setiosflags (std::ios::right) << std::setw (param.parameter_length); | |
1090 break; | |
1091 | |
1092 case 'c': | |
1093 if (param.command != 's') | |
1094 { | |
1095 os << std::setiosflags (std::ios::left) | |
1096 << std::setw (param.parameter_length); | |
1097 param_buf << std::setiosflags (std::ios::left) | |
1098 << std::setw (param.parameter_length); | |
1099 } | |
1100 break; | |
1101 | |
1102 default: | |
1103 os << std::setiosflags (std::ios::left) << std::setw (param.parameter_length); | |
1104 param_buf << std::setiosflags (std::ios::left) << std::setw (param.parameter_length); | |
1105 } | |
1106 | |
1107 if (param.command == 's' && param.modifier == 'c') | |
1108 { | |
1109 int a, b; | |
1110 | |
1111 if (param.modifier == 'c') | |
1112 { | |
1113 a = param.first_parameter_length - param.balance; | |
1114 a = (a < 0 ? 0 : a); | |
1115 b = param.parameter_length - a - param.text . length (); | |
1116 b = (b < 0 ? 0 : b); | |
1117 os << std::setiosflags (std::ios::left) << std::setw (a) | |
1118 << "" << std::resetiosflags (std::ios::left) << param.text | |
1119 << std::setiosflags (std::ios::left) | |
1120 << std::setw (b) << "" | |
1121 << std::resetiosflags (std::ios::left); | |
1122 param_buf << std::setiosflags (std::ios::left) << std::setw (a) | |
1123 << "" << std::resetiosflags (std::ios::left) << param.line | |
1124 << std::setiosflags (std::ios::left) | |
1125 << std::setw (b) << "" | |
1126 << std::resetiosflags (std::ios::left); | |
1127 } | |
1128 } | |
1129 else | |
1130 { | |
1131 os << param.text; | |
1132 param_buf << param.line; | |
1133 } | |
1134 os << std::resetiosflags (std::ios::left) | |
1135 << std::resetiosflags (std::ios::right); | |
1136 param_buf << std::resetiosflags (std::ios::left) | |
1137 << std::resetiosflags (std::ios::right); | |
1138 i++; | |
1139 } | |
1140 else | |
2975 | 1141 { |
7336 | 1142 os << param.text; |
1143 param_buf << param.line; | |
1144 i++; | |
2975 | 1145 } |
195 | 1146 } |
7336 | 1147 |
1148 os << param_buf.str (); | |
195 | 1149 } |
1150 | |
7336 | 1151 // Calculate how much space needs to be reserved for the first part of |
1152 // the dimensions string. For example, | |
581 | 1153 // |
7336 | 1154 // mat is a 12x3 matrix |
1155 // ^^ => 2 columns | |
1156 | |
1157 static int | |
1158 dimensions_string_req_first_space (const dim_vector& dims, int print_dims) | |
593 | 1159 { |
7336 | 1160 int first_param_space = 0; |
1161 | |
1162 // Calculating dimensions. | |
1163 | |
1164 std::string dim_str = ""; | |
1165 std::stringstream ss; | |
1166 long dim = dims.length (); | |
1167 | |
1168 first_param_space = (first_param_space >= 1 ? first_param_space : 1); | |
1169 | |
1170 // Preparing dimension string. | |
1171 | |
1172 if ((dim <= print_dims || print_dims < 0) && print_dims != 0) | |
1173 { | |
1174 // Dimensions string must be printed like this: 2x3x4x2. | |
1175 | |
1176 if (dim == 0 || dim == 1) | |
1177 first_param_space = 1; // First parameter is 1. | |
1178 else | |
1179 { | |
1180 ss << dims (0); | |
1181 | |
1182 dim_str = ss.str (); | |
1183 first_param_space = dim_str.length (); | |
1184 } | |
1185 } | |
1186 else | |
593 | 1187 { |
7336 | 1188 // Printing dimension string as: a-D. |
1189 | |
1190 ss << dim; | |
1191 | |
1192 dim_str = ss.str (); | |
1193 first_param_space = dim_str.length (); | |
1194 } | |
1195 | |
1196 return first_param_space; | |
1197 } | |
1198 | |
1199 // Make the dimensions-string. For example: mat is a 2x3 matrix. | |
1200 // ^^^ | |
1201 // | |
1202 // FIXME -- why not just use the dim_vector::str () method? | |
1203 | |
1204 std::string | |
1205 make_dimensions_string (const dim_vector& dims, int print_dims) | |
1206 { | |
1207 // Calculating dimensions. | |
1208 | |
1209 std::string dim_str = ""; | |
1210 std::stringstream ss; | |
1211 long dim = dims.length (); | |
1212 | |
1213 // Preparing dimension string. | |
1214 | |
1215 if ((dim <= print_dims || print_dims < 0) && print_dims != 0) | |
1216 { | |
1217 // Only printing the dimension string as: axbxc... | |
1218 | |
1219 if (dim == 0) | |
1220 ss << "1x1"; | |
1221 else | |
1222 { | |
1223 for (int i = 0; i < dim; i++) | |
2294 | 1224 { |
7336 | 1225 if (i == 0) |
2294 | 1226 { |
7336 | 1227 if (dim == 1) |
1228 { | |
1229 // Looks like this is not going to happen in | |
1230 // Octave, but ... | |
1231 | |
1232 ss << "1x" << dims (i); | |
1233 } | |
2294 | 1234 else |
7336 | 1235 ss << dims (i); |
2294 | 1236 } |
7336 | 1237 else if (i < dim && dim != 1) |
1238 ss << "x" << dims (i); | |
2294 | 1239 } |
593 | 1240 } |
1241 } | |
1242 else | |
7336 | 1243 { |
1244 // Printing dimension string as: a-D. | |
1245 | |
1246 ss << dim << "-D"; | |
1247 } | |
1248 | |
1249 dim_str = ss.str (); | |
1250 | |
1251 return dim_str; | |
593 | 1252 } |
1253 | |
7336 | 1254 // Calculate how much space needs to be reserved for the |
1255 // dimensions string. For example, | |
1256 // | |
1257 // mat is a 12x3 matrix | |
1258 // ^^^^ => 4 columns | |
1259 // | |
1260 // FIXME -- why not just use the dim_vector::str () method? | |
5659 | 1261 |
1262 static int | |
7336 | 1263 dimensions_string_req_total_space (const dim_vector& dims, int print_dims) |
1264 { | |
1265 std::string dim_str = ""; | |
1266 std::stringstream ss; | |
1267 | |
1268 ss << make_dimensions_string (dims, print_dims); | |
1269 dim_str = ss.str (); | |
1270 | |
1271 return dim_str.length (); | |
1272 } | |
1273 | |
1274 static std::list<whos_parameter> | |
1275 parse_whos_line_format (const std::list<symbol_table::symbol_record>& symbols) | |
5659 | 1276 { |
7336 | 1277 // This method parses the string whos_line_format, and returns |
1278 // a parameter list, containing all information needed to print | |
1279 // the given attributtes of the symbols | |
1280 int idx; | |
1281 size_t format_len = Vwhos_line_format.length (); | |
1282 char garbage; | |
1283 std::list<whos_parameter> params; | |
1284 | |
1285 size_t bytes1; | |
1286 int elements1; | |
1287 | |
1288 std::string param_string = "abcenst"; | |
1289 Array<int> param_length (dim_vector (param_string.length (), 1)); | |
1290 Array<std::string> param_names (dim_vector (param_string.length (), 1)); | |
1291 size_t pos_a, pos_b, pos_c, pos_e, pos_n, pos_s, pos_t; | |
1292 | |
1293 pos_a = param_string.find ('a'); // Attributes | |
1294 pos_b = param_string.find ('b'); // Bytes | |
1295 pos_c = param_string.find ('c'); // Class | |
1296 pos_e = param_string.find ('e'); // Elements | |
1297 pos_n = param_string.find ('n'); // Name | |
1298 pos_s = param_string.find ('s'); // Size | |
1299 pos_t = param_string.find ('t'); // Type | |
1300 | |
1301 param_names(pos_a) = "Attr"; | |
1302 param_names(pos_b) = "Bytes"; | |
1303 param_names(pos_c) = "Class"; | |
1304 param_names(pos_e) = "Elements"; | |
1305 param_names(pos_n) = "Name"; | |
1306 param_names(pos_s) = "Size"; | |
1307 param_names(pos_t) = "Type"; | |
1308 | |
1309 for (size_t i = 0; i < param_string.length (); i++) | |
1310 param_length(i) = param_names(i) . length (); | |
1311 | |
1312 // Calculating necessary spacing for name column, | |
1313 // bytes column, elements column and class column | |
1314 | |
1315 for (std::list<symbol_table::symbol_record>::const_iterator p = symbols.begin (); | |
1316 p != symbols.end (); p++) | |
1317 { | |
1318 std::stringstream ss1, ss2; | |
1319 std::string str; | |
1320 | |
1321 str = p->name (); | |
1322 param_length(pos_n) = ((str.length () | |
1323 > static_cast<size_t> (param_length(pos_n))) | |
1324 ? str.length () : param_length(pos_n)); | |
1325 | |
1326 octave_value val = p->varval (); | |
1327 | |
1328 str = val.type_name (); | |
1329 param_length(pos_t) = ((str.length () | |
1330 > static_cast<size_t> (param_length(pos_t))) | |
1331 ? str.length () : param_length(pos_t)); | |
1332 | |
1333 elements1 = val.capacity (); | |
1334 ss1 << elements1; | |
1335 str = ss1.str (); | |
1336 param_length(pos_e) = ((str.length () | |
1337 > static_cast<size_t> (param_length(pos_e))) | |
1338 ? str.length () : param_length(pos_e)); | |
1339 | |
1340 bytes1 = val.byte_size (); | |
1341 ss2 << bytes1; | |
1342 str = ss2.str (); | |
1343 param_length(pos_b) = ((str.length () | |
1344 > static_cast<size_t> (param_length(pos_b))) | |
1345 ? str.length () : param_length (pos_b)); | |
1346 } | |
1347 | |
1348 idx = 0; | |
1349 while (static_cast<size_t> (idx) < format_len) | |
1350 { | |
1351 whos_parameter param; | |
1352 param.command = '\0'; | |
1353 | |
1354 if (Vwhos_line_format[idx] == '%') | |
1355 { | |
1356 bool error_encountered = false; | |
1357 param.modifier = 'r'; | |
1358 param.parameter_length = 0; | |
1359 param.dimensions = 8; | |
1360 | |
1361 int a = 0, b = -1, c = 8, balance = 1; | |
1362 unsigned int items; | |
1363 size_t pos; | |
1364 std::string cmd; | |
1365 | |
1366 // Parse one command from whos_line_format | |
1367 cmd = Vwhos_line_format.substr (idx, Vwhos_line_format.length ()); | |
1368 pos = cmd.find (';'); | |
1369 if (pos != NPOS) | |
1370 cmd = cmd.substr (0, pos+1); | |
1371 else | |
1372 error ("parameter without ; in whos_line_format"); | |
1373 | |
1374 idx += cmd.length (); | |
1375 | |
1376 // FIXME -- use iostream functions instead of sscanf! | |
1377 | |
1378 if (cmd.find_first_of ("crl") != 1) | |
1379 items = sscanf (cmd.c_str (), "%c%c:%d:%d:%d:%d;", | |
1380 &garbage, ¶m.command, &a, &b, &c, &balance); | |
1381 else | |
1382 items = sscanf (cmd.c_str (), "%c%c%c:%d:%d:%d:%d;", | |
1383 &garbage, ¶m.modifier, ¶m.command, | |
1384 &a, &b, &c, &balance) - 1; | |
1385 | |
1386 if (items < 2) | |
1387 { | |
1388 error ("whos_line_format: parameter structure without command in whos_line_format"); | |
1389 error_encountered = true; | |
1390 } | |
1391 | |
1392 // Insert data into parameter | |
1393 param.first_parameter_length = 0; | |
1394 pos = param_string.find (param.command); | |
1395 if (pos != NPOS) | |
1396 { | |
1397 param.parameter_length = param_length(pos); | |
1398 param.text = param_names(pos); | |
1399 param.line.assign (param_names(pos).length (), '='); | |
1400 | |
1401 param.parameter_length = (a > param.parameter_length | |
1402 ? a : param.parameter_length); | |
1403 if (param.command == 's' && param.modifier == 'c' && b > 0) | |
1404 param.first_parameter_length = b; | |
1405 } | |
1406 else | |
1407 { | |
1408 error ("whos_line_format: '%c' is not a command", | |
1409 param.command); | |
1410 error_encountered = true; | |
1411 } | |
1412 | |
1413 if (param.command == 's') | |
1414 { | |
1415 // Have to calculate space needed for printing matrix dimensions | |
1416 // Space needed for Size column is hard to determine in prior, | |
1417 // because it depends on dimensions to be shown. That is why it is | |
1418 // recalculated for each Size-command | |
1419 int first, rest = 0, total; | |
1420 param.dimensions = c; | |
1421 first = param.first_parameter_length; | |
1422 total = param.parameter_length; | |
1423 | |
1424 for (std::list<symbol_table::symbol_record>::const_iterator p = symbols.begin (); | |
1425 p != symbols.end (); p++) | |
1426 { | |
1427 octave_value val = p->varval (); | |
1428 dim_vector dims = val.dims (); | |
1429 int first1 = dimensions_string_req_first_space (dims, param.dimensions); | |
1430 int total1 = dimensions_string_req_total_space (dims, param.dimensions); | |
1431 int rest1 = total1 - first1; | |
1432 rest = (rest1 > rest ? rest1 : rest); | |
1433 first = (first1 > first ? first1 : first); | |
1434 total = (total1 > total ? total1 : total); | |
1435 } | |
1436 | |
1437 if (param.modifier == 'c') | |
1438 { | |
1439 if (first < balance) | |
1440 first += balance - first; | |
1441 if (rest + balance < param.parameter_length) | |
1442 rest += param.parameter_length - rest - balance; | |
1443 | |
1444 param.parameter_length = first + rest; | |
1445 param.first_parameter_length = first; | |
1446 param.balance = balance; | |
1447 } | |
1448 else | |
1449 { | |
1450 param.parameter_length = total; | |
1451 param.first_parameter_length = 0; | |
1452 } | |
1453 } | |
1454 else if (param.modifier == 'c') | |
1455 { | |
1456 error ("whos_line_format: modifier 'c' not available for command '%c'", | |
1457 param.command); | |
1458 error_encountered = true; | |
1459 } | |
1460 | |
1461 // What happens if whos_line_format contains negative numbers | |
1462 // at param_length positions? | |
1463 param.balance = (b < 0 ? 0 : param.balance); | |
1464 param.first_parameter_length = (b < 0 ? 0 : | |
1465 param.first_parameter_length); | |
1466 param.parameter_length = (a < 0 | |
1467 ? 0 | |
1468 : (param.parameter_length | |
1469 < param_length(pos_s) | |
1470 ? param_length(pos_s) | |
1471 : param.parameter_length)); | |
1472 | |
1473 // Parameter will not be pushed into parameter list if ... | |
1474 if (! error_encountered) | |
1475 params.push_back (param); | |
1476 } | |
1477 else | |
1478 { | |
1479 // Text string, to be printed as it is ... | |
1480 std::string text; | |
1481 size_t pos; | |
1482 text = Vwhos_line_format.substr (idx, Vwhos_line_format.length ()); | |
1483 pos = text.find ('%'); | |
1484 if (pos != NPOS) | |
1485 text = text.substr (0, pos); | |
1486 | |
1487 // Push parameter into list ... | |
1488 idx += text.length (); | |
1489 param.text=text; | |
1490 param.line.assign (text.length(), ' '); | |
1491 params.push_back (param); | |
1492 } | |
1493 } | |
1494 | |
1495 return params; | |
1496 } | |
1497 | |
1498 void | |
1499 print_symbol_info_line (std::ostream& os, | |
1500 const symbol_table::symbol_record& sr, | |
1501 std::list<whos_parameter>& params) | |
1502 { | |
1503 octave_value val = sr.varval (); | |
1504 dim_vector dims = val.dims (); | |
1505 | |
1506 std::list<whos_parameter>::iterator i = params.begin (); | |
1507 | |
1508 while (i != params.end ()) | |
1509 { | |
1510 whos_parameter param = *i; | |
1511 | |
1512 if (param.command != '\0') | |
1513 { | |
1514 // Do the actual printing. | |
1515 | |
1516 switch (param.modifier) | |
1517 { | |
1518 case 'l': | |
1519 os << std::setiosflags (std::ios::left) | |
1520 << std::setw (param.parameter_length); | |
1521 break; | |
1522 | |
1523 case 'r': | |
1524 os << std::setiosflags (std::ios::right) | |
1525 << std::setw (param.parameter_length); | |
1526 break; | |
1527 | |
1528 case 'c': | |
1529 if (param.command == 's') | |
1530 { | |
1531 int front = param.first_parameter_length | |
1532 - dimensions_string_req_first_space (dims, param.dimensions); | |
1533 int back = param.parameter_length | |
1534 - dimensions_string_req_total_space (dims, param.dimensions) | |
1535 - front; | |
1536 front = (front > 0) ? front : 0; | |
1537 back = (back > 0) ? back : 0; | |
1538 | |
1539 os << std::setiosflags (std::ios::left) | |
1540 << std::setw (front) | |
1541 << "" | |
1542 << std::resetiosflags (std::ios::left) | |
1543 << make_dimensions_string (dims, param.dimensions) | |
1544 << std::setiosflags (std::ios::left) | |
1545 << std::setw (back) | |
1546 << "" | |
1547 << std::resetiosflags (std::ios::left); | |
1548 } | |
1549 else | |
1550 { | |
1551 os << std::setiosflags (std::ios::left) | |
1552 << std::setw (param.parameter_length); | |
1553 } | |
1554 break; | |
1555 | |
1556 default: | |
1557 error ("whos_line_format: modifier `%c' unknown", | |
1558 param.modifier); | |
1559 | |
1560 os << std::setiosflags (std::ios::right) | |
1561 << std::setw (param.parameter_length); | |
1562 } | |
1563 | |
1564 switch (param.command) | |
1565 { | |
1566 case 'a': | |
1567 { | |
1568 char tmp[5]; | |
1569 | |
1570 tmp[0] = (sr.is_automatic () ? 'a' : ' '); | |
1571 tmp[1] = (sr.is_formal () ? 'f' : ' '); | |
1572 tmp[2] = (sr.is_global () ? 'g' : ' '); | |
1573 tmp[3] = (sr.is_persistent () ? 'p' : ' '); | |
1574 tmp[4] = 0; | |
1575 | |
1576 os << tmp; | |
1577 } | |
1578 break; | |
1579 | |
1580 case 'b': | |
1581 os << val.byte_size (); | |
1582 break; | |
1583 | |
1584 case 'c': | |
1585 os << val.class_name (); | |
1586 break; | |
1587 | |
1588 case 'e': | |
1589 os << val.capacity (); | |
1590 break; | |
1591 | |
1592 case 'n': | |
1593 os << sr.name (); | |
1594 break; | |
1595 | |
1596 case 's': | |
1597 if (param.modifier != 'c') | |
1598 os << make_dimensions_string (dims, param.dimensions); | |
1599 break; | |
1600 | |
1601 case 't': | |
1602 os << val.type_name (); | |
1603 break; | |
1604 | |
1605 default: | |
1606 error ("whos_line_format: command `%c' unknown", param.command); | |
1607 } | |
1608 | |
1609 os << std::resetiosflags (std::ios::left) | |
1610 << std::resetiosflags (std::ios::right); | |
1611 i++; | |
1612 } | |
1613 else | |
1614 { | |
1615 os << param.text; | |
1616 i++; | |
1617 } | |
1618 } | |
5659 | 1619 } |
1620 | |
4435 | 1621 static octave_value |
7336 | 1622 do_who (int argc, const string_vector& argv, bool return_list, |
1623 bool verbose = false) | |
529 | 1624 { |
4435 | 1625 octave_value retval; |
529 | 1626 |
3523 | 1627 std::string my_name = argv[0]; |
584 | 1628 |
7336 | 1629 bool global_only = false; |
1630 | |
1857 | 1631 int i; |
1632 for (i = 1; i < argc; i++) | |
529 | 1633 { |
7336 | 1634 if (argv[i] == "-regexp" || argv[i] == "-file") |
529 | 1635 { |
7336 | 1636 error ("%s: `%s' option not implemented", my_name.c_str (), |
1637 argv[i].c_str ()); | |
1638 | |
1639 return retval; | |
529 | 1640 } |
7336 | 1641 else if (argv[i] == "global") |
1642 global_only = true; | |
1755 | 1643 else if (argv[i][0] == '-') |
1644 warning ("%s: unrecognized option `%s'", my_name.c_str (), | |
1645 argv[i].c_str ()); | |
529 | 1646 else |
867 | 1647 break; |
529 | 1648 } |
1649 | |
7336 | 1650 int npats = argc - i; |
1651 string_vector pats (npats > 0 ? npats : 1); | |
1652 if (npats > 0) | |
3248 | 1653 { |
7336 | 1654 for (int j = 0; j < npats; j++) |
1655 pats[j] = argv[i+j]; | |
3248 | 1656 } |
7336 | 1657 else |
1658 pats[0] = "*"; | |
1659 | |
1660 symbol_table::scope_id scope = global_only | |
1661 ? symbol_table::global_scope () : symbol_table::current_scope (); | |
1662 | |
1663 std::list<symbol_table::symbol_record> symbols | |
1664 = symbol_table::glob_variables (pats, scope); | |
1665 | |
1666 size_t symbols_len = symbols.size (); | |
529 | 1667 |
4435 | 1668 if (return_list) |
529 | 1669 { |
7336 | 1670 if (verbose) |
4435 | 1671 { |
5659 | 1672 Array<octave_value> name_info (symbols_len, 1); |
1673 Array<octave_value> size_info (symbols_len, 1); | |
1674 Array<octave_value> bytes_info (symbols_len, 1); | |
1675 Array<octave_value> class_info (symbols_len, 1); | |
1676 Array<octave_value> global_info (symbols_len, 1); | |
1677 Array<octave_value> sparse_info (symbols_len, 1); | |
1678 Array<octave_value> complex_info (symbols_len, 1); | |
1679 Array<octave_value> nesting_info (symbols_len, 1); | |
1680 | |
7336 | 1681 std::list<symbol_table::symbol_record>::const_iterator p |
1682 = symbols.begin (); | |
1683 | |
1684 for (size_t j = 0; j < symbols_len; j++) | |
5659 | 1685 { |
7336 | 1686 const symbol_table::symbol_record& sr = *p++; |
5659 | 1687 |
1688 Octave_map ni; | |
1689 | |
1690 std::string caller_function_name; | |
5743 | 1691 |
1692 octave_function *caller = octave_call_stack::caller (); | |
1693 if (caller) | |
1694 caller_function_name = caller->name (); | |
5659 | 1695 |
1696 ni.assign ("function", caller_function_name); | |
1697 ni.assign ("level", 1); | |
1698 | |
7336 | 1699 name_info(j) = sr.name (); |
1700 global_info(j) = sr.is_global (); | |
1701 | |
1702 octave_value val = sr.varval (); | |
1703 | |
1704 size_info(j) = val.size (); | |
1705 bytes_info(j) = val.byte_size (); | |
1706 class_info(j) = val.class_name (); | |
1707 sparse_info(j) = val.is_sparse_type (); | |
1708 complex_info(j) = val.is_complex_type (); | |
5659 | 1709 nesting_info(j) = ni; |
1710 } | |
1711 | |
1712 Octave_map info; | |
1713 | |
1714 info.assign ("name", name_info); | |
1715 info.assign ("size", size_info); | |
1716 info.assign ("bytes", bytes_info); | |
1717 info.assign ("class", class_info); | |
1718 info.assign ("global", global_info); | |
1719 info.assign ("sparse", sparse_info); | |
1720 info.assign ("complex", complex_info); | |
1721 info.assign ("nesting", nesting_info); | |
1722 | |
1723 retval = info; | |
4435 | 1724 } |
1725 else | |
5659 | 1726 { |
1727 string_vector names; | |
1728 | |
1729 if (symbols_len > 0) | |
1730 { | |
1731 names.resize (symbols_len); | |
1732 | |
7336 | 1733 std::list<symbol_table::symbol_record>::const_iterator p |
1734 = symbols.begin (); | |
1735 | |
1736 for (size_t j = 0; j < symbols_len; j++) | |
1737 { | |
1738 names[j] = p->name (); | |
1739 p++; | |
1740 } | |
5659 | 1741 } |
1742 | |
1743 retval = Cell (names); | |
1744 } | |
529 | 1745 } |
7336 | 1746 else if (symbols_len > 0) |
529 | 1747 { |
7336 | 1748 if (global_only) |
1749 octave_stdout << "Global variables:\n\n"; | |
1750 else | |
1751 octave_stdout << "Variables in the current scope:\n\n"; | |
1752 | |
1753 if (verbose) | |
4435 | 1754 { |
7336 | 1755 size_t bytes = 0; |
1756 size_t elements = 0; | |
1757 | |
1758 std::list<whos_parameter> params; | |
1759 | |
1760 params = parse_whos_line_format (symbols); | |
1761 | |
1762 print_descriptor (octave_stdout, params); | |
1763 | |
1764 octave_stdout << "\n"; | |
1765 | |
1766 for (std::list<symbol_table::symbol_record>::const_iterator p = symbols.begin (); | |
1767 p != symbols.end (); p++) | |
1768 { | |
1769 print_symbol_info_line (octave_stdout, *p, params); | |
1770 octave_value val = p->varval (); | |
1771 elements += val.capacity (); | |
1772 bytes += val.byte_size (); | |
1773 } | |
1774 | |
1775 octave_stdout << "\nTotal is " << elements | |
1776 << (elements == 1 ? " element" : " elements") | |
1777 << " using " << bytes | |
1778 << (bytes == 1 ? " byte" : " bytes") << "\n"; | |
4435 | 1779 } |
7336 | 1780 else |
4435 | 1781 { |
7336 | 1782 string_vector names (symbols_len); |
1783 | |
1784 std::list<symbol_table::symbol_record>::const_iterator p | |
1785 = symbols.begin (); | |
1786 | |
1787 for (size_t j = 0; j < symbols_len; j++) | |
1788 { | |
1789 names[j] = p->name (); | |
1790 p++; | |
1791 } | |
1792 | |
1793 names.list_in_columns (octave_stdout); | |
4435 | 1794 } |
1795 | |
7336 | 1796 octave_stdout << "\n"; |
529 | 1797 } |
1798 | |
581 | 1799 return retval; |
1800 } | |
1801 | |
4435 | 1802 DEFCMD (who, args, nargout, |
3361 | 1803 "-*- texinfo -*-\n\ |
1804 @deffn {Command} who options pattern @dots{}\n\ | |
1805 @deffnx {Command} whos options pattern @dots{}\n\ | |
1806 List currently defined symbols matching the given patterns. The\n\ | |
1807 following are valid options. They may be shortened to one character but\n\ | |
1808 may not be combined.\n\ | |
1809 \n\ | |
1810 @table @code\n\ | |
1811 @item -all\n\ | |
1812 List all currently defined symbols.\n\ | |
1813 \n\ | |
1814 @item -builtins\n\ | |
5794 | 1815 List built-in functions. This includes all currently\n\ |
3361 | 1816 compiled function files, but does not include all function files that\n\ |
5814 | 1817 are in the search path.\n\ |
581 | 1818 \n\ |
3361 | 1819 @item -functions\n\ |
1820 List user-defined functions.\n\ | |
1821 \n\ | |
1822 @item -long\n\ | |
1823 Print a long listing including the type and dimensions of any symbols.\n\ | |
1824 The symbols in the first column of output indicate whether it is\n\ | |
1825 possible to redefine the symbol, and whether it is possible for it to be\n\ | |
1826 cleared.\n\ | |
1827 \n\ | |
1828 @item -variables\n\ | |
1829 List user-defined variables.\n\ | |
1830 @end table\n\ | |
1831 \n\ | |
1832 Valid patterns are the same as described for the @code{clear} command\n\ | |
1833 above. If no patterns are supplied, all symbols from the given category\n\ | |
1834 are listed. By default, only user defined functions and variables\n\ | |
1835 visible in the local scope are displayed.\n\ | |
1836 \n\ | |
1837 The command @kbd{whos} is equivalent to @kbd{who -long}.\n\ | |
1838 @end deffn") | |
581 | 1839 { |
4435 | 1840 octave_value retval; |
581 | 1841 |
4435 | 1842 if (nargout < 2) |
1843 { | |
1844 int argc = args.length () + 1; | |
1845 | |
1846 string_vector argv = args.make_argv ("who"); | |
1755 | 1847 |
7336 | 1848 if (! error_state) |
1849 retval = do_who (argc, argv, nargout == 1); | |
4435 | 1850 } |
1851 else | |
5823 | 1852 print_usage (); |
581 | 1853 |
529 | 1854 return retval; |
1855 } | |
1856 | |
4435 | 1857 DEFCMD (whos, args, nargout, |
3458 | 1858 "-*- texinfo -*-\n\ |
1859 @deffn {Command} whos options pattern @dots{}\n\ | |
1860 See who.\n\ | |
1861 @end deffn") | |
581 | 1862 { |
4435 | 1863 octave_value retval; |
712 | 1864 |
4435 | 1865 if (nargout < 2) |
1866 { | |
7336 | 1867 int argc = args.length () + 1; |
1868 | |
1869 string_vector argv = args.make_argv ("whos"); | |
1870 | |
1871 if (! error_state) | |
1872 retval = do_who (argc, argv, nargout == 1, true); | |
4435 | 1873 } |
1874 else | |
5823 | 1875 print_usage (); |
581 | 1876 |
1877 return retval; | |
1878 } | |
1879 | |
593 | 1880 // Defining variables. |
1881 | |
1162 | 1882 void |
2856 | 1883 bind_ans (const octave_value& val, bool print) |
1162 | 1884 { |
7336 | 1885 static std::string ans = "ans"; |
1162 | 1886 |
2978 | 1887 if (val.is_defined ()) |
1888 { | |
7531
c9a476b1e664
correctly set ans for cs-lists and simplify printing them
John W. Eaton <jwe@octave.org>
parents:
7347
diff
changeset
|
1889 if (val.is_cs_list ()) |
c9a476b1e664
correctly set ans for cs-lists and simplify printing them
John W. Eaton <jwe@octave.org>
parents:
7347
diff
changeset
|
1890 { |
c9a476b1e664
correctly set ans for cs-lists and simplify printing them
John W. Eaton <jwe@octave.org>
parents:
7347
diff
changeset
|
1891 octave_value_list lst = val.list_value (); |
c9a476b1e664
correctly set ans for cs-lists and simplify printing them
John W. Eaton <jwe@octave.org>
parents:
7347
diff
changeset
|
1892 |
c9a476b1e664
correctly set ans for cs-lists and simplify printing them
John W. Eaton <jwe@octave.org>
parents:
7347
diff
changeset
|
1893 for (octave_idx_type i = 0; i < lst.length (); i++) |
c9a476b1e664
correctly set ans for cs-lists and simplify printing them
John W. Eaton <jwe@octave.org>
parents:
7347
diff
changeset
|
1894 bind_ans (lst(i), print); |
c9a476b1e664
correctly set ans for cs-lists and simplify printing them
John W. Eaton <jwe@octave.org>
parents:
7347
diff
changeset
|
1895 } |
c9a476b1e664
correctly set ans for cs-lists and simplify printing them
John W. Eaton <jwe@octave.org>
parents:
7347
diff
changeset
|
1896 else |
c9a476b1e664
correctly set ans for cs-lists and simplify printing them
John W. Eaton <jwe@octave.org>
parents:
7347
diff
changeset
|
1897 { |
c9a476b1e664
correctly set ans for cs-lists and simplify printing them
John W. Eaton <jwe@octave.org>
parents:
7347
diff
changeset
|
1898 symbol_table::varref (ans) = val; |
c9a476b1e664
correctly set ans for cs-lists and simplify printing them
John W. Eaton <jwe@octave.org>
parents:
7347
diff
changeset
|
1899 |
c9a476b1e664
correctly set ans for cs-lists and simplify printing them
John W. Eaton <jwe@octave.org>
parents:
7347
diff
changeset
|
1900 if (print) |
c9a476b1e664
correctly set ans for cs-lists and simplify printing them
John W. Eaton <jwe@octave.org>
parents:
7347
diff
changeset
|
1901 val.print_with_name (octave_stdout, ans); |
c9a476b1e664
correctly set ans for cs-lists and simplify printing them
John W. Eaton <jwe@octave.org>
parents:
7347
diff
changeset
|
1902 } |
2978 | 1903 } |
1162 | 1904 } |
1905 | |
593 | 1906 void |
5794 | 1907 bind_internal_variable (const std::string& fname, const octave_value& val) |
593 | 1908 { |
5794 | 1909 octave_value_list args; |
1910 | |
1911 args(0) = val; | |
1912 | |
1913 feval (fname, args, 0); | |
529 | 1914 } |
1915 | |
4319 | 1916 void |
7336 | 1917 mlock (void) |
4319 | 1918 { |
7336 | 1919 octave_function *fcn = octave_call_stack::caller (); |
1920 | |
1921 if (fcn) | |
1922 fcn->lock (); | |
1923 else | |
1924 error ("mlock: invalid use outside a function"); | |
4319 | 1925 } |
1926 | |
1927 void | |
1928 munlock (const std::string& nm) | |
1929 { | |
7336 | 1930 octave_value val = symbol_table::find_function (nm); |
1931 | |
1932 if (val.is_defined ()) | |
1933 { | |
1934 octave_function *fcn = val.function_value (); | |
1935 | |
1936 if (fcn) | |
1937 fcn->unlock (); | |
1938 } | |
4319 | 1939 } |
1940 | |
1941 bool | |
1942 mislocked (const std::string& nm) | |
1943 { | |
7336 | 1944 bool retval = false; |
1945 | |
1946 octave_value val = symbol_table::find_function (nm); | |
1947 | |
1948 if (val.is_defined ()) | |
1949 { | |
1950 octave_function *fcn = val.function_value (); | |
1951 | |
1952 if (fcn) | |
1953 retval = fcn->islocked (); | |
1954 } | |
1955 | |
1956 return retval; | |
4319 | 1957 } |
1958 | |
1959 DEFCMD (mlock, args, , | |
1960 "-*- texinfo -*-\n\ | |
4526 | 1961 @deftypefn {Built-in Function} {} mlock (@var{name})\n\ |
7336 | 1962 Lock the current function into memory so that it can't be cleared.\n\ |
5642 | 1963 @seealso{munlock, mislocked, persistent}\n\ |
1964 @end deftypefn") | |
4319 | 1965 { |
1966 octave_value_list retval; | |
1967 | |
7336 | 1968 if (args.length () == 0) |
1969 mlock (); | |
4319 | 1970 else |
5823 | 1971 print_usage (); |
4319 | 1972 |
1973 return retval; | |
1974 } | |
1975 | |
1976 DEFCMD (munlock, args, , | |
1977 "-*- texinfo -*-\n\ | |
1978 @deftypefn {Built-in Function} {} munlock (@var{fcn})\n\ | |
1979 Unlock the named function. If no function is named\n\ | |
1980 then unlock the current function.\n\ | |
5642 | 1981 @seealso{mlock, mislocked, persistent}\n\ |
1982 @end deftypefn") | |
4319 | 1983 { |
1984 octave_value_list retval; | |
1985 | |
1986 if (args.length() == 1) | |
1987 { | |
1988 std::string name = args(0).string_value (); | |
1989 | |
1990 if (! error_state) | |
1991 munlock (name); | |
1992 else | |
1993 error ("munlock: expecting argument to be a function name"); | |
1994 } | |
1995 else if (args.length () == 0) | |
1996 { | |
7336 | 1997 octave_function *fcn = octave_call_stack::caller (); |
5743 | 1998 |
1999 if (fcn) | |
7336 | 2000 fcn->unlock (); |
4319 | 2001 else |
2002 error ("munlock: invalid use outside a function"); | |
2003 } | |
2004 else | |
5823 | 2005 print_usage (); |
4319 | 2006 |
2007 return retval; | |
2008 } | |
2009 | |
2010 | |
2011 DEFCMD (mislocked, args, , | |
2012 "-*- texinfo -*-\n\ | |
2013 @deftypefn {Built-in Function} {} mislocked (@var{fcn})\n\ | |
2014 Return true if the named function is locked. If no function is named\n\ | |
2015 then return true if the current function is locked.\n\ | |
5642 | 2016 @seealso{mlock, munlock, persistent}\n\ |
2017 @end deftypefn") | |
4319 | 2018 { |
2019 octave_value retval; | |
2020 | |
2021 if (args.length() == 1) | |
2022 { | |
2023 std::string name = args(0).string_value (); | |
2024 | |
2025 if (! error_state) | |
2026 retval = mislocked (name); | |
2027 else | |
2028 error ("mislocked: expecting argument to be a function name"); | |
2029 } | |
2030 else if (args.length () == 0) | |
2031 { | |
7336 | 2032 octave_function *fcn = octave_call_stack::caller (); |
5743 | 2033 |
2034 if (fcn) | |
7336 | 2035 retval = fcn->islocked (); |
4319 | 2036 else |
2037 error ("mislocked: invalid use outside a function"); | |
2038 } | |
2039 else | |
5823 | 2040 print_usage (); |
4319 | 2041 |
2042 return retval; | |
2043 } | |
2044 | |
593 | 2045 // Deleting names from the symbol tables. |
2046 | |
3681 | 2047 static inline bool |
4009 | 2048 name_matches_any_pattern (const std::string& nm, |
2049 const string_vector& argv, int argc, int idx) | |
3681 | 2050 { |
2051 bool retval = false; | |
2052 | |
2053 for (int k = idx; k < argc; k++) | |
2054 { | |
2055 std::string patstr = argv[k]; | |
2056 | |
2057 if (! patstr.empty ()) | |
2058 { | |
2059 glob_match pattern (patstr); | |
2060 | |
2061 if (pattern.match (nm)) | |
2062 { | |
2063 retval = true; | |
2064 break; | |
2065 } | |
2066 } | |
2067 } | |
2068 | |
2069 return retval; | |
2070 } | |
2071 | |
4009 | 2072 static inline void |
2073 maybe_warn_exclusive (bool exclusive) | |
2074 { | |
2075 if (exclusive) | |
2076 warning ("clear: ignoring --exclusive option"); | |
2077 } | |
2078 | |
7336 | 2079 static void |
4009 | 2080 do_clear_functions (const string_vector& argv, int argc, int idx, |
2081 bool exclusive = false) | |
2082 { | |
2083 if (idx == argc) | |
7336 | 2084 symbol_table::clear_functions (); |
4009 | 2085 else |
2086 { | |
2087 if (exclusive) | |
2088 { | |
7336 | 2089 string_vector fcns = symbol_table::user_function_names (); |
4009 | 2090 |
2091 int fcount = fcns.length (); | |
2092 | |
2093 for (int i = 0; i < fcount; i++) | |
2094 { | |
2095 std::string nm = fcns[i]; | |
2096 | |
2097 if (! name_matches_any_pattern (nm, argv, argc, idx)) | |
7336 | 2098 symbol_table::clear_function (nm); |
4009 | 2099 } |
2100 } | |
2101 else | |
2102 { | |
2103 while (idx < argc) | |
7336 | 2104 symbol_table::clear_function_pattern (argv[idx++]); |
4009 | 2105 } |
2106 } | |
2107 } | |
2108 | |
7336 | 2109 static void |
4009 | 2110 do_clear_globals (const string_vector& argv, int argc, int idx, |
2111 bool exclusive = false) | |
2112 { | |
2113 if (idx == argc) | |
7336 | 2114 symbol_table::clear_variables(symbol_table::global_scope ()); |
4009 | 2115 else |
2116 { | |
2117 if (exclusive) | |
2118 { | |
7336 | 2119 string_vector gvars |
2120 = symbol_table::variable_names (symbol_table::global_scope ()); | |
4009 | 2121 |
2122 int gcount = gvars.length (); | |
2123 | |
2124 for (int i = 0; i < gcount; i++) | |
2125 { | |
2126 std::string nm = gvars[i]; | |
2127 | |
2128 if (! name_matches_any_pattern (nm, argv, argc, idx)) | |
7336 | 2129 symbol_table::clear_global (nm); |
4009 | 2130 } |
2131 } | |
2132 else | |
2133 { | |
2134 while (idx < argc) | |
7336 | 2135 symbol_table::clear_global_pattern (argv[idx++]); |
4009 | 2136 } |
2137 } | |
2138 } | |
2139 | |
7336 | 2140 static void |
4009 | 2141 do_clear_variables (const string_vector& argv, int argc, int idx, |
2142 bool exclusive = false) | |
2143 { | |
2144 if (idx == argc) | |
7336 | 2145 symbol_table::clear_variables (); |
4009 | 2146 else |
2147 { | |
2148 if (exclusive) | |
2149 { | |
7336 | 2150 string_vector lvars = symbol_table::variable_names (); |
4009 | 2151 |
2152 int lcount = lvars.length (); | |
2153 | |
2154 for (int i = 0; i < lcount; i++) | |
2155 { | |
2156 std::string nm = lvars[i]; | |
2157 | |
2158 if (! name_matches_any_pattern (nm, argv, argc, idx)) | |
7336 | 2159 symbol_table::clear_variable (nm); |
4009 | 2160 } |
2161 } | |
2162 else | |
2163 { | |
2164 while (idx < argc) | |
7336 | 2165 symbol_table::clear_variable_pattern (argv[idx++]); |
4009 | 2166 } |
2167 } | |
2168 } | |
2169 | |
7336 | 2170 static void |
4009 | 2171 do_clear_symbols (const string_vector& argv, int argc, int idx, |
2172 bool exclusive = false) | |
2173 { | |
2174 if (idx == argc) | |
7336 | 2175 symbol_table::clear_variables (); |
4009 | 2176 else |
2177 { | |
2178 if (exclusive) | |
2179 { | |
5775 | 2180 // FIXME -- is this really what we want, or do we |
4009 | 2181 // somehow want to only clear the functions that are not |
2182 // shadowed by local variables? It seems that would be a | |
2183 // bit harder to do. | |
2184 | |
2185 do_clear_variables (argv, argc, idx, exclusive); | |
2186 do_clear_functions (argv, argc, idx, exclusive); | |
2187 } | |
2188 else | |
2189 { | |
2190 while (idx < argc) | |
7336 | 2191 symbol_table::clear_symbol_pattern (argv[idx++]); |
4009 | 2192 } |
2193 } | |
2194 } | |
2195 | |
2196 static void | |
2197 do_matlab_compatible_clear (const string_vector& argv, int argc, int idx) | |
2198 { | |
2199 // This is supposed to be mostly Matlab compatible. | |
2200 | |
2201 for (; idx < argc; idx++) | |
2202 { | |
7336 | 2203 if (argv[idx] == "all" |
2204 && ! symbol_table::is_local_variable ("all")) | |
4009 | 2205 { |
7336 | 2206 symbol_table::clear_all (); |
4009 | 2207 } |
7336 | 2208 else if (argv[idx] == "functions" |
2209 && ! symbol_table::is_local_variable ("functions")) | |
4009 | 2210 { |
2211 do_clear_functions (argv, argc, ++idx); | |
2212 } | |
7336 | 2213 else if (argv[idx] == "global" |
2214 && ! symbol_table::is_local_variable ("global")) | |
4009 | 2215 { |
2216 do_clear_globals (argv, argc, ++idx); | |
2217 } | |
7336 | 2218 else if (argv[idx] == "variables" |
2219 && ! symbol_table::is_local_variable ("variables")) | |
4009 | 2220 { |
7336 | 2221 symbol_table::clear_variables (); |
4009 | 2222 } |
2223 else | |
2224 { | |
7336 | 2225 symbol_table::clear_symbol_pattern (argv[idx]); |
4009 | 2226 } |
2227 } | |
2228 } | |
2229 | |
2230 #define CLEAR_OPTION_ERROR(cond) \ | |
2231 do \ | |
2232 { \ | |
2233 if (cond) \ | |
2234 { \ | |
5823 | 2235 print_usage (); \ |
4009 | 2236 return retval; \ |
2237 } \ | |
2238 } \ | |
2239 while (0) | |
2240 | |
4208 | 2241 DEFCMD (clear, args, , |
3361 | 2242 "-*- texinfo -*-\n\ |
7347 | 2243 @deffn {Command} clear [options] pattern @dots{}\n\ |
3361 | 2244 Delete the names matching the given patterns from the symbol table. The\n\ |
2245 pattern may contain the following special characters:\n\ | |
4016 | 2246 \n\ |
3361 | 2247 @table @code\n\ |
2248 @item ?\n\ | |
2249 Match any single character.\n\ | |
668 | 2250 \n\ |
3361 | 2251 @item *\n\ |
2252 Match zero or more characters.\n\ | |
2253 \n\ | |
2254 @item [ @var{list} ]\n\ | |
2255 Match the list of characters specified by @var{list}. If the first\n\ | |
2256 character is @code{!} or @code{^}, match all characters except those\n\ | |
2257 specified by @var{list}. For example, the pattern @samp{[a-zA-Z]} will\n\ | |
2258 match all lower and upper case alphabetic characters.\n\ | |
2259 @end table\n\ | |
2260 \n\ | |
2261 For example, the command\n\ | |
593 | 2262 \n\ |
3361 | 2263 @example\n\ |
2264 clear foo b*r\n\ | |
2265 @end example\n\ | |
2266 \n\ | |
2267 @noindent\n\ | |
2268 clears the name @code{foo} and all names that begin with the letter\n\ | |
2269 @code{b} and end with the letter @code{r}.\n\ | |
668 | 2270 \n\ |
3361 | 2271 If @code{clear} is called without any arguments, all user-defined\n\ |
2272 variables (local and global) are cleared from the symbol table. If\n\ | |
2273 @code{clear} is called with at least one argument, only the visible\n\ | |
2274 names matching the arguments are cleared. For example, suppose you have\n\ | |
2275 defined a function @code{foo}, and then hidden it by performing the\n\ | |
2276 assignment @code{foo = 2}. Executing the command @kbd{clear foo} once\n\ | |
2277 will clear the variable definition and restore the definition of\n\ | |
2278 @code{foo} as a function. Executing @kbd{clear foo} a second time will\n\ | |
2279 clear the function definition.\n\ | |
2280 \n\ | |
7347 | 2281 The following options are available in both long and short form\n\ |
2282 @table @code\n\ | |
2283 @item -all, -a\n\ | |
2284 Clears all local and global user-defined variables and all functions\n\ | |
2285 from the symbol table.\n\ | |
2286 \n\ | |
2287 @item -exclusive, -x\n\ | |
2288 Clears the variables that don't match the following pattern.\n\ | |
2289 \n\ | |
2290 @item -functions, -f\n\ | |
2291 Clears the function names and the built-in symbols names.\n\ | |
2292 @item -global, -g\n\ | |
2293 Clears the global symbol names.\n\ | |
2294 @item -variables, -v\n\ | |
2295 Clears the local variable names.\n\ | |
2296 @end table\n\ | |
2297 With the execption of @code{exclusive}, all long options can be used \n\ | |
2298 without the dash as well.\n\ | |
3361 | 2299 @end deffn") |
529 | 2300 { |
2086 | 2301 octave_value_list retval; |
593 | 2302 |
1755 | 2303 int argc = args.length () + 1; |
593 | 2304 |
1968 | 2305 string_vector argv = args.make_argv ("clear"); |
1755 | 2306 |
4009 | 2307 if (! error_state) |
529 | 2308 { |
4009 | 2309 if (argc == 1) |
593 | 2310 { |
7336 | 2311 symbol_table::clear_variables (); |
3681 | 2312 } |
2313 else | |
2314 { | |
4009 | 2315 int idx = 0; |
2316 | |
2317 bool clear_all = false; | |
2318 bool clear_functions = false; | |
2319 bool clear_globals = false; | |
2320 bool clear_variables = false; | |
2321 bool exclusive = false; | |
2322 bool have_dash_option = false; | |
3681 | 2323 |
4009 | 2324 while (++idx < argc) |
2325 { | |
4010 | 2326 if (argv[idx] == "-all" || argv[idx] == "-a") |
593 | 2327 { |
4009 | 2328 CLEAR_OPTION_ERROR (have_dash_option && ! exclusive); |
3681 | 2329 |
4009 | 2330 have_dash_option = true; |
2331 clear_all = true; | |
2332 } | |
4010 | 2333 else if (argv[idx] == "-exclusive" || argv[idx] == "-x") |
4009 | 2334 { |
2335 have_dash_option = true; | |
2336 exclusive = true; | |
2337 } | |
4010 | 2338 else if (argv[idx] == "-functions" || argv[idx] == "-f") |
4009 | 2339 { |
2340 CLEAR_OPTION_ERROR (have_dash_option && ! exclusive); | |
3681 | 2341 |
4009 | 2342 have_dash_option = true; |
2343 clear_functions = true; | |
2344 } | |
4010 | 2345 else if (argv[idx] == "-global" || argv[idx] == "-g") |
4009 | 2346 { |
2347 CLEAR_OPTION_ERROR (have_dash_option && ! exclusive); | |
2348 | |
2349 have_dash_option = true; | |
2350 clear_globals = true; | |
2351 } | |
4010 | 2352 else if (argv[idx] == "-variables" || argv[idx] == "-v") |
4009 | 2353 { |
2354 CLEAR_OPTION_ERROR (have_dash_option && ! exclusive); | |
3681 | 2355 |
4009 | 2356 have_dash_option = true; |
2357 clear_variables = true; | |
2358 } | |
2359 else | |
2360 break; | |
2361 } | |
3681 | 2362 |
4224 | 2363 if (idx <= argc) |
4009 | 2364 { |
2365 if (! have_dash_option) | |
2366 { | |
2367 do_matlab_compatible_clear (argv, argc, idx); | |
2368 } | |
2369 else | |
2370 { | |
2371 if (clear_all) | |
3681 | 2372 { |
4009 | 2373 maybe_warn_exclusive (exclusive); |
3681 | 2374 |
4009 | 2375 if (++idx < argc) |
2376 warning | |
4010 | 2377 ("clear: ignoring extra arguments after -all"); |
3681 | 2378 |
7336 | 2379 symbol_table::clear_all (); |
4009 | 2380 } |
2381 else if (clear_functions) | |
2382 { | |
2383 do_clear_functions (argv, argc, idx, exclusive); | |
2384 } | |
2385 else if (clear_globals) | |
593 | 2386 { |
4009 | 2387 do_clear_globals (argv, argc, idx, exclusive); |
2388 } | |
2389 else if (clear_variables) | |
2390 { | |
2391 do_clear_variables (argv, argc, idx, exclusive); | |
2392 } | |
2393 else | |
2394 { | |
2395 do_clear_symbols (argv, argc, idx, exclusive); | |
593 | 2396 } |
2397 } | |
2398 } | |
2399 } | |
2400 } | |
2401 | |
2402 return retval; | |
529 | 2403 } |
2404 | |
3933 | 2405 DEFUN (__print_symtab_info__, args, , |
3446 | 2406 "-*- texinfo -*-\n\ |
3933 | 2407 @deftypefn {Built-in Function} {} __print_symtab_info__ ()\n\ |
6945 | 2408 Undocumented internal function.\n\ |
3446 | 2409 @end deftypefn") |
3005 | 2410 { |
2411 octave_value_list retval; | |
2412 | |
7336 | 2413 // FIXME -- what should this function do now? Print a summary for |
2414 // each scope? Print the entire symbol table? Accept a scope | |
2415 // argument? | |
3005 | 2416 |
2417 return retval; | |
2418 } | |
2419 | |
3933 | 2420 DEFUN (__print_symbol_info__, args, , |
3446 | 2421 "-*- texinfo -*-\n\ |
2422 @deftypefn {Built-in Function} {} __dump_symbol_info__ (@var{name})\n\ | |
6945 | 2423 Undocumented internal function.\n\ |
3446 | 2424 @end deftypefn") |
3239 | 2425 { |
2426 octave_value_list retval; | |
2427 | |
7336 | 2428 // FIXME -- what should this function do now? |
3239 | 2429 |
2430 return retval; | |
2431 } | |
2432 | |
7336 | 2433 DEFUN (whos_line_format, args, nargout, |
2434 "-*- texinfo -*-\n\ | |
2435 @deftypefn {Built-in Function} {@var{val} =} whos_line_format ()\n\ | |
2436 @deftypefnx {Built-in Function} {@var{old_val} =} whos_line_format (@var{new_val})\n\ | |
2437 Query or set the format string used by the @code{whos}.\n\ | |
2438 \n\ | |
2439 The following escape sequences may be used in the format:\n\ | |
2440 @table @code\n\ | |
2441 @item %a\n\ | |
2442 Prints attributes of variables (g=global, p=persistent,\n\ | |
2443 f=formal parameter, a=automatic variable).\n\ | |
2444 @item %b\n\ | |
2445 Prints number of bytes occupied by variables.\n\ | |
2446 @item %c\n\ | |
2447 Prints class names of variables.\n\ | |
2448 @item %e\n\ | |
2449 Prints elements held by variables.\n\ | |
2450 @item %n\n\ | |
2451 Prints variable names.\n\ | |
2452 @item %s\n\ | |
2453 Prints dimensions of variables.\n\ | |
2454 @item %t\n\ | |
2455 Prints type names of variables.\n\ | |
2456 @end table\n\ | |
2457 \n\ | |
2458 Every command may also have a modifier:\n\ | |
2459 @table @code\n\ | |
2460 @item l\n\ | |
2461 Left alignment.\n\ | |
2462 @item r\n\ | |
2463 Right alignment (this is the default).\n\ | |
2464 @item c\n\ | |
2465 Centered (may only be applied to command %s).\n\ | |
2466 @end table\n\ | |
2467 \n\ | |
2468 A command is composed like this:\n\ | |
2469 \n\ | |
2470 @example\n\ | |
2471 %[modifier]<command>[:size_of_parameter[:center-specific[\n\ | |
2472 :print_dims[:balance]]]];\n\ | |
2473 @end example\n\ | |
2474 \n\ | |
2475 Command and modifier is already explained. Size_of_parameter\n\ | |
2476 tells how many columns the parameter will need for printing.\n\ | |
2477 print_dims tells how many dimensions to print. If number of\n\ | |
2478 dimensions exceeds print_dims, dimensions will be printed like\n\ | |
2479 x-D.\n\ | |
2480 center-specific and print_dims may only be applied to command\n\ | |
2481 %s. A negative value for print_dims will cause Octave to print all\n\ | |
2482 dimensions whatsoever.\n\ | |
2483 balance specifies the offset for printing of the dimensions string.\n\ | |
2484 \n\ | |
2485 The default format is \" %a:4; %ln:6; %cs:16:6:8:1; %rb:12; %lc:-1;\\n\".\n\ | |
5794 | 2486 @end deftypefn") |
2487 { | |
7336 | 2488 return SET_INTERNAL_VARIABLE (whos_line_format); |
3016 | 2489 } |
2490 | |
1 | 2491 /* |
2492 ;;; Local Variables: *** | |
2493 ;;; mode: C++ *** | |
2494 ;;; End: *** | |
2495 */ |