1
|
1 // variables.cc -*- C++ -*- |
|
2 /* |
|
3 |
296
|
4 Copyright (C) 1992, 1993, 1994 John W. Eaton |
1
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
1
|
26 #endif |
|
27 |
|
28 #include <sys/types.h> |
|
29 #ifdef HAVE_UNISTD_H |
|
30 #include <unistd.h> |
|
31 #endif |
|
32 #include <ctype.h> |
529
|
33 #include <float.h> |
|
34 #include <string.h> |
|
35 #include <fstream.h> |
1
|
36 #include <iostream.h> |
279
|
37 #include <strstream.h> |
1
|
38 |
|
39 #include "statdefs.h" |
|
40 #include "tree-const.h" |
|
41 #include "variables.h" |
529
|
42 #include "mappers.h" |
195
|
43 #include "user-prefs.h" |
529
|
44 #include "version.h" |
1
|
45 #include "symtab.h" |
529
|
46 #include "defaults.h" |
|
47 #include "dirfns.h" |
|
48 #include "pager.h" |
|
49 #include "sysdep.h" |
|
50 #include "octave.h" |
|
51 #include "oct-obj.h" |
1
|
52 #include "error.h" |
|
53 #include "utils.h" |
|
54 #include "tree.h" |
164
|
55 #include "help.h" |
529
|
56 #include "defun.h" |
|
57 |
|
58 extern "C" |
|
59 { |
|
60 #include <readline/tilde.h> |
|
61 |
|
62 #include "fnmatch.h" |
|
63 } |
1
|
64 |
|
65 // Symbol table for symbols at the top level. |
|
66 symbol_table *top_level_sym_tab; |
|
67 |
|
68 // Symbol table for the current scope. |
|
69 symbol_table *curr_sym_tab; |
|
70 |
|
71 // Symbol table for global symbols. |
|
72 symbol_table *global_sym_tab; |
|
73 |
195
|
74 void |
|
75 initialize_symbol_tables (void) |
|
76 { |
|
77 global_sym_tab = new symbol_table (); |
|
78 |
|
79 top_level_sym_tab = new symbol_table (); |
|
80 |
|
81 curr_sym_tab = top_level_sym_tab; |
|
82 } |
|
83 |
1
|
84 /* |
338
|
85 * Is there a corresponding function file that is newer than the |
|
86 * symbol definition? |
1
|
87 */ |
|
88 int |
|
89 symbol_out_of_date (symbol_record *sr) |
|
90 { |
195
|
91 int ignore = user_pref.ignore_function_time_stamp; |
|
92 |
|
93 if (ignore == 2) |
|
94 return 0; |
|
95 |
529
|
96 if (sr) |
1
|
97 { |
490
|
98 tree_fvc *ans = sr->def (); |
529
|
99 if (ans) |
1
|
100 { |
339
|
101 char *ff = ans->fcn_file_name (); |
529
|
102 if (ff && ! (ignore && ans->is_system_fcn_file ())) |
1
|
103 { |
|
104 time_t tp = ans->time_parsed (); |
339
|
105 char *fname = fcn_file_in_path (ff); |
195
|
106 int status = is_newer (fname, tp); |
|
107 delete [] fname; |
|
108 if (status > 0) |
|
109 return 1; |
1
|
110 } |
|
111 } |
|
112 } |
195
|
113 return 0; |
1
|
114 } |
|
115 |
195
|
116 void |
|
117 document_symbol (const char *name, const char *help) |
1
|
118 { |
195
|
119 if (is_builtin_variable (name)) |
1
|
120 { |
195
|
121 error ("sorry, can't redefine help for builtin variables"); |
|
122 } |
|
123 else |
|
124 { |
|
125 symbol_record *sym_rec = curr_sym_tab->lookup (name, 0); |
529
|
126 if (sym_rec) |
|
127 sym_rec->document (help); |
1
|
128 else |
529
|
129 error ("document: no such symbol `%s'", name); |
1
|
130 } |
195
|
131 } |
1
|
132 |
195
|
133 void |
529
|
134 install_builtin_mapper (builtin_mapper_function *mf) |
195
|
135 { |
|
136 symbol_record *sym_rec = global_sym_tab->lookup (mf->name, 1); |
|
137 sym_rec->unprotect (); |
|
138 |
|
139 Mapper_fcn mfcn; |
330
|
140 mfcn.can_return_complex_for_real_arg = mf->can_return_complex_for_real_arg; |
|
141 mfcn.lower_limit = mf->lower_limit; |
|
142 mfcn.upper_limit = mf->upper_limit; |
195
|
143 mfcn.d_d_mapper = mf->d_d_mapper; |
|
144 mfcn.d_c_mapper = mf->d_c_mapper; |
|
145 mfcn.c_c_mapper = mf->c_c_mapper; |
|
146 |
330
|
147 tree_builtin *def = new tree_builtin (2, 1, mfcn, mf->name); |
195
|
148 |
|
149 sym_rec->define (def); |
|
150 |
|
151 sym_rec->document (mf->help_string); |
|
152 sym_rec->make_eternal (); |
|
153 sym_rec->protect (); |
|
154 } |
|
155 |
|
156 void |
529
|
157 install_builtin_function (builtin_function *f) |
195
|
158 { |
529
|
159 symbol_record *sym_rec = global_sym_tab->lookup (f->name, 1); |
195
|
160 sym_rec->unprotect (); |
|
161 |
529
|
162 tree_builtin *def = new tree_builtin (f->nargin_max, f->nargout_max, |
|
163 f->fcn, f->name); |
1
|
164 |
529
|
165 sym_rec->define (def, f->is_text_fcn); |
195
|
166 |
529
|
167 sym_rec->document (f->help_string); |
195
|
168 sym_rec->make_eternal (); |
|
169 sym_rec->protect (); |
|
170 } |
|
171 |
|
172 void |
529
|
173 install_builtin_variable (builtin_variable *v) |
1
|
174 { |
529
|
175 if (v->install_as_function) |
|
176 install_builtin_variable_as_function (v->name, v->value, v->protect, |
|
177 v->eternal, v->help_string); |
|
178 else |
|
179 bind_builtin_variable (v->name, v->value, v->protect, v->eternal, |
|
180 v->sv_function, v->help_string); |
195
|
181 } |
|
182 |
|
183 void |
|
184 install_builtin_variable_as_function (const char *name, tree_constant *val, |
529
|
185 int protect, int eternal, |
|
186 const char *help) |
195
|
187 { |
|
188 symbol_record *sym_rec = global_sym_tab->lookup (name, 1); |
|
189 sym_rec->unprotect (); |
|
190 |
529
|
191 const char *tmp_help = help; |
|
192 if (! help) |
|
193 tmp_help = sym_rec->help (); |
1
|
194 |
195
|
195 sym_rec->define_as_fcn (val); |
|
196 |
|
197 sym_rec->document (tmp_help); |
|
198 |
|
199 if (protect) |
|
200 sym_rec->protect (); |
|
201 |
|
202 if (eternal) |
|
203 sym_rec->make_eternal (); |
|
204 } |
|
205 |
|
206 void |
549
|
207 alias_builtin (const char *alias, const char *name) |
|
208 { |
|
209 symbol_record *sr_name = global_sym_tab->lookup (name, 0, 0); |
|
210 if (! sr_name) |
|
211 panic ("can't alias to undefined name!"); |
|
212 |
|
213 symbol_record *sr_alias = global_sym_tab->lookup (alias, 1, 0); |
|
214 |
|
215 if (sr_alias) |
|
216 sr_alias->alias (sr_name); |
|
217 else |
|
218 panic_impossible (); |
|
219 } |
|
220 |
|
221 void |
195
|
222 bind_nargin_and_nargout (symbol_table *sym_tab, int nargin, int nargout) |
|
223 { |
|
224 tree_constant *tmp; |
1
|
225 symbol_record *sr; |
|
226 |
195
|
227 sr = sym_tab->lookup ("nargin", 1, 0); |
|
228 sr->unprotect (); |
|
229 tmp = new tree_constant (nargin-1); |
|
230 sr->define (tmp); |
|
231 sr->protect (); |
|
232 |
|
233 sr = sym_tab->lookup ("nargout", 1, 0); |
|
234 sr->unprotect (); |
|
235 tmp = new tree_constant (nargout); |
|
236 sr->define (tmp); |
|
237 sr->protect (); |
1
|
238 } |
|
239 |
|
240 /* |
195
|
241 * Give a global variable a definition. This will insert the symbol |
|
242 * in the global table if necessary. |
|
243 */ |
|
244 void |
|
245 bind_builtin_variable (const char *varname, tree_constant *val, |
529
|
246 int protect, int eternal, sv_Function sv_fcn, |
|
247 const char *help) |
195
|
248 { |
|
249 symbol_record *sr = global_sym_tab->lookup (varname, 1, 0); |
|
250 |
|
251 // It is a programming error for a builtin symbol to be missing. |
|
252 // Besides, we just inserted it, so it must be there. |
|
253 |
529
|
254 assert (sr); |
195
|
255 |
|
256 sr->unprotect (); |
|
257 |
|
258 // Must do this before define, since define will call the special |
|
259 // variable function only if it knows about it, and it needs to, so |
|
260 // that user prefs can be properly initialized. |
|
261 |
|
262 if (sv_fcn) |
|
263 sr->set_sv_function (sv_fcn); |
|
264 |
|
265 sr->define_builtin_var (val); |
|
266 |
|
267 if (protect) |
|
268 sr->protect (); |
|
269 |
|
270 if (eternal) |
|
271 sr->make_eternal (); |
|
272 |
|
273 if (help) |
|
274 sr->document (help); |
|
275 } |
|
276 |
|
277 /* |
|
278 * Look for the given name in the global symbol table. If it refers |
529
|
279 * to a string, return a new copy. If not, return 0; |
1
|
280 */ |
|
281 char * |
195
|
282 builtin_string_variable (const char *name) |
1
|
283 { |
195
|
284 symbol_record *sr = global_sym_tab->lookup (name, 0, 0); |
|
285 |
|
286 // It is a prorgramming error to look for builtins that aren't. |
|
287 |
529
|
288 assert (sr); |
195
|
289 |
529
|
290 char *retval = 0; |
1
|
291 |
490
|
292 tree_fvc *defn = sr->def (); |
195
|
293 |
529
|
294 if (defn) |
1
|
295 { |
|
296 tree_constant val = defn->eval (0); |
195
|
297 |
|
298 if (! error_state && val.is_string_type ()) |
1
|
299 { |
|
300 char *s = val.string_value (); |
529
|
301 if (s) |
1
|
302 retval = strsave (s); |
|
303 } |
|
304 } |
|
305 |
|
306 return retval; |
|
307 } |
|
308 |
|
309 /* |
195
|
310 * Look for the given name in the global symbol table. If it refers |
|
311 * to a real scalar, place the value in d and return 0. Otherwise, |
|
312 * return -1. |
1
|
313 */ |
|
314 int |
195
|
315 builtin_real_scalar_variable (const char *name, double& d) |
1
|
316 { |
|
317 int status = -1; |
195
|
318 symbol_record *sr = global_sym_tab->lookup (name, 0, 0); |
|
319 |
|
320 // It is a prorgramming error to look for builtins that aren't. |
|
321 |
529
|
322 assert (sr); |
1
|
323 |
490
|
324 tree_fvc *defn = sr->def (); |
195
|
325 |
529
|
326 if (defn) |
1
|
327 { |
|
328 tree_constant val = defn->eval (0); |
195
|
329 |
|
330 if (! error_state |
|
331 && val.const_type () == tree_constant_rep::scalar_constant) |
1
|
332 { |
|
333 d = val.double_value (); |
|
334 status = 0; |
|
335 } |
|
336 } |
|
337 |
|
338 return status; |
|
339 } |
|
340 |
|
341 /* |
195
|
342 * Make the definition of the symbol record sr be the same as the |
|
343 * definition of the global variable of the same name, creating it if |
|
344 * it doesn't already exist. |
|
345 */ |
|
346 void |
|
347 link_to_global_variable (symbol_record *sr) |
|
348 { |
|
349 if (sr->is_linked_to_global ()) |
|
350 return; |
|
351 |
|
352 symbol_record *gsr = global_sym_tab->lookup (sr->name (), 1, 0); |
|
353 |
|
354 if (sr->is_formal_parameter ()) |
|
355 { |
|
356 error ("can't make function parameter `%s' global", sr->name ()); |
|
357 return; |
|
358 } |
|
359 |
|
360 // There must be a better way to do this. XXX FIXME XXX |
|
361 |
|
362 if (sr->is_variable ()) |
|
363 { |
|
364 // Would be nice not to have this cast. XXX FIXME XXX |
|
365 tree_constant *tmp = (tree_constant *) sr->def (); |
529
|
366 if (tmp) |
|
367 tmp = new tree_constant (*tmp); |
|
368 else |
207
|
369 tmp = new tree_constant (); |
195
|
370 gsr->define (tmp); |
|
371 } |
|
372 else |
529
|
373 sr->clear (); |
195
|
374 |
|
375 // If the global symbol is currently defined as a function, we need to |
|
376 // hide it with a variable. |
|
377 |
|
378 if (gsr->is_function ()) |
529
|
379 gsr->define ((tree_constant *) 0); |
195
|
380 |
|
381 sr->alias (gsr, 1); |
|
382 sr->mark_as_linked_to_global (); |
|
383 } |
|
384 |
|
385 /* |
|
386 * Make the definition of the symbol record sr be the same as the |
|
387 * definition of the builtin variable of the same name. |
|
388 */ |
|
389 void |
|
390 link_to_builtin_variable (symbol_record *sr) |
|
391 { |
|
392 symbol_record *tmp_sym = global_sym_tab->lookup (sr->name (), 0, 0); |
|
393 |
529
|
394 if (tmp_sym && tmp_sym->is_builtin_variable ()) |
|
395 sr->alias (tmp_sym); |
195
|
396 } |
|
397 |
|
398 /* |
|
399 * Make the definition of the symbol record sr be the same as the |
|
400 * definition of the builtin variable or function, or user function of |
|
401 * the same name, provided that the name has not been used as a formal |
|
402 * parameter. |
|
403 */ |
|
404 void |
|
405 link_to_builtin_or_function (symbol_record *sr) |
|
406 { |
|
407 symbol_record *tmp_sym = global_sym_tab->lookup (sr->name (), 0, 0); |
|
408 |
529
|
409 if (tmp_sym |
|
410 && (tmp_sym->is_builtin_variable () || tmp_sym->is_function ()) |
|
411 && ! tmp_sym->is_formal_parameter ()) |
|
412 sr->alias (tmp_sym); |
195
|
413 } |
|
414 |
|
415 /* |
|
416 * Force a link to a function in the current symbol table. This is |
|
417 * used just after defining a function to avoid different behavior |
|
418 * depending on whether or not the function has been evaluated after |
|
419 * being defined. |
|
420 * |
|
421 * Return without doing anything if there isn't a function with the |
|
422 * given name defined in the global symbol table. |
|
423 */ |
|
424 void |
|
425 force_link_to_function (const char *id_name) |
|
426 { |
|
427 symbol_record *gsr = global_sym_tab->lookup (id_name, 1, 0); |
|
428 if (gsr->is_function ()) |
|
429 { |
|
430 curr_sym_tab->clear (id_name); |
|
431 symbol_record *csr = curr_sym_tab->lookup (id_name, 1, 0); |
|
432 csr->alias (gsr); |
|
433 } |
|
434 } |
|
435 |
529
|
436 DEFUN ("is_global", Fis_global, Sis_global, 2, 1, |
|
437 "is_global (X): return 1 if the string X names a global variable\n\ |
|
438 otherwise, return 0.") |
195
|
439 { |
564
|
440 Octave_object retval = 0.0; |
529
|
441 |
|
442 int nargin = args.length (); |
|
443 |
|
444 if (nargin != 2 || ! args(1).is_string_type ()) |
|
445 { |
|
446 print_usage ("is_global"); |
|
447 return retval; |
|
448 } |
|
449 |
|
450 char *name = args(1).string_value (); |
|
451 |
195
|
452 symbol_record *sr = curr_sym_tab->lookup (name, 0, 0); |
529
|
453 |
|
454 retval = (double) (sr && sr->is_linked_to_global ()); |
|
455 |
|
456 return retval; |
195
|
457 } |
|
458 |
|
459 /* |
1
|
460 * Extract a keyword and its value from a file. Input should look |
|
461 * something like: |
|
462 * |
|
463 * #[ \t]*keyword[ \t]*:[ \t]*string-value\n |
279
|
464 * |
312
|
465 * Returns a pointer to new storage. The caller is responsible for |
|
466 * deleting it. |
1
|
467 */ |
279
|
468 char * |
|
469 extract_keyword (istream& is, char *keyword) |
1
|
470 { |
279
|
471 ostrstream buf; |
1
|
472 |
529
|
473 char *retval = 0; |
1
|
474 |
|
475 char c; |
|
476 while (is.get (c)) |
|
477 { |
|
478 if (c == '#') |
|
479 { |
|
480 while (is.get (c) && (c == ' ' || c == '\t' || c == '#')) |
|
481 ; // Skip whitespace and comment characters. |
|
482 |
|
483 if (isalpha (c)) |
279
|
484 buf << c; |
1
|
485 |
|
486 while (is.get (c) && isalpha (c)) |
279
|
487 buf << c; |
1
|
488 |
279
|
489 buf << ends; |
|
490 char *tmp = buf.str (); |
|
491 int match = (strncmp (tmp, keyword, strlen (keyword)) == 0); |
|
492 delete [] tmp; |
|
493 |
|
494 if (match) |
1
|
495 { |
279
|
496 ostrstream value; |
1
|
497 while (is.get (c) && (c == ' ' || c == '\t' || c == ':')) |
|
498 ; // Skip whitespace and the colon. |
|
499 |
|
500 if (c != '\n') |
|
501 { |
279
|
502 value << c; |
1
|
503 while (is.get (c) && c != '\n') |
279
|
504 value << c; |
1
|
505 } |
279
|
506 value << ends; |
|
507 retval = value.str (); |
1
|
508 break; |
|
509 } |
|
510 } |
|
511 } |
279
|
512 return retval; |
1
|
513 } |
|
514 |
|
515 int |
|
516 extract_keyword (istream& is, char *keyword, int& value) |
|
517 { |
279
|
518 ostrstream buf; |
1
|
519 |
|
520 int status = 0; |
|
521 value = 0; |
|
522 |
|
523 char c; |
|
524 while (is.get (c)) |
|
525 { |
|
526 if (c == '#') |
|
527 { |
|
528 while (is.get (c) && (c == ' ' || c == '\t' || c == '#')) |
|
529 ; // Skip whitespace and comment characters. |
|
530 |
|
531 if (isalpha (c)) |
279
|
532 buf << c; |
1
|
533 |
|
534 while (is.get (c) && isalpha (c)) |
279
|
535 buf << c; |
1
|
536 |
279
|
537 buf << ends; |
|
538 char *tmp = buf.str (); |
|
539 int match = (strncmp (tmp, keyword, strlen (keyword)) == 0); |
|
540 delete [] tmp; |
|
541 |
|
542 if (match) |
1
|
543 { |
|
544 while (is.get (c) && (c == ' ' || c == '\t' || c == ':')) |
|
545 ; // Skip whitespace and the colon. |
|
546 |
|
547 is.putback (c); |
|
548 if (c != '\n') |
|
549 is >> value; |
|
550 if (is) |
|
551 status = 1; |
|
552 while (is.get (c) && c != '\n') |
|
553 ; // Skip to beginning of next line; |
|
554 break; |
|
555 } |
|
556 } |
|
557 } |
|
558 return status; |
|
559 } |
|
560 |
|
561 /* |
|
562 * Skip trailing white space and |
|
563 */ |
|
564 void |
|
565 skip_comments (istream& is) |
|
566 { |
|
567 char c = '\0'; |
|
568 while (is.get (c)) |
|
569 { |
|
570 if (c == ' ' || c == '\t' || c == '\n') |
|
571 ; // Skip whitespace on way to beginning of next line. |
|
572 else |
|
573 break; |
|
574 } |
|
575 |
|
576 for (;;) |
|
577 { |
|
578 if (is && c == '#') |
|
579 while (is.get (c) && c != '\n') |
|
580 ; // Skip to beginning of next line, ignoring everything. |
|
581 else |
|
582 break; |
|
583 } |
|
584 } |
|
585 |
|
586 /* |
|
587 * Is `s' a valid identifier? |
|
588 */ |
|
589 int |
|
590 valid_identifier (char *s) |
|
591 { |
529
|
592 if (! s || ! (isalnum (*s) || *s == '_')) |
1
|
593 return 0; |
|
594 |
|
595 while (*++s != '\0') |
|
596 if (! (isalnum (*s) || *s == '_')) |
|
597 return 0; |
|
598 |
|
599 return 1; |
|
600 } |
|
601 |
529
|
602 DEFUN ("exist", Fexist, Sexist, 2, 1, |
|
603 "exist (NAME): check if variable or file exists\n\ |
|
604 \n\ |
|
605 return 0 if NAME is undefined, 1 if it is a variable, or 2 if it is\n\ |
|
606 a function.") |
1
|
607 { |
529
|
608 Octave_object retval; |
|
609 |
|
610 int nargin = args.length (); |
|
611 |
|
612 if (nargin != 2 || ! args(1).is_string_type ()) |
|
613 { |
|
614 print_usage ("exist"); |
|
615 return retval; |
|
616 } |
|
617 |
|
618 char *name = args(1).string_value (); |
|
619 |
159
|
620 symbol_record *sr = curr_sym_tab->lookup (name, 0, 0); |
529
|
621 if (! sr) |
159
|
622 sr = global_sym_tab->lookup (name, 0, 0); |
1
|
623 |
529
|
624 retval = 0.0; |
|
625 |
|
626 if (sr && sr->is_variable () && sr->is_defined ()) |
|
627 retval = 1.0; |
|
628 else if (sr && sr->is_function ()) |
|
629 retval = 2.0; |
1
|
630 else |
|
631 { |
338
|
632 char *path = fcn_file_in_path (name); |
529
|
633 if (path) |
1
|
634 { |
|
635 delete [] path; |
529
|
636 retval = 2.0; |
1
|
637 } |
|
638 else |
|
639 { |
|
640 struct stat buf; |
|
641 if (stat (name, &buf) == 0 && S_ISREG (buf.st_mode)) |
529
|
642 retval = 2.0; |
1
|
643 } |
|
644 } |
529
|
645 |
|
646 return retval; |
1
|
647 } |
|
648 |
|
649 /* |
195
|
650 * Is this variable a builtin? |
|
651 */ |
|
652 int |
|
653 is_builtin_variable (const char *name) |
|
654 { |
|
655 symbol_record *sr = global_sym_tab->lookup (name, 0, 0); |
529
|
656 return (sr && sr->is_builtin_variable ()); |
195
|
657 } |
|
658 |
|
659 /* |
1
|
660 * Is this tree_constant a valid function? |
|
661 */ |
490
|
662 tree_fvc * |
529
|
663 is_valid_function (const tree_constant& arg, char *warn_for, int warn) |
1
|
664 { |
529
|
665 tree_fvc *ans = 0; |
1
|
666 |
|
667 if (! arg.is_string_type ()) |
|
668 { |
|
669 if (warn) |
217
|
670 error ("%s: expecting function name as argument", warn_for); |
1
|
671 return ans; |
|
672 } |
|
673 |
|
674 char *fcn_name = arg.string_value (); |
|
675 symbol_record *sr = global_sym_tab->lookup (fcn_name, 0, 0); |
|
676 |
529
|
677 if (sr && symbol_out_of_date (sr)) |
|
678 { |
|
679 tree_identifier tmp (sr); |
|
680 tmp.parse_fcn_file (0); |
|
681 } |
|
682 else |
1
|
683 { |
|
684 sr = global_sym_tab->lookup (fcn_name, 1, 0); |
|
685 tree_identifier tmp (sr); |
338
|
686 tmp.parse_fcn_file (0); |
1
|
687 } |
|
688 |
|
689 ans = sr->def (); |
529
|
690 if (! ans || ! sr->is_function ()) |
1
|
691 { |
|
692 if (warn) |
217
|
693 error ("%s: the symbol `%s' is not valid as a function", |
|
694 warn_for, fcn_name); |
529
|
695 ans = 0; |
1
|
696 } |
|
697 |
|
698 return ans; |
|
699 } |
|
700 |
|
701 /* |
|
702 * Does this function take the right number of arguments? |
|
703 */ |
|
704 int |
490
|
705 takes_correct_nargs (tree_fvc *fcn, int expected_nargin, char *warn_for, |
529
|
706 int warn) |
1
|
707 { |
506
|
708 int nargin = fcn->max_expected_args () - 1; |
|
709 int e_nargin = expected_nargin - 1; |
|
710 if (nargin != e_nargin) |
1
|
711 { |
|
712 if (warn) |
217
|
713 error ("%s: expecting function to take %d argument%c", |
529
|
714 warn_for, e_nargin, (e_nargin == 1 ? "" : "s")); |
1
|
715 return 0; |
|
716 } |
|
717 return 1; |
|
718 } |
|
719 |
195
|
720 // It's not likely that this does the right thing now. XXX FIXME XXX |
|
721 |
1
|
722 char ** |
|
723 make_name_list (void) |
|
724 { |
|
725 int key_len = 0; |
|
726 int glb_len = 0; |
|
727 int top_len = 0; |
|
728 int lcl_len = 0; |
339
|
729 int ffl_len = 0; |
1
|
730 |
529
|
731 char **key = 0; |
|
732 char **glb = 0; |
|
733 char **top = 0; |
|
734 char **lcl = 0; |
|
735 char **ffl = 0; |
1
|
736 |
244
|
737 // Each of these functions returns a new vector of pointers to new |
|
738 // strings. |
|
739 |
1
|
740 key = names (keyword_help (), key_len); |
|
741 glb = global_sym_tab->list (glb_len); |
|
742 top = top_level_sym_tab->list (top_len); |
|
743 if (top_level_sym_tab != curr_sym_tab) |
|
744 lcl = curr_sym_tab->list (lcl_len); |
339
|
745 ffl = get_fcn_file_names (ffl_len, 1); |
1
|
746 |
339
|
747 int total_len = key_len + glb_len + top_len + lcl_len + ffl_len; |
1
|
748 |
|
749 char **list = new char * [total_len+1]; |
|
750 |
244
|
751 // Put all the symbols in one big list. Only copy pointers, not the |
|
752 // strings they point to, then only delete the original array of |
|
753 // pointers, and not the strings they point to. |
|
754 |
1
|
755 int j = 0; |
|
756 int i = 0; |
|
757 for (i = 0; i < key_len; i++) |
244
|
758 list[j++] = key[i]; |
1
|
759 |
|
760 for (i = 0; i < glb_len; i++) |
244
|
761 list[j++] = glb[i]; |
1
|
762 |
|
763 for (i = 0; i < top_len; i++) |
244
|
764 list[j++] = top[i]; |
1
|
765 |
|
766 for (i = 0; i < lcl_len; i++) |
244
|
767 list[j++] = lcl[i]; |
1
|
768 |
339
|
769 for (i = 0; i < ffl_len; i++) |
|
770 list[j++] = ffl[i]; |
1
|
771 |
529
|
772 list[j] = 0; |
1
|
773 |
|
774 delete [] key; |
|
775 delete [] glb; |
|
776 delete [] top; |
|
777 delete [] lcl; |
339
|
778 delete [] ffl; |
1
|
779 |
|
780 return list; |
|
781 } |
|
782 |
529
|
783 int |
|
784 is_text_function_name (const char *s) |
|
785 { |
|
786 symbol_record *sr = global_sym_tab->lookup (s); |
|
787 return (sr && sr->is_text_function ()); |
|
788 } |
|
789 |
|
790 void |
|
791 install_builtin_variables (void) |
|
792 { |
|
793 // XXX FIXME XX -- these should probably be moved to where they |
|
794 // logically belong instead of being all grouped here. |
|
795 |
|
796 DEFVAR ("EDITOR", SBV_EDITOR, editor, 0, 0, 1, sv_editor, |
|
797 "name of the editor to be invoked by the edit_history command"); |
|
798 |
|
799 DEFVAR ("I", SBV_I, Complex (0.0, 1.0), 0, 1, 1, 0, |
|
800 "sqrt (-1)"); |
|
801 |
|
802 DEFVAR ("Inf", SBV_Inf, octave_Inf, 0, 1, 1, 0, |
|
803 "infinity"); |
|
804 |
|
805 DEFVAR ("INFO_FILE", SBV_INFO_FILE, info_file, 0, 0, 1, sv_info_file, |
|
806 "name of the Octave info file"); |
|
807 |
|
808 DEFVAR ("J", SBV_J, Complex (0.0, 1.0), 0, 1, 1, 0, |
|
809 "sqrt (-1)"); |
|
810 |
|
811 #if defined (HAVE_ISNAN) |
|
812 DEFVAR ("NaN", SBV_NaN, octave_NaN, 0, 1, 1, 0, |
|
813 "not a number"); |
|
814 #endif |
|
815 |
|
816 DEFVAR ("LOADPATH", SBV_LOADPATH, load_path, 0, 0, 1, sv_loadpath, |
|
817 "colon separated list of directories to search for scripts"); |
|
818 |
|
819 DEFVAR ("PAGER", SBV_PAGER, default_pager (), 0, 0, 1, sv_pager_binary, |
|
820 "path to pager binary"); |
|
821 |
|
822 DEFVAR ("PS1", SBV_PS1, "\\s:\\#> ", 0, 0, 1, sv_ps1, |
|
823 "primary prompt string"); |
|
824 |
|
825 DEFVAR ("PS2", SBV_PS2, "> ", 0, 0, 1, sv_ps2, |
|
826 "secondary prompt string"); |
|
827 |
|
828 DEFVAR ("PWD", SBV_PWD, get_working_directory ("initialize_globals"), |
|
829 0, 1, 1, sv_pwd, |
|
830 "current working directory"); |
|
831 |
|
832 DEFVAR ("SEEK_SET", SBV_SEEK_SET, 0.0, 0, 1, 1, 0, |
|
833 "used with fseek to position file relative to the beginning"); |
|
834 |
|
835 DEFVAR ("SEEK_CUR", SBV_SEEK_CUR, 1.0, 0, 1, 1, 0, |
|
836 "used with fseek to position file relative to the current position"); |
|
837 |
|
838 DEFVAR ("SEEK_END", SBV_SEEK_END, 2.0, 0, 1, 1, 0, |
|
839 "used with fseek to position file relative to the end"); |
|
840 |
|
841 DEFVAR ("ans", SBV_ans, , 0, 0, 1, 0, |
|
842 ""); |
|
843 |
|
844 DEFVAR ("commas_in_literal_matrix", SBV_commas_in_literal_matrix, "", |
|
845 0, 0, 1, commas_in_literal_matrix, |
|
846 "control auto-insertion of commas in literal matrices"); |
|
847 |
|
848 DEFVAR ("do_fortran_indexing", SBV_do_fortran_indexing, "false", 0, 0, |
|
849 1, do_fortran_indexing, |
|
850 "allow single indices for matrices"); |
|
851 |
|
852 DEFVAR ("empty_list_elements_ok", SBV_empty_list_elements_ok, "warn", |
|
853 0, 0, 1, empty_list_elements_ok, |
|
854 "ignore the empty element in expressions like `a = [[], 1]'"); |
|
855 |
|
856 DEFVAR ("eps", SBV_eps, DBL_EPSILON, 0, 1, 1, 0, |
|
857 "machine precision"); |
|
858 |
|
859 DEFVAR ("gnuplot_binary", SBV_gnuplot_binary, "gnuplot", 0, 0, 1, |
|
860 sv_gnuplot_binary, |
|
861 "path to gnuplot binary"); |
|
862 |
|
863 DEFVAR ("i", SBV_i, Complex (0.0, 1.0), 1, 1, 1, 0, |
|
864 "sqrt (-1)"); |
|
865 |
|
866 DEFVAR ("ignore_function_time_stamp", SBV_ignore_function_time_stamp, |
|
867 "system", 0, 0, 1, |
|
868 ignore_function_time_stamp, |
|
869 "don't check to see if function files have changed since they were\n\ |
|
870 last compiled. Possible values are \"system\" and \"all\""); |
|
871 |
|
872 DEFVAR ("implicit_str_to_num_ok", SBV_implicit_str_to_num_ok, "false", |
|
873 0, 0, 1, implicit_str_to_num_ok, |
|
874 "allow implicit string to number conversion"); |
|
875 |
|
876 DEFVAR ("inf", SBV_inf, octave_Inf, 0, 1, 1, 0, |
|
877 "infinity"); |
|
878 |
|
879 DEFVAR ("j", SBV_j, Complex (0.0, 1.0), 1, 1, 1, 0, |
|
880 "sqrt (-1)"); |
|
881 |
|
882 #if defined (HAVE_ISNAN) |
|
883 DEFVAR ("nan", SBV_nan, octave_NaN, 0, 1, 1, 0, |
|
884 "not a number"); |
|
885 #endif |
|
886 |
|
887 DEFVAR ("ok_to_lose_imaginary_part", SBV_ok_to_lose_imaginary_part, |
|
888 "warn", 0, 0, 1, ok_to_lose_imaginary_part, |
|
889 "silently convert from complex to real by dropping imaginary part"); |
|
890 |
|
891 DEFVAR ("output_max_field_width", SBV_output_max_field_width, 10.0, 0, |
|
892 0, 1, set_output_max_field_width, |
|
893 "maximum width of an output field for numeric output"); |
|
894 |
|
895 DEFVAR ("output_precision", SBV_output_precision, 5.0, 0, 0, 1, |
|
896 set_output_precision, |
|
897 "number of significant figures to display for numeric output"); |
|
898 |
|
899 DEFVAR ("page_screen_output", SBV_page_screen_output, "true", 0, 0, 1, |
|
900 page_screen_output, |
|
901 "if possible, send output intended for the screen through the pager"); |
|
902 |
|
903 DEFVAR ("pi", SBV_pi, 4.0 * atan (1.0), 0, 1, 1, 0, |
|
904 "ratio of the circumference of a circle to its diameter"); |
|
905 |
|
906 DEFVAR ("prefer_column_vectors", SBV_prefer_column_vectors, "true", 0, |
|
907 0, 1, prefer_column_vectors, |
|
908 "prefer column/row vectors"); |
|
909 |
|
910 DEFVAR ("prefer_zero_one_indexing", SBV_prefer_zero_one_indexing, |
|
911 "false", 0, 0, 1, prefer_zero_one_indexing, |
|
912 "when there is a conflict, prefer zero-one style indexing"); |
|
913 |
|
914 DEFVAR ("print_answer_id_name", SBV_print_answer_id_name, "true", 0, |
|
915 0, 1, print_answer_id_name, |
|
916 "set output style to print `var_name = ...'"); |
|
917 |
|
918 DEFVAR ("print_empty_dimensions", SBV_print_empty_dimensions, "true", |
|
919 0, 0, 1, print_empty_dimensions, |
|
920 "also print dimensions of empty matrices"); |
|
921 |
|
922 DEFVAR ("propagate_empty_matrices", SBV_propagate_empty_matrices, |
|
923 "true", 0, 0, 1, propagate_empty_matrices, |
|
924 "operations on empty matrices return an empty matrix, not an error"); |
|
925 |
|
926 DEFVAR ("resize_on_range_error", SBV_resize_on_range_error, "true", 0, |
|
927 0, 1, resize_on_range_error, |
|
928 "enlarge matrices on assignment"); |
|
929 |
|
930 DEFVAR ("return_last_computed_value", SBV_return_last_computed_value, |
|
931 "false", 0, 0, 1, |
|
932 return_last_computed_value, |
|
933 "if a function does not return any values explicitly, return the\n\ |
|
934 last computed value"); |
|
935 |
|
936 DEFVAR ("save_precision", SBV_save_precision, 17.0, 0, 0, 1, |
|
937 set_save_precision, |
|
938 "number of significant figures kept by the ASCII save command"); |
|
939 |
|
940 DEFVAR ("silent_functions", SBV_silent_functions, "false", 0, 0, 1, |
|
941 silent_functions, |
|
942 "suppress printing results in called functions"); |
|
943 |
|
944 DEFVAR ("split_long_rows", SBV_split_long_rows, "true", 0, 0, 1, |
|
945 split_long_rows, |
|
946 "split long matrix rows instead of wrapping"); |
|
947 |
|
948 DEFVAR ("stdin", SBV_stdin, 0.0, 0, 1, 1, 0, |
|
949 "file number of the standard input stream"); |
|
950 |
|
951 DEFVAR ("stdout", SBV_stdout, 1.0, 0, 1, 1, 0, |
|
952 "file number of the standard output stream"); |
|
953 |
|
954 DEFVAR ("stderr", SBV_stderr, 2.0, 0, 1, 1, 0, |
|
955 "file number of the standard error stream"); |
|
956 |
|
957 DEFVAR ("treat_neg_dim_as_zero", SBV_treat_neg_dim_as_zero, "false", |
|
958 0, 0, 1, treat_neg_dim_as_zero, |
|
959 "convert negative dimensions to zero"); |
|
960 |
|
961 DEFVAR ("warn_assign_as_truth_value", SBV_warn_assign_as_truth_value, |
|
962 "true", 0, 0, 1, |
|
963 warn_assign_as_truth_value, |
|
964 "produce warning for assignments used as truth values"); |
|
965 |
|
966 DEFVAR ("warn_comma_in_global_decl", SBV_warn_comma_in_global_decl, |
|
967 "true", 0, 0, 1, warn_comma_in_global_decl, |
|
968 "produce warning for commas in global declarations"); |
|
969 |
|
970 DEFVAR ("warn_divide_by_zero", SBV_warn_divide_by_zero, "true", 0, 0, |
|
971 1, warn_divide_by_zero, |
|
972 "on IEEE machines, allow divide by zero errors to be suppressed"); |
|
973 } |
|
974 |
|
975 /* |
|
976 * List variable names. |
|
977 */ |
|
978 static void |
|
979 print_symbol_info_line (ostrstream& output_buf, const symbol_record_info& s) |
|
980 { |
|
981 output_buf << (s.is_read_only () ? " -" : " w"); |
|
982 output_buf << (s.is_eternal () ? "- " : "d "); |
|
983 #if 0 |
|
984 output_buf << (s.hides_fcn () ? "f" : (s.hides_builtin () ? "F" : "-")); |
|
985 #endif |
|
986 output_buf.form (" %-16s", s.type_as_string ()); |
|
987 if (s.is_function ()) |
|
988 output_buf << " - -"; |
|
989 else |
|
990 { |
|
991 output_buf.form ("%7d", s.rows ()); |
|
992 output_buf.form ("%7d", s.columns ()); |
|
993 } |
|
994 output_buf << " " << s.name () << "\n"; |
|
995 } |
|
996 |
|
997 static void |
|
998 print_long_listing (ostrstream& output_buf, symbol_record_info *s) |
|
999 { |
|
1000 if (! s) |
|
1001 return; |
|
1002 |
|
1003 symbol_record_info *ptr = s; |
|
1004 while (ptr->is_defined ()) |
|
1005 { |
|
1006 print_symbol_info_line (output_buf, *ptr); |
|
1007 ptr++; |
|
1008 } |
|
1009 } |
|
1010 |
|
1011 static int |
|
1012 maybe_list (const char *header, ostrstream& output_buf, |
|
1013 int show_verbose, symbol_table *sym_tab, unsigned type, |
|
1014 unsigned scope) |
|
1015 { |
|
1016 int count; |
|
1017 int status = 0; |
|
1018 if (show_verbose) |
|
1019 { |
|
1020 symbol_record_info *symbols; |
|
1021 symbols = sym_tab->long_list (count, 1, type, scope); |
|
1022 if (symbols && count > 0) |
|
1023 { |
|
1024 output_buf << "\n" << header << "\n\n" |
|
1025 << "prot type rows cols name\n" |
|
1026 << "==== ==== ==== ==== ====\n"; |
|
1027 |
|
1028 print_long_listing (output_buf, symbols); |
|
1029 status = 1; |
|
1030 } |
|
1031 delete [] symbols; |
|
1032 } |
|
1033 else |
|
1034 { |
|
1035 char **symbols = sym_tab->list (count, 1, type, scope); |
|
1036 if (symbols && count > 0) |
|
1037 { |
|
1038 output_buf << "\n" << header << "\n\n"; |
|
1039 list_in_columns (output_buf, symbols); |
|
1040 status = 1; |
|
1041 } |
|
1042 delete [] symbols; |
|
1043 } |
|
1044 return status; |
|
1045 } |
|
1046 |
|
1047 DEFUN_TEXT ("clear", Fclear, Sclear, -1, 1, |
|
1048 "clear [name ...]\n\ |
|
1049 \n\ |
|
1050 clear symbol(s) matching a list of globbing patterns\n\ |
|
1051 if no arguments are given, clear all user-defined variables and functions") |
|
1052 { |
|
1053 Octave_object retval; |
|
1054 |
|
1055 DEFINE_ARGV("clear"); |
|
1056 |
|
1057 // Always clear the local table, but don't clear currently compiled |
|
1058 // functions unless we are at the top level. (Allowing that to happen |
|
1059 // inside functions would result in pretty odd behavior...) |
|
1060 |
|
1061 int clear_user_functions = (curr_sym_tab == top_level_sym_tab); |
|
1062 |
|
1063 if (argc == 1) |
|
1064 { |
|
1065 curr_sym_tab->clear (); |
|
1066 global_sym_tab->clear (clear_user_functions); |
|
1067 } |
|
1068 else |
|
1069 { |
|
1070 int lcount; |
|
1071 char **lvars = curr_sym_tab->list (lcount, 0, |
|
1072 symbol_def::USER_VARIABLE, |
|
1073 SYMTAB_LOCAL_SCOPE); |
|
1074 int gcount; |
|
1075 char **gvars = curr_sym_tab->list (gcount, 0, |
|
1076 symbol_def::USER_VARIABLE, |
|
1077 SYMTAB_GLOBAL_SCOPE); |
|
1078 int fcount; |
|
1079 char **fcns = curr_sym_tab->list (fcount, 0, |
|
1080 symbol_def::USER_FUNCTION, |
|
1081 SYMTAB_ALL_SCOPES); |
|
1082 |
|
1083 while (--argc > 0) |
|
1084 { |
|
1085 argv++; |
|
1086 if (*argv) |
|
1087 { |
|
1088 int i; |
|
1089 for (i = 0; i < lcount; i++) |
|
1090 { |
|
1091 if (fnmatch (*argv, lvars[i], __FNM_FLAGS) == 0) |
|
1092 curr_sym_tab->clear (lvars[i]); |
|
1093 } |
|
1094 |
|
1095 int count; |
|
1096 for (i = 0; i < gcount; i++) |
|
1097 { |
|
1098 if (fnmatch (*argv, gvars[i], __FNM_FLAGS) == 0) |
|
1099 { |
|
1100 count = curr_sym_tab->clear (gvars[i]); |
|
1101 if (count > 0) |
|
1102 global_sym_tab->clear (gvars[i], clear_user_functions); |
|
1103 } |
|
1104 } |
|
1105 |
|
1106 for (i = 0; i < fcount; i++) |
|
1107 { |
|
1108 if (fnmatch (*argv, fcns[i], __FNM_FLAGS) == 0) |
|
1109 { |
|
1110 count = curr_sym_tab->clear (fcns[i]); |
|
1111 if (count > 0) |
|
1112 global_sym_tab->clear (fcns[i], clear_user_functions); |
|
1113 } |
|
1114 } |
|
1115 } |
|
1116 } |
|
1117 |
|
1118 delete [] lvars; |
|
1119 delete [] gvars; |
|
1120 delete [] fcns; |
|
1121 |
|
1122 } |
|
1123 |
|
1124 DELETE_ARGV; |
|
1125 |
|
1126 return retval; |
|
1127 } |
|
1128 |
|
1129 DEFUN_TEXT ("document", Fdocument, Sdocument, -1, 1, |
|
1130 "document symbol string ...\n\ |
|
1131 \n\ |
|
1132 Associate a cryptic message with a variable name.") |
|
1133 { |
|
1134 Octave_object retval; |
|
1135 |
|
1136 DEFINE_ARGV("document"); |
|
1137 |
|
1138 if (argc == 3) |
|
1139 document_symbol (argv[1], argv[2]); |
|
1140 else |
|
1141 print_usage ("document"); |
|
1142 |
|
1143 DELETE_ARGV; |
|
1144 |
|
1145 return retval; |
|
1146 } |
|
1147 |
|
1148 static int |
|
1149 load_variable (char *nm, int force, istream& is) |
|
1150 { |
|
1151 // Is there already a symbol by this name? If so, what is it? |
|
1152 |
|
1153 symbol_record *lsr = curr_sym_tab->lookup (nm, 0, 0); |
|
1154 |
|
1155 int is_undefined = 1; |
|
1156 int is_variable = 0; |
|
1157 int is_function = 0; |
|
1158 int is_global = 0; |
|
1159 |
|
1160 if (lsr) |
|
1161 { |
|
1162 is_undefined = ! lsr->is_defined (); |
|
1163 is_variable = lsr->is_variable (); |
|
1164 is_function = lsr->is_function (); |
|
1165 is_global = lsr->is_linked_to_global (); |
|
1166 } |
|
1167 |
|
1168 // Try to read data for this name. |
|
1169 |
|
1170 tree_constant tc; |
|
1171 int global = tc.load (is); |
|
1172 |
|
1173 if (tc.const_type () == tree_constant_rep::unknown_constant) |
|
1174 { |
|
1175 error ("load: unable to load variable `%s'", nm); |
|
1176 return 0; |
|
1177 } |
|
1178 |
|
1179 symbol_record *sr = 0; |
|
1180 |
|
1181 if (global) |
|
1182 { |
|
1183 if (is_global || is_undefined) |
|
1184 { |
|
1185 if (force || is_undefined) |
|
1186 { |
|
1187 lsr = curr_sym_tab->lookup (nm, 1, 0); |
|
1188 link_to_global_variable (lsr); |
|
1189 sr = lsr; |
|
1190 } |
|
1191 else |
|
1192 { |
|
1193 warning ("load: global variable name `%s' exists.", nm); |
|
1194 warning ("use `load -force' to overwrite"); |
|
1195 } |
|
1196 } |
|
1197 else if (is_function) |
|
1198 { |
|
1199 if (force) |
|
1200 { |
|
1201 lsr = curr_sym_tab->lookup (nm, 1, 0); |
|
1202 link_to_global_variable (lsr); |
|
1203 sr = lsr; |
|
1204 } |
|
1205 else |
|
1206 { |
|
1207 warning ("load: `%s' is currently a function in this scope", nm); |
|
1208 warning ("`load -force' will load variable and hide function"); |
|
1209 } |
|
1210 } |
|
1211 else if (is_variable) |
|
1212 { |
|
1213 if (force) |
|
1214 { |
|
1215 lsr = curr_sym_tab->lookup (nm, 1, 0); |
|
1216 link_to_global_variable (lsr); |
|
1217 sr = lsr; |
|
1218 } |
|
1219 else |
|
1220 { |
|
1221 warning ("load: local variable name `%s' exists.", nm); |
|
1222 warning ("use `load -force' to overwrite"); |
|
1223 } |
|
1224 } |
|
1225 else |
|
1226 panic_impossible (); |
|
1227 } |
|
1228 else |
|
1229 { |
|
1230 if (is_global) |
|
1231 { |
|
1232 if (force || is_undefined) |
|
1233 { |
|
1234 lsr = curr_sym_tab->lookup (nm, 1, 0); |
|
1235 link_to_global_variable (lsr); |
|
1236 sr = lsr; |
|
1237 } |
|
1238 else |
|
1239 { |
|
1240 warning ("load: global variable name `%s' exists.", nm); |
|
1241 warning ("use `load -force' to overwrite"); |
|
1242 } |
|
1243 } |
|
1244 else if (is_function) |
|
1245 { |
|
1246 if (force) |
|
1247 { |
|
1248 lsr = curr_sym_tab->lookup (nm, 1, 0); |
|
1249 link_to_global_variable (lsr); |
|
1250 sr = lsr; |
|
1251 } |
|
1252 else |
|
1253 { |
|
1254 warning ("load: `%s' is currently a function in this scope", nm); |
|
1255 warning ("`load -force' will load variable and hide function"); |
|
1256 } |
|
1257 } |
|
1258 else if (is_variable || is_undefined) |
|
1259 { |
|
1260 if (force || is_undefined) |
|
1261 { |
|
1262 lsr = curr_sym_tab->lookup (nm, 1, 0); |
|
1263 sr = lsr; |
|
1264 } |
|
1265 else |
|
1266 { |
|
1267 warning ("load: local variable name `%s' exists.", nm); |
|
1268 warning ("use `load -force' to overwrite"); |
|
1269 } |
|
1270 } |
|
1271 else |
|
1272 panic_impossible (); |
|
1273 } |
|
1274 |
|
1275 if (sr) |
|
1276 { |
|
1277 tree_constant *tmp_tc = new tree_constant (tc); |
|
1278 sr->define (tmp_tc); |
|
1279 return 1; |
|
1280 } |
|
1281 else |
|
1282 error ("load: unable to load variable `%s'", nm); |
|
1283 |
|
1284 return 0; |
|
1285 } |
|
1286 |
|
1287 DEFUN_TEXT ("load", Fload, Sload, -1, 1, |
|
1288 "load [-force] file\n |
|
1289 \n\ |
|
1290 load variables from a file") |
|
1291 { |
|
1292 Octave_object retval; |
|
1293 |
|
1294 DEFINE_ARGV("load"); |
|
1295 |
|
1296 argc--; |
|
1297 argv++; |
|
1298 |
|
1299 int force = 0; |
|
1300 if (argc > 0 && strcmp (*argv, "-force") == 0) |
|
1301 { |
|
1302 force++; |
|
1303 argc--; |
|
1304 argv++; |
|
1305 } |
|
1306 |
|
1307 if (argc < 1) |
|
1308 { |
|
1309 error ("load: you must specify a single file to read"); |
|
1310 DELETE_ARGV; |
|
1311 return retval; |
|
1312 } |
|
1313 |
|
1314 static istream stream; |
|
1315 static ifstream file; |
|
1316 if (strcmp (*argv, "-") == 0) |
|
1317 { |
|
1318 stream = cin; |
|
1319 } |
|
1320 else |
|
1321 { |
|
1322 char *fname = tilde_expand (*argv); |
|
1323 file.open (fname); |
|
1324 if (! file) |
|
1325 { |
|
1326 error ("load: couldn't open input file `%s'", *argv); |
|
1327 DELETE_ARGV; |
|
1328 return retval; |
|
1329 } |
|
1330 stream = file; |
|
1331 } |
|
1332 |
|
1333 int count = 0; |
|
1334 char *nm = 0; |
|
1335 for (;;) |
|
1336 { |
|
1337 // Read name for this entry or break on EOF. |
|
1338 delete [] nm; |
|
1339 nm = extract_keyword (stream, "name"); |
|
1340 if (nm) |
|
1341 count++; |
|
1342 else |
|
1343 { |
|
1344 if (count == 0) |
|
1345 { |
|
1346 error ("load: no name keywords found in file `%s'", *argv); |
|
1347 error ("Are you sure this is an octave data file?"); |
|
1348 } |
|
1349 break; |
|
1350 } |
|
1351 |
|
1352 if (! *nm) |
|
1353 continue; |
|
1354 |
|
1355 if (! valid_identifier (nm)) |
|
1356 { |
|
1357 warning ("load: skipping bogus identifier `%s'"); |
|
1358 continue; |
|
1359 } |
|
1360 |
|
1361 load_variable (nm, force, stream); |
|
1362 |
|
1363 if (error_state) |
|
1364 { |
|
1365 error ("reading file %s", *argv); |
|
1366 break; |
|
1367 } |
|
1368 } |
|
1369 |
|
1370 if (file); |
|
1371 file.close (); |
|
1372 |
|
1373 DELETE_ARGV; |
|
1374 |
|
1375 return retval; |
|
1376 } |
|
1377 |
|
1378 /* |
|
1379 * Return nonzero if PATTERN has any special globbing chars in it. |
|
1380 */ |
|
1381 static int |
|
1382 glob_pattern_p (char *pattern) |
|
1383 { |
|
1384 char *p = pattern; |
|
1385 char c; |
|
1386 int open = 0; |
|
1387 |
|
1388 while ((c = *p++) != '\0') |
|
1389 { |
|
1390 switch (c) |
|
1391 { |
|
1392 case '?': |
|
1393 case '*': |
|
1394 return 1; |
|
1395 |
|
1396 case '[': // Only accept an open brace if there is a close |
|
1397 open++; // brace to match it. Bracket expressions must be |
|
1398 continue; // complete, according to Posix.2 |
|
1399 |
|
1400 case ']': |
|
1401 if (open) |
|
1402 return 1; |
|
1403 continue; |
|
1404 |
|
1405 case '\\': |
|
1406 if (*p++ == '\0') |
|
1407 return 0; |
|
1408 |
|
1409 default: |
|
1410 continue; |
|
1411 } |
|
1412 } |
|
1413 |
|
1414 return 0; |
|
1415 } |
|
1416 |
549
|
1417 static int |
|
1418 is_globally_visible (const char *name) |
|
1419 { |
|
1420 symbol_record *sr = curr_sym_tab->lookup (name, 0, 0); |
|
1421 return (sr && sr->is_linked_to_global ()); |
|
1422 } |
|
1423 |
529
|
1424 DEFUN_TEXT ("save", Fsave, Ssave, -1, 1, |
|
1425 "save file [var ...]\n\ |
|
1426 \n\ |
|
1427 save variables in a file") |
|
1428 { |
|
1429 Octave_object retval; |
|
1430 |
|
1431 DEFINE_ARGV("save"); |
|
1432 |
|
1433 if (argc < 2) |
|
1434 { |
|
1435 print_usage ("save"); |
|
1436 DELETE_ARGV; |
|
1437 return retval; |
|
1438 } |
|
1439 |
|
1440 argc--; |
|
1441 argv++; |
|
1442 |
|
1443 static ostream stream; |
|
1444 static ofstream file; |
|
1445 if (strcmp (*argv, "-") == 0) |
|
1446 { |
|
1447 // XXX FIXME XXX -- should things intended for the screen end up in a |
|
1448 // tree_constant (string)? |
|
1449 stream = cout; |
|
1450 } |
|
1451 else if (argc == 1 && glob_pattern_p (*argv)) // Guard against things |
|
1452 { // like `save a*', |
|
1453 print_usage ("save"); // which are probably |
|
1454 DELETE_ARGV; // mistakes... |
|
1455 return retval; |
|
1456 } |
|
1457 else |
|
1458 { |
|
1459 char *fname = tilde_expand (*argv); |
|
1460 file.open (fname); |
|
1461 if (! file) |
|
1462 { |
|
1463 error ("save: couldn't open output file `%s'", *argv); |
|
1464 DELETE_ARGV; |
|
1465 return retval; |
|
1466 } |
|
1467 stream = file; |
|
1468 |
|
1469 } |
|
1470 |
|
1471 int prec = user_pref.save_precision; |
|
1472 |
|
1473 if (argc == 1) |
|
1474 { |
|
1475 int count; |
|
1476 char **vars = curr_sym_tab->list (count, 0, |
|
1477 symbol_def::USER_VARIABLE, |
|
1478 SYMTAB_ALL_SCOPES); |
|
1479 |
|
1480 for (int i = 0; i < count; i++) |
|
1481 curr_sym_tab->save (stream, vars[i], |
|
1482 is_globally_visible (vars[i]), prec); |
|
1483 |
|
1484 delete [] vars; |
|
1485 } |
|
1486 else |
|
1487 { |
|
1488 while (--argc > 0) |
|
1489 { |
|
1490 argv++; |
|
1491 |
|
1492 int count; |
|
1493 char **lvars = curr_sym_tab->list (count, 0, |
|
1494 symbol_def::USER_VARIABLE); |
|
1495 |
|
1496 int saved_or_error = 0; |
|
1497 int i; |
|
1498 for (i = 0; i < count; i++) |
|
1499 { |
|
1500 if (fnmatch (*argv, lvars[i], __FNM_FLAGS) == 0 |
|
1501 && curr_sym_tab->save (stream, lvars[i], |
|
1502 is_globally_visible (lvars[i]), |
|
1503 prec) != 0) |
|
1504 saved_or_error++; |
|
1505 } |
|
1506 |
|
1507 char **bvars = global_sym_tab->list (count, 0, |
|
1508 symbol_def::BUILTIN_VARIABLE); |
|
1509 |
|
1510 for (i = 0; i < count; i++) |
|
1511 { |
|
1512 if (fnmatch (*argv, bvars[i], __FNM_FLAGS) == 0 |
|
1513 && global_sym_tab->save (stream, bvars[i], 0, prec) != 0) |
|
1514 saved_or_error++; |
|
1515 } |
|
1516 |
|
1517 delete [] lvars; |
|
1518 delete [] bvars; |
|
1519 |
|
1520 if (! saved_or_error) |
|
1521 warning ("save: no such variable `%s'", *argv); |
|
1522 } |
|
1523 } |
|
1524 |
|
1525 if (file); |
|
1526 file.close (); |
|
1527 |
|
1528 DELETE_ARGV; |
|
1529 |
|
1530 return retval; |
|
1531 } |
|
1532 |
|
1533 DEFUN_TEXT ("who", Fwho, Swho, -1, 1, |
|
1534 "who [-all] [-builtins] [-functions] [-long] [-variables]\n\ |
|
1535 \n\ |
|
1536 List currently defined symbol(s). Options may be shortened to one\n\ |
|
1537 character, but may not be combined.") |
|
1538 { |
|
1539 Octave_object retval; |
|
1540 |
|
1541 DEFINE_ARGV("who"); |
|
1542 |
|
1543 int show_builtins = 0; |
|
1544 int show_functions = (curr_sym_tab == top_level_sym_tab); |
|
1545 int show_variables = 1; |
|
1546 int show_verbose = 0; |
|
1547 |
|
1548 if (argc > 1) |
|
1549 { |
|
1550 show_functions = 0; |
|
1551 show_variables = 0; |
|
1552 } |
|
1553 |
|
1554 for (int i = 1; i < argc; i++) |
|
1555 { |
|
1556 argv++; |
|
1557 if (strcmp (*argv, "-all") == 0 || strcmp (*argv, "-a") == 0) |
|
1558 { |
|
1559 show_builtins++; |
|
1560 show_functions++; |
|
1561 show_variables++; |
|
1562 } |
|
1563 else if (strcmp (*argv, "-builtins") == 0 |
|
1564 || strcmp (*argv, "-b") == 0) |
|
1565 show_builtins++; |
|
1566 else if (strcmp (*argv, "-functions") == 0 |
|
1567 || strcmp (*argv, "-f") == 0) |
|
1568 show_functions++; |
|
1569 else if (strcmp (*argv, "-long") == 0 |
|
1570 || strcmp (*argv, "-l") == 0) |
|
1571 show_verbose++; |
|
1572 else if (strcmp (*argv, "-variables") == 0 |
|
1573 || strcmp (*argv, "-v") == 0) |
|
1574 show_variables++; |
|
1575 else |
|
1576 warning ("who: unrecognized option `%s'", *argv); |
|
1577 } |
|
1578 |
|
1579 // If the user specified -l and nothing else, show variables. If |
|
1580 // evaluating this at the top level, also show functions. |
|
1581 |
|
1582 if (show_verbose && ! (show_builtins || show_functions || show_variables)) |
|
1583 { |
|
1584 show_functions = (curr_sym_tab == top_level_sym_tab); |
|
1585 show_variables = 1; |
|
1586 } |
|
1587 |
|
1588 ostrstream output_buf; |
|
1589 int pad_after = 0; |
|
1590 |
|
1591 if (show_builtins) |
|
1592 { |
|
1593 pad_after += maybe_list ("*** built-in variables:", |
|
1594 output_buf, show_verbose, global_sym_tab, |
|
1595 symbol_def::BUILTIN_VARIABLE, |
|
1596 SYMTAB_ALL_SCOPES); |
|
1597 |
|
1598 pad_after += maybe_list ("*** built-in functions:", |
|
1599 output_buf, show_verbose, global_sym_tab, |
|
1600 symbol_def::BUILTIN_FUNCTION, |
|
1601 SYMTAB_ALL_SCOPES); |
|
1602 } |
|
1603 |
|
1604 if (show_functions) |
|
1605 { |
|
1606 pad_after += maybe_list ("*** currently compiled functions:", |
|
1607 output_buf, show_verbose, global_sym_tab, |
|
1608 symbol_def::USER_FUNCTION, |
|
1609 SYMTAB_ALL_SCOPES); |
|
1610 } |
|
1611 |
|
1612 if (show_variables) |
|
1613 { |
|
1614 pad_after += maybe_list ("*** local user variables:", |
|
1615 output_buf, show_verbose, curr_sym_tab, |
|
1616 symbol_def::USER_VARIABLE, |
|
1617 SYMTAB_LOCAL_SCOPE); |
|
1618 |
|
1619 pad_after += maybe_list ("*** globally visible user variables:", |
|
1620 output_buf, show_verbose, curr_sym_tab, |
|
1621 symbol_def::USER_VARIABLE, |
|
1622 SYMTAB_GLOBAL_SCOPE); |
|
1623 } |
|
1624 |
|
1625 if (pad_after) |
|
1626 output_buf << "\n"; |
|
1627 |
|
1628 output_buf << ends; |
|
1629 maybe_page_output (output_buf); |
|
1630 |
|
1631 DELETE_ARGV; |
|
1632 |
|
1633 return retval; |
|
1634 } |
|
1635 |
|
1636 // XXX FIXME XXX -- should these really be here? |
|
1637 |
|
1638 char * |
|
1639 octave_home (void) |
|
1640 { |
|
1641 #ifdef RUN_IN_PLACE |
|
1642 static char *home = OCTAVE_HOME; |
|
1643 return home; |
|
1644 #else |
|
1645 static char *home = 0; |
|
1646 delete [] home; |
|
1647 char *oh = getenv ("OCTAVE_HOME"); |
|
1648 if (oh) |
|
1649 home = strsave (oh); |
|
1650 else |
|
1651 home = strsave (OCTAVE_HOME); |
|
1652 return home; |
|
1653 #endif |
|
1654 } |
|
1655 |
|
1656 char * |
|
1657 octave_lib_dir (void) |
|
1658 { |
|
1659 #ifdef RUN_IN_PLACE |
|
1660 static char *ol = OCTAVE_LIB_DIR; |
|
1661 return ol; |
|
1662 #else |
|
1663 static char *ol = 0; |
|
1664 delete [] ol; |
|
1665 char *oh = octave_home (); |
|
1666 char *tmp = strconcat (oh, "/lib/octave/"); |
|
1667 ol = strconcat (tmp, version_string); |
|
1668 delete [] tmp; |
|
1669 return ol; |
|
1670 #endif |
|
1671 } |
|
1672 |
|
1673 char * |
|
1674 octave_info_dir (void) |
|
1675 { |
|
1676 #ifdef RUN_IN_PLACE |
|
1677 static char *oi = OCTAVE_INFO_DIR; |
|
1678 return oi; |
|
1679 #else |
|
1680 static char *oi = 0; |
|
1681 delete [] oi; |
|
1682 char *oh = octave_home (); |
|
1683 oi = strconcat (oh, "/info/"); |
|
1684 return oi; |
|
1685 #endif |
|
1686 } |
|
1687 |
|
1688 /* |
|
1689 * Handle OCTAVE_PATH from the environment like TeX handles TEXINPUTS. |
|
1690 * If the path starts with `:', prepend the standard path. If it ends |
|
1691 * with `:' append the standard path. If it begins and ends with |
|
1692 * `:', do both (which is useless, but the luser asked for it...). |
|
1693 * |
|
1694 * This function may eventually be called more than once, so be |
|
1695 * careful not to create memory leaks. |
|
1696 */ |
|
1697 char * |
|
1698 default_path (void) |
|
1699 { |
|
1700 static char *pathstring = 0; |
|
1701 delete [] pathstring; |
|
1702 |
|
1703 static char *std_path = 0; |
|
1704 delete [] std_path; |
|
1705 |
|
1706 char *libdir = octave_lib_dir (); |
|
1707 |
|
1708 std_path = strconcat (".:", libdir); |
|
1709 |
|
1710 char *oct_path = getenv ("OCTAVE_PATH"); |
|
1711 |
|
1712 if (oct_path) |
|
1713 { |
|
1714 pathstring = strsave (oct_path); |
|
1715 |
|
1716 if (pathstring[0] == ':') |
|
1717 { |
|
1718 char *tmp = pathstring; |
|
1719 pathstring = strconcat (std_path, pathstring); |
|
1720 delete [] tmp; |
|
1721 } |
|
1722 |
|
1723 int tmp_len = strlen (pathstring); |
|
1724 if (pathstring[tmp_len-1] == ':') |
|
1725 { |
|
1726 char *tmp = pathstring; |
|
1727 pathstring = strconcat (pathstring, std_path); |
|
1728 delete [] tmp; |
|
1729 } |
|
1730 } |
|
1731 else |
|
1732 pathstring = strsave (std_path); |
|
1733 |
|
1734 return pathstring; |
|
1735 } |
|
1736 |
|
1737 char * |
|
1738 default_info_file (void) |
|
1739 { |
|
1740 static char *info_file_string = 0; |
|
1741 delete [] info_file_string; |
|
1742 char *oct_info_file = getenv ("OCTAVE_INFO_FILE"); |
|
1743 if (oct_info_file) |
|
1744 info_file_string = strsave (oct_info_file); |
|
1745 else |
|
1746 { |
|
1747 char *infodir = octave_info_dir (); |
|
1748 info_file_string = strconcat (infodir, "/octave.info"); |
|
1749 } |
|
1750 return info_file_string; |
|
1751 } |
|
1752 |
|
1753 char * |
|
1754 default_editor (void) |
|
1755 { |
|
1756 static char *editor_string = 0; |
|
1757 delete [] editor_string; |
|
1758 char *env_editor = getenv ("EDITOR"); |
|
1759 if (env_editor && *env_editor) |
|
1760 editor_string = strsave (env_editor); |
|
1761 else |
|
1762 editor_string = strsave ("vi"); |
|
1763 return editor_string; |
|
1764 } |
|
1765 |
|
1766 char * |
|
1767 get_site_defaults (void) |
|
1768 { |
|
1769 static char *sd = 0; |
|
1770 delete [] sd; |
|
1771 char *libdir = octave_lib_dir (); |
|
1772 sd = strconcat (libdir, "/octaverc"); |
|
1773 return sd; |
|
1774 } |
|
1775 |
|
1776 char * |
|
1777 default_pager (void) |
|
1778 { |
|
1779 static char *pager_binary = 0; |
|
1780 delete [] pager_binary; |
|
1781 char *pgr = getenv ("PAGER"); |
|
1782 if (pgr) |
|
1783 pager_binary = strsave (pgr); |
|
1784 else |
|
1785 #ifdef DEFAULT_PAGER |
|
1786 pager_binary = strsave (DEFAULT_PAGER); |
|
1787 #else |
|
1788 pager_binary = strsave (""); |
|
1789 #endif |
|
1790 |
|
1791 return pager_binary; |
|
1792 } |
|
1793 |
1
|
1794 /* |
|
1795 ;;; Local Variables: *** |
|
1796 ;;; mode: C++ *** |
|
1797 ;;; page-delimiter: "^/\\*" *** |
|
1798 ;;; End: *** |
|
1799 */ |