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