1
|
1 // variables.cc -*- C++ -*- |
|
2 /* |
|
3 |
296
|
4 Copyright (C) 1992, 1993, 1994 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 |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
1
|
26 #endif |
|
27 |
605
|
28 #if 0 |
|
29 #include <ctype.h> |
|
30 #include <iostream.h> |
|
31 |
|
32 #include "mappers.h" |
|
33 #endif |
|
34 |
1
|
35 #include <sys/types.h> |
|
36 #ifdef HAVE_UNISTD_H |
|
37 #include <unistd.h> |
|
38 #endif |
529
|
39 #include <float.h> |
|
40 #include <string.h> |
279
|
41 #include <strstream.h> |
1
|
42 |
605
|
43 #include "defaults.h" |
|
44 #include "version.h" |
581
|
45 #include "octave-hist.h" |
|
46 #include "unwind-prot.h" |
605
|
47 #include "variables.h" |
581
|
48 #include "user-prefs.h" |
605
|
49 #include "statdefs.h" |
584
|
50 #include "tree-base.h" |
|
51 #include "tree-expr.h" |
1
|
52 #include "tree-const.h" |
605
|
53 #include "dirfns.h" |
581
|
54 #include "oct-obj.h" |
|
55 #include "sysdep.h" |
|
56 #include "symtab.h" |
529
|
57 #include "octave.h" |
605
|
58 #include "pager.h" |
1
|
59 #include "error.h" |
605
|
60 #include "defun.h" |
1
|
61 #include "utils.h" |
605
|
62 #include "parse.h" |
581
|
63 #include "input.h" |
164
|
64 #include "help.h" |
581
|
65 #include "lex.h" |
529
|
66 |
|
67 extern "C" |
|
68 { |
581
|
69 #include <readline/readline.h> |
529
|
70 |
|
71 #include "fnmatch.h" |
|
72 } |
1
|
73 |
|
74 // Symbol table for symbols at the top level. |
581
|
75 symbol_table *top_level_sym_tab = 0; |
1
|
76 |
|
77 // Symbol table for the current scope. |
581
|
78 symbol_table *curr_sym_tab = 0; |
1
|
79 |
|
80 // Symbol table for global symbols. |
581
|
81 symbol_table *global_sym_tab = 0; |
1
|
82 |
593
|
83 // Initialization. |
|
84 |
|
85 // Create the initial symbol tables and set the current scope at the |
|
86 // top level. |
|
87 |
195
|
88 void |
|
89 initialize_symbol_tables (void) |
|
90 { |
581
|
91 if (! global_sym_tab) |
|
92 global_sym_tab = new symbol_table (); |
195
|
93 |
581
|
94 if (! top_level_sym_tab) |
|
95 top_level_sym_tab = new symbol_table (); |
195
|
96 |
|
97 curr_sym_tab = top_level_sym_tab; |
|
98 } |
|
99 |
593
|
100 // Attributes of variables and functions. |
|
101 |
|
102 // Is this variable a builtin? |
|
103 |
|
104 int |
|
105 is_builtin_variable (const char *name) |
|
106 { |
|
107 symbol_record *sr = global_sym_tab->lookup (name, 0, 0); |
|
108 return (sr && sr->is_builtin_variable ()); |
|
109 } |
|
110 |
|
111 // Is this a text-style function? |
|
112 |
|
113 int |
|
114 is_text_function_name (const char *s) |
|
115 { |
|
116 symbol_record *sr = global_sym_tab->lookup (s); |
|
117 return (sr && sr->is_text_function ()); |
|
118 } |
|
119 |
|
120 // Is this function globally in this scope? |
|
121 |
605
|
122 int |
593
|
123 is_globally_visible (const char *name) |
|
124 { |
|
125 symbol_record *sr = curr_sym_tab->lookup (name, 0, 0); |
|
126 return (sr && sr->is_linked_to_global ()); |
|
127 } |
|
128 |
|
129 // Is this tree_constant a valid function? |
|
130 |
|
131 tree_fvc * |
|
132 is_valid_function (const tree_constant& arg, char *warn_for, int warn) |
|
133 { |
|
134 tree_fvc *ans = 0; |
|
135 |
636
|
136 char *fcn_name = arg.string_value (); |
|
137 |
|
138 if (error_state) |
593
|
139 { |
|
140 if (warn) |
|
141 error ("%s: expecting function name as argument", warn_for); |
|
142 return ans; |
|
143 } |
|
144 |
|
145 symbol_record *sr = 0; |
|
146 if (fcn_name) |
|
147 sr = lookup_by_name (fcn_name); |
|
148 |
|
149 if (sr) |
|
150 ans = sr->def (); |
|
151 |
|
152 if (! sr || ! ans || ! sr->is_function ()) |
|
153 { |
|
154 if (warn) |
|
155 error ("%s: the symbol `%s' is not valid as a function", |
|
156 warn_for, fcn_name); |
|
157 ans = 0; |
|
158 } |
|
159 |
|
160 return ans; |
|
161 } |
|
162 |
|
163 // Does this function take the right number of arguments? |
|
164 |
|
165 int |
|
166 takes_correct_nargs (tree_fvc *fcn, int expected_nargin, char *warn_for, |
|
167 int warn) |
|
168 { |
|
169 int nargin = fcn->max_expected_args () - 1; |
|
170 int e_nargin = expected_nargin - 1; |
|
171 if (nargin != e_nargin) |
|
172 { |
|
173 if (warn) |
|
174 error ("%s: expecting function to take %d argument%c", |
|
175 warn_for, e_nargin, (e_nargin == 1 ? "" : "s")); |
|
176 return 0; |
|
177 } |
|
178 return 1; |
|
179 } |
|
180 |
|
181 DEFUN ("is_global", Fis_global, Sis_global, 2, 1, |
|
182 "is_global (X): return 1 if the string X names a global variable\n\ |
|
183 otherwise, return 0.") |
|
184 { |
|
185 Octave_object retval = 0.0; |
|
186 |
636
|
187 if (args.length () != 2) |
593
|
188 { |
|
189 print_usage ("is_global"); |
|
190 return retval; |
|
191 } |
|
192 |
|
193 char *name = args(1).string_value (); |
|
194 |
636
|
195 if (error_state) |
|
196 { |
|
197 error ("is_global: expecting string argument"); |
|
198 return retval; |
|
199 } |
|
200 |
593
|
201 symbol_record *sr = curr_sym_tab->lookup (name, 0, 0); |
|
202 |
|
203 retval = (double) (sr && sr->is_linked_to_global ()); |
|
204 |
|
205 return retval; |
|
206 } |
|
207 |
|
208 DEFUN ("exist", Fexist, Sexist, 2, 1, |
|
209 "exist (NAME): check if variable or file exists\n\ |
|
210 \n\ |
|
211 return 0 if NAME is undefined, 1 if it is a variable, or 2 if it is\n\ |
|
212 a function.") |
|
213 { |
|
214 Octave_object retval; |
|
215 |
636
|
216 if (args.length () != 2) |
593
|
217 { |
|
218 print_usage ("exist"); |
|
219 return retval; |
|
220 } |
|
221 |
|
222 char *name = args(1).string_value (); |
|
223 |
636
|
224 if (error_state) |
|
225 { |
|
226 error ("exist: expecting string argument"); |
|
227 return retval; |
|
228 } |
|
229 |
593
|
230 symbol_record *sr = curr_sym_tab->lookup (name, 0, 0); |
|
231 if (! sr) |
|
232 sr = global_sym_tab->lookup (name, 0, 0); |
|
233 |
|
234 retval = 0.0; |
|
235 |
|
236 if (sr && sr->is_variable () && sr->is_defined ()) |
|
237 retval = 1.0; |
|
238 else if (sr && sr->is_function ()) |
|
239 retval = 2.0; |
|
240 else |
|
241 { |
|
242 char *path = fcn_file_in_path (name); |
|
243 if (path) |
|
244 { |
|
245 delete [] path; |
|
246 retval = 2.0; |
|
247 } |
|
248 else |
|
249 { |
|
250 struct stat buf; |
|
251 if (stat (name, &buf) == 0 && S_ISREG (buf.st_mode)) |
|
252 retval = 2.0; |
|
253 } |
|
254 } |
|
255 |
|
256 return retval; |
|
257 } |
|
258 |
|
259 // XXX FIXME XXX -- should these really be here? |
|
260 |
|
261 static char * |
|
262 octave_home (void) |
|
263 { |
|
264 char *oh = getenv ("OCTAVE_HOME"); |
666
|
265 |
|
266 return (oh ? oh : OCTAVE_PREFIX); |
|
267 } |
|
268 |
|
269 static char * |
|
270 subst_octave_home (char *s) |
|
271 { |
|
272 char *home = octave_home (); |
|
273 char *prefix = OCTAVE_PREFIX; |
|
274 |
|
275 char *retval; |
|
276 |
|
277 if (strcmp (home, prefix) == 0) |
|
278 retval = strsave (s); |
593
|
279 else |
666
|
280 { |
|
281 int len_home = strlen (home); |
|
282 int len_prefix = strlen (prefix); |
|
283 |
|
284 int count = 0; |
|
285 char *ptr = s; |
|
286 while (strstr (ptr, prefix)) |
|
287 { |
|
288 ptr += len_prefix; |
|
289 count++; |
|
290 } |
|
291 |
|
292 int grow_size = count * (len_home - len_prefix); |
|
293 |
|
294 int len_retval = strlen (s) + grow_size; |
|
295 |
|
296 retval = new char [len_retval+1]; |
|
297 |
|
298 char *p1 = s; |
|
299 char *p2 = p1; |
|
300 char *pdest = retval; |
|
301 |
|
302 for (int i = 0; i < count; i++) |
|
303 { |
|
304 p2 = strstr (p1, prefix); |
|
305 |
|
306 if (! p2) |
|
307 panic_impossible (); |
|
308 |
|
309 if (p1 == p2) |
|
310 { |
|
311 memcpy (pdest, home, len_home); |
|
312 pdest += len_home; |
|
313 p1 += len_prefix; |
|
314 } |
|
315 else |
|
316 { |
|
317 int len = (int) (p2 - p1); |
|
318 memcpy (pdest, p1, len); |
|
319 pdest += len; |
|
320 p1 += len; |
|
321 } |
|
322 |
|
323 } |
|
324 } |
|
325 |
|
326 return retval; |
593
|
327 } |
|
328 |
|
329 static char * |
|
330 octave_info_dir (void) |
|
331 { |
666
|
332 static char *retval = subst_octave_home (OCTAVE_INFODIR); |
|
333 return retval; |
|
334 } |
|
335 |
|
336 static char * |
|
337 octave_arch_lib_dir (void) |
|
338 { |
|
339 static char *retval = subst_octave_home (OCTAVE_ARCHLIBDIR); |
|
340 return retval; |
593
|
341 } |
|
342 |
|
343 static char * |
|
344 default_pager (void) |
|
345 { |
|
346 static char *pager_binary = 0; |
|
347 delete [] pager_binary; |
|
348 char *pgr = getenv ("PAGER"); |
|
349 if (pgr) |
|
350 pager_binary = strsave (pgr); |
|
351 else |
|
352 #ifdef DEFAULT_PAGER |
|
353 pager_binary = strsave (DEFAULT_PAGER); |
|
354 #else |
|
355 pager_binary = strsave (""); |
|
356 #endif |
|
357 |
|
358 return pager_binary; |
|
359 } |
|
360 |
|
361 char * |
|
362 octave_lib_dir (void) |
|
363 { |
666
|
364 static char *retval = subst_octave_home (OCTAVE_LIBDIR); |
|
365 return retval; |
593
|
366 } |
|
367 |
|
368 // Handle OCTAVE_PATH from the environment like TeX handles TEXINPUTS. |
|
369 // If the path starts with `:', prepend the standard path. If it ends |
|
370 // with `:' append the standard path. If it begins and ends with |
|
371 // `:', do both (which is useless, but the luser asked for it...). |
|
372 // |
|
373 // This function may eventually be called more than once, so be |
|
374 // careful not to create memory leaks. |
|
375 |
|
376 char * |
|
377 default_path (void) |
|
378 { |
|
379 static char *pathstring = 0; |
|
380 delete [] pathstring; |
|
381 |
666
|
382 static char *std_path = subst_octave_home (OCTAVE_FCNFILEPATH); |
593
|
383 |
|
384 char *oct_path = getenv ("OCTAVE_PATH"); |
|
385 |
|
386 if (oct_path) |
|
387 { |
|
388 pathstring = strsave (oct_path); |
|
389 |
|
390 if (pathstring[0] == ':') |
|
391 { |
|
392 char *tmp = pathstring; |
|
393 pathstring = strconcat (std_path, pathstring); |
|
394 delete [] tmp; |
|
395 } |
|
396 |
|
397 int tmp_len = strlen (pathstring); |
|
398 if (pathstring[tmp_len-1] == ':') |
|
399 { |
|
400 char *tmp = pathstring; |
|
401 pathstring = strconcat (pathstring, std_path); |
|
402 delete [] tmp; |
|
403 } |
|
404 } |
|
405 else |
|
406 pathstring = strsave (std_path); |
|
407 |
|
408 return pathstring; |
|
409 } |
|
410 |
|
411 char * |
|
412 default_info_file (void) |
|
413 { |
|
414 static char *info_file_string = 0; |
|
415 delete [] info_file_string; |
|
416 char *oct_info_file = getenv ("OCTAVE_INFO_FILE"); |
|
417 if (oct_info_file) |
|
418 info_file_string = strsave (oct_info_file); |
|
419 else |
|
420 { |
|
421 char *infodir = octave_info_dir (); |
|
422 info_file_string = strconcat (infodir, "/octave.info"); |
|
423 } |
|
424 return info_file_string; |
|
425 } |
|
426 |
|
427 char * |
|
428 default_editor (void) |
|
429 { |
|
430 static char *editor_string = 0; |
|
431 delete [] editor_string; |
|
432 char *env_editor = getenv ("EDITOR"); |
|
433 if (env_editor && *env_editor) |
|
434 editor_string = strsave (env_editor); |
|
435 else |
|
436 editor_string = strsave ("vi"); |
|
437 return editor_string; |
|
438 } |
|
439 |
|
440 char * |
|
441 get_site_defaults (void) |
|
442 { |
|
443 static char *sd = 0; |
|
444 delete [] sd; |
|
445 char *libdir = octave_lib_dir (); |
|
446 sd = strconcat (libdir, "/octaverc"); |
|
447 return sd; |
|
448 } |
|
449 |
|
450 // Functions for looking up variables and functions. |
|
451 |
581
|
452 // Is there a corresponding function file that is newer than the |
|
453 // symbol definition? |
|
454 |
593
|
455 static int |
1
|
456 symbol_out_of_date (symbol_record *sr) |
|
457 { |
195
|
458 int ignore = user_pref.ignore_function_time_stamp; |
|
459 |
|
460 if (ignore == 2) |
|
461 return 0; |
|
462 |
529
|
463 if (sr) |
1
|
464 { |
490
|
465 tree_fvc *ans = sr->def (); |
529
|
466 if (ans) |
1
|
467 { |
339
|
468 char *ff = ans->fcn_file_name (); |
529
|
469 if (ff && ! (ignore && ans->is_system_fcn_file ())) |
1
|
470 { |
|
471 time_t tp = ans->time_parsed (); |
339
|
472 char *fname = fcn_file_in_path (ff); |
195
|
473 int status = is_newer (fname, tp); |
|
474 delete [] fname; |
|
475 if (status > 0) |
|
476 return 1; |
1
|
477 } |
|
478 } |
|
479 } |
195
|
480 return 0; |
1
|
481 } |
|
482 |
581
|
483 static void |
|
484 gobble_leading_white_space (FILE *ffile) |
|
485 { |
|
486 int in_comment = 0; |
|
487 int c; |
|
488 while ((c = getc (ffile)) != EOF) |
|
489 { |
|
490 if (in_comment) |
|
491 { |
|
492 if (c == '\n') |
|
493 in_comment = 0; |
|
494 } |
|
495 else |
|
496 { |
|
497 if (c == ' ' || c == '\t' || c == '\n') |
|
498 continue; |
|
499 else if (c == '%' || c == '#') |
|
500 in_comment = 1; |
|
501 else |
|
502 { |
|
503 ungetc (c, ffile); |
|
504 break; |
|
505 } |
|
506 } |
|
507 } |
|
508 } |
|
509 |
|
510 static int |
|
511 is_function_file (FILE *ffile) |
|
512 { |
|
513 int status = 0; |
|
514 |
|
515 gobble_leading_white_space (ffile); |
|
516 |
|
517 long pos = ftell (ffile); |
|
518 |
|
519 char buf [10]; |
|
520 fgets (buf, 10, ffile); |
|
521 int len = strlen (buf); |
|
522 if (len > 8 && strncmp (buf, "function", 8) == 0 |
|
523 && ! (isalnum (buf[8]) || buf[8] == '_')) |
|
524 status = 1; |
|
525 |
|
526 fseek (ffile, pos, SEEK_SET); |
|
527 |
|
528 return status; |
|
529 } |
|
530 |
|
531 static int |
|
532 parse_fcn_file (int exec_script, char *ff) |
|
533 { |
|
534 begin_unwind_frame ("parse_fcn_file"); |
|
535 |
|
536 int script_file_executed = 0; |
|
537 |
|
538 assert (ff); |
|
539 |
|
540 // Open function file and parse. |
|
541 |
|
542 int old_reading_fcn_file_state = reading_fcn_file; |
|
543 |
|
544 unwind_protect_ptr (rl_instream); |
|
545 unwind_protect_ptr (ff_instream); |
|
546 |
|
547 unwind_protect_int (using_readline); |
|
548 unwind_protect_int (input_line_number); |
|
549 unwind_protect_int (current_input_column); |
|
550 unwind_protect_int (reading_fcn_file); |
|
551 |
|
552 using_readline = 0; |
|
553 reading_fcn_file = 1; |
|
554 input_line_number = 0; |
|
555 current_input_column = 1; |
|
556 |
|
557 FILE *ffile = get_input_from_file (ff, 0); |
|
558 |
|
559 if (ffile) |
|
560 { |
|
561 // Check to see if this file defines a function or is just a list of |
|
562 // commands. |
|
563 |
|
564 if (is_function_file (ffile)) |
|
565 { |
|
566 unwind_protect_int (echo_input); |
|
567 unwind_protect_int (saving_history); |
|
568 unwind_protect_int (reading_fcn_file); |
|
569 |
|
570 echo_input = 0; |
|
571 saving_history = 0; |
|
572 reading_fcn_file = 1; |
|
573 |
|
574 YY_BUFFER_STATE old_buf = current_buffer (); |
|
575 YY_BUFFER_STATE new_buf = create_buffer (ffile); |
|
576 |
|
577 add_unwind_protect (restore_input_buffer, (void *) old_buf); |
|
578 add_unwind_protect (delete_input_buffer, (void *) new_buf); |
|
579 |
|
580 switch_to_buffer (new_buf); |
|
581 |
|
582 unwind_protect_ptr (curr_sym_tab); |
|
583 |
|
584 reset_parser (); |
|
585 |
|
586 int status = yyparse (); |
|
587 |
|
588 if (status != 0) |
|
589 { |
|
590 error ("parse error while reading function file %s", ff); |
|
591 global_sym_tab->clear (curr_fcn_file_name); |
|
592 } |
|
593 } |
|
594 else if (exec_script) |
|
595 { |
|
596 // The value of `reading_fcn_file' will be restored to the proper value |
|
597 // when we unwind from this frame. |
|
598 reading_fcn_file = old_reading_fcn_file_state; |
|
599 |
|
600 unwind_protect_int (reading_script_file); |
|
601 reading_script_file = 1; |
|
602 |
|
603 parse_and_execute (ffile, 1); |
|
604 |
|
605 script_file_executed = 1; |
|
606 } |
|
607 fclose (ffile); |
|
608 } |
|
609 |
|
610 run_unwind_frame ("parse_fcn_file"); |
|
611 |
|
612 return script_file_executed; |
|
613 } |
|
614 |
593
|
615 static int |
581
|
616 load_fcn_from_file (symbol_record *sym_rec, int exec_script) |
|
617 { |
|
618 int script_file_executed = 0; |
|
619 |
|
620 char *nm = sym_rec->name (); |
|
621 |
|
622 curr_fcn_file_name = nm; |
|
623 |
|
624 char *oct_file = oct_file_in_path (curr_fcn_file_name); |
|
625 |
|
626 int loaded_oct_file = 0; |
|
627 |
|
628 if (oct_file) |
|
629 { |
|
630 cerr << "found: " << oct_file << "\n"; |
|
631 |
|
632 delete [] oct_file; |
|
633 |
|
634 // XXX FIXME XXX -- this is where we try to link to an external |
|
635 // object... |
|
636 loaded_oct_file = 1; |
|
637 } |
|
638 |
|
639 if (! loaded_oct_file) |
|
640 { |
|
641 char *ff = fcn_file_in_path (curr_fcn_file_name); |
|
642 |
|
643 if (ff) |
|
644 { |
|
645 script_file_executed = parse_fcn_file (exec_script, ff); |
|
646 delete [] ff; |
|
647 } |
|
648 |
|
649 if (! (error_state || script_file_executed)) |
|
650 force_link_to_function (nm); |
|
651 } |
|
652 |
|
653 return script_file_executed; |
|
654 } |
|
655 |
|
656 int |
|
657 lookup (symbol_record *sym_rec, int exec_script) |
|
658 { |
|
659 int script_file_executed = 0; |
|
660 |
|
661 if (! sym_rec->is_linked_to_global ()) |
|
662 { |
|
663 if (sym_rec->is_defined ()) |
|
664 { |
|
665 if (sym_rec->is_function () && symbol_out_of_date (sym_rec)) |
593
|
666 script_file_executed = load_fcn_from_file (sym_rec, exec_script); |
581
|
667 } |
|
668 else if (! sym_rec->is_formal_parameter ()) |
|
669 { |
|
670 link_to_builtin_or_function (sym_rec); |
|
671 |
|
672 if (! sym_rec->is_defined ()) |
593
|
673 script_file_executed = load_fcn_from_file (sym_rec, exec_script); |
581
|
674 else if (sym_rec->is_function () && symbol_out_of_date (sym_rec)) |
593
|
675 script_file_executed = load_fcn_from_file (sym_rec, exec_script); |
581
|
676 } |
|
677 } |
|
678 |
|
679 return script_file_executed; |
|
680 } |
|
681 |
|
682 // Get the symbol record for the given name that is visible in the |
|
683 // current scope. Reread any function definitions that appear to be |
|
684 // out of date. If a function is available in a file but is not |
|
685 // currently loaded, this will load it and insert the name in the |
|
686 // current symbol table. |
|
687 |
|
688 symbol_record * |
|
689 lookup_by_name (const char *nm, int exec_script) |
|
690 { |
|
691 symbol_record *sym_rec = curr_sym_tab->lookup (nm, 1, 0); |
|
692 |
|
693 lookup (sym_rec, exec_script); |
|
694 |
|
695 return sym_rec; |
|
696 } |
|
697 |
593
|
698 // Variable values. |
195
|
699 |
581
|
700 // Look for the given name in the global symbol table. If it refers |
|
701 // to a string, return a new copy. If not, return 0; |
|
702 |
1
|
703 char * |
195
|
704 builtin_string_variable (const char *name) |
1
|
705 { |
195
|
706 symbol_record *sr = global_sym_tab->lookup (name, 0, 0); |
|
707 |
|
708 // It is a prorgramming error to look for builtins that aren't. |
|
709 |
529
|
710 assert (sr); |
195
|
711 |
529
|
712 char *retval = 0; |
1
|
713 |
490
|
714 tree_fvc *defn = sr->def (); |
195
|
715 |
529
|
716 if (defn) |
1
|
717 { |
|
718 tree_constant val = defn->eval (0); |
195
|
719 |
610
|
720 if (! error_state && val.is_string ()) |
1
|
721 { |
|
722 char *s = val.string_value (); |
636
|
723 |
529
|
724 if (s) |
1
|
725 retval = strsave (s); |
|
726 } |
|
727 } |
|
728 |
|
729 return retval; |
|
730 } |
|
731 |
581
|
732 // Look for the given name in the global symbol table. If it refers |
|
733 // to a real scalar, place the value in d and return 0. Otherwise, |
|
734 // return -1. |
|
735 |
1
|
736 int |
195
|
737 builtin_real_scalar_variable (const char *name, double& d) |
1
|
738 { |
|
739 int status = -1; |
195
|
740 symbol_record *sr = global_sym_tab->lookup (name, 0, 0); |
|
741 |
|
742 // It is a prorgramming error to look for builtins that aren't. |
|
743 |
529
|
744 assert (sr); |
1
|
745 |
490
|
746 tree_fvc *defn = sr->def (); |
195
|
747 |
529
|
748 if (defn) |
1
|
749 { |
|
750 tree_constant val = defn->eval (0); |
195
|
751 |
598
|
752 if (! error_state && val.is_scalar_type ()) |
1
|
753 { |
|
754 d = val.double_value (); |
|
755 status = 0; |
|
756 } |
|
757 } |
|
758 |
|
759 return status; |
|
760 } |
|
761 |
593
|
762 // Global stuff and links to builtin variables and functions. |
|
763 |
581
|
764 // Make the definition of the symbol record sr be the same as the |
|
765 // definition of the global variable of the same name, creating it if |
|
766 // it doesn't already exist. |
|
767 |
195
|
768 void |
|
769 link_to_global_variable (symbol_record *sr) |
|
770 { |
|
771 if (sr->is_linked_to_global ()) |
|
772 return; |
|
773 |
|
774 symbol_record *gsr = global_sym_tab->lookup (sr->name (), 1, 0); |
|
775 |
|
776 if (sr->is_formal_parameter ()) |
|
777 { |
|
778 error ("can't make function parameter `%s' global", sr->name ()); |
|
779 return; |
|
780 } |
|
781 |
|
782 // There must be a better way to do this. XXX FIXME XXX |
|
783 |
|
784 if (sr->is_variable ()) |
|
785 { |
|
786 // Would be nice not to have this cast. XXX FIXME XXX |
|
787 tree_constant *tmp = (tree_constant *) sr->def (); |
529
|
788 if (tmp) |
|
789 tmp = new tree_constant (*tmp); |
|
790 else |
207
|
791 tmp = new tree_constant (); |
195
|
792 gsr->define (tmp); |
|
793 } |
|
794 else |
529
|
795 sr->clear (); |
195
|
796 |
|
797 // If the global symbol is currently defined as a function, we need to |
|
798 // hide it with a variable. |
|
799 |
|
800 if (gsr->is_function ()) |
529
|
801 gsr->define ((tree_constant *) 0); |
195
|
802 |
|
803 sr->alias (gsr, 1); |
|
804 sr->mark_as_linked_to_global (); |
|
805 } |
|
806 |
581
|
807 // Make the definition of the symbol record sr be the same as the |
|
808 // definition of the builtin variable of the same name. |
|
809 |
195
|
810 void |
|
811 link_to_builtin_variable (symbol_record *sr) |
|
812 { |
|
813 symbol_record *tmp_sym = global_sym_tab->lookup (sr->name (), 0, 0); |
|
814 |
529
|
815 if (tmp_sym && tmp_sym->is_builtin_variable ()) |
|
816 sr->alias (tmp_sym); |
195
|
817 } |
|
818 |
581
|
819 // Make the definition of the symbol record sr be the same as the |
|
820 // definition of the builtin variable or function, or user function of |
|
821 // the same name, provided that the name has not been used as a formal |
|
822 // parameter. |
|
823 |
195
|
824 void |
|
825 link_to_builtin_or_function (symbol_record *sr) |
|
826 { |
|
827 symbol_record *tmp_sym = global_sym_tab->lookup (sr->name (), 0, 0); |
|
828 |
529
|
829 if (tmp_sym |
|
830 && (tmp_sym->is_builtin_variable () || tmp_sym->is_function ()) |
|
831 && ! tmp_sym->is_formal_parameter ()) |
|
832 sr->alias (tmp_sym); |
195
|
833 } |
|
834 |
581
|
835 // Force a link to a function in the current symbol table. This is |
|
836 // used just after defining a function to avoid different behavior |
|
837 // depending on whether or not the function has been evaluated after |
|
838 // being defined. |
|
839 // |
|
840 // Return without doing anything if there isn't a function with the |
|
841 // given name defined in the global symbol table. |
|
842 |
195
|
843 void |
|
844 force_link_to_function (const char *id_name) |
|
845 { |
|
846 symbol_record *gsr = global_sym_tab->lookup (id_name, 1, 0); |
|
847 if (gsr->is_function ()) |
|
848 { |
|
849 curr_sym_tab->clear (id_name); |
|
850 symbol_record *csr = curr_sym_tab->lookup (id_name, 1, 0); |
|
851 csr->alias (gsr); |
|
852 } |
|
853 } |
|
854 |
605
|
855 // Help stuff. Shouldn't this go in help.cc? |
593
|
856 |
|
857 // It's not likely that this does the right thing now. XXX FIXME XXX |
|
858 |
|
859 char ** |
|
860 make_name_list (void) |
|
861 { |
|
862 int key_len = 0; |
|
863 int glb_len = 0; |
|
864 int top_len = 0; |
|
865 int lcl_len = 0; |
|
866 int ffl_len = 0; |
|
867 |
|
868 char **key = 0; |
|
869 char **glb = 0; |
|
870 char **top = 0; |
|
871 char **lcl = 0; |
|
872 char **ffl = 0; |
|
873 |
|
874 // Each of these functions returns a new vector of pointers to new |
|
875 // strings. |
|
876 |
|
877 key = names (keyword_help (), key_len); |
|
878 glb = global_sym_tab->list (glb_len); |
|
879 top = top_level_sym_tab->list (top_len); |
|
880 if (top_level_sym_tab != curr_sym_tab) |
|
881 lcl = curr_sym_tab->list (lcl_len); |
|
882 ffl = get_fcn_file_names (ffl_len, 1); |
|
883 |
|
884 int total_len = key_len + glb_len + top_len + lcl_len + ffl_len; |
|
885 |
|
886 char **list = new char * [total_len+1]; |
|
887 |
|
888 // Put all the symbols in one big list. Only copy pointers, not the |
|
889 // strings they point to, then only delete the original array of |
|
890 // pointers, and not the strings they point to. |
|
891 |
|
892 int j = 0; |
|
893 int i = 0; |
|
894 for (i = 0; i < key_len; i++) |
|
895 list[j++] = key[i]; |
|
896 |
|
897 for (i = 0; i < glb_len; i++) |
|
898 list[j++] = glb[i]; |
|
899 |
|
900 for (i = 0; i < top_len; i++) |
|
901 list[j++] = top[i]; |
|
902 |
|
903 for (i = 0; i < lcl_len; i++) |
|
904 list[j++] = lcl[i]; |
|
905 |
|
906 for (i = 0; i < ffl_len; i++) |
|
907 list[j++] = ffl[i]; |
|
908 |
|
909 list[j] = 0; |
|
910 |
|
911 delete [] key; |
|
912 delete [] glb; |
|
913 delete [] top; |
|
914 delete [] lcl; |
|
915 delete [] ffl; |
|
916 |
|
917 return list; |
|
918 } |
|
919 |
|
920 // List variable names. |
|
921 |
|
922 static void |
|
923 print_symbol_info_line (ostrstream& output_buf, const symbol_record_info& s) |
|
924 { |
|
925 output_buf << (s.is_read_only () ? " -" : " w"); |
|
926 output_buf << (s.is_eternal () ? "- " : "d "); |
|
927 #if 0 |
|
928 output_buf << (s.hides_fcn () ? "f" : (s.hides_builtin () ? "F" : "-")); |
|
929 #endif |
|
930 output_buf.form (" %-16s", s.type_as_string ()); |
|
931 if (s.is_function ()) |
|
932 output_buf << " - -"; |
|
933 else |
|
934 { |
|
935 output_buf.form ("%7d", s.rows ()); |
|
936 output_buf.form ("%7d", s.columns ()); |
|
937 } |
|
938 output_buf << " " << s.name () << "\n"; |
|
939 } |
|
940 |
|
941 static void |
|
942 print_long_listing (ostrstream& output_buf, symbol_record_info *s) |
|
943 { |
|
944 if (! s) |
|
945 return; |
|
946 |
|
947 symbol_record_info *ptr = s; |
|
948 while (ptr->is_defined ()) |
|
949 { |
|
950 print_symbol_info_line (output_buf, *ptr); |
|
951 ptr++; |
|
952 } |
|
953 } |
|
954 |
|
955 static int |
|
956 maybe_list (const char *header, ostrstream& output_buf, |
|
957 int show_verbose, symbol_table *sym_tab, unsigned type, |
|
958 unsigned scope) |
|
959 { |
|
960 int count; |
|
961 int status = 0; |
|
962 if (show_verbose) |
|
963 { |
|
964 symbol_record_info *symbols; |
|
965 symbols = sym_tab->long_list (count, 1, type, scope); |
|
966 if (symbols && count > 0) |
|
967 { |
|
968 output_buf << "\n" << header << "\n\n" |
|
969 << "prot type rows cols name\n" |
|
970 << "==== ==== ==== ==== ====\n"; |
|
971 |
|
972 print_long_listing (output_buf, symbols); |
|
973 status = 1; |
|
974 } |
|
975 delete [] symbols; |
|
976 } |
|
977 else |
|
978 { |
|
979 char **symbols = sym_tab->list (count, 1, type, scope); |
|
980 if (symbols && count > 0) |
|
981 { |
|
982 output_buf << "\n" << header << "\n\n"; |
|
983 list_in_columns (output_buf, symbols); |
|
984 status = 1; |
|
985 } |
|
986 delete [] symbols; |
|
987 } |
|
988 return status; |
|
989 } |
|
990 |
|
991 DEFUN_TEXT ("document", Fdocument, Sdocument, -1, 1, |
|
992 "document symbol string ...\n\ |
|
993 \n\ |
|
994 Associate a cryptic message with a variable name.") |
|
995 { |
|
996 Octave_object retval; |
|
997 |
|
998 DEFINE_ARGV("document"); |
|
999 |
|
1000 if (argc == 3) |
|
1001 { |
|
1002 char *name = argv[1]; |
|
1003 char *help = argv[2]; |
|
1004 |
|
1005 if (is_builtin_variable (name)) |
|
1006 error ("sorry, can't redefine help for builtin variables"); |
|
1007 else |
|
1008 { |
|
1009 symbol_record *sym_rec = curr_sym_tab->lookup (name, 0); |
|
1010 |
|
1011 if (sym_rec) |
|
1012 sym_rec->document (help); |
|
1013 else |
|
1014 error ("document: no such symbol `%s'", name); |
|
1015 } |
|
1016 } |
|
1017 else |
|
1018 print_usage ("document"); |
|
1019 |
|
1020 DELETE_ARGV; |
|
1021 |
|
1022 return retval; |
|
1023 } |
|
1024 |
584
|
1025 // XXX FIXME XXX -- this should take a list of regular expressions |
|
1026 // naming the variables to look for. |
|
1027 |
581
|
1028 static Octave_object |
|
1029 do_who (int argc, char **argv, int nargout) |
529
|
1030 { |
|
1031 Octave_object retval; |
|
1032 |
|
1033 int show_builtins = 0; |
|
1034 int show_functions = (curr_sym_tab == top_level_sym_tab); |
|
1035 int show_variables = 1; |
|
1036 int show_verbose = 0; |
|
1037 |
584
|
1038 char *my_name = argv[0]; |
|
1039 |
529
|
1040 if (argc > 1) |
|
1041 { |
|
1042 show_functions = 0; |
|
1043 show_variables = 0; |
|
1044 } |
|
1045 |
|
1046 for (int i = 1; i < argc; i++) |
|
1047 { |
|
1048 argv++; |
|
1049 if (strcmp (*argv, "-all") == 0 || strcmp (*argv, "-a") == 0) |
|
1050 { |
|
1051 show_builtins++; |
|
1052 show_functions++; |
|
1053 show_variables++; |
|
1054 } |
605
|
1055 else if (strcmp (*argv, "-builtins") == 0 || strcmp (*argv, "-b") == 0) |
529
|
1056 show_builtins++; |
605
|
1057 else if (strcmp (*argv, "-functions") == 0 || strcmp (*argv, "-f") == 0) |
529
|
1058 show_functions++; |
605
|
1059 else if (strcmp (*argv, "-long") == 0 || strcmp (*argv, "-l") == 0) |
|
1060 show_verbose++; |
|
1061 else if (strcmp (*argv, "-variables") == 0 || strcmp (*argv, "-v") == 0) |
529
|
1062 show_variables++; |
|
1063 else |
584
|
1064 warning ("%s: unrecognized option `%s'", my_name, *argv); |
529
|
1065 } |
|
1066 |
|
1067 // If the user specified -l and nothing else, show variables. If |
|
1068 // evaluating this at the top level, also show functions. |
|
1069 |
|
1070 if (show_verbose && ! (show_builtins || show_functions || show_variables)) |
|
1071 { |
|
1072 show_functions = (curr_sym_tab == top_level_sym_tab); |
|
1073 show_variables = 1; |
|
1074 } |
|
1075 |
|
1076 ostrstream output_buf; |
|
1077 int pad_after = 0; |
|
1078 |
|
1079 if (show_builtins) |
|
1080 { |
|
1081 pad_after += maybe_list ("*** built-in variables:", |
|
1082 output_buf, show_verbose, global_sym_tab, |
|
1083 symbol_def::BUILTIN_VARIABLE, |
|
1084 SYMTAB_ALL_SCOPES); |
|
1085 |
|
1086 pad_after += maybe_list ("*** built-in functions:", |
|
1087 output_buf, show_verbose, global_sym_tab, |
|
1088 symbol_def::BUILTIN_FUNCTION, |
|
1089 SYMTAB_ALL_SCOPES); |
|
1090 } |
|
1091 |
|
1092 if (show_functions) |
|
1093 { |
|
1094 pad_after += maybe_list ("*** currently compiled functions:", |
|
1095 output_buf, show_verbose, global_sym_tab, |
|
1096 symbol_def::USER_FUNCTION, |
|
1097 SYMTAB_ALL_SCOPES); |
|
1098 } |
|
1099 |
|
1100 if (show_variables) |
|
1101 { |
|
1102 pad_after += maybe_list ("*** local user variables:", |
|
1103 output_buf, show_verbose, curr_sym_tab, |
|
1104 symbol_def::USER_VARIABLE, |
|
1105 SYMTAB_LOCAL_SCOPE); |
|
1106 |
|
1107 pad_after += maybe_list ("*** globally visible user variables:", |
|
1108 output_buf, show_verbose, curr_sym_tab, |
|
1109 symbol_def::USER_VARIABLE, |
|
1110 SYMTAB_GLOBAL_SCOPE); |
|
1111 } |
|
1112 |
|
1113 if (pad_after) |
|
1114 output_buf << "\n"; |
|
1115 |
|
1116 output_buf << ends; |
|
1117 maybe_page_output (output_buf); |
|
1118 |
581
|
1119 return retval; |
|
1120 } |
|
1121 |
|
1122 DEFUN_TEXT ("who", Fwho, Swho, -1, 1, |
|
1123 "who [-all] [-builtins] [-functions] [-long] [-variables]\n\ |
|
1124 \n\ |
|
1125 List currently defined symbol(s). Options may be shortened to one\n\ |
|
1126 character, but may not be combined.") |
|
1127 { |
|
1128 Octave_object retval; |
|
1129 |
|
1130 DEFINE_ARGV("who"); |
|
1131 |
|
1132 retval = do_who (argc, argv, nargout); |
|
1133 |
529
|
1134 DELETE_ARGV; |
|
1135 |
|
1136 return retval; |
|
1137 } |
|
1138 |
581
|
1139 DEFUN_TEXT ("whos", Fwhos, Swhos, -1, 1, |
|
1140 "whos [-all] [-builtins] [-functions] [-long] [-variables]\n\ |
|
1141 \n\ |
|
1142 List currently defined symbol(s). Options may be shortened to one\n\ |
|
1143 character, but may not be combined.") |
|
1144 { |
|
1145 Octave_object retval; |
|
1146 |
|
1147 Octave_object tmp_args = args; |
|
1148 tmp_args(args.length ()) = "-long"; |
|
1149 |
|
1150 int argc = tmp_args.length (); |
|
1151 char **argv = make_argv (tmp_args, "whos"); |
|
1152 |
|
1153 if (error_state) |
|
1154 return retval; |
|
1155 |
|
1156 retval = do_who (argc, argv, nargout); |
|
1157 |
|
1158 while (--argc >= 0) |
|
1159 delete [] argv[argc]; |
|
1160 delete [] argv; |
|
1161 |
|
1162 return retval; |
|
1163 } |
|
1164 |
593
|
1165 // Install variables and functions in the symbol tables. |
529
|
1166 |
593
|
1167 void |
|
1168 install_builtin_mapper (builtin_mapper_function *mf) |
529
|
1169 { |
593
|
1170 symbol_record *sym_rec = global_sym_tab->lookup (mf->name, 1); |
|
1171 sym_rec->unprotect (); |
|
1172 |
|
1173 Mapper_fcn mfcn; |
628
|
1174 mfcn.name = strsave (mf->name); |
593
|
1175 mfcn.can_return_complex_for_real_arg = mf->can_return_complex_for_real_arg; |
|
1176 mfcn.lower_limit = mf->lower_limit; |
|
1177 mfcn.upper_limit = mf->upper_limit; |
|
1178 mfcn.d_d_mapper = mf->d_d_mapper; |
|
1179 mfcn.d_c_mapper = mf->d_c_mapper; |
|
1180 mfcn.c_c_mapper = mf->c_c_mapper; |
|
1181 |
|
1182 tree_builtin *def = new tree_builtin (2, 1, mfcn, mf->name); |
|
1183 |
|
1184 sym_rec->define (def); |
|
1185 |
|
1186 sym_rec->document (mf->help_string); |
|
1187 sym_rec->make_eternal (); |
|
1188 sym_rec->protect (); |
|
1189 } |
|
1190 |
|
1191 void |
|
1192 install_builtin_function (builtin_function *f) |
|
1193 { |
|
1194 symbol_record *sym_rec = global_sym_tab->lookup (f->name, 1); |
|
1195 sym_rec->unprotect (); |
|
1196 |
|
1197 tree_builtin *def = new tree_builtin (f->nargin_max, f->nargout_max, |
|
1198 f->fcn, f->name); |
|
1199 |
|
1200 sym_rec->define (def, f->is_text_fcn); |
|
1201 |
|
1202 sym_rec->document (f->help_string); |
|
1203 sym_rec->make_eternal (); |
|
1204 sym_rec->protect (); |
|
1205 } |
|
1206 |
|
1207 void |
|
1208 install_builtin_variable (builtin_variable *v) |
|
1209 { |
|
1210 if (v->install_as_function) |
|
1211 install_builtin_variable_as_function (v->name, v->value, v->protect, |
|
1212 v->eternal, v->help_string); |
529
|
1213 else |
593
|
1214 bind_builtin_variable (v->name, v->value, v->protect, v->eternal, |
|
1215 v->sv_function, v->help_string); |
529
|
1216 } |
|
1217 |
593
|
1218 void |
|
1219 install_builtin_variable_as_function (const char *name, tree_constant *val, |
|
1220 int protect, int eternal, |
|
1221 const char *help) |
529
|
1222 { |
593
|
1223 symbol_record *sym_rec = global_sym_tab->lookup (name, 1); |
|
1224 sym_rec->unprotect (); |
|
1225 |
|
1226 const char *tmp_help = help; |
|
1227 if (! help) |
|
1228 tmp_help = sym_rec->help (); |
|
1229 |
|
1230 sym_rec->define_as_fcn (val); |
|
1231 |
|
1232 sym_rec->document (tmp_help); |
|
1233 |
|
1234 if (protect) |
|
1235 sym_rec->protect (); |
|
1236 |
|
1237 if (eternal) |
|
1238 sym_rec->make_eternal (); |
|
1239 } |
|
1240 |
|
1241 void |
|
1242 alias_builtin (const char *alias, const char *name) |
|
1243 { |
|
1244 symbol_record *sr_name = global_sym_tab->lookup (name, 0, 0); |
|
1245 if (! sr_name) |
|
1246 panic ("can't alias to undefined name!"); |
|
1247 |
|
1248 symbol_record *sr_alias = global_sym_tab->lookup (alias, 1, 0); |
|
1249 |
|
1250 if (sr_alias) |
|
1251 sr_alias->alias (sr_name); |
|
1252 else |
|
1253 panic_impossible (); |
529
|
1254 } |
|
1255 |
593
|
1256 // Defining variables. |
|
1257 |
|
1258 void |
|
1259 bind_nargin_and_nargout (symbol_table *sym_tab, int nargin, int nargout) |
529
|
1260 { |
593
|
1261 tree_constant *tmp; |
|
1262 symbol_record *sr; |
|
1263 |
|
1264 sr = sym_tab->lookup ("nargin", 1, 0); |
|
1265 sr->unprotect (); |
|
1266 tmp = new tree_constant (nargin-1); |
|
1267 sr->define (tmp); |
|
1268 sr->protect (); |
|
1269 |
|
1270 sr = sym_tab->lookup ("nargout", 1, 0); |
|
1271 sr->unprotect (); |
|
1272 tmp = new tree_constant (nargout); |
|
1273 sr->define (tmp); |
|
1274 sr->protect (); |
|
1275 } |
|
1276 |
|
1277 // Give a global variable a definition. This will insert the symbol |
|
1278 // in the global table if necessary. |
|
1279 |
|
1280 // How is this different than install_builtin_variable? Are both |
|
1281 // functions needed? |
|
1282 |
|
1283 void |
|
1284 bind_builtin_variable (const char *varname, tree_constant *val, |
|
1285 int protect, int eternal, sv_Function sv_fcn, |
|
1286 const char *help) |
|
1287 { |
|
1288 symbol_record *sr = global_sym_tab->lookup (varname, 1, 0); |
|
1289 |
|
1290 // It is a programming error for a builtin symbol to be missing. |
|
1291 // Besides, we just inserted it, so it must be there. |
|
1292 |
|
1293 assert (sr); |
|
1294 |
|
1295 sr->unprotect (); |
|
1296 |
|
1297 // Must do this before define, since define will call the special |
|
1298 // variable function only if it knows about it, and it needs to, so |
|
1299 // that user prefs can be properly initialized. |
|
1300 |
|
1301 if (sv_fcn) |
|
1302 sr->set_sv_function (sv_fcn); |
|
1303 |
|
1304 sr->define_builtin_var (val); |
|
1305 |
|
1306 if (protect) |
|
1307 sr->protect (); |
|
1308 |
|
1309 if (eternal) |
|
1310 sr->make_eternal (); |
|
1311 |
|
1312 if (help) |
|
1313 sr->document (help); |
529
|
1314 } |
|
1315 |
593
|
1316 void |
|
1317 install_builtin_variables (void) |
529
|
1318 { |
593
|
1319 // XXX FIXME XX -- these should probably be moved to where they |
|
1320 // logically belong instead of being all grouped here. |
|
1321 |
|
1322 DEFVAR ("EDITOR", SBV_EDITOR, editor, 0, 0, 1, sv_editor, |
|
1323 "name of the editor to be invoked by the edit_history command"); |
|
1324 |
|
1325 DEFVAR ("I", SBV_I, Complex (0.0, 1.0), 0, 1, 1, 0, |
|
1326 "sqrt (-1)"); |
|
1327 |
|
1328 DEFVAR ("Inf", SBV_Inf, octave_Inf, 0, 1, 1, 0, |
|
1329 "infinity"); |
|
1330 |
|
1331 DEFVAR ("INFO_FILE", SBV_INFO_FILE, info_file, 0, 0, 1, sv_info_file, |
|
1332 "name of the Octave info file"); |
|
1333 |
|
1334 DEFVAR ("J", SBV_J, Complex (0.0, 1.0), 0, 1, 1, 0, |
|
1335 "sqrt (-1)"); |
529
|
1336 |
593
|
1337 #if defined (HAVE_ISNAN) |
|
1338 DEFVAR ("NaN", SBV_NaN, octave_NaN, 0, 1, 1, 0, |
|
1339 "not a number"); |
|
1340 #endif |
|
1341 |
|
1342 DEFVAR ("LOADPATH", SBV_LOADPATH, load_path, 0, 0, 1, sv_loadpath, |
|
1343 "colon separated list of directories to search for scripts"); |
|
1344 |
664
|
1345 DEFVAR ("OCTAVE_VERSION", SBV_version, version_string, 0, 1, 1, 0, |
|
1346 "Octave version"); |
|
1347 |
593
|
1348 DEFVAR ("PAGER", SBV_PAGER, default_pager (), 0, 0, 1, sv_pager_binary, |
|
1349 "path to pager binary"); |
|
1350 |
|
1351 DEFVAR ("PS1", SBV_PS1, "\\s:\\#> ", 0, 0, 1, sv_ps1, |
|
1352 "primary prompt string"); |
|
1353 |
|
1354 DEFVAR ("PS2", SBV_PS2, "> ", 0, 0, 1, sv_ps2, |
|
1355 "secondary prompt string"); |
|
1356 |
|
1357 DEFVAR ("PWD", SBV_PWD, get_working_directory ("initialize_globals"), |
|
1358 0, 1, 1, sv_pwd, |
|
1359 "current working directory"); |
|
1360 |
|
1361 DEFVAR ("SEEK_SET", SBV_SEEK_SET, 0.0, 0, 1, 1, 0, |
|
1362 "used with fseek to position file relative to the beginning"); |
529
|
1363 |
593
|
1364 DEFVAR ("SEEK_CUR", SBV_SEEK_CUR, 1.0, 0, 1, 1, 0, |
|
1365 "used with fseek to position file relative to the current position"); |
|
1366 |
|
1367 DEFVAR ("SEEK_END", SBV_SEEK_END, 2.0, 0, 1, 1, 0, |
|
1368 "used with fseek to position file relative to the end"); |
|
1369 |
|
1370 DEFVAR ("ans", SBV_ans, , 0, 0, 1, 0, |
|
1371 ""); |
|
1372 |
661
|
1373 DEFVAR ("automatic_replot", SBV_automatic_replot, "false", |
|
1374 0, 0, 1, automatic_replot, |
|
1375 "if true, auto-insert a replot command when a plot changes"); |
|
1376 |
593
|
1377 DEFVAR ("commas_in_literal_matrix", SBV_commas_in_literal_matrix, "", |
|
1378 0, 0, 1, commas_in_literal_matrix, |
|
1379 "control auto-insertion of commas in literal matrices"); |
|
1380 |
605
|
1381 DEFVAR ("default_save_format", SBV_default_save_format, "ascii", |
|
1382 0, 0, 1, sv_default_save_format, |
|
1383 "default format for files created with save, may be either\n\ |
|
1384 \"binary\" or \"text\""); |
|
1385 |
593
|
1386 DEFVAR ("do_fortran_indexing", SBV_do_fortran_indexing, "false", 0, 0, |
|
1387 1, do_fortran_indexing, |
|
1388 "allow single indices for matrices"); |
|
1389 |
|
1390 DEFVAR ("empty_list_elements_ok", SBV_empty_list_elements_ok, "warn", |
|
1391 0, 0, 1, empty_list_elements_ok, |
|
1392 "ignore the empty element in expressions like `a = [[], 1]'"); |
529
|
1393 |
593
|
1394 DEFVAR ("eps", SBV_eps, DBL_EPSILON, 0, 1, 1, 0, |
|
1395 "machine precision"); |
|
1396 |
|
1397 DEFVAR ("gnuplot_binary", SBV_gnuplot_binary, "gnuplot", 0, 0, 1, |
|
1398 sv_gnuplot_binary, |
|
1399 "path to gnuplot binary"); |
|
1400 |
|
1401 DEFVAR ("i", SBV_i, Complex (0.0, 1.0), 1, 1, 1, 0, |
|
1402 "sqrt (-1)"); |
529
|
1403 |
593
|
1404 DEFVAR ("ignore_function_time_stamp", SBV_ignore_function_time_stamp, |
|
1405 "system", 0, 0, 1, |
|
1406 ignore_function_time_stamp, |
|
1407 "don't check to see if function files have changed since they were\n\ |
|
1408 last compiled. Possible values are \"system\" and \"all\""); |
|
1409 |
|
1410 DEFVAR ("implicit_str_to_num_ok", SBV_implicit_str_to_num_ok, "false", |
|
1411 0, 0, 1, implicit_str_to_num_ok, |
|
1412 "allow implicit string to number conversion"); |
|
1413 |
|
1414 DEFVAR ("inf", SBV_inf, octave_Inf, 0, 1, 1, 0, |
|
1415 "infinity"); |
|
1416 |
|
1417 DEFVAR ("j", SBV_j, Complex (0.0, 1.0), 1, 1, 1, 0, |
|
1418 "sqrt (-1)"); |
529
|
1419 |
593
|
1420 #if defined (HAVE_ISNAN) |
|
1421 DEFVAR ("nan", SBV_nan, octave_NaN, 0, 1, 1, 0, |
|
1422 "not a number"); |
|
1423 #endif |
|
1424 |
|
1425 DEFVAR ("ok_to_lose_imaginary_part", SBV_ok_to_lose_imaginary_part, |
|
1426 "warn", 0, 0, 1, ok_to_lose_imaginary_part, |
|
1427 "silently convert from complex to real by dropping imaginary part"); |
|
1428 |
|
1429 DEFVAR ("output_max_field_width", SBV_output_max_field_width, 10.0, 0, |
|
1430 0, 1, set_output_max_field_width, |
|
1431 "maximum width of an output field for numeric output"); |
|
1432 |
|
1433 DEFVAR ("output_precision", SBV_output_precision, 5.0, 0, 0, 1, |
|
1434 set_output_precision, |
|
1435 "number of significant figures to display for numeric output"); |
|
1436 |
|
1437 DEFVAR ("page_screen_output", SBV_page_screen_output, "true", 0, 0, 1, |
|
1438 page_screen_output, |
|
1439 "if possible, send output intended for the screen through the pager"); |
529
|
1440 |
593
|
1441 DEFVAR ("pi", SBV_pi, 4.0 * atan (1.0), 0, 1, 1, 0, |
|
1442 "ratio of the circumference of a circle to its diameter"); |
|
1443 |
|
1444 DEFVAR ("prefer_column_vectors", SBV_prefer_column_vectors, "true", 0, |
|
1445 0, 1, prefer_column_vectors, |
|
1446 "prefer column/row vectors"); |
|
1447 |
|
1448 DEFVAR ("prefer_zero_one_indexing", SBV_prefer_zero_one_indexing, |
|
1449 "false", 0, 0, 1, prefer_zero_one_indexing, |
|
1450 "when there is a conflict, prefer zero-one style indexing"); |
|
1451 |
|
1452 DEFVAR ("print_answer_id_name", SBV_print_answer_id_name, "true", 0, |
|
1453 0, 1, print_answer_id_name, |
|
1454 "set output style to print `var_name = ...'"); |
|
1455 |
|
1456 DEFVAR ("print_empty_dimensions", SBV_print_empty_dimensions, "true", |
|
1457 0, 0, 1, print_empty_dimensions, |
|
1458 "also print dimensions of empty matrices"); |
|
1459 |
|
1460 DEFVAR ("propagate_empty_matrices", SBV_propagate_empty_matrices, |
|
1461 "true", 0, 0, 1, propagate_empty_matrices, |
|
1462 "operations on empty matrices return an empty matrix, not an error"); |
529
|
1463 |
593
|
1464 DEFVAR ("resize_on_range_error", SBV_resize_on_range_error, "true", 0, |
|
1465 0, 1, resize_on_range_error, |
|
1466 "enlarge matrices on assignment"); |
|
1467 |
|
1468 DEFVAR ("return_last_computed_value", SBV_return_last_computed_value, |
|
1469 "false", 0, 0, 1, |
|
1470 return_last_computed_value, |
|
1471 "if a function does not return any values explicitly, return the\n\ |
|
1472 last computed value"); |
|
1473 |
|
1474 DEFVAR ("save_precision", SBV_save_precision, 17.0, 0, 0, 1, |
|
1475 set_save_precision, |
|
1476 "number of significant figures kept by the ASCII save command"); |
|
1477 |
|
1478 DEFVAR ("silent_functions", SBV_silent_functions, "false", 0, 0, 1, |
|
1479 silent_functions, |
|
1480 "suppress printing results in called functions"); |
|
1481 |
|
1482 DEFVAR ("split_long_rows", SBV_split_long_rows, "true", 0, 0, 1, |
|
1483 split_long_rows, |
|
1484 "split long matrix rows instead of wrapping"); |
529
|
1485 |
593
|
1486 DEFVAR ("stdin", SBV_stdin, 0.0, 0, 1, 1, 0, |
|
1487 "file number of the standard input stream"); |
|
1488 |
|
1489 DEFVAR ("stdout", SBV_stdout, 1.0, 0, 1, 1, 0, |
|
1490 "file number of the standard output stream"); |
|
1491 |
|
1492 DEFVAR ("stderr", SBV_stderr, 2.0, 0, 1, 1, 0, |
|
1493 "file number of the standard error stream"); |
|
1494 |
|
1495 DEFVAR ("treat_neg_dim_as_zero", SBV_treat_neg_dim_as_zero, "false", |
|
1496 0, 0, 1, treat_neg_dim_as_zero, |
|
1497 "convert negative dimensions to zero"); |
|
1498 |
|
1499 DEFVAR ("warn_assign_as_truth_value", SBV_warn_assign_as_truth_value, |
|
1500 "true", 0, 0, 1, |
|
1501 warn_assign_as_truth_value, |
|
1502 "produce warning for assignments used as truth values"); |
|
1503 |
|
1504 DEFVAR ("warn_comma_in_global_decl", SBV_warn_comma_in_global_decl, |
|
1505 "true", 0, 0, 1, warn_comma_in_global_decl, |
|
1506 "produce warning for commas in global declarations"); |
|
1507 |
|
1508 DEFVAR ("warn_divide_by_zero", SBV_warn_divide_by_zero, "true", 0, 0, |
|
1509 1, warn_divide_by_zero, |
|
1510 "on IEEE machines, allow divide by zero errors to be suppressed"); |
529
|
1511 } |
|
1512 |
593
|
1513 // Deleting names from the symbol tables. |
|
1514 |
|
1515 DEFUN_TEXT ("clear", Fclear, Sclear, -1, 1, |
|
1516 "clear [name ...]\n\ |
|
1517 \n\ |
|
1518 clear symbol(s) matching a list of globbing patterns\n\ |
|
1519 if no arguments are given, clear all user-defined variables and functions") |
529
|
1520 { |
593
|
1521 Octave_object retval; |
|
1522 |
|
1523 DEFINE_ARGV("clear"); |
|
1524 |
|
1525 // Always clear the local table, but don't clear currently compiled |
|
1526 // functions unless we are at the top level. (Allowing that to happen |
|
1527 // inside functions would result in pretty odd behavior...) |
|
1528 |
|
1529 int clear_user_functions = (curr_sym_tab == top_level_sym_tab); |
|
1530 |
|
1531 if (argc == 1) |
|
1532 { |
|
1533 curr_sym_tab->clear (); |
|
1534 global_sym_tab->clear (clear_user_functions); |
|
1535 } |
529
|
1536 else |
|
1537 { |
593
|
1538 int lcount; |
|
1539 char **lvars = curr_sym_tab->list (lcount, 0, |
|
1540 symbol_def::USER_VARIABLE, |
|
1541 SYMTAB_LOCAL_SCOPE); |
|
1542 int gcount; |
|
1543 char **gvars = curr_sym_tab->list (gcount, 0, |
|
1544 symbol_def::USER_VARIABLE, |
|
1545 SYMTAB_GLOBAL_SCOPE); |
|
1546 int fcount; |
|
1547 char **fcns = curr_sym_tab->list (fcount, 0, |
|
1548 symbol_def::USER_FUNCTION, |
|
1549 SYMTAB_ALL_SCOPES); |
529
|
1550 |
593
|
1551 while (--argc > 0) |
|
1552 { |
|
1553 argv++; |
|
1554 if (*argv) |
|
1555 { |
|
1556 int i; |
|
1557 for (i = 0; i < lcount; i++) |
|
1558 { |
|
1559 if (fnmatch (*argv, lvars[i], __FNM_FLAGS) == 0) |
|
1560 curr_sym_tab->clear (lvars[i]); |
|
1561 } |
529
|
1562 |
593
|
1563 int count; |
|
1564 for (i = 0; i < gcount; i++) |
|
1565 { |
|
1566 if (fnmatch (*argv, gvars[i], __FNM_FLAGS) == 0) |
|
1567 { |
|
1568 count = curr_sym_tab->clear (gvars[i]); |
|
1569 if (count > 0) |
|
1570 global_sym_tab->clear (gvars[i], clear_user_functions); |
|
1571 } |
|
1572 } |
529
|
1573 |
593
|
1574 for (i = 0; i < fcount; i++) |
|
1575 { |
|
1576 if (fnmatch (*argv, fcns[i], __FNM_FLAGS) == 0) |
|
1577 { |
|
1578 count = curr_sym_tab->clear (fcns[i]); |
|
1579 if (count > 0) |
|
1580 global_sym_tab->clear (fcns[i], clear_user_functions); |
|
1581 } |
|
1582 } |
|
1583 } |
|
1584 } |
529
|
1585 |
593
|
1586 delete [] lvars; |
|
1587 delete [] gvars; |
|
1588 delete [] fcns; |
|
1589 |
|
1590 } |
|
1591 |
|
1592 DELETE_ARGV; |
|
1593 |
|
1594 return retval; |
529
|
1595 } |
|
1596 |
1
|
1597 /* |
|
1598 ;;; Local Variables: *** |
|
1599 ;;; mode: C++ *** |
|
1600 ;;; page-delimiter: "^/\\*" *** |
|
1601 ;;; End: *** |
|
1602 */ |