1
|
1 /* |
|
2 |
1884
|
3 Copyright (C) 1996 John W. Eaton |
1
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
240
|
23 #ifdef HAVE_CONFIG_H |
1192
|
24 #include <config.h> |
1
|
25 #endif |
|
26 |
1343
|
27 #include <cfloat> |
1468
|
28 #include <cstdio> |
1343
|
29 #include <cstring> |
605
|
30 |
1728
|
31 #include <string> |
|
32 |
2095
|
33 #include <iostream.h> |
1350
|
34 #include <strstream.h> |
|
35 |
|
36 #ifdef HAVE_UNISTD_H |
1
|
37 #include <sys/types.h> |
|
38 #include <unistd.h> |
|
39 #endif |
|
40 |
1465
|
41 #include <readline/readline.h> |
|
42 |
1792
|
43 #include "file-ops.h" |
|
44 #include "oct-glob.h" |
1755
|
45 #include "str-vec.h" |
|
46 |
605
|
47 #include "defaults.h" |
1352
|
48 #include "defun.h" |
|
49 #include "dirfns.h" |
704
|
50 #include "dynamic-ld.h" |
1352
|
51 #include "error.h" |
|
52 #include "help.h" |
|
53 #include "input.h" |
|
54 #include "lex.h" |
1742
|
55 #include "mappers.h" |
|
56 #include "oct-hist.h" |
1670
|
57 #include "toplev.h" |
605
|
58 #include "pager.h" |
1352
|
59 #include "parse.h" |
|
60 #include "symtab.h" |
|
61 #include "sysdep.h" |
1742
|
62 #include "pt-const.h" |
|
63 #include "oct-obj.h" |
|
64 #include "pt-exp.h" |
|
65 #include "pt-fvc.h" |
2076
|
66 #include "syscalls.h" |
1352
|
67 #include "unwind-prot.h" |
|
68 #include "user-prefs.h" |
1
|
69 #include "utils.h" |
1352
|
70 #include "variables.h" |
|
71 #include "version.h" |
529
|
72 |
1
|
73 // Symbol table for symbols at the top level. |
581
|
74 symbol_table *top_level_sym_tab = 0; |
1
|
75 |
|
76 // Symbol table for the current scope. |
581
|
77 symbol_table *curr_sym_tab = 0; |
1
|
78 |
|
79 // Symbol table for global symbols. |
581
|
80 symbol_table *global_sym_tab = 0; |
1
|
81 |
593
|
82 // Initialization. |
|
83 |
|
84 // Create the initial symbol tables and set the current scope at the |
|
85 // top level. |
|
86 |
195
|
87 void |
|
88 initialize_symbol_tables (void) |
|
89 { |
581
|
90 if (! global_sym_tab) |
|
91 global_sym_tab = new symbol_table (); |
195
|
92 |
581
|
93 if (! top_level_sym_tab) |
|
94 top_level_sym_tab = new symbol_table (); |
195
|
95 |
|
96 curr_sym_tab = top_level_sym_tab; |
|
97 } |
|
98 |
593
|
99 // Attributes of variables and functions. |
|
100 |
|
101 // Is this variable a builtin? |
|
102 |
1827
|
103 bool |
1755
|
104 is_builtin_variable (const string& name) |
593
|
105 { |
|
106 symbol_record *sr = global_sym_tab->lookup (name, 0, 0); |
|
107 return (sr && sr->is_builtin_variable ()); |
|
108 } |
|
109 |
|
110 // Is this a text-style function? |
|
111 |
1827
|
112 bool |
1755
|
113 is_text_function_name (const string& s) |
593
|
114 { |
|
115 symbol_record *sr = global_sym_tab->lookup (s); |
|
116 return (sr && sr->is_text_function ()); |
|
117 } |
|
118 |
|
119 // Is this function globally in this scope? |
|
120 |
1827
|
121 bool |
1755
|
122 is_globally_visible (const string& name) |
593
|
123 { |
|
124 symbol_record *sr = curr_sym_tab->lookup (name, 0, 0); |
|
125 return (sr && sr->is_linked_to_global ()); |
|
126 } |
|
127 |
2086
|
128 // Is this octave_value a valid function? |
593
|
129 |
|
130 tree_fvc * |
2086
|
131 is_valid_function (const octave_value& arg, const string& warn_for, int warn) |
593
|
132 { |
|
133 tree_fvc *ans = 0; |
|
134 |
1755
|
135 string fcn_name; |
1728
|
136 |
1517
|
137 if (arg.is_string ()) |
1755
|
138 fcn_name = arg.string_value (); |
1517
|
139 |
1755
|
140 if (fcn_name.empty () || error_state) |
593
|
141 { |
|
142 if (warn) |
1755
|
143 error ("%s: expecting function name as argument", |
|
144 warn_for.c_str ()); |
593
|
145 return ans; |
|
146 } |
|
147 |
|
148 symbol_record *sr = 0; |
1755
|
149 |
|
150 if (! fcn_name.empty ()) |
593
|
151 sr = lookup_by_name (fcn_name); |
|
152 |
|
153 if (sr) |
|
154 ans = sr->def (); |
|
155 |
|
156 if (! sr || ! ans || ! sr->is_function ()) |
|
157 { |
|
158 if (warn) |
|
159 error ("%s: the symbol `%s' is not valid as a function", |
1755
|
160 warn_for.c_str (), fcn_name.c_str ()); |
593
|
161 ans = 0; |
|
162 } |
|
163 |
|
164 return ans; |
|
165 } |
|
166 |
1957
|
167 DEFUN (is_global, args, , |
593
|
168 "is_global (X): return 1 if the string X names a global variable\n\ |
|
169 otherwise, return 0.") |
|
170 { |
2086
|
171 octave_value_list retval = 0.0; |
593
|
172 |
712
|
173 int nargin = args.length (); |
|
174 |
|
175 if (nargin != 1) |
593
|
176 { |
|
177 print_usage ("is_global"); |
|
178 return retval; |
|
179 } |
|
180 |
1755
|
181 string name = args(0).string_value (); |
593
|
182 |
636
|
183 if (error_state) |
|
184 { |
|
185 error ("is_global: expecting string argument"); |
|
186 return retval; |
|
187 } |
|
188 |
593
|
189 symbol_record *sr = curr_sym_tab->lookup (name, 0, 0); |
|
190 |
|
191 retval = (double) (sr && sr->is_linked_to_global ()); |
|
192 |
|
193 return retval; |
|
194 } |
|
195 |
1957
|
196 DEFUN (exist, args, , |
593
|
197 "exist (NAME): check if variable or file exists\n\ |
|
198 \n\ |
1564
|
199 returns:\n\ |
|
200 \n\ |
|
201 0 : NAME is undefined\n\ |
|
202 1 : NAME is a variable\n\ |
|
203 2 : NAME is a function\n\ |
|
204 3 : NAME is a .oct file in the current LOADPATH\n\ |
|
205 5 : NAME is a built-in function") |
593
|
206 { |
2086
|
207 octave_value_list retval; |
593
|
208 |
712
|
209 int nargin = args.length (); |
|
210 |
|
211 if (nargin != 1) |
593
|
212 { |
|
213 print_usage ("exist"); |
|
214 return retval; |
|
215 } |
|
216 |
1755
|
217 string name = args(0).string_value (); |
593
|
218 |
636
|
219 if (error_state) |
|
220 { |
|
221 error ("exist: expecting string argument"); |
|
222 return retval; |
|
223 } |
|
224 |
1755
|
225 string struct_elts; |
|
226 |
|
227 size_t pos = name.find ('.'); |
|
228 |
|
229 if (pos != NPOS) |
1277
|
230 { |
1755
|
231 struct_elts = name.substr (pos+1); |
|
232 name = name.substr (0, pos); |
1277
|
233 } |
|
234 |
593
|
235 symbol_record *sr = curr_sym_tab->lookup (name, 0, 0); |
|
236 if (! sr) |
|
237 sr = global_sym_tab->lookup (name, 0, 0); |
|
238 |
|
239 retval = 0.0; |
|
240 |
|
241 if (sr && sr->is_variable () && sr->is_defined ()) |
1277
|
242 { |
|
243 retval = 1.0; |
|
244 tree_fvc *def = sr->def (); |
1755
|
245 |
|
246 if (! struct_elts.empty ()) |
1277
|
247 { |
|
248 retval = 0.0; |
|
249 if (def->is_constant ()) |
|
250 { |
2086
|
251 octave_value *tmp = (octave_value *) def; |
1755
|
252 |
2086
|
253 octave_value ult = tmp->lookup_map_element (struct_elts, 0, 1); |
1755
|
254 |
1277
|
255 if (ult.is_defined ()) |
|
256 retval = 1.0; |
|
257 } |
|
258 } |
|
259 } |
1421
|
260 else if (sr && sr->is_builtin_function ()) |
|
261 { |
|
262 retval = 5.0; |
|
263 } |
|
264 else if (sr && sr->is_user_function ()) |
|
265 { |
|
266 retval = 2.0; |
|
267 } |
593
|
268 else |
|
269 { |
1755
|
270 string path = fcn_file_in_path (name); |
|
271 |
|
272 if (path.length () > 0) |
593
|
273 { |
|
274 retval = 2.0; |
|
275 } |
|
276 else |
|
277 { |
1421
|
278 path = oct_file_in_path (name); |
1755
|
279 |
|
280 if (path.length () > 0) |
1421
|
281 { |
|
282 retval = 3.0; |
|
283 } |
|
284 else |
|
285 { |
1766
|
286 file_stat fs (name); |
|
287 |
|
288 if (fs && fs.is_reg ()) |
1421
|
289 retval = 2.0; |
|
290 } |
593
|
291 } |
|
292 } |
|
293 |
|
294 return retval; |
|
295 } |
|
296 |
|
297 // XXX FIXME XXX -- should these really be here? |
|
298 |
1755
|
299 static string |
593
|
300 octave_home (void) |
|
301 { |
|
302 char *oh = getenv ("OCTAVE_HOME"); |
666
|
303 |
1755
|
304 return oh ? string (oh) : string (OCTAVE_PREFIX); |
666
|
305 } |
|
306 |
1755
|
307 static string |
|
308 subst_octave_home (const string& s) |
666
|
309 { |
1755
|
310 string retval; |
666
|
311 |
1755
|
312 string home = octave_home (); |
|
313 string prefix = OCTAVE_PREFIX; |
666
|
314 |
1755
|
315 retval = s; |
1418
|
316 |
1755
|
317 if (home != prefix) |
|
318 { |
|
319 int len = prefix.length (); |
|
320 size_t start = 0; |
|
321 while ((start = s.find (prefix)) != NPOS) |
|
322 { |
|
323 retval.replace (start, len, home); |
|
324 start++; |
666
|
325 } |
|
326 } |
|
327 |
|
328 return retval; |
593
|
329 } |
|
330 |
1755
|
331 static string |
593
|
332 octave_info_dir (void) |
|
333 { |
1755
|
334 return subst_octave_home (OCTAVE_INFODIR); |
666
|
335 } |
|
336 |
1755
|
337 string |
666
|
338 octave_arch_lib_dir (void) |
|
339 { |
1755
|
340 return subst_octave_home (OCTAVE_ARCHLIBDIR); |
593
|
341 } |
|
342 |
1755
|
343 string |
1719
|
344 octave_fcn_file_dir (void) |
|
345 { |
1755
|
346 return subst_octave_home (OCTAVE_FCNFILEDIR); |
1719
|
347 } |
|
348 |
1755
|
349 string |
798
|
350 octave_bin_dir (void) |
|
351 { |
1755
|
352 return subst_octave_home (OCTAVE_BINDIR); |
798
|
353 } |
|
354 |
1755
|
355 string |
|
356 maybe_add_default_load_path (const string& pathstring) |
|
357 { |
|
358 string std_path = subst_octave_home (OCTAVE_FCNFILEPATH); |
1121
|
359 |
1755
|
360 string retval; |
1121
|
361 |
1755
|
362 if (! pathstring.empty ()) |
1121
|
363 { |
1755
|
364 if (pathstring[0] == SEPCHAR) |
|
365 { |
|
366 retval = std_path; |
|
367 retval.append (pathstring); |
|
368 } |
|
369 else |
|
370 retval = pathstring; |
|
371 |
|
372 if (pathstring[pathstring.length () - 1] == SEPCHAR) |
|
373 retval.append (std_path); |
1121
|
374 } |
|
375 |
666
|
376 return retval; |
593
|
377 } |
|
378 |
1755
|
379 string |
|
380 octave_lib_dir (void) |
|
381 { |
|
382 return subst_octave_home (OCTAVE_LIBDIR); |
|
383 } |
|
384 |
|
385 string |
1613
|
386 default_exec_path (void) |
|
387 { |
1755
|
388 string exec_path_string; |
|
389 |
1613
|
390 char *octave_exec_path = getenv ("OCTAVE_EXEC_PATH"); |
1755
|
391 |
1613
|
392 if (octave_exec_path) |
1755
|
393 exec_path_string = string (octave_exec_path); |
1613
|
394 else |
|
395 { |
|
396 char *shell_path = getenv ("PATH"); |
1755
|
397 |
1613
|
398 if (shell_path) |
1755
|
399 { |
|
400 exec_path_string = string (":"); |
|
401 exec_path_string.append (shell_path); |
|
402 } |
1613
|
403 } |
1755
|
404 |
1613
|
405 return exec_path_string; |
|
406 } |
|
407 |
593
|
408 // Handle OCTAVE_PATH from the environment like TeX handles TEXINPUTS. |
|
409 // If the path starts with `:', prepend the standard path. If it ends |
|
410 // with `:' append the standard path. If it begins and ends with |
|
411 // `:', do both (which is useless, but the luser asked for it...). |
|
412 |
1755
|
413 string |
593
|
414 default_path (void) |
|
415 { |
1755
|
416 string std_path = subst_octave_home (OCTAVE_FCNFILEPATH); |
1121
|
417 |
1755
|
418 char *oct_path = getenv ("OCTAVE_PATH"); |
593
|
419 |
1755
|
420 return oct_path ? string (oct_path) : std_path; |
593
|
421 } |
|
422 |
1755
|
423 string |
593
|
424 default_info_file (void) |
|
425 { |
1755
|
426 string info_file_string; |
|
427 |
593
|
428 char *oct_info_file = getenv ("OCTAVE_INFO_FILE"); |
1755
|
429 |
593
|
430 if (oct_info_file) |
1755
|
431 info_file_string = string (oct_info_file); |
593
|
432 else |
|
433 { |
1755
|
434 string infodir = octave_info_dir (); |
|
435 info_file_string = infodir.append ("/octave.info"); |
593
|
436 } |
1755
|
437 |
593
|
438 return info_file_string; |
|
439 } |
|
440 |
1755
|
441 string |
1613
|
442 default_info_prog (void) |
|
443 { |
1755
|
444 string info_prog_string; |
|
445 |
1613
|
446 char *oct_info_prog = getenv ("OCTAVE_INFO_PROGRAM"); |
1755
|
447 |
1613
|
448 if (oct_info_prog) |
1755
|
449 info_prog_string = string (oct_info_prog); |
1613
|
450 else |
|
451 { |
1755
|
452 string archdir = octave_arch_lib_dir (); |
|
453 info_prog_string = archdir.append ("/info"); |
1613
|
454 } |
1755
|
455 |
1613
|
456 return info_prog_string; |
|
457 } |
|
458 |
1755
|
459 string |
593
|
460 default_editor (void) |
|
461 { |
1755
|
462 string editor_string = "vi"; |
|
463 |
593
|
464 char *env_editor = getenv ("EDITOR"); |
1755
|
465 |
593
|
466 if (env_editor && *env_editor) |
1755
|
467 editor_string = string (env_editor); |
|
468 |
593
|
469 return editor_string; |
|
470 } |
|
471 |
1755
|
472 string |
1476
|
473 get_local_site_defaults (void) |
|
474 { |
1755
|
475 string startupdir = subst_octave_home (OCTAVE_LOCALSTARTUPFILEDIR); |
|
476 return startupdir.append ("/octaverc"); |
1476
|
477 } |
|
478 |
1755
|
479 string |
593
|
480 get_site_defaults (void) |
|
481 { |
1755
|
482 string startupdir = subst_octave_home (OCTAVE_STARTUPFILEDIR); |
|
483 return startupdir.append ("/octaverc"); |
593
|
484 } |
|
485 |
|
486 // Functions for looking up variables and functions. |
|
487 |
581
|
488 // Is there a corresponding function file that is newer than the |
|
489 // symbol definition? |
|
490 |
593
|
491 static int |
1
|
492 symbol_out_of_date (symbol_record *sr) |
|
493 { |
195
|
494 int ignore = user_pref.ignore_function_time_stamp; |
|
495 |
|
496 if (ignore == 2) |
|
497 return 0; |
|
498 |
529
|
499 if (sr) |
1
|
500 { |
490
|
501 tree_fvc *ans = sr->def (); |
529
|
502 if (ans) |
1
|
503 { |
1755
|
504 string ff = ans->fcn_file_name (); |
|
505 if (! ff.empty () && ! (ignore && ans->is_system_fcn_file ())) |
1
|
506 { |
|
507 time_t tp = ans->time_parsed (); |
1755
|
508 |
|
509 string fname = fcn_file_in_path (ff); |
|
510 |
195
|
511 int status = is_newer (fname, tp); |
1755
|
512 |
195
|
513 if (status > 0) |
|
514 return 1; |
1
|
515 } |
|
516 } |
|
517 } |
195
|
518 return 0; |
1
|
519 } |
|
520 |
991
|
521 static int |
1755
|
522 looks_like_octave_copyright (const string& s) |
991
|
523 { |
1755
|
524 string t = s.substr (0, 15); |
|
525 |
|
526 if (t == " Copyright (C) ") |
991
|
527 { |
1755
|
528 size_t pos = s.find ('\n'); |
|
529 |
|
530 if (pos != NPOS) |
991
|
531 { |
1755
|
532 pos = s.find ('\n', pos + 1); |
|
533 |
|
534 if (pos != NPOS) |
991
|
535 { |
1755
|
536 pos++; |
|
537 |
|
538 t = s.substr (pos, 29); |
|
539 |
|
540 if (t == " This file is part of Octave." |
1983
|
541 || t == " This program is free softwar") |
991
|
542 return 1; |
|
543 } |
|
544 } |
|
545 } |
|
546 return 0; |
|
547 } |
|
548 |
1419
|
549 // Eat whitespace and comments from FFILE, returning the text of the |
|
550 // comments read if it doesn't look like a copyright notice. If |
|
551 // IN_PARTS, consider each block of comments separately; otherwise, |
|
552 // grab them all at once. |
1418
|
553 |
1755
|
554 static string |
1419
|
555 gobble_leading_white_space (FILE *ffile, int in_parts) |
581
|
556 { |
1755
|
557 string help_txt; |
991
|
558 |
|
559 int first_comments_seen = 0; |
|
560 int have_help_text = 0; |
581
|
561 int in_comment = 0; |
|
562 int c; |
1755
|
563 |
581
|
564 while ((c = getc (ffile)) != EOF) |
|
565 { |
984
|
566 current_input_column++; |
1755
|
567 |
581
|
568 if (in_comment) |
|
569 { |
991
|
570 if (! have_help_text) |
|
571 { |
|
572 first_comments_seen = 1; |
1755
|
573 help_txt += (char) c; |
991
|
574 } |
|
575 |
581
|
576 if (c == '\n') |
984
|
577 { |
|
578 input_line_number++; |
|
579 current_input_column = 0; |
|
580 in_comment = 0; |
1755
|
581 |
1419
|
582 if (in_parts) |
1418
|
583 { |
1419
|
584 if ((c = getc (ffile)) != EOF) |
|
585 { |
|
586 current_input_column--; |
|
587 ungetc (c, ffile); |
|
588 if (c == '\n') |
|
589 break; |
|
590 } |
|
591 else |
1418
|
592 break; |
|
593 } |
984
|
594 } |
581
|
595 } |
|
596 else |
|
597 { |
984
|
598 switch (c) |
581
|
599 { |
984
|
600 case ' ': |
|
601 case '\t': |
991
|
602 if (first_comments_seen) |
|
603 have_help_text = 1; |
984
|
604 break; |
|
605 |
|
606 case '\n': |
993
|
607 if (first_comments_seen) |
|
608 have_help_text = 1; |
984
|
609 input_line_number++; |
|
610 current_input_column = 0; |
|
611 continue; |
|
612 |
|
613 case '%': |
|
614 case '#': |
|
615 in_comment = 1; |
|
616 break; |
|
617 |
|
618 default: |
|
619 current_input_column--; |
581
|
620 ungetc (c, ffile); |
991
|
621 goto done; |
581
|
622 } |
|
623 } |
|
624 } |
991
|
625 |
|
626 done: |
|
627 |
1755
|
628 if (! help_txt.empty ()) |
991
|
629 { |
1419
|
630 if (looks_like_octave_copyright (help_txt)) |
1755
|
631 help_txt.resize (0); |
1419
|
632 |
1755
|
633 if (in_parts && help_txt.empty ()) |
1419
|
634 help_txt = gobble_leading_white_space (ffile, in_parts); |
1418
|
635 } |
|
636 |
991
|
637 return help_txt; |
581
|
638 } |
|
639 |
|
640 static int |
|
641 is_function_file (FILE *ffile) |
|
642 { |
|
643 int status = 0; |
|
644 |
|
645 long pos = ftell (ffile); |
|
646 |
|
647 char buf [10]; |
|
648 fgets (buf, 10, ffile); |
|
649 int len = strlen (buf); |
|
650 if (len > 8 && strncmp (buf, "function", 8) == 0 |
|
651 && ! (isalnum (buf[8]) || buf[8] == '_')) |
|
652 status = 1; |
|
653 |
|
654 fseek (ffile, pos, SEEK_SET); |
|
655 |
|
656 return status; |
|
657 } |
|
658 |
1907
|
659 static void |
|
660 restore_command_history (void *) |
|
661 { |
|
662 octave_command_history.ignore_entries (! user_pref.saving_history); |
|
663 } |
|
664 |
581
|
665 static int |
1755
|
666 parse_fcn_file (int exec_script, const string& ff) |
581
|
667 { |
|
668 begin_unwind_frame ("parse_fcn_file"); |
|
669 |
|
670 int script_file_executed = 0; |
|
671 |
1358
|
672 // Open function file and parse. |
581
|
673 |
|
674 int old_reading_fcn_file_state = reading_fcn_file; |
|
675 |
|
676 unwind_protect_ptr (rl_instream); |
|
677 unwind_protect_ptr (ff_instream); |
|
678 |
|
679 unwind_protect_int (using_readline); |
|
680 unwind_protect_int (input_line_number); |
|
681 unwind_protect_int (current_input_column); |
|
682 unwind_protect_int (reading_fcn_file); |
|
683 |
|
684 using_readline = 0; |
|
685 reading_fcn_file = 1; |
|
686 input_line_number = 0; |
|
687 current_input_column = 1; |
|
688 |
|
689 FILE *ffile = get_input_from_file (ff, 0); |
|
690 |
|
691 if (ffile) |
|
692 { |
1271
|
693 // Check to see if this file defines a function or is just a |
|
694 // list of commands. |
581
|
695 |
1755
|
696 string tmp_help_txt = gobble_leading_white_space (ffile, 0); |
991
|
697 |
581
|
698 if (is_function_file (ffile)) |
|
699 { |
1907
|
700 // XXX FIXME XXX -- we shouldn't need both the |
|
701 // octave_command_history object and the |
|
702 // user_pref.saving_history variable... |
|
703 octave_command_history.ignore_entries (); |
|
704 |
|
705 add_unwind_protect (restore_command_history, 0); |
|
706 |
1588
|
707 unwind_protect_int (user_pref.echo_executing_commands); |
1643
|
708 unwind_protect_int (user_pref.saving_history); |
581
|
709 unwind_protect_int (reading_fcn_file); |
1516
|
710 unwind_protect_int (input_from_command_line_file); |
581
|
711 |
1588
|
712 user_pref.echo_executing_commands = ECHO_OFF; |
1643
|
713 user_pref.saving_history = 0; |
581
|
714 reading_fcn_file = 1; |
1516
|
715 input_from_command_line_file = 0; |
581
|
716 |
|
717 YY_BUFFER_STATE old_buf = current_buffer (); |
|
718 YY_BUFFER_STATE new_buf = create_buffer (ffile); |
|
719 |
|
720 add_unwind_protect (restore_input_buffer, (void *) old_buf); |
|
721 add_unwind_protect (delete_input_buffer, (void *) new_buf); |
|
722 |
|
723 switch_to_buffer (new_buf); |
|
724 |
|
725 unwind_protect_ptr (curr_sym_tab); |
|
726 |
|
727 reset_parser (); |
|
728 |
991
|
729 help_buf = tmp_help_txt; |
|
730 |
581
|
731 int status = yyparse (); |
|
732 |
|
733 if (status != 0) |
|
734 { |
1755
|
735 error ("parse error while reading function file %s", |
|
736 ff.c_str ()); |
581
|
737 global_sym_tab->clear (curr_fcn_file_name); |
|
738 } |
|
739 } |
|
740 else if (exec_script) |
|
741 { |
1271
|
742 // The value of `reading_fcn_file' will be restored to the |
|
743 // proper value when we unwind from this frame. |
581
|
744 reading_fcn_file = old_reading_fcn_file_state; |
|
745 |
1952
|
746 // XXX FIXME XXX -- we shouldn't need both the |
|
747 // octave_command_history object and the |
|
748 // user_pref.saving_history variable... |
|
749 octave_command_history.ignore_entries (); |
|
750 |
|
751 add_unwind_protect (restore_command_history, 0); |
|
752 |
|
753 unwind_protect_int (user_pref.saving_history); |
581
|
754 unwind_protect_int (reading_script_file); |
1952
|
755 |
|
756 user_pref.saving_history = 0; |
581
|
757 reading_script_file = 1; |
|
758 |
|
759 parse_and_execute (ffile, 1); |
|
760 |
|
761 script_file_executed = 1; |
|
762 } |
|
763 fclose (ffile); |
|
764 } |
|
765 |
|
766 run_unwind_frame ("parse_fcn_file"); |
|
767 |
|
768 return script_file_executed; |
|
769 } |
|
770 |
1827
|
771 static bool |
581
|
772 load_fcn_from_file (symbol_record *sym_rec, int exec_script) |
|
773 { |
1827
|
774 bool script_file_executed = false; |
581
|
775 |
1755
|
776 string nm = sym_rec->name (); |
581
|
777 |
704
|
778 if (load_octave_oct_file (nm)) |
|
779 { |
|
780 force_link_to_function (nm); |
|
781 } |
|
782 else |
581
|
783 { |
1755
|
784 string ff = fcn_file_in_path (nm); |
581
|
785 |
1606
|
786 // These are needed by yyparse. |
|
787 |
1755
|
788 begin_unwind_frame ("load_fcn_from_file"); |
|
789 |
|
790 unwind_protect_str (curr_fcn_file_name); |
|
791 unwind_protect_str (curr_fcn_file_full_name); |
|
792 |
1606
|
793 curr_fcn_file_name = nm; |
|
794 curr_fcn_file_full_name = ff; |
|
795 |
1755
|
796 if (ff.length () > 0) |
|
797 script_file_executed = parse_fcn_file (exec_script, ff); |
581
|
798 |
|
799 if (! (error_state || script_file_executed)) |
|
800 force_link_to_function (nm); |
1755
|
801 |
|
802 run_unwind_frame ("load_fcn_from_file"); |
581
|
803 } |
|
804 |
|
805 return script_file_executed; |
|
806 } |
|
807 |
1827
|
808 bool |
581
|
809 lookup (symbol_record *sym_rec, int exec_script) |
|
810 { |
1827
|
811 bool script_executed = false; |
581
|
812 |
|
813 if (! sym_rec->is_linked_to_global ()) |
|
814 { |
|
815 if (sym_rec->is_defined ()) |
|
816 { |
|
817 if (sym_rec->is_function () && symbol_out_of_date (sym_rec)) |
1271
|
818 script_executed = load_fcn_from_file (sym_rec, exec_script); |
581
|
819 } |
|
820 else if (! sym_rec->is_formal_parameter ()) |
|
821 { |
|
822 link_to_builtin_or_function (sym_rec); |
1271
|
823 |
581
|
824 if (! sym_rec->is_defined ()) |
1271
|
825 script_executed = load_fcn_from_file (sym_rec, exec_script); |
581
|
826 else if (sym_rec->is_function () && symbol_out_of_date (sym_rec)) |
1271
|
827 script_executed = load_fcn_from_file (sym_rec, exec_script); |
581
|
828 } |
|
829 } |
|
830 |
1271
|
831 return script_executed; |
581
|
832 } |
|
833 |
|
834 // Get the symbol record for the given name that is visible in the |
|
835 // current scope. Reread any function definitions that appear to be |
|
836 // out of date. If a function is available in a file but is not |
|
837 // currently loaded, this will load it and insert the name in the |
|
838 // current symbol table. |
|
839 |
|
840 symbol_record * |
1755
|
841 lookup_by_name (const string& nm, int exec_script) |
581
|
842 { |
|
843 symbol_record *sym_rec = curr_sym_tab->lookup (nm, 1, 0); |
|
844 |
|
845 lookup (sym_rec, exec_script); |
|
846 |
|
847 return sym_rec; |
|
848 } |
|
849 |
1755
|
850 string |
|
851 get_help_from_file (const string& path) |
991
|
852 { |
1755
|
853 string retval; |
|
854 |
|
855 if (! path.empty ()) |
991
|
856 { |
1755
|
857 FILE *fptr = fopen (path.c_str (), "r"); |
|
858 |
991
|
859 if (fptr) |
|
860 { |
1755
|
861 retval = gobble_leading_white_space (fptr, 1); |
991
|
862 fclose (fptr); |
|
863 } |
|
864 } |
1755
|
865 |
|
866 return retval; |
991
|
867 } |
|
868 |
593
|
869 // Variable values. |
195
|
870 |
581
|
871 // Look for the given name in the global symbol table. If it refers |
|
872 // to a string, return a new copy. If not, return 0; |
|
873 |
1755
|
874 string |
|
875 builtin_string_variable (const string& name) |
1
|
876 { |
195
|
877 symbol_record *sr = global_sym_tab->lookup (name, 0, 0); |
|
878 |
1271
|
879 // It is a prorgramming error to look for builtins that aren't. |
195
|
880 |
529
|
881 assert (sr); |
195
|
882 |
1755
|
883 string retval; |
1
|
884 |
490
|
885 tree_fvc *defn = sr->def (); |
195
|
886 |
529
|
887 if (defn) |
1
|
888 { |
2086
|
889 octave_value val = defn->eval (0); |
195
|
890 |
610
|
891 if (! error_state && val.is_string ()) |
1755
|
892 retval = val.string_value (); |
1
|
893 } |
|
894 |
|
895 return retval; |
|
896 } |
|
897 |
581
|
898 // Look for the given name in the global symbol table. If it refers |
1504
|
899 // to a real scalar, place the value in d and return 1. Otherwise, |
|
900 // return 0. |
581
|
901 |
1
|
902 int |
1755
|
903 builtin_real_scalar_variable (const string& name, double& d) |
1
|
904 { |
1504
|
905 int status = 0; |
195
|
906 symbol_record *sr = global_sym_tab->lookup (name, 0, 0); |
|
907 |
1271
|
908 // It is a prorgramming error to look for builtins that aren't. |
195
|
909 |
529
|
910 assert (sr); |
1
|
911 |
490
|
912 tree_fvc *defn = sr->def (); |
195
|
913 |
529
|
914 if (defn) |
1
|
915 { |
2086
|
916 octave_value val = defn->eval (0); |
195
|
917 |
598
|
918 if (! error_state && val.is_scalar_type ()) |
1
|
919 { |
|
920 d = val.double_value (); |
1504
|
921 status = 1; |
1
|
922 } |
|
923 } |
|
924 |
|
925 return status; |
|
926 } |
|
927 |
1093
|
928 // Look for the given name in the global symbol table. |
|
929 |
2086
|
930 octave_value |
1755
|
931 builtin_any_variable (const string& name) |
1093
|
932 { |
2086
|
933 octave_value retval; |
1093
|
934 |
|
935 symbol_record *sr = global_sym_tab->lookup (name, 0, 0); |
|
936 |
1271
|
937 // It is a prorgramming error to look for builtins that aren't. |
1093
|
938 |
|
939 assert (sr); |
|
940 |
|
941 tree_fvc *defn = sr->def (); |
|
942 |
|
943 if (defn) |
|
944 retval = defn->eval (0); |
|
945 |
|
946 return retval; |
|
947 } |
|
948 |
593
|
949 // Global stuff and links to builtin variables and functions. |
|
950 |
581
|
951 // Make the definition of the symbol record sr be the same as the |
|
952 // definition of the global variable of the same name, creating it if |
1418
|
953 // it doesn't already exist. |
581
|
954 |
195
|
955 void |
|
956 link_to_global_variable (symbol_record *sr) |
|
957 { |
|
958 if (sr->is_linked_to_global ()) |
|
959 return; |
|
960 |
1755
|
961 string nm = sr->name (); |
|
962 |
|
963 symbol_record *gsr = global_sym_tab->lookup (nm, 1, 0); |
195
|
964 |
|
965 if (sr->is_formal_parameter ()) |
|
966 { |
1755
|
967 error ("can't make function parameter `%s' global", nm.c_str ()); |
195
|
968 return; |
|
969 } |
|
970 |
1271
|
971 // There must be a better way to do this. XXX FIXME XXX |
195
|
972 |
|
973 if (sr->is_variable ()) |
|
974 { |
1271
|
975 // Would be nice not to have this cast. XXX FIXME XXX |
|
976 |
2086
|
977 octave_value *tmp = (octave_value *) sr->def (); |
529
|
978 if (tmp) |
2086
|
979 tmp = new octave_value (*tmp); |
529
|
980 else |
2086
|
981 tmp = new octave_value (); |
195
|
982 gsr->define (tmp); |
|
983 } |
|
984 else |
529
|
985 sr->clear (); |
195
|
986 |
1271
|
987 // If the global symbol is currently defined as a function, we need |
|
988 // to hide it with a variable. |
195
|
989 |
|
990 if (gsr->is_function ()) |
2086
|
991 gsr->define ((octave_value *) 0); |
195
|
992 |
|
993 sr->alias (gsr, 1); |
|
994 sr->mark_as_linked_to_global (); |
|
995 } |
|
996 |
581
|
997 // Make the definition of the symbol record sr be the same as the |
|
998 // definition of the builtin variable of the same name. |
|
999 |
195
|
1000 void |
|
1001 link_to_builtin_variable (symbol_record *sr) |
|
1002 { |
|
1003 symbol_record *tmp_sym = global_sym_tab->lookup (sr->name (), 0, 0); |
|
1004 |
529
|
1005 if (tmp_sym && tmp_sym->is_builtin_variable ()) |
|
1006 sr->alias (tmp_sym); |
195
|
1007 } |
|
1008 |
581
|
1009 // Make the definition of the symbol record sr be the same as the |
|
1010 // definition of the builtin variable or function, or user function of |
|
1011 // the same name, provided that the name has not been used as a formal |
|
1012 // parameter. |
|
1013 |
195
|
1014 void |
|
1015 link_to_builtin_or_function (symbol_record *sr) |
|
1016 { |
|
1017 symbol_record *tmp_sym = global_sym_tab->lookup (sr->name (), 0, 0); |
|
1018 |
529
|
1019 if (tmp_sym |
|
1020 && (tmp_sym->is_builtin_variable () || tmp_sym->is_function ()) |
|
1021 && ! tmp_sym->is_formal_parameter ()) |
|
1022 sr->alias (tmp_sym); |
195
|
1023 } |
|
1024 |
581
|
1025 // Force a link to a function in the current symbol table. This is |
|
1026 // used just after defining a function to avoid different behavior |
|
1027 // depending on whether or not the function has been evaluated after |
|
1028 // being defined. |
|
1029 // |
|
1030 // Return without doing anything if there isn't a function with the |
|
1031 // given name defined in the global symbol table. |
|
1032 |
195
|
1033 void |
1755
|
1034 force_link_to_function (const string& id_name) |
195
|
1035 { |
|
1036 symbol_record *gsr = global_sym_tab->lookup (id_name, 1, 0); |
|
1037 if (gsr->is_function ()) |
|
1038 { |
|
1039 curr_sym_tab->clear (id_name); |
|
1040 symbol_record *csr = curr_sym_tab->lookup (id_name, 1, 0); |
|
1041 csr->alias (gsr); |
|
1042 } |
|
1043 } |
|
1044 |
605
|
1045 // Help stuff. Shouldn't this go in help.cc? |
593
|
1046 |
|
1047 // It's not likely that this does the right thing now. XXX FIXME XXX |
|
1048 |
1755
|
1049 string_vector |
593
|
1050 make_name_list (void) |
|
1051 { |
|
1052 int key_len = 0; |
|
1053 int glb_len = 0; |
|
1054 int top_len = 0; |
|
1055 int lcl_len = 0; |
|
1056 |
1755
|
1057 string_vector key; |
|
1058 string_vector glb; |
|
1059 string_vector top; |
|
1060 string_vector lcl; |
|
1061 string_vector ffl; |
593
|
1062 |
1271
|
1063 // Each of these functions returns a new vector of pointers to new |
|
1064 // strings. |
593
|
1065 |
|
1066 key = names (keyword_help (), key_len); |
1795
|
1067 |
593
|
1068 glb = global_sym_tab->list (glb_len); |
1795
|
1069 |
593
|
1070 top = top_level_sym_tab->list (top_len); |
1795
|
1071 |
593
|
1072 if (top_level_sym_tab != curr_sym_tab) |
|
1073 lcl = curr_sym_tab->list (lcl_len); |
1795
|
1074 |
1796
|
1075 ffl = get_fcn_file_names (1); |
1795
|
1076 int ffl_len = ffl.length (); |
593
|
1077 |
|
1078 int total_len = key_len + glb_len + top_len + lcl_len + ffl_len; |
|
1079 |
1755
|
1080 string_vector list (total_len); |
1418
|
1081 |
1271
|
1082 // Put all the symbols in one big list. Only copy pointers, not the |
|
1083 // strings they point to, then only delete the original array of |
|
1084 // pointers, and not the strings they point to. |
593
|
1085 |
|
1086 int j = 0; |
|
1087 int i = 0; |
|
1088 for (i = 0; i < key_len; i++) |
|
1089 list[j++] = key[i]; |
|
1090 |
|
1091 for (i = 0; i < glb_len; i++) |
|
1092 list[j++] = glb[i]; |
|
1093 |
|
1094 for (i = 0; i < top_len; i++) |
|
1095 list[j++] = top[i]; |
|
1096 |
|
1097 for (i = 0; i < lcl_len; i++) |
|
1098 list[j++] = lcl[i]; |
|
1099 |
|
1100 for (i = 0; i < ffl_len; i++) |
|
1101 list[j++] = ffl[i]; |
|
1102 |
|
1103 return list; |
|
1104 } |
|
1105 |
|
1106 // List variable names. |
|
1107 |
|
1108 static void |
2095
|
1109 print_symbol_info_line (ostream& os, const symbol_record_info& s) |
593
|
1110 { |
2095
|
1111 os << (s.is_read_only () ? " -" : " w"); |
|
1112 os << (s.is_eternal () ? "- " : "d "); |
593
|
1113 #if 0 |
2095
|
1114 os << (s.hides_fcn () ? "f" : (s.hides_builtin () ? "F" : "-")); |
593
|
1115 #endif |
2095
|
1116 os.form (" %-16s", s.type_as_string ().c_str ()); |
593
|
1117 if (s.is_function ()) |
2095
|
1118 os << " - -"; |
593
|
1119 else |
|
1120 { |
2095
|
1121 os.form ("%7d", s.rows ()); |
|
1122 os.form ("%7d", s.columns ()); |
593
|
1123 } |
2095
|
1124 os << " " << s.name () << "\n"; |
593
|
1125 } |
|
1126 |
|
1127 static void |
2095
|
1128 print_long_listing (ostream& os, symbol_record_info *s) |
593
|
1129 { |
|
1130 if (! s) |
|
1131 return; |
|
1132 |
|
1133 symbol_record_info *ptr = s; |
|
1134 while (ptr->is_defined ()) |
|
1135 { |
2095
|
1136 print_symbol_info_line (os, *ptr); |
593
|
1137 ptr++; |
|
1138 } |
|
1139 } |
|
1140 |
|
1141 static int |
1755
|
1142 maybe_list (const char *header, const string_vector& argv, int argc, |
2095
|
1143 ostream& os, int show_verbose, symbol_table |
867
|
1144 *sym_tab, unsigned type, unsigned scope) |
593
|
1145 { |
|
1146 int count; |
|
1147 int status = 0; |
|
1148 if (show_verbose) |
|
1149 { |
|
1150 symbol_record_info *symbols; |
867
|
1151 symbols = sym_tab->long_list (count, argv, argc, 1, type, scope); |
593
|
1152 if (symbols && count > 0) |
|
1153 { |
2095
|
1154 os << "\n" << header << "\n\n" |
593
|
1155 << "prot type rows cols name\n" |
|
1156 << "==== ==== ==== ==== ====\n"; |
|
1157 |
2095
|
1158 print_long_listing (os, symbols); |
593
|
1159 status = 1; |
|
1160 } |
|
1161 delete [] symbols; |
|
1162 } |
|
1163 else |
|
1164 { |
1755
|
1165 string_vector symbols = sym_tab->list (count, argv, argc, 1, |
|
1166 type, scope); |
|
1167 if (symbols.length () > 0 && count > 0) |
593
|
1168 { |
2095
|
1169 os << "\n" << header << "\n\n"; |
|
1170 symbols.list_in_columns (os); |
593
|
1171 status = 1; |
|
1172 } |
|
1173 } |
|
1174 return status; |
|
1175 } |
|
1176 |
1957
|
1177 DEFUN_TEXT (document, args, , |
593
|
1178 "document symbol string ...\n\ |
|
1179 \n\ |
|
1180 Associate a cryptic message with a variable name.") |
|
1181 { |
2086
|
1182 octave_value_list retval; |
593
|
1183 |
1755
|
1184 int argc = args.length () + 1; |
|
1185 |
1968
|
1186 string_vector argv = args.make_argv ("document"); |
1755
|
1187 |
|
1188 if (error_state) |
|
1189 return retval; |
593
|
1190 |
|
1191 if (argc == 3) |
|
1192 { |
1755
|
1193 string name = argv[1]; |
|
1194 string help = argv[2]; |
593
|
1195 |
|
1196 if (is_builtin_variable (name)) |
|
1197 error ("sorry, can't redefine help for builtin variables"); |
|
1198 else |
|
1199 { |
|
1200 symbol_record *sym_rec = curr_sym_tab->lookup (name, 0); |
|
1201 |
|
1202 if (sym_rec) |
|
1203 sym_rec->document (help); |
|
1204 else |
1755
|
1205 error ("document: no such symbol `%s'", name.c_str ()); |
593
|
1206 } |
|
1207 } |
|
1208 else |
|
1209 print_usage ("document"); |
|
1210 |
|
1211 return retval; |
|
1212 } |
|
1213 |
584
|
1214 // XXX FIXME XXX -- this should take a list of regular expressions |
|
1215 // naming the variables to look for. |
|
1216 |
2086
|
1217 static octave_value_list |
1755
|
1218 do_who (int argc, const string_vector& argv) |
529
|
1219 { |
2086
|
1220 octave_value_list retval; |
529
|
1221 |
|
1222 int show_builtins = 0; |
|
1223 int show_functions = (curr_sym_tab == top_level_sym_tab); |
|
1224 int show_variables = 1; |
|
1225 int show_verbose = 0; |
|
1226 |
1755
|
1227 string my_name = argv[0]; |
584
|
1228 |
529
|
1229 if (argc > 1) |
|
1230 { |
|
1231 show_functions = 0; |
|
1232 show_variables = 0; |
|
1233 } |
|
1234 |
1857
|
1235 int i; |
|
1236 for (i = 1; i < argc; i++) |
529
|
1237 { |
1755
|
1238 if (argv[i] == "-all" || argv[i] == "-a") |
529
|
1239 { |
|
1240 show_builtins++; |
|
1241 show_functions++; |
1418
|
1242 show_variables++; |
529
|
1243 } |
1755
|
1244 else if (argv[i] == "-builtins" || argv[i] == "-b") |
529
|
1245 show_builtins++; |
1755
|
1246 else if (argv[i] == "-functions" || argv[i] == "-f") |
529
|
1247 show_functions++; |
1755
|
1248 else if (argv[i] == "-long" || argv[i] == "-l") |
605
|
1249 show_verbose++; |
1755
|
1250 else if (argv[i] == "-variables" || argv[i] == "-v") |
529
|
1251 show_variables++; |
1755
|
1252 else if (argv[i][0] == '-') |
|
1253 warning ("%s: unrecognized option `%s'", my_name.c_str (), |
|
1254 argv[i].c_str ()); |
529
|
1255 else |
867
|
1256 break; |
529
|
1257 } |
|
1258 |
1857
|
1259 int npats = argc - i; |
|
1260 string_vector pats (npats); |
|
1261 for (int j = 0; j < npats; j++) |
|
1262 pats[j] = argv[i+j]; |
|
1263 |
1271
|
1264 // If the user specified -l and nothing else, show variables. If |
|
1265 // evaluating this at the top level, also show functions. |
529
|
1266 |
|
1267 if (show_verbose && ! (show_builtins || show_functions || show_variables)) |
|
1268 { |
|
1269 show_functions = (curr_sym_tab == top_level_sym_tab); |
|
1270 show_variables = 1; |
|
1271 } |
|
1272 |
|
1273 int pad_after = 0; |
|
1274 |
|
1275 if (show_builtins) |
|
1276 { |
1857
|
1277 pad_after += maybe_list ("*** built-in variables:", pats, npats, |
2095
|
1278 octave_stdout, show_verbose, global_sym_tab, |
529
|
1279 symbol_def::BUILTIN_VARIABLE, |
|
1280 SYMTAB_ALL_SCOPES); |
|
1281 |
1857
|
1282 pad_after += maybe_list ("*** built-in functions:", pats, npats, |
2095
|
1283 octave_stdout, show_verbose, global_sym_tab, |
529
|
1284 symbol_def::BUILTIN_FUNCTION, |
|
1285 SYMTAB_ALL_SCOPES); |
|
1286 } |
|
1287 |
|
1288 if (show_functions) |
|
1289 { |
|
1290 pad_after += maybe_list ("*** currently compiled functions:", |
2095
|
1291 pats, npats, octave_stdout, show_verbose, |
867
|
1292 global_sym_tab, symbol_def::USER_FUNCTION, |
529
|
1293 SYMTAB_ALL_SCOPES); |
|
1294 } |
|
1295 |
|
1296 if (show_variables) |
|
1297 { |
1857
|
1298 pad_after += maybe_list ("*** local user variables:", pats, npats, |
2095
|
1299 octave_stdout, show_verbose, curr_sym_tab, |
529
|
1300 symbol_def::USER_VARIABLE, |
1418
|
1301 SYMTAB_LOCAL_SCOPE); |
529
|
1302 |
|
1303 pad_after += maybe_list ("*** globally visible user variables:", |
2095
|
1304 pats, npats, octave_stdout, show_verbose, |
867
|
1305 curr_sym_tab, symbol_def::USER_VARIABLE, |
529
|
1306 SYMTAB_GLOBAL_SCOPE); |
|
1307 } |
|
1308 |
|
1309 if (pad_after) |
2095
|
1310 octave_stdout << "\n"; |
529
|
1311 |
581
|
1312 return retval; |
|
1313 } |
|
1314 |
1957
|
1315 DEFUN_TEXT (who, args, , |
581
|
1316 "who [-all] [-builtins] [-functions] [-long] [-variables]\n\ |
|
1317 \n\ |
|
1318 List currently defined symbol(s). Options may be shortened to one\n\ |
|
1319 character, but may not be combined.") |
|
1320 { |
2086
|
1321 octave_value_list retval; |
581
|
1322 |
1755
|
1323 int argc = args.length () + 1; |
|
1324 |
1968
|
1325 string_vector argv = args.make_argv ("who"); |
1755
|
1326 |
|
1327 if (error_state) |
|
1328 return retval; |
581
|
1329 |
1488
|
1330 retval = do_who (argc, argv); |
581
|
1331 |
529
|
1332 return retval; |
|
1333 } |
|
1334 |
1957
|
1335 DEFUN_TEXT (whos, args, , |
581
|
1336 "whos [-all] [-builtins] [-functions] [-long] [-variables]\n\ |
|
1337 \n\ |
|
1338 List currently defined symbol(s). Options may be shortened to one\n\ |
|
1339 character, but may not be combined.") |
|
1340 { |
2086
|
1341 octave_value_list retval; |
581
|
1342 |
712
|
1343 int nargin = args.length (); |
|
1344 |
2086
|
1345 octave_value_list tmp_args; |
742
|
1346 for (int i = nargin; i > 0; i--) |
|
1347 tmp_args(i) = args(i-1); |
|
1348 tmp_args(0) = "-long"; |
581
|
1349 |
742
|
1350 int argc = tmp_args.length () + 1; |
1755
|
1351 |
1980
|
1352 string_vector argv = tmp_args.make_argv ("whos"); |
581
|
1353 |
|
1354 if (error_state) |
|
1355 return retval; |
|
1356 |
1488
|
1357 retval = do_who (argc, argv); |
581
|
1358 |
|
1359 return retval; |
|
1360 } |
|
1361 |
593
|
1362 // Install variables and functions in the symbol tables. |
529
|
1363 |
593
|
1364 void |
1755
|
1365 install_builtin_mapper (const builtin_mapper_function& mf) |
529
|
1366 { |
1755
|
1367 symbol_record *sym_rec = global_sym_tab->lookup (mf.name, 1); |
593
|
1368 sym_rec->unprotect (); |
|
1369 |
2089
|
1370 tree_builtin *def = new tree_builtin (mf, mf.name); |
593
|
1371 |
|
1372 sym_rec->define (def); |
|
1373 |
1755
|
1374 sym_rec->document (mf.help_string); |
593
|
1375 sym_rec->make_eternal (); |
|
1376 sym_rec->protect (); |
|
1377 } |
|
1378 |
|
1379 void |
1755
|
1380 install_builtin_function (const builtin_function& f) |
593
|
1381 { |
1755
|
1382 symbol_record *sym_rec = global_sym_tab->lookup (f.name, 1); |
593
|
1383 sym_rec->unprotect (); |
|
1384 |
1755
|
1385 tree_builtin *def = new tree_builtin (f.fcn, f.name); |
593
|
1386 |
1755
|
1387 sym_rec->define (def, f.is_text_fcn); |
593
|
1388 |
1755
|
1389 sym_rec->document (f.help_string); |
593
|
1390 sym_rec->make_eternal (); |
|
1391 sym_rec->protect (); |
|
1392 } |
|
1393 |
|
1394 void |
1755
|
1395 install_builtin_variable (const builtin_variable& v) |
593
|
1396 { |
1755
|
1397 if (v.install_as_function) |
|
1398 install_builtin_variable_as_function (v.name, v.value, v.protect, |
|
1399 v.eternal, v.help_string); |
529
|
1400 else |
1755
|
1401 bind_builtin_variable (v.name, v.value, v.protect, v.eternal, |
|
1402 v.sv_function, v.help_string); |
529
|
1403 } |
|
1404 |
593
|
1405 void |
2086
|
1406 install_builtin_variable_as_function (const string& name, octave_value *val, |
593
|
1407 int protect, int eternal, |
1755
|
1408 const string& help) |
529
|
1409 { |
593
|
1410 symbol_record *sym_rec = global_sym_tab->lookup (name, 1); |
|
1411 sym_rec->unprotect (); |
|
1412 |
1755
|
1413 string tmp_help = help.empty () ? sym_rec->help () : help; |
593
|
1414 |
|
1415 sym_rec->define_as_fcn (val); |
|
1416 |
|
1417 sym_rec->document (tmp_help); |
|
1418 |
|
1419 if (protect) |
|
1420 sym_rec->protect (); |
|
1421 |
|
1422 if (eternal) |
|
1423 sym_rec->make_eternal (); |
|
1424 } |
|
1425 |
|
1426 void |
1755
|
1427 alias_builtin (const string& alias, const string& name) |
593
|
1428 { |
|
1429 symbol_record *sr_name = global_sym_tab->lookup (name, 0, 0); |
1755
|
1430 |
593
|
1431 if (! sr_name) |
|
1432 panic ("can't alias to undefined name!"); |
|
1433 |
|
1434 symbol_record *sr_alias = global_sym_tab->lookup (alias, 1, 0); |
|
1435 |
|
1436 if (sr_alias) |
|
1437 sr_alias->alias (sr_name); |
|
1438 else |
1755
|
1439 panic ("can't find symbol record for builtin function `%s'", |
|
1440 alias.c_str ()); |
529
|
1441 } |
|
1442 |
593
|
1443 // Defining variables. |
|
1444 |
1161
|
1445 #if 0 |
593
|
1446 void |
|
1447 bind_nargin_and_nargout (symbol_table *sym_tab, int nargin, int nargout) |
529
|
1448 { |
2086
|
1449 octave_value *tmp; |
593
|
1450 symbol_record *sr; |
|
1451 |
|
1452 sr = sym_tab->lookup ("nargin", 1, 0); |
|
1453 sr->unprotect (); |
2086
|
1454 tmp = new octave_value (nargin); |
593
|
1455 sr->define (tmp); |
|
1456 |
|
1457 sr = sym_tab->lookup ("nargout", 1, 0); |
|
1458 sr->unprotect (); |
2086
|
1459 tmp = new octave_value (nargout); |
593
|
1460 sr->define (tmp); |
|
1461 } |
1161
|
1462 #endif |
593
|
1463 |
1162
|
1464 void |
2086
|
1465 bind_ans (const octave_value& val, int print) |
1162
|
1466 { |
|
1467 static symbol_record *sr = global_sym_tab->lookup ("ans", 1, 0); |
|
1468 |
1281
|
1469 tree_identifier *ans_id = new tree_identifier (sr); |
2086
|
1470 octave_value *tmp = new octave_value (val); |
1162
|
1471 |
1281
|
1472 // XXX FIXME XXX -- making ans_id static, passing its address to |
|
1473 // tree_simple_assignment_expression along with a flag to not delete |
|
1474 // it seems to create a memory leak. Hmm. |
|
1475 |
1827
|
1476 tree_simple_assignment_expression tmp_ass (ans_id, tmp, false, true); |
1162
|
1477 |
|
1478 tmp_ass.eval (print); |
|
1479 } |
|
1480 |
1489
|
1481 void |
|
1482 bind_global_error_variable (void) |
|
1483 { |
|
1484 *error_message_buffer << ends; |
|
1485 |
|
1486 char *error_text = error_message_buffer->str (); |
|
1487 |
|
1488 bind_builtin_variable ("__error_text__", error_text, 1); |
|
1489 |
|
1490 delete [] error_text; |
|
1491 |
|
1492 delete error_message_buffer; |
|
1493 |
|
1494 error_message_buffer = 0; |
|
1495 } |
|
1496 |
|
1497 void |
|
1498 clear_global_error_variable (void *) |
|
1499 { |
|
1500 delete error_message_buffer; |
|
1501 error_message_buffer = 0; |
|
1502 |
|
1503 bind_builtin_variable ("__error_text__", "", 1); |
|
1504 } |
|
1505 |
593
|
1506 // Give a global variable a definition. This will insert the symbol |
|
1507 // in the global table if necessary. |
|
1508 |
|
1509 // How is this different than install_builtin_variable? Are both |
|
1510 // functions needed? |
|
1511 |
|
1512 void |
2086
|
1513 bind_builtin_variable (const string& varname, octave_value *val, |
593
|
1514 int protect, int eternal, sv_Function sv_fcn, |
1755
|
1515 const string& help) |
593
|
1516 { |
|
1517 symbol_record *sr = global_sym_tab->lookup (varname, 1, 0); |
|
1518 |
1271
|
1519 // It is a programming error for a builtin symbol to be missing. |
|
1520 // Besides, we just inserted it, so it must be there. |
593
|
1521 |
|
1522 assert (sr); |
|
1523 |
|
1524 sr->unprotect (); |
|
1525 |
1271
|
1526 // Must do this before define, since define will call the special |
|
1527 // variable function only if it knows about it, and it needs to, so |
|
1528 // that user prefs can be properly initialized. |
593
|
1529 |
|
1530 if (sv_fcn) |
|
1531 sr->set_sv_function (sv_fcn); |
|
1532 |
|
1533 sr->define_builtin_var (val); |
|
1534 |
|
1535 if (protect) |
|
1536 sr->protect (); |
|
1537 |
|
1538 if (eternal) |
|
1539 sr->make_eternal (); |
|
1540 |
1755
|
1541 sr->document (help); |
529
|
1542 } |
|
1543 |
593
|
1544 void |
2086
|
1545 bind_builtin_variable (const string& varname, const octave_value& val, |
1406
|
1546 int protect, int eternal, sv_Function sv_fcn, |
1755
|
1547 const string& help) |
1406
|
1548 { |
2086
|
1549 octave_value *tc = new octave_value (val); |
1406
|
1550 bind_builtin_variable (varname, tc, protect, eternal, sv_fcn, help); |
|
1551 } |
|
1552 |
2065
|
1553 // XXX FIXME XX -- these should probably be moved to where they |
|
1554 // logically belong instead of being all grouped here. |
|
1555 |
|
1556 // This is split up to try to make compiling with gcc consume less |
|
1557 // memory and go a little faster. |
|
1558 |
|
1559 static void |
|
1560 install_builtin_variables_1 (void) |
529
|
1561 { |
1957
|
1562 DEFVAR (EDITOR, editor, 0, sv_editor, |
593
|
1563 "name of the editor to be invoked by the edit_history command"); |
|
1564 |
1957
|
1565 DEFVAR (EXEC_PATH, exec_path, 0, sv_exec_path, |
1613
|
1566 "colon separated list of directories to search for programs to run"); |
|
1567 |
1957
|
1568 DEFCONST (I, Complex (0.0, 1.0), 0, 0, |
593
|
1569 "sqrt (-1)"); |
|
1570 |
1957
|
1571 DEFCONST (Inf, octave_Inf, 0, 0, |
593
|
1572 "infinity"); |
|
1573 |
1957
|
1574 DEFVAR (INFO_FILE, info_file, 0, sv_info_file, |
593
|
1575 "name of the Octave info file"); |
|
1576 |
1957
|
1577 DEFVAR (INFO_PROGRAM, info_prog, 0, sv_info_prog, |
1613
|
1578 "name of the Octave info reader"); |
|
1579 |
1957
|
1580 DEFCONST (J, Complex (0.0, 1.0), 0, 0, |
593
|
1581 "sqrt (-1)"); |
529
|
1582 |
1957
|
1583 DEFCONST (NaN, octave_NaN, 0, 0, |
593
|
1584 "not a number"); |
|
1585 |
1957
|
1586 DEFVAR (LOADPATH, load_path, 0, sv_loadpath, |
593
|
1587 "colon separated list of directories to search for scripts"); |
|
1588 |
1957
|
1589 DEFVAR (IMAGEPATH, OCTAVE_IMAGEPATH, 0, |
686
|
1590 sv_imagepath, |
|
1591 "colon separated list of directories to search for image files"); |
2065
|
1592 } |
686
|
1593 |
2065
|
1594 static void |
|
1595 install_builtin_variables_2 (void) |
|
1596 { |
1957
|
1597 DEFCONSTX ("OCTAVE_VERSION", SBV_OCTAVE_VERSION, OCTAVE_VERSION, 0, 0, |
664
|
1598 "Octave version"); |
|
1599 |
1957
|
1600 DEFVAR (PS1, "\\s:\\#> ", 0, sv_ps1, |
593
|
1601 "primary prompt string"); |
|
1602 |
1957
|
1603 DEFVAR (PS2, "> ", 0, sv_ps2, |
593
|
1604 "secondary prompt string"); |
|
1605 |
1957
|
1606 DEFVAR (PS4, "+ ", 0, sv_ps4, |
1044
|
1607 "string printed before echoed input (enabled by --echo-input)"); |
|
1608 |
1957
|
1609 DEFCONST (PWD, get_working_directory ("initialize_globals"), 0, sv_pwd, |
593
|
1610 "current working directory"); |
|
1611 |
1957
|
1612 DEFCONST (SEEK_SET, 0.0, 0, 0, |
593
|
1613 "used with fseek to position file relative to the beginning"); |
529
|
1614 |
1957
|
1615 DEFCONST (SEEK_CUR, 1.0, 0, 0, |
593
|
1616 "used with fseek to position file relative to the current position"); |
|
1617 |
1957
|
1618 DEFCONST (SEEK_END, 2.0, 0, 0, |
593
|
1619 "used with fseek to position file relative to the end"); |
|
1620 |
1957
|
1621 DEFVAR (ans, , 0, 0, |
593
|
1622 ""); |
2065
|
1623 } |
593
|
1624 |
2065
|
1625 static void |
|
1626 install_builtin_variables_3 (void) |
|
1627 { |
1957
|
1628 DEFCONST (argv, , 0, 0, |
1343
|
1629 "the command line arguments this program was invoked with"); |
|
1630 |
1957
|
1631 DEFVAR (automatic_replot, 0.0, 0, automatic_replot, |
661
|
1632 "if true, auto-insert a replot command when a plot changes"); |
|
1633 |
1957
|
1634 DEFVAR (beep_on_error, 0.0, 0, beep_on_error, |
1423
|
1635 "if true, beep before printing error messages"); |
|
1636 |
1957
|
1637 DEFVAR (completion_append_char, " ", 0, sv_completion_append_char, |
1429
|
1638 "the string to append after successful command-line completion\n\ |
|
1639 attempts"); |
|
1640 |
1957
|
1641 DEFCONST (error_text, "", 0, 0, |
1489
|
1642 "the text of error messages that would have been printed in the |
|
1643 body of the most recent unwind_protect statement or the TRY part of\n\ |
|
1644 the most recent eval() command. Outside of unwind_protect and\n\ |
|
1645 eval(), or if no error has ocurred within them, the value of\n\ |
|
1646 __error_text__ is guaranteed to be the empty string."); |
|
1647 |
1957
|
1648 DEFVAR (default_return_value, Matrix (), 0, 0, |
1093
|
1649 "the default for value for unitialized variables returned from\n\ |
|
1650 functions. Only used if the variable initialize_return_values is\n\ |
|
1651 set to \"true\"."); |
593
|
1652 |
1957
|
1653 DEFVAR (default_save_format, "ascii", 0, sv_default_save_format, |
997
|
1654 "default format for files created with save, may be one of\n\ |
1418
|
1655 \"binary\", \"text\", or \"mat-binary\""); |
605
|
1656 |
1957
|
1657 DEFVAR (define_all_return_values, 0.0, 0, define_all_return_values, |
1407
|
1658 "control whether values returned from functions should have a\n\ |
|
1659 value even if one has not been explicitly assigned. See also\n\ |
1418
|
1660 default_return_value"); |
1407
|
1661 |
1957
|
1662 DEFVAR (do_fortran_indexing, 0.0, 0, do_fortran_indexing, |
593
|
1663 "allow single indices for matrices"); |
|
1664 |
1957
|
1665 DEFVAR (echo_executing_commands, 0.0, 0, echo_executing_commands, |
1588
|
1666 "echo commands as they are executed"); |
2065
|
1667 } |
1588
|
1668 |
2065
|
1669 static void |
|
1670 install_builtin_variables_4 (void) |
|
1671 { |
1957
|
1672 DEFCONST (e, exp (1.0), 0, 0, |
1474
|
1673 "exp (1)"); |
|
1674 |
1957
|
1675 DEFVAR (empty_list_elements_ok, "warn", 0, empty_list_elements_ok, |
593
|
1676 "ignore the empty element in expressions like `a = [[], 1]'"); |
529
|
1677 |
1957
|
1678 DEFCONST (eps, DBL_EPSILON, 0, 0, |
593
|
1679 "machine precision"); |
|
1680 |
1957
|
1681 DEFVAR (gnuplot_binary, "gnuplot", 0, sv_gnuplot_binary, |
593
|
1682 "path to gnuplot binary"); |
|
1683 |
1502
|
1684 #ifdef GNUPLOT_HAS_MULTIPLOT |
1506
|
1685 double with_multiplot = 1.0; |
1502
|
1686 #else |
1506
|
1687 double with_multiplot = 0.0; |
1502
|
1688 #endif |
|
1689 |
1957
|
1690 DEFVAR (gnuplot_has_multiplot, with_multiplot, 0, gnuplot_has_multiplot, |
1502
|
1691 "true if gnuplot supports multiplot, false otherwise"); |
|
1692 |
1957
|
1693 DEFVAR (history_file, default_history_file (), 0, sv_history_file, |
1646
|
1694 "name of command history file"); |
|
1695 |
1957
|
1696 DEFVAR (history_size, default_history_size (), 0, history_size, |
1646
|
1697 "number of commands to save in the history list"); |
|
1698 |
1957
|
1699 DEFCONST (i, Complex (0.0, 1.0), 1, 0, |
593
|
1700 "sqrt (-1)"); |
529
|
1701 |
1957
|
1702 DEFVAR (ignore_function_time_stamp, "system", 0, ignore_function_time_stamp, |
593
|
1703 "don't check to see if function files have changed since they were\n\ |
|
1704 last compiled. Possible values are \"system\" and \"all\""); |
|
1705 |
1957
|
1706 DEFVAR (implicit_str_to_num_ok, 0.0, 0, implicit_str_to_num_ok, |
593
|
1707 "allow implicit string to number conversion"); |
2065
|
1708 } |
593
|
1709 |
2065
|
1710 static void |
|
1711 install_builtin_variables_5 (void) |
|
1712 { |
1957
|
1713 DEFCONST (inf, octave_Inf, 0, 0, |
593
|
1714 "infinity"); |
|
1715 |
1957
|
1716 DEFCONST (j, Complex (0.0, 1.0), 1, 0, |
593
|
1717 "sqrt (-1)"); |
529
|
1718 |
1957
|
1719 DEFCONST (nan, octave_NaN, 0, 0, |
593
|
1720 "not a number"); |
|
1721 |
1957
|
1722 DEFVAR (ok_to_lose_imaginary_part, "warn", 0, ok_to_lose_imaginary_part, |
593
|
1723 "silently convert from complex to real by dropping imaginary part"); |
|
1724 |
1957
|
1725 DEFVAR (output_max_field_width, 10.0, 0, set_output_max_field_width, |
593
|
1726 "maximum width of an output field for numeric output"); |
|
1727 |
1957
|
1728 DEFVAR (output_precision, 5.0, 0, set_output_precision, |
593
|
1729 "number of significant figures to display for numeric output"); |
|
1730 |
1957
|
1731 DEFCONST (pi, 4.0 * atan (1.0), 0, 0, |
593
|
1732 "ratio of the circumference of a circle to its diameter"); |
|
1733 |
1957
|
1734 DEFVAR (prefer_column_vectors, 1.0, 0, prefer_column_vectors, |
593
|
1735 "prefer column/row vectors"); |
|
1736 |
1957
|
1737 DEFVAR (prefer_zero_one_indexing, 0.0, 0, prefer_zero_one_indexing, |
593
|
1738 "when there is a conflict, prefer zero-one style indexing"); |
2065
|
1739 } |
593
|
1740 |
2065
|
1741 static void |
|
1742 install_builtin_variables_6 (void) |
|
1743 { |
1957
|
1744 DEFVAR (print_answer_id_name, 1.0, 0, print_answer_id_name, |
593
|
1745 "set output style to print `var_name = ...'"); |
|
1746 |
1957
|
1747 DEFVAR (print_empty_dimensions, 1.0, 0, print_empty_dimensions, |
593
|
1748 "also print dimensions of empty matrices"); |
|
1749 |
1957
|
1750 DEFCONST (program_invocation_name, raw_prog_name, 0, 0, |
1343
|
1751 "the full name of the current program or script, including the\n\ |
|
1752 directory specification"); |
|
1753 |
1957
|
1754 DEFCONST (program_name, prog_name, 0, 0, |
1343
|
1755 "the name of the current program or script"); |
|
1756 |
1957
|
1757 DEFVAR (propagate_empty_matrices, 1.0, 0, propagate_empty_matrices, |
593
|
1758 "operations on empty matrices return an empty matrix, not an error"); |
529
|
1759 |
1045
|
1760 #if 0 |
1957
|
1761 DEFVAR (read_only_constants, 1.0, 0, read_only_constants, |
1045
|
1762 "allow built-in constants to be modified"); |
|
1763 #endif |
|
1764 |
1957
|
1765 DEFCONST (realmax, DBL_MAX, 0, 0, |
868
|
1766 "realmax (): return largest representable floating point number"); |
|
1767 |
1957
|
1768 DEFCONST (realmin, DBL_MIN, 0, 0, |
868
|
1769 "realmin (): return smallest representable floating point number"); |
|
1770 |
1957
|
1771 DEFVAR (resize_on_range_error, 1.0, 0, resize_on_range_error, |
593
|
1772 "enlarge matrices on assignment"); |
|
1773 |
1957
|
1774 DEFVAR (return_last_computed_value, 0.0, 0, return_last_computed_value, |
593
|
1775 "if a function does not return any values explicitly, return the\n\ |
|
1776 last computed value"); |
2065
|
1777 } |
593
|
1778 |
2065
|
1779 static void |
|
1780 install_builtin_variables_7 (void) |
|
1781 { |
1957
|
1782 DEFVAR (save_precision, 15.0, 0, set_save_precision, |
1923
|
1783 "number of significant figures kept by the ASCII save command"); |
|
1784 |
1957
|
1785 DEFVAR (saving_history, 1.0, 0, saving_history, |
1643
|
1786 "save command history"); |
593
|
1787 |
1957
|
1788 DEFVAR (silent_functions, 0.0, 0, silent_functions, |
593
|
1789 "suppress printing results in called functions"); |
|
1790 |
1957
|
1791 DEFVAR (split_long_rows, 1.0, 0, split_long_rows, |
593
|
1792 "split long matrix rows instead of wrapping"); |
529
|
1793 |
1957
|
1794 DEFVAR (struct_levels_to_print, 2.0, 0, struct_levels_to_print, |
1198
|
1795 "number of levels of structure elements to print"); |
|
1796 |
1139
|
1797 #ifdef USE_GNU_INFO |
1957
|
1798 DEFVAR (suppress_verbose_help_message, 0.0, 0, suppress_verbose_help_message, |
1116
|
1799 "suppress printing of message pointing to additional help in the\n\ |
|
1800 help and usage functions"); |
1139
|
1801 #endif |
1116
|
1802 |
1957
|
1803 DEFCONSTX ("stdin", SBV_stdin, 0.0, 0, 0, |
593
|
1804 "file number of the standard input stream"); |
|
1805 |
1957
|
1806 DEFCONSTX ("stdout", SBV_stderr, 1.0, 0, 0, |
593
|
1807 "file number of the standard output stream"); |
|
1808 |
1957
|
1809 DEFCONSTX ("stderr", SBV_stderr, 2.0, 0, 0, |
593
|
1810 "file number of the standard error stream"); |
|
1811 |
1957
|
1812 DEFVAR (treat_neg_dim_as_zero, 0.0, 0, treat_neg_dim_as_zero, |
593
|
1813 "convert negative dimensions to zero"); |
2065
|
1814 } |
593
|
1815 |
2065
|
1816 static void |
|
1817 install_builtin_variables_8 (void) |
|
1818 { |
1957
|
1819 DEFVAR (warn_assign_as_truth_value, 1.0, 0, warn_assign_as_truth_value, |
593
|
1820 "produce warning for assignments used as truth values"); |
|
1821 |
1957
|
1822 DEFVAR (warn_comma_in_global_decl, 1.0, 0, warn_comma_in_global_decl, |
593
|
1823 "produce warning for commas in global declarations"); |
|
1824 |
1957
|
1825 DEFVAR (warn_divide_by_zero, 1.0, 0, warn_divide_by_zero, |
593
|
1826 "on IEEE machines, allow divide by zero errors to be suppressed"); |
1037
|
1827 |
1957
|
1828 DEFVAR (warn_function_name_clash, 1.0, 0, warn_function_name_clash, |
1037
|
1829 "produce warning if function name conflicts with file name"); |
1093
|
1830 |
1957
|
1831 DEFVAR (warn_missing_semicolon, 0.0, 0, warn_missing_semicolon, |
1511
|
1832 "produce a warning if a statement in a function file is not |
|
1833 terminated with a semicolon"); |
1509
|
1834 |
1957
|
1835 DEFVAR (whitespace_in_literal_matrix, "", 0, whitespace_in_literal_matrix, |
1093
|
1836 "control auto-insertion of commas and semicolons in literal matrices"); |
529
|
1837 } |
|
1838 |
2065
|
1839 void |
|
1840 install_builtin_variables (void) |
|
1841 { |
|
1842 install_builtin_variables_1 (); |
|
1843 install_builtin_variables_2 (); |
|
1844 install_builtin_variables_3 (); |
|
1845 install_builtin_variables_4 (); |
|
1846 install_builtin_variables_5 (); |
|
1847 install_builtin_variables_6 (); |
|
1848 install_builtin_variables_7 (); |
|
1849 install_builtin_variables_8 (); |
2076
|
1850 |
2097
|
1851 symbols_of_pager (); |
2076
|
1852 symbols_of_syscalls (); |
2065
|
1853 } |
|
1854 |
593
|
1855 // Deleting names from the symbol tables. |
|
1856 |
1957
|
1857 DEFUN_TEXT (clear, args, , |
668
|
1858 "clear [-x] [name ...]\n\ |
|
1859 \n\ |
|
1860 Clear symbol(s) matching a list of globbing patterns.\n\ |
593
|
1861 \n\ |
668
|
1862 If no arguments are given, clear all user-defined variables and |
|
1863 functions.\n\ |
|
1864 \n\ |
|
1865 With -x, exclude the named variables") |
529
|
1866 { |
2086
|
1867 octave_value_list retval; |
593
|
1868 |
1755
|
1869 int argc = args.length () + 1; |
593
|
1870 |
1968
|
1871 string_vector argv = args.make_argv ("clear"); |
1755
|
1872 |
|
1873 if (error_state) |
|
1874 return retval; |
668
|
1875 |
1271
|
1876 // Always clear the local table, but don't clear currently compiled |
|
1877 // functions unless we are at the top level. (Allowing that to |
|
1878 // happen inside functions would result in pretty odd behavior...) |
593
|
1879 |
|
1880 int clear_user_functions = (curr_sym_tab == top_level_sym_tab); |
|
1881 |
1755
|
1882 if (argc == 1) |
593
|
1883 { |
|
1884 curr_sym_tab->clear (); |
|
1885 global_sym_tab->clear (clear_user_functions); |
|
1886 } |
529
|
1887 else |
|
1888 { |
668
|
1889 int exclusive = 0; |
|
1890 |
1755
|
1891 int idx = 1; |
|
1892 |
|
1893 if (argc > 1) |
668
|
1894 { |
1755
|
1895 if (argv[idx] == "-x") |
|
1896 exclusive = 1; |
668
|
1897 } |
|
1898 |
|
1899 int lcount = 0; |
|
1900 int gcount = 0; |
|
1901 int fcount = 0; |
529
|
1902 |
1755
|
1903 string_vector lvars; |
|
1904 string_vector gvars; |
|
1905 string_vector fcns; |
668
|
1906 |
|
1907 if (argc > 0) |
593
|
1908 { |
893
|
1909 lvars = curr_sym_tab->list (lcount, 0, 0, 0, |
1418
|
1910 SYMTAB_VARIABLES, |
668
|
1911 SYMTAB_LOCAL_SCOPE); |
|
1912 |
893
|
1913 gvars = curr_sym_tab->list (gcount, 0, 0, 0, |
1418
|
1914 SYMTAB_VARIABLES, |
668
|
1915 SYMTAB_GLOBAL_SCOPE); |
|
1916 |
893
|
1917 fcns = global_sym_tab->list (fcount, 0, 0, 0, |
|
1918 symbol_def::USER_FUNCTION, |
864
|
1919 SYMTAB_ALL_SCOPES); |
668
|
1920 } |
|
1921 |
1970
|
1922 for (int k = idx; k < argc; k++) |
668
|
1923 { |
1755
|
1924 string patstr = argv[k]; |
668
|
1925 |
1755
|
1926 if (! patstr.empty ()) |
593
|
1927 { |
1792
|
1928 glob_match pattern (patstr); |
1755
|
1929 |
593
|
1930 int i; |
|
1931 for (i = 0; i < lcount; i++) |
|
1932 { |
1755
|
1933 string nm = lvars[i]; |
1792
|
1934 int match = pattern.match (nm); |
668
|
1935 if ((exclusive && ! match) || (! exclusive && match)) |
|
1936 curr_sym_tab->clear (nm); |
593
|
1937 } |
529
|
1938 |
593
|
1939 int count; |
|
1940 for (i = 0; i < gcount; i++) |
|
1941 { |
1755
|
1942 string nm = gvars[i]; |
1792
|
1943 int match = pattern.match (nm); |
668
|
1944 if ((exclusive && ! match) || (! exclusive && match)) |
593
|
1945 { |
668
|
1946 count = curr_sym_tab->clear (nm); |
593
|
1947 if (count > 0) |
668
|
1948 global_sym_tab->clear (nm, clear_user_functions); |
593
|
1949 } |
|
1950 } |
529
|
1951 |
593
|
1952 for (i = 0; i < fcount; i++) |
|
1953 { |
1755
|
1954 string nm = fcns[i]; |
1792
|
1955 int match = pattern.match (nm); |
668
|
1956 if ((exclusive && ! match) || (! exclusive && match)) |
593
|
1957 { |
668
|
1958 count = curr_sym_tab->clear (nm); |
864
|
1959 global_sym_tab->clear (nm, clear_user_functions); |
593
|
1960 } |
|
1961 } |
|
1962 } |
|
1963 } |
|
1964 } |
|
1965 |
|
1966 return retval; |
529
|
1967 } |
|
1968 |
1
|
1969 /* |
|
1970 ;;; Local Variables: *** |
|
1971 ;;; mode: C++ *** |
|
1972 ;;; End: *** |
|
1973 */ |