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 |
1
|
286 } |
|
287 return retval; |
|
288 } |
|
289 |
|
290 /* |
|
291 * Associate a cryptic message with a variable name. |
|
292 */ |
|
293 tree_constant |
|
294 builtin_document (int argc, char **argv) |
|
295 { |
|
296 tree_constant retval; |
|
297 if (argc == 3) |
|
298 { |
|
299 symbol_record *sym_rec = curr_sym_tab->lookup (argv[1], 0); |
|
300 if (sym_rec == (symbol_record *) NULL) |
|
301 { |
|
302 sym_rec = global_sym_tab->lookup (argv[1], 0); |
|
303 if (sym_rec == (symbol_record *) NULL) |
|
304 { |
|
305 error ("document: no such symbol `%s'", argv[1]); |
|
306 return retval; |
|
307 } |
|
308 } |
|
309 sym_rec->document (argv[2]); |
|
310 } |
|
311 else |
|
312 usage ("document symbol string ..."); |
|
313 |
|
314 return retval; |
|
315 } |
|
316 |
|
317 /* |
|
318 * Edit commands with your favorite editor. |
|
319 */ |
|
320 tree_constant |
|
321 builtin_edit_history (int argc, char **argv) |
|
322 { |
|
323 tree_constant retval; |
|
324 do_edit_history (argc, argv); |
|
325 return retval; |
|
326 } |
|
327 |
|
328 /* |
|
329 * Set output format state. |
|
330 */ |
|
331 tree_constant |
|
332 builtin_format (int argc, char **argv) |
|
333 { |
|
334 tree_constant retval; |
|
335 set_format_style (argc, argv); |
|
336 return retval; |
|
337 } |
|
338 |
|
339 /* |
|
340 * Print cryptic yet witty messages. |
|
341 */ |
|
342 tree_constant |
|
343 builtin_help (int argc, char **argv) |
|
344 { |
|
345 tree_constant retval; |
|
346 |
|
347 ostrstream output_buf; |
|
348 if (argc == 1) |
|
349 { |
|
350 char **symbols; |
|
351 int count = 0; |
|
352 |
|
353 symbols = names (operator_help (), count); |
|
354 output_buf << "\n*** operators:\n\n"; |
|
355 if (symbols != (char **) NULL && count > 0) |
|
356 list_in_columns (output_buf, symbols); |
|
357 delete [] symbols; |
|
358 |
|
359 symbols = names (keyword_help (), count); |
|
360 output_buf << "\n*** reserved words:\n\n"; |
|
361 if (symbols != (char **) NULL && count > 0) |
|
362 list_in_columns (output_buf, symbols); |
|
363 delete [] symbols; |
|
364 |
|
365 symbols = names (builtin_text_functions_help (), count); |
|
366 output_buf |
|
367 << "\n*** text functions (these names are also reserved):\n\n"; |
|
368 if (symbols != (char **) NULL && count > 0) |
|
369 list_in_columns (output_buf, symbols); |
|
370 delete [] symbols; |
|
371 |
|
372 symbols = names (builtin_mapper_functions_help (), count); |
|
373 output_buf << "\n*** mapper functions:\n\n"; |
|
374 if (symbols != (char **) NULL && count > 0) |
|
375 list_in_columns (output_buf, symbols); |
|
376 delete [] symbols; |
|
377 |
|
378 symbols = names (builtin_general_functions_help (), count); |
|
379 output_buf << "\n*** general functions:\n\n"; |
|
380 if (symbols != (char **) NULL && count > 0) |
|
381 list_in_columns (output_buf, symbols); |
|
382 delete [] symbols; |
|
383 |
|
384 symbols = names (builtin_variables_help (), count); |
|
385 output_buf << "\n*** builtin variables:\n\n"; |
|
386 if (symbols != (char **) NULL && count > 0) |
|
387 list_in_columns (output_buf, symbols); |
|
388 delete [] symbols; |
|
389 |
|
390 // Also need to list variables and currently compiled functions from |
|
391 // the symbol table, if there are any. |
|
392 |
|
393 // Also need to search octave_path for script files. |
|
394 |
|
395 char **path = pathstring_to_vector (user_pref.loadpath); |
|
396 |
|
397 char **ptr = path; |
|
398 if (ptr != (char **) NULL) |
|
399 { |
|
400 while (*ptr != (char *) NULL) |
|
401 { |
|
402 int count; |
|
403 char **names = get_m_file_names (count, *ptr, 0); |
|
404 output_buf << "\n*** M-files in " |
|
405 << make_absolute (*ptr, the_current_working_directory) |
|
406 << ":\n\n"; |
|
407 if (names != (char **) NULL && count > 0) |
|
408 list_in_columns (output_buf, names); |
|
409 delete [] names; |
|
410 ptr++; |
|
411 } |
|
412 } |
|
413 } |
|
414 else |
|
415 { |
110
|
416 char *m_file_name = (char *) NULL; |
1
|
417 symbol_record *sym_rec; |
|
418 help_list *op_help_list = operator_help (); |
|
419 help_list *kw_help_list = keyword_help (); |
|
420 for (int i = 1; i < argc; i++) |
|
421 { |
|
422 if (argv[i] == (char *) NULL || argv[i][0] == '\0') |
|
423 continue; |
|
424 |
|
425 int j = 0; |
|
426 char *name; |
|
427 while ((name = op_help_list[j].name) != (char *) NULL) |
|
428 { |
|
429 if (strcmp (name, argv[i]) == 0) |
|
430 { |
|
431 output_buf << "\n" << op_help_list[j].help << "\n"; |
|
432 goto next; |
|
433 } |
|
434 j++; |
|
435 } |
|
436 |
|
437 j = 0; |
|
438 while ((name = kw_help_list[j].name) != (char *) NULL) |
|
439 { |
|
440 if (strcmp (name, argv[i]) == 0) |
|
441 { |
|
442 output_buf << "\n" << kw_help_list[j].help << "\n"; |
|
443 goto next; |
|
444 } |
|
445 j++; |
|
446 } |
|
447 |
|
448 sym_rec = curr_sym_tab->lookup (argv[i], 0, 0); |
|
449 if (sym_rec != (symbol_record *) NULL) |
|
450 { |
|
451 char *h = sym_rec->help (); |
|
452 if (h != (char *) NULL && *h != '\0') |
|
453 { |
|
454 output_buf << "\n" << h << "\n"; |
|
455 goto next; |
|
456 } |
|
457 } |
|
458 |
|
459 sym_rec = global_sym_tab->lookup (argv[i], 0, 0); |
|
460 if (sym_rec != (symbol_record *) NULL |
|
461 && ! symbol_out_of_date (sym_rec)) |
|
462 { |
|
463 char *h = sym_rec->help (); |
|
464 if (h != (char *) NULL && *h != '\0') |
|
465 { |
|
466 output_buf << "\n" << h << "\n"; |
|
467 goto next; |
|
468 } |
|
469 } |
|
470 |
|
471 // Try harder to find M-files that might not be defined yet, or that |
|
472 // appear to be out of date. Don\'t execute commands from the file if |
|
473 // it turns out to be a script file. |
|
474 |
110
|
475 m_file_name = m_file_in_path (argv[i]); |
|
476 if (m_file_name != (char *) NULL) |
1
|
477 { |
110
|
478 sym_rec = global_sym_tab->lookup (argv[i], 1, 0); |
|
479 if (sym_rec != (symbol_record *) NULL) |
1
|
480 { |
110
|
481 tree_identifier tmp (sym_rec); |
|
482 tmp.parse_m_file (0); |
|
483 char *h = sym_rec->help (); |
|
484 if (h != (char *) NULL && *h != '\0') |
|
485 { |
|
486 output_buf << "\n" << h << "\n"; |
|
487 goto next; |
|
488 } |
1
|
489 } |
|
490 } |
110
|
491 delete [] m_file_name; |
1
|
492 |
|
493 output_buf << "Sorry, `" << argv[i] << "' is not documented\n"; |
|
494 |
|
495 next: |
|
496 continue; |
|
497 } |
|
498 } |
|
499 |
|
500 output_buf << ends; |
|
501 maybe_page_output (output_buf); |
|
502 |
|
503 return retval; |
|
504 } |
|
505 |
|
506 /* |
|
507 * Display, save, or load history. |
|
508 */ |
|
509 tree_constant |
|
510 builtin_history (int argc, char **argv) |
|
511 { |
|
512 tree_constant retval; |
|
513 |
|
514 do_history (argc, argv); |
|
515 |
|
516 return retval; |
|
517 } |
|
518 |
|
519 static int |
|
520 load_variable (char *nm, int force, istream& is) |
|
521 { |
|
522 symbol_record *gsr = global_sym_tab->lookup (nm, 0, 0); |
|
523 symbol_record *lsr = curr_sym_tab->lookup (nm, 0, 0); |
|
524 |
|
525 if (! force |
|
526 && ((gsr != (symbol_record *) NULL && gsr->is_variable ()) |
|
527 || lsr != (symbol_record *) NULL)) |
|
528 { |
|
529 message ("load", |
|
530 "variable name `%s' exists -- use `load -force' to overwrite", nm); |
|
531 return -1; |
|
532 } |
|
533 |
|
534 // We found it. Read data for this entry, and if that succeeds, |
|
535 // insert it into symbol table. |
|
536 |
|
537 tree_constant tc; |
|
538 int global = tc.load (is); |
|
539 if (tc.const_type () != tree_constant_rep::unknown_constant) |
|
540 { |
|
541 symbol_record *sr; |
|
542 if (global) |
|
543 { |
|
544 if (lsr != (symbol_record *) NULL) |
|
545 { |
|
546 warning ("load: replacing local symbol `%s' with global\ |
|
547 value from file", nm); |
|
548 curr_sym_tab->clear (nm); |
|
549 } |
|
550 sr = global_sym_tab->lookup (nm, 1, 0); |
|
551 } |
|
552 else |
|
553 { |
|
554 if (gsr != (symbol_record *) NULL) |
|
555 { |
|
556 warning ("loading `%s' as a global variable", nm); |
|
557 sr = gsr; |
|
558 } |
|
559 else |
|
560 sr = curr_sym_tab->lookup (nm, 1, 0); |
|
561 } |
|
562 |
|
563 if (sr != (symbol_record *) NULL) |
|
564 { |
|
565 tree_constant *tmp_tc = new tree_constant (tc); |
|
566 sr->define (tmp_tc); |
|
567 return 1; |
|
568 } |
|
569 else |
|
570 error ("load: unable to load variable `%s'", nm); |
|
571 } |
|
572 |
|
573 return 0; |
|
574 } |
|
575 |
|
576 /* |
|
577 * Read variables from an input stream. |
|
578 * |
|
579 * BUGS: |
|
580 * |
|
581 * -- This function is not terribly robust. |
|
582 * -- Symbols are only inserted into the current symbol table. |
|
583 */ |
|
584 tree_constant |
|
585 builtin_load (int argc, char **argv) |
|
586 { |
|
587 tree_constant retval; |
|
588 |
|
589 argc--; |
|
590 argv++; |
|
591 |
|
592 int force = 0; |
|
593 if (argc > 0 && strcmp (*argv, "-force") == 0) |
|
594 { |
|
595 force++; |
|
596 argc--; |
|
597 argv++; |
|
598 } |
|
599 |
|
600 if (argc < 1) |
|
601 { |
|
602 message ("load", "you must specify a single file to read"); |
|
603 return retval; |
|
604 } |
|
605 |
|
606 static istream stream; |
|
607 static ifstream file; |
|
608 if (strcmp (*argv, "-") == 0) |
|
609 { |
|
610 stream = cin; |
|
611 } |
|
612 else |
|
613 { |
|
614 char *fname = tilde_expand (*argv); |
|
615 file.open (fname); |
|
616 if (! file) |
|
617 { |
|
618 error ("load: couldn't open input file `%s'", *argv); |
|
619 return retval; |
|
620 } |
|
621 stream = file; |
|
622 } |
|
623 |
|
624 char nm [128]; // XXX FIXME XXX |
|
625 int count = 0; |
|
626 for (;;) |
|
627 { |
|
628 // Read name for this entry or break on EOF. |
|
629 if (extract_keyword (stream, "name", nm) == 0 || nm == (char *) NULL) |
|
630 { |
|
631 if (count == 0) |
|
632 message ("load", |
|
633 "no name keywords found. Are you sure this is an octave data file?"); |
|
634 break; |
|
635 } |
|
636 |
|
637 if (*nm == '\0') |
|
638 continue; |
|
639 |
|
640 if (! valid_identifier (nm)) |
|
641 { |
|
642 message ("load", "skipping bogus identifier `%s'", nm); |
|
643 continue; |
|
644 } |
|
645 |
|
646 if (load_variable (nm, force, stream)) |
|
647 count++; |
|
648 } |
|
649 |
|
650 if (file); |
|
651 file.close (); |
|
652 |
|
653 return retval; |
|
654 } |
|
655 |
|
656 /* |
|
657 * Get a directory listing. |
|
658 */ |
|
659 tree_constant |
|
660 builtin_ls (int argc, char **argv) |
|
661 { |
|
662 tree_constant retval; |
|
663 |
|
664 ostrstream ls_buf; |
|
665 |
|
666 ls_buf << "ls -C "; |
|
667 for (int i = 1; i < argc; i++) |
|
668 ls_buf << tilde_expand (argv[i]) << " "; |
|
669 |
|
670 ls_buf << ends; |
|
671 |
|
672 char *ls_command = ls_buf.str (); |
|
673 |
|
674 iprocstream cmd (ls_command); |
|
675 |
|
676 char ch; |
|
677 ostrstream output_buf; |
|
678 while (cmd.get (ch)) |
|
679 output_buf.put (ch); |
|
680 |
|
681 output_buf << ends; |
|
682 |
|
683 maybe_page_output (output_buf); |
|
684 |
|
685 delete [] ls_command; |
|
686 |
|
687 return retval; |
|
688 } |
|
689 |
|
690 /* |
62
|
691 * Run previous commands from the history list. |
|
692 */ |
|
693 tree_constant |
|
694 builtin_run_history (int argc, char **argv) |
|
695 { |
|
696 tree_constant retval; |
|
697 do_run_history (argc, argv); |
|
698 return retval; |
|
699 } |
|
700 |
|
701 /* |
1
|
702 * Write variables to an output stream. |
|
703 */ |
|
704 tree_constant |
|
705 builtin_save (int argc, char **argv) |
|
706 { |
|
707 tree_constant retval; |
|
708 |
|
709 if (argc < 2) |
|
710 { |
|
711 usage ("save file -- save all variables in named file\n\ |
|
712 save file var ... -- saved named variables"); |
|
713 return retval; |
|
714 } |
|
715 |
|
716 argc--; |
|
717 argv++; |
|
718 |
|
719 static ostream stream; |
|
720 static ofstream file; |
|
721 if (strcmp (*argv, "-") == 0) |
|
722 { |
|
723 // XXX FIXME XXX -- things intended for the screen should end up in a |
|
724 // tree_constant (string)? |
|
725 stream = cout; |
|
726 } |
|
727 else |
|
728 { |
|
729 char *fname = tilde_expand (*argv); |
|
730 file.open (fname); |
|
731 if (! file) |
|
732 { |
|
733 error ("save: couldn't open output file `%s'", *argv); |
|
734 return retval; |
|
735 } |
|
736 stream = file; |
|
737 } |
|
738 |
|
739 if (argc == 1) |
|
740 { |
|
741 curr_sym_tab->save (stream); |
|
742 global_sym_tab->save (stream, 1); |
|
743 } |
|
744 else |
|
745 { |
|
746 while (--argc > 0) |
|
747 { |
|
748 argv++; |
|
749 if (! curr_sym_tab->save (stream, *argv)) |
|
750 if (! global_sym_tab->save (stream, *argv, 1)) |
|
751 { |
|
752 message ("save", "no such variable `%s'", *argv); |
|
753 continue; |
|
754 } |
|
755 } |
|
756 } |
|
757 |
|
758 if (file); |
|
759 file.close (); |
|
760 |
|
761 return retval; |
|
762 } |
|
763 |
|
764 /* |
|
765 * Set plotting options. |
|
766 */ |
|
767 tree_constant |
|
768 builtin_set (int argc, char **argv) |
|
769 { |
|
770 tree_constant retval; |
|
771 |
|
772 ostrstream plot_buf; |
|
773 |
|
774 if (argc > 1) |
|
775 { |
|
776 if (almost_match ("parametric", argv[1], 3)) |
|
777 parametric_plot = 1; |
|
778 else if (almost_match ("noparametric", argv[1], 5)) |
|
779 parametric_plot = 0; |
|
780 } |
|
781 |
|
782 for (int i = 0; i < argc; i++) |
|
783 plot_buf << argv[i] << " "; |
|
784 |
|
785 plot_buf << "\n" << ends; |
|
786 |
|
787 char *plot_command = plot_buf.str (); |
|
788 send_to_plot_stream (plot_command); |
|
789 |
|
790 delete [] plot_command; |
|
791 |
|
792 return retval; |
|
793 } |
|
794 |
|
795 /* |
|
796 * Set plotting options. |
|
797 */ |
|
798 tree_constant |
|
799 builtin_show (int argc, char **argv) |
|
800 { |
|
801 tree_constant retval; |
|
802 |
|
803 ostrstream plot_buf; |
|
804 |
|
805 for (int i = 0; i < argc; i++) |
|
806 plot_buf << argv[i] << " "; |
|
807 |
|
808 plot_buf << "\n" << ends; |
|
809 |
|
810 char *plot_command = plot_buf.str (); |
|
811 send_to_plot_stream (plot_command); |
|
812 |
|
813 delete [] plot_command; |
|
814 |
|
815 return retval; |
|
816 } |
|
817 |
|
818 /* |
|
819 * List variable names. |
|
820 */ |
|
821 tree_constant |
|
822 builtin_who (int argc, char **argv) |
|
823 { |
|
824 tree_constant retval; |
|
825 int show_global = 0; |
|
826 int show_local = 1; |
|
827 int show_top = 0; |
|
828 int show_fcns = 0; |
|
829 |
|
830 if (argc > 1) |
|
831 show_local = 0; |
|
832 |
|
833 for (int i = 1; i < argc; i++) |
|
834 { |
|
835 argv++; |
|
836 if (strcmp (*argv, "-all") == 0) |
|
837 { |
|
838 show_global++; |
|
839 show_local++; |
|
840 show_top++; |
|
841 show_fcns++; |
|
842 } |
|
843 else if (strcmp (*argv, "-global") == 0) |
|
844 show_global++; |
|
845 else if (strcmp (*argv, "-local") == 0) |
|
846 show_local++; |
|
847 else if (strcmp (*argv, "-top") == 0) |
|
848 show_top++; |
|
849 else if (strcmp (*argv, "-fcn") == 0 |
|
850 || strcmp (*argv, "-fcns") == 0 |
|
851 || strcmp (*argv, "-functions") == 0) |
|
852 show_fcns++; |
|
853 else |
|
854 { |
|
855 message ("who", "unrecognized option `%s'", *argv); |
|
856 if (argc == 2) |
|
857 show_local = 1; |
|
858 } |
|
859 } |
|
860 |
|
861 ostrstream output_buf; |
|
862 int pad_after = 0; |
|
863 if (show_global) |
|
864 { |
|
865 int count = 0; |
|
866 char **symbols = global_sym_tab->sorted_var_list (count); |
|
867 if (symbols != (char **) NULL && count > 0) |
|
868 { |
|
869 output_buf << "\n*** global symbols:\n\n"; |
|
870 list_in_columns (output_buf, symbols); |
|
871 delete [] symbols; |
|
872 pad_after++; |
|
873 } |
|
874 } |
|
875 |
|
876 if (show_top) |
|
877 { |
|
878 int count = 0; |
|
879 char **symbols = top_level_sym_tab->sorted_var_list (count); |
|
880 if (symbols != (char **) NULL && count > 0) |
|
881 { |
|
882 output_buf << "\n*** top level symbols:\n\n"; |
|
883 list_in_columns (output_buf, symbols); |
|
884 delete [] symbols; |
|
885 pad_after++; |
|
886 } |
|
887 } |
|
888 |
|
889 if (show_local) |
|
890 { |
|
891 if (show_top && curr_sym_tab == top_level_sym_tab) |
|
892 output_buf << |
|
893 "\ncurrent (local) symbol table == top level symbol table\n"; |
|
894 else |
|
895 { |
|
896 int count = 0; |
|
897 char **symbols = curr_sym_tab->sorted_var_list (count); |
|
898 if (symbols != (char **) NULL && count > 0) |
|
899 { |
|
900 output_buf << "\n*** local symbols:\n\n"; |
|
901 list_in_columns (output_buf, symbols); |
|
902 delete [] symbols; |
|
903 pad_after++; |
|
904 } |
|
905 } |
|
906 } |
|
907 |
|
908 if (show_fcns) |
|
909 { |
|
910 int count = 0; |
|
911 char **symbols = global_sym_tab->sorted_fcn_list (count); |
|
912 if (symbols != (char **) NULL && count > 0) |
|
913 { |
|
914 output_buf << "\n*** functions builtin or currently compiled:\n\n"; |
|
915 list_in_columns (output_buf, symbols); |
|
916 delete [] symbols; |
|
917 pad_after++; |
|
918 } |
|
919 } |
|
920 |
|
921 if (pad_after) |
|
922 output_buf << "\n"; |
|
923 |
|
924 output_buf << ends; |
|
925 maybe_page_output (output_buf); |
|
926 |
|
927 return retval; |
|
928 } |
|
929 |
|
930 /* |
|
931 ;;; Local Variables: *** |
|
932 ;;; mode: C++ *** |
|
933 ;;; page-delimiter: "^/\\*" *** |
|
934 ;;; End: *** |
|
935 */ |