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