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