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