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