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