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