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 |
2926
|
978 static bool |
5397
|
979 fcn_out_of_date (octave_function *fcn, const std::string& ff, time_t tp) |
5312
|
980 { |
|
981 bool retval = false; |
|
982 |
5397
|
983 fcn->mark_fcn_file_up_to_date (octave_time ()); |
|
984 |
|
985 if (! (Vignore_function_time_stamp == 2 |
|
986 || (Vignore_function_time_stamp && fcn->is_system_fcn_file ()))) |
5312
|
987 { |
5397
|
988 file_stat fs (ff); |
|
989 |
|
990 if (fs && fs.is_newer (tp)) |
|
991 retval = true; |
5312
|
992 } |
|
993 |
|
994 return retval; |
|
995 } |
|
996 |
5397
|
997 // Is there a corresponding function file that is newer than the |
|
998 // symbol definition? |
|
999 |
5312
|
1000 static bool |
1
|
1001 symbol_out_of_date (symbol_record *sr) |
|
1002 { |
2926
|
1003 bool retval = false; |
195
|
1004 |
5397
|
1005 if (sr) |
1
|
1006 { |
2975
|
1007 octave_value ans = sr->def (); |
|
1008 |
5397
|
1009 octave_function *fcn = ans.function_value (true); |
|
1010 |
5436
|
1011 // No need to check nested functions. They can only be executed |
|
1012 // from within the parent function the contains them. Parent |
|
1013 // and nested functions will be updated simultaneously when we |
|
1014 // check the parent. |
|
1015 |
|
1016 if (fcn && ! fcn->is_nested_function ()) |
5397
|
1017 { |
|
1018 std::string ff = fcn->fcn_file_name (); |
|
1019 |
|
1020 if (! ff.empty ()) |
|
1021 { |
|
1022 if (fcn->time_checked () < Vlast_prompt_time) |
|
1023 { |
|
1024 time_t tp = fcn->time_parsed (); |
|
1025 |
|
1026 std::string nm = fcn->name (); |
|
1027 |
|
1028 string_vector names (2); |
|
1029 |
|
1030 names[0] = nm + ".oct"; |
|
1031 names[1] = nm + ".m"; |
|
1032 |
|
1033 std::string file = octave_env::make_absolute |
|
1034 (Vload_path_dir_path.find_first_of (names), |
|
1035 octave_env::getcwd ()); |
|
1036 |
|
1037 if (same_file (file, ff)) |
|
1038 { |
|
1039 retval = fcn_out_of_date (fcn, ff, tp); |
|
1040 } |
|
1041 else |
|
1042 { |
|
1043 // Check the full function name. Maybe we alrady |
|
1044 // parsed it. |
|
1045 |
|
1046 symbol_record *full_sr = fbi_sym_tab->lookup (file); |
|
1047 |
|
1048 if (full_sr) |
|
1049 { |
|
1050 octave_value v = full_sr->def (); |
|
1051 |
|
1052 if (v.is_function ()) |
|
1053 { |
|
1054 // OK, swap the aliases around. |
|
1055 |
|
1056 // XXX FIXME XXX -- this is a bit |
|
1057 // tricky, so maybe some refactoring is |
|
1058 // in order here too... |
|
1059 |
|
1060 symbol_record *short_sr = fbi_sym_tab->lookup (nm); |
|
1061 |
|
1062 if (short_sr) |
|
1063 short_sr->alias (full_sr); |
|
1064 |
|
1065 // Make local symbol table entry point |
|
1066 // to correct global function too. |
|
1067 |
|
1068 sr->alias (full_sr); |
|
1069 |
|
1070 fcn = v.function_value (); |
|
1071 |
|
1072 retval = fcn_out_of_date (fcn, file, tp); |
|
1073 } |
|
1074 else |
|
1075 retval = true; |
|
1076 } |
|
1077 else |
|
1078 retval = true; |
|
1079 } |
|
1080 } |
|
1081 } |
|
1082 } |
1
|
1083 } |
2926
|
1084 |
|
1085 return retval; |
1
|
1086 } |
|
1087 |
1827
|
1088 bool |
2856
|
1089 lookup (symbol_record *sym_rec, bool exec_script) |
581
|
1090 { |
1827
|
1091 bool script_executed = false; |
581
|
1092 |
|
1093 if (! sym_rec->is_linked_to_global ()) |
|
1094 { |
|
1095 if (sym_rec->is_defined ()) |
|
1096 { |
|
1097 if (sym_rec->is_function () && symbol_out_of_date (sym_rec)) |
1271
|
1098 script_executed = load_fcn_from_file (sym_rec, exec_script); |
581
|
1099 } |
|
1100 else if (! sym_rec->is_formal_parameter ()) |
|
1101 { |
|
1102 link_to_builtin_or_function (sym_rec); |
1271
|
1103 |
581
|
1104 if (! sym_rec->is_defined ()) |
1271
|
1105 script_executed = load_fcn_from_file (sym_rec, exec_script); |
581
|
1106 else if (sym_rec->is_function () && symbol_out_of_date (sym_rec)) |
1271
|
1107 script_executed = load_fcn_from_file (sym_rec, exec_script); |
581
|
1108 } |
|
1109 } |
|
1110 |
1271
|
1111 return script_executed; |
581
|
1112 } |
|
1113 |
|
1114 // Get the symbol record for the given name that is visible in the |
|
1115 // current scope. Reread any function definitions that appear to be |
|
1116 // out of date. If a function is available in a file but is not |
|
1117 // currently loaded, this will load it and insert the name in the |
|
1118 // current symbol table. |
|
1119 |
|
1120 symbol_record * |
3523
|
1121 lookup_by_name (const std::string& nm, bool exec_script) |
581
|
1122 { |
2856
|
1123 symbol_record *sym_rec = curr_sym_tab->lookup (nm, true); |
581
|
1124 |
|
1125 lookup (sym_rec, exec_script); |
|
1126 |
|
1127 return sym_rec; |
|
1128 } |
|
1129 |
4930
|
1130 octave_value |
4342
|
1131 lookup_function (const std::string& nm) |
|
1132 { |
4930
|
1133 octave_value retval; |
4342
|
1134 |
|
1135 symbol_record *sr = 0; |
|
1136 |
|
1137 if (curr_parent_function) |
|
1138 { |
4748
|
1139 std::string parent = curr_parent_function->name (); |
4342
|
1140 |
|
1141 sr = fbi_sym_tab->lookup (parent + ":" + nm); |
|
1142 } |
|
1143 |
|
1144 if (! sr || ! sr->is_function ()) |
|
1145 { |
|
1146 sr = fbi_sym_tab->lookup (nm, true); |
|
1147 |
|
1148 if (sr && ! sr->is_function ()) |
|
1149 load_fcn_from_file (sr, false); |
|
1150 } |
|
1151 |
|
1152 if (sr) |
|
1153 { |
|
1154 octave_value v = sr->def (); |
|
1155 |
|
1156 if (v.is_function ()) |
4930
|
1157 retval = v; |
4342
|
1158 } |
|
1159 |
|
1160 return retval; |
|
1161 } |
|
1162 |
4930
|
1163 octave_value |
4700
|
1164 lookup_user_function (const std::string& nm) |
|
1165 { |
4930
|
1166 octave_value retval; |
4700
|
1167 |
|
1168 symbol_record *sr = 0; |
|
1169 |
|
1170 if (curr_parent_function) |
|
1171 { |
4748
|
1172 std::string parent = curr_parent_function->name (); |
4700
|
1173 |
|
1174 sr = fbi_sym_tab->lookup (parent + ":" + nm); |
|
1175 } |
|
1176 |
|
1177 if (! sr || ! sr->is_user_function ()) |
|
1178 { |
|
1179 sr = fbi_sym_tab->lookup (nm, true); |
|
1180 |
|
1181 if (sr && ! sr->is_user_function ()) |
|
1182 load_fcn_from_file (sr, false); |
|
1183 } |
|
1184 |
|
1185 if (sr) |
4930
|
1186 retval = sr->def (); |
4700
|
1187 |
|
1188 return retval; |
|
1189 } |
|
1190 |
2849
|
1191 octave_value |
4988
|
1192 lookup_function_handle (const std::string& nm) |
|
1193 { |
|
1194 octave_value retval; |
|
1195 |
|
1196 symbol_record *sr = curr_sym_tab->lookup (nm, true); |
|
1197 |
|
1198 if (sr && sr->def ().is_function_handle ()) |
|
1199 retval = sr->def (); |
|
1200 |
|
1201 return retval; |
|
1202 } |
|
1203 |
|
1204 octave_value |
5027
|
1205 get_global_value (const std::string& nm, bool silent) |
2849
|
1206 { |
|
1207 octave_value retval; |
|
1208 |
|
1209 symbol_record *sr = global_sym_tab->lookup (nm); |
|
1210 |
|
1211 if (sr) |
|
1212 { |
2975
|
1213 octave_value sr_def = sr->def (); |
2849
|
1214 |
5027
|
1215 if (sr_def.is_defined ()) |
|
1216 retval = sr_def; |
|
1217 else if (! silent) |
2849
|
1218 error ("get_global_by_name: undefined symbol `%s'", nm.c_str ()); |
|
1219 } |
5027
|
1220 else if (! silent) |
2849
|
1221 error ("get_global_by_name: unknown symbol `%s'", nm.c_str ()); |
|
1222 |
|
1223 return retval; |
|
1224 } |
|
1225 |
|
1226 void |
3523
|
1227 set_global_value (const std::string& nm, const octave_value& val) |
2849
|
1228 { |
2856
|
1229 symbol_record *sr = global_sym_tab->lookup (nm, true); |
2849
|
1230 |
|
1231 if (sr) |
|
1232 sr->define (val); |
|
1233 else |
|
1234 panic_impossible (); |
|
1235 } |
|
1236 |
593
|
1237 // Variable values. |
195
|
1238 |
581
|
1239 // Look for the given name in the global symbol table. If it refers |
|
1240 // to a string, return a new copy. If not, return 0; |
|
1241 |
3523
|
1242 std::string |
|
1243 builtin_string_variable (const std::string& name) |
1
|
1244 { |
4009
|
1245 symbol_record *sr = fbi_sym_tab->lookup (name); |
195
|
1246 |
4329
|
1247 // It is a programming error to look for builtins that aren't. |
195
|
1248 |
4280
|
1249 // Use != here to avoid possible conversion to int of smaller type |
|
1250 // than the sr pointer. |
|
1251 |
|
1252 assert (sr != 0); |
195
|
1253 |
3523
|
1254 std::string retval; |
1
|
1255 |
2975
|
1256 octave_value val = sr->def (); |
195
|
1257 |
2975
|
1258 if (! error_state && val.is_string ()) |
|
1259 retval = val.string_value (); |
1
|
1260 |
|
1261 return retval; |
|
1262 } |
|
1263 |
581
|
1264 // Look for the given name in the global symbol table. If it refers |
1504
|
1265 // to a real scalar, place the value in d and return 1. Otherwise, |
|
1266 // return 0. |
581
|
1267 |
1
|
1268 int |
3523
|
1269 builtin_real_scalar_variable (const std::string& name, double& d) |
1
|
1270 { |
1504
|
1271 int status = 0; |
4009
|
1272 symbol_record *sr = fbi_sym_tab->lookup (name); |
195
|
1273 |
4329
|
1274 // It is a programming error to look for builtins that aren't. |
195
|
1275 |
4280
|
1276 // Use != here to avoid possible conversion to int of smaller type |
|
1277 // than the sr pointer. |
|
1278 |
|
1279 assert (sr != 0); |
1
|
1280 |
2975
|
1281 octave_value val = sr->def (); |
195
|
1282 |
2975
|
1283 if (! error_state && val.is_scalar_type ()) |
|
1284 { |
|
1285 d = val.double_value (); |
|
1286 status = 1; |
1
|
1287 } |
|
1288 |
|
1289 return status; |
|
1290 } |
|
1291 |
1093
|
1292 // Look for the given name in the global symbol table. |
|
1293 |
2086
|
1294 octave_value |
3523
|
1295 builtin_any_variable (const std::string& name) |
1093
|
1296 { |
4009
|
1297 symbol_record *sr = fbi_sym_tab->lookup (name); |
1093
|
1298 |
4329
|
1299 // It is a programming error to look for builtins that aren't. |
1093
|
1300 |
4280
|
1301 // Use != here to avoid possible conversion to int of smaller type |
|
1302 // than the sr pointer. |
|
1303 |
|
1304 assert (sr != 0); |
1093
|
1305 |
2975
|
1306 return sr->def (); |
1093
|
1307 } |
|
1308 |
593
|
1309 // Global stuff and links to builtin variables and functions. |
|
1310 |
581
|
1311 // Make the definition of the symbol record sr be the same as the |
|
1312 // definition of the global variable of the same name, creating it if |
1418
|
1313 // it doesn't already exist. |
581
|
1314 |
195
|
1315 void |
|
1316 link_to_global_variable (symbol_record *sr) |
|
1317 { |
2975
|
1318 if (! sr->is_linked_to_global ()) |
195
|
1319 { |
2975
|
1320 sr->mark_as_linked_to_global (); |
195
|
1321 |
2975
|
1322 if (! error_state) |
|
1323 { |
3523
|
1324 std::string nm = sr->name (); |
2846
|
1325 |
2975
|
1326 symbol_record *gsr = global_sym_tab->lookup (nm, true); |
|
1327 |
|
1328 // Make sure this symbol is a variable. |
2900
|
1329 |
4009
|
1330 if (! gsr->is_user_variable ()) |
2975
|
1331 gsr->define (octave_value ()); |
2900
|
1332 |
4009
|
1333 sr->alias (gsr); |
2975
|
1334 } |
195
|
1335 } |
|
1336 } |
|
1337 |
581
|
1338 // Make the definition of the symbol record sr be the same as the |
|
1339 // definition of the builtin variable of the same name. |
|
1340 |
|
1341 // Make the definition of the symbol record sr be the same as the |
3258
|
1342 // definition of the builtin variable, constant, or function, or user |
|
1343 // function of the same name, provided that the name has not been used |
|
1344 // as a formal parameter. |
581
|
1345 |
195
|
1346 void |
|
1347 link_to_builtin_or_function (symbol_record *sr) |
|
1348 { |
4238
|
1349 std::string nm = sr->name (); |
|
1350 |
|
1351 symbol_record *tmp_sym = 0; |
|
1352 |
|
1353 if (curr_parent_function) |
|
1354 { |
4748
|
1355 std::string parent = curr_parent_function->name (); |
4238
|
1356 |
|
1357 tmp_sym = fbi_sym_tab->lookup (parent + ":" + nm); |
|
1358 } |
|
1359 |
|
1360 if (! tmp_sym) |
|
1361 tmp_sym = fbi_sym_tab->lookup (nm); |
195
|
1362 |
529
|
1363 if (tmp_sym |
3258
|
1364 && (tmp_sym->is_builtin_variable () |
|
1365 || tmp_sym->is_builtin_constant () |
|
1366 || tmp_sym->is_function ()) |
529
|
1367 && ! tmp_sym->is_formal_parameter ()) |
|
1368 sr->alias (tmp_sym); |
195
|
1369 } |
|
1370 |
581
|
1371 // Force a link to a function in the current symbol table. This is |
|
1372 // used just after defining a function to avoid different behavior |
|
1373 // depending on whether or not the function has been evaluated after |
|
1374 // being defined. |
|
1375 // |
|
1376 // Return without doing anything if there isn't a function with the |
|
1377 // given name defined in the global symbol table. |
|
1378 |
195
|
1379 void |
3523
|
1380 force_link_to_function (const std::string& id_name) |
195
|
1381 { |
4009
|
1382 symbol_record *fsr = fbi_sym_tab->lookup (id_name, true); |
|
1383 if (fsr->is_function ()) |
195
|
1384 { |
|
1385 curr_sym_tab->clear (id_name); |
2856
|
1386 symbol_record *csr = curr_sym_tab->lookup (id_name, true); |
4009
|
1387 csr->alias (fsr); |
195
|
1388 } |
|
1389 } |
|
1390 |
2294
|
1391 DEFUN (document, args, , |
3361
|
1392 "-*- texinfo -*-\n\ |
|
1393 @deftypefn {Built-in Function} {} document (@var{symbol}, @var{text})\n\ |
|
1394 Set the documentation string for @var{symbol} to @var{text}.\n\ |
|
1395 @end deftypefn") |
593
|
1396 { |
2294
|
1397 octave_value retval; |
1755
|
1398 |
2294
|
1399 int nargin = args.length (); |
1755
|
1400 |
2294
|
1401 if (nargin == 2) |
593
|
1402 { |
3523
|
1403 std::string name = args(0).string_value (); |
593
|
1404 |
2294
|
1405 if (! error_state) |
593
|
1406 { |
3523
|
1407 std::string help = args(1).string_value (); |
593
|
1408 |
2294
|
1409 if (! error_state) |
|
1410 { |
|
1411 if (is_builtin_variable (name) |
4208
|
1412 || is_command_name (name) |
2294
|
1413 || is_mapper_function_name (name) |
|
1414 || is_builtin_function_name (name)) |
|
1415 error ("document: can't redefine help for built-in variables and functions"); |
|
1416 else |
|
1417 { |
2856
|
1418 symbol_record *sym_rec = curr_sym_tab->lookup (name); |
2294
|
1419 |
|
1420 if (sym_rec) |
|
1421 sym_rec->document (help); |
|
1422 else |
|
1423 error ("document: no such symbol `%s'", name.c_str ()); |
|
1424 } |
|
1425 } |
593
|
1426 } |
|
1427 } |
|
1428 else |
|
1429 print_usage ("document"); |
|
1430 |
|
1431 return retval; |
|
1432 } |
|
1433 |
4435
|
1434 static octave_value |
|
1435 do_who (int argc, const string_vector& argv, int return_list) |
529
|
1436 { |
4435
|
1437 octave_value retval; |
529
|
1438 |
2856
|
1439 bool show_builtins = false; |
3248
|
1440 bool show_functions = false; |
|
1441 bool show_variables = false; |
2856
|
1442 bool show_verbose = false; |
529
|
1443 |
3523
|
1444 std::string my_name = argv[0]; |
584
|
1445 |
1857
|
1446 int i; |
|
1447 for (i = 1; i < argc; i++) |
529
|
1448 { |
1755
|
1449 if (argv[i] == "-all" || argv[i] == "-a") |
529
|
1450 { |
2856
|
1451 show_builtins = true; |
|
1452 show_functions = true; |
|
1453 show_variables = true; |
529
|
1454 } |
1755
|
1455 else if (argv[i] == "-builtins" || argv[i] == "-b") |
2856
|
1456 show_builtins = true; |
1755
|
1457 else if (argv[i] == "-functions" || argv[i] == "-f") |
2856
|
1458 show_functions = true; |
1755
|
1459 else if (argv[i] == "-long" || argv[i] == "-l") |
2856
|
1460 show_verbose = true; |
1755
|
1461 else if (argv[i] == "-variables" || argv[i] == "-v") |
2856
|
1462 show_variables = true; |
1755
|
1463 else if (argv[i][0] == '-') |
|
1464 warning ("%s: unrecognized option `%s'", my_name.c_str (), |
|
1465 argv[i].c_str ()); |
529
|
1466 else |
867
|
1467 break; |
529
|
1468 } |
|
1469 |
3248
|
1470 // If no options were specified to select the type of symbol to |
|
1471 // display, then set defaults. |
|
1472 |
|
1473 if (! (show_builtins || show_functions || show_variables)) |
|
1474 { |
4208
|
1475 show_functions = at_top_level (); |
3248
|
1476 show_variables = true; |
|
1477 } |
|
1478 |
1857
|
1479 int npats = argc - i; |
|
1480 string_vector pats (npats); |
|
1481 for (int j = 0; j < npats; j++) |
|
1482 pats[j] = argv[i+j]; |
|
1483 |
1271
|
1484 // If the user specified -l and nothing else, show variables. If |
|
1485 // evaluating this at the top level, also show functions. |
529
|
1486 |
|
1487 if (show_verbose && ! (show_builtins || show_functions || show_variables)) |
|
1488 { |
4208
|
1489 show_functions = at_top_level (); |
529
|
1490 show_variables = 1; |
|
1491 } |
|
1492 |
4435
|
1493 if (return_list) |
529
|
1494 { |
4435
|
1495 string_vector names; |
|
1496 |
|
1497 if (show_builtins) |
|
1498 { |
|
1499 names.append (fbi_sym_tab->name_list |
|
1500 (pats, true, symbol_record::BUILTIN_CONSTANT, |
|
1501 SYMTAB_ALL_SCOPES)); |
|
1502 |
|
1503 names.append (fbi_sym_tab->name_list |
|
1504 (pats, true, symbol_record::BUILTIN_VARIABLE, |
|
1505 SYMTAB_ALL_SCOPES)); |
|
1506 |
|
1507 names.append (fbi_sym_tab->name_list |
|
1508 (pats, true, symbol_record::BUILTIN_FUNCTION, |
|
1509 SYMTAB_ALL_SCOPES)); |
|
1510 } |
|
1511 |
|
1512 if (show_functions) |
|
1513 { |
|
1514 names.append (fbi_sym_tab->name_list |
|
1515 (pats, true, symbol_record::DLD_FUNCTION, |
|
1516 SYMTAB_ALL_SCOPES)); |
4108
|
1517 |
4435
|
1518 names.append (fbi_sym_tab->name_list |
|
1519 (pats, true, symbol_record::USER_FUNCTION, |
|
1520 SYMTAB_ALL_SCOPES)); |
|
1521 } |
|
1522 |
|
1523 if (show_variables) |
|
1524 { |
|
1525 names.append (curr_sym_tab->name_list |
|
1526 (pats, true, symbol_record::USER_VARIABLE, |
|
1527 SYMTAB_LOCAL_SCOPE)); |
|
1528 |
|
1529 names.append (curr_sym_tab->name_list |
|
1530 (pats, true, symbol_record::USER_VARIABLE, |
|
1531 SYMTAB_GLOBAL_SCOPE)); |
|
1532 } |
|
1533 |
|
1534 if (show_verbose) |
|
1535 { |
|
1536 int len = names.length (); |
|
1537 |
|
1538 octave_value_list ovl (len, octave_value ()); |
|
1539 |
5058
|
1540 for (int j = 0; j < len; j++) |
4587
|
1541 ovl(j) = names(j); |
4435
|
1542 |
|
1543 retval = Octave_map ("name", ovl); |
|
1544 } |
|
1545 else |
|
1546 retval = Cell (names); |
529
|
1547 } |
4435
|
1548 else |
529
|
1549 { |
4435
|
1550 int pad_after = 0; |
|
1551 |
|
1552 if (show_builtins) |
|
1553 { |
|
1554 pad_after += fbi_sym_tab->maybe_list |
|
1555 ("*** built-in constants:", pats, octave_stdout, |
|
1556 show_verbose, symbol_record::BUILTIN_CONSTANT, SYMTAB_ALL_SCOPES); |
|
1557 |
|
1558 pad_after += fbi_sym_tab->maybe_list |
|
1559 ("*** built-in variables:", pats, octave_stdout, |
|
1560 show_verbose, symbol_record::BUILTIN_VARIABLE, SYMTAB_ALL_SCOPES); |
|
1561 |
|
1562 pad_after += fbi_sym_tab->maybe_list |
|
1563 ("*** built-in functions:", pats, octave_stdout, |
|
1564 show_verbose, symbol_record::BUILTIN_FUNCTION, SYMTAB_ALL_SCOPES); |
|
1565 } |
529
|
1566 |
4435
|
1567 if (show_functions) |
|
1568 { |
|
1569 pad_after += fbi_sym_tab->maybe_list |
|
1570 ("*** dynamically linked functions:", pats, |
|
1571 octave_stdout, show_verbose, symbol_record::DLD_FUNCTION, |
|
1572 SYMTAB_ALL_SCOPES); |
|
1573 |
|
1574 pad_after += fbi_sym_tab->maybe_list |
|
1575 ("*** currently compiled functions:", pats, |
|
1576 octave_stdout, show_verbose, symbol_record::USER_FUNCTION, |
|
1577 SYMTAB_ALL_SCOPES); |
|
1578 } |
|
1579 |
|
1580 if (show_variables) |
|
1581 { |
|
1582 pad_after += curr_sym_tab->maybe_list |
|
1583 ("*** local user variables:", pats, octave_stdout, |
|
1584 show_verbose, symbol_record::USER_VARIABLE, SYMTAB_LOCAL_SCOPE); |
|
1585 |
|
1586 pad_after += curr_sym_tab->maybe_list |
|
1587 ("*** globally visible user variables:", pats, |
|
1588 octave_stdout, show_verbose, symbol_record::USER_VARIABLE, |
|
1589 SYMTAB_GLOBAL_SCOPE); |
|
1590 } |
|
1591 |
|
1592 if (pad_after) |
|
1593 octave_stdout << "\n"; |
529
|
1594 } |
|
1595 |
581
|
1596 return retval; |
|
1597 } |
|
1598 |
4435
|
1599 DEFCMD (who, args, nargout, |
3361
|
1600 "-*- texinfo -*-\n\ |
|
1601 @deffn {Command} who options pattern @dots{}\n\ |
|
1602 @deffnx {Command} whos options pattern @dots{}\n\ |
|
1603 List currently defined symbols matching the given patterns. The\n\ |
|
1604 following are valid options. They may be shortened to one character but\n\ |
|
1605 may not be combined.\n\ |
|
1606 \n\ |
|
1607 @table @code\n\ |
|
1608 @item -all\n\ |
|
1609 List all currently defined symbols.\n\ |
|
1610 \n\ |
|
1611 @item -builtins\n\ |
|
1612 List built-in variables and functions. This includes all currently\n\ |
|
1613 compiled function files, but does not include all function files that\n\ |
|
1614 are in the @code{LOADPATH}.\n\ |
581
|
1615 \n\ |
3361
|
1616 @item -functions\n\ |
|
1617 List user-defined functions.\n\ |
|
1618 \n\ |
|
1619 @item -long\n\ |
|
1620 Print a long listing including the type and dimensions of any symbols.\n\ |
|
1621 The symbols in the first column of output indicate whether it is\n\ |
|
1622 possible to redefine the symbol, and whether it is possible for it to be\n\ |
|
1623 cleared.\n\ |
|
1624 \n\ |
|
1625 @item -variables\n\ |
|
1626 List user-defined variables.\n\ |
|
1627 @end table\n\ |
|
1628 \n\ |
|
1629 Valid patterns are the same as described for the @code{clear} command\n\ |
|
1630 above. If no patterns are supplied, all symbols from the given category\n\ |
|
1631 are listed. By default, only user defined functions and variables\n\ |
|
1632 visible in the local scope are displayed.\n\ |
|
1633 \n\ |
|
1634 The command @kbd{whos} is equivalent to @kbd{who -long}.\n\ |
|
1635 @end deffn") |
581
|
1636 { |
4435
|
1637 octave_value retval; |
581
|
1638 |
4435
|
1639 if (nargout < 2) |
|
1640 { |
|
1641 int argc = args.length () + 1; |
|
1642 |
|
1643 string_vector argv = args.make_argv ("who"); |
1755
|
1644 |
4435
|
1645 if (error_state) |
|
1646 return retval; |
1755
|
1647 |
4435
|
1648 retval = do_who (argc, argv, nargout == 1); |
|
1649 } |
|
1650 else |
|
1651 print_usage ("who"); |
581
|
1652 |
529
|
1653 return retval; |
|
1654 } |
|
1655 |
4435
|
1656 DEFCMD (whos, args, nargout, |
3458
|
1657 "-*- texinfo -*-\n\ |
|
1658 @deffn {Command} whos options pattern @dots{}\n\ |
|
1659 See who.\n\ |
|
1660 @end deffn") |
581
|
1661 { |
4435
|
1662 octave_value retval; |
712
|
1663 |
4435
|
1664 if (nargout < 2) |
|
1665 { |
|
1666 int nargin = args.length (); |
|
1667 |
|
1668 octave_value_list tmp_args; |
|
1669 |
|
1670 for (int i = nargin; i > 0; i--) |
|
1671 tmp_args(i) = args(i-1); |
581
|
1672 |
4435
|
1673 tmp_args(0) = "-long"; |
1755
|
1674 |
4435
|
1675 int argc = tmp_args.length () + 1; |
|
1676 |
|
1677 string_vector argv = tmp_args.make_argv ("whos"); |
581
|
1678 |
4435
|
1679 if (error_state) |
|
1680 return retval; |
581
|
1681 |
4435
|
1682 retval = do_who (argc, argv, nargout == 1); |
|
1683 } |
|
1684 else |
|
1685 print_usage ("whos"); |
581
|
1686 |
|
1687 return retval; |
|
1688 } |
|
1689 |
593
|
1690 // Defining variables. |
|
1691 |
1162
|
1692 void |
2856
|
1693 bind_ans (const octave_value& val, bool print) |
1162
|
1694 { |
4009
|
1695 static symbol_record *sr = fbi_sym_tab->lookup ("ans", true); |
1162
|
1696 |
2978
|
1697 if (val.is_defined ()) |
|
1698 { |
|
1699 sr->define (val); |
1162
|
1700 |
2978
|
1701 if (print) |
|
1702 val.print_with_name (octave_stdout, "ans"); |
|
1703 } |
1162
|
1704 } |
|
1705 |
3259
|
1706 // Give a global constant a definition. This will insert the symbol |
|
1707 // in the global table if necessary. |
|
1708 |
|
1709 // How is this different than install_builtin_constant? Are both |
|
1710 // functions needed? |
|
1711 |
|
1712 void |
3523
|
1713 bind_builtin_constant (const std::string& name, const octave_value& val, |
|
1714 bool protect, bool eternal, const std::string& help) |
3259
|
1715 { |
4009
|
1716 symbol_record *sym_rec = fbi_sym_tab->lookup (name, true); |
3259
|
1717 sym_rec->unprotect (); |
|
1718 |
3523
|
1719 std::string tmp_help = help.empty () ? sym_rec->help () : help; |
3259
|
1720 |
|
1721 sym_rec->define_builtin_const (val); |
|
1722 |
|
1723 sym_rec->document (tmp_help); |
|
1724 |
|
1725 if (protect) |
|
1726 sym_rec->protect (); |
|
1727 |
|
1728 if (eternal) |
|
1729 sym_rec->make_eternal (); |
|
1730 } |
|
1731 |
593
|
1732 // Give a global variable a definition. This will insert the symbol |
|
1733 // in the global table if necessary. |
|
1734 |
|
1735 // How is this different than install_builtin_variable? Are both |
|
1736 // functions needed? |
|
1737 |
|
1738 void |
3523
|
1739 bind_builtin_variable (const std::string& varname, const octave_value& val, |
2953
|
1740 bool protect, bool eternal, |
3005
|
1741 symbol_record::change_function chg_fcn, |
3523
|
1742 const std::string& help) |
593
|
1743 { |
4009
|
1744 symbol_record *sr = fbi_sym_tab->lookup (varname, true); |
593
|
1745 |
1271
|
1746 // It is a programming error for a builtin symbol to be missing. |
|
1747 // Besides, we just inserted it, so it must be there. |
593
|
1748 |
4280
|
1749 // Use != here to avoid possible conversion to int of smaller type |
|
1750 // than the sr pointer. |
|
1751 |
|
1752 assert (sr != 0); |
593
|
1753 |
|
1754 sr->unprotect (); |
|
1755 |
1271
|
1756 // Must do this before define, since define will call the special |
|
1757 // variable function only if it knows about it, and it needs to, so |
|
1758 // that user prefs can be properly initialized. |
593
|
1759 |
3005
|
1760 if (chg_fcn) |
|
1761 sr->set_change_function (chg_fcn); |
593
|
1762 |
|
1763 sr->define_builtin_var (val); |
|
1764 |
|
1765 if (protect) |
|
1766 sr->protect (); |
|
1767 |
|
1768 if (eternal) |
|
1769 sr->make_eternal (); |
|
1770 |
1755
|
1771 sr->document (help); |
529
|
1772 } |
|
1773 |
4319
|
1774 void |
|
1775 mlock (const std::string& nm) |
|
1776 { |
|
1777 symbol_record *sr = fbi_sym_tab->lookup (nm, true); |
|
1778 |
|
1779 if (sr) |
|
1780 sr->mark_as_static (); |
|
1781 } |
|
1782 |
|
1783 void |
|
1784 munlock (const std::string& nm) |
|
1785 { |
|
1786 symbol_record *sr = fbi_sym_tab->lookup (nm); |
|
1787 |
|
1788 if (sr && sr->is_static ()) |
|
1789 sr->unmark_static (); |
|
1790 else |
|
1791 error ("munlock: %s is not locked", nm.c_str ()); |
|
1792 } |
|
1793 |
|
1794 bool |
|
1795 mislocked (const std::string& nm) |
|
1796 { |
|
1797 symbol_record *sr = fbi_sym_tab->lookup (nm); |
|
1798 |
|
1799 return (sr && sr->is_static ()); |
|
1800 } |
|
1801 |
|
1802 DEFCMD (mlock, args, , |
|
1803 "-*- texinfo -*-\n\ |
4526
|
1804 @deftypefn {Built-in Function} {} mlock (@var{name})\n\ |
4319
|
1805 Lock the named function into memory. If no function is named\n\ |
|
1806 then lock in the current function.\n\ |
|
1807 @end deftypefn\n\ |
|
1808 @seealso{munlock, mislocked, and persistent}") |
|
1809 { |
|
1810 octave_value_list retval; |
|
1811 |
|
1812 if (args.length () == 1) |
|
1813 { |
|
1814 std::string name = args(0).string_value (); |
|
1815 |
|
1816 if (! error_state) |
|
1817 mlock (name); |
|
1818 else |
|
1819 error ("mlock: expecting argument to be a function name"); |
|
1820 } |
|
1821 else if (args.length () == 0) |
|
1822 { |
|
1823 if (curr_function) |
4748
|
1824 mlock (curr_function->name ()); |
4319
|
1825 else |
|
1826 error ("mlock: invalid use outside a function"); |
|
1827 } |
|
1828 else |
|
1829 print_usage ("mlock"); |
|
1830 |
|
1831 return retval; |
|
1832 } |
|
1833 |
|
1834 DEFCMD (munlock, args, , |
|
1835 "-*- texinfo -*-\n\ |
|
1836 @deftypefn {Built-in Function} {} munlock (@var{fcn})\n\ |
|
1837 Unlock the named function. If no function is named\n\ |
|
1838 then unlock the current function.\n\ |
|
1839 @end deftypefn\n\ |
|
1840 @seealso{mlock, mislocked, and persistent}") |
|
1841 { |
|
1842 octave_value_list retval; |
|
1843 |
|
1844 if (args.length() == 1) |
|
1845 { |
|
1846 std::string name = args(0).string_value (); |
|
1847 |
|
1848 if (! error_state) |
|
1849 munlock (name); |
|
1850 else |
|
1851 error ("munlock: expecting argument to be a function name"); |
|
1852 } |
|
1853 else if (args.length () == 0) |
|
1854 { |
|
1855 if (curr_function) |
4748
|
1856 mlock (curr_function->name ()); |
4319
|
1857 else |
|
1858 error ("munlock: invalid use outside a function"); |
|
1859 } |
|
1860 else |
|
1861 print_usage ("munlock"); |
|
1862 |
|
1863 return retval; |
|
1864 } |
|
1865 |
|
1866 |
|
1867 DEFCMD (mislocked, args, , |
|
1868 "-*- texinfo -*-\n\ |
|
1869 @deftypefn {Built-in Function} {} mislocked (@var{fcn})\n\ |
|
1870 Return true if the named function is locked. If no function is named\n\ |
|
1871 then return true if the current function is locked.\n\ |
|
1872 @end deftypefn\n\ |
|
1873 @seealso{mlock, munlock, and persistent}") |
|
1874 { |
|
1875 octave_value retval; |
|
1876 |
|
1877 if (args.length() == 1) |
|
1878 { |
|
1879 std::string name = args(0).string_value (); |
|
1880 |
|
1881 if (! error_state) |
|
1882 retval = mislocked (name); |
|
1883 else |
|
1884 error ("mislocked: expecting argument to be a function name"); |
|
1885 } |
|
1886 else if (args.length () == 0) |
|
1887 { |
|
1888 if (curr_function) |
4748
|
1889 retval = mislocked (curr_function->name ()); |
4319
|
1890 else |
|
1891 error ("mislocked: invalid use outside a function"); |
|
1892 } |
|
1893 else |
|
1894 print_usage ("mislocked"); |
|
1895 |
|
1896 return retval; |
|
1897 } |
|
1898 |
593
|
1899 // Deleting names from the symbol tables. |
|
1900 |
3681
|
1901 static inline bool |
4009
|
1902 name_matches_any_pattern (const std::string& nm, |
|
1903 const string_vector& argv, int argc, int idx) |
3681
|
1904 { |
|
1905 bool retval = false; |
|
1906 |
|
1907 for (int k = idx; k < argc; k++) |
|
1908 { |
|
1909 std::string patstr = argv[k]; |
|
1910 |
|
1911 if (! patstr.empty ()) |
|
1912 { |
|
1913 glob_match pattern (patstr); |
|
1914 |
|
1915 if (pattern.match (nm)) |
|
1916 { |
|
1917 retval = true; |
|
1918 break; |
|
1919 } |
|
1920 } |
|
1921 } |
|
1922 |
|
1923 return retval; |
|
1924 } |
|
1925 |
4009
|
1926 static inline bool |
|
1927 is_local_variable (const std::string& nm) |
|
1928 { |
|
1929 symbol_record *sr = curr_sym_tab->lookup (nm); |
|
1930 |
|
1931 return (sr && sr->is_variable ()); |
|
1932 } |
|
1933 |
|
1934 static inline void |
|
1935 maybe_warn_exclusive (bool exclusive) |
|
1936 { |
|
1937 if (exclusive) |
|
1938 warning ("clear: ignoring --exclusive option"); |
|
1939 } |
|
1940 |
|
1941 static inline void |
|
1942 do_clear_all (void) |
|
1943 { |
|
1944 curr_sym_tab->clear (); |
|
1945 fbi_sym_tab->clear_functions (); |
|
1946 global_sym_tab->clear (); |
|
1947 } |
|
1948 |
|
1949 static inline void |
|
1950 do_clear_functions (void) |
|
1951 { |
|
1952 curr_sym_tab->clear_functions (); |
|
1953 fbi_sym_tab->clear_functions (); |
|
1954 } |
|
1955 |
|
1956 static inline void |
|
1957 do_clear_globals (void) |
|
1958 { |
|
1959 curr_sym_tab->clear_globals (); |
|
1960 global_sym_tab->clear (); |
|
1961 } |
|
1962 |
|
1963 static inline void |
|
1964 do_clear_variables (void) |
|
1965 { |
|
1966 curr_sym_tab->clear (); |
|
1967 } |
|
1968 |
|
1969 static inline bool |
|
1970 do_clear_function (const std::string& nm) |
|
1971 { |
|
1972 bool b1 = curr_sym_tab->clear_function (nm); |
|
1973 |
|
1974 bool b2 = fbi_sym_tab->clear_function (nm); |
|
1975 |
|
1976 return b1 || b2; |
|
1977 } |
|
1978 |
|
1979 static inline bool |
|
1980 do_clear_global (const std::string& nm) |
|
1981 { |
|
1982 bool b1 = curr_sym_tab->clear_global (nm); |
|
1983 |
|
1984 bool b2 = global_sym_tab->clear_variable (nm); |
|
1985 |
|
1986 return b1 || b2; |
|
1987 } |
|
1988 |
|
1989 static inline bool |
|
1990 do_clear_variable (const std::string& nm) |
|
1991 { |
|
1992 return curr_sym_tab->clear_variable (nm); |
|
1993 } |
|
1994 |
|
1995 static inline bool |
|
1996 do_clear_symbol (const std::string& nm) |
|
1997 { |
|
1998 bool cleared = curr_sym_tab->clear_variable (nm); |
|
1999 |
|
2000 if (! cleared) |
|
2001 cleared = do_clear_function (nm); |
|
2002 |
|
2003 return cleared; |
|
2004 } |
|
2005 |
|
2006 static inline bool |
|
2007 do_clear_function_pattern (const std::string& pat) |
|
2008 { |
|
2009 bool b1 = curr_sym_tab->clear_function_pattern (pat); |
|
2010 |
|
2011 bool b2 = fbi_sym_tab->clear_function_pattern (pat); |
|
2012 |
|
2013 return b1 || b2; |
|
2014 } |
|
2015 |
|
2016 static inline bool |
|
2017 do_clear_global_pattern (const std::string& pat) |
|
2018 { |
|
2019 bool b1 = curr_sym_tab->clear_global_pattern (pat); |
|
2020 |
|
2021 bool b2 = global_sym_tab->clear_variable_pattern (pat); |
|
2022 |
|
2023 return b1 || b2; |
|
2024 } |
|
2025 |
|
2026 static inline bool |
|
2027 do_clear_variable_pattern (const std::string& pat) |
|
2028 { |
|
2029 return curr_sym_tab->clear_variable_pattern (pat); |
|
2030 } |
|
2031 |
|
2032 static inline bool |
|
2033 do_clear_symbol_pattern (const std::string& pat) |
|
2034 { |
|
2035 // XXX FIXME XXX -- if we have a variable v1 and a function v2 and |
|
2036 // someone says clear v*, we will clear the variable but not the |
|
2037 // function. Is that really what should happen? (I think it is |
|
2038 // what Matlab does.) |
|
2039 |
|
2040 bool cleared = curr_sym_tab->clear_variable_pattern (pat); |
|
2041 |
|
2042 if (! cleared) |
|
2043 cleared = do_clear_function_pattern (pat); |
|
2044 |
|
2045 return cleared; |
|
2046 } |
|
2047 |
|
2048 static inline void |
|
2049 do_clear_functions (const string_vector& argv, int argc, int idx, |
|
2050 bool exclusive = false) |
|
2051 { |
|
2052 if (idx == argc) |
|
2053 do_clear_functions (); |
|
2054 else |
|
2055 { |
|
2056 if (exclusive) |
|
2057 { |
|
2058 string_vector lfcns = curr_sym_tab->user_function_name_list (); |
|
2059 |
|
2060 int lcount = lfcns.length (); |
|
2061 |
|
2062 for (int i = 0; i < lcount; i++) |
|
2063 { |
|
2064 std::string nm = lfcns[i]; |
|
2065 |
|
2066 if (! name_matches_any_pattern (nm, argv, argc, idx)) |
|
2067 do_clear_function (nm); |
|
2068 } |
|
2069 |
|
2070 string_vector fcns = fbi_sym_tab->user_function_name_list (); |
|
2071 |
|
2072 int fcount = fcns.length (); |
|
2073 |
|
2074 for (int i = 0; i < fcount; i++) |
|
2075 { |
|
2076 std::string nm = fcns[i]; |
|
2077 |
|
2078 if (! name_matches_any_pattern (nm, argv, argc, idx)) |
|
2079 do_clear_function (nm); |
|
2080 } |
|
2081 } |
|
2082 else |
|
2083 { |
|
2084 while (idx < argc) |
|
2085 do_clear_function_pattern (argv[idx++]); |
|
2086 } |
|
2087 } |
|
2088 } |
|
2089 |
|
2090 static inline void |
|
2091 do_clear_globals (const string_vector& argv, int argc, int idx, |
|
2092 bool exclusive = false) |
|
2093 { |
|
2094 if (idx == argc) |
|
2095 do_clear_globals (); |
|
2096 else |
|
2097 { |
|
2098 if (exclusive) |
|
2099 { |
|
2100 string_vector lvars = curr_sym_tab->global_variable_name_list (); |
|
2101 |
|
2102 int lcount = lvars.length (); |
|
2103 |
|
2104 for (int i = 0; i < lcount; i++) |
|
2105 { |
|
2106 std::string nm = lvars[i]; |
|
2107 |
|
2108 if (! name_matches_any_pattern (nm, argv, argc, idx)) |
|
2109 do_clear_global (nm); |
|
2110 } |
|
2111 |
|
2112 string_vector gvars = global_sym_tab->global_variable_name_list (); |
|
2113 |
|
2114 int gcount = gvars.length (); |
|
2115 |
|
2116 for (int i = 0; i < gcount; i++) |
|
2117 { |
|
2118 std::string nm = gvars[i]; |
|
2119 |
|
2120 if (! name_matches_any_pattern (nm, argv, argc, idx)) |
|
2121 do_clear_global (nm); |
|
2122 } |
|
2123 } |
|
2124 else |
|
2125 { |
|
2126 while (idx < argc) |
|
2127 do_clear_global_pattern (argv[idx++]); |
|
2128 } |
|
2129 } |
|
2130 } |
|
2131 |
|
2132 static inline void |
|
2133 do_clear_variables (const string_vector& argv, int argc, int idx, |
|
2134 bool exclusive = false) |
|
2135 { |
|
2136 if (idx == argc) |
|
2137 do_clear_variables (); |
|
2138 else |
|
2139 { |
|
2140 if (exclusive) |
|
2141 { |
|
2142 string_vector lvars = curr_sym_tab->variable_name_list (); |
|
2143 |
|
2144 int lcount = lvars.length (); |
|
2145 |
|
2146 for (int i = 0; i < lcount; i++) |
|
2147 { |
|
2148 std::string nm = lvars[i]; |
|
2149 |
|
2150 if (! name_matches_any_pattern (nm, argv, argc, idx)) |
|
2151 do_clear_variable (nm); |
|
2152 } |
|
2153 } |
|
2154 else |
|
2155 { |
|
2156 while (idx < argc) |
|
2157 do_clear_variable_pattern (argv[idx++]); |
|
2158 } |
|
2159 } |
|
2160 } |
|
2161 |
|
2162 static inline void |
|
2163 do_clear_symbols (const string_vector& argv, int argc, int idx, |
|
2164 bool exclusive = false) |
|
2165 { |
|
2166 if (idx == argc) |
|
2167 do_clear_variables (); |
|
2168 else |
|
2169 { |
|
2170 if (exclusive) |
|
2171 { |
|
2172 // XXX FIXME XXX -- is this really what we want, or do we |
|
2173 // somehow want to only clear the functions that are not |
|
2174 // shadowed by local variables? It seems that would be a |
|
2175 // bit harder to do. |
|
2176 |
|
2177 do_clear_variables (argv, argc, idx, exclusive); |
|
2178 do_clear_functions (argv, argc, idx, exclusive); |
|
2179 } |
|
2180 else |
|
2181 { |
|
2182 while (idx < argc) |
|
2183 do_clear_symbol_pattern (argv[idx++]); |
|
2184 } |
|
2185 } |
|
2186 } |
|
2187 |
|
2188 static void |
|
2189 do_matlab_compatible_clear (const string_vector& argv, int argc, int idx) |
|
2190 { |
|
2191 // This is supposed to be mostly Matlab compatible. |
|
2192 |
|
2193 for (; idx < argc; idx++) |
|
2194 { |
|
2195 if (argv[idx] == "all" && ! is_local_variable ("all")) |
|
2196 { |
|
2197 do_clear_all (); |
|
2198 } |
|
2199 else if (argv[idx] == "functions" && ! is_local_variable ("functions")) |
|
2200 { |
|
2201 do_clear_functions (argv, argc, ++idx); |
|
2202 } |
|
2203 else if (argv[idx] == "global" && ! is_local_variable ("global")) |
|
2204 { |
|
2205 do_clear_globals (argv, argc, ++idx); |
|
2206 } |
|
2207 else if (argv[idx] == "variables" && ! is_local_variable ("variables")) |
|
2208 { |
|
2209 do_clear_variables (); |
|
2210 } |
|
2211 else |
|
2212 { |
|
2213 do_clear_symbol_pattern (argv[idx]); |
|
2214 } |
|
2215 } |
|
2216 } |
|
2217 |
|
2218 #define CLEAR_OPTION_ERROR(cond) \ |
|
2219 do \ |
|
2220 { \ |
|
2221 if (cond) \ |
|
2222 { \ |
|
2223 print_usage ("clear"); \ |
|
2224 return retval; \ |
|
2225 } \ |
|
2226 } \ |
|
2227 while (0) |
|
2228 |
4954
|
2229 bool |
|
2230 clear_function (const std::string& nm) |
|
2231 { |
|
2232 return do_clear_function (nm); |
|
2233 } |
|
2234 |
4988
|
2235 bool |
|
2236 clear_variable (const std::string& nm) |
|
2237 { |
|
2238 return do_clear_variable (nm); |
|
2239 } |
|
2240 |
|
2241 bool |
|
2242 clear_symbol (const std::string& nm) |
|
2243 { |
|
2244 return do_clear_symbol (nm); |
|
2245 } |
|
2246 |
4208
|
2247 DEFCMD (clear, args, , |
3361
|
2248 "-*- texinfo -*-\n\ |
|
2249 @deffn {Command} clear [-x] pattern @dots{}\n\ |
|
2250 Delete the names matching the given patterns from the symbol table. The\n\ |
|
2251 pattern may contain the following special characters:\n\ |
4016
|
2252 \n\ |
3361
|
2253 @table @code\n\ |
|
2254 @item ?\n\ |
|
2255 Match any single character.\n\ |
668
|
2256 \n\ |
3361
|
2257 @item *\n\ |
|
2258 Match zero or more characters.\n\ |
|
2259 \n\ |
|
2260 @item [ @var{list} ]\n\ |
|
2261 Match the list of characters specified by @var{list}. If the first\n\ |
|
2262 character is @code{!} or @code{^}, match all characters except those\n\ |
|
2263 specified by @var{list}. For example, the pattern @samp{[a-zA-Z]} will\n\ |
|
2264 match all lower and upper case alphabetic characters.\n\ |
|
2265 @end table\n\ |
|
2266 \n\ |
|
2267 For example, the command\n\ |
593
|
2268 \n\ |
3361
|
2269 @example\n\ |
|
2270 clear foo b*r\n\ |
|
2271 @end example\n\ |
|
2272 \n\ |
|
2273 @noindent\n\ |
|
2274 clears the name @code{foo} and all names that begin with the letter\n\ |
|
2275 @code{b} and end with the letter @code{r}.\n\ |
668
|
2276 \n\ |
3361
|
2277 If @code{clear} is called without any arguments, all user-defined\n\ |
|
2278 variables (local and global) are cleared from the symbol table. If\n\ |
|
2279 @code{clear} is called with at least one argument, only the visible\n\ |
|
2280 names matching the arguments are cleared. For example, suppose you have\n\ |
|
2281 defined a function @code{foo}, and then hidden it by performing the\n\ |
|
2282 assignment @code{foo = 2}. Executing the command @kbd{clear foo} once\n\ |
|
2283 will clear the variable definition and restore the definition of\n\ |
|
2284 @code{foo} as a function. Executing @kbd{clear foo} a second time will\n\ |
|
2285 clear the function definition.\n\ |
|
2286 \n\ |
|
2287 With -x, clear the variables that don't match the patterns.\n\ |
|
2288 \n\ |
|
2289 This command may not be used within a function body.\n\ |
|
2290 @end deffn") |
529
|
2291 { |
2086
|
2292 octave_value_list retval; |
593
|
2293 |
1755
|
2294 int argc = args.length () + 1; |
593
|
2295 |
1968
|
2296 string_vector argv = args.make_argv ("clear"); |
1755
|
2297 |
4009
|
2298 if (! error_state) |
529
|
2299 { |
4009
|
2300 if (argc == 1) |
593
|
2301 { |
4009
|
2302 do_clear_variables (); |
3681
|
2303 } |
|
2304 else |
|
2305 { |
4009
|
2306 int idx = 0; |
|
2307 |
|
2308 bool clear_all = false; |
|
2309 bool clear_functions = false; |
|
2310 bool clear_globals = false; |
|
2311 bool clear_variables = false; |
|
2312 bool exclusive = false; |
|
2313 bool have_dash_option = false; |
3681
|
2314 |
4009
|
2315 while (++idx < argc) |
|
2316 { |
4010
|
2317 if (argv[idx] == "-all" || argv[idx] == "-a") |
593
|
2318 { |
4009
|
2319 CLEAR_OPTION_ERROR (have_dash_option && ! exclusive); |
3681
|
2320 |
4009
|
2321 have_dash_option = true; |
|
2322 clear_all = true; |
|
2323 } |
4010
|
2324 else if (argv[idx] == "-exclusive" || argv[idx] == "-x") |
4009
|
2325 { |
|
2326 have_dash_option = true; |
|
2327 exclusive = true; |
|
2328 } |
4010
|
2329 else if (argv[idx] == "-functions" || argv[idx] == "-f") |
4009
|
2330 { |
|
2331 CLEAR_OPTION_ERROR (have_dash_option && ! exclusive); |
3681
|
2332 |
4009
|
2333 have_dash_option = true; |
|
2334 clear_functions = true; |
|
2335 } |
4010
|
2336 else if (argv[idx] == "-global" || argv[idx] == "-g") |
4009
|
2337 { |
|
2338 CLEAR_OPTION_ERROR (have_dash_option && ! exclusive); |
|
2339 |
|
2340 have_dash_option = true; |
|
2341 clear_globals = true; |
|
2342 } |
4010
|
2343 else if (argv[idx] == "-variables" || argv[idx] == "-v") |
4009
|
2344 { |
|
2345 CLEAR_OPTION_ERROR (have_dash_option && ! exclusive); |
3681
|
2346 |
4009
|
2347 have_dash_option = true; |
|
2348 clear_variables = true; |
|
2349 } |
|
2350 else |
|
2351 break; |
|
2352 } |
3681
|
2353 |
4224
|
2354 if (idx <= argc) |
4009
|
2355 { |
|
2356 if (! have_dash_option) |
|
2357 { |
|
2358 do_matlab_compatible_clear (argv, argc, idx); |
|
2359 } |
|
2360 else |
|
2361 { |
|
2362 if (clear_all) |
3681
|
2363 { |
4009
|
2364 maybe_warn_exclusive (exclusive); |
3681
|
2365 |
4009
|
2366 if (++idx < argc) |
|
2367 warning |
4010
|
2368 ("clear: ignoring extra arguments after -all"); |
3681
|
2369 |
4009
|
2370 curr_sym_tab->clear (); |
|
2371 fbi_sym_tab->clear_functions (); |
|
2372 global_sym_tab->clear (); |
|
2373 } |
|
2374 else if (clear_functions) |
|
2375 { |
|
2376 do_clear_functions (argv, argc, idx, exclusive); |
|
2377 } |
|
2378 else if (clear_globals) |
593
|
2379 { |
4009
|
2380 do_clear_globals (argv, argc, idx, exclusive); |
|
2381 } |
|
2382 else if (clear_variables) |
|
2383 { |
|
2384 do_clear_variables (argv, argc, idx, exclusive); |
|
2385 } |
|
2386 else |
|
2387 { |
|
2388 do_clear_symbols (argv, argc, idx, exclusive); |
593
|
2389 } |
|
2390 } |
|
2391 } |
|
2392 } |
|
2393 } |
|
2394 |
|
2395 return retval; |
529
|
2396 } |
|
2397 |
3933
|
2398 DEFUN (__print_symtab_info__, args, , |
3446
|
2399 "-*- texinfo -*-\n\ |
3933
|
2400 @deftypefn {Built-in Function} {} __print_symtab_info__ ()\n\ |
3446
|
2401 Print raw symbol table statistices.\n\ |
|
2402 @end deftypefn") |
3005
|
2403 { |
|
2404 octave_value_list retval; |
|
2405 |
|
2406 int nargin = args.length (); |
|
2407 |
|
2408 if (nargin == 1) |
|
2409 { |
3523
|
2410 std::string arg = args(0).string_value (); |
3005
|
2411 |
4009
|
2412 if (arg == "fbi") |
|
2413 fbi_sym_tab->print_info (octave_stdout); |
|
2414 else if (arg == "global") |
3933
|
2415 global_sym_tab->print_info (octave_stdout); |
|
2416 else if (arg == "top-level") |
|
2417 top_level_sym_tab->print_info (octave_stdout); |
3005
|
2418 else |
3933
|
2419 { |
4009
|
2420 symbol_record *fsr = fbi_sym_tab->lookup (arg, true); |
3933
|
2421 |
4009
|
2422 if (fsr && fsr->is_user_function ()) |
3933
|
2423 { |
4009
|
2424 octave_value tmp = fsr->def (); |
3933
|
2425 const octave_value& rep = tmp.get_rep (); |
|
2426 |
|
2427 const octave_user_function& fcn |
|
2428 = static_cast<const octave_user_function&> (rep); |
|
2429 |
|
2430 fcn.print_symtab_info (octave_stdout); |
|
2431 } |
|
2432 else |
|
2433 error ("no user-defined function named %s", arg.c_str ()); |
|
2434 } |
3005
|
2435 } |
|
2436 else if (nargin == 0) |
3933
|
2437 curr_sym_tab->print_info (octave_stdout); |
3005
|
2438 else |
3933
|
2439 print_usage ("__print_symtab_info__"); |
3005
|
2440 |
|
2441 return retval; |
|
2442 } |
|
2443 |
3933
|
2444 DEFUN (__print_symbol_info__, args, , |
3446
|
2445 "-*- texinfo -*-\n\ |
|
2446 @deftypefn {Built-in Function} {} __dump_symbol_info__ (@var{name})\n\ |
3548
|
2447 Print symbol table information for the symbol @var{name}.\n\ |
3446
|
2448 @end deftypefn") |
3239
|
2449 { |
|
2450 octave_value_list retval; |
|
2451 |
|
2452 int nargin = args.length (); |
|
2453 |
|
2454 if (nargin == 1) |
|
2455 { |
3523
|
2456 std::string symbol_name = args(0).string_value (); |
3239
|
2457 |
|
2458 if (! error_state) |
|
2459 { |
|
2460 symbol_record *sr = curr_sym_tab->lookup (symbol_name); |
|
2461 |
|
2462 if (sr) |
3933
|
2463 sr->print_info (octave_stdout); |
3239
|
2464 else |
3933
|
2465 error ("__print_symbol_info__: symbol %s not found", |
3239
|
2466 symbol_name.c_str ()); |
|
2467 } |
|
2468 else |
3933
|
2469 print_usage ("__print_symbol_info__"); |
3239
|
2470 } |
|
2471 else |
3933
|
2472 print_usage ("__print_symbol_info__"); |
3239
|
2473 |
|
2474 return retval; |
|
2475 } |
|
2476 |
3016
|
2477 // XXX FIXME XXX -- some of these should do their own checking to be |
|
2478 // able to provide more meaningful warning or error messages. |
|
2479 |
|
2480 static int |
|
2481 ignore_function_time_stamp (void) |
|
2482 { |
|
2483 int pref = 0; |
|
2484 |
3523
|
2485 std::string val = builtin_string_variable ("ignore_function_time_stamp"); |
3016
|
2486 |
|
2487 if (! val.empty ()) |
|
2488 { |
3565
|
2489 if (val == "all") |
3016
|
2490 pref = 2; |
3565
|
2491 else if (val == "system") |
3016
|
2492 pref = 1; |
|
2493 } |
|
2494 |
|
2495 Vignore_function_time_stamp = pref; |
|
2496 |
|
2497 return 0; |
|
2498 } |
|
2499 |
|
2500 // XXX FIXME XXX -- there still may be better places for some of these |
|
2501 // to be defined. |
|
2502 |
|
2503 void |
|
2504 symbols_of_variables (void) |
|
2505 { |
3258
|
2506 DEFVAR (ans, , 0, |
3372
|
2507 "-*- texinfo -*-\n\ |
|
2508 @defvr {Built-in Variable} ans\n\ |
|
2509 This variable holds the most recently computed result that was not\n\ |
|
2510 explicitly assigned to a variable. For example, after the expression\n\ |
|
2511 \n\ |
|
2512 @example\n\ |
|
2513 3^2 + 4^2\n\ |
|
2514 @end example\n\ |
|
2515 \n\ |
|
2516 @noindent\n\ |
|
2517 is evaluated, the value of @code{ans} is 25.\n\ |
|
2518 @end defvr"); |
3016
|
2519 |
3258
|
2520 DEFVAR (ignore_function_time_stamp, "system", ignore_function_time_stamp, |
3371
|
2521 "-*- texinfo -*-\n\ |
|
2522 @defvr {Built-in Variable} ignore_function_time_stamp\n\ |
|
2523 This variable can be used to prevent Octave from making the system call\n\ |
|
2524 @code{stat} each time it looks up functions defined in function files.\n\ |
|
2525 If @code{ignore_function_time_stamp} to @code{\"system\"}, Octave will not\n\ |
|
2526 automatically recompile function files in subdirectories of\n\ |
|
2527 @file{@var{octave-home}/lib/@var{version}} if they have changed since\n\ |
|
2528 they were last compiled, but will recompile other function files in the\n\ |
|
2529 @code{LOADPATH} if they change. If set to @code{\"all\"}, Octave will not\n\ |
|
2530 recompile any function files unless their definitions are removed with\n\ |
|
2531 @code{clear}. For any other value of @code{ignore_function_time_stamp},\n\ |
|
2532 Octave will always check to see if functions defined in function files\n\ |
|
2533 need to recompiled. The default value of @code{ignore_function_time_stamp} is\n\ |
|
2534 @code{\"system\"}.\n\ |
|
2535 @end defvr"); |
3016
|
2536 } |
|
2537 |
1
|
2538 /* |
|
2539 ;;; Local Variables: *** |
|
2540 ;;; mode: C++ *** |
|
2541 ;;; End: *** |
|
2542 */ |