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