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