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 |
|
102 int |
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 |
|
111 int |
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 |
605
|
120 int |
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 |
593
|
765 static int |
581
|
766 load_fcn_from_file (symbol_record *sym_rec, int exec_script) |
|
767 { |
|
768 int script_file_executed = 0; |
|
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 |
|
802 int |
|
803 lookup (symbol_record *sym_rec, int exec_script) |
|
804 { |
1271
|
805 int script_executed = 0; |
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 |
1755
|
1229 for (int i = 1; i < argc; i++) |
529
|
1230 { |
1755
|
1231 if (argv[i] == "-all" || argv[i] == "-a") |
529
|
1232 { |
|
1233 show_builtins++; |
|
1234 show_functions++; |
1418
|
1235 show_variables++; |
529
|
1236 } |
1755
|
1237 else if (argv[i] == "-builtins" || argv[i] == "-b") |
529
|
1238 show_builtins++; |
1755
|
1239 else if (argv[i] == "-functions" || argv[i] == "-f") |
529
|
1240 show_functions++; |
1755
|
1241 else if (argv[i] == "-long" || argv[i] == "-l") |
605
|
1242 show_verbose++; |
1755
|
1243 else if (argv[i] == "-variables" || argv[i] == "-v") |
529
|
1244 show_variables++; |
1755
|
1245 else if (argv[i][0] == '-') |
|
1246 warning ("%s: unrecognized option `%s'", my_name.c_str (), |
|
1247 argv[i].c_str ()); |
529
|
1248 else |
867
|
1249 break; |
529
|
1250 } |
|
1251 |
1271
|
1252 // If the user specified -l and nothing else, show variables. If |
|
1253 // evaluating this at the top level, also show functions. |
529
|
1254 |
|
1255 if (show_verbose && ! (show_builtins || show_functions || show_variables)) |
|
1256 { |
|
1257 show_functions = (curr_sym_tab == top_level_sym_tab); |
|
1258 show_variables = 1; |
|
1259 } |
|
1260 |
|
1261 ostrstream output_buf; |
|
1262 int pad_after = 0; |
|
1263 |
|
1264 if (show_builtins) |
|
1265 { |
867
|
1266 pad_after += maybe_list ("*** built-in variables:", argv, argc, |
529
|
1267 output_buf, show_verbose, global_sym_tab, |
|
1268 symbol_def::BUILTIN_VARIABLE, |
|
1269 SYMTAB_ALL_SCOPES); |
|
1270 |
867
|
1271 pad_after += maybe_list ("*** built-in functions:", argv, argc, |
529
|
1272 output_buf, show_verbose, global_sym_tab, |
|
1273 symbol_def::BUILTIN_FUNCTION, |
|
1274 SYMTAB_ALL_SCOPES); |
|
1275 } |
|
1276 |
|
1277 if (show_functions) |
|
1278 { |
|
1279 pad_after += maybe_list ("*** currently compiled functions:", |
867
|
1280 argv, argc, output_buf, show_verbose, |
|
1281 global_sym_tab, symbol_def::USER_FUNCTION, |
529
|
1282 SYMTAB_ALL_SCOPES); |
|
1283 } |
|
1284 |
|
1285 if (show_variables) |
|
1286 { |
867
|
1287 pad_after += maybe_list ("*** local user variables:", argv, argc, |
529
|
1288 output_buf, show_verbose, curr_sym_tab, |
|
1289 symbol_def::USER_VARIABLE, |
1418
|
1290 SYMTAB_LOCAL_SCOPE); |
529
|
1291 |
|
1292 pad_after += maybe_list ("*** globally visible user variables:", |
867
|
1293 argv, argc, output_buf, show_verbose, |
|
1294 curr_sym_tab, symbol_def::USER_VARIABLE, |
529
|
1295 SYMTAB_GLOBAL_SCOPE); |
|
1296 } |
|
1297 |
|
1298 if (pad_after) |
|
1299 output_buf << "\n"; |
|
1300 |
|
1301 output_buf << ends; |
|
1302 maybe_page_output (output_buf); |
|
1303 |
581
|
1304 return retval; |
|
1305 } |
|
1306 |
1488
|
1307 DEFUN_TEXT ("who", Fwho, Swho, 10, |
581
|
1308 "who [-all] [-builtins] [-functions] [-long] [-variables]\n\ |
|
1309 \n\ |
|
1310 List currently defined symbol(s). Options may be shortened to one\n\ |
|
1311 character, but may not be combined.") |
|
1312 { |
|
1313 Octave_object retval; |
|
1314 |
1755
|
1315 int argc = args.length () + 1; |
|
1316 |
|
1317 string_vector argv = make_argv (args, "who"); |
|
1318 |
|
1319 if (error_state) |
|
1320 return retval; |
581
|
1321 |
1488
|
1322 retval = do_who (argc, argv); |
581
|
1323 |
529
|
1324 return retval; |
|
1325 } |
|
1326 |
1488
|
1327 DEFUN_TEXT ("whos", Fwhos, Swhos, 10, |
581
|
1328 "whos [-all] [-builtins] [-functions] [-long] [-variables]\n\ |
|
1329 \n\ |
|
1330 List currently defined symbol(s). Options may be shortened to one\n\ |
|
1331 character, but may not be combined.") |
|
1332 { |
|
1333 Octave_object retval; |
|
1334 |
712
|
1335 int nargin = args.length (); |
|
1336 |
742
|
1337 Octave_object tmp_args; |
|
1338 for (int i = nargin; i > 0; i--) |
|
1339 tmp_args(i) = args(i-1); |
|
1340 tmp_args(0) = "-long"; |
581
|
1341 |
742
|
1342 int argc = tmp_args.length () + 1; |
1755
|
1343 |
|
1344 string_vector argv = make_argv (tmp_args, "whos"); |
581
|
1345 |
|
1346 if (error_state) |
|
1347 return retval; |
|
1348 |
1488
|
1349 retval = do_who (argc, argv); |
581
|
1350 |
|
1351 return retval; |
|
1352 } |
|
1353 |
593
|
1354 // Install variables and functions in the symbol tables. |
529
|
1355 |
593
|
1356 void |
1755
|
1357 install_builtin_mapper (const builtin_mapper_function& mf) |
529
|
1358 { |
1755
|
1359 symbol_record *sym_rec = global_sym_tab->lookup (mf.name, 1); |
593
|
1360 sym_rec->unprotect (); |
|
1361 |
|
1362 Mapper_fcn mfcn; |
|
1363 |
1755
|
1364 mfcn.name = mf.name; |
|
1365 mfcn.can_return_complex_for_real_arg = mf.can_return_complex_for_real_arg; |
|
1366 mfcn.lower_limit = mf.lower_limit; |
|
1367 mfcn.upper_limit = mf.upper_limit; |
|
1368 mfcn.d_d_mapper = mf.d_d_mapper; |
|
1369 mfcn.d_c_mapper = mf.d_c_mapper; |
|
1370 mfcn.c_c_mapper = mf.c_c_mapper; |
|
1371 |
|
1372 tree_builtin *def = new tree_builtin (mfcn, mf.name); |
593
|
1373 |
|
1374 sym_rec->define (def); |
|
1375 |
1755
|
1376 sym_rec->document (mf.help_string); |
593
|
1377 sym_rec->make_eternal (); |
|
1378 sym_rec->protect (); |
|
1379 } |
|
1380 |
|
1381 void |
1755
|
1382 install_builtin_function (const builtin_function& f) |
593
|
1383 { |
1755
|
1384 symbol_record *sym_rec = global_sym_tab->lookup (f.name, 1); |
593
|
1385 sym_rec->unprotect (); |
|
1386 |
1755
|
1387 tree_builtin *def = new tree_builtin (f.fcn, f.name); |
593
|
1388 |
1755
|
1389 sym_rec->define (def, f.is_text_fcn); |
593
|
1390 |
1755
|
1391 sym_rec->document (f.help_string); |
593
|
1392 sym_rec->make_eternal (); |
|
1393 sym_rec->protect (); |
|
1394 } |
|
1395 |
|
1396 void |
1755
|
1397 install_builtin_variable (const builtin_variable& v) |
593
|
1398 { |
1755
|
1399 if (v.install_as_function) |
|
1400 install_builtin_variable_as_function (v.name, v.value, v.protect, |
|
1401 v.eternal, v.help_string); |
529
|
1402 else |
1755
|
1403 bind_builtin_variable (v.name, v.value, v.protect, v.eternal, |
|
1404 v.sv_function, v.help_string); |
529
|
1405 } |
|
1406 |
593
|
1407 void |
1755
|
1408 install_builtin_variable_as_function (const string& name, tree_constant *val, |
593
|
1409 int protect, int eternal, |
1755
|
1410 const string& help) |
529
|
1411 { |
593
|
1412 symbol_record *sym_rec = global_sym_tab->lookup (name, 1); |
|
1413 sym_rec->unprotect (); |
|
1414 |
1755
|
1415 string tmp_help = help.empty () ? sym_rec->help () : help; |
593
|
1416 |
|
1417 sym_rec->define_as_fcn (val); |
|
1418 |
|
1419 sym_rec->document (tmp_help); |
|
1420 |
|
1421 if (protect) |
|
1422 sym_rec->protect (); |
|
1423 |
|
1424 if (eternal) |
|
1425 sym_rec->make_eternal (); |
|
1426 } |
|
1427 |
|
1428 void |
1755
|
1429 alias_builtin (const string& alias, const string& name) |
593
|
1430 { |
|
1431 symbol_record *sr_name = global_sym_tab->lookup (name, 0, 0); |
1755
|
1432 |
593
|
1433 if (! sr_name) |
|
1434 panic ("can't alias to undefined name!"); |
|
1435 |
|
1436 symbol_record *sr_alias = global_sym_tab->lookup (alias, 1, 0); |
|
1437 |
|
1438 if (sr_alias) |
|
1439 sr_alias->alias (sr_name); |
|
1440 else |
1755
|
1441 panic ("can't find symbol record for builtin function `%s'", |
|
1442 alias.c_str ()); |
529
|
1443 } |
|
1444 |
593
|
1445 // Defining variables. |
|
1446 |
1161
|
1447 #if 0 |
593
|
1448 void |
|
1449 bind_nargin_and_nargout (symbol_table *sym_tab, int nargin, int nargout) |
529
|
1450 { |
593
|
1451 tree_constant *tmp; |
|
1452 symbol_record *sr; |
|
1453 |
|
1454 sr = sym_tab->lookup ("nargin", 1, 0); |
|
1455 sr->unprotect (); |
712
|
1456 tmp = new tree_constant (nargin); |
593
|
1457 sr->define (tmp); |
|
1458 |
|
1459 sr = sym_tab->lookup ("nargout", 1, 0); |
|
1460 sr->unprotect (); |
|
1461 tmp = new tree_constant (nargout); |
|
1462 sr->define (tmp); |
|
1463 } |
1161
|
1464 #endif |
593
|
1465 |
1162
|
1466 void |
|
1467 bind_ans (const tree_constant& val, int print) |
|
1468 { |
|
1469 static symbol_record *sr = global_sym_tab->lookup ("ans", 1, 0); |
|
1470 |
1281
|
1471 tree_identifier *ans_id = new tree_identifier (sr); |
1162
|
1472 tree_constant *tmp = new tree_constant (val); |
|
1473 |
1281
|
1474 // XXX FIXME XXX -- making ans_id static, passing its address to |
|
1475 // tree_simple_assignment_expression along with a flag to not delete |
|
1476 // it seems to create a memory leak. Hmm. |
|
1477 |
|
1478 tree_simple_assignment_expression tmp_ass (ans_id, tmp, 0, 1); |
1162
|
1479 |
|
1480 tmp_ass.eval (print); |
|
1481 } |
|
1482 |
1489
|
1483 void |
|
1484 bind_global_error_variable (void) |
|
1485 { |
|
1486 *error_message_buffer << ends; |
|
1487 |
|
1488 char *error_text = error_message_buffer->str (); |
|
1489 |
|
1490 bind_builtin_variable ("__error_text__", error_text, 1); |
|
1491 |
|
1492 delete [] error_text; |
|
1493 |
|
1494 delete error_message_buffer; |
|
1495 |
|
1496 error_message_buffer = 0; |
|
1497 } |
|
1498 |
|
1499 void |
|
1500 clear_global_error_variable (void *) |
|
1501 { |
|
1502 delete error_message_buffer; |
|
1503 error_message_buffer = 0; |
|
1504 |
|
1505 bind_builtin_variable ("__error_text__", "", 1); |
|
1506 } |
|
1507 |
593
|
1508 // Give a global variable a definition. This will insert the symbol |
|
1509 // in the global table if necessary. |
|
1510 |
|
1511 // How is this different than install_builtin_variable? Are both |
|
1512 // functions needed? |
|
1513 |
|
1514 void |
1755
|
1515 bind_builtin_variable (const string& varname, tree_constant *val, |
593
|
1516 int protect, int eternal, sv_Function sv_fcn, |
1755
|
1517 const string& help) |
593
|
1518 { |
|
1519 symbol_record *sr = global_sym_tab->lookup (varname, 1, 0); |
|
1520 |
1271
|
1521 // It is a programming error for a builtin symbol to be missing. |
|
1522 // Besides, we just inserted it, so it must be there. |
593
|
1523 |
|
1524 assert (sr); |
|
1525 |
|
1526 sr->unprotect (); |
|
1527 |
1271
|
1528 // Must do this before define, since define will call the special |
|
1529 // variable function only if it knows about it, and it needs to, so |
|
1530 // that user prefs can be properly initialized. |
593
|
1531 |
|
1532 if (sv_fcn) |
|
1533 sr->set_sv_function (sv_fcn); |
|
1534 |
|
1535 sr->define_builtin_var (val); |
|
1536 |
|
1537 if (protect) |
|
1538 sr->protect (); |
|
1539 |
|
1540 if (eternal) |
|
1541 sr->make_eternal (); |
|
1542 |
1755
|
1543 sr->document (help); |
529
|
1544 } |
|
1545 |
593
|
1546 void |
1755
|
1547 bind_builtin_variable (const string& varname, const tree_constant& val, |
1406
|
1548 int protect, int eternal, sv_Function sv_fcn, |
1755
|
1549 const string& help) |
1406
|
1550 { |
|
1551 tree_constant *tc = new tree_constant (val); |
|
1552 bind_builtin_variable (varname, tc, protect, eternal, sv_fcn, help); |
|
1553 } |
|
1554 |
|
1555 void |
593
|
1556 install_builtin_variables (void) |
529
|
1557 { |
1271
|
1558 // XXX FIXME XX -- these should probably be moved to where they |
|
1559 // logically belong instead of being all grouped here. |
593
|
1560 |
1418
|
1561 DEFVAR ("EDITOR", SBV_EDITOR, editor, 0, sv_editor, |
593
|
1562 "name of the editor to be invoked by the edit_history command"); |
|
1563 |
1613
|
1564 DEFVAR ("EXEC_PATH", SBV_EXEC_PATH, exec_path, 0, sv_exec_path, |
|
1565 "colon separated list of directories to search for programs to run"); |
|
1566 |
1418
|
1567 DEFCONST ("I", SBV_I, Complex (0.0, 1.0), 0, 0, |
593
|
1568 "sqrt (-1)"); |
|
1569 |
1418
|
1570 DEFCONST ("Inf", SBV_Inf, octave_Inf, 0, 0, |
593
|
1571 "infinity"); |
|
1572 |
1418
|
1573 DEFVAR ("INFO_FILE", SBV_INFO_FILE, info_file, 0, sv_info_file, |
593
|
1574 "name of the Octave info file"); |
|
1575 |
1613
|
1576 DEFVAR ("INFO_PROGRAM", SBV_INFO_PROGRAM, info_prog, 0, sv_info_prog, |
|
1577 "name of the Octave info reader"); |
|
1578 |
1418
|
1579 DEFCONST ("J", SBV_J, Complex (0.0, 1.0), 0, 0, |
593
|
1580 "sqrt (-1)"); |
529
|
1581 |
1418
|
1582 DEFCONST ("NaN", SBV_NaN, octave_NaN, 0, 0, |
593
|
1583 "not a number"); |
|
1584 |
1418
|
1585 DEFVAR ("LOADPATH", SBV_LOADPATH, load_path, 0, sv_loadpath, |
593
|
1586 "colon separated list of directories to search for scripts"); |
|
1587 |
1418
|
1588 DEFVAR ("IMAGEPATH", SBV_IMAGEPATH, OCTAVE_IMAGEPATH, 0, |
686
|
1589 sv_imagepath, |
|
1590 "colon separated list of directories to search for image files"); |
|
1591 |
1418
|
1592 DEFCONST ("OCTAVE_VERSION", SBV_version, OCTAVE_VERSION, 0, 0, |
664
|
1593 "Octave version"); |
|
1594 |
1418
|
1595 DEFVAR ("PAGER", SBV_PAGER, default_pager (), 0, sv_pager_binary, |
593
|
1596 "path to pager binary"); |
|
1597 |
1418
|
1598 DEFVAR ("PS1", SBV_PS1, "\\s:\\#> ", 0, sv_ps1, |
593
|
1599 "primary prompt string"); |
|
1600 |
1418
|
1601 DEFVAR ("PS2", SBV_PS2, "> ", 0, sv_ps2, |
593
|
1602 "secondary prompt string"); |
|
1603 |
1418
|
1604 DEFVAR ("PS4", SBV_PS4, "+ ", 0, sv_ps4, |
1044
|
1605 "string printed before echoed input (enabled by --echo-input)"); |
|
1606 |
1418
|
1607 DEFCONST ("PWD", SBV_PWD, |
|
1608 get_working_directory ("initialize_globals"), 0, sv_pwd, |
593
|
1609 "current working directory"); |
|
1610 |
1418
|
1611 DEFCONST ("SEEK_SET", SBV_SEEK_SET, 0.0, 0, 0, |
593
|
1612 "used with fseek to position file relative to the beginning"); |
529
|
1613 |
1418
|
1614 DEFCONST ("SEEK_CUR", SBV_SEEK_CUR, 1.0, 0, 0, |
593
|
1615 "used with fseek to position file relative to the current position"); |
|
1616 |
1418
|
1617 DEFCONST ("SEEK_END", SBV_SEEK_END, 2.0, 0, 0, |
593
|
1618 "used with fseek to position file relative to the end"); |
|
1619 |
1418
|
1620 DEFVAR ("ans", SBV_ans, , 0, 0, |
593
|
1621 ""); |
|
1622 |
1418
|
1623 DEFCONST ("argv", SBV_argv, , 0, 0, |
1343
|
1624 "the command line arguments this program was invoked with"); |
|
1625 |
1505
|
1626 DEFVAR ("automatic_replot", SBV_automatic_replot, 0.0, |
1418
|
1627 0, automatic_replot, |
661
|
1628 "if true, auto-insert a replot command when a plot changes"); |
|
1629 |
1505
|
1630 DEFVAR ("beep_on_error", SBV_beep_on_error, 0.0, 0, |
1423
|
1631 beep_on_error, |
|
1632 "if true, beep before printing error messages"); |
|
1633 |
1429
|
1634 DEFVAR ("completion_append_char", SBV_completion_append_char, " ", |
|
1635 0, sv_completion_append_char, |
|
1636 "the string to append after successful command-line completion\n\ |
|
1637 attempts"); |
|
1638 |
1489
|
1639 DEFCONST ("error_text", SBV_current_error_text, "", 0, 0, |
|
1640 "the text of error messages that would have been printed in the |
|
1641 body of the most recent unwind_protect statement or the TRY part of\n\ |
|
1642 the most recent eval() command. Outside of unwind_protect and\n\ |
|
1643 eval(), or if no error has ocurred within them, the value of\n\ |
|
1644 __error_text__ is guaranteed to be the empty string."); |
|
1645 |
1093
|
1646 DEFVAR ("default_return_value", SBV_default_return_value, Matrix (), |
1418
|
1647 0, 0, |
1093
|
1648 "the default for value for unitialized variables returned from\n\ |
|
1649 functions. Only used if the variable initialize_return_values is\n\ |
|
1650 set to \"true\"."); |
593
|
1651 |
605
|
1652 DEFVAR ("default_save_format", SBV_default_save_format, "ascii", |
1418
|
1653 0, sv_default_save_format, |
997
|
1654 "default format for files created with save, may be one of\n\ |
1418
|
1655 \"binary\", \"text\", or \"mat-binary\""); |
605
|
1656 |
1407
|
1657 DEFVAR ("define_all_return_values", SBV_define_all_return_values, |
1505
|
1658 0.0, 0, define_all_return_values, |
1407
|
1659 "control whether values returned from functions should have a\n\ |
|
1660 value even if one has not been explicitly assigned. See also\n\ |
1418
|
1661 default_return_value"); |
1407
|
1662 |
1505
|
1663 DEFVAR ("do_fortran_indexing", SBV_do_fortran_indexing, 0.0, 0, |
1418
|
1664 do_fortran_indexing, |
593
|
1665 "allow single indices for matrices"); |
|
1666 |
1588
|
1667 DEFVAR ("echo_executing_commands", SBV_echo_executing_commands, 0.0, 0, |
|
1668 echo_executing_commands, |
|
1669 "echo commands as they are executed"); |
|
1670 |
1474
|
1671 DEFCONST ("e", SBV_e, exp (1.0), 0, 0, |
|
1672 "exp (1)"); |
|
1673 |
1418
|
1674 DEFVAR ("empty_list_elements_ok", SBV_empty_list_elements_ok, |
|
1675 "warn", 0, empty_list_elements_ok, |
593
|
1676 "ignore the empty element in expressions like `a = [[], 1]'"); |
529
|
1677 |
1418
|
1678 DEFCONST ("eps", SBV_eps, DBL_EPSILON, 0, 0, |
593
|
1679 "machine precision"); |
|
1680 |
1418
|
1681 DEFVAR ("gnuplot_binary", SBV_gnuplot_binary, "gnuplot", 0, |
593
|
1682 sv_gnuplot_binary, |
|
1683 "path to gnuplot binary"); |
|
1684 |
1502
|
1685 #ifdef GNUPLOT_HAS_MULTIPLOT |
1506
|
1686 double with_multiplot = 1.0; |
1502
|
1687 #else |
1506
|
1688 double with_multiplot = 0.0; |
1502
|
1689 #endif |
|
1690 |
|
1691 DEFVAR ("gnuplot_has_multiplot", SBV_gnuplot_has_multiplot, |
1504
|
1692 with_multiplot, 0, gnuplot_has_multiplot, |
1502
|
1693 "true if gnuplot supports multiplot, false otherwise"); |
|
1694 |
1646
|
1695 DEFVAR ("history_file", SBV_history_file, |
|
1696 default_history_file (), 0, sv_history_file, |
|
1697 "name of command history file"); |
|
1698 |
|
1699 DEFVAR ("history_size", SBV_history_size, |
|
1700 default_history_size (), 0, history_size, |
|
1701 "number of commands to save in the history list"); |
|
1702 |
1418
|
1703 DEFCONST ("i", SBV_i, Complex (0.0, 1.0), 1, 0, |
593
|
1704 "sqrt (-1)"); |
529
|
1705 |
1418
|
1706 DEFVAR ("ignore_function_time_stamp", |
|
1707 SBV_ignore_function_time_stamp, "system", 0, |
593
|
1708 ignore_function_time_stamp, |
|
1709 "don't check to see if function files have changed since they were\n\ |
|
1710 last compiled. Possible values are \"system\" and \"all\""); |
|
1711 |
1418
|
1712 DEFVAR ("implicit_str_to_num_ok", SBV_implicit_str_to_num_ok, |
1505
|
1713 0.0, 0, implicit_str_to_num_ok, |
593
|
1714 "allow implicit string to number conversion"); |
|
1715 |
1418
|
1716 DEFCONST ("inf", SBV_inf, octave_Inf, 0, 0, |
593
|
1717 "infinity"); |
|
1718 |
1418
|
1719 DEFCONST ("j", SBV_j, Complex (0.0, 1.0), 1, 0, |
593
|
1720 "sqrt (-1)"); |
529
|
1721 |
1418
|
1722 DEFCONST ("nan", SBV_nan, octave_NaN, 0, 0, |
593
|
1723 "not a number"); |
|
1724 |
|
1725 DEFVAR ("ok_to_lose_imaginary_part", SBV_ok_to_lose_imaginary_part, |
1418
|
1726 "warn", 0, ok_to_lose_imaginary_part, |
593
|
1727 "silently convert from complex to real by dropping imaginary part"); |
|
1728 |
1418
|
1729 DEFVAR ("output_max_field_width", SBV_output_max_field_width, 10.0, |
|
1730 0, set_output_max_field_width, |
593
|
1731 "maximum width of an output field for numeric output"); |
|
1732 |
1418
|
1733 DEFVAR ("output_precision", SBV_output_precision, 5.0, 0, |
593
|
1734 set_output_precision, |
|
1735 "number of significant figures to display for numeric output"); |
|
1736 |
1505
|
1737 DEFVAR ("page_screen_output", SBV_page_screen_output, 1.0, 0, |
593
|
1738 page_screen_output, |
|
1739 "if possible, send output intended for the screen through the pager"); |
529
|
1740 |
1418
|
1741 DEFCONST ("pi", SBV_pi, 4.0 * atan (1.0), 0, 0, |
593
|
1742 "ratio of the circumference of a circle to its diameter"); |
|
1743 |
1505
|
1744 DEFVAR ("prefer_column_vectors", SBV_prefer_column_vectors, 1.0, |
1418
|
1745 0, prefer_column_vectors, |
593
|
1746 "prefer column/row vectors"); |
|
1747 |
|
1748 DEFVAR ("prefer_zero_one_indexing", SBV_prefer_zero_one_indexing, |
1505
|
1749 0.0, 0, prefer_zero_one_indexing, |
593
|
1750 "when there is a conflict, prefer zero-one style indexing"); |
|
1751 |
1505
|
1752 DEFVAR ("print_answer_id_name", SBV_print_answer_id_name, 1.0, 0, |
1418
|
1753 print_answer_id_name, |
593
|
1754 "set output style to print `var_name = ...'"); |
|
1755 |
1418
|
1756 DEFVAR ("print_empty_dimensions", SBV_print_empty_dimensions, |
1505
|
1757 1.0, 0, print_empty_dimensions, |
593
|
1758 "also print dimensions of empty matrices"); |
|
1759 |
1418
|
1760 DEFCONST ("program_invocation_name", SBV_program_invocation_name, |
|
1761 raw_prog_name, 0, 0, |
1343
|
1762 "the full name of the current program or script, including the\n\ |
|
1763 directory specification"); |
|
1764 |
1418
|
1765 DEFCONST ("program_name", SBV_program_name, prog_name, 0, 0, |
1343
|
1766 "the name of the current program or script"); |
|
1767 |
593
|
1768 DEFVAR ("propagate_empty_matrices", SBV_propagate_empty_matrices, |
1505
|
1769 1.0, 0, propagate_empty_matrices, |
593
|
1770 "operations on empty matrices return an empty matrix, not an error"); |
529
|
1771 |
1045
|
1772 #if 0 |
1505
|
1773 DEFVAR ("read_only_constants", SBV_read_only_constants, 1.0, 0, |
1418
|
1774 read_only_constants, |
1045
|
1775 "allow built-in constants to be modified"); |
|
1776 #endif |
|
1777 |
1418
|
1778 DEFCONST ("realmax", SBV_realmax, DBL_MAX, 0, 0, |
868
|
1779 "realmax (): return largest representable floating point number"); |
|
1780 |
1418
|
1781 DEFCONST ("realmin", SBV_realmin, DBL_MIN, 0, 0, |
868
|
1782 "realmin (): return smallest representable floating point number"); |
|
1783 |
1505
|
1784 DEFVAR ("resize_on_range_error", SBV_resize_on_range_error, 1.0, |
1418
|
1785 0, resize_on_range_error, |
593
|
1786 "enlarge matrices on assignment"); |
|
1787 |
1418
|
1788 DEFVAR ("return_last_computed_value", |
1505
|
1789 SBV_return_last_computed_value, 0.0, 0, |
593
|
1790 return_last_computed_value, |
|
1791 "if a function does not return any values explicitly, return the\n\ |
|
1792 last computed value"); |
|
1793 |
1643
|
1794 DEFVAR ("saving_history", SBV_saving_history, 1.0, 0, saving_history, |
|
1795 "save command history"); |
593
|
1796 |
1505
|
1797 DEFVAR ("silent_functions", SBV_silent_functions, 0.0, 0, |
593
|
1798 silent_functions, |
|
1799 "suppress printing results in called functions"); |
|
1800 |
1505
|
1801 DEFVAR ("split_long_rows", SBV_split_long_rows, 1.0, 0, |
593
|
1802 split_long_rows, |
|
1803 "split long matrix rows instead of wrapping"); |
529
|
1804 |
1198
|
1805 DEFVAR ("struct_levels_to_print", SBV_struct_levels_to_print, 2.0, |
1418
|
1806 0, struct_levels_to_print, |
1198
|
1807 "number of levels of structure elements to print"); |
|
1808 |
1139
|
1809 #ifdef USE_GNU_INFO |
1116
|
1810 DEFVAR ("suppress_verbose_help_message", |
1505
|
1811 SBV_suppress_verbose_help_message, 0.0, 0, |
1116
|
1812 suppress_verbose_help_message, |
|
1813 "suppress printing of message pointing to additional help in the\n\ |
|
1814 help and usage functions"); |
1139
|
1815 #endif |
1116
|
1816 |
1418
|
1817 DEFCONST ("stdin", SBV_stdin, 0.0, 0, 0, |
593
|
1818 "file number of the standard input stream"); |
|
1819 |
1418
|
1820 DEFCONST ("stdout", SBV_stdout, 1.0, 0, 0, |
593
|
1821 "file number of the standard output stream"); |
|
1822 |
1418
|
1823 DEFCONST ("stderr", SBV_stderr, 2.0, 0, 0, |
593
|
1824 "file number of the standard error stream"); |
|
1825 |
1505
|
1826 DEFVAR ("treat_neg_dim_as_zero", SBV_treat_neg_dim_as_zero, 0.0, 0, |
|
1827 treat_neg_dim_as_zero, |
593
|
1828 "convert negative dimensions to zero"); |
|
1829 |
1418
|
1830 DEFVAR ("warn_assign_as_truth_value", |
1505
|
1831 SBV_warn_assign_as_truth_value, 1.0, 0, |
593
|
1832 warn_assign_as_truth_value, |
|
1833 "produce warning for assignments used as truth values"); |
|
1834 |
|
1835 DEFVAR ("warn_comma_in_global_decl", SBV_warn_comma_in_global_decl, |
1505
|
1836 1.0, 0, warn_comma_in_global_decl, |
593
|
1837 "produce warning for commas in global declarations"); |
|
1838 |
1505
|
1839 DEFVAR ("warn_divide_by_zero", SBV_warn_divide_by_zero, 1.0, 0, |
1418
|
1840 warn_divide_by_zero, |
593
|
1841 "on IEEE machines, allow divide by zero errors to be suppressed"); |
1037
|
1842 |
|
1843 DEFVAR ("warn_function_name_clash", SBV_warn_function_name_clash, |
1505
|
1844 1.0, 0, warn_function_name_clash, |
1037
|
1845 "produce warning if function name conflicts with file name"); |
1093
|
1846 |
1509
|
1847 DEFVAR ("warn_missing_semicolon", SBV_warn_missing_semicolon, |
|
1848 0.0, 0, warn_missing_semicolon, |
1511
|
1849 "produce a warning if a statement in a function file is not |
|
1850 terminated with a semicolon"); |
1509
|
1851 |
1418
|
1852 DEFVAR ("whitespace_in_literal_matrix", |
|
1853 SBV_whitespace_in_literal_matrix, "", 0, |
|
1854 whitespace_in_literal_matrix, |
1093
|
1855 "control auto-insertion of commas and semicolons in literal matrices"); |
529
|
1856 } |
|
1857 |
593
|
1858 // Deleting names from the symbol tables. |
|
1859 |
1488
|
1860 DEFUN_TEXT ("clear", Fclear, Sclear, 10, |
668
|
1861 "clear [-x] [name ...]\n\ |
|
1862 \n\ |
|
1863 Clear symbol(s) matching a list of globbing patterns.\n\ |
593
|
1864 \n\ |
668
|
1865 If no arguments are given, clear all user-defined variables and |
|
1866 functions.\n\ |
|
1867 \n\ |
|
1868 With -x, exclude the named variables") |
529
|
1869 { |
593
|
1870 Octave_object retval; |
|
1871 |
1755
|
1872 int argc = args.length () + 1; |
593
|
1873 |
1755
|
1874 string_vector argv = make_argv (args, "clear"); |
|
1875 |
|
1876 if (error_state) |
|
1877 return retval; |
668
|
1878 |
1271
|
1879 // Always clear the local table, but don't clear currently compiled |
|
1880 // functions unless we are at the top level. (Allowing that to |
|
1881 // happen inside functions would result in pretty odd behavior...) |
593
|
1882 |
|
1883 int clear_user_functions = (curr_sym_tab == top_level_sym_tab); |
|
1884 |
1755
|
1885 if (argc == 1) |
593
|
1886 { |
|
1887 curr_sym_tab->clear (); |
|
1888 global_sym_tab->clear (clear_user_functions); |
|
1889 } |
529
|
1890 else |
|
1891 { |
668
|
1892 int exclusive = 0; |
|
1893 |
1755
|
1894 int idx = 1; |
|
1895 |
|
1896 if (argc > 1) |
668
|
1897 { |
1755
|
1898 if (argv[idx] == "-x") |
|
1899 exclusive = 1; |
668
|
1900 } |
|
1901 |
|
1902 int lcount = 0; |
|
1903 int gcount = 0; |
|
1904 int fcount = 0; |
529
|
1905 |
1755
|
1906 string_vector lvars; |
|
1907 string_vector gvars; |
|
1908 string_vector fcns; |
668
|
1909 |
|
1910 if (argc > 0) |
593
|
1911 { |
893
|
1912 lvars = curr_sym_tab->list (lcount, 0, 0, 0, |
1418
|
1913 SYMTAB_VARIABLES, |
668
|
1914 SYMTAB_LOCAL_SCOPE); |
|
1915 |
893
|
1916 gvars = curr_sym_tab->list (gcount, 0, 0, 0, |
1418
|
1917 SYMTAB_VARIABLES, |
668
|
1918 SYMTAB_GLOBAL_SCOPE); |
|
1919 |
893
|
1920 fcns = global_sym_tab->list (fcount, 0, 0, 0, |
|
1921 symbol_def::USER_FUNCTION, |
864
|
1922 SYMTAB_ALL_SCOPES); |
668
|
1923 } |
|
1924 |
1755
|
1925 for (int k = idx + 1; k < argc; k++) |
668
|
1926 { |
1755
|
1927 string patstr = argv[k]; |
668
|
1928 |
1755
|
1929 if (! patstr.empty ()) |
593
|
1930 { |
1792
|
1931 glob_match pattern (patstr); |
1755
|
1932 |
593
|
1933 int i; |
|
1934 for (i = 0; i < lcount; i++) |
|
1935 { |
1755
|
1936 string nm = lvars[i]; |
1792
|
1937 int match = pattern.match (nm); |
668
|
1938 if ((exclusive && ! match) || (! exclusive && match)) |
|
1939 curr_sym_tab->clear (nm); |
593
|
1940 } |
529
|
1941 |
593
|
1942 int count; |
|
1943 for (i = 0; i < gcount; i++) |
|
1944 { |
1755
|
1945 string nm = gvars[i]; |
1792
|
1946 int match = pattern.match (nm); |
668
|
1947 if ((exclusive && ! match) || (! exclusive && match)) |
593
|
1948 { |
668
|
1949 count = curr_sym_tab->clear (nm); |
593
|
1950 if (count > 0) |
668
|
1951 global_sym_tab->clear (nm, clear_user_functions); |
593
|
1952 } |
|
1953 } |
529
|
1954 |
593
|
1955 for (i = 0; i < fcount; i++) |
|
1956 { |
1755
|
1957 string nm = fcns[i]; |
1792
|
1958 int match = pattern.match (nm); |
668
|
1959 if ((exclusive && ! match) || (! exclusive && match)) |
593
|
1960 { |
668
|
1961 count = curr_sym_tab->clear (nm); |
864
|
1962 global_sym_tab->clear (nm, clear_user_functions); |
593
|
1963 } |
|
1964 } |
|
1965 } |
|
1966 } |
|
1967 } |
|
1968 |
|
1969 return retval; |
529
|
1970 } |
|
1971 |
1
|
1972 /* |
|
1973 ;;; Local Variables: *** |
|
1974 ;;; mode: C++ *** |
|
1975 ;;; page-delimiter: "^/\\*" *** |
|
1976 ;;; End: *** |
|
1977 */ |