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 |
|
36 #ifdef __GNUG__ |
|
37 #pragma implementation |
|
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> |
79
|
53 #include <String.h> |
1
|
54 |
|
55 #include "procstream.h" |
|
56 |
|
57 #include "variables.h" |
|
58 #include "symtab.h" |
|
59 #include "error.h" |
|
60 #include "input.h" |
|
61 #include "pager.h" |
|
62 #include "utils.h" |
|
63 #include "builtins.h" |
|
64 #include "t-builtins.h" |
|
65 #include "octave.h" |
|
66 #include "octave-hist.h" |
|
67 #include "user-prefs.h" |
|
68 #include "pr-output.h" |
|
69 #include "tree.h" |
|
70 #include "help.h" |
|
71 |
|
72 // May need replacement for this on some machines. |
|
73 extern "C" |
|
74 { |
|
75 extern char *strerror (int); |
|
76 char *tilde_expand (char *s); /* From readline's tilde.c */ |
|
77 } |
|
78 |
|
79 extern int symbol_out_of_date (symbol_record *s); |
|
80 |
|
81 // Is this a parametric plot? Makes a difference for 3D plotting. |
|
82 extern int parametric_plot; |
|
83 |
|
84 /* |
|
85 * Format a list in neat columns. Mostly stolen from GNU ls. This |
|
86 * should maybe be in utils.cc. |
|
87 */ |
|
88 static ostrstream& |
|
89 list_in_columns (ostrstream& os, char **list) |
|
90 { |
|
91 // Compute the maximum name length. |
|
92 |
|
93 int max_name_length = 0; |
|
94 int total_names = 0; |
|
95 for (char **names = list; *names != (char *) NULL; names++) |
|
96 { |
|
97 total_names++; |
|
98 int name_length = strlen (*names); |
|
99 if (name_length > max_name_length) |
|
100 max_name_length = name_length; |
|
101 } |
|
102 |
|
103 // Allow at least two spaces between names. |
|
104 |
|
105 max_name_length += 2; |
|
106 |
|
107 // Calculate the maximum number of columns that will fit. |
|
108 |
|
109 int line_length = terminal_columns (); |
|
110 int cols = line_length / max_name_length; |
|
111 if (cols == 0) |
|
112 cols = 1; |
|
113 |
|
114 // Calculate the number of rows that will be in each column except |
|
115 // possibly for a short column on the right. |
|
116 |
|
117 int rows = total_names / cols + (total_names % cols != 0); |
|
118 |
|
119 // Recalculate columns based on rows. |
|
120 |
|
121 cols = total_names / rows + (total_names % rows != 0); |
|
122 |
|
123 names = list; |
|
124 int count; |
|
125 for (int row = 0; row < rows; row++) |
|
126 { |
|
127 count = row; |
|
128 int pos = 0; |
|
129 |
|
130 // Print the next row. |
|
131 |
|
132 while (1) |
|
133 { |
|
134 os << *(names + count); |
|
135 int name_length = strlen (*(names + count)); |
|
136 |
|
137 count += rows; |
|
138 if (count >= total_names) |
|
139 break; |
|
140 |
|
141 int spaces_to_pad = max_name_length - name_length; |
|
142 for (int i = 0; i < spaces_to_pad; i++) |
|
143 os << " "; |
|
144 pos += max_name_length; |
|
145 } |
|
146 os << "\n"; |
|
147 } |
|
148 |
|
149 return os; |
|
150 } |
|
151 |
|
152 tree_constant |
|
153 builtin_casesen (int argc, char **argv) |
|
154 { |
|
155 tree_constant retval; |
|
156 |
|
157 if (argc == 1 || (argc > 1 && strcmp (argv[1], "off") == 0)) |
|
158 message ("casesen", "sorry, octave is always case sensitive"); |
|
159 else if (argc > 1 && strcmp (argv[1], "on") == 0) |
|
160 ; // ok. |
|
161 else |
|
162 usage ("casesen [on|off]"); |
|
163 |
|
164 return retval; |
|
165 } |
|
166 |
|
167 /* |
|
168 * Change current working directory. |
|
169 */ |
|
170 tree_constant |
|
171 builtin_cd (int argc, char **argv) |
|
172 { |
|
173 tree_constant retval; |
|
174 |
|
175 if (argc > 1) |
|
176 { |
|
177 static char *dirname = (char *) NULL; |
|
178 |
|
179 if (dirname) |
|
180 free (dirname); |
|
181 |
|
182 dirname = tilde_expand (argv[1]); |
|
183 |
|
184 if (dirname != (char *) NULL && !change_to_directory (dirname)) |
|
185 { |
|
186 error ("%s: %s", dirname, strerror (errno)); |
|
187 return retval; |
|
188 } |
|
189 } |
|
190 else |
|
191 { |
|
192 if (!home_directory) |
|
193 return retval; |
|
194 |
|
195 if (!change_to_directory (home_directory)) |
|
196 { |
|
197 error ("%s: %s", home_directory, strerror (errno)); |
|
198 return retval; |
|
199 } |
|
200 } |
|
201 |
|
202 |
|
203 char *directory = get_working_directory ("cd"); |
|
204 tree_constant *dir = new tree_constant (directory); |
|
205 bind_protected_variable ("PWD", dir); |
|
206 |
|
207 return retval; |
|
208 } |
|
209 |
79
|
210 static int |
|
211 in_list (char *s, char **list) |
|
212 { |
|
213 while (*list != (char *) NULL) |
|
214 { |
|
215 if (strcmp (s, *list) == 0) |
|
216 return 1; |
|
217 list++; |
|
218 } |
|
219 |
|
220 return 0; |
|
221 } |
|
222 |
1
|
223 /* |
79
|
224 * Wipe out user-defined variables and functions given a list of |
|
225 * regular expressions. |
1
|
226 */ |
|
227 tree_constant |
|
228 builtin_clear (int argc, char **argv) |
|
229 { |
|
230 tree_constant retval; |
|
231 if (argc == 1) |
|
232 { |
|
233 curr_sym_tab->clear (); |
|
234 global_sym_tab->clear (); |
|
235 } |
|
236 else |
|
237 { |
79
|
238 int count; |
|
239 char **names = curr_sym_tab->list (count); |
|
240 |
|
241 int g_count; |
|
242 char **g_names = global_sym_tab->list (g_count); |
|
243 |
|
244 int num_cleared = 0; |
|
245 char **locals_cleared = new char * [count+1]; |
|
246 locals_cleared[num_cleared] = (char *) NULL; |
|
247 |
1
|
248 while (--argc > 0) |
|
249 { |
|
250 argv++; |
79
|
251 if (*argv != (char *) NULL) |
|
252 { |
|
253 Regex rx (*argv); |
|
254 |
|
255 int i; |
|
256 for (i = 0; i < count; i++) |
|
257 { |
|
258 String nm (names[i]); |
|
259 if (nm.matches (rx) && curr_sym_tab->clear (names[i])) |
|
260 { |
|
261 locals_cleared[num_cleared++] = strsave (names[i]); |
|
262 locals_cleared[num_cleared] = (char *) NULL; |
|
263 } |
|
264 } |
|
265 |
|
266 for (i = 0; i < g_count; i++) |
|
267 { |
|
268 String nm (g_names[i]); |
|
269 if (nm.matches (rx) |
|
270 && ! in_list (g_names[i], locals_cleared)) |
|
271 { |
|
272 global_sym_tab->clear (g_names[i]); |
|
273 } |
|
274 } |
|
275 } |
1
|
276 } |
79
|
277 |
|
278 int i = 0; |
|
279 while (locals_cleared[i] != (char *) NULL) |
|
280 delete [] locals_cleared[i++]; |
|
281 delete [] locals_cleared; |
|
282 |
|
283 delete [] names; |
|
284 delete [] g_names; |
|
285 delete [] g_names; |
|
286 |
1
|
287 } |
|
288 return retval; |
|
289 } |
|
290 |
|
291 /* |
|
292 * Associate a cryptic message with a variable name. |
|
293 */ |
|
294 tree_constant |
|
295 builtin_document (int argc, char **argv) |
|
296 { |
|
297 tree_constant retval; |
|
298 if (argc == 3) |
|
299 { |
|
300 symbol_record *sym_rec = curr_sym_tab->lookup (argv[1], 0); |
|
301 if (sym_rec == (symbol_record *) NULL) |
|
302 { |
|
303 sym_rec = global_sym_tab->lookup (argv[1], 0); |
|
304 if (sym_rec == (symbol_record *) NULL) |
|
305 { |
|
306 error ("document: no such symbol `%s'", argv[1]); |
|
307 return retval; |
|
308 } |
|
309 } |
|
310 sym_rec->document (argv[2]); |
|
311 } |
|
312 else |
|
313 usage ("document symbol string ..."); |
|
314 |
|
315 return retval; |
|
316 } |
|
317 |
|
318 /* |
|
319 * Edit commands with your favorite editor. |
|
320 */ |
|
321 tree_constant |
|
322 builtin_edit_history (int argc, char **argv) |
|
323 { |
|
324 tree_constant retval; |
|
325 do_edit_history (argc, argv); |
|
326 return retval; |
|
327 } |
|
328 |
|
329 /* |
|
330 * Set output format state. |
|
331 */ |
|
332 tree_constant |
|
333 builtin_format (int argc, char **argv) |
|
334 { |
|
335 tree_constant retval; |
|
336 set_format_style (argc, argv); |
|
337 return retval; |
|
338 } |
|
339 |
|
340 /* |
|
341 * Print cryptic yet witty messages. |
|
342 */ |
|
343 tree_constant |
|
344 builtin_help (int argc, char **argv) |
|
345 { |
|
346 tree_constant retval; |
|
347 |
|
348 ostrstream output_buf; |
|
349 if (argc == 1) |
|
350 { |
|
351 char **symbols; |
|
352 int count = 0; |
|
353 |
|
354 symbols = names (operator_help (), count); |
|
355 output_buf << "\n*** operators:\n\n"; |
|
356 if (symbols != (char **) NULL && count > 0) |
|
357 list_in_columns (output_buf, symbols); |
|
358 delete [] symbols; |
|
359 |
|
360 symbols = names (keyword_help (), count); |
|
361 output_buf << "\n*** reserved words:\n\n"; |
|
362 if (symbols != (char **) NULL && count > 0) |
|
363 list_in_columns (output_buf, symbols); |
|
364 delete [] symbols; |
|
365 |
|
366 symbols = names (builtin_text_functions_help (), count); |
|
367 output_buf |
|
368 << "\n*** text functions (these names are also reserved):\n\n"; |
|
369 if (symbols != (char **) NULL && count > 0) |
|
370 list_in_columns (output_buf, symbols); |
|
371 delete [] symbols; |
|
372 |
|
373 symbols = names (builtin_mapper_functions_help (), count); |
|
374 output_buf << "\n*** mapper functions:\n\n"; |
|
375 if (symbols != (char **) NULL && count > 0) |
|
376 list_in_columns (output_buf, symbols); |
|
377 delete [] symbols; |
|
378 |
|
379 symbols = names (builtin_general_functions_help (), count); |
|
380 output_buf << "\n*** general functions:\n\n"; |
|
381 if (symbols != (char **) NULL && count > 0) |
|
382 list_in_columns (output_buf, symbols); |
|
383 delete [] symbols; |
|
384 |
|
385 symbols = names (builtin_variables_help (), count); |
|
386 output_buf << "\n*** builtin variables:\n\n"; |
|
387 if (symbols != (char **) NULL && count > 0) |
|
388 list_in_columns (output_buf, symbols); |
|
389 delete [] symbols; |
|
390 |
|
391 // Also need to list variables and currently compiled functions from |
|
392 // the symbol table, if there are any. |
|
393 |
|
394 // Also need to search octave_path for script files. |
|
395 |
|
396 char **path = pathstring_to_vector (user_pref.loadpath); |
|
397 |
|
398 char **ptr = path; |
|
399 if (ptr != (char **) NULL) |
|
400 { |
|
401 while (*ptr != (char *) NULL) |
|
402 { |
|
403 int count; |
|
404 char **names = get_m_file_names (count, *ptr, 0); |
|
405 output_buf << "\n*** M-files in " |
|
406 << make_absolute (*ptr, the_current_working_directory) |
|
407 << ":\n\n"; |
|
408 if (names != (char **) NULL && count > 0) |
|
409 list_in_columns (output_buf, names); |
|
410 delete [] names; |
|
411 ptr++; |
|
412 } |
|
413 } |
|
414 } |
|
415 else |
|
416 { |
110
|
417 char *m_file_name = (char *) NULL; |
1
|
418 symbol_record *sym_rec; |
|
419 help_list *op_help_list = operator_help (); |
|
420 help_list *kw_help_list = keyword_help (); |
|
421 for (int i = 1; i < argc; i++) |
|
422 { |
|
423 if (argv[i] == (char *) NULL || argv[i][0] == '\0') |
|
424 continue; |
|
425 |
|
426 int j = 0; |
|
427 char *name; |
|
428 while ((name = op_help_list[j].name) != (char *) NULL) |
|
429 { |
|
430 if (strcmp (name, argv[i]) == 0) |
|
431 { |
|
432 output_buf << "\n" << op_help_list[j].help << "\n"; |
|
433 goto next; |
|
434 } |
|
435 j++; |
|
436 } |
|
437 |
|
438 j = 0; |
|
439 while ((name = kw_help_list[j].name) != (char *) NULL) |
|
440 { |
|
441 if (strcmp (name, argv[i]) == 0) |
|
442 { |
|
443 output_buf << "\n" << kw_help_list[j].help << "\n"; |
|
444 goto next; |
|
445 } |
|
446 j++; |
|
447 } |
|
448 |
|
449 sym_rec = curr_sym_tab->lookup (argv[i], 0, 0); |
|
450 if (sym_rec != (symbol_record *) NULL) |
|
451 { |
|
452 char *h = sym_rec->help (); |
|
453 if (h != (char *) NULL && *h != '\0') |
|
454 { |
|
455 output_buf << "\n" << h << "\n"; |
|
456 goto next; |
|
457 } |
|
458 } |
|
459 |
|
460 sym_rec = global_sym_tab->lookup (argv[i], 0, 0); |
|
461 if (sym_rec != (symbol_record *) NULL |
|
462 && ! symbol_out_of_date (sym_rec)) |
|
463 { |
|
464 char *h = sym_rec->help (); |
|
465 if (h != (char *) NULL && *h != '\0') |
|
466 { |
|
467 output_buf << "\n" << h << "\n"; |
|
468 goto next; |
|
469 } |
|
470 } |
|
471 |
|
472 // Try harder to find M-files that might not be defined yet, or that |
|
473 // appear to be out of date. Don\'t execute commands from the file if |
|
474 // it turns out to be a script file. |
|
475 |
110
|
476 m_file_name = m_file_in_path (argv[i]); |
|
477 if (m_file_name != (char *) NULL) |
1
|
478 { |
110
|
479 sym_rec = global_sym_tab->lookup (argv[i], 1, 0); |
|
480 if (sym_rec != (symbol_record *) NULL) |
1
|
481 { |
110
|
482 tree_identifier tmp (sym_rec); |
|
483 tmp.parse_m_file (0); |
|
484 char *h = sym_rec->help (); |
|
485 if (h != (char *) NULL && *h != '\0') |
|
486 { |
|
487 output_buf << "\n" << h << "\n"; |
|
488 goto next; |
|
489 } |
1
|
490 } |
|
491 } |
110
|
492 delete [] m_file_name; |
1
|
493 |
|
494 output_buf << "Sorry, `" << argv[i] << "' is not documented\n"; |
|
495 |
|
496 next: |
|
497 continue; |
|
498 } |
|
499 } |
|
500 |
|
501 output_buf << ends; |
|
502 maybe_page_output (output_buf); |
|
503 |
|
504 return retval; |
|
505 } |
|
506 |
|
507 /* |
|
508 * Display, save, or load history. |
|
509 */ |
|
510 tree_constant |
|
511 builtin_history (int argc, char **argv) |
|
512 { |
|
513 tree_constant retval; |
|
514 |
|
515 do_history (argc, argv); |
|
516 |
|
517 return retval; |
|
518 } |
|
519 |
|
520 static int |
|
521 load_variable (char *nm, int force, istream& is) |
|
522 { |
|
523 symbol_record *gsr = global_sym_tab->lookup (nm, 0, 0); |
|
524 symbol_record *lsr = curr_sym_tab->lookup (nm, 0, 0); |
|
525 |
|
526 if (! force |
|
527 && ((gsr != (symbol_record *) NULL && gsr->is_variable ()) |
|
528 || lsr != (symbol_record *) NULL)) |
|
529 { |
|
530 message ("load", |
|
531 "variable name `%s' exists -- use `load -force' to overwrite", nm); |
|
532 return -1; |
|
533 } |
|
534 |
|
535 // We found it. Read data for this entry, and if that succeeds, |
|
536 // insert it into symbol table. |
|
537 |
|
538 tree_constant tc; |
|
539 int global = tc.load (is); |
|
540 if (tc.const_type () != tree_constant_rep::unknown_constant) |
|
541 { |
|
542 symbol_record *sr; |
|
543 if (global) |
|
544 { |
|
545 if (lsr != (symbol_record *) NULL) |
|
546 { |
|
547 warning ("load: replacing local symbol `%s' with global\ |
|
548 value from file", nm); |
|
549 curr_sym_tab->clear (nm); |
|
550 } |
|
551 sr = global_sym_tab->lookup (nm, 1, 0); |
|
552 } |
|
553 else |
|
554 { |
|
555 if (gsr != (symbol_record *) NULL) |
|
556 { |
|
557 warning ("loading `%s' as a global variable", nm); |
|
558 sr = gsr; |
|
559 } |
|
560 else |
|
561 sr = curr_sym_tab->lookup (nm, 1, 0); |
|
562 } |
|
563 |
|
564 if (sr != (symbol_record *) NULL) |
|
565 { |
|
566 tree_constant *tmp_tc = new tree_constant (tc); |
|
567 sr->define (tmp_tc); |
|
568 return 1; |
|
569 } |
|
570 else |
|
571 error ("load: unable to load variable `%s'", nm); |
|
572 } |
|
573 |
|
574 return 0; |
|
575 } |
|
576 |
|
577 /* |
|
578 * Read variables from an input stream. |
|
579 * |
|
580 * BUGS: |
|
581 * |
|
582 * -- This function is not terribly robust. |
|
583 * -- Symbols are only inserted into the current symbol table. |
|
584 */ |
|
585 tree_constant |
|
586 builtin_load (int argc, char **argv) |
|
587 { |
|
588 tree_constant retval; |
|
589 |
|
590 argc--; |
|
591 argv++; |
|
592 |
|
593 int force = 0; |
|
594 if (argc > 0 && strcmp (*argv, "-force") == 0) |
|
595 { |
|
596 force++; |
|
597 argc--; |
|
598 argv++; |
|
599 } |
|
600 |
|
601 if (argc < 1) |
|
602 { |
|
603 message ("load", "you must specify a single file to read"); |
|
604 return retval; |
|
605 } |
|
606 |
|
607 static istream stream; |
|
608 static ifstream file; |
|
609 if (strcmp (*argv, "-") == 0) |
|
610 { |
|
611 stream = cin; |
|
612 } |
|
613 else |
|
614 { |
|
615 char *fname = tilde_expand (*argv); |
|
616 file.open (fname); |
|
617 if (! file) |
|
618 { |
|
619 error ("load: couldn't open input file `%s'", *argv); |
|
620 return retval; |
|
621 } |
|
622 stream = file; |
|
623 } |
|
624 |
|
625 char nm [128]; // XXX FIXME XXX |
|
626 int count = 0; |
|
627 for (;;) |
|
628 { |
|
629 // Read name for this entry or break on EOF. |
|
630 if (extract_keyword (stream, "name", nm) == 0 || nm == (char *) NULL) |
|
631 { |
|
632 if (count == 0) |
|
633 message ("load", |
|
634 "no name keywords found. Are you sure this is an octave data file?"); |
|
635 break; |
|
636 } |
|
637 |
|
638 if (*nm == '\0') |
|
639 continue; |
|
640 |
|
641 if (! valid_identifier (nm)) |
|
642 { |
|
643 message ("load", "skipping bogus identifier `%s'", nm); |
|
644 continue; |
|
645 } |
|
646 |
|
647 if (load_variable (nm, force, stream)) |
|
648 count++; |
|
649 } |
|
650 |
|
651 if (file); |
|
652 file.close (); |
|
653 |
|
654 return retval; |
|
655 } |
|
656 |
|
657 /* |
|
658 * Get a directory listing. |
|
659 */ |
|
660 tree_constant |
|
661 builtin_ls (int argc, char **argv) |
|
662 { |
|
663 tree_constant retval; |
|
664 |
|
665 ostrstream ls_buf; |
|
666 |
|
667 ls_buf << "ls -C "; |
|
668 for (int i = 1; i < argc; i++) |
|
669 ls_buf << tilde_expand (argv[i]) << " "; |
|
670 |
|
671 ls_buf << ends; |
|
672 |
|
673 char *ls_command = ls_buf.str (); |
|
674 |
|
675 iprocstream cmd (ls_command); |
|
676 |
|
677 char ch; |
|
678 ostrstream output_buf; |
|
679 while (cmd.get (ch)) |
|
680 output_buf.put (ch); |
|
681 |
|
682 output_buf << ends; |
|
683 |
|
684 maybe_page_output (output_buf); |
|
685 |
|
686 delete [] ls_command; |
|
687 |
|
688 return retval; |
|
689 } |
|
690 |
|
691 /* |
62
|
692 * Run previous commands from the history list. |
|
693 */ |
|
694 tree_constant |
|
695 builtin_run_history (int argc, char **argv) |
|
696 { |
|
697 tree_constant retval; |
|
698 do_run_history (argc, argv); |
|
699 return retval; |
|
700 } |
|
701 |
|
702 /* |
1
|
703 * Write variables to an output stream. |
|
704 */ |
|
705 tree_constant |
|
706 builtin_save (int argc, char **argv) |
|
707 { |
|
708 tree_constant retval; |
|
709 |
|
710 if (argc < 2) |
|
711 { |
|
712 usage ("save file -- save all variables in named file\n\ |
|
713 save file var ... -- saved named variables"); |
|
714 return retval; |
|
715 } |
|
716 |
|
717 argc--; |
|
718 argv++; |
|
719 |
|
720 static ostream stream; |
|
721 static ofstream file; |
|
722 if (strcmp (*argv, "-") == 0) |
|
723 { |
|
724 // XXX FIXME XXX -- things intended for the screen should end up in a |
|
725 // tree_constant (string)? |
|
726 stream = cout; |
|
727 } |
|
728 else |
|
729 { |
|
730 char *fname = tilde_expand (*argv); |
|
731 file.open (fname); |
|
732 if (! file) |
|
733 { |
|
734 error ("save: couldn't open output file `%s'", *argv); |
|
735 return retval; |
|
736 } |
|
737 stream = file; |
|
738 } |
|
739 |
|
740 if (argc == 1) |
|
741 { |
|
742 curr_sym_tab->save (stream); |
|
743 global_sym_tab->save (stream, 1); |
|
744 } |
|
745 else |
|
746 { |
|
747 while (--argc > 0) |
|
748 { |
|
749 argv++; |
|
750 if (! curr_sym_tab->save (stream, *argv)) |
|
751 if (! global_sym_tab->save (stream, *argv, 1)) |
|
752 { |
|
753 message ("save", "no such variable `%s'", *argv); |
|
754 continue; |
|
755 } |
|
756 } |
|
757 } |
|
758 |
|
759 if (file); |
|
760 file.close (); |
|
761 |
|
762 return retval; |
|
763 } |
|
764 |
|
765 /* |
|
766 * Set plotting options. |
|
767 */ |
|
768 tree_constant |
|
769 builtin_set (int argc, char **argv) |
|
770 { |
|
771 tree_constant retval; |
|
772 |
|
773 ostrstream plot_buf; |
|
774 |
|
775 if (argc > 1) |
|
776 { |
|
777 if (almost_match ("parametric", argv[1], 3)) |
|
778 parametric_plot = 1; |
|
779 else if (almost_match ("noparametric", argv[1], 5)) |
|
780 parametric_plot = 0; |
|
781 } |
|
782 |
|
783 for (int i = 0; i < argc; i++) |
|
784 plot_buf << argv[i] << " "; |
|
785 |
|
786 plot_buf << "\n" << ends; |
|
787 |
|
788 char *plot_command = plot_buf.str (); |
|
789 send_to_plot_stream (plot_command); |
|
790 |
|
791 delete [] plot_command; |
|
792 |
|
793 return retval; |
|
794 } |
|
795 |
|
796 /* |
|
797 * Set plotting options. |
|
798 */ |
|
799 tree_constant |
|
800 builtin_show (int argc, char **argv) |
|
801 { |
|
802 tree_constant retval; |
|
803 |
|
804 ostrstream plot_buf; |
|
805 |
|
806 for (int i = 0; i < argc; i++) |
|
807 plot_buf << argv[i] << " "; |
|
808 |
|
809 plot_buf << "\n" << ends; |
|
810 |
|
811 char *plot_command = plot_buf.str (); |
|
812 send_to_plot_stream (plot_command); |
|
813 |
|
814 delete [] plot_command; |
|
815 |
|
816 return retval; |
|
817 } |
|
818 |
|
819 /* |
|
820 * List variable names. |
|
821 */ |
|
822 tree_constant |
|
823 builtin_who (int argc, char **argv) |
|
824 { |
|
825 tree_constant retval; |
|
826 int show_global = 0; |
|
827 int show_local = 1; |
|
828 int show_top = 0; |
|
829 int show_fcns = 0; |
|
830 |
|
831 if (argc > 1) |
|
832 show_local = 0; |
|
833 |
|
834 for (int i = 1; i < argc; i++) |
|
835 { |
|
836 argv++; |
|
837 if (strcmp (*argv, "-all") == 0) |
|
838 { |
|
839 show_global++; |
|
840 show_local++; |
|
841 show_top++; |
|
842 show_fcns++; |
|
843 } |
|
844 else if (strcmp (*argv, "-global") == 0) |
|
845 show_global++; |
|
846 else if (strcmp (*argv, "-local") == 0) |
|
847 show_local++; |
|
848 else if (strcmp (*argv, "-top") == 0) |
|
849 show_top++; |
|
850 else if (strcmp (*argv, "-fcn") == 0 |
|
851 || strcmp (*argv, "-fcns") == 0 |
|
852 || strcmp (*argv, "-functions") == 0) |
|
853 show_fcns++; |
|
854 else |
|
855 { |
|
856 message ("who", "unrecognized option `%s'", *argv); |
|
857 if (argc == 2) |
|
858 show_local = 1; |
|
859 } |
|
860 } |
|
861 |
|
862 ostrstream output_buf; |
|
863 int pad_after = 0; |
|
864 if (show_global) |
|
865 { |
|
866 int count = 0; |
|
867 char **symbols = global_sym_tab->sorted_var_list (count); |
|
868 if (symbols != (char **) NULL && count > 0) |
|
869 { |
|
870 output_buf << "\n*** global symbols:\n\n"; |
|
871 list_in_columns (output_buf, symbols); |
|
872 delete [] symbols; |
|
873 pad_after++; |
|
874 } |
|
875 } |
|
876 |
|
877 if (show_top) |
|
878 { |
|
879 int count = 0; |
|
880 char **symbols = top_level_sym_tab->sorted_var_list (count); |
|
881 if (symbols != (char **) NULL && count > 0) |
|
882 { |
|
883 output_buf << "\n*** top level symbols:\n\n"; |
|
884 list_in_columns (output_buf, symbols); |
|
885 delete [] symbols; |
|
886 pad_after++; |
|
887 } |
|
888 } |
|
889 |
|
890 if (show_local) |
|
891 { |
|
892 if (show_top && curr_sym_tab == top_level_sym_tab) |
|
893 output_buf << |
|
894 "\ncurrent (local) symbol table == top level symbol table\n"; |
|
895 else |
|
896 { |
|
897 int count = 0; |
|
898 char **symbols = curr_sym_tab->sorted_var_list (count); |
|
899 if (symbols != (char **) NULL && count > 0) |
|
900 { |
|
901 output_buf << "\n*** local symbols:\n\n"; |
|
902 list_in_columns (output_buf, symbols); |
|
903 delete [] symbols; |
|
904 pad_after++; |
|
905 } |
|
906 } |
|
907 } |
|
908 |
|
909 if (show_fcns) |
|
910 { |
|
911 int count = 0; |
|
912 char **symbols = global_sym_tab->sorted_fcn_list (count); |
|
913 if (symbols != (char **) NULL && count > 0) |
|
914 { |
|
915 output_buf << "\n*** functions builtin or currently compiled:\n\n"; |
|
916 list_in_columns (output_buf, symbols); |
|
917 delete [] symbols; |
|
918 pad_after++; |
|
919 } |
|
920 } |
|
921 |
|
922 if (pad_after) |
|
923 output_buf << "\n"; |
|
924 |
|
925 output_buf << ends; |
|
926 maybe_page_output (output_buf); |
|
927 |
|
928 return retval; |
|
929 } |
|
930 |
|
931 /* |
|
932 ;;; Local Variables: *** |
|
933 ;;; mode: C++ *** |
|
934 ;;; page-delimiter: "^/\\*" *** |
|
935 ;;; End: *** |
|
936 */ |