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