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