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