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