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