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