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