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