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