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