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