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