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