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); |
|
709 |
|
710 echo_input = 0; |
|
711 saving_history = 0; |
|
712 reading_fcn_file = 1; |
|
713 |
|
714 YY_BUFFER_STATE old_buf = current_buffer (); |
|
715 YY_BUFFER_STATE new_buf = create_buffer (ffile); |
|
716 |
|
717 add_unwind_protect (restore_input_buffer, (void *) old_buf); |
|
718 add_unwind_protect (delete_input_buffer, (void *) new_buf); |
|
719 |
|
720 switch_to_buffer (new_buf); |
|
721 |
|
722 unwind_protect_ptr (curr_sym_tab); |
|
723 |
|
724 reset_parser (); |
|
725 |
991
|
726 delete [] help_buf; |
|
727 help_buf = tmp_help_txt; |
|
728 |
581
|
729 int status = yyparse (); |
|
730 |
|
731 if (status != 0) |
|
732 { |
|
733 error ("parse error while reading function file %s", ff); |
|
734 global_sym_tab->clear (curr_fcn_file_name); |
|
735 } |
|
736 } |
|
737 else if (exec_script) |
|
738 { |
1271
|
739 // We don't need this now. |
|
740 |
|
741 delete [] tmp_help_txt; |
|
742 |
|
743 // The value of `reading_fcn_file' will be restored to the |
|
744 // proper value when we unwind from this frame. |
|
745 |
581
|
746 reading_fcn_file = old_reading_fcn_file_state; |
|
747 |
|
748 unwind_protect_int (reading_script_file); |
|
749 reading_script_file = 1; |
|
750 |
|
751 parse_and_execute (ffile, 1); |
|
752 |
|
753 script_file_executed = 1; |
|
754 } |
|
755 fclose (ffile); |
|
756 } |
|
757 |
|
758 run_unwind_frame ("parse_fcn_file"); |
|
759 |
|
760 return script_file_executed; |
|
761 } |
|
762 |
593
|
763 static int |
581
|
764 load_fcn_from_file (symbol_record *sym_rec, int exec_script) |
|
765 { |
|
766 int script_file_executed = 0; |
|
767 |
|
768 char *nm = sym_rec->name (); |
|
769 |
1271
|
770 // This is needed by yyparse. |
704
|
771 |
581
|
772 curr_fcn_file_name = nm; |
|
773 |
704
|
774 #ifdef WITH_DLD |
581
|
775 |
704
|
776 if (load_octave_oct_file (nm)) |
|
777 { |
|
778 force_link_to_function (nm); |
|
779 } |
|
780 else |
581
|
781 |
704
|
782 #endif |
581
|
783 |
|
784 { |
704
|
785 char *ff = fcn_file_in_path (nm); |
581
|
786 |
|
787 if (ff) |
|
788 { |
|
789 script_file_executed = parse_fcn_file (exec_script, ff); |
|
790 delete [] ff; |
|
791 } |
|
792 |
|
793 if (! (error_state || script_file_executed)) |
|
794 force_link_to_function (nm); |
|
795 } |
|
796 |
|
797 return script_file_executed; |
|
798 } |
|
799 |
|
800 int |
|
801 lookup (symbol_record *sym_rec, int exec_script) |
|
802 { |
1271
|
803 int script_executed = 0; |
581
|
804 |
|
805 if (! sym_rec->is_linked_to_global ()) |
|
806 { |
|
807 if (sym_rec->is_defined ()) |
|
808 { |
|
809 if (sym_rec->is_function () && symbol_out_of_date (sym_rec)) |
1271
|
810 script_executed = load_fcn_from_file (sym_rec, exec_script); |
581
|
811 } |
|
812 else if (! sym_rec->is_formal_parameter ()) |
|
813 { |
|
814 link_to_builtin_or_function (sym_rec); |
1271
|
815 |
581
|
816 if (! sym_rec->is_defined ()) |
1271
|
817 script_executed = load_fcn_from_file (sym_rec, exec_script); |
581
|
818 else if (sym_rec->is_function () && symbol_out_of_date (sym_rec)) |
1271
|
819 script_executed = load_fcn_from_file (sym_rec, exec_script); |
581
|
820 } |
|
821 } |
|
822 |
1271
|
823 return script_executed; |
581
|
824 } |
|
825 |
|
826 // Get the symbol record for the given name that is visible in the |
|
827 // current scope. Reread any function definitions that appear to be |
|
828 // out of date. If a function is available in a file but is not |
|
829 // currently loaded, this will load it and insert the name in the |
|
830 // current symbol table. |
|
831 |
|
832 symbol_record * |
|
833 lookup_by_name (const char *nm, int exec_script) |
|
834 { |
|
835 symbol_record *sym_rec = curr_sym_tab->lookup (nm, 1, 0); |
|
836 |
|
837 lookup (sym_rec, exec_script); |
|
838 |
|
839 return sym_rec; |
|
840 } |
|
841 |
991
|
842 char * |
993
|
843 get_help_from_file (const char *path) |
991
|
844 { |
|
845 if (path && *path) |
|
846 { |
|
847 FILE *fptr = fopen (path, "r"); |
|
848 if (fptr) |
|
849 { |
1418
|
850 char *help_txt = gobble_leading_white_space (fptr, 1); |
991
|
851 fclose (fptr); |
|
852 return help_txt; |
|
853 } |
|
854 } |
|
855 return 0; |
|
856 } |
|
857 |
593
|
858 // Variable values. |
195
|
859 |
581
|
860 // Look for the given name in the global symbol table. If it refers |
|
861 // to a string, return a new copy. If not, return 0; |
|
862 |
1
|
863 char * |
195
|
864 builtin_string_variable (const char *name) |
1
|
865 { |
195
|
866 symbol_record *sr = global_sym_tab->lookup (name, 0, 0); |
|
867 |
1271
|
868 // It is a prorgramming error to look for builtins that aren't. |
195
|
869 |
529
|
870 assert (sr); |
195
|
871 |
529
|
872 char *retval = 0; |
1
|
873 |
490
|
874 tree_fvc *defn = sr->def (); |
195
|
875 |
529
|
876 if (defn) |
1
|
877 { |
|
878 tree_constant val = defn->eval (0); |
195
|
879 |
610
|
880 if (! error_state && val.is_string ()) |
1
|
881 { |
1363
|
882 const char *s = val.string_value (); |
636
|
883 |
529
|
884 if (s) |
1
|
885 retval = strsave (s); |
|
886 } |
|
887 } |
|
888 |
|
889 return retval; |
|
890 } |
|
891 |
581
|
892 // Look for the given name in the global symbol table. If it refers |
1504
|
893 // to a real scalar, place the value in d and return 1. Otherwise, |
|
894 // return 0. |
581
|
895 |
1
|
896 int |
195
|
897 builtin_real_scalar_variable (const char *name, double& d) |
1
|
898 { |
1504
|
899 int status = 0; |
195
|
900 symbol_record *sr = global_sym_tab->lookup (name, 0, 0); |
|
901 |
1271
|
902 // It is a prorgramming error to look for builtins that aren't. |
195
|
903 |
529
|
904 assert (sr); |
1
|
905 |
490
|
906 tree_fvc *defn = sr->def (); |
195
|
907 |
529
|
908 if (defn) |
1
|
909 { |
|
910 tree_constant val = defn->eval (0); |
195
|
911 |
598
|
912 if (! error_state && val.is_scalar_type ()) |
1
|
913 { |
|
914 d = val.double_value (); |
1504
|
915 status = 1; |
1
|
916 } |
|
917 } |
|
918 |
|
919 return status; |
|
920 } |
|
921 |
1093
|
922 // Look for the given name in the global symbol table. |
|
923 |
|
924 tree_constant |
|
925 builtin_any_variable (const char *name) |
|
926 { |
|
927 tree_constant retval; |
|
928 |
|
929 symbol_record *sr = global_sym_tab->lookup (name, 0, 0); |
|
930 |
1271
|
931 // It is a prorgramming error to look for builtins that aren't. |
1093
|
932 |
|
933 assert (sr); |
|
934 |
|
935 tree_fvc *defn = sr->def (); |
|
936 |
|
937 if (defn) |
|
938 retval = defn->eval (0); |
|
939 |
|
940 return retval; |
|
941 } |
|
942 |
593
|
943 // Global stuff and links to builtin variables and functions. |
|
944 |
581
|
945 // Make the definition of the symbol record sr be the same as the |
|
946 // definition of the global variable of the same name, creating it if |
1418
|
947 // it doesn't already exist. |
581
|
948 |
195
|
949 void |
|
950 link_to_global_variable (symbol_record *sr) |
|
951 { |
|
952 if (sr->is_linked_to_global ()) |
|
953 return; |
|
954 |
|
955 symbol_record *gsr = global_sym_tab->lookup (sr->name (), 1, 0); |
|
956 |
|
957 if (sr->is_formal_parameter ()) |
|
958 { |
|
959 error ("can't make function parameter `%s' global", sr->name ()); |
|
960 return; |
|
961 } |
|
962 |
1271
|
963 // There must be a better way to do this. XXX FIXME XXX |
195
|
964 |
|
965 if (sr->is_variable ()) |
|
966 { |
1271
|
967 // Would be nice not to have this cast. XXX FIXME XXX |
|
968 |
195
|
969 tree_constant *tmp = (tree_constant *) sr->def (); |
529
|
970 if (tmp) |
|
971 tmp = new tree_constant (*tmp); |
|
972 else |
207
|
973 tmp = new tree_constant (); |
195
|
974 gsr->define (tmp); |
|
975 } |
|
976 else |
529
|
977 sr->clear (); |
195
|
978 |
1271
|
979 // If the global symbol is currently defined as a function, we need |
|
980 // to hide it with a variable. |
195
|
981 |
|
982 if (gsr->is_function ()) |
529
|
983 gsr->define ((tree_constant *) 0); |
195
|
984 |
|
985 sr->alias (gsr, 1); |
|
986 sr->mark_as_linked_to_global (); |
|
987 } |
|
988 |
581
|
989 // Make the definition of the symbol record sr be the same as the |
|
990 // definition of the builtin variable of the same name. |
|
991 |
195
|
992 void |
|
993 link_to_builtin_variable (symbol_record *sr) |
|
994 { |
|
995 symbol_record *tmp_sym = global_sym_tab->lookup (sr->name (), 0, 0); |
|
996 |
529
|
997 if (tmp_sym && tmp_sym->is_builtin_variable ()) |
|
998 sr->alias (tmp_sym); |
195
|
999 } |
|
1000 |
581
|
1001 // Make the definition of the symbol record sr be the same as the |
|
1002 // definition of the builtin variable or function, or user function of |
|
1003 // the same name, provided that the name has not been used as a formal |
|
1004 // parameter. |
|
1005 |
195
|
1006 void |
|
1007 link_to_builtin_or_function (symbol_record *sr) |
|
1008 { |
|
1009 symbol_record *tmp_sym = global_sym_tab->lookup (sr->name (), 0, 0); |
|
1010 |
529
|
1011 if (tmp_sym |
|
1012 && (tmp_sym->is_builtin_variable () || tmp_sym->is_function ()) |
|
1013 && ! tmp_sym->is_formal_parameter ()) |
|
1014 sr->alias (tmp_sym); |
195
|
1015 } |
|
1016 |
581
|
1017 // Force a link to a function in the current symbol table. This is |
|
1018 // used just after defining a function to avoid different behavior |
|
1019 // depending on whether or not the function has been evaluated after |
|
1020 // being defined. |
|
1021 // |
|
1022 // Return without doing anything if there isn't a function with the |
|
1023 // given name defined in the global symbol table. |
|
1024 |
195
|
1025 void |
|
1026 force_link_to_function (const char *id_name) |
|
1027 { |
|
1028 symbol_record *gsr = global_sym_tab->lookup (id_name, 1, 0); |
|
1029 if (gsr->is_function ()) |
|
1030 { |
|
1031 curr_sym_tab->clear (id_name); |
|
1032 symbol_record *csr = curr_sym_tab->lookup (id_name, 1, 0); |
|
1033 csr->alias (gsr); |
|
1034 } |
|
1035 } |
|
1036 |
605
|
1037 // Help stuff. Shouldn't this go in help.cc? |
593
|
1038 |
|
1039 // It's not likely that this does the right thing now. XXX FIXME XXX |
|
1040 |
|
1041 char ** |
|
1042 make_name_list (void) |
|
1043 { |
|
1044 int key_len = 0; |
|
1045 int glb_len = 0; |
|
1046 int top_len = 0; |
|
1047 int lcl_len = 0; |
|
1048 int ffl_len = 0; |
|
1049 |
|
1050 char **key = 0; |
|
1051 char **glb = 0; |
|
1052 char **top = 0; |
|
1053 char **lcl = 0; |
|
1054 char **ffl = 0; |
|
1055 |
1271
|
1056 // Each of these functions returns a new vector of pointers to new |
|
1057 // strings. |
593
|
1058 |
|
1059 key = names (keyword_help (), key_len); |
|
1060 glb = global_sym_tab->list (glb_len); |
|
1061 top = top_level_sym_tab->list (top_len); |
|
1062 if (top_level_sym_tab != curr_sym_tab) |
|
1063 lcl = curr_sym_tab->list (lcl_len); |
|
1064 ffl = get_fcn_file_names (ffl_len, 1); |
|
1065 |
|
1066 int total_len = key_len + glb_len + top_len + lcl_len + ffl_len; |
|
1067 |
|
1068 char **list = new char * [total_len+1]; |
1418
|
1069 |
1271
|
1070 // Put all the symbols in one big list. Only copy pointers, not the |
|
1071 // strings they point to, then only delete the original array of |
|
1072 // pointers, and not the strings they point to. |
593
|
1073 |
|
1074 int j = 0; |
|
1075 int i = 0; |
|
1076 for (i = 0; i < key_len; i++) |
|
1077 list[j++] = key[i]; |
|
1078 |
|
1079 for (i = 0; i < glb_len; i++) |
|
1080 list[j++] = glb[i]; |
|
1081 |
|
1082 for (i = 0; i < top_len; i++) |
|
1083 list[j++] = top[i]; |
|
1084 |
|
1085 for (i = 0; i < lcl_len; i++) |
|
1086 list[j++] = lcl[i]; |
|
1087 |
|
1088 for (i = 0; i < ffl_len; i++) |
|
1089 list[j++] = ffl[i]; |
|
1090 |
|
1091 list[j] = 0; |
|
1092 |
|
1093 delete [] key; |
|
1094 delete [] glb; |
|
1095 delete [] top; |
|
1096 delete [] lcl; |
|
1097 delete [] ffl; |
|
1098 |
|
1099 return list; |
|
1100 } |
|
1101 |
|
1102 // List variable names. |
|
1103 |
|
1104 static void |
|
1105 print_symbol_info_line (ostrstream& output_buf, const symbol_record_info& s) |
|
1106 { |
|
1107 output_buf << (s.is_read_only () ? " -" : " w"); |
|
1108 output_buf << (s.is_eternal () ? "- " : "d "); |
|
1109 #if 0 |
|
1110 output_buf << (s.hides_fcn () ? "f" : (s.hides_builtin () ? "F" : "-")); |
|
1111 #endif |
|
1112 output_buf.form (" %-16s", s.type_as_string ()); |
|
1113 if (s.is_function ()) |
|
1114 output_buf << " - -"; |
|
1115 else |
|
1116 { |
|
1117 output_buf.form ("%7d", s.rows ()); |
|
1118 output_buf.form ("%7d", s.columns ()); |
|
1119 } |
|
1120 output_buf << " " << s.name () << "\n"; |
|
1121 } |
|
1122 |
|
1123 static void |
|
1124 print_long_listing (ostrstream& output_buf, symbol_record_info *s) |
|
1125 { |
|
1126 if (! s) |
|
1127 return; |
|
1128 |
|
1129 symbol_record_info *ptr = s; |
|
1130 while (ptr->is_defined ()) |
|
1131 { |
|
1132 print_symbol_info_line (output_buf, *ptr); |
|
1133 ptr++; |
|
1134 } |
|
1135 } |
|
1136 |
|
1137 static int |
867
|
1138 maybe_list (const char *header, char **argv, int argc, |
|
1139 ostrstream& output_buf, int show_verbose, symbol_table |
|
1140 *sym_tab, unsigned type, unsigned scope) |
593
|
1141 { |
|
1142 int count; |
|
1143 int status = 0; |
|
1144 if (show_verbose) |
|
1145 { |
|
1146 symbol_record_info *symbols; |
867
|
1147 symbols = sym_tab->long_list (count, argv, argc, 1, type, scope); |
593
|
1148 if (symbols && count > 0) |
|
1149 { |
|
1150 output_buf << "\n" << header << "\n\n" |
|
1151 << "prot type rows cols name\n" |
|
1152 << "==== ==== ==== ==== ====\n"; |
|
1153 |
|
1154 print_long_listing (output_buf, symbols); |
|
1155 status = 1; |
|
1156 } |
|
1157 delete [] symbols; |
|
1158 } |
|
1159 else |
|
1160 { |
867
|
1161 char **symbols = sym_tab->list (count, argv, argc, 1, type, scope); |
593
|
1162 if (symbols && count > 0) |
|
1163 { |
|
1164 output_buf << "\n" << header << "\n\n"; |
|
1165 list_in_columns (output_buf, symbols); |
|
1166 status = 1; |
|
1167 } |
|
1168 delete [] symbols; |
|
1169 } |
|
1170 return status; |
|
1171 } |
|
1172 |
1488
|
1173 DEFUN_TEXT ("document", Fdocument, Sdocument, 10, |
593
|
1174 "document symbol string ...\n\ |
|
1175 \n\ |
|
1176 Associate a cryptic message with a variable name.") |
|
1177 { |
|
1178 Octave_object retval; |
|
1179 |
|
1180 DEFINE_ARGV("document"); |
|
1181 |
|
1182 if (argc == 3) |
|
1183 { |
|
1184 char *name = argv[1]; |
|
1185 char *help = argv[2]; |
|
1186 |
|
1187 if (is_builtin_variable (name)) |
|
1188 error ("sorry, can't redefine help for builtin variables"); |
|
1189 else |
|
1190 { |
|
1191 symbol_record *sym_rec = curr_sym_tab->lookup (name, 0); |
|
1192 |
|
1193 if (sym_rec) |
|
1194 sym_rec->document (help); |
|
1195 else |
|
1196 error ("document: no such symbol `%s'", name); |
|
1197 } |
|
1198 } |
|
1199 else |
|
1200 print_usage ("document"); |
|
1201 |
|
1202 DELETE_ARGV; |
|
1203 |
|
1204 return retval; |
|
1205 } |
|
1206 |
584
|
1207 // XXX FIXME XXX -- this should take a list of regular expressions |
|
1208 // naming the variables to look for. |
|
1209 |
581
|
1210 static Octave_object |
1488
|
1211 do_who (int argc, char **argv) |
529
|
1212 { |
|
1213 Octave_object retval; |
|
1214 |
|
1215 int show_builtins = 0; |
|
1216 int show_functions = (curr_sym_tab == top_level_sym_tab); |
|
1217 int show_variables = 1; |
|
1218 int show_verbose = 0; |
|
1219 |
584
|
1220 char *my_name = argv[0]; |
|
1221 |
529
|
1222 if (argc > 1) |
|
1223 { |
|
1224 show_functions = 0; |
|
1225 show_variables = 0; |
|
1226 } |
|
1227 |
867
|
1228 while (--argc > 0) |
529
|
1229 { |
|
1230 argv++; |
867
|
1231 |
529
|
1232 if (strcmp (*argv, "-all") == 0 || strcmp (*argv, "-a") == 0) |
|
1233 { |
|
1234 show_builtins++; |
|
1235 show_functions++; |
1418
|
1236 show_variables++; |
529
|
1237 } |
605
|
1238 else if (strcmp (*argv, "-builtins") == 0 || strcmp (*argv, "-b") == 0) |
529
|
1239 show_builtins++; |
605
|
1240 else if (strcmp (*argv, "-functions") == 0 || strcmp (*argv, "-f") == 0) |
529
|
1241 show_functions++; |
605
|
1242 else if (strcmp (*argv, "-long") == 0 || strcmp (*argv, "-l") == 0) |
|
1243 show_verbose++; |
|
1244 else if (strcmp (*argv, "-variables") == 0 || strcmp (*argv, "-v") == 0) |
529
|
1245 show_variables++; |
867
|
1246 else if (*argv[0] == '-') |
|
1247 warning ("%s: unrecognized option `%s'", my_name, *argv); |
529
|
1248 else |
867
|
1249 break; |
529
|
1250 } |
|
1251 |
1271
|
1252 // If the user specified -l and nothing else, show variables. If |
|
1253 // evaluating this at the top level, also show functions. |
529
|
1254 |
|
1255 if (show_verbose && ! (show_builtins || show_functions || show_variables)) |
|
1256 { |
|
1257 show_functions = (curr_sym_tab == top_level_sym_tab); |
|
1258 show_variables = 1; |
|
1259 } |
|
1260 |
|
1261 ostrstream output_buf; |
|
1262 int pad_after = 0; |
|
1263 |
|
1264 if (show_builtins) |
|
1265 { |
867
|
1266 pad_after += maybe_list ("*** built-in variables:", argv, argc, |
529
|
1267 output_buf, show_verbose, global_sym_tab, |
|
1268 symbol_def::BUILTIN_VARIABLE, |
|
1269 SYMTAB_ALL_SCOPES); |
|
1270 |
867
|
1271 pad_after += maybe_list ("*** built-in functions:", argv, argc, |
529
|
1272 output_buf, show_verbose, global_sym_tab, |
|
1273 symbol_def::BUILTIN_FUNCTION, |
|
1274 SYMTAB_ALL_SCOPES); |
|
1275 } |
|
1276 |
|
1277 if (show_functions) |
|
1278 { |
|
1279 pad_after += maybe_list ("*** currently compiled functions:", |
867
|
1280 argv, argc, output_buf, show_verbose, |
|
1281 global_sym_tab, symbol_def::USER_FUNCTION, |
529
|
1282 SYMTAB_ALL_SCOPES); |
|
1283 } |
|
1284 |
|
1285 if (show_variables) |
|
1286 { |
867
|
1287 pad_after += maybe_list ("*** local user variables:", argv, argc, |
529
|
1288 output_buf, show_verbose, curr_sym_tab, |
|
1289 symbol_def::USER_VARIABLE, |
1418
|
1290 SYMTAB_LOCAL_SCOPE); |
529
|
1291 |
|
1292 pad_after += maybe_list ("*** globally visible user variables:", |
867
|
1293 argv, argc, output_buf, show_verbose, |
|
1294 curr_sym_tab, symbol_def::USER_VARIABLE, |
529
|
1295 SYMTAB_GLOBAL_SCOPE); |
|
1296 } |
|
1297 |
|
1298 if (pad_after) |
|
1299 output_buf << "\n"; |
|
1300 |
|
1301 output_buf << ends; |
|
1302 maybe_page_output (output_buf); |
|
1303 |
581
|
1304 return retval; |
|
1305 } |
|
1306 |
1488
|
1307 DEFUN_TEXT ("who", Fwho, Swho, 10, |
581
|
1308 "who [-all] [-builtins] [-functions] [-long] [-variables]\n\ |
|
1309 \n\ |
|
1310 List currently defined symbol(s). Options may be shortened to one\n\ |
|
1311 character, but may not be combined.") |
|
1312 { |
|
1313 Octave_object retval; |
|
1314 |
|
1315 DEFINE_ARGV("who"); |
|
1316 |
1488
|
1317 retval = do_who (argc, argv); |
581
|
1318 |
529
|
1319 DELETE_ARGV; |
|
1320 |
|
1321 return retval; |
|
1322 } |
|
1323 |
1488
|
1324 DEFUN_TEXT ("whos", Fwhos, Swhos, 10, |
581
|
1325 "whos [-all] [-builtins] [-functions] [-long] [-variables]\n\ |
|
1326 \n\ |
|
1327 List currently defined symbol(s). Options may be shortened to one\n\ |
|
1328 character, but may not be combined.") |
|
1329 { |
|
1330 Octave_object retval; |
|
1331 |
712
|
1332 int nargin = args.length (); |
|
1333 |
742
|
1334 Octave_object tmp_args; |
|
1335 for (int i = nargin; i > 0; i--) |
|
1336 tmp_args(i) = args(i-1); |
|
1337 tmp_args(0) = "-long"; |
581
|
1338 |
742
|
1339 int argc = tmp_args.length () + 1; |
581
|
1340 char **argv = make_argv (tmp_args, "whos"); |
|
1341 |
|
1342 if (error_state) |
|
1343 return retval; |
|
1344 |
1488
|
1345 retval = do_who (argc, argv); |
581
|
1346 |
|
1347 while (--argc >= 0) |
|
1348 delete [] argv[argc]; |
|
1349 delete [] argv; |
|
1350 |
|
1351 return retval; |
|
1352 } |
|
1353 |
593
|
1354 // Install variables and functions in the symbol tables. |
529
|
1355 |
593
|
1356 void |
|
1357 install_builtin_mapper (builtin_mapper_function *mf) |
529
|
1358 { |
593
|
1359 symbol_record *sym_rec = global_sym_tab->lookup (mf->name, 1); |
|
1360 sym_rec->unprotect (); |
|
1361 |
|
1362 Mapper_fcn mfcn; |
628
|
1363 mfcn.name = strsave (mf->name); |
593
|
1364 mfcn.can_return_complex_for_real_arg = mf->can_return_complex_for_real_arg; |
|
1365 mfcn.lower_limit = mf->lower_limit; |
|
1366 mfcn.upper_limit = mf->upper_limit; |
|
1367 mfcn.d_d_mapper = mf->d_d_mapper; |
|
1368 mfcn.d_c_mapper = mf->d_c_mapper; |
|
1369 mfcn.c_c_mapper = mf->c_c_mapper; |
|
1370 |
1488
|
1371 tree_builtin *def = new tree_builtin (mfcn, mf->name); |
593
|
1372 |
|
1373 sym_rec->define (def); |
|
1374 |
|
1375 sym_rec->document (mf->help_string); |
|
1376 sym_rec->make_eternal (); |
|
1377 sym_rec->protect (); |
|
1378 } |
|
1379 |
|
1380 void |
|
1381 install_builtin_function (builtin_function *f) |
|
1382 { |
|
1383 symbol_record *sym_rec = global_sym_tab->lookup (f->name, 1); |
|
1384 sym_rec->unprotect (); |
|
1385 |
1488
|
1386 tree_builtin *def = new tree_builtin (f->fcn, f->name); |
593
|
1387 |
|
1388 sym_rec->define (def, f->is_text_fcn); |
|
1389 |
|
1390 sym_rec->document (f->help_string); |
|
1391 sym_rec->make_eternal (); |
|
1392 sym_rec->protect (); |
|
1393 } |
|
1394 |
|
1395 void |
|
1396 install_builtin_variable (builtin_variable *v) |
|
1397 { |
|
1398 if (v->install_as_function) |
|
1399 install_builtin_variable_as_function (v->name, v->value, v->protect, |
|
1400 v->eternal, v->help_string); |
529
|
1401 else |
593
|
1402 bind_builtin_variable (v->name, v->value, v->protect, v->eternal, |
|
1403 v->sv_function, v->help_string); |
529
|
1404 } |
|
1405 |
593
|
1406 void |
|
1407 install_builtin_variable_as_function (const char *name, tree_constant *val, |
|
1408 int protect, int eternal, |
|
1409 const char *help) |
529
|
1410 { |
593
|
1411 symbol_record *sym_rec = global_sym_tab->lookup (name, 1); |
|
1412 sym_rec->unprotect (); |
|
1413 |
|
1414 const char *tmp_help = help; |
|
1415 if (! help) |
|
1416 tmp_help = sym_rec->help (); |
|
1417 |
|
1418 sym_rec->define_as_fcn (val); |
|
1419 |
|
1420 sym_rec->document (tmp_help); |
|
1421 |
|
1422 if (protect) |
|
1423 sym_rec->protect (); |
|
1424 |
|
1425 if (eternal) |
|
1426 sym_rec->make_eternal (); |
|
1427 } |
|
1428 |
|
1429 void |
|
1430 alias_builtin (const char *alias, const char *name) |
|
1431 { |
|
1432 symbol_record *sr_name = global_sym_tab->lookup (name, 0, 0); |
|
1433 if (! sr_name) |
|
1434 panic ("can't alias to undefined name!"); |
|
1435 |
|
1436 symbol_record *sr_alias = global_sym_tab->lookup (alias, 1, 0); |
|
1437 |
|
1438 if (sr_alias) |
|
1439 sr_alias->alias (sr_name); |
|
1440 else |
770
|
1441 panic ("can't find symbol record for builtin function `%s'", alias); |
529
|
1442 } |
|
1443 |
593
|
1444 // Defining variables. |
|
1445 |
1161
|
1446 #if 0 |
593
|
1447 void |
|
1448 bind_nargin_and_nargout (symbol_table *sym_tab, int nargin, int nargout) |
529
|
1449 { |
593
|
1450 tree_constant *tmp; |
|
1451 symbol_record *sr; |
|
1452 |
|
1453 sr = sym_tab->lookup ("nargin", 1, 0); |
|
1454 sr->unprotect (); |
712
|
1455 tmp = new tree_constant (nargin); |
593
|
1456 sr->define (tmp); |
|
1457 |
|
1458 sr = sym_tab->lookup ("nargout", 1, 0); |
|
1459 sr->unprotect (); |
|
1460 tmp = new tree_constant (nargout); |
|
1461 sr->define (tmp); |
|
1462 } |
1161
|
1463 #endif |
593
|
1464 |
1162
|
1465 void |
|
1466 bind_ans (const tree_constant& val, int print) |
|
1467 { |
|
1468 static symbol_record *sr = global_sym_tab->lookup ("ans", 1, 0); |
|
1469 |
1281
|
1470 tree_identifier *ans_id = new tree_identifier (sr); |
1162
|
1471 tree_constant *tmp = new tree_constant (val); |
|
1472 |
1281
|
1473 // XXX FIXME XXX -- making ans_id static, passing its address to |
|
1474 // tree_simple_assignment_expression along with a flag to not delete |
|
1475 // it seems to create a memory leak. Hmm. |
|
1476 |
|
1477 tree_simple_assignment_expression tmp_ass (ans_id, tmp, 0, 1); |
1162
|
1478 |
|
1479 tmp_ass.eval (print); |
|
1480 } |
|
1481 |
1489
|
1482 void |
|
1483 bind_global_error_variable (void) |
|
1484 { |
|
1485 *error_message_buffer << ends; |
|
1486 |
|
1487 char *error_text = error_message_buffer->str (); |
|
1488 |
|
1489 bind_builtin_variable ("__error_text__", error_text, 1); |
|
1490 |
|
1491 delete [] error_text; |
|
1492 |
|
1493 delete error_message_buffer; |
|
1494 |
|
1495 error_message_buffer = 0; |
|
1496 } |
|
1497 |
|
1498 void |
|
1499 clear_global_error_variable (void *) |
|
1500 { |
|
1501 delete error_message_buffer; |
|
1502 error_message_buffer = 0; |
|
1503 |
|
1504 bind_builtin_variable ("__error_text__", "", 1); |
|
1505 } |
|
1506 |
593
|
1507 // Give a global variable a definition. This will insert the symbol |
|
1508 // in the global table if necessary. |
|
1509 |
|
1510 // How is this different than install_builtin_variable? Are both |
|
1511 // functions needed? |
|
1512 |
|
1513 void |
|
1514 bind_builtin_variable (const char *varname, tree_constant *val, |
|
1515 int protect, int eternal, sv_Function sv_fcn, |
|
1516 const char *help) |
|
1517 { |
|
1518 symbol_record *sr = global_sym_tab->lookup (varname, 1, 0); |
|
1519 |
1271
|
1520 // It is a programming error for a builtin symbol to be missing. |
|
1521 // Besides, we just inserted it, so it must be there. |
593
|
1522 |
|
1523 assert (sr); |
|
1524 |
|
1525 sr->unprotect (); |
|
1526 |
1271
|
1527 // Must do this before define, since define will call the special |
|
1528 // variable function only if it knows about it, and it needs to, so |
|
1529 // that user prefs can be properly initialized. |
593
|
1530 |
|
1531 if (sv_fcn) |
|
1532 sr->set_sv_function (sv_fcn); |
|
1533 |
|
1534 sr->define_builtin_var (val); |
|
1535 |
|
1536 if (protect) |
|
1537 sr->protect (); |
|
1538 |
|
1539 if (eternal) |
|
1540 sr->make_eternal (); |
|
1541 |
|
1542 if (help) |
1418
|
1543 sr->document (help); |
529
|
1544 } |
|
1545 |
593
|
1546 void |
1406
|
1547 bind_builtin_variable (const char *varname, const tree_constant& val, |
|
1548 int protect, int eternal, sv_Function sv_fcn, |
|
1549 const char *help) |
|
1550 { |
|
1551 tree_constant *tc = new tree_constant (val); |
|
1552 bind_builtin_variable (varname, tc, protect, eternal, sv_fcn, help); |
|
1553 } |
|
1554 |
|
1555 void |
593
|
1556 install_builtin_variables (void) |
529
|
1557 { |
1271
|
1558 // XXX FIXME XX -- these should probably be moved to where they |
|
1559 // logically belong instead of being all grouped here. |
593
|
1560 |
1418
|
1561 DEFVAR ("EDITOR", SBV_EDITOR, editor, 0, sv_editor, |
593
|
1562 "name of the editor to be invoked by the edit_history command"); |
|
1563 |
1418
|
1564 DEFCONST ("I", SBV_I, Complex (0.0, 1.0), 0, 0, |
593
|
1565 "sqrt (-1)"); |
|
1566 |
1418
|
1567 DEFCONST ("Inf", SBV_Inf, octave_Inf, 0, 0, |
593
|
1568 "infinity"); |
|
1569 |
1418
|
1570 DEFVAR ("INFO_FILE", SBV_INFO_FILE, info_file, 0, sv_info_file, |
593
|
1571 "name of the Octave info file"); |
|
1572 |
1418
|
1573 DEFCONST ("J", SBV_J, Complex (0.0, 1.0), 0, 0, |
593
|
1574 "sqrt (-1)"); |
529
|
1575 |
1418
|
1576 DEFCONST ("NaN", SBV_NaN, octave_NaN, 0, 0, |
593
|
1577 "not a number"); |
|
1578 |
1418
|
1579 DEFVAR ("LOADPATH", SBV_LOADPATH, load_path, 0, sv_loadpath, |
593
|
1580 "colon separated list of directories to search for scripts"); |
|
1581 |
1418
|
1582 DEFVAR ("IMAGEPATH", SBV_IMAGEPATH, OCTAVE_IMAGEPATH, 0, |
686
|
1583 sv_imagepath, |
|
1584 "colon separated list of directories to search for image files"); |
|
1585 |
1418
|
1586 DEFCONST ("OCTAVE_VERSION", SBV_version, OCTAVE_VERSION, 0, 0, |
664
|
1587 "Octave version"); |
|
1588 |
1418
|
1589 DEFVAR ("PAGER", SBV_PAGER, default_pager (), 0, sv_pager_binary, |
593
|
1590 "path to pager binary"); |
|
1591 |
1418
|
1592 DEFVAR ("PS1", SBV_PS1, "\\s:\\#> ", 0, sv_ps1, |
593
|
1593 "primary prompt string"); |
|
1594 |
1418
|
1595 DEFVAR ("PS2", SBV_PS2, "> ", 0, sv_ps2, |
593
|
1596 "secondary prompt string"); |
|
1597 |
1418
|
1598 DEFVAR ("PS4", SBV_PS4, "+ ", 0, sv_ps4, |
1044
|
1599 "string printed before echoed input (enabled by --echo-input)"); |
|
1600 |
1418
|
1601 DEFCONST ("PWD", SBV_PWD, |
|
1602 get_working_directory ("initialize_globals"), 0, sv_pwd, |
593
|
1603 "current working directory"); |
|
1604 |
1418
|
1605 DEFCONST ("SEEK_SET", SBV_SEEK_SET, 0.0, 0, 0, |
593
|
1606 "used with fseek to position file relative to the beginning"); |
529
|
1607 |
1418
|
1608 DEFCONST ("SEEK_CUR", SBV_SEEK_CUR, 1.0, 0, 0, |
593
|
1609 "used with fseek to position file relative to the current position"); |
|
1610 |
1418
|
1611 DEFCONST ("SEEK_END", SBV_SEEK_END, 2.0, 0, 0, |
593
|
1612 "used with fseek to position file relative to the end"); |
|
1613 |
1418
|
1614 DEFVAR ("ans", SBV_ans, , 0, 0, |
593
|
1615 ""); |
|
1616 |
1418
|
1617 DEFCONST ("argv", SBV_argv, , 0, 0, |
1343
|
1618 "the command line arguments this program was invoked with"); |
|
1619 |
1505
|
1620 DEFVAR ("automatic_replot", SBV_automatic_replot, 0.0, |
1418
|
1621 0, automatic_replot, |
661
|
1622 "if true, auto-insert a replot command when a plot changes"); |
|
1623 |
1505
|
1624 DEFVAR ("beep_on_error", SBV_beep_on_error, 0.0, 0, |
1423
|
1625 beep_on_error, |
|
1626 "if true, beep before printing error messages"); |
|
1627 |
1429
|
1628 DEFVAR ("completion_append_char", SBV_completion_append_char, " ", |
|
1629 0, sv_completion_append_char, |
|
1630 "the string to append after successful command-line completion\n\ |
|
1631 attempts"); |
|
1632 |
1489
|
1633 DEFCONST ("error_text", SBV_current_error_text, "", 0, 0, |
|
1634 "the text of error messages that would have been printed in the |
|
1635 body of the most recent unwind_protect statement or the TRY part of\n\ |
|
1636 the most recent eval() command. Outside of unwind_protect and\n\ |
|
1637 eval(), or if no error has ocurred within them, the value of\n\ |
|
1638 __error_text__ is guaranteed to be the empty string."); |
|
1639 |
1093
|
1640 DEFVAR ("default_return_value", SBV_default_return_value, Matrix (), |
1418
|
1641 0, 0, |
1093
|
1642 "the default for value for unitialized variables returned from\n\ |
|
1643 functions. Only used if the variable initialize_return_values is\n\ |
|
1644 set to \"true\"."); |
593
|
1645 |
605
|
1646 DEFVAR ("default_save_format", SBV_default_save_format, "ascii", |
1418
|
1647 0, sv_default_save_format, |
997
|
1648 "default format for files created with save, may be one of\n\ |
1418
|
1649 \"binary\", \"text\", or \"mat-binary\""); |
605
|
1650 |
1407
|
1651 DEFVAR ("define_all_return_values", SBV_define_all_return_values, |
1505
|
1652 0.0, 0, define_all_return_values, |
1407
|
1653 "control whether values returned from functions should have a\n\ |
|
1654 value even if one has not been explicitly assigned. See also\n\ |
1418
|
1655 default_return_value"); |
1407
|
1656 |
1505
|
1657 DEFVAR ("do_fortran_indexing", SBV_do_fortran_indexing, 0.0, 0, |
1418
|
1658 do_fortran_indexing, |
593
|
1659 "allow single indices for matrices"); |
|
1660 |
1474
|
1661 DEFCONST ("e", SBV_e, exp (1.0), 0, 0, |
|
1662 "exp (1)"); |
|
1663 |
1418
|
1664 DEFVAR ("empty_list_elements_ok", SBV_empty_list_elements_ok, |
|
1665 "warn", 0, empty_list_elements_ok, |
593
|
1666 "ignore the empty element in expressions like `a = [[], 1]'"); |
529
|
1667 |
1418
|
1668 DEFCONST ("eps", SBV_eps, DBL_EPSILON, 0, 0, |
593
|
1669 "machine precision"); |
|
1670 |
1418
|
1671 DEFVAR ("gnuplot_binary", SBV_gnuplot_binary, "gnuplot", 0, |
593
|
1672 sv_gnuplot_binary, |
|
1673 "path to gnuplot binary"); |
|
1674 |
1502
|
1675 #ifdef GNUPLOT_HAS_MULTIPLOT |
1506
|
1676 double with_multiplot = 1.0; |
1502
|
1677 #else |
1506
|
1678 double with_multiplot = 0.0; |
1502
|
1679 #endif |
|
1680 |
|
1681 DEFVAR ("gnuplot_has_multiplot", SBV_gnuplot_has_multiplot, |
1504
|
1682 with_multiplot, 0, gnuplot_has_multiplot, |
1502
|
1683 "true if gnuplot supports multiplot, false otherwise"); |
|
1684 |
1418
|
1685 DEFCONST ("i", SBV_i, Complex (0.0, 1.0), 1, 0, |
593
|
1686 "sqrt (-1)"); |
529
|
1687 |
1418
|
1688 DEFVAR ("ignore_function_time_stamp", |
|
1689 SBV_ignore_function_time_stamp, "system", 0, |
593
|
1690 ignore_function_time_stamp, |
|
1691 "don't check to see if function files have changed since they were\n\ |
|
1692 last compiled. Possible values are \"system\" and \"all\""); |
|
1693 |
1418
|
1694 DEFVAR ("implicit_str_to_num_ok", SBV_implicit_str_to_num_ok, |
1505
|
1695 0.0, 0, implicit_str_to_num_ok, |
593
|
1696 "allow implicit string to number conversion"); |
|
1697 |
1418
|
1698 DEFCONST ("inf", SBV_inf, octave_Inf, 0, 0, |
593
|
1699 "infinity"); |
|
1700 |
1418
|
1701 DEFCONST ("j", SBV_j, Complex (0.0, 1.0), 1, 0, |
593
|
1702 "sqrt (-1)"); |
529
|
1703 |
1418
|
1704 DEFCONST ("nan", SBV_nan, octave_NaN, 0, 0, |
593
|
1705 "not a number"); |
|
1706 |
|
1707 DEFVAR ("ok_to_lose_imaginary_part", SBV_ok_to_lose_imaginary_part, |
1418
|
1708 "warn", 0, ok_to_lose_imaginary_part, |
593
|
1709 "silently convert from complex to real by dropping imaginary part"); |
|
1710 |
1418
|
1711 DEFVAR ("output_max_field_width", SBV_output_max_field_width, 10.0, |
|
1712 0, set_output_max_field_width, |
593
|
1713 "maximum width of an output field for numeric output"); |
|
1714 |
1418
|
1715 DEFVAR ("output_precision", SBV_output_precision, 5.0, 0, |
593
|
1716 set_output_precision, |
|
1717 "number of significant figures to display for numeric output"); |
|
1718 |
1505
|
1719 DEFVAR ("page_screen_output", SBV_page_screen_output, 1.0, 0, |
593
|
1720 page_screen_output, |
|
1721 "if possible, send output intended for the screen through the pager"); |
529
|
1722 |
1418
|
1723 DEFCONST ("pi", SBV_pi, 4.0 * atan (1.0), 0, 0, |
593
|
1724 "ratio of the circumference of a circle to its diameter"); |
|
1725 |
1505
|
1726 DEFVAR ("prefer_column_vectors", SBV_prefer_column_vectors, 1.0, |
1418
|
1727 0, prefer_column_vectors, |
593
|
1728 "prefer column/row vectors"); |
|
1729 |
|
1730 DEFVAR ("prefer_zero_one_indexing", SBV_prefer_zero_one_indexing, |
1505
|
1731 0.0, 0, prefer_zero_one_indexing, |
593
|
1732 "when there is a conflict, prefer zero-one style indexing"); |
|
1733 |
1505
|
1734 DEFVAR ("print_answer_id_name", SBV_print_answer_id_name, 1.0, 0, |
1418
|
1735 print_answer_id_name, |
593
|
1736 "set output style to print `var_name = ...'"); |
|
1737 |
1418
|
1738 DEFVAR ("print_empty_dimensions", SBV_print_empty_dimensions, |
1505
|
1739 1.0, 0, print_empty_dimensions, |
593
|
1740 "also print dimensions of empty matrices"); |
|
1741 |
1418
|
1742 DEFCONST ("program_invocation_name", SBV_program_invocation_name, |
|
1743 raw_prog_name, 0, 0, |
1343
|
1744 "the full name of the current program or script, including the\n\ |
|
1745 directory specification"); |
|
1746 |
1418
|
1747 DEFCONST ("program_name", SBV_program_name, prog_name, 0, 0, |
1343
|
1748 "the name of the current program or script"); |
|
1749 |
593
|
1750 DEFVAR ("propagate_empty_matrices", SBV_propagate_empty_matrices, |
1505
|
1751 1.0, 0, propagate_empty_matrices, |
593
|
1752 "operations on empty matrices return an empty matrix, not an error"); |
529
|
1753 |
1045
|
1754 #if 0 |
1505
|
1755 DEFVAR ("read_only_constants", SBV_read_only_constants, 1.0, 0, |
1418
|
1756 read_only_constants, |
1045
|
1757 "allow built-in constants to be modified"); |
|
1758 #endif |
|
1759 |
1418
|
1760 DEFCONST ("realmax", SBV_realmax, DBL_MAX, 0, 0, |
868
|
1761 "realmax (): return largest representable floating point number"); |
|
1762 |
1418
|
1763 DEFCONST ("realmin", SBV_realmin, DBL_MIN, 0, 0, |
868
|
1764 "realmin (): return smallest representable floating point number"); |
|
1765 |
1505
|
1766 DEFVAR ("resize_on_range_error", SBV_resize_on_range_error, 1.0, |
1418
|
1767 0, resize_on_range_error, |
593
|
1768 "enlarge matrices on assignment"); |
|
1769 |
1418
|
1770 DEFVAR ("return_last_computed_value", |
1505
|
1771 SBV_return_last_computed_value, 0.0, 0, |
593
|
1772 return_last_computed_value, |
|
1773 "if a function does not return any values explicitly, return the\n\ |
|
1774 last computed value"); |
|
1775 |
1505
|
1776 DEFVAR ("save_precision", SBV_save_precision, 15.0, 0, |
593
|
1777 set_save_precision, |
|
1778 "number of significant figures kept by the ASCII save command"); |
|
1779 |
1505
|
1780 DEFVAR ("silent_functions", SBV_silent_functions, 0.0, 0, |
593
|
1781 silent_functions, |
|
1782 "suppress printing results in called functions"); |
|
1783 |
1505
|
1784 DEFVAR ("split_long_rows", SBV_split_long_rows, 1.0, 0, |
593
|
1785 split_long_rows, |
|
1786 "split long matrix rows instead of wrapping"); |
529
|
1787 |
1198
|
1788 DEFVAR ("struct_levels_to_print", SBV_struct_levels_to_print, 2.0, |
1418
|
1789 0, struct_levels_to_print, |
1198
|
1790 "number of levels of structure elements to print"); |
|
1791 |
1139
|
1792 #ifdef USE_GNU_INFO |
1116
|
1793 DEFVAR ("suppress_verbose_help_message", |
1505
|
1794 SBV_suppress_verbose_help_message, 0.0, 0, |
1116
|
1795 suppress_verbose_help_message, |
|
1796 "suppress printing of message pointing to additional help in the\n\ |
|
1797 help and usage functions"); |
1139
|
1798 #endif |
1116
|
1799 |
1418
|
1800 DEFCONST ("stdin", SBV_stdin, 0.0, 0, 0, |
593
|
1801 "file number of the standard input stream"); |
|
1802 |
1418
|
1803 DEFCONST ("stdout", SBV_stdout, 1.0, 0, 0, |
593
|
1804 "file number of the standard output stream"); |
|
1805 |
1418
|
1806 DEFCONST ("stderr", SBV_stderr, 2.0, 0, 0, |
593
|
1807 "file number of the standard error stream"); |
|
1808 |
1505
|
1809 DEFVAR ("treat_neg_dim_as_zero", SBV_treat_neg_dim_as_zero, 0.0, 0, |
|
1810 treat_neg_dim_as_zero, |
593
|
1811 "convert negative dimensions to zero"); |
|
1812 |
1418
|
1813 DEFVAR ("warn_assign_as_truth_value", |
1505
|
1814 SBV_warn_assign_as_truth_value, 1.0, 0, |
593
|
1815 warn_assign_as_truth_value, |
|
1816 "produce warning for assignments used as truth values"); |
|
1817 |
|
1818 DEFVAR ("warn_comma_in_global_decl", SBV_warn_comma_in_global_decl, |
1505
|
1819 1.0, 0, warn_comma_in_global_decl, |
593
|
1820 "produce warning for commas in global declarations"); |
|
1821 |
1505
|
1822 DEFVAR ("warn_divide_by_zero", SBV_warn_divide_by_zero, 1.0, 0, |
1418
|
1823 warn_divide_by_zero, |
593
|
1824 "on IEEE machines, allow divide by zero errors to be suppressed"); |
1037
|
1825 |
|
1826 DEFVAR ("warn_function_name_clash", SBV_warn_function_name_clash, |
1505
|
1827 1.0, 0, warn_function_name_clash, |
1037
|
1828 "produce warning if function name conflicts with file name"); |
1093
|
1829 |
1509
|
1830 DEFVAR ("warn_missing_semicolon", SBV_warn_missing_semicolon, |
|
1831 0.0, 0, warn_missing_semicolon, |
|
1832 "produce a warning if a statement in a script or function file is |
|
1833 not terminated with a semicolon") |
|
1834 |
1418
|
1835 DEFVAR ("whitespace_in_literal_matrix", |
|
1836 SBV_whitespace_in_literal_matrix, "", 0, |
|
1837 whitespace_in_literal_matrix, |
1093
|
1838 "control auto-insertion of commas and semicolons in literal matrices"); |
529
|
1839 } |
|
1840 |
593
|
1841 // Deleting names from the symbol tables. |
|
1842 |
1488
|
1843 DEFUN_TEXT ("clear", Fclear, Sclear, 10, |
668
|
1844 "clear [-x] [name ...]\n\ |
|
1845 \n\ |
|
1846 Clear symbol(s) matching a list of globbing patterns.\n\ |
593
|
1847 \n\ |
668
|
1848 If no arguments are given, clear all user-defined variables and |
|
1849 functions.\n\ |
|
1850 \n\ |
|
1851 With -x, exclude the named variables") |
529
|
1852 { |
593
|
1853 Octave_object retval; |
|
1854 |
|
1855 DEFINE_ARGV("clear"); |
|
1856 |
668
|
1857 argc--; |
|
1858 argv++; |
|
1859 |
1271
|
1860 // Always clear the local table, but don't clear currently compiled |
|
1861 // functions unless we are at the top level. (Allowing that to |
|
1862 // happen inside functions would result in pretty odd behavior...) |
593
|
1863 |
|
1864 int clear_user_functions = (curr_sym_tab == top_level_sym_tab); |
|
1865 |
668
|
1866 if (argc == 0) |
593
|
1867 { |
|
1868 curr_sym_tab->clear (); |
|
1869 global_sym_tab->clear (clear_user_functions); |
|
1870 } |
529
|
1871 else |
|
1872 { |
668
|
1873 int exclusive = 0; |
|
1874 |
|
1875 if (argc > 0) |
|
1876 { |
|
1877 if (strcmp (*argv, "-x") == 0) |
|
1878 { |
|
1879 exclusive = 1; |
|
1880 argv++; |
|
1881 argc--; |
|
1882 } |
|
1883 } |
|
1884 |
|
1885 int lcount = 0; |
|
1886 int gcount = 0; |
|
1887 int fcount = 0; |
529
|
1888 |
668
|
1889 char **lvars = 0; |
|
1890 char **gvars = 0; |
|
1891 char **fcns = 0; |
|
1892 |
|
1893 if (argc > 0) |
593
|
1894 { |
893
|
1895 lvars = curr_sym_tab->list (lcount, 0, 0, 0, |
1418
|
1896 SYMTAB_VARIABLES, |
668
|
1897 SYMTAB_LOCAL_SCOPE); |
|
1898 |
893
|
1899 gvars = curr_sym_tab->list (gcount, 0, 0, 0, |
1418
|
1900 SYMTAB_VARIABLES, |
668
|
1901 SYMTAB_GLOBAL_SCOPE); |
|
1902 |
893
|
1903 fcns = global_sym_tab->list (fcount, 0, 0, 0, |
|
1904 symbol_def::USER_FUNCTION, |
864
|
1905 SYMTAB_ALL_SCOPES); |
668
|
1906 } |
|
1907 |
|
1908 while (argc > 0) |
|
1909 { |
|
1910 char *pat = *argv; |
|
1911 |
|
1912 if (pat) |
593
|
1913 { |
|
1914 int i; |
|
1915 for (i = 0; i < lcount; i++) |
|
1916 { |
668
|
1917 char *nm = lvars[i]; |
|
1918 int match = (fnmatch (pat, nm, __FNM_FLAGS) == 0); |
|
1919 if ((exclusive && ! match) || (! exclusive && match)) |
|
1920 curr_sym_tab->clear (nm); |
593
|
1921 } |
529
|
1922 |
593
|
1923 int count; |
|
1924 for (i = 0; i < gcount; i++) |
|
1925 { |
668
|
1926 char *nm = gvars[i]; |
|
1927 int match = (fnmatch (pat, nm, __FNM_FLAGS) == 0); |
|
1928 if ((exclusive && ! match) || (! exclusive && match)) |
593
|
1929 { |
668
|
1930 count = curr_sym_tab->clear (nm); |
593
|
1931 if (count > 0) |
668
|
1932 global_sym_tab->clear (nm, clear_user_functions); |
593
|
1933 } |
|
1934 } |
529
|
1935 |
593
|
1936 for (i = 0; i < fcount; i++) |
|
1937 { |
668
|
1938 char *nm = fcns[i]; |
|
1939 int match = (fnmatch (pat, nm, __FNM_FLAGS) == 0); |
|
1940 if ((exclusive && ! match) || (! exclusive && match)) |
593
|
1941 { |
668
|
1942 count = curr_sym_tab->clear (nm); |
864
|
1943 global_sym_tab->clear (nm, clear_user_functions); |
593
|
1944 } |
|
1945 } |
|
1946 } |
668
|
1947 |
|
1948 argc--; |
|
1949 argv++; |
593
|
1950 } |
529
|
1951 |
593
|
1952 delete [] lvars; |
|
1953 delete [] gvars; |
|
1954 delete [] fcns; |
|
1955 |
|
1956 } |
|
1957 |
|
1958 DELETE_ARGV; |
|
1959 |
|
1960 return retval; |
529
|
1961 } |
|
1962 |
1
|
1963 /* |
|
1964 ;;; Local Variables: *** |
|
1965 ;;; mode: C++ *** |
|
1966 ;;; page-delimiter: "^/\\*" *** |
|
1967 ;;; End: *** |
|
1968 */ |