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