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