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. |
|
760 |
581
|
761 reading_fcn_file = old_reading_fcn_file_state; |
|
762 |
|
763 unwind_protect_int (reading_script_file); |
|
764 reading_script_file = 1; |
|
765 |
|
766 parse_and_execute (ffile, 1); |
|
767 |
|
768 script_file_executed = 1; |
|
769 } |
|
770 fclose (ffile); |
|
771 } |
|
772 |
|
773 run_unwind_frame ("parse_fcn_file"); |
|
774 |
|
775 return script_file_executed; |
|
776 } |
|
777 |
1827
|
778 static bool |
581
|
779 load_fcn_from_file (symbol_record *sym_rec, int exec_script) |
|
780 { |
1827
|
781 bool script_file_executed = false; |
581
|
782 |
1755
|
783 string nm = sym_rec->name (); |
581
|
784 |
704
|
785 if (load_octave_oct_file (nm)) |
|
786 { |
|
787 force_link_to_function (nm); |
|
788 } |
|
789 else |
581
|
790 { |
1755
|
791 string ff = fcn_file_in_path (nm); |
581
|
792 |
1606
|
793 // These are needed by yyparse. |
|
794 |
1755
|
795 begin_unwind_frame ("load_fcn_from_file"); |
|
796 |
|
797 unwind_protect_str (curr_fcn_file_name); |
|
798 unwind_protect_str (curr_fcn_file_full_name); |
|
799 |
1606
|
800 curr_fcn_file_name = nm; |
|
801 curr_fcn_file_full_name = ff; |
|
802 |
1755
|
803 if (ff.length () > 0) |
|
804 script_file_executed = parse_fcn_file (exec_script, ff); |
581
|
805 |
|
806 if (! (error_state || script_file_executed)) |
|
807 force_link_to_function (nm); |
1755
|
808 |
|
809 run_unwind_frame ("load_fcn_from_file"); |
581
|
810 } |
|
811 |
|
812 return script_file_executed; |
|
813 } |
|
814 |
1827
|
815 bool |
581
|
816 lookup (symbol_record *sym_rec, int exec_script) |
|
817 { |
1827
|
818 bool script_executed = false; |
581
|
819 |
|
820 if (! sym_rec->is_linked_to_global ()) |
|
821 { |
|
822 if (sym_rec->is_defined ()) |
|
823 { |
|
824 if (sym_rec->is_function () && symbol_out_of_date (sym_rec)) |
1271
|
825 script_executed = load_fcn_from_file (sym_rec, exec_script); |
581
|
826 } |
|
827 else if (! sym_rec->is_formal_parameter ()) |
|
828 { |
|
829 link_to_builtin_or_function (sym_rec); |
1271
|
830 |
581
|
831 if (! sym_rec->is_defined ()) |
1271
|
832 script_executed = load_fcn_from_file (sym_rec, exec_script); |
581
|
833 else 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 } |
|
837 |
1271
|
838 return script_executed; |
581
|
839 } |
|
840 |
|
841 // Get the symbol record for the given name that is visible in the |
|
842 // current scope. Reread any function definitions that appear to be |
|
843 // out of date. If a function is available in a file but is not |
|
844 // currently loaded, this will load it and insert the name in the |
|
845 // current symbol table. |
|
846 |
|
847 symbol_record * |
1755
|
848 lookup_by_name (const string& nm, int exec_script) |
581
|
849 { |
|
850 symbol_record *sym_rec = curr_sym_tab->lookup (nm, 1, 0); |
|
851 |
|
852 lookup (sym_rec, exec_script); |
|
853 |
|
854 return sym_rec; |
|
855 } |
|
856 |
1755
|
857 string |
|
858 get_help_from_file (const string& path) |
991
|
859 { |
1755
|
860 string retval; |
|
861 |
|
862 if (! path.empty ()) |
991
|
863 { |
1755
|
864 FILE *fptr = fopen (path.c_str (), "r"); |
|
865 |
991
|
866 if (fptr) |
|
867 { |
1755
|
868 retval = gobble_leading_white_space (fptr, 1); |
991
|
869 fclose (fptr); |
|
870 } |
|
871 } |
1755
|
872 |
|
873 return retval; |
991
|
874 } |
|
875 |
593
|
876 // Variable values. |
195
|
877 |
581
|
878 // Look for the given name in the global symbol table. If it refers |
|
879 // to a string, return a new copy. If not, return 0; |
|
880 |
1755
|
881 string |
|
882 builtin_string_variable (const string& name) |
1
|
883 { |
195
|
884 symbol_record *sr = global_sym_tab->lookup (name, 0, 0); |
|
885 |
1271
|
886 // It is a prorgramming error to look for builtins that aren't. |
195
|
887 |
529
|
888 assert (sr); |
195
|
889 |
1755
|
890 string retval; |
1
|
891 |
490
|
892 tree_fvc *defn = sr->def (); |
195
|
893 |
529
|
894 if (defn) |
1
|
895 { |
|
896 tree_constant val = defn->eval (0); |
195
|
897 |
610
|
898 if (! error_state && val.is_string ()) |
1755
|
899 retval = val.string_value (); |
1
|
900 } |
|
901 |
|
902 return retval; |
|
903 } |
|
904 |
581
|
905 // Look for the given name in the global symbol table. If it refers |
1504
|
906 // to a real scalar, place the value in d and return 1. Otherwise, |
|
907 // return 0. |
581
|
908 |
1
|
909 int |
1755
|
910 builtin_real_scalar_variable (const string& name, double& d) |
1
|
911 { |
1504
|
912 int status = 0; |
195
|
913 symbol_record *sr = global_sym_tab->lookup (name, 0, 0); |
|
914 |
1271
|
915 // It is a prorgramming error to look for builtins that aren't. |
195
|
916 |
529
|
917 assert (sr); |
1
|
918 |
490
|
919 tree_fvc *defn = sr->def (); |
195
|
920 |
529
|
921 if (defn) |
1
|
922 { |
|
923 tree_constant val = defn->eval (0); |
195
|
924 |
598
|
925 if (! error_state && val.is_scalar_type ()) |
1
|
926 { |
|
927 d = val.double_value (); |
1504
|
928 status = 1; |
1
|
929 } |
|
930 } |
|
931 |
|
932 return status; |
|
933 } |
|
934 |
1093
|
935 // Look for the given name in the global symbol table. |
|
936 |
|
937 tree_constant |
1755
|
938 builtin_any_variable (const string& name) |
1093
|
939 { |
|
940 tree_constant retval; |
|
941 |
|
942 symbol_record *sr = global_sym_tab->lookup (name, 0, 0); |
|
943 |
1271
|
944 // It is a prorgramming error to look for builtins that aren't. |
1093
|
945 |
|
946 assert (sr); |
|
947 |
|
948 tree_fvc *defn = sr->def (); |
|
949 |
|
950 if (defn) |
|
951 retval = defn->eval (0); |
|
952 |
|
953 return retval; |
|
954 } |
|
955 |
593
|
956 // Global stuff and links to builtin variables and functions. |
|
957 |
581
|
958 // Make the definition of the symbol record sr be the same as the |
|
959 // definition of the global variable of the same name, creating it if |
1418
|
960 // it doesn't already exist. |
581
|
961 |
195
|
962 void |
|
963 link_to_global_variable (symbol_record *sr) |
|
964 { |
|
965 if (sr->is_linked_to_global ()) |
|
966 return; |
|
967 |
1755
|
968 string nm = sr->name (); |
|
969 |
|
970 symbol_record *gsr = global_sym_tab->lookup (nm, 1, 0); |
195
|
971 |
|
972 if (sr->is_formal_parameter ()) |
|
973 { |
1755
|
974 error ("can't make function parameter `%s' global", nm.c_str ()); |
195
|
975 return; |
|
976 } |
|
977 |
1271
|
978 // There must be a better way to do this. XXX FIXME XXX |
195
|
979 |
|
980 if (sr->is_variable ()) |
|
981 { |
1271
|
982 // Would be nice not to have this cast. XXX FIXME XXX |
|
983 |
195
|
984 tree_constant *tmp = (tree_constant *) sr->def (); |
529
|
985 if (tmp) |
|
986 tmp = new tree_constant (*tmp); |
|
987 else |
207
|
988 tmp = new tree_constant (); |
195
|
989 gsr->define (tmp); |
|
990 } |
|
991 else |
529
|
992 sr->clear (); |
195
|
993 |
1271
|
994 // If the global symbol is currently defined as a function, we need |
|
995 // to hide it with a variable. |
195
|
996 |
|
997 if (gsr->is_function ()) |
529
|
998 gsr->define ((tree_constant *) 0); |
195
|
999 |
|
1000 sr->alias (gsr, 1); |
|
1001 sr->mark_as_linked_to_global (); |
|
1002 } |
|
1003 |
581
|
1004 // Make the definition of the symbol record sr be the same as the |
|
1005 // definition of the builtin variable of the same name. |
|
1006 |
195
|
1007 void |
|
1008 link_to_builtin_variable (symbol_record *sr) |
|
1009 { |
|
1010 symbol_record *tmp_sym = global_sym_tab->lookup (sr->name (), 0, 0); |
|
1011 |
529
|
1012 if (tmp_sym && tmp_sym->is_builtin_variable ()) |
|
1013 sr->alias (tmp_sym); |
195
|
1014 } |
|
1015 |
581
|
1016 // Make the definition of the symbol record sr be the same as the |
|
1017 // definition of the builtin variable or function, or user function of |
|
1018 // the same name, provided that the name has not been used as a formal |
|
1019 // parameter. |
|
1020 |
195
|
1021 void |
|
1022 link_to_builtin_or_function (symbol_record *sr) |
|
1023 { |
|
1024 symbol_record *tmp_sym = global_sym_tab->lookup (sr->name (), 0, 0); |
|
1025 |
529
|
1026 if (tmp_sym |
|
1027 && (tmp_sym->is_builtin_variable () || tmp_sym->is_function ()) |
|
1028 && ! tmp_sym->is_formal_parameter ()) |
|
1029 sr->alias (tmp_sym); |
195
|
1030 } |
|
1031 |
581
|
1032 // Force a link to a function in the current symbol table. This is |
|
1033 // used just after defining a function to avoid different behavior |
|
1034 // depending on whether or not the function has been evaluated after |
|
1035 // being defined. |
|
1036 // |
|
1037 // Return without doing anything if there isn't a function with the |
|
1038 // given name defined in the global symbol table. |
|
1039 |
195
|
1040 void |
1755
|
1041 force_link_to_function (const string& id_name) |
195
|
1042 { |
|
1043 symbol_record *gsr = global_sym_tab->lookup (id_name, 1, 0); |
|
1044 if (gsr->is_function ()) |
|
1045 { |
|
1046 curr_sym_tab->clear (id_name); |
|
1047 symbol_record *csr = curr_sym_tab->lookup (id_name, 1, 0); |
|
1048 csr->alias (gsr); |
|
1049 } |
|
1050 } |
|
1051 |
605
|
1052 // Help stuff. Shouldn't this go in help.cc? |
593
|
1053 |
|
1054 // It's not likely that this does the right thing now. XXX FIXME XXX |
|
1055 |
1755
|
1056 string_vector |
593
|
1057 make_name_list (void) |
|
1058 { |
|
1059 int key_len = 0; |
|
1060 int glb_len = 0; |
|
1061 int top_len = 0; |
|
1062 int lcl_len = 0; |
|
1063 |
1755
|
1064 string_vector key; |
|
1065 string_vector glb; |
|
1066 string_vector top; |
|
1067 string_vector lcl; |
|
1068 string_vector ffl; |
593
|
1069 |
1271
|
1070 // Each of these functions returns a new vector of pointers to new |
|
1071 // strings. |
593
|
1072 |
|
1073 key = names (keyword_help (), key_len); |
1795
|
1074 |
593
|
1075 glb = global_sym_tab->list (glb_len); |
1795
|
1076 |
593
|
1077 top = top_level_sym_tab->list (top_len); |
1795
|
1078 |
593
|
1079 if (top_level_sym_tab != curr_sym_tab) |
|
1080 lcl = curr_sym_tab->list (lcl_len); |
1795
|
1081 |
1796
|
1082 ffl = get_fcn_file_names (1); |
1795
|
1083 int ffl_len = ffl.length (); |
593
|
1084 |
|
1085 int total_len = key_len + glb_len + top_len + lcl_len + ffl_len; |
|
1086 |
1755
|
1087 string_vector list (total_len); |
1418
|
1088 |
1271
|
1089 // Put all the symbols in one big list. Only copy pointers, not the |
|
1090 // strings they point to, then only delete the original array of |
|
1091 // pointers, and not the strings they point to. |
593
|
1092 |
|
1093 int j = 0; |
|
1094 int i = 0; |
|
1095 for (i = 0; i < key_len; i++) |
|
1096 list[j++] = key[i]; |
|
1097 |
|
1098 for (i = 0; i < glb_len; i++) |
|
1099 list[j++] = glb[i]; |
|
1100 |
|
1101 for (i = 0; i < top_len; i++) |
|
1102 list[j++] = top[i]; |
|
1103 |
|
1104 for (i = 0; i < lcl_len; i++) |
|
1105 list[j++] = lcl[i]; |
|
1106 |
|
1107 for (i = 0; i < ffl_len; i++) |
|
1108 list[j++] = ffl[i]; |
|
1109 |
|
1110 return list; |
|
1111 } |
|
1112 |
|
1113 // List variable names. |
|
1114 |
|
1115 static void |
|
1116 print_symbol_info_line (ostrstream& output_buf, const symbol_record_info& s) |
|
1117 { |
|
1118 output_buf << (s.is_read_only () ? " -" : " w"); |
|
1119 output_buf << (s.is_eternal () ? "- " : "d "); |
|
1120 #if 0 |
|
1121 output_buf << (s.hides_fcn () ? "f" : (s.hides_builtin () ? "F" : "-")); |
|
1122 #endif |
1755
|
1123 output_buf.form (" %-16s", s.type_as_string ().c_str ()); |
593
|
1124 if (s.is_function ()) |
|
1125 output_buf << " - -"; |
|
1126 else |
|
1127 { |
|
1128 output_buf.form ("%7d", s.rows ()); |
|
1129 output_buf.form ("%7d", s.columns ()); |
|
1130 } |
|
1131 output_buf << " " << s.name () << "\n"; |
|
1132 } |
|
1133 |
|
1134 static void |
|
1135 print_long_listing (ostrstream& output_buf, symbol_record_info *s) |
|
1136 { |
|
1137 if (! s) |
|
1138 return; |
|
1139 |
|
1140 symbol_record_info *ptr = s; |
|
1141 while (ptr->is_defined ()) |
|
1142 { |
|
1143 print_symbol_info_line (output_buf, *ptr); |
|
1144 ptr++; |
|
1145 } |
|
1146 } |
|
1147 |
|
1148 static int |
1755
|
1149 maybe_list (const char *header, const string_vector& argv, int argc, |
867
|
1150 ostrstream& output_buf, int show_verbose, symbol_table |
|
1151 *sym_tab, unsigned type, unsigned scope) |
593
|
1152 { |
|
1153 int count; |
|
1154 int status = 0; |
|
1155 if (show_verbose) |
|
1156 { |
|
1157 symbol_record_info *symbols; |
867
|
1158 symbols = sym_tab->long_list (count, argv, argc, 1, type, scope); |
593
|
1159 if (symbols && count > 0) |
|
1160 { |
|
1161 output_buf << "\n" << header << "\n\n" |
|
1162 << "prot type rows cols name\n" |
|
1163 << "==== ==== ==== ==== ====\n"; |
|
1164 |
|
1165 print_long_listing (output_buf, symbols); |
|
1166 status = 1; |
|
1167 } |
|
1168 delete [] symbols; |
|
1169 } |
|
1170 else |
|
1171 { |
1755
|
1172 string_vector symbols = sym_tab->list (count, argv, argc, 1, |
|
1173 type, scope); |
|
1174 if (symbols.length () > 0 && count > 0) |
593
|
1175 { |
|
1176 output_buf << "\n" << header << "\n\n"; |
1808
|
1177 symbols.list_in_columns (output_buf); |
593
|
1178 status = 1; |
|
1179 } |
|
1180 } |
|
1181 return status; |
|
1182 } |
|
1183 |
1488
|
1184 DEFUN_TEXT ("document", Fdocument, Sdocument, 10, |
593
|
1185 "document symbol string ...\n\ |
|
1186 \n\ |
|
1187 Associate a cryptic message with a variable name.") |
|
1188 { |
|
1189 Octave_object retval; |
|
1190 |
1755
|
1191 int argc = args.length () + 1; |
|
1192 |
|
1193 string_vector argv = make_argv (args, "document"); |
|
1194 |
|
1195 if (error_state) |
|
1196 return retval; |
593
|
1197 |
|
1198 if (argc == 3) |
|
1199 { |
1755
|
1200 string name = argv[1]; |
|
1201 string help = argv[2]; |
593
|
1202 |
|
1203 if (is_builtin_variable (name)) |
|
1204 error ("sorry, can't redefine help for builtin variables"); |
|
1205 else |
|
1206 { |
|
1207 symbol_record *sym_rec = curr_sym_tab->lookup (name, 0); |
|
1208 |
|
1209 if (sym_rec) |
|
1210 sym_rec->document (help); |
|
1211 else |
1755
|
1212 error ("document: no such symbol `%s'", name.c_str ()); |
593
|
1213 } |
|
1214 } |
|
1215 else |
|
1216 print_usage ("document"); |
|
1217 |
|
1218 return retval; |
|
1219 } |
|
1220 |
584
|
1221 // XXX FIXME XXX -- this should take a list of regular expressions |
|
1222 // naming the variables to look for. |
|
1223 |
581
|
1224 static Octave_object |
1755
|
1225 do_who (int argc, const string_vector& argv) |
529
|
1226 { |
|
1227 Octave_object retval; |
|
1228 |
|
1229 int show_builtins = 0; |
|
1230 int show_functions = (curr_sym_tab == top_level_sym_tab); |
|
1231 int show_variables = 1; |
|
1232 int show_verbose = 0; |
|
1233 |
1755
|
1234 string my_name = argv[0]; |
584
|
1235 |
529
|
1236 if (argc > 1) |
|
1237 { |
|
1238 show_functions = 0; |
|
1239 show_variables = 0; |
|
1240 } |
|
1241 |
1857
|
1242 int i; |
|
1243 for (i = 1; i < argc; i++) |
529
|
1244 { |
1755
|
1245 if (argv[i] == "-all" || argv[i] == "-a") |
529
|
1246 { |
|
1247 show_builtins++; |
|
1248 show_functions++; |
1418
|
1249 show_variables++; |
529
|
1250 } |
1755
|
1251 else if (argv[i] == "-builtins" || argv[i] == "-b") |
529
|
1252 show_builtins++; |
1755
|
1253 else if (argv[i] == "-functions" || argv[i] == "-f") |
529
|
1254 show_functions++; |
1755
|
1255 else if (argv[i] == "-long" || argv[i] == "-l") |
605
|
1256 show_verbose++; |
1755
|
1257 else if (argv[i] == "-variables" || argv[i] == "-v") |
529
|
1258 show_variables++; |
1755
|
1259 else if (argv[i][0] == '-') |
|
1260 warning ("%s: unrecognized option `%s'", my_name.c_str (), |
|
1261 argv[i].c_str ()); |
529
|
1262 else |
867
|
1263 break; |
529
|
1264 } |
|
1265 |
1857
|
1266 int npats = argc - i; |
|
1267 string_vector pats (npats); |
|
1268 for (int j = 0; j < npats; j++) |
|
1269 pats[j] = argv[i+j]; |
|
1270 |
1271
|
1271 // If the user specified -l and nothing else, show variables. If |
|
1272 // evaluating this at the top level, also show functions. |
529
|
1273 |
|
1274 if (show_verbose && ! (show_builtins || show_functions || show_variables)) |
|
1275 { |
|
1276 show_functions = (curr_sym_tab == top_level_sym_tab); |
|
1277 show_variables = 1; |
|
1278 } |
|
1279 |
|
1280 ostrstream output_buf; |
|
1281 int pad_after = 0; |
|
1282 |
|
1283 if (show_builtins) |
|
1284 { |
1857
|
1285 pad_after += maybe_list ("*** built-in variables:", pats, npats, |
529
|
1286 output_buf, show_verbose, global_sym_tab, |
|
1287 symbol_def::BUILTIN_VARIABLE, |
|
1288 SYMTAB_ALL_SCOPES); |
|
1289 |
1857
|
1290 pad_after += maybe_list ("*** built-in functions:", pats, npats, |
529
|
1291 output_buf, show_verbose, global_sym_tab, |
|
1292 symbol_def::BUILTIN_FUNCTION, |
|
1293 SYMTAB_ALL_SCOPES); |
|
1294 } |
|
1295 |
|
1296 if (show_functions) |
|
1297 { |
|
1298 pad_after += maybe_list ("*** currently compiled functions:", |
1857
|
1299 pats, npats, output_buf, show_verbose, |
867
|
1300 global_sym_tab, symbol_def::USER_FUNCTION, |
529
|
1301 SYMTAB_ALL_SCOPES); |
|
1302 } |
|
1303 |
|
1304 if (show_variables) |
|
1305 { |
1857
|
1306 pad_after += maybe_list ("*** local user variables:", pats, npats, |
529
|
1307 output_buf, show_verbose, curr_sym_tab, |
|
1308 symbol_def::USER_VARIABLE, |
1418
|
1309 SYMTAB_LOCAL_SCOPE); |
529
|
1310 |
|
1311 pad_after += maybe_list ("*** globally visible user variables:", |
1857
|
1312 pats, npats, output_buf, show_verbose, |
867
|
1313 curr_sym_tab, symbol_def::USER_VARIABLE, |
529
|
1314 SYMTAB_GLOBAL_SCOPE); |
|
1315 } |
|
1316 |
|
1317 if (pad_after) |
|
1318 output_buf << "\n"; |
|
1319 |
|
1320 output_buf << ends; |
|
1321 maybe_page_output (output_buf); |
|
1322 |
581
|
1323 return retval; |
|
1324 } |
|
1325 |
1488
|
1326 DEFUN_TEXT ("who", Fwho, Swho, 10, |
581
|
1327 "who [-all] [-builtins] [-functions] [-long] [-variables]\n\ |
|
1328 \n\ |
|
1329 List currently defined symbol(s). Options may be shortened to one\n\ |
|
1330 character, but may not be combined.") |
|
1331 { |
|
1332 Octave_object retval; |
|
1333 |
1755
|
1334 int argc = args.length () + 1; |
|
1335 |
|
1336 string_vector argv = make_argv (args, "who"); |
|
1337 |
|
1338 if (error_state) |
|
1339 return retval; |
581
|
1340 |
1488
|
1341 retval = do_who (argc, argv); |
581
|
1342 |
529
|
1343 return retval; |
|
1344 } |
|
1345 |
1488
|
1346 DEFUN_TEXT ("whos", Fwhos, Swhos, 10, |
581
|
1347 "whos [-all] [-builtins] [-functions] [-long] [-variables]\n\ |
|
1348 \n\ |
|
1349 List currently defined symbol(s). Options may be shortened to one\n\ |
|
1350 character, but may not be combined.") |
|
1351 { |
|
1352 Octave_object retval; |
|
1353 |
712
|
1354 int nargin = args.length (); |
|
1355 |
742
|
1356 Octave_object tmp_args; |
|
1357 for (int i = nargin; i > 0; i--) |
|
1358 tmp_args(i) = args(i-1); |
|
1359 tmp_args(0) = "-long"; |
581
|
1360 |
742
|
1361 int argc = tmp_args.length () + 1; |
1755
|
1362 |
|
1363 string_vector argv = make_argv (tmp_args, "whos"); |
581
|
1364 |
|
1365 if (error_state) |
|
1366 return retval; |
|
1367 |
1488
|
1368 retval = do_who (argc, argv); |
581
|
1369 |
|
1370 return retval; |
|
1371 } |
|
1372 |
593
|
1373 // Install variables and functions in the symbol tables. |
529
|
1374 |
593
|
1375 void |
1755
|
1376 install_builtin_mapper (const builtin_mapper_function& mf) |
529
|
1377 { |
1755
|
1378 symbol_record *sym_rec = global_sym_tab->lookup (mf.name, 1); |
593
|
1379 sym_rec->unprotect (); |
|
1380 |
|
1381 Mapper_fcn mfcn; |
|
1382 |
1755
|
1383 mfcn.name = mf.name; |
|
1384 mfcn.can_return_complex_for_real_arg = mf.can_return_complex_for_real_arg; |
|
1385 mfcn.lower_limit = mf.lower_limit; |
|
1386 mfcn.upper_limit = mf.upper_limit; |
|
1387 mfcn.d_d_mapper = mf.d_d_mapper; |
|
1388 mfcn.d_c_mapper = mf.d_c_mapper; |
|
1389 mfcn.c_c_mapper = mf.c_c_mapper; |
|
1390 |
|
1391 tree_builtin *def = new tree_builtin (mfcn, mf.name); |
593
|
1392 |
|
1393 sym_rec->define (def); |
|
1394 |
1755
|
1395 sym_rec->document (mf.help_string); |
593
|
1396 sym_rec->make_eternal (); |
|
1397 sym_rec->protect (); |
|
1398 } |
|
1399 |
|
1400 void |
1755
|
1401 install_builtin_function (const builtin_function& f) |
593
|
1402 { |
1755
|
1403 symbol_record *sym_rec = global_sym_tab->lookup (f.name, 1); |
593
|
1404 sym_rec->unprotect (); |
|
1405 |
1755
|
1406 tree_builtin *def = new tree_builtin (f.fcn, f.name); |
593
|
1407 |
1755
|
1408 sym_rec->define (def, f.is_text_fcn); |
593
|
1409 |
1755
|
1410 sym_rec->document (f.help_string); |
593
|
1411 sym_rec->make_eternal (); |
|
1412 sym_rec->protect (); |
|
1413 } |
|
1414 |
|
1415 void |
1755
|
1416 install_builtin_variable (const builtin_variable& v) |
593
|
1417 { |
1755
|
1418 if (v.install_as_function) |
|
1419 install_builtin_variable_as_function (v.name, v.value, v.protect, |
|
1420 v.eternal, v.help_string); |
529
|
1421 else |
1755
|
1422 bind_builtin_variable (v.name, v.value, v.protect, v.eternal, |
|
1423 v.sv_function, v.help_string); |
529
|
1424 } |
|
1425 |
593
|
1426 void |
1755
|
1427 install_builtin_variable_as_function (const string& name, tree_constant *val, |
593
|
1428 int protect, int eternal, |
1755
|
1429 const string& help) |
529
|
1430 { |
593
|
1431 symbol_record *sym_rec = global_sym_tab->lookup (name, 1); |
|
1432 sym_rec->unprotect (); |
|
1433 |
1755
|
1434 string tmp_help = help.empty () ? sym_rec->help () : help; |
593
|
1435 |
|
1436 sym_rec->define_as_fcn (val); |
|
1437 |
|
1438 sym_rec->document (tmp_help); |
|
1439 |
|
1440 if (protect) |
|
1441 sym_rec->protect (); |
|
1442 |
|
1443 if (eternal) |
|
1444 sym_rec->make_eternal (); |
|
1445 } |
|
1446 |
|
1447 void |
1755
|
1448 alias_builtin (const string& alias, const string& name) |
593
|
1449 { |
|
1450 symbol_record *sr_name = global_sym_tab->lookup (name, 0, 0); |
1755
|
1451 |
593
|
1452 if (! sr_name) |
|
1453 panic ("can't alias to undefined name!"); |
|
1454 |
|
1455 symbol_record *sr_alias = global_sym_tab->lookup (alias, 1, 0); |
|
1456 |
|
1457 if (sr_alias) |
|
1458 sr_alias->alias (sr_name); |
|
1459 else |
1755
|
1460 panic ("can't find symbol record for builtin function `%s'", |
|
1461 alias.c_str ()); |
529
|
1462 } |
|
1463 |
593
|
1464 // Defining variables. |
|
1465 |
1161
|
1466 #if 0 |
593
|
1467 void |
|
1468 bind_nargin_and_nargout (symbol_table *sym_tab, int nargin, int nargout) |
529
|
1469 { |
593
|
1470 tree_constant *tmp; |
|
1471 symbol_record *sr; |
|
1472 |
|
1473 sr = sym_tab->lookup ("nargin", 1, 0); |
|
1474 sr->unprotect (); |
712
|
1475 tmp = new tree_constant (nargin); |
593
|
1476 sr->define (tmp); |
|
1477 |
|
1478 sr = sym_tab->lookup ("nargout", 1, 0); |
|
1479 sr->unprotect (); |
|
1480 tmp = new tree_constant (nargout); |
|
1481 sr->define (tmp); |
|
1482 } |
1161
|
1483 #endif |
593
|
1484 |
1162
|
1485 void |
|
1486 bind_ans (const tree_constant& val, int print) |
|
1487 { |
|
1488 static symbol_record *sr = global_sym_tab->lookup ("ans", 1, 0); |
|
1489 |
1281
|
1490 tree_identifier *ans_id = new tree_identifier (sr); |
1162
|
1491 tree_constant *tmp = new tree_constant (val); |
|
1492 |
1281
|
1493 // XXX FIXME XXX -- making ans_id static, passing its address to |
|
1494 // tree_simple_assignment_expression along with a flag to not delete |
|
1495 // it seems to create a memory leak. Hmm. |
|
1496 |
1827
|
1497 tree_simple_assignment_expression tmp_ass (ans_id, tmp, false, true); |
1162
|
1498 |
|
1499 tmp_ass.eval (print); |
|
1500 } |
|
1501 |
1489
|
1502 void |
|
1503 bind_global_error_variable (void) |
|
1504 { |
|
1505 *error_message_buffer << ends; |
|
1506 |
|
1507 char *error_text = error_message_buffer->str (); |
|
1508 |
|
1509 bind_builtin_variable ("__error_text__", error_text, 1); |
|
1510 |
|
1511 delete [] error_text; |
|
1512 |
|
1513 delete error_message_buffer; |
|
1514 |
|
1515 error_message_buffer = 0; |
|
1516 } |
|
1517 |
|
1518 void |
|
1519 clear_global_error_variable (void *) |
|
1520 { |
|
1521 delete error_message_buffer; |
|
1522 error_message_buffer = 0; |
|
1523 |
|
1524 bind_builtin_variable ("__error_text__", "", 1); |
|
1525 } |
|
1526 |
593
|
1527 // Give a global variable a definition. This will insert the symbol |
|
1528 // in the global table if necessary. |
|
1529 |
|
1530 // How is this different than install_builtin_variable? Are both |
|
1531 // functions needed? |
|
1532 |
|
1533 void |
1755
|
1534 bind_builtin_variable (const string& varname, tree_constant *val, |
593
|
1535 int protect, int eternal, sv_Function sv_fcn, |
1755
|
1536 const string& help) |
593
|
1537 { |
|
1538 symbol_record *sr = global_sym_tab->lookup (varname, 1, 0); |
|
1539 |
1271
|
1540 // It is a programming error for a builtin symbol to be missing. |
|
1541 // Besides, we just inserted it, so it must be there. |
593
|
1542 |
|
1543 assert (sr); |
|
1544 |
|
1545 sr->unprotect (); |
|
1546 |
1271
|
1547 // Must do this before define, since define will call the special |
|
1548 // variable function only if it knows about it, and it needs to, so |
|
1549 // that user prefs can be properly initialized. |
593
|
1550 |
|
1551 if (sv_fcn) |
|
1552 sr->set_sv_function (sv_fcn); |
|
1553 |
|
1554 sr->define_builtin_var (val); |
|
1555 |
|
1556 if (protect) |
|
1557 sr->protect (); |
|
1558 |
|
1559 if (eternal) |
|
1560 sr->make_eternal (); |
|
1561 |
1755
|
1562 sr->document (help); |
529
|
1563 } |
|
1564 |
593
|
1565 void |
1755
|
1566 bind_builtin_variable (const string& varname, const tree_constant& val, |
1406
|
1567 int protect, int eternal, sv_Function sv_fcn, |
1755
|
1568 const string& help) |
1406
|
1569 { |
|
1570 tree_constant *tc = new tree_constant (val); |
|
1571 bind_builtin_variable (varname, tc, protect, eternal, sv_fcn, help); |
|
1572 } |
|
1573 |
|
1574 void |
593
|
1575 install_builtin_variables (void) |
529
|
1576 { |
1271
|
1577 // XXX FIXME XX -- these should probably be moved to where they |
|
1578 // logically belong instead of being all grouped here. |
593
|
1579 |
1418
|
1580 DEFVAR ("EDITOR", SBV_EDITOR, editor, 0, sv_editor, |
593
|
1581 "name of the editor to be invoked by the edit_history command"); |
|
1582 |
1613
|
1583 DEFVAR ("EXEC_PATH", SBV_EXEC_PATH, exec_path, 0, sv_exec_path, |
|
1584 "colon separated list of directories to search for programs to run"); |
|
1585 |
1418
|
1586 DEFCONST ("I", SBV_I, Complex (0.0, 1.0), 0, 0, |
593
|
1587 "sqrt (-1)"); |
|
1588 |
1418
|
1589 DEFCONST ("Inf", SBV_Inf, octave_Inf, 0, 0, |
593
|
1590 "infinity"); |
|
1591 |
1418
|
1592 DEFVAR ("INFO_FILE", SBV_INFO_FILE, info_file, 0, sv_info_file, |
593
|
1593 "name of the Octave info file"); |
|
1594 |
1613
|
1595 DEFVAR ("INFO_PROGRAM", SBV_INFO_PROGRAM, info_prog, 0, sv_info_prog, |
|
1596 "name of the Octave info reader"); |
|
1597 |
1418
|
1598 DEFCONST ("J", SBV_J, Complex (0.0, 1.0), 0, 0, |
593
|
1599 "sqrt (-1)"); |
529
|
1600 |
1418
|
1601 DEFCONST ("NaN", SBV_NaN, octave_NaN, 0, 0, |
593
|
1602 "not a number"); |
|
1603 |
1418
|
1604 DEFVAR ("LOADPATH", SBV_LOADPATH, load_path, 0, sv_loadpath, |
593
|
1605 "colon separated list of directories to search for scripts"); |
|
1606 |
1418
|
1607 DEFVAR ("IMAGEPATH", SBV_IMAGEPATH, OCTAVE_IMAGEPATH, 0, |
686
|
1608 sv_imagepath, |
|
1609 "colon separated list of directories to search for image files"); |
|
1610 |
1418
|
1611 DEFCONST ("OCTAVE_VERSION", SBV_version, OCTAVE_VERSION, 0, 0, |
664
|
1612 "Octave version"); |
|
1613 |
1418
|
1614 DEFVAR ("PAGER", SBV_PAGER, default_pager (), 0, sv_pager_binary, |
593
|
1615 "path to pager binary"); |
|
1616 |
1418
|
1617 DEFVAR ("PS1", SBV_PS1, "\\s:\\#> ", 0, sv_ps1, |
593
|
1618 "primary prompt string"); |
|
1619 |
1418
|
1620 DEFVAR ("PS2", SBV_PS2, "> ", 0, sv_ps2, |
593
|
1621 "secondary prompt string"); |
|
1622 |
1418
|
1623 DEFVAR ("PS4", SBV_PS4, "+ ", 0, sv_ps4, |
1044
|
1624 "string printed before echoed input (enabled by --echo-input)"); |
|
1625 |
1418
|
1626 DEFCONST ("PWD", SBV_PWD, |
|
1627 get_working_directory ("initialize_globals"), 0, sv_pwd, |
593
|
1628 "current working directory"); |
|
1629 |
1418
|
1630 DEFCONST ("SEEK_SET", SBV_SEEK_SET, 0.0, 0, 0, |
593
|
1631 "used with fseek to position file relative to the beginning"); |
529
|
1632 |
1418
|
1633 DEFCONST ("SEEK_CUR", SBV_SEEK_CUR, 1.0, 0, 0, |
593
|
1634 "used with fseek to position file relative to the current position"); |
|
1635 |
1418
|
1636 DEFCONST ("SEEK_END", SBV_SEEK_END, 2.0, 0, 0, |
593
|
1637 "used with fseek to position file relative to the end"); |
|
1638 |
1418
|
1639 DEFVAR ("ans", SBV_ans, , 0, 0, |
593
|
1640 ""); |
|
1641 |
1418
|
1642 DEFCONST ("argv", SBV_argv, , 0, 0, |
1343
|
1643 "the command line arguments this program was invoked with"); |
|
1644 |
1505
|
1645 DEFVAR ("automatic_replot", SBV_automatic_replot, 0.0, |
1418
|
1646 0, automatic_replot, |
661
|
1647 "if true, auto-insert a replot command when a plot changes"); |
|
1648 |
1505
|
1649 DEFVAR ("beep_on_error", SBV_beep_on_error, 0.0, 0, |
1423
|
1650 beep_on_error, |
|
1651 "if true, beep before printing error messages"); |
|
1652 |
1429
|
1653 DEFVAR ("completion_append_char", SBV_completion_append_char, " ", |
|
1654 0, sv_completion_append_char, |
|
1655 "the string to append after successful command-line completion\n\ |
|
1656 attempts"); |
|
1657 |
1489
|
1658 DEFCONST ("error_text", SBV_current_error_text, "", 0, 0, |
|
1659 "the text of error messages that would have been printed in the |
|
1660 body of the most recent unwind_protect statement or the TRY part of\n\ |
|
1661 the most recent eval() command. Outside of unwind_protect and\n\ |
|
1662 eval(), or if no error has ocurred within them, the value of\n\ |
|
1663 __error_text__ is guaranteed to be the empty string."); |
|
1664 |
1093
|
1665 DEFVAR ("default_return_value", SBV_default_return_value, Matrix (), |
1418
|
1666 0, 0, |
1093
|
1667 "the default for value for unitialized variables returned from\n\ |
|
1668 functions. Only used if the variable initialize_return_values is\n\ |
|
1669 set to \"true\"."); |
593
|
1670 |
605
|
1671 DEFVAR ("default_save_format", SBV_default_save_format, "ascii", |
1418
|
1672 0, sv_default_save_format, |
997
|
1673 "default format for files created with save, may be one of\n\ |
1418
|
1674 \"binary\", \"text\", or \"mat-binary\""); |
605
|
1675 |
1407
|
1676 DEFVAR ("define_all_return_values", SBV_define_all_return_values, |
1505
|
1677 0.0, 0, define_all_return_values, |
1407
|
1678 "control whether values returned from functions should have a\n\ |
|
1679 value even if one has not been explicitly assigned. See also\n\ |
1418
|
1680 default_return_value"); |
1407
|
1681 |
1505
|
1682 DEFVAR ("do_fortran_indexing", SBV_do_fortran_indexing, 0.0, 0, |
1418
|
1683 do_fortran_indexing, |
593
|
1684 "allow single indices for matrices"); |
|
1685 |
1588
|
1686 DEFVAR ("echo_executing_commands", SBV_echo_executing_commands, 0.0, 0, |
|
1687 echo_executing_commands, |
|
1688 "echo commands as they are executed"); |
|
1689 |
1474
|
1690 DEFCONST ("e", SBV_e, exp (1.0), 0, 0, |
|
1691 "exp (1)"); |
|
1692 |
1418
|
1693 DEFVAR ("empty_list_elements_ok", SBV_empty_list_elements_ok, |
|
1694 "warn", 0, empty_list_elements_ok, |
593
|
1695 "ignore the empty element in expressions like `a = [[], 1]'"); |
529
|
1696 |
1418
|
1697 DEFCONST ("eps", SBV_eps, DBL_EPSILON, 0, 0, |
593
|
1698 "machine precision"); |
|
1699 |
1418
|
1700 DEFVAR ("gnuplot_binary", SBV_gnuplot_binary, "gnuplot", 0, |
593
|
1701 sv_gnuplot_binary, |
|
1702 "path to gnuplot binary"); |
|
1703 |
1502
|
1704 #ifdef GNUPLOT_HAS_MULTIPLOT |
1506
|
1705 double with_multiplot = 1.0; |
1502
|
1706 #else |
1506
|
1707 double with_multiplot = 0.0; |
1502
|
1708 #endif |
|
1709 |
|
1710 DEFVAR ("gnuplot_has_multiplot", SBV_gnuplot_has_multiplot, |
1504
|
1711 with_multiplot, 0, gnuplot_has_multiplot, |
1502
|
1712 "true if gnuplot supports multiplot, false otherwise"); |
|
1713 |
1646
|
1714 DEFVAR ("history_file", SBV_history_file, |
|
1715 default_history_file (), 0, sv_history_file, |
|
1716 "name of command history file"); |
|
1717 |
|
1718 DEFVAR ("history_size", SBV_history_size, |
|
1719 default_history_size (), 0, history_size, |
|
1720 "number of commands to save in the history list"); |
|
1721 |
1418
|
1722 DEFCONST ("i", SBV_i, Complex (0.0, 1.0), 1, 0, |
593
|
1723 "sqrt (-1)"); |
529
|
1724 |
1418
|
1725 DEFVAR ("ignore_function_time_stamp", |
|
1726 SBV_ignore_function_time_stamp, "system", 0, |
593
|
1727 ignore_function_time_stamp, |
|
1728 "don't check to see if function files have changed since they were\n\ |
|
1729 last compiled. Possible values are \"system\" and \"all\""); |
|
1730 |
1418
|
1731 DEFVAR ("implicit_str_to_num_ok", SBV_implicit_str_to_num_ok, |
1505
|
1732 0.0, 0, implicit_str_to_num_ok, |
593
|
1733 "allow implicit string to number conversion"); |
|
1734 |
1418
|
1735 DEFCONST ("inf", SBV_inf, octave_Inf, 0, 0, |
593
|
1736 "infinity"); |
|
1737 |
1418
|
1738 DEFCONST ("j", SBV_j, Complex (0.0, 1.0), 1, 0, |
593
|
1739 "sqrt (-1)"); |
529
|
1740 |
1418
|
1741 DEFCONST ("nan", SBV_nan, octave_NaN, 0, 0, |
593
|
1742 "not a number"); |
|
1743 |
|
1744 DEFVAR ("ok_to_lose_imaginary_part", SBV_ok_to_lose_imaginary_part, |
1418
|
1745 "warn", 0, ok_to_lose_imaginary_part, |
593
|
1746 "silently convert from complex to real by dropping imaginary part"); |
|
1747 |
1418
|
1748 DEFVAR ("output_max_field_width", SBV_output_max_field_width, 10.0, |
|
1749 0, set_output_max_field_width, |
593
|
1750 "maximum width of an output field for numeric output"); |
|
1751 |
1418
|
1752 DEFVAR ("output_precision", SBV_output_precision, 5.0, 0, |
593
|
1753 set_output_precision, |
|
1754 "number of significant figures to display for numeric output"); |
|
1755 |
1505
|
1756 DEFVAR ("page_screen_output", SBV_page_screen_output, 1.0, 0, |
593
|
1757 page_screen_output, |
|
1758 "if possible, send output intended for the screen through the pager"); |
529
|
1759 |
1418
|
1760 DEFCONST ("pi", SBV_pi, 4.0 * atan (1.0), 0, 0, |
593
|
1761 "ratio of the circumference of a circle to its diameter"); |
|
1762 |
1505
|
1763 DEFVAR ("prefer_column_vectors", SBV_prefer_column_vectors, 1.0, |
1418
|
1764 0, prefer_column_vectors, |
593
|
1765 "prefer column/row vectors"); |
|
1766 |
|
1767 DEFVAR ("prefer_zero_one_indexing", SBV_prefer_zero_one_indexing, |
1505
|
1768 0.0, 0, prefer_zero_one_indexing, |
593
|
1769 "when there is a conflict, prefer zero-one style indexing"); |
|
1770 |
1505
|
1771 DEFVAR ("print_answer_id_name", SBV_print_answer_id_name, 1.0, 0, |
1418
|
1772 print_answer_id_name, |
593
|
1773 "set output style to print `var_name = ...'"); |
|
1774 |
1418
|
1775 DEFVAR ("print_empty_dimensions", SBV_print_empty_dimensions, |
1505
|
1776 1.0, 0, print_empty_dimensions, |
593
|
1777 "also print dimensions of empty matrices"); |
|
1778 |
1418
|
1779 DEFCONST ("program_invocation_name", SBV_program_invocation_name, |
|
1780 raw_prog_name, 0, 0, |
1343
|
1781 "the full name of the current program or script, including the\n\ |
|
1782 directory specification"); |
|
1783 |
1418
|
1784 DEFCONST ("program_name", SBV_program_name, prog_name, 0, 0, |
1343
|
1785 "the name of the current program or script"); |
|
1786 |
593
|
1787 DEFVAR ("propagate_empty_matrices", SBV_propagate_empty_matrices, |
1505
|
1788 1.0, 0, propagate_empty_matrices, |
593
|
1789 "operations on empty matrices return an empty matrix, not an error"); |
529
|
1790 |
1045
|
1791 #if 0 |
1505
|
1792 DEFVAR ("read_only_constants", SBV_read_only_constants, 1.0, 0, |
1418
|
1793 read_only_constants, |
1045
|
1794 "allow built-in constants to be modified"); |
|
1795 #endif |
|
1796 |
1418
|
1797 DEFCONST ("realmax", SBV_realmax, DBL_MAX, 0, 0, |
868
|
1798 "realmax (): return largest representable floating point number"); |
|
1799 |
1418
|
1800 DEFCONST ("realmin", SBV_realmin, DBL_MIN, 0, 0, |
868
|
1801 "realmin (): return smallest representable floating point number"); |
|
1802 |
1505
|
1803 DEFVAR ("resize_on_range_error", SBV_resize_on_range_error, 1.0, |
1418
|
1804 0, resize_on_range_error, |
593
|
1805 "enlarge matrices on assignment"); |
|
1806 |
1418
|
1807 DEFVAR ("return_last_computed_value", |
1505
|
1808 SBV_return_last_computed_value, 0.0, 0, |
593
|
1809 return_last_computed_value, |
|
1810 "if a function does not return any values explicitly, return the\n\ |
|
1811 last computed value"); |
|
1812 |
1643
|
1813 DEFVAR ("saving_history", SBV_saving_history, 1.0, 0, saving_history, |
|
1814 "save command history"); |
593
|
1815 |
1505
|
1816 DEFVAR ("silent_functions", SBV_silent_functions, 0.0, 0, |
593
|
1817 silent_functions, |
|
1818 "suppress printing results in called functions"); |
|
1819 |
1505
|
1820 DEFVAR ("split_long_rows", SBV_split_long_rows, 1.0, 0, |
593
|
1821 split_long_rows, |
|
1822 "split long matrix rows instead of wrapping"); |
529
|
1823 |
1198
|
1824 DEFVAR ("struct_levels_to_print", SBV_struct_levels_to_print, 2.0, |
1418
|
1825 0, struct_levels_to_print, |
1198
|
1826 "number of levels of structure elements to print"); |
|
1827 |
1139
|
1828 #ifdef USE_GNU_INFO |
1116
|
1829 DEFVAR ("suppress_verbose_help_message", |
1505
|
1830 SBV_suppress_verbose_help_message, 0.0, 0, |
1116
|
1831 suppress_verbose_help_message, |
|
1832 "suppress printing of message pointing to additional help in the\n\ |
|
1833 help and usage functions"); |
1139
|
1834 #endif |
1116
|
1835 |
1418
|
1836 DEFCONST ("stdin", SBV_stdin, 0.0, 0, 0, |
593
|
1837 "file number of the standard input stream"); |
|
1838 |
1418
|
1839 DEFCONST ("stdout", SBV_stdout, 1.0, 0, 0, |
593
|
1840 "file number of the standard output stream"); |
|
1841 |
1418
|
1842 DEFCONST ("stderr", SBV_stderr, 2.0, 0, 0, |
593
|
1843 "file number of the standard error stream"); |
|
1844 |
1505
|
1845 DEFVAR ("treat_neg_dim_as_zero", SBV_treat_neg_dim_as_zero, 0.0, 0, |
|
1846 treat_neg_dim_as_zero, |
593
|
1847 "convert negative dimensions to zero"); |
|
1848 |
1418
|
1849 DEFVAR ("warn_assign_as_truth_value", |
1505
|
1850 SBV_warn_assign_as_truth_value, 1.0, 0, |
593
|
1851 warn_assign_as_truth_value, |
|
1852 "produce warning for assignments used as truth values"); |
|
1853 |
|
1854 DEFVAR ("warn_comma_in_global_decl", SBV_warn_comma_in_global_decl, |
1505
|
1855 1.0, 0, warn_comma_in_global_decl, |
593
|
1856 "produce warning for commas in global declarations"); |
|
1857 |
1505
|
1858 DEFVAR ("warn_divide_by_zero", SBV_warn_divide_by_zero, 1.0, 0, |
1418
|
1859 warn_divide_by_zero, |
593
|
1860 "on IEEE machines, allow divide by zero errors to be suppressed"); |
1037
|
1861 |
|
1862 DEFVAR ("warn_function_name_clash", SBV_warn_function_name_clash, |
1505
|
1863 1.0, 0, warn_function_name_clash, |
1037
|
1864 "produce warning if function name conflicts with file name"); |
1093
|
1865 |
1509
|
1866 DEFVAR ("warn_missing_semicolon", SBV_warn_missing_semicolon, |
|
1867 0.0, 0, warn_missing_semicolon, |
1511
|
1868 "produce a warning if a statement in a function file is not |
|
1869 terminated with a semicolon"); |
1509
|
1870 |
1418
|
1871 DEFVAR ("whitespace_in_literal_matrix", |
|
1872 SBV_whitespace_in_literal_matrix, "", 0, |
|
1873 whitespace_in_literal_matrix, |
1093
|
1874 "control auto-insertion of commas and semicolons in literal matrices"); |
529
|
1875 } |
|
1876 |
593
|
1877 // Deleting names from the symbol tables. |
|
1878 |
1488
|
1879 DEFUN_TEXT ("clear", Fclear, Sclear, 10, |
668
|
1880 "clear [-x] [name ...]\n\ |
|
1881 \n\ |
|
1882 Clear symbol(s) matching a list of globbing patterns.\n\ |
593
|
1883 \n\ |
668
|
1884 If no arguments are given, clear all user-defined variables and |
|
1885 functions.\n\ |
|
1886 \n\ |
|
1887 With -x, exclude the named variables") |
529
|
1888 { |
593
|
1889 Octave_object retval; |
|
1890 |
1755
|
1891 int argc = args.length () + 1; |
593
|
1892 |
1755
|
1893 string_vector argv = make_argv (args, "clear"); |
|
1894 |
|
1895 if (error_state) |
|
1896 return retval; |
668
|
1897 |
1271
|
1898 // Always clear the local table, but don't clear currently compiled |
|
1899 // functions unless we are at the top level. (Allowing that to |
|
1900 // happen inside functions would result in pretty odd behavior...) |
593
|
1901 |
|
1902 int clear_user_functions = (curr_sym_tab == top_level_sym_tab); |
|
1903 |
1755
|
1904 if (argc == 1) |
593
|
1905 { |
|
1906 curr_sym_tab->clear (); |
|
1907 global_sym_tab->clear (clear_user_functions); |
|
1908 } |
529
|
1909 else |
|
1910 { |
668
|
1911 int exclusive = 0; |
|
1912 |
1755
|
1913 int idx = 1; |
|
1914 |
|
1915 if (argc > 1) |
668
|
1916 { |
1755
|
1917 if (argv[idx] == "-x") |
|
1918 exclusive = 1; |
668
|
1919 } |
|
1920 |
|
1921 int lcount = 0; |
|
1922 int gcount = 0; |
|
1923 int fcount = 0; |
529
|
1924 |
1755
|
1925 string_vector lvars; |
|
1926 string_vector gvars; |
|
1927 string_vector fcns; |
668
|
1928 |
|
1929 if (argc > 0) |
593
|
1930 { |
893
|
1931 lvars = curr_sym_tab->list (lcount, 0, 0, 0, |
1418
|
1932 SYMTAB_VARIABLES, |
668
|
1933 SYMTAB_LOCAL_SCOPE); |
|
1934 |
893
|
1935 gvars = curr_sym_tab->list (gcount, 0, 0, 0, |
1418
|
1936 SYMTAB_VARIABLES, |
668
|
1937 SYMTAB_GLOBAL_SCOPE); |
|
1938 |
893
|
1939 fcns = global_sym_tab->list (fcount, 0, 0, 0, |
|
1940 symbol_def::USER_FUNCTION, |
864
|
1941 SYMTAB_ALL_SCOPES); |
668
|
1942 } |
|
1943 |
1755
|
1944 for (int k = idx + 1; k < argc; k++) |
668
|
1945 { |
1755
|
1946 string patstr = argv[k]; |
668
|
1947 |
1755
|
1948 if (! patstr.empty ()) |
593
|
1949 { |
1792
|
1950 glob_match pattern (patstr); |
1755
|
1951 |
593
|
1952 int i; |
|
1953 for (i = 0; i < lcount; i++) |
|
1954 { |
1755
|
1955 string nm = lvars[i]; |
1792
|
1956 int match = pattern.match (nm); |
668
|
1957 if ((exclusive && ! match) || (! exclusive && match)) |
|
1958 curr_sym_tab->clear (nm); |
593
|
1959 } |
529
|
1960 |
593
|
1961 int count; |
|
1962 for (i = 0; i < gcount; i++) |
|
1963 { |
1755
|
1964 string nm = gvars[i]; |
1792
|
1965 int match = pattern.match (nm); |
668
|
1966 if ((exclusive && ! match) || (! exclusive && match)) |
593
|
1967 { |
668
|
1968 count = curr_sym_tab->clear (nm); |
593
|
1969 if (count > 0) |
668
|
1970 global_sym_tab->clear (nm, clear_user_functions); |
593
|
1971 } |
|
1972 } |
529
|
1973 |
593
|
1974 for (i = 0; i < fcount; i++) |
|
1975 { |
1755
|
1976 string nm = fcns[i]; |
1792
|
1977 int match = pattern.match (nm); |
668
|
1978 if ((exclusive && ! match) || (! exclusive && match)) |
593
|
1979 { |
668
|
1980 count = curr_sym_tab->clear (nm); |
864
|
1981 global_sym_tab->clear (nm, clear_user_functions); |
593
|
1982 } |
|
1983 } |
|
1984 } |
|
1985 } |
|
1986 } |
|
1987 |
|
1988 return retval; |
529
|
1989 } |
|
1990 |
1
|
1991 /* |
|
1992 ;;; Local Variables: *** |
|
1993 ;;; mode: C++ *** |
|
1994 ;;; page-delimiter: "^/\\*" *** |
|
1995 ;;; End: *** |
|
1996 */ |