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