1
|
1 // t-builtins.cc -*- C++ -*- |
|
2 /* |
|
3 |
267
|
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 |
|
24 /* |
|
25 |
|
26 The function builtin_cd was adapted from a similar function from GNU |
|
27 Bash, the Bourne Again SHell, copyright (C) 1987, 1989, 1991 Free |
|
28 Software Foundation, Inc. |
|
29 |
|
30 The function list_in_columns was adapted from a similar function from |
|
31 GNU ls, print_many_per_line, copyright (C) 1985, 1988, 1990, 1991 Free |
|
32 Software Foundation, Inc. |
|
33 |
266
|
34 The function glob_pattern_p was taken from the file glob.c distributed |
|
35 with GNU Bash, the Bourne Again SHell, copyright (C) 1985, 1988, 1989 |
|
36 Free Software Foundation, Inc. |
|
37 |
1
|
38 */ |
|
39 |
240
|
40 #ifdef HAVE_CONFIG_H |
|
41 #include "config.h" |
1
|
42 #endif |
|
43 |
|
44 #include <sys/types.h> |
|
45 #ifdef HAVE_UNISTD_H |
|
46 #include <unistd.h> |
|
47 #endif |
|
48 #include <iostream.h> |
|
49 #include <strstream.h> |
|
50 #include <fstream.h> |
|
51 #include <stdio.h> |
|
52 #include <fcntl.h> |
|
53 #include <sys/file.h> |
|
54 #include <sys/stat.h> |
|
55 #include <time.h> |
|
56 #include <errno.h> |
169
|
57 #include <signal.h> |
1
|
58 |
|
59 #include "procstream.h" |
|
60 |
|
61 #include "variables.h" |
|
62 #include "symtab.h" |
|
63 #include "error.h" |
|
64 #include "input.h" |
|
65 #include "pager.h" |
|
66 #include "utils.h" |
169
|
67 #include "sighandlers.h" |
1
|
68 #include "builtins.h" |
|
69 #include "t-builtins.h" |
|
70 #include "octave.h" |
|
71 #include "octave-hist.h" |
|
72 #include "user-prefs.h" |
|
73 #include "pr-output.h" |
169
|
74 #include "defaults.h" |
1
|
75 #include "tree.h" |
|
76 #include "help.h" |
|
77 |
266
|
78 extern "C" |
|
79 { |
|
80 #include "fnmatch.h" |
|
81 } |
|
82 |
1
|
83 // May need replacement for this on some machines. |
|
84 extern "C" |
|
85 { |
|
86 extern char *strerror (int); |
|
87 char *tilde_expand (char *s); /* From readline's tilde.c */ |
|
88 } |
|
89 |
169
|
90 extern "C" |
|
91 { |
184
|
92 #include "info/info.h" |
|
93 #include "info/dribble.h" |
|
94 #include "info/terminal.h" |
169
|
95 |
|
96 extern int initialize_info_session (); |
|
97 extern int index_entry_exists (); |
|
98 extern int do_info_index_search (); |
|
99 extern void finish_info_session (); |
|
100 extern char *replace_in_documentation (); |
|
101 } |
|
102 |
1
|
103 // Is this a parametric plot? Makes a difference for 3D plotting. |
|
104 extern int parametric_plot; |
|
105 |
|
106 /* |
|
107 * Format a list in neat columns. Mostly stolen from GNU ls. This |
|
108 * should maybe be in utils.cc. |
|
109 */ |
|
110 static ostrstream& |
|
111 list_in_columns (ostrstream& os, char **list) |
|
112 { |
|
113 // Compute the maximum name length. |
|
114 |
|
115 int max_name_length = 0; |
|
116 int total_names = 0; |
|
117 for (char **names = list; *names != (char *) NULL; names++) |
|
118 { |
|
119 total_names++; |
|
120 int name_length = strlen (*names); |
|
121 if (name_length > max_name_length) |
|
122 max_name_length = name_length; |
|
123 } |
|
124 |
|
125 // Allow at least two spaces between names. |
|
126 |
|
127 max_name_length += 2; |
|
128 |
|
129 // Calculate the maximum number of columns that will fit. |
|
130 |
|
131 int line_length = terminal_columns (); |
|
132 int cols = line_length / max_name_length; |
|
133 if (cols == 0) |
|
134 cols = 1; |
|
135 |
|
136 // Calculate the number of rows that will be in each column except |
|
137 // possibly for a short column on the right. |
|
138 |
|
139 int rows = total_names / cols + (total_names % cols != 0); |
|
140 |
|
141 // Recalculate columns based on rows. |
|
142 |
|
143 cols = total_names / rows + (total_names % rows != 0); |
|
144 |
|
145 names = list; |
|
146 int count; |
|
147 for (int row = 0; row < rows; row++) |
|
148 { |
|
149 count = row; |
|
150 int pos = 0; |
|
151 |
|
152 // Print the next row. |
|
153 |
|
154 while (1) |
|
155 { |
|
156 os << *(names + count); |
|
157 int name_length = strlen (*(names + count)); |
|
158 |
|
159 count += rows; |
|
160 if (count >= total_names) |
|
161 break; |
|
162 |
|
163 int spaces_to_pad = max_name_length - name_length; |
|
164 for (int i = 0; i < spaces_to_pad; i++) |
|
165 os << " "; |
|
166 pos += max_name_length; |
|
167 } |
|
168 os << "\n"; |
|
169 } |
|
170 |
|
171 return os; |
|
172 } |
|
173 |
|
174 tree_constant |
|
175 builtin_casesen (int argc, char **argv) |
|
176 { |
|
177 tree_constant retval; |
|
178 |
|
179 if (argc == 1 || (argc > 1 && strcmp (argv[1], "off") == 0)) |
181
|
180 warning ("casesen: sorry, Octave is always case sensitive"); |
1
|
181 else if (argc > 1 && strcmp (argv[1], "on") == 0) |
|
182 ; // ok. |
|
183 else |
181
|
184 print_usage ("casesen"); |
1
|
185 |
|
186 return retval; |
|
187 } |
|
188 |
|
189 /* |
|
190 * Change current working directory. |
|
191 */ |
|
192 tree_constant |
|
193 builtin_cd (int argc, char **argv) |
|
194 { |
|
195 tree_constant retval; |
|
196 |
|
197 if (argc > 1) |
|
198 { |
|
199 static char *dirname = (char *) NULL; |
|
200 |
|
201 if (dirname) |
|
202 free (dirname); |
|
203 |
|
204 dirname = tilde_expand (argv[1]); |
|
205 |
|
206 if (dirname != (char *) NULL && !change_to_directory (dirname)) |
|
207 { |
|
208 error ("%s: %s", dirname, strerror (errno)); |
|
209 return retval; |
|
210 } |
|
211 } |
|
212 else |
|
213 { |
|
214 if (!home_directory) |
|
215 return retval; |
|
216 |
|
217 if (!change_to_directory (home_directory)) |
|
218 { |
|
219 error ("%s: %s", home_directory, strerror (errno)); |
|
220 return retval; |
|
221 } |
|
222 } |
|
223 |
|
224 |
|
225 char *directory = get_working_directory ("cd"); |
|
226 tree_constant *dir = new tree_constant (directory); |
195
|
227 bind_builtin_variable ("PWD", dir, 1); |
1
|
228 |
|
229 return retval; |
|
230 } |
|
231 |
195
|
232 #if 0 |
79
|
233 static int |
|
234 in_list (char *s, char **list) |
|
235 { |
|
236 while (*list != (char *) NULL) |
|
237 { |
|
238 if (strcmp (s, *list) == 0) |
|
239 return 1; |
|
240 list++; |
|
241 } |
|
242 |
|
243 return 0; |
|
244 } |
195
|
245 #endif |
79
|
246 |
1
|
247 /* |
79
|
248 * Wipe out user-defined variables and functions given a list of |
279
|
249 * globbing patterns. |
1
|
250 */ |
|
251 tree_constant |
|
252 builtin_clear (int argc, char **argv) |
|
253 { |
|
254 tree_constant retval; |
195
|
255 |
|
256 // Always clear the local table, but don't clear currently compiled |
|
257 // functions unless we are at the top level. (Allowing that to happen |
|
258 // inside functions would result in pretty odd behavior...) |
|
259 |
|
260 int clear_user_functions = (curr_sym_tab == top_level_sym_tab); |
|
261 |
1
|
262 if (argc == 1) |
|
263 { |
|
264 curr_sym_tab->clear (); |
195
|
265 global_sym_tab->clear (clear_user_functions); |
1
|
266 } |
|
267 else |
|
268 { |
195
|
269 int lcount; |
|
270 char **lvars = curr_sym_tab->list (lcount, 0, |
|
271 symbol_def::USER_VARIABLE, |
|
272 SYMTAB_LOCAL_SCOPE); |
|
273 int gcount; |
|
274 char **gvars = curr_sym_tab->list (gcount, 0, |
|
275 symbol_def::USER_VARIABLE, |
|
276 SYMTAB_GLOBAL_SCOPE); |
|
277 int fcount; |
|
278 char **fcns = curr_sym_tab->list (fcount, 0, |
|
279 symbol_def::USER_FUNCTION, |
|
280 SYMTAB_ALL_SCOPES); |
79
|
281 |
1
|
282 while (--argc > 0) |
|
283 { |
|
284 argv++; |
79
|
285 if (*argv != (char *) NULL) |
|
286 { |
|
287 int i; |
195
|
288 for (i = 0; i < lcount; i++) |
79
|
289 { |
266
|
290 if (fnmatch (*argv, lvars[i], __FNM_FLAGS) == 0) |
195
|
291 curr_sym_tab->clear (lvars[i]); |
|
292 } |
|
293 |
|
294 int count; |
|
295 for (i = 0; i < gcount; i++) |
|
296 { |
266
|
297 if (fnmatch (*argv, gvars[i], __FNM_FLAGS) == 0) |
79
|
298 { |
195
|
299 count = curr_sym_tab->clear (gvars[i]); |
|
300 if (count > 0) |
|
301 global_sym_tab->clear (gvars[i], clear_user_functions); |
79
|
302 } |
|
303 } |
|
304 |
195
|
305 for (i = 0; i < fcount; i++) |
79
|
306 { |
266
|
307 if (fnmatch (*argv, fcns[i], __FNM_FLAGS) == 0) |
79
|
308 { |
195
|
309 count = curr_sym_tab->clear (fcns[i]); |
|
310 if (count > 0) |
|
311 global_sym_tab->clear (fcns[i], clear_user_functions); |
79
|
312 } |
|
313 } |
|
314 } |
1
|
315 } |
79
|
316 |
195
|
317 delete [] lvars; |
|
318 delete [] gvars; |
|
319 delete [] fcns; |
79
|
320 |
1
|
321 } |
|
322 return retval; |
|
323 } |
|
324 |
|
325 /* |
|
326 * Associate a cryptic message with a variable name. |
|
327 */ |
|
328 tree_constant |
|
329 builtin_document (int argc, char **argv) |
|
330 { |
|
331 tree_constant retval; |
|
332 if (argc == 3) |
195
|
333 document_symbol (argv[1], argv[2]); |
1
|
334 else |
181
|
335 print_usage ("document"); |
1
|
336 return retval; |
|
337 } |
|
338 |
|
339 /* |
|
340 * Edit commands with your favorite editor. |
|
341 */ |
|
342 tree_constant |
|
343 builtin_edit_history (int argc, char **argv) |
|
344 { |
|
345 tree_constant retval; |
|
346 do_edit_history (argc, argv); |
|
347 return retval; |
|
348 } |
|
349 |
|
350 /* |
|
351 * Set output format state. |
|
352 */ |
|
353 tree_constant |
|
354 builtin_format (int argc, char **argv) |
|
355 { |
|
356 tree_constant retval; |
|
357 set_format_style (argc, argv); |
|
358 return retval; |
|
359 } |
|
360 |
169
|
361 static void |
|
362 help_syms_list (ostrstream& output_buf, help_list *list, |
|
363 const char *desc) |
|
364 { |
|
365 int count = 0; |
|
366 char **symbols = names (list, count); |
|
367 output_buf << "\n*** " << desc << ":\n\n"; |
|
368 if (symbols != (char **) NULL && count > 0) |
|
369 list_in_columns (output_buf, symbols); |
|
370 delete [] symbols; |
|
371 } |
|
372 |
|
373 static void |
|
374 simple_help (void) |
|
375 { |
|
376 ostrstream output_buf; |
|
377 |
|
378 help_syms_list (output_buf, operator_help (), "operators"); |
|
379 |
|
380 help_syms_list (output_buf, keyword_help (), "reserved words"); |
|
381 |
|
382 help_syms_list (output_buf, builtin_text_functions_help (), |
|
383 "text functions (these names are also reserved)"); |
|
384 |
|
385 help_syms_list (output_buf, builtin_mapper_functions_help (), |
|
386 "mapper functions"); |
|
387 |
|
388 help_syms_list (output_buf, builtin_general_functions_help (), |
|
389 "general functions"); |
|
390 |
|
391 help_syms_list (output_buf, builtin_variables_help (), |
|
392 "builtin variables"); |
|
393 |
|
394 // Also need to list variables and currently compiled functions from |
|
395 // the symbol table, if there are any. |
|
396 |
|
397 // Also need to search octave_path for script files. |
|
398 |
|
399 char **path = pathstring_to_vector (user_pref.loadpath); |
|
400 |
|
401 char **ptr = path; |
|
402 if (ptr != (char **) NULL) |
|
403 { |
|
404 while (*ptr != (char *) NULL) |
|
405 { |
|
406 int count; |
338
|
407 char **names = get_fcn_file_names (count, *ptr, 0); |
|
408 output_buf << "\n*** function files in " |
169
|
409 << make_absolute (*ptr, the_current_working_directory) |
|
410 << ":\n\n"; |
|
411 if (names != (char **) NULL && count > 0) |
|
412 list_in_columns (output_buf, names); |
|
413 delete [] names; |
|
414 ptr++; |
|
415 } |
|
416 } |
|
417 |
181
|
418 additional_help_message (output_buf); |
169
|
419 output_buf << ends; |
|
420 maybe_page_output (output_buf); |
|
421 } |
|
422 |
|
423 static int |
181
|
424 try_info (const char *string, int force = 0) |
169
|
425 { |
|
426 int status = 0; |
|
427 |
186
|
428 char *directory_name = strsave (user_pref.info_file); |
169
|
429 char *temp = filename_non_directory (directory_name); |
|
430 |
|
431 if (temp != directory_name) |
|
432 { |
|
433 *temp = 0; |
|
434 info_add_path (directory_name, INFOPATH_PREPEND); |
|
435 } |
|
436 |
|
437 delete [] directory_name; |
|
438 |
186
|
439 NODE *initial_node = info_get_node (user_pref.info_file, (char *)NULL); |
169
|
440 |
|
441 if (! initial_node) |
|
442 { |
|
443 warning ("can't find info file!\n"); |
186
|
444 status = -1; |
169
|
445 } |
186
|
446 else |
169
|
447 { |
249
|
448 status = initialize_info_session (initial_node, 0); |
169
|
449 |
249
|
450 if (status == 0 && (force || index_entry_exists (windows, string))) |
186
|
451 { |
|
452 terminal_clear_screen (); |
169
|
453 |
186
|
454 terminal_prep_terminal (); |
169
|
455 |
186
|
456 display_update_display (windows); |
169
|
457 |
186
|
458 info_last_executed_command = (VFunction *)NULL; |
|
459 |
|
460 if (! force) |
|
461 do_info_index_search (windows, 0, string); |
169
|
462 |
186
|
463 char *format = replace_in_documentation |
|
464 ("Type \"\\[quit]\" to quit, \"\\[get-help-window]\" for help."); |
169
|
465 |
186
|
466 window_message_in_echo_area (format); |
169
|
467 |
186
|
468 info_read_and_dispatch (); |
169
|
469 |
186
|
470 terminal_goto_xy (0, screenheight - 1); |
169
|
471 |
186
|
472 terminal_clear_to_eol (); |
169
|
473 |
186
|
474 terminal_unprep_terminal (); |
169
|
475 |
186
|
476 status = 1; |
|
477 } |
|
478 |
|
479 finish_info_session (initial_node, 0); |
169
|
480 } |
|
481 |
|
482 return status; |
|
483 } |
|
484 |
1
|
485 /* |
|
486 * Print cryptic yet witty messages. |
|
487 */ |
|
488 tree_constant |
|
489 builtin_help (int argc, char **argv) |
|
490 { |
|
491 tree_constant retval; |
|
492 |
|
493 if (argc == 1) |
|
494 { |
169
|
495 simple_help (); |
1
|
496 } |
|
497 else |
|
498 { |
181
|
499 if (argv[1] != (char *) NULL && strcmp (argv[1], "-i") == 0) |
1
|
500 { |
181
|
501 argc--; |
|
502 argv++; |
1
|
503 |
181
|
504 if (argc == 1) |
|
505 { |
|
506 volatile sig_handler *old_sigint_handler; |
|
507 old_sigint_handler = signal (SIGINT, SIG_IGN); |
169
|
508 |
181
|
509 try_info ((char *) NULL, 1); |
1
|
510 |
181
|
511 signal (SIGINT, old_sigint_handler); |
|
512 } |
|
513 else |
|
514 { |
|
515 while (--argc > 0) |
|
516 { |
|
517 argv++; |
169
|
518 |
181
|
519 if (*argv == (char *) NULL || **argv == '\0') |
|
520 continue; |
169
|
521 |
181
|
522 volatile sig_handler *old_sigint_handler; |
|
523 old_sigint_handler = signal (SIGINT, SIG_IGN); |
1
|
524 |
181
|
525 if (! try_info (*argv)) |
|
526 { |
|
527 message ("help", |
|
528 "sorry, `%s' is not indexed in the manual", |
|
529 *argv); |
|
530 sleep (2); |
|
531 } |
|
532 |
|
533 signal (SIGINT, old_sigint_handler); |
1
|
534 } |
|
535 } |
181
|
536 } |
|
537 else |
|
538 { |
|
539 ostrstream output_buf; |
1
|
540 |
338
|
541 char *fcn_file_name = (char *) NULL; |
181
|
542 symbol_record *sym_rec; |
|
543 help_list *op_help_list = operator_help (); |
|
544 help_list *kw_help_list = keyword_help (); |
|
545 |
|
546 while (--argc > 0) |
1
|
547 { |
181
|
548 argv++; |
|
549 |
|
550 if (*argv == (char *) NULL || **argv == '\0') |
|
551 continue; |
|
552 |
|
553 if (help_from_list (output_buf, op_help_list, *argv, 0)) |
|
554 continue; |
|
555 |
|
556 if (help_from_list (output_buf, kw_help_list, *argv, 0)) |
|
557 continue; |
|
558 |
|
559 sym_rec = curr_sym_tab->lookup (*argv, 0, 0); |
|
560 if (sym_rec != (symbol_record *) NULL) |
1
|
561 { |
181
|
562 char *h = sym_rec->help (); |
|
563 if (h != (char *) NULL && *h != '\0') |
|
564 { |
|
565 output_buf << "\n*** " << *argv << ":\n\n" |
|
566 << h << "\n"; |
|
567 continue; |
|
568 } |
1
|
569 } |
181
|
570 |
|
571 sym_rec = global_sym_tab->lookup (*argv, 0, 0); |
|
572 if (sym_rec != (symbol_record *) NULL |
|
573 && ! symbol_out_of_date (sym_rec)) |
|
574 { |
|
575 char *h = sym_rec->help (); |
|
576 if (h != (char *) NULL && *h != '\0') |
|
577 { |
|
578 output_buf << "\n*** " << *argv << ":\n\n" |
|
579 << h << "\n"; |
|
580 continue; |
|
581 } |
|
582 } |
1
|
583 |
338
|
584 // Try harder to find function files that might not be defined yet, or |
|
585 // that appear to be out of date. Don\'t execute commands from the |
|
586 // file if it turns out to be a script file. |
1
|
587 |
338
|
588 fcn_file_name = fcn_file_in_path (*argv); |
|
589 if (fcn_file_name != (char *) NULL) |
1
|
590 { |
181
|
591 sym_rec = global_sym_tab->lookup (*argv, 1, 0); |
|
592 if (sym_rec != (symbol_record *) NULL) |
110
|
593 { |
181
|
594 tree_identifier tmp (sym_rec); |
338
|
595 tmp.parse_fcn_file (0); |
181
|
596 char *h = sym_rec->help (); |
|
597 if (h != (char *) NULL && *h != '\0') |
|
598 { |
|
599 output_buf << "\n*** " << *argv << ":\n\n" |
|
600 << h << "\n"; |
|
601 continue; |
|
602 } |
110
|
603 } |
1
|
604 } |
338
|
605 delete [] fcn_file_name; |
1
|
606 |
181
|
607 output_buf << "\nhelp: sorry, `" << *argv |
|
608 << "' is not documented\n"; |
|
609 } |
1
|
610 |
181
|
611 additional_help_message (output_buf); |
|
612 output_buf << ends; |
|
613 maybe_page_output (output_buf); |
|
614 } |
1
|
615 } |
|
616 |
|
617 return retval; |
|
618 } |
|
619 |
|
620 /* |
|
621 * Display, save, or load history. |
|
622 */ |
|
623 tree_constant |
|
624 builtin_history (int argc, char **argv) |
|
625 { |
|
626 tree_constant retval; |
|
627 |
|
628 do_history (argc, argv); |
|
629 |
|
630 return retval; |
|
631 } |
|
632 |
|
633 static int |
|
634 load_variable (char *nm, int force, istream& is) |
|
635 { |
348
|
636 // Is there already a symbol by this name? If so, what is it? |
|
637 |
1
|
638 symbol_record *lsr = curr_sym_tab->lookup (nm, 0, 0); |
|
639 |
348
|
640 int is_undefined = 1; |
|
641 int is_variable = 0; |
|
642 int is_function = 0; |
|
643 int is_global = 0; |
|
644 |
|
645 if (lsr != (symbol_record *) NULL) |
1
|
646 { |
348
|
647 is_undefined = ! lsr->is_defined (); |
|
648 is_variable = lsr->is_variable (); |
|
649 is_function = lsr->is_function (); |
|
650 is_global = lsr->is_linked_to_global (); |
1
|
651 } |
|
652 |
348
|
653 // Try to read data for this name. |
1
|
654 |
|
655 tree_constant tc; |
|
656 int global = tc.load (is); |
348
|
657 |
|
658 if (tc.const_type () == tree_constant_rep::unknown_constant) |
1
|
659 { |
348
|
660 error ("load: unable to load variable `%s'", nm); |
|
661 return 0; |
|
662 } |
|
663 |
|
664 symbol_record *sr = (symbol_record *) NULL; |
|
665 |
|
666 if (global) |
|
667 { |
|
668 if (is_global || is_undefined) |
1
|
669 { |
348
|
670 if (force || is_undefined) |
|
671 { |
|
672 lsr = curr_sym_tab->lookup (nm, 1, 0); |
|
673 link_to_global_variable (lsr); |
|
674 sr = lsr; |
|
675 } |
|
676 else |
|
677 { |
|
678 warning ("load: global variable name `%s' exists.", nm); |
|
679 warning ("use `load -force' to overwrite"); |
|
680 } |
|
681 } |
|
682 else if (is_function) |
|
683 { |
|
684 if (force) |
1
|
685 { |
348
|
686 lsr = curr_sym_tab->lookup (nm, 1, 0); |
|
687 link_to_global_variable (lsr); |
|
688 sr = lsr; |
|
689 } |
|
690 else |
|
691 { |
|
692 warning ("load: `%s' is currently a function in this scope", nm); |
|
693 warning ("`load -force' will load variable and hide function"); |
1
|
694 } |
348
|
695 } |
|
696 else if (is_variable) |
|
697 { |
|
698 if (force) |
|
699 { |
|
700 lsr = curr_sym_tab->lookup (nm, 1, 0); |
|
701 link_to_global_variable (lsr); |
|
702 sr = lsr; |
|
703 } |
|
704 else |
|
705 { |
|
706 warning ("load: local variable name `%s' exists.", nm); |
|
707 warning ("use `load -force' to overwrite"); |
|
708 } |
1
|
709 } |
|
710 else |
348
|
711 panic_impossible (); |
|
712 } |
|
713 else |
|
714 { |
|
715 if (is_global) |
1
|
716 { |
348
|
717 if (force || is_undefined) |
1
|
718 { |
348
|
719 lsr = curr_sym_tab->lookup (nm, 1, 0); |
|
720 link_to_global_variable (lsr); |
|
721 sr = lsr; |
|
722 } |
|
723 else |
|
724 { |
|
725 warning ("load: global variable name `%s' exists.", nm); |
|
726 warning ("use `load -force' to overwrite"); |
|
727 } |
|
728 } |
|
729 else if (is_function) |
|
730 { |
|
731 if (force) |
|
732 { |
|
733 lsr = curr_sym_tab->lookup (nm, 1, 0); |
|
734 link_to_global_variable (lsr); |
|
735 sr = lsr; |
1
|
736 } |
|
737 else |
348
|
738 { |
|
739 warning ("load: `%s' is currently a function in this scope", nm); |
|
740 warning ("`load -force' will load variable and hide function"); |
|
741 } |
1
|
742 } |
348
|
743 else if (is_variable || is_undefined) |
1
|
744 { |
348
|
745 if (force || is_undefined) |
|
746 { |
|
747 lsr = curr_sym_tab->lookup (nm, 1, 0); |
|
748 sr = lsr; |
|
749 } |
|
750 else |
|
751 { |
|
752 warning ("load: local variable name `%s' exists.", nm); |
|
753 warning ("use `load -force' to overwrite"); |
|
754 } |
1
|
755 } |
|
756 else |
348
|
757 panic_impossible (); |
1
|
758 } |
|
759 |
348
|
760 if (sr != (symbol_record *) NULL) |
|
761 { |
|
762 tree_constant *tmp_tc = new tree_constant (tc); |
|
763 sr->define (tmp_tc); |
|
764 return 1; |
|
765 } |
|
766 else |
|
767 error ("load: unable to load variable `%s'", nm); |
|
768 |
1
|
769 return 0; |
|
770 } |
|
771 |
|
772 /* |
|
773 * Read variables from an input stream. |
|
774 * |
|
775 * BUGS: |
|
776 * |
|
777 * -- This function is not terribly robust. |
|
778 */ |
|
779 tree_constant |
|
780 builtin_load (int argc, char **argv) |
|
781 { |
|
782 tree_constant retval; |
|
783 |
|
784 argc--; |
|
785 argv++; |
|
786 |
|
787 int force = 0; |
|
788 if (argc > 0 && strcmp (*argv, "-force") == 0) |
|
789 { |
|
790 force++; |
|
791 argc--; |
|
792 argv++; |
|
793 } |
|
794 |
|
795 if (argc < 1) |
|
796 { |
181
|
797 error ("load: you must specify a single file to read"); |
1
|
798 return retval; |
|
799 } |
|
800 |
|
801 static istream stream; |
|
802 static ifstream file; |
|
803 if (strcmp (*argv, "-") == 0) |
|
804 { |
|
805 stream = cin; |
|
806 } |
|
807 else |
|
808 { |
|
809 char *fname = tilde_expand (*argv); |
|
810 file.open (fname); |
|
811 if (! file) |
|
812 { |
|
813 error ("load: couldn't open input file `%s'", *argv); |
|
814 return retval; |
|
815 } |
|
816 stream = file; |
|
817 } |
|
818 |
|
819 int count = 0; |
312
|
820 char *nm = (char *) NULL; |
1
|
821 for (;;) |
|
822 { |
|
823 // Read name for this entry or break on EOF. |
312
|
824 delete [] nm; |
|
825 nm = extract_keyword (stream, "name"); |
279
|
826 if (nm == (char *) NULL) |
1
|
827 { |
|
828 if (count == 0) |
214
|
829 { |
|
830 error ("load: no name keywords found in file `%s'", *argv); |
|
831 error ("Are you sure this is an octave data file?"); |
|
832 } |
1
|
833 break; |
|
834 } |
279
|
835 else |
|
836 count++; |
1
|
837 |
|
838 if (*nm == '\0') |
|
839 continue; |
|
840 |
|
841 if (! valid_identifier (nm)) |
|
842 { |
181
|
843 warning ("load: skipping bogus identifier `%s'"); |
1
|
844 continue; |
|
845 } |
|
846 |
279
|
847 load_variable (nm, force, stream); |
|
848 |
|
849 if (error_state) |
|
850 { |
|
851 error ("reading file %s", *argv); |
|
852 break; |
|
853 } |
1
|
854 } |
|
855 |
|
856 if (file); |
|
857 file.close (); |
|
858 |
|
859 return retval; |
|
860 } |
|
861 |
|
862 /* |
|
863 * Get a directory listing. |
|
864 */ |
|
865 tree_constant |
|
866 builtin_ls (int argc, char **argv) |
|
867 { |
|
868 tree_constant retval; |
|
869 |
|
870 ostrstream ls_buf; |
|
871 |
|
872 ls_buf << "ls -C "; |
|
873 for (int i = 1; i < argc; i++) |
|
874 ls_buf << tilde_expand (argv[i]) << " "; |
|
875 |
|
876 ls_buf << ends; |
|
877 |
|
878 char *ls_command = ls_buf.str (); |
|
879 |
|
880 iprocstream cmd (ls_command); |
|
881 |
|
882 char ch; |
|
883 ostrstream output_buf; |
|
884 while (cmd.get (ch)) |
|
885 output_buf.put (ch); |
|
886 |
|
887 output_buf << ends; |
|
888 |
|
889 maybe_page_output (output_buf); |
|
890 |
|
891 delete [] ls_command; |
|
892 |
|
893 return retval; |
|
894 } |
|
895 |
|
896 /* |
62
|
897 * Run previous commands from the history list. |
|
898 */ |
|
899 tree_constant |
|
900 builtin_run_history (int argc, char **argv) |
|
901 { |
|
902 tree_constant retval; |
|
903 do_run_history (argc, argv); |
|
904 return retval; |
|
905 } |
|
906 |
|
907 /* |
266
|
908 * Return nonzero if PATTERN has any special globbing chars in it. |
|
909 */ |
|
910 static int |
|
911 glob_pattern_p (char *pattern) |
|
912 { |
|
913 char *p = pattern; |
|
914 char c; |
|
915 int open = 0; |
|
916 |
|
917 while ((c = *p++) != '\0') |
|
918 { |
|
919 switch (c) |
|
920 { |
|
921 case '?': |
|
922 case '*': |
|
923 return 1; |
|
924 |
|
925 case '[': // Only accept an open brace if there is a close |
|
926 open++; // brace to match it. Bracket expressions must be |
|
927 continue; // complete, according to Posix.2 |
|
928 |
|
929 case ']': |
|
930 if (open) |
|
931 return 1; |
|
932 continue; |
|
933 |
|
934 case '\\': |
|
935 if (*p++ == '\0') |
|
936 return 0; |
|
937 |
|
938 default: |
|
939 continue; |
|
940 } |
|
941 } |
|
942 |
|
943 return 0; |
|
944 } |
|
945 |
|
946 /* |
1
|
947 * Write variables to an output stream. |
|
948 */ |
|
949 tree_constant |
|
950 builtin_save (int argc, char **argv) |
|
951 { |
|
952 tree_constant retval; |
|
953 |
|
954 if (argc < 2) |
|
955 { |
181
|
956 print_usage ("save"); |
1
|
957 return retval; |
|
958 } |
|
959 |
|
960 argc--; |
|
961 argv++; |
|
962 |
|
963 static ostream stream; |
|
964 static ofstream file; |
|
965 if (strcmp (*argv, "-") == 0) |
|
966 { |
279
|
967 // XXX FIXME XXX -- should things intended for the screen end up in a |
1
|
968 // tree_constant (string)? |
|
969 stream = cout; |
|
970 } |
266
|
971 else if (argc == 1 && glob_pattern_p (*argv)) // Guard against things |
|
972 { // like `save a*', |
|
973 print_usage ("save"); // which are probably |
|
974 return retval; // mistakes... |
|
975 } |
1
|
976 else |
|
977 { |
|
978 char *fname = tilde_expand (*argv); |
|
979 file.open (fname); |
|
980 if (! file) |
|
981 { |
|
982 error ("save: couldn't open output file `%s'", *argv); |
|
983 return retval; |
|
984 } |
|
985 stream = file; |
276
|
986 |
1
|
987 } |
|
988 |
276
|
989 int prec = user_pref.save_precision; |
|
990 |
1
|
991 if (argc == 1) |
|
992 { |
195
|
993 int count; |
|
994 char **vars = curr_sym_tab->list (count, 0, |
|
995 symbol_def::USER_VARIABLE, |
|
996 SYMTAB_ALL_SCOPES); |
|
997 |
|
998 for (int i = 0; i < count; i++) |
|
999 curr_sym_tab->save (stream, vars[i], |
276
|
1000 is_globally_visible (vars[i]), prec); |
195
|
1001 |
|
1002 delete [] vars; |
1
|
1003 } |
|
1004 else |
|
1005 { |
|
1006 while (--argc > 0) |
|
1007 { |
|
1008 argv++; |
195
|
1009 |
|
1010 int count; |
|
1011 char **lvars = curr_sym_tab->list (count, 0, |
|
1012 symbol_def::USER_VARIABLE); |
|
1013 |
|
1014 int saved_or_error = 0; |
|
1015 int i; |
|
1016 for (i = 0; i < count; i++) |
|
1017 { |
266
|
1018 if (fnmatch (*argv, lvars[i], __FNM_FLAGS) == 0 |
312
|
1019 && curr_sym_tab->save (stream, lvars[i], |
|
1020 is_globally_visible (lvars[i]), |
|
1021 prec) != 0) |
195
|
1022 saved_or_error++; |
|
1023 } |
|
1024 |
|
1025 char **bvars = global_sym_tab->list (count, 0, |
|
1026 symbol_def::BUILTIN_VARIABLE); |
|
1027 |
|
1028 for (i = 0; i < count; i++) |
|
1029 { |
266
|
1030 if (fnmatch (*argv, bvars[i], __FNM_FLAGS) == 0 |
276
|
1031 && global_sym_tab->save (stream, bvars[i], 0, prec) != 0) |
195
|
1032 saved_or_error++; |
|
1033 } |
|
1034 |
|
1035 delete [] lvars; |
|
1036 delete [] bvars; |
|
1037 |
|
1038 if (! saved_or_error) |
|
1039 warning ("save: no such variable `%s'", *argv); |
1
|
1040 } |
|
1041 } |
|
1042 |
|
1043 if (file); |
|
1044 file.close (); |
|
1045 |
|
1046 return retval; |
|
1047 } |
|
1048 |
|
1049 /* |
|
1050 * Set plotting options. |
|
1051 */ |
|
1052 tree_constant |
|
1053 builtin_set (int argc, char **argv) |
|
1054 { |
|
1055 tree_constant retval; |
|
1056 |
|
1057 ostrstream plot_buf; |
|
1058 |
|
1059 if (argc > 1) |
|
1060 { |
|
1061 if (almost_match ("parametric", argv[1], 3)) |
|
1062 parametric_plot = 1; |
|
1063 else if (almost_match ("noparametric", argv[1], 5)) |
|
1064 parametric_plot = 0; |
|
1065 } |
|
1066 |
|
1067 for (int i = 0; i < argc; i++) |
|
1068 plot_buf << argv[i] << " "; |
|
1069 |
|
1070 plot_buf << "\n" << ends; |
|
1071 |
|
1072 char *plot_command = plot_buf.str (); |
|
1073 send_to_plot_stream (plot_command); |
|
1074 |
|
1075 delete [] plot_command; |
|
1076 |
|
1077 return retval; |
|
1078 } |
|
1079 |
|
1080 /* |
|
1081 * Set plotting options. |
|
1082 */ |
|
1083 tree_constant |
|
1084 builtin_show (int argc, char **argv) |
|
1085 { |
|
1086 tree_constant retval; |
|
1087 |
|
1088 ostrstream plot_buf; |
|
1089 |
|
1090 for (int i = 0; i < argc; i++) |
|
1091 plot_buf << argv[i] << " "; |
|
1092 |
|
1093 plot_buf << "\n" << ends; |
|
1094 |
|
1095 char *plot_command = plot_buf.str (); |
|
1096 send_to_plot_stream (plot_command); |
|
1097 |
|
1098 delete [] plot_command; |
|
1099 |
|
1100 return retval; |
|
1101 } |
|
1102 |
|
1103 /* |
|
1104 * List variable names. |
|
1105 */ |
195
|
1106 static void |
|
1107 print_symbol_info_line (ostrstream& output_buf, const symbol_record_info& s) |
|
1108 { |
|
1109 output_buf << (s.is_read_only () ? " -" : " w"); |
|
1110 output_buf << (s.is_eternal () ? "- " : "d "); |
|
1111 #if 0 |
|
1112 output_buf << (s.hides_fcn () ? "f" : (s.hides_builtin () ? "F" : "-")); |
|
1113 #endif |
|
1114 output_buf.form (" %-16s", s.type_as_string ()); |
|
1115 if (s.is_function ()) |
|
1116 output_buf << " - -"; |
|
1117 else |
|
1118 { |
|
1119 output_buf.form ("%7d", s.rows ()); |
|
1120 output_buf.form ("%7d", s.columns ()); |
|
1121 } |
|
1122 output_buf << " " << s.name () << "\n"; |
|
1123 } |
|
1124 |
|
1125 static void |
|
1126 print_long_listing (ostrstream& output_buf, symbol_record_info *s) |
|
1127 { |
|
1128 if (s == (symbol_record_info *) NULL) |
|
1129 return; |
|
1130 |
|
1131 symbol_record_info *ptr = s; |
|
1132 while (ptr->is_defined ()) |
|
1133 { |
|
1134 print_symbol_info_line (output_buf, *ptr); |
|
1135 ptr++; |
|
1136 } |
|
1137 } |
|
1138 |
|
1139 static int |
|
1140 maybe_list (const char *header, ostrstream& output_buf, |
|
1141 int show_verbose, symbol_table *sym_tab, unsigned type, |
|
1142 unsigned scope) |
|
1143 { |
|
1144 int count; |
|
1145 int status = 0; |
|
1146 if (show_verbose) |
|
1147 { |
|
1148 symbol_record_info *symbols; |
|
1149 symbols = sym_tab->long_list (count, 1, type, scope); |
|
1150 if (symbols != (symbol_record_info *) NULL && count > 0) |
|
1151 { |
|
1152 output_buf << "\n" << header << "\n\n" |
|
1153 << "prot type rows cols name\n" |
|
1154 << "==== ==== ==== ==== ====\n"; |
|
1155 |
|
1156 print_long_listing (output_buf, symbols); |
|
1157 status = 1; |
|
1158 } |
|
1159 delete [] symbols; |
|
1160 } |
|
1161 else |
|
1162 { |
|
1163 char **symbols = sym_tab->list (count, 1, type, scope); |
|
1164 if (symbols != (char **) NULL && count > 0) |
|
1165 { |
|
1166 output_buf << "\n" << header << "\n\n"; |
|
1167 list_in_columns (output_buf, symbols); |
|
1168 status = 1; |
|
1169 } |
|
1170 delete [] symbols; |
|
1171 } |
|
1172 return status; |
|
1173 } |
|
1174 |
1
|
1175 tree_constant |
|
1176 builtin_who (int argc, char **argv) |
|
1177 { |
|
1178 tree_constant retval; |
195
|
1179 |
|
1180 int show_builtins = 0; |
|
1181 int show_functions = (curr_sym_tab == top_level_sym_tab); |
|
1182 int show_variables = 1; |
|
1183 int show_verbose = 0; |
1
|
1184 |
|
1185 if (argc > 1) |
195
|
1186 { |
|
1187 show_functions = 0; |
|
1188 show_variables = 0; |
|
1189 } |
1
|
1190 |
|
1191 for (int i = 1; i < argc; i++) |
|
1192 { |
|
1193 argv++; |
195
|
1194 if (strcmp (*argv, "-all") == 0 || strcmp (*argv, "-a") == 0) |
1
|
1195 { |
195
|
1196 show_builtins++; |
|
1197 show_functions++; |
|
1198 show_variables++; |
1
|
1199 } |
195
|
1200 else if (strcmp (*argv, "-builtins") == 0 |
|
1201 || strcmp (*argv, "-b") == 0) |
|
1202 show_builtins++; |
|
1203 else if (strcmp (*argv, "-functions") == 0 |
|
1204 || strcmp (*argv, "-f") == 0) |
|
1205 show_functions++; |
|
1206 else if (strcmp (*argv, "-long") == 0 |
|
1207 || strcmp (*argv, "-l") == 0) |
|
1208 show_verbose++; |
|
1209 else if (strcmp (*argv, "-variables") == 0 |
|
1210 || strcmp (*argv, "-v") == 0) |
|
1211 show_variables++; |
1
|
1212 else |
195
|
1213 warning ("who: unrecognized option `%s'", *argv); |
|
1214 } |
|
1215 |
|
1216 // If the user specified -l and nothing else, show variables. If |
|
1217 // evaluating this at the top level, also show functions. |
|
1218 |
|
1219 if (show_verbose && ! (show_builtins || show_functions || show_variables)) |
|
1220 { |
|
1221 show_functions = (curr_sym_tab == top_level_sym_tab); |
|
1222 show_variables = 1; |
1
|
1223 } |
|
1224 |
|
1225 ostrstream output_buf; |
|
1226 int pad_after = 0; |
195
|
1227 |
|
1228 if (show_builtins) |
1
|
1229 { |
195
|
1230 pad_after += maybe_list ("*** built-in variables:", |
|
1231 output_buf, show_verbose, global_sym_tab, |
|
1232 symbol_def::BUILTIN_VARIABLE, |
|
1233 SYMTAB_ALL_SCOPES); |
1
|
1234 |
195
|
1235 pad_after += maybe_list ("*** built-in functions:", |
|
1236 output_buf, show_verbose, global_sym_tab, |
|
1237 symbol_def::BUILTIN_FUNCTION, |
|
1238 SYMTAB_ALL_SCOPES); |
1
|
1239 } |
|
1240 |
195
|
1241 if (show_functions) |
1
|
1242 { |
195
|
1243 pad_after += maybe_list ("*** currently compiled functions:", |
|
1244 output_buf, show_verbose, global_sym_tab, |
|
1245 symbol_def::USER_FUNCTION, |
|
1246 SYMTAB_ALL_SCOPES); |
1
|
1247 } |
|
1248 |
195
|
1249 if (show_variables) |
1
|
1250 { |
195
|
1251 pad_after += maybe_list ("*** local user variables:", |
|
1252 output_buf, show_verbose, curr_sym_tab, |
|
1253 symbol_def::USER_VARIABLE, |
|
1254 SYMTAB_LOCAL_SCOPE); |
|
1255 |
|
1256 pad_after += maybe_list ("*** globally visible user variables:", |
|
1257 output_buf, show_verbose, curr_sym_tab, |
|
1258 symbol_def::USER_VARIABLE, |
|
1259 SYMTAB_GLOBAL_SCOPE); |
1
|
1260 } |
|
1261 |
|
1262 if (pad_after) |
|
1263 output_buf << "\n"; |
|
1264 |
|
1265 output_buf << ends; |
|
1266 maybe_page_output (output_buf); |
|
1267 |
|
1268 return retval; |
|
1269 } |
|
1270 |
|
1271 /* |
|
1272 ;;; Local Variables: *** |
|
1273 ;;; mode: C++ *** |
|
1274 ;;; page-delimiter: "^/\\*" *** |
|
1275 ;;; End: *** |
|
1276 */ |