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