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