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