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