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