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