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