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 |
|
279 static inline bool |
|
280 is_marked_as_rawcommand (const std::string& s) |
|
281 { |
|
282 return rawcommand_set.find (s) != rawcommand_set.end (); |
|
283 } |
|
284 |
|
285 static inline void |
|
286 mark_as_rawcommand (const std::string& s) |
|
287 { |
|
288 command_set.insert (s); |
|
289 rawcommand_set.insert (s); |
|
290 } |
|
291 |
|
292 static inline void |
|
293 unmark_rawcommand (const std::string& s) |
|
294 { |
|
295 rawcommand_set.erase (s); |
|
296 |
|
297 symbol_record *sr = fbi_sym_tab->lookup (s); |
|
298 |
|
299 if (sr) |
|
300 sr->unmark_rawcommand (); |
|
301 } |
|
302 |
|
303 DEFCMD (mark_as_rawcommand, args, , |
|
304 "-*- texinfo -*-\n\ |
|
305 @deftypefn {Built-in Function} {} mark_as_rawcommand (@var{name})\n\ |
|
306 Enter @var{name} into the list of raw input commands and to the list of\n\ |
|
307 command style functions.\n\ |
|
308 Raw input commands are like normal command style functions, but they\n\ |
|
309 receive their input unprocessed (ie. strings still contain the quotes\n\ |
|
310 and escapes they had when input). However, comments and continuations\n\ |
|
311 are handled as usual, you cannot pass a token starting with a comment\n\ |
|
312 character ('#' or '%') to your function, and the last token cannot be\n\ |
|
313 a continuation token ('\\' or '...').\n\ |
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 |
2926
|
983 static 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; |
|
1628 if (curr_caller_function) |
|
1629 caller_function_name = curr_caller_function->name (); |
|
1630 |
|
1631 ni.assign ("function", caller_function_name); |
|
1632 ni.assign ("level", 1); |
|
1633 |
|
1634 name_info(j) = sr->name (); |
|
1635 size_info(j) = sr->size (); |
|
1636 bytes_info(j) = sr->byte_size (); |
|
1637 class_info(j) = sr->class_name (); |
|
1638 global_info(j) = sr->is_linked_to_global (); |
|
1639 sparse_info(j) = sr->is_sparse_type (); |
|
1640 complex_info(j) = sr->is_complex_type (); |
|
1641 nesting_info(j) = ni; |
|
1642 } |
|
1643 |
|
1644 Octave_map info; |
|
1645 |
|
1646 info.assign ("name", name_info); |
|
1647 info.assign ("size", size_info); |
|
1648 info.assign ("bytes", bytes_info); |
|
1649 info.assign ("class", class_info); |
|
1650 info.assign ("global", global_info); |
|
1651 info.assign ("sparse", sparse_info); |
|
1652 info.assign ("complex", complex_info); |
|
1653 info.assign ("nesting", nesting_info); |
|
1654 |
|
1655 retval = info; |
4435
|
1656 } |
|
1657 else |
5659
|
1658 { |
|
1659 string_vector names; |
|
1660 |
|
1661 if (symbols_len > 0) |
|
1662 { |
|
1663 names.resize (symbols_len); |
|
1664 |
|
1665 for (octave_idx_type j = 0; j < symbols_len; j++) |
|
1666 names[j] = symbols(j)->name (); |
|
1667 } |
|
1668 |
|
1669 retval = Cell (names); |
|
1670 } |
529
|
1671 } |
4435
|
1672 else |
529
|
1673 { |
4435
|
1674 int pad_after = 0; |
|
1675 |
|
1676 if (show_builtins) |
|
1677 { |
|
1678 pad_after += fbi_sym_tab->maybe_list |
|
1679 ("*** built-in constants:", pats, octave_stdout, |
|
1680 show_verbose, symbol_record::BUILTIN_CONSTANT, SYMTAB_ALL_SCOPES); |
|
1681 |
|
1682 pad_after += fbi_sym_tab->maybe_list |
|
1683 ("*** built-in variables:", pats, octave_stdout, |
|
1684 show_verbose, symbol_record::BUILTIN_VARIABLE, SYMTAB_ALL_SCOPES); |
|
1685 |
|
1686 pad_after += fbi_sym_tab->maybe_list |
|
1687 ("*** built-in functions:", pats, octave_stdout, |
|
1688 show_verbose, symbol_record::BUILTIN_FUNCTION, SYMTAB_ALL_SCOPES); |
|
1689 } |
529
|
1690 |
4435
|
1691 if (show_functions) |
|
1692 { |
|
1693 pad_after += fbi_sym_tab->maybe_list |
|
1694 ("*** dynamically linked functions:", pats, |
|
1695 octave_stdout, show_verbose, symbol_record::DLD_FUNCTION, |
|
1696 SYMTAB_ALL_SCOPES); |
|
1697 |
|
1698 pad_after += fbi_sym_tab->maybe_list |
|
1699 ("*** currently compiled functions:", pats, |
|
1700 octave_stdout, show_verbose, symbol_record::USER_FUNCTION, |
|
1701 SYMTAB_ALL_SCOPES); |
|
1702 } |
|
1703 |
|
1704 if (show_variables) |
|
1705 { |
|
1706 pad_after += curr_sym_tab->maybe_list |
|
1707 ("*** local user variables:", pats, octave_stdout, |
|
1708 show_verbose, symbol_record::USER_VARIABLE, SYMTAB_LOCAL_SCOPE); |
|
1709 |
|
1710 pad_after += curr_sym_tab->maybe_list |
|
1711 ("*** globally visible user variables:", pats, |
|
1712 octave_stdout, show_verbose, symbol_record::USER_VARIABLE, |
|
1713 SYMTAB_GLOBAL_SCOPE); |
|
1714 } |
|
1715 |
|
1716 if (pad_after) |
|
1717 octave_stdout << "\n"; |
529
|
1718 } |
|
1719 |
581
|
1720 return retval; |
|
1721 } |
|
1722 |
4435
|
1723 DEFCMD (who, args, nargout, |
3361
|
1724 "-*- texinfo -*-\n\ |
|
1725 @deffn {Command} who options pattern @dots{}\n\ |
|
1726 @deffnx {Command} whos options pattern @dots{}\n\ |
|
1727 List currently defined symbols matching the given patterns. The\n\ |
|
1728 following are valid options. They may be shortened to one character but\n\ |
|
1729 may not be combined.\n\ |
|
1730 \n\ |
|
1731 @table @code\n\ |
|
1732 @item -all\n\ |
|
1733 List all currently defined symbols.\n\ |
|
1734 \n\ |
|
1735 @item -builtins\n\ |
|
1736 List built-in variables and functions. This includes all currently\n\ |
|
1737 compiled function files, but does not include all function files that\n\ |
|
1738 are in the @code{LOADPATH}.\n\ |
581
|
1739 \n\ |
3361
|
1740 @item -functions\n\ |
|
1741 List user-defined functions.\n\ |
|
1742 \n\ |
|
1743 @item -long\n\ |
|
1744 Print a long listing including the type and dimensions of any symbols.\n\ |
|
1745 The symbols in the first column of output indicate whether it is\n\ |
|
1746 possible to redefine the symbol, and whether it is possible for it to be\n\ |
|
1747 cleared.\n\ |
|
1748 \n\ |
|
1749 @item -variables\n\ |
|
1750 List user-defined variables.\n\ |
|
1751 @end table\n\ |
|
1752 \n\ |
|
1753 Valid patterns are the same as described for the @code{clear} command\n\ |
|
1754 above. If no patterns are supplied, all symbols from the given category\n\ |
|
1755 are listed. By default, only user defined functions and variables\n\ |
|
1756 visible in the local scope are displayed.\n\ |
|
1757 \n\ |
|
1758 The command @kbd{whos} is equivalent to @kbd{who -long}.\n\ |
|
1759 @end deffn") |
581
|
1760 { |
4435
|
1761 octave_value retval; |
581
|
1762 |
4435
|
1763 if (nargout < 2) |
|
1764 { |
|
1765 int argc = args.length () + 1; |
|
1766 |
|
1767 string_vector argv = args.make_argv ("who"); |
1755
|
1768 |
4435
|
1769 if (error_state) |
|
1770 return retval; |
1755
|
1771 |
4435
|
1772 retval = do_who (argc, argv, nargout == 1); |
|
1773 } |
|
1774 else |
|
1775 print_usage ("who"); |
581
|
1776 |
529
|
1777 return retval; |
|
1778 } |
|
1779 |
4435
|
1780 DEFCMD (whos, args, nargout, |
3458
|
1781 "-*- texinfo -*-\n\ |
|
1782 @deffn {Command} whos options pattern @dots{}\n\ |
|
1783 See who.\n\ |
|
1784 @end deffn") |
581
|
1785 { |
4435
|
1786 octave_value retval; |
712
|
1787 |
4435
|
1788 if (nargout < 2) |
|
1789 { |
|
1790 int nargin = args.length (); |
|
1791 |
|
1792 octave_value_list tmp_args; |
|
1793 |
|
1794 for (int i = nargin; i > 0; i--) |
|
1795 tmp_args(i) = args(i-1); |
581
|
1796 |
4435
|
1797 tmp_args(0) = "-long"; |
1755
|
1798 |
4435
|
1799 int argc = tmp_args.length () + 1; |
|
1800 |
|
1801 string_vector argv = tmp_args.make_argv ("whos"); |
581
|
1802 |
4435
|
1803 if (error_state) |
|
1804 return retval; |
581
|
1805 |
4435
|
1806 retval = do_who (argc, argv, nargout == 1); |
|
1807 } |
|
1808 else |
|
1809 print_usage ("whos"); |
581
|
1810 |
|
1811 return retval; |
|
1812 } |
|
1813 |
593
|
1814 // Defining variables. |
|
1815 |
1162
|
1816 void |
2856
|
1817 bind_ans (const octave_value& val, bool print) |
1162
|
1818 { |
4009
|
1819 static symbol_record *sr = fbi_sym_tab->lookup ("ans", true); |
1162
|
1820 |
2978
|
1821 if (val.is_defined ()) |
|
1822 { |
|
1823 sr->define (val); |
1162
|
1824 |
2978
|
1825 if (print) |
|
1826 val.print_with_name (octave_stdout, "ans"); |
|
1827 } |
1162
|
1828 } |
|
1829 |
3259
|
1830 // Give a global constant a definition. This will insert the symbol |
|
1831 // in the global table if necessary. |
|
1832 |
|
1833 // How is this different than install_builtin_constant? Are both |
|
1834 // functions needed? |
|
1835 |
|
1836 void |
3523
|
1837 bind_builtin_constant (const std::string& name, const octave_value& val, |
|
1838 bool protect, bool eternal, const std::string& help) |
3259
|
1839 { |
4009
|
1840 symbol_record *sym_rec = fbi_sym_tab->lookup (name, true); |
3259
|
1841 sym_rec->unprotect (); |
|
1842 |
3523
|
1843 std::string tmp_help = help.empty () ? sym_rec->help () : help; |
3259
|
1844 |
|
1845 sym_rec->define_builtin_const (val); |
|
1846 |
|
1847 sym_rec->document (tmp_help); |
|
1848 |
|
1849 if (protect) |
|
1850 sym_rec->protect (); |
|
1851 |
|
1852 if (eternal) |
|
1853 sym_rec->make_eternal (); |
|
1854 } |
|
1855 |
593
|
1856 // Give a global variable a definition. This will insert the symbol |
|
1857 // in the global table if necessary. |
|
1858 |
|
1859 // How is this different than install_builtin_variable? Are both |
|
1860 // functions needed? |
|
1861 |
|
1862 void |
3523
|
1863 bind_builtin_variable (const std::string& varname, const octave_value& val, |
2953
|
1864 bool protect, bool eternal, |
3005
|
1865 symbol_record::change_function chg_fcn, |
3523
|
1866 const std::string& help) |
593
|
1867 { |
4009
|
1868 symbol_record *sr = fbi_sym_tab->lookup (varname, true); |
593
|
1869 |
1271
|
1870 // It is a programming error for a builtin symbol to be missing. |
|
1871 // Besides, we just inserted it, so it must be there. |
593
|
1872 |
4280
|
1873 // Use != here to avoid possible conversion to int of smaller type |
|
1874 // than the sr pointer. |
|
1875 |
|
1876 assert (sr != 0); |
593
|
1877 |
|
1878 sr->unprotect (); |
|
1879 |
1271
|
1880 // Must do this before define, since define will call the special |
|
1881 // variable function only if it knows about it, and it needs to, so |
|
1882 // that user prefs can be properly initialized. |
593
|
1883 |
3005
|
1884 if (chg_fcn) |
|
1885 sr->set_change_function (chg_fcn); |
593
|
1886 |
|
1887 sr->define_builtin_var (val); |
|
1888 |
|
1889 if (protect) |
|
1890 sr->protect (); |
|
1891 |
|
1892 if (eternal) |
|
1893 sr->make_eternal (); |
|
1894 |
1755
|
1895 sr->document (help); |
529
|
1896 } |
|
1897 |
4319
|
1898 void |
|
1899 mlock (const std::string& nm) |
|
1900 { |
|
1901 symbol_record *sr = fbi_sym_tab->lookup (nm, true); |
|
1902 |
|
1903 if (sr) |
|
1904 sr->mark_as_static (); |
|
1905 } |
|
1906 |
|
1907 void |
|
1908 munlock (const std::string& nm) |
|
1909 { |
|
1910 symbol_record *sr = fbi_sym_tab->lookup (nm); |
|
1911 |
|
1912 if (sr && sr->is_static ()) |
|
1913 sr->unmark_static (); |
|
1914 else |
|
1915 error ("munlock: %s is not locked", nm.c_str ()); |
|
1916 } |
|
1917 |
|
1918 bool |
|
1919 mislocked (const std::string& nm) |
|
1920 { |
|
1921 symbol_record *sr = fbi_sym_tab->lookup (nm); |
|
1922 |
|
1923 return (sr && sr->is_static ()); |
|
1924 } |
|
1925 |
|
1926 DEFCMD (mlock, args, , |
|
1927 "-*- texinfo -*-\n\ |
4526
|
1928 @deftypefn {Built-in Function} {} mlock (@var{name})\n\ |
4319
|
1929 Lock the named function into memory. If no function is named\n\ |
|
1930 then lock in the current function.\n\ |
5642
|
1931 @seealso{munlock, mislocked, persistent}\n\ |
|
1932 @end deftypefn") |
4319
|
1933 { |
|
1934 octave_value_list retval; |
|
1935 |
|
1936 if (args.length () == 1) |
|
1937 { |
|
1938 std::string name = args(0).string_value (); |
|
1939 |
|
1940 if (! error_state) |
|
1941 mlock (name); |
|
1942 else |
|
1943 error ("mlock: expecting argument to be a function name"); |
|
1944 } |
|
1945 else if (args.length () == 0) |
|
1946 { |
|
1947 if (curr_function) |
4748
|
1948 mlock (curr_function->name ()); |
4319
|
1949 else |
|
1950 error ("mlock: invalid use outside a function"); |
|
1951 } |
|
1952 else |
|
1953 print_usage ("mlock"); |
|
1954 |
|
1955 return retval; |
|
1956 } |
|
1957 |
|
1958 DEFCMD (munlock, args, , |
|
1959 "-*- texinfo -*-\n\ |
|
1960 @deftypefn {Built-in Function} {} munlock (@var{fcn})\n\ |
|
1961 Unlock the named function. If no function is named\n\ |
|
1962 then unlock the current function.\n\ |
5642
|
1963 @seealso{mlock, mislocked, persistent}\n\ |
|
1964 @end deftypefn") |
4319
|
1965 { |
|
1966 octave_value_list retval; |
|
1967 |
|
1968 if (args.length() == 1) |
|
1969 { |
|
1970 std::string name = args(0).string_value (); |
|
1971 |
|
1972 if (! error_state) |
|
1973 munlock (name); |
|
1974 else |
|
1975 error ("munlock: expecting argument to be a function name"); |
|
1976 } |
|
1977 else if (args.length () == 0) |
|
1978 { |
|
1979 if (curr_function) |
4748
|
1980 mlock (curr_function->name ()); |
4319
|
1981 else |
|
1982 error ("munlock: invalid use outside a function"); |
|
1983 } |
|
1984 else |
|
1985 print_usage ("munlock"); |
|
1986 |
|
1987 return retval; |
|
1988 } |
|
1989 |
|
1990 |
|
1991 DEFCMD (mislocked, args, , |
|
1992 "-*- texinfo -*-\n\ |
|
1993 @deftypefn {Built-in Function} {} mislocked (@var{fcn})\n\ |
|
1994 Return true if the named function is locked. If no function is named\n\ |
|
1995 then return true if the current function is locked.\n\ |
5642
|
1996 @seealso{mlock, munlock, persistent}\n\ |
|
1997 @end deftypefn") |
4319
|
1998 { |
|
1999 octave_value retval; |
|
2000 |
|
2001 if (args.length() == 1) |
|
2002 { |
|
2003 std::string name = args(0).string_value (); |
|
2004 |
|
2005 if (! error_state) |
|
2006 retval = mislocked (name); |
|
2007 else |
|
2008 error ("mislocked: expecting argument to be a function name"); |
|
2009 } |
|
2010 else if (args.length () == 0) |
|
2011 { |
|
2012 if (curr_function) |
4748
|
2013 retval = mislocked (curr_function->name ()); |
4319
|
2014 else |
|
2015 error ("mislocked: invalid use outside a function"); |
|
2016 } |
|
2017 else |
|
2018 print_usage ("mislocked"); |
|
2019 |
|
2020 return retval; |
|
2021 } |
|
2022 |
593
|
2023 // Deleting names from the symbol tables. |
|
2024 |
3681
|
2025 static inline bool |
4009
|
2026 name_matches_any_pattern (const std::string& nm, |
|
2027 const string_vector& argv, int argc, int idx) |
3681
|
2028 { |
|
2029 bool retval = false; |
|
2030 |
|
2031 for (int k = idx; k < argc; k++) |
|
2032 { |
|
2033 std::string patstr = argv[k]; |
|
2034 |
|
2035 if (! patstr.empty ()) |
|
2036 { |
|
2037 glob_match pattern (patstr); |
|
2038 |
|
2039 if (pattern.match (nm)) |
|
2040 { |
|
2041 retval = true; |
|
2042 break; |
|
2043 } |
|
2044 } |
|
2045 } |
|
2046 |
|
2047 return retval; |
|
2048 } |
|
2049 |
4009
|
2050 static inline bool |
|
2051 is_local_variable (const std::string& nm) |
|
2052 { |
|
2053 symbol_record *sr = curr_sym_tab->lookup (nm); |
|
2054 |
|
2055 return (sr && sr->is_variable ()); |
|
2056 } |
|
2057 |
|
2058 static inline void |
|
2059 maybe_warn_exclusive (bool exclusive) |
|
2060 { |
|
2061 if (exclusive) |
|
2062 warning ("clear: ignoring --exclusive option"); |
|
2063 } |
|
2064 |
|
2065 static inline void |
|
2066 do_clear_all (void) |
|
2067 { |
|
2068 curr_sym_tab->clear (); |
|
2069 fbi_sym_tab->clear_functions (); |
|
2070 global_sym_tab->clear (); |
|
2071 } |
|
2072 |
|
2073 static inline void |
|
2074 do_clear_functions (void) |
|
2075 { |
|
2076 curr_sym_tab->clear_functions (); |
|
2077 fbi_sym_tab->clear_functions (); |
|
2078 } |
|
2079 |
|
2080 static inline void |
|
2081 do_clear_globals (void) |
|
2082 { |
|
2083 curr_sym_tab->clear_globals (); |
|
2084 global_sym_tab->clear (); |
|
2085 } |
|
2086 |
|
2087 static inline void |
|
2088 do_clear_variables (void) |
|
2089 { |
|
2090 curr_sym_tab->clear (); |
|
2091 } |
|
2092 |
|
2093 static inline bool |
|
2094 do_clear_function (const std::string& nm) |
|
2095 { |
|
2096 bool b1 = curr_sym_tab->clear_function (nm); |
|
2097 |
|
2098 bool b2 = fbi_sym_tab->clear_function (nm); |
|
2099 |
|
2100 return b1 || b2; |
|
2101 } |
|
2102 |
|
2103 static inline bool |
|
2104 do_clear_global (const std::string& nm) |
|
2105 { |
|
2106 bool b1 = curr_sym_tab->clear_global (nm); |
|
2107 |
|
2108 bool b2 = global_sym_tab->clear_variable (nm); |
|
2109 |
|
2110 return b1 || b2; |
|
2111 } |
|
2112 |
|
2113 static inline bool |
|
2114 do_clear_variable (const std::string& nm) |
|
2115 { |
|
2116 return curr_sym_tab->clear_variable (nm); |
|
2117 } |
|
2118 |
|
2119 static inline bool |
|
2120 do_clear_symbol (const std::string& nm) |
|
2121 { |
|
2122 bool cleared = curr_sym_tab->clear_variable (nm); |
|
2123 |
|
2124 if (! cleared) |
|
2125 cleared = do_clear_function (nm); |
|
2126 |
|
2127 return cleared; |
|
2128 } |
|
2129 |
|
2130 static inline bool |
|
2131 do_clear_function_pattern (const std::string& pat) |
|
2132 { |
|
2133 bool b1 = curr_sym_tab->clear_function_pattern (pat); |
|
2134 |
|
2135 bool b2 = fbi_sym_tab->clear_function_pattern (pat); |
|
2136 |
|
2137 return b1 || b2; |
|
2138 } |
|
2139 |
|
2140 static inline bool |
|
2141 do_clear_global_pattern (const std::string& pat) |
|
2142 { |
|
2143 bool b1 = curr_sym_tab->clear_global_pattern (pat); |
|
2144 |
|
2145 bool b2 = global_sym_tab->clear_variable_pattern (pat); |
|
2146 |
|
2147 return b1 || b2; |
|
2148 } |
|
2149 |
|
2150 static inline bool |
|
2151 do_clear_variable_pattern (const std::string& pat) |
|
2152 { |
|
2153 return curr_sym_tab->clear_variable_pattern (pat); |
|
2154 } |
|
2155 |
|
2156 static inline bool |
|
2157 do_clear_symbol_pattern (const std::string& pat) |
|
2158 { |
|
2159 // XXX FIXME XXX -- if we have a variable v1 and a function v2 and |
|
2160 // someone says clear v*, we will clear the variable but not the |
|
2161 // function. Is that really what should happen? (I think it is |
|
2162 // what Matlab does.) |
|
2163 |
|
2164 bool cleared = curr_sym_tab->clear_variable_pattern (pat); |
|
2165 |
|
2166 if (! cleared) |
|
2167 cleared = do_clear_function_pattern (pat); |
|
2168 |
|
2169 return cleared; |
|
2170 } |
|
2171 |
|
2172 static inline void |
|
2173 do_clear_functions (const string_vector& argv, int argc, int idx, |
|
2174 bool exclusive = false) |
|
2175 { |
|
2176 if (idx == argc) |
|
2177 do_clear_functions (); |
|
2178 else |
|
2179 { |
|
2180 if (exclusive) |
|
2181 { |
|
2182 string_vector lfcns = curr_sym_tab->user_function_name_list (); |
|
2183 |
|
2184 int lcount = lfcns.length (); |
|
2185 |
|
2186 for (int i = 0; i < lcount; i++) |
|
2187 { |
|
2188 std::string nm = lfcns[i]; |
|
2189 |
|
2190 if (! name_matches_any_pattern (nm, argv, argc, idx)) |
|
2191 do_clear_function (nm); |
|
2192 } |
|
2193 |
|
2194 string_vector fcns = fbi_sym_tab->user_function_name_list (); |
|
2195 |
|
2196 int fcount = fcns.length (); |
|
2197 |
|
2198 for (int i = 0; i < fcount; i++) |
|
2199 { |
|
2200 std::string nm = fcns[i]; |
|
2201 |
|
2202 if (! name_matches_any_pattern (nm, argv, argc, idx)) |
|
2203 do_clear_function (nm); |
|
2204 } |
|
2205 } |
|
2206 else |
|
2207 { |
|
2208 while (idx < argc) |
|
2209 do_clear_function_pattern (argv[idx++]); |
|
2210 } |
|
2211 } |
|
2212 } |
|
2213 |
|
2214 static inline void |
|
2215 do_clear_globals (const string_vector& argv, int argc, int idx, |
|
2216 bool exclusive = false) |
|
2217 { |
|
2218 if (idx == argc) |
|
2219 do_clear_globals (); |
|
2220 else |
|
2221 { |
|
2222 if (exclusive) |
|
2223 { |
|
2224 string_vector lvars = curr_sym_tab->global_variable_name_list (); |
|
2225 |
|
2226 int lcount = lvars.length (); |
|
2227 |
|
2228 for (int i = 0; i < lcount; i++) |
|
2229 { |
|
2230 std::string nm = lvars[i]; |
|
2231 |
|
2232 if (! name_matches_any_pattern (nm, argv, argc, idx)) |
|
2233 do_clear_global (nm); |
|
2234 } |
|
2235 |
|
2236 string_vector gvars = global_sym_tab->global_variable_name_list (); |
|
2237 |
|
2238 int gcount = gvars.length (); |
|
2239 |
|
2240 for (int i = 0; i < gcount; i++) |
|
2241 { |
|
2242 std::string nm = gvars[i]; |
|
2243 |
|
2244 if (! name_matches_any_pattern (nm, argv, argc, idx)) |
|
2245 do_clear_global (nm); |
|
2246 } |
|
2247 } |
|
2248 else |
|
2249 { |
|
2250 while (idx < argc) |
|
2251 do_clear_global_pattern (argv[idx++]); |
|
2252 } |
|
2253 } |
|
2254 } |
|
2255 |
|
2256 static inline void |
|
2257 do_clear_variables (const string_vector& argv, int argc, int idx, |
|
2258 bool exclusive = false) |
|
2259 { |
|
2260 if (idx == argc) |
|
2261 do_clear_variables (); |
|
2262 else |
|
2263 { |
|
2264 if (exclusive) |
|
2265 { |
|
2266 string_vector lvars = curr_sym_tab->variable_name_list (); |
|
2267 |
|
2268 int lcount = lvars.length (); |
|
2269 |
|
2270 for (int i = 0; i < lcount; i++) |
|
2271 { |
|
2272 std::string nm = lvars[i]; |
|
2273 |
|
2274 if (! name_matches_any_pattern (nm, argv, argc, idx)) |
|
2275 do_clear_variable (nm); |
|
2276 } |
|
2277 } |
|
2278 else |
|
2279 { |
|
2280 while (idx < argc) |
|
2281 do_clear_variable_pattern (argv[idx++]); |
|
2282 } |
|
2283 } |
|
2284 } |
|
2285 |
|
2286 static inline void |
|
2287 do_clear_symbols (const string_vector& argv, int argc, int idx, |
|
2288 bool exclusive = false) |
|
2289 { |
|
2290 if (idx == argc) |
|
2291 do_clear_variables (); |
|
2292 else |
|
2293 { |
|
2294 if (exclusive) |
|
2295 { |
|
2296 // XXX FIXME XXX -- is this really what we want, or do we |
|
2297 // somehow want to only clear the functions that are not |
|
2298 // shadowed by local variables? It seems that would be a |
|
2299 // bit harder to do. |
|
2300 |
|
2301 do_clear_variables (argv, argc, idx, exclusive); |
|
2302 do_clear_functions (argv, argc, idx, exclusive); |
|
2303 } |
|
2304 else |
|
2305 { |
|
2306 while (idx < argc) |
|
2307 do_clear_symbol_pattern (argv[idx++]); |
|
2308 } |
|
2309 } |
|
2310 } |
|
2311 |
|
2312 static void |
|
2313 do_matlab_compatible_clear (const string_vector& argv, int argc, int idx) |
|
2314 { |
|
2315 // This is supposed to be mostly Matlab compatible. |
|
2316 |
|
2317 for (; idx < argc; idx++) |
|
2318 { |
|
2319 if (argv[idx] == "all" && ! is_local_variable ("all")) |
|
2320 { |
|
2321 do_clear_all (); |
|
2322 } |
|
2323 else if (argv[idx] == "functions" && ! is_local_variable ("functions")) |
|
2324 { |
|
2325 do_clear_functions (argv, argc, ++idx); |
|
2326 } |
|
2327 else if (argv[idx] == "global" && ! is_local_variable ("global")) |
|
2328 { |
|
2329 do_clear_globals (argv, argc, ++idx); |
|
2330 } |
|
2331 else if (argv[idx] == "variables" && ! is_local_variable ("variables")) |
|
2332 { |
|
2333 do_clear_variables (); |
|
2334 } |
|
2335 else |
|
2336 { |
|
2337 do_clear_symbol_pattern (argv[idx]); |
|
2338 } |
|
2339 } |
|
2340 } |
|
2341 |
|
2342 #define CLEAR_OPTION_ERROR(cond) \ |
|
2343 do \ |
|
2344 { \ |
|
2345 if (cond) \ |
|
2346 { \ |
|
2347 print_usage ("clear"); \ |
|
2348 return retval; \ |
|
2349 } \ |
|
2350 } \ |
|
2351 while (0) |
|
2352 |
4954
|
2353 bool |
|
2354 clear_function (const std::string& nm) |
|
2355 { |
|
2356 return do_clear_function (nm); |
|
2357 } |
|
2358 |
4988
|
2359 bool |
|
2360 clear_variable (const std::string& nm) |
|
2361 { |
|
2362 return do_clear_variable (nm); |
|
2363 } |
|
2364 |
|
2365 bool |
|
2366 clear_symbol (const std::string& nm) |
|
2367 { |
|
2368 return do_clear_symbol (nm); |
|
2369 } |
|
2370 |
4208
|
2371 DEFCMD (clear, args, , |
3361
|
2372 "-*- texinfo -*-\n\ |
|
2373 @deffn {Command} clear [-x] pattern @dots{}\n\ |
|
2374 Delete the names matching the given patterns from the symbol table. The\n\ |
|
2375 pattern may contain the following special characters:\n\ |
4016
|
2376 \n\ |
3361
|
2377 @table @code\n\ |
|
2378 @item ?\n\ |
|
2379 Match any single character.\n\ |
668
|
2380 \n\ |
3361
|
2381 @item *\n\ |
|
2382 Match zero or more characters.\n\ |
|
2383 \n\ |
|
2384 @item [ @var{list} ]\n\ |
|
2385 Match the list of characters specified by @var{list}. If the first\n\ |
|
2386 character is @code{!} or @code{^}, match all characters except those\n\ |
|
2387 specified by @var{list}. For example, the pattern @samp{[a-zA-Z]} will\n\ |
|
2388 match all lower and upper case alphabetic characters.\n\ |
|
2389 @end table\n\ |
|
2390 \n\ |
|
2391 For example, the command\n\ |
593
|
2392 \n\ |
3361
|
2393 @example\n\ |
|
2394 clear foo b*r\n\ |
|
2395 @end example\n\ |
|
2396 \n\ |
|
2397 @noindent\n\ |
|
2398 clears the name @code{foo} and all names that begin with the letter\n\ |
|
2399 @code{b} and end with the letter @code{r}.\n\ |
668
|
2400 \n\ |
3361
|
2401 If @code{clear} is called without any arguments, all user-defined\n\ |
|
2402 variables (local and global) are cleared from the symbol table. If\n\ |
|
2403 @code{clear} is called with at least one argument, only the visible\n\ |
|
2404 names matching the arguments are cleared. For example, suppose you have\n\ |
|
2405 defined a function @code{foo}, and then hidden it by performing the\n\ |
|
2406 assignment @code{foo = 2}. Executing the command @kbd{clear foo} once\n\ |
|
2407 will clear the variable definition and restore the definition of\n\ |
|
2408 @code{foo} as a function. Executing @kbd{clear foo} a second time will\n\ |
|
2409 clear the function definition.\n\ |
|
2410 \n\ |
|
2411 With -x, clear the variables that don't match the patterns.\n\ |
|
2412 \n\ |
|
2413 This command may not be used within a function body.\n\ |
|
2414 @end deffn") |
529
|
2415 { |
2086
|
2416 octave_value_list retval; |
593
|
2417 |
1755
|
2418 int argc = args.length () + 1; |
593
|
2419 |
1968
|
2420 string_vector argv = args.make_argv ("clear"); |
1755
|
2421 |
4009
|
2422 if (! error_state) |
529
|
2423 { |
4009
|
2424 if (argc == 1) |
593
|
2425 { |
4009
|
2426 do_clear_variables (); |
3681
|
2427 } |
|
2428 else |
|
2429 { |
4009
|
2430 int idx = 0; |
|
2431 |
|
2432 bool clear_all = false; |
|
2433 bool clear_functions = false; |
|
2434 bool clear_globals = false; |
|
2435 bool clear_variables = false; |
|
2436 bool exclusive = false; |
|
2437 bool have_dash_option = false; |
3681
|
2438 |
4009
|
2439 while (++idx < argc) |
|
2440 { |
4010
|
2441 if (argv[idx] == "-all" || argv[idx] == "-a") |
593
|
2442 { |
4009
|
2443 CLEAR_OPTION_ERROR (have_dash_option && ! exclusive); |
3681
|
2444 |
4009
|
2445 have_dash_option = true; |
|
2446 clear_all = true; |
|
2447 } |
4010
|
2448 else if (argv[idx] == "-exclusive" || argv[idx] == "-x") |
4009
|
2449 { |
|
2450 have_dash_option = true; |
|
2451 exclusive = true; |
|
2452 } |
4010
|
2453 else if (argv[idx] == "-functions" || argv[idx] == "-f") |
4009
|
2454 { |
|
2455 CLEAR_OPTION_ERROR (have_dash_option && ! exclusive); |
3681
|
2456 |
4009
|
2457 have_dash_option = true; |
|
2458 clear_functions = true; |
|
2459 } |
4010
|
2460 else if (argv[idx] == "-global" || argv[idx] == "-g") |
4009
|
2461 { |
|
2462 CLEAR_OPTION_ERROR (have_dash_option && ! exclusive); |
|
2463 |
|
2464 have_dash_option = true; |
|
2465 clear_globals = true; |
|
2466 } |
4010
|
2467 else if (argv[idx] == "-variables" || argv[idx] == "-v") |
4009
|
2468 { |
|
2469 CLEAR_OPTION_ERROR (have_dash_option && ! exclusive); |
3681
|
2470 |
4009
|
2471 have_dash_option = true; |
|
2472 clear_variables = true; |
|
2473 } |
|
2474 else |
|
2475 break; |
|
2476 } |
3681
|
2477 |
4224
|
2478 if (idx <= argc) |
4009
|
2479 { |
|
2480 if (! have_dash_option) |
|
2481 { |
|
2482 do_matlab_compatible_clear (argv, argc, idx); |
|
2483 } |
|
2484 else |
|
2485 { |
|
2486 if (clear_all) |
3681
|
2487 { |
4009
|
2488 maybe_warn_exclusive (exclusive); |
3681
|
2489 |
4009
|
2490 if (++idx < argc) |
|
2491 warning |
4010
|
2492 ("clear: ignoring extra arguments after -all"); |
3681
|
2493 |
4009
|
2494 curr_sym_tab->clear (); |
|
2495 fbi_sym_tab->clear_functions (); |
|
2496 global_sym_tab->clear (); |
|
2497 } |
|
2498 else if (clear_functions) |
|
2499 { |
|
2500 do_clear_functions (argv, argc, idx, exclusive); |
|
2501 } |
|
2502 else if (clear_globals) |
593
|
2503 { |
4009
|
2504 do_clear_globals (argv, argc, idx, exclusive); |
|
2505 } |
|
2506 else if (clear_variables) |
|
2507 { |
|
2508 do_clear_variables (argv, argc, idx, exclusive); |
|
2509 } |
|
2510 else |
|
2511 { |
|
2512 do_clear_symbols (argv, argc, idx, exclusive); |
593
|
2513 } |
|
2514 } |
|
2515 } |
|
2516 } |
|
2517 } |
|
2518 |
|
2519 return retval; |
529
|
2520 } |
|
2521 |
3933
|
2522 DEFUN (__print_symtab_info__, args, , |
3446
|
2523 "-*- texinfo -*-\n\ |
3933
|
2524 @deftypefn {Built-in Function} {} __print_symtab_info__ ()\n\ |
3446
|
2525 Print raw symbol table statistices.\n\ |
|
2526 @end deftypefn") |
3005
|
2527 { |
|
2528 octave_value_list retval; |
|
2529 |
|
2530 int nargin = args.length (); |
|
2531 |
|
2532 if (nargin == 1) |
|
2533 { |
3523
|
2534 std::string arg = args(0).string_value (); |
3005
|
2535 |
4009
|
2536 if (arg == "fbi") |
|
2537 fbi_sym_tab->print_info (octave_stdout); |
|
2538 else if (arg == "global") |
3933
|
2539 global_sym_tab->print_info (octave_stdout); |
|
2540 else if (arg == "top-level") |
|
2541 top_level_sym_tab->print_info (octave_stdout); |
3005
|
2542 else |
3933
|
2543 { |
4009
|
2544 symbol_record *fsr = fbi_sym_tab->lookup (arg, true); |
3933
|
2545 |
4009
|
2546 if (fsr && fsr->is_user_function ()) |
3933
|
2547 { |
4009
|
2548 octave_value tmp = fsr->def (); |
3933
|
2549 const octave_value& rep = tmp.get_rep (); |
|
2550 |
|
2551 const octave_user_function& fcn |
|
2552 = static_cast<const octave_user_function&> (rep); |
|
2553 |
|
2554 fcn.print_symtab_info (octave_stdout); |
|
2555 } |
|
2556 else |
|
2557 error ("no user-defined function named %s", arg.c_str ()); |
|
2558 } |
3005
|
2559 } |
|
2560 else if (nargin == 0) |
3933
|
2561 curr_sym_tab->print_info (octave_stdout); |
3005
|
2562 else |
3933
|
2563 print_usage ("__print_symtab_info__"); |
3005
|
2564 |
|
2565 return retval; |
|
2566 } |
|
2567 |
3933
|
2568 DEFUN (__print_symbol_info__, args, , |
3446
|
2569 "-*- texinfo -*-\n\ |
|
2570 @deftypefn {Built-in Function} {} __dump_symbol_info__ (@var{name})\n\ |
3548
|
2571 Print symbol table information for the symbol @var{name}.\n\ |
3446
|
2572 @end deftypefn") |
3239
|
2573 { |
|
2574 octave_value_list retval; |
|
2575 |
|
2576 int nargin = args.length (); |
|
2577 |
|
2578 if (nargin == 1) |
|
2579 { |
3523
|
2580 std::string symbol_name = args(0).string_value (); |
3239
|
2581 |
|
2582 if (! error_state) |
|
2583 { |
|
2584 symbol_record *sr = curr_sym_tab->lookup (symbol_name); |
|
2585 |
|
2586 if (sr) |
3933
|
2587 sr->print_info (octave_stdout); |
3239
|
2588 else |
3933
|
2589 error ("__print_symbol_info__: symbol %s not found", |
3239
|
2590 symbol_name.c_str ()); |
|
2591 } |
|
2592 else |
3933
|
2593 print_usage ("__print_symbol_info__"); |
3239
|
2594 } |
|
2595 else |
3933
|
2596 print_usage ("__print_symbol_info__"); |
3239
|
2597 |
|
2598 return retval; |
|
2599 } |
|
2600 |
3016
|
2601 // XXX FIXME XXX -- some of these should do their own checking to be |
|
2602 // able to provide more meaningful warning or error messages. |
|
2603 |
|
2604 static int |
|
2605 ignore_function_time_stamp (void) |
|
2606 { |
|
2607 int pref = 0; |
|
2608 |
3523
|
2609 std::string val = builtin_string_variable ("ignore_function_time_stamp"); |
3016
|
2610 |
|
2611 if (! val.empty ()) |
|
2612 { |
3565
|
2613 if (val == "all") |
3016
|
2614 pref = 2; |
3565
|
2615 else if (val == "system") |
3016
|
2616 pref = 1; |
|
2617 } |
|
2618 |
|
2619 Vignore_function_time_stamp = pref; |
|
2620 |
|
2621 return 0; |
|
2622 } |
|
2623 |
|
2624 // XXX FIXME XXX -- there still may be better places for some of these |
|
2625 // to be defined. |
|
2626 |
|
2627 void |
|
2628 symbols_of_variables (void) |
|
2629 { |
3258
|
2630 DEFVAR (ans, , 0, |
3372
|
2631 "-*- texinfo -*-\n\ |
|
2632 @defvr {Built-in Variable} ans\n\ |
|
2633 This variable holds the most recently computed result that was not\n\ |
|
2634 explicitly assigned to a variable. For example, after the expression\n\ |
|
2635 \n\ |
|
2636 @example\n\ |
|
2637 3^2 + 4^2\n\ |
|
2638 @end example\n\ |
|
2639 \n\ |
|
2640 @noindent\n\ |
|
2641 is evaluated, the value of @code{ans} is 25.\n\ |
|
2642 @end defvr"); |
3016
|
2643 |
3258
|
2644 DEFVAR (ignore_function_time_stamp, "system", ignore_function_time_stamp, |
3371
|
2645 "-*- texinfo -*-\n\ |
|
2646 @defvr {Built-in Variable} ignore_function_time_stamp\n\ |
|
2647 This variable can be used to prevent Octave from making the system call\n\ |
|
2648 @code{stat} each time it looks up functions defined in function files.\n\ |
|
2649 If @code{ignore_function_time_stamp} to @code{\"system\"}, Octave will not\n\ |
|
2650 automatically recompile function files in subdirectories of\n\ |
|
2651 @file{@var{octave-home}/lib/@var{version}} if they have changed since\n\ |
|
2652 they were last compiled, but will recompile other function files in the\n\ |
|
2653 @code{LOADPATH} if they change. If set to @code{\"all\"}, Octave will not\n\ |
|
2654 recompile any function files unless their definitions are removed with\n\ |
|
2655 @code{clear}. For any other value of @code{ignore_function_time_stamp},\n\ |
|
2656 Octave will always check to see if functions defined in function files\n\ |
|
2657 need to recompiled. The default value of @code{ignore_function_time_stamp} is\n\ |
|
2658 @code{\"system\"}.\n\ |
|
2659 @end defvr"); |
3016
|
2660 } |
|
2661 |
1
|
2662 /* |
|
2663 ;;; Local Variables: *** |
|
2664 ;;; mode: C++ *** |
|
2665 ;;; End: *** |
|
2666 */ |