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 |
195
|
249 * regular expressions. |
|
250 * |
|
251 * It's not likely that this works correctly now. XXX FIXME XXX |
1
|
252 */ |
|
253 tree_constant |
|
254 builtin_clear (int argc, char **argv) |
|
255 { |
|
256 tree_constant retval; |
195
|
257 |
|
258 // Always clear the local table, but don't clear currently compiled |
|
259 // functions unless we are at the top level. (Allowing that to happen |
|
260 // inside functions would result in pretty odd behavior...) |
|
261 |
|
262 int clear_user_functions = (curr_sym_tab == top_level_sym_tab); |
|
263 |
1
|
264 if (argc == 1) |
|
265 { |
|
266 curr_sym_tab->clear (); |
195
|
267 global_sym_tab->clear (clear_user_functions); |
1
|
268 } |
|
269 else |
|
270 { |
195
|
271 int lcount; |
|
272 char **lvars = curr_sym_tab->list (lcount, 0, |
|
273 symbol_def::USER_VARIABLE, |
|
274 SYMTAB_LOCAL_SCOPE); |
|
275 int gcount; |
|
276 char **gvars = curr_sym_tab->list (gcount, 0, |
|
277 symbol_def::USER_VARIABLE, |
|
278 SYMTAB_GLOBAL_SCOPE); |
|
279 int fcount; |
|
280 char **fcns = curr_sym_tab->list (fcount, 0, |
|
281 symbol_def::USER_FUNCTION, |
|
282 SYMTAB_ALL_SCOPES); |
79
|
283 |
1
|
284 while (--argc > 0) |
|
285 { |
|
286 argv++; |
79
|
287 if (*argv != (char *) NULL) |
|
288 { |
|
289 int i; |
195
|
290 for (i = 0; i < lcount; i++) |
79
|
291 { |
266
|
292 if (fnmatch (*argv, lvars[i], __FNM_FLAGS) == 0) |
195
|
293 curr_sym_tab->clear (lvars[i]); |
|
294 } |
|
295 |
|
296 int count; |
|
297 for (i = 0; i < gcount; i++) |
|
298 { |
266
|
299 if (fnmatch (*argv, gvars[i], __FNM_FLAGS) == 0) |
79
|
300 { |
195
|
301 count = curr_sym_tab->clear (gvars[i]); |
|
302 if (count > 0) |
|
303 global_sym_tab->clear (gvars[i], clear_user_functions); |
79
|
304 } |
|
305 } |
|
306 |
195
|
307 for (i = 0; i < fcount; i++) |
79
|
308 { |
266
|
309 if (fnmatch (*argv, fcns[i], __FNM_FLAGS) == 0) |
79
|
310 { |
195
|
311 count = curr_sym_tab->clear (fcns[i]); |
|
312 if (count > 0) |
|
313 global_sym_tab->clear (fcns[i], clear_user_functions); |
79
|
314 } |
|
315 } |
|
316 } |
1
|
317 } |
79
|
318 |
195
|
319 delete [] lvars; |
|
320 delete [] gvars; |
|
321 delete [] fcns; |
79
|
322 |
1
|
323 } |
|
324 return retval; |
|
325 } |
|
326 |
|
327 /* |
|
328 * Associate a cryptic message with a variable name. |
|
329 */ |
|
330 tree_constant |
|
331 builtin_document (int argc, char **argv) |
|
332 { |
|
333 tree_constant retval; |
|
334 if (argc == 3) |
195
|
335 document_symbol (argv[1], argv[2]); |
1
|
336 else |
181
|
337 print_usage ("document"); |
1
|
338 return retval; |
|
339 } |
|
340 |
|
341 /* |
|
342 * Edit commands with your favorite editor. |
|
343 */ |
|
344 tree_constant |
|
345 builtin_edit_history (int argc, char **argv) |
|
346 { |
|
347 tree_constant retval; |
|
348 do_edit_history (argc, argv); |
|
349 return retval; |
|
350 } |
|
351 |
|
352 /* |
|
353 * Set output format state. |
|
354 */ |
|
355 tree_constant |
|
356 builtin_format (int argc, char **argv) |
|
357 { |
|
358 tree_constant retval; |
|
359 set_format_style (argc, argv); |
|
360 return retval; |
|
361 } |
|
362 |
169
|
363 static void |
|
364 help_syms_list (ostrstream& output_buf, help_list *list, |
|
365 const char *desc) |
|
366 { |
|
367 int count = 0; |
|
368 char **symbols = names (list, count); |
|
369 output_buf << "\n*** " << desc << ":\n\n"; |
|
370 if (symbols != (char **) NULL && count > 0) |
|
371 list_in_columns (output_buf, symbols); |
|
372 delete [] symbols; |
|
373 } |
|
374 |
|
375 static void |
|
376 simple_help (void) |
|
377 { |
|
378 ostrstream output_buf; |
|
379 |
|
380 help_syms_list (output_buf, operator_help (), "operators"); |
|
381 |
|
382 help_syms_list (output_buf, keyword_help (), "reserved words"); |
|
383 |
|
384 help_syms_list (output_buf, builtin_text_functions_help (), |
|
385 "text functions (these names are also reserved)"); |
|
386 |
|
387 help_syms_list (output_buf, builtin_mapper_functions_help (), |
|
388 "mapper functions"); |
|
389 |
|
390 help_syms_list (output_buf, builtin_general_functions_help (), |
|
391 "general functions"); |
|
392 |
|
393 help_syms_list (output_buf, builtin_variables_help (), |
|
394 "builtin variables"); |
|
395 |
|
396 // Also need to list variables and currently compiled functions from |
|
397 // the symbol table, if there are any. |
|
398 |
|
399 // Also need to search octave_path for script files. |
|
400 |
|
401 char **path = pathstring_to_vector (user_pref.loadpath); |
|
402 |
|
403 char **ptr = path; |
|
404 if (ptr != (char **) NULL) |
|
405 { |
|
406 while (*ptr != (char *) NULL) |
|
407 { |
|
408 int count; |
|
409 char **names = get_m_file_names (count, *ptr, 0); |
|
410 output_buf << "\n*** M-files in " |
|
411 << make_absolute (*ptr, the_current_working_directory) |
|
412 << ":\n\n"; |
|
413 if (names != (char **) NULL && count > 0) |
|
414 list_in_columns (output_buf, names); |
|
415 delete [] names; |
|
416 ptr++; |
|
417 } |
|
418 } |
|
419 |
181
|
420 additional_help_message (output_buf); |
169
|
421 output_buf << ends; |
|
422 maybe_page_output (output_buf); |
|
423 } |
|
424 |
|
425 static int |
181
|
426 try_info (const char *string, int force = 0) |
169
|
427 { |
|
428 int status = 0; |
|
429 |
186
|
430 char *directory_name = strsave (user_pref.info_file); |
169
|
431 char *temp = filename_non_directory (directory_name); |
|
432 |
|
433 if (temp != directory_name) |
|
434 { |
|
435 *temp = 0; |
|
436 info_add_path (directory_name, INFOPATH_PREPEND); |
|
437 } |
|
438 |
|
439 delete [] directory_name; |
|
440 |
186
|
441 NODE *initial_node = info_get_node (user_pref.info_file, (char *)NULL); |
169
|
442 |
|
443 if (! initial_node) |
|
444 { |
|
445 warning ("can't find info file!\n"); |
186
|
446 status = -1; |
169
|
447 } |
186
|
448 else |
169
|
449 { |
249
|
450 status = initialize_info_session (initial_node, 0); |
169
|
451 |
249
|
452 if (status == 0 && (force || index_entry_exists (windows, string))) |
186
|
453 { |
|
454 terminal_clear_screen (); |
169
|
455 |
186
|
456 terminal_prep_terminal (); |
169
|
457 |
186
|
458 display_update_display (windows); |
169
|
459 |
186
|
460 info_last_executed_command = (VFunction *)NULL; |
|
461 |
|
462 if (! force) |
|
463 do_info_index_search (windows, 0, string); |
169
|
464 |
186
|
465 char *format = replace_in_documentation |
|
466 ("Type \"\\[quit]\" to quit, \"\\[get-help-window]\" for help."); |
169
|
467 |
186
|
468 window_message_in_echo_area (format); |
169
|
469 |
186
|
470 info_read_and_dispatch (); |
169
|
471 |
186
|
472 terminal_goto_xy (0, screenheight - 1); |
169
|
473 |
186
|
474 terminal_clear_to_eol (); |
169
|
475 |
186
|
476 terminal_unprep_terminal (); |
169
|
477 |
186
|
478 status = 1; |
|
479 } |
|
480 |
|
481 finish_info_session (initial_node, 0); |
169
|
482 } |
|
483 |
|
484 return status; |
|
485 } |
|
486 |
1
|
487 /* |
|
488 * Print cryptic yet witty messages. |
|
489 */ |
|
490 tree_constant |
|
491 builtin_help (int argc, char **argv) |
|
492 { |
|
493 tree_constant retval; |
|
494 |
|
495 if (argc == 1) |
|
496 { |
169
|
497 simple_help (); |
1
|
498 } |
|
499 else |
|
500 { |
181
|
501 if (argv[1] != (char *) NULL && strcmp (argv[1], "-i") == 0) |
1
|
502 { |
181
|
503 argc--; |
|
504 argv++; |
1
|
505 |
181
|
506 if (argc == 1) |
|
507 { |
|
508 volatile sig_handler *old_sigint_handler; |
|
509 old_sigint_handler = signal (SIGINT, SIG_IGN); |
169
|
510 |
181
|
511 try_info ((char *) NULL, 1); |
1
|
512 |
181
|
513 signal (SIGINT, old_sigint_handler); |
|
514 } |
|
515 else |
|
516 { |
|
517 while (--argc > 0) |
|
518 { |
|
519 argv++; |
169
|
520 |
181
|
521 if (*argv == (char *) NULL || **argv == '\0') |
|
522 continue; |
169
|
523 |
181
|
524 volatile sig_handler *old_sigint_handler; |
|
525 old_sigint_handler = signal (SIGINT, SIG_IGN); |
1
|
526 |
181
|
527 if (! try_info (*argv)) |
|
528 { |
|
529 message ("help", |
|
530 "sorry, `%s' is not indexed in the manual", |
|
531 *argv); |
|
532 sleep (2); |
|
533 } |
|
534 |
|
535 signal (SIGINT, old_sigint_handler); |
1
|
536 } |
|
537 } |
181
|
538 } |
|
539 else |
|
540 { |
|
541 ostrstream output_buf; |
1
|
542 |
181
|
543 char *m_file_name = (char *) NULL; |
|
544 symbol_record *sym_rec; |
|
545 help_list *op_help_list = operator_help (); |
|
546 help_list *kw_help_list = keyword_help (); |
|
547 |
|
548 while (--argc > 0) |
1
|
549 { |
181
|
550 argv++; |
|
551 |
|
552 if (*argv == (char *) NULL || **argv == '\0') |
|
553 continue; |
|
554 |
|
555 if (help_from_list (output_buf, op_help_list, *argv, 0)) |
|
556 continue; |
|
557 |
|
558 if (help_from_list (output_buf, kw_help_list, *argv, 0)) |
|
559 continue; |
|
560 |
|
561 sym_rec = curr_sym_tab->lookup (*argv, 0, 0); |
|
562 if (sym_rec != (symbol_record *) NULL) |
1
|
563 { |
181
|
564 char *h = sym_rec->help (); |
|
565 if (h != (char *) NULL && *h != '\0') |
|
566 { |
|
567 output_buf << "\n*** " << *argv << ":\n\n" |
|
568 << h << "\n"; |
|
569 continue; |
|
570 } |
1
|
571 } |
181
|
572 |
|
573 sym_rec = global_sym_tab->lookup (*argv, 0, 0); |
|
574 if (sym_rec != (symbol_record *) NULL |
|
575 && ! symbol_out_of_date (sym_rec)) |
|
576 { |
|
577 char *h = sym_rec->help (); |
|
578 if (h != (char *) NULL && *h != '\0') |
|
579 { |
|
580 output_buf << "\n*** " << *argv << ":\n\n" |
|
581 << h << "\n"; |
|
582 continue; |
|
583 } |
|
584 } |
1
|
585 |
|
586 // Try harder to find M-files that might not be defined yet, or that |
|
587 // appear to be out of date. Don\'t execute commands from the file if |
|
588 // it turns out to be a script file. |
|
589 |
181
|
590 m_file_name = m_file_in_path (*argv); |
|
591 if (m_file_name != (char *) NULL) |
1
|
592 { |
181
|
593 sym_rec = global_sym_tab->lookup (*argv, 1, 0); |
|
594 if (sym_rec != (symbol_record *) NULL) |
110
|
595 { |
181
|
596 tree_identifier tmp (sym_rec); |
|
597 tmp.parse_m_file (0); |
|
598 char *h = sym_rec->help (); |
|
599 if (h != (char *) NULL && *h != '\0') |
|
600 { |
|
601 output_buf << "\n*** " << *argv << ":\n\n" |
|
602 << h << "\n"; |
|
603 continue; |
|
604 } |
110
|
605 } |
1
|
606 } |
181
|
607 delete [] m_file_name; |
1
|
608 |
181
|
609 output_buf << "\nhelp: sorry, `" << *argv |
|
610 << "' is not documented\n"; |
|
611 } |
1
|
612 |
181
|
613 additional_help_message (output_buf); |
|
614 output_buf << ends; |
|
615 maybe_page_output (output_buf); |
|
616 } |
1
|
617 } |
|
618 |
|
619 return retval; |
|
620 } |
|
621 |
|
622 /* |
|
623 * Display, save, or load history. |
|
624 */ |
|
625 tree_constant |
|
626 builtin_history (int argc, char **argv) |
|
627 { |
|
628 tree_constant retval; |
|
629 |
|
630 do_history (argc, argv); |
|
631 |
|
632 return retval; |
|
633 } |
|
634 |
|
635 static int |
|
636 load_variable (char *nm, int force, istream& is) |
|
637 { |
|
638 symbol_record *gsr = global_sym_tab->lookup (nm, 0, 0); |
|
639 symbol_record *lsr = curr_sym_tab->lookup (nm, 0, 0); |
|
640 |
|
641 if (! force |
|
642 && ((gsr != (symbol_record *) NULL && gsr->is_variable ()) |
|
643 || lsr != (symbol_record *) NULL)) |
|
644 { |
214
|
645 warning ("load: variable name `%s' exists.", nm); |
|
646 warning ("Use `load -force' to overwrite"); |
1
|
647 return -1; |
|
648 } |
|
649 |
|
650 // We found it. Read data for this entry, and if that succeeds, |
|
651 // insert it into symbol table. |
|
652 |
|
653 tree_constant tc; |
|
654 int global = tc.load (is); |
|
655 if (tc.const_type () != tree_constant_rep::unknown_constant) |
|
656 { |
|
657 symbol_record *sr; |
|
658 if (global) |
|
659 { |
|
660 if (lsr != (symbol_record *) NULL) |
|
661 { |
214
|
662 warning ("load: replacing local symbol `%s' with", nm); |
|
663 warning ("global value from file"); |
1
|
664 curr_sym_tab->clear (nm); |
|
665 } |
|
666 sr = global_sym_tab->lookup (nm, 1, 0); |
|
667 } |
|
668 else |
|
669 { |
|
670 if (gsr != (symbol_record *) NULL) |
|
671 { |
|
672 warning ("loading `%s' as a global variable", nm); |
|
673 sr = gsr; |
|
674 } |
|
675 else |
|
676 sr = curr_sym_tab->lookup (nm, 1, 0); |
|
677 } |
|
678 |
|
679 if (sr != (symbol_record *) NULL) |
|
680 { |
|
681 tree_constant *tmp_tc = new tree_constant (tc); |
|
682 sr->define (tmp_tc); |
|
683 return 1; |
|
684 } |
|
685 else |
|
686 error ("load: unable to load variable `%s'", nm); |
|
687 } |
|
688 |
|
689 return 0; |
|
690 } |
|
691 |
|
692 /* |
|
693 * Read variables from an input stream. |
|
694 * |
|
695 * BUGS: |
|
696 * |
|
697 * -- This function is not terribly robust. |
|
698 */ |
|
699 tree_constant |
|
700 builtin_load (int argc, char **argv) |
|
701 { |
|
702 tree_constant retval; |
|
703 |
|
704 argc--; |
|
705 argv++; |
|
706 |
|
707 int force = 0; |
|
708 if (argc > 0 && strcmp (*argv, "-force") == 0) |
|
709 { |
|
710 force++; |
|
711 argc--; |
|
712 argv++; |
|
713 } |
|
714 |
|
715 if (argc < 1) |
|
716 { |
181
|
717 error ("load: you must specify a single file to read"); |
1
|
718 return retval; |
|
719 } |
|
720 |
|
721 static istream stream; |
|
722 static ifstream file; |
|
723 if (strcmp (*argv, "-") == 0) |
|
724 { |
|
725 stream = cin; |
|
726 } |
|
727 else |
|
728 { |
|
729 char *fname = tilde_expand (*argv); |
|
730 file.open (fname); |
|
731 if (! file) |
|
732 { |
|
733 error ("load: couldn't open input file `%s'", *argv); |
|
734 return retval; |
|
735 } |
|
736 stream = file; |
|
737 } |
|
738 |
|
739 char nm [128]; // XXX FIXME XXX |
|
740 int count = 0; |
|
741 for (;;) |
|
742 { |
|
743 // Read name for this entry or break on EOF. |
|
744 if (extract_keyword (stream, "name", nm) == 0 || nm == (char *) NULL) |
|
745 { |
|
746 if (count == 0) |
214
|
747 { |
|
748 error ("load: no name keywords found in file `%s'", *argv); |
|
749 error ("Are you sure this is an octave data file?"); |
|
750 } |
1
|
751 break; |
|
752 } |
|
753 |
|
754 if (*nm == '\0') |
|
755 continue; |
|
756 |
|
757 if (! valid_identifier (nm)) |
|
758 { |
181
|
759 warning ("load: skipping bogus identifier `%s'"); |
1
|
760 continue; |
|
761 } |
|
762 |
|
763 if (load_variable (nm, force, stream)) |
|
764 count++; |
|
765 } |
|
766 |
|
767 if (file); |
|
768 file.close (); |
|
769 |
|
770 return retval; |
|
771 } |
|
772 |
|
773 /* |
|
774 * Get a directory listing. |
|
775 */ |
|
776 tree_constant |
|
777 builtin_ls (int argc, char **argv) |
|
778 { |
|
779 tree_constant retval; |
|
780 |
|
781 ostrstream ls_buf; |
|
782 |
|
783 ls_buf << "ls -C "; |
|
784 for (int i = 1; i < argc; i++) |
|
785 ls_buf << tilde_expand (argv[i]) << " "; |
|
786 |
|
787 ls_buf << ends; |
|
788 |
|
789 char *ls_command = ls_buf.str (); |
|
790 |
|
791 iprocstream cmd (ls_command); |
|
792 |
|
793 char ch; |
|
794 ostrstream output_buf; |
|
795 while (cmd.get (ch)) |
|
796 output_buf.put (ch); |
|
797 |
|
798 output_buf << ends; |
|
799 |
|
800 maybe_page_output (output_buf); |
|
801 |
|
802 delete [] ls_command; |
|
803 |
|
804 return retval; |
|
805 } |
|
806 |
|
807 /* |
62
|
808 * Run previous commands from the history list. |
|
809 */ |
|
810 tree_constant |
|
811 builtin_run_history (int argc, char **argv) |
|
812 { |
|
813 tree_constant retval; |
|
814 do_run_history (argc, argv); |
|
815 return retval; |
|
816 } |
|
817 |
|
818 /* |
266
|
819 * Return nonzero if PATTERN has any special globbing chars in it. |
|
820 */ |
|
821 static int |
|
822 glob_pattern_p (char *pattern) |
|
823 { |
|
824 char *p = pattern; |
|
825 char c; |
|
826 int open = 0; |
|
827 |
|
828 while ((c = *p++) != '\0') |
|
829 { |
|
830 switch (c) |
|
831 { |
|
832 case '?': |
|
833 case '*': |
|
834 return 1; |
|
835 |
|
836 case '[': // Only accept an open brace if there is a close |
|
837 open++; // brace to match it. Bracket expressions must be |
|
838 continue; // complete, according to Posix.2 |
|
839 |
|
840 case ']': |
|
841 if (open) |
|
842 return 1; |
|
843 continue; |
|
844 |
|
845 case '\\': |
|
846 if (*p++ == '\0') |
|
847 return 0; |
|
848 |
|
849 default: |
|
850 continue; |
|
851 } |
|
852 } |
|
853 |
|
854 return 0; |
|
855 } |
|
856 |
|
857 /* |
1
|
858 * Write variables to an output stream. |
|
859 */ |
|
860 tree_constant |
|
861 builtin_save (int argc, char **argv) |
|
862 { |
|
863 tree_constant retval; |
|
864 |
|
865 if (argc < 2) |
|
866 { |
181
|
867 print_usage ("save"); |
1
|
868 return retval; |
|
869 } |
|
870 |
|
871 argc--; |
|
872 argv++; |
|
873 |
|
874 static ostream stream; |
|
875 static ofstream file; |
|
876 if (strcmp (*argv, "-") == 0) |
|
877 { |
|
878 // XXX FIXME XXX -- things intended for the screen should end up in a |
|
879 // tree_constant (string)? |
|
880 stream = cout; |
|
881 } |
266
|
882 else if (argc == 1 && glob_pattern_p (*argv)) // Guard against things |
|
883 { // like `save a*', |
|
884 print_usage ("save"); // which are probably |
|
885 return retval; // mistakes... |
|
886 } |
1
|
887 else |
|
888 { |
|
889 char *fname = tilde_expand (*argv); |
|
890 file.open (fname); |
|
891 if (! file) |
|
892 { |
|
893 error ("save: couldn't open output file `%s'", *argv); |
|
894 return retval; |
|
895 } |
|
896 stream = file; |
276
|
897 |
1
|
898 } |
|
899 |
276
|
900 int prec = user_pref.save_precision; |
|
901 |
1
|
902 if (argc == 1) |
|
903 { |
195
|
904 int count; |
|
905 char **vars = curr_sym_tab->list (count, 0, |
|
906 symbol_def::USER_VARIABLE, |
|
907 SYMTAB_ALL_SCOPES); |
|
908 |
|
909 for (int i = 0; i < count; i++) |
|
910 curr_sym_tab->save (stream, vars[i], |
276
|
911 is_globally_visible (vars[i]), prec); |
195
|
912 |
|
913 delete [] vars; |
1
|
914 } |
|
915 else |
|
916 { |
|
917 while (--argc > 0) |
|
918 { |
|
919 argv++; |
195
|
920 |
|
921 int count; |
|
922 char **lvars = curr_sym_tab->list (count, 0, |
|
923 symbol_def::USER_VARIABLE); |
|
924 |
|
925 int saved_or_error = 0; |
|
926 int i; |
|
927 for (i = 0; i < count; i++) |
|
928 { |
266
|
929 if (fnmatch (*argv, lvars[i], __FNM_FLAGS) == 0 |
276
|
930 && curr_sym_tab->save (stream, lvars[i], 0, prec) != 0) |
195
|
931 saved_or_error++; |
|
932 } |
|
933 |
|
934 char **bvars = global_sym_tab->list (count, 0, |
|
935 symbol_def::BUILTIN_VARIABLE); |
|
936 |
|
937 for (i = 0; i < count; i++) |
|
938 { |
266
|
939 if (fnmatch (*argv, bvars[i], __FNM_FLAGS) == 0 |
276
|
940 && global_sym_tab->save (stream, bvars[i], 0, prec) != 0) |
195
|
941 saved_or_error++; |
|
942 } |
|
943 |
|
944 delete [] lvars; |
|
945 delete [] bvars; |
|
946 |
|
947 if (! saved_or_error) |
|
948 warning ("save: no such variable `%s'", *argv); |
1
|
949 } |
|
950 } |
|
951 |
|
952 if (file); |
|
953 file.close (); |
|
954 |
|
955 return retval; |
|
956 } |
|
957 |
|
958 /* |
|
959 * Set plotting options. |
|
960 */ |
|
961 tree_constant |
|
962 builtin_set (int argc, char **argv) |
|
963 { |
|
964 tree_constant retval; |
|
965 |
|
966 ostrstream plot_buf; |
|
967 |
|
968 if (argc > 1) |
|
969 { |
|
970 if (almost_match ("parametric", argv[1], 3)) |
|
971 parametric_plot = 1; |
|
972 else if (almost_match ("noparametric", argv[1], 5)) |
|
973 parametric_plot = 0; |
|
974 } |
|
975 |
|
976 for (int i = 0; i < argc; i++) |
|
977 plot_buf << argv[i] << " "; |
|
978 |
|
979 plot_buf << "\n" << ends; |
|
980 |
|
981 char *plot_command = plot_buf.str (); |
|
982 send_to_plot_stream (plot_command); |
|
983 |
|
984 delete [] plot_command; |
|
985 |
|
986 return retval; |
|
987 } |
|
988 |
|
989 /* |
|
990 * Set plotting options. |
|
991 */ |
|
992 tree_constant |
|
993 builtin_show (int argc, char **argv) |
|
994 { |
|
995 tree_constant retval; |
|
996 |
|
997 ostrstream plot_buf; |
|
998 |
|
999 for (int i = 0; i < argc; i++) |
|
1000 plot_buf << argv[i] << " "; |
|
1001 |
|
1002 plot_buf << "\n" << ends; |
|
1003 |
|
1004 char *plot_command = plot_buf.str (); |
|
1005 send_to_plot_stream (plot_command); |
|
1006 |
|
1007 delete [] plot_command; |
|
1008 |
|
1009 return retval; |
|
1010 } |
|
1011 |
|
1012 /* |
|
1013 * List variable names. |
|
1014 */ |
195
|
1015 static void |
|
1016 print_symbol_info_line (ostrstream& output_buf, const symbol_record_info& s) |
|
1017 { |
|
1018 output_buf << (s.is_read_only () ? " -" : " w"); |
|
1019 output_buf << (s.is_eternal () ? "- " : "d "); |
|
1020 #if 0 |
|
1021 output_buf << (s.hides_fcn () ? "f" : (s.hides_builtin () ? "F" : "-")); |
|
1022 #endif |
|
1023 output_buf.form (" %-16s", s.type_as_string ()); |
|
1024 if (s.is_function ()) |
|
1025 output_buf << " - -"; |
|
1026 else |
|
1027 { |
|
1028 output_buf.form ("%7d", s.rows ()); |
|
1029 output_buf.form ("%7d", s.columns ()); |
|
1030 } |
|
1031 output_buf << " " << s.name () << "\n"; |
|
1032 } |
|
1033 |
|
1034 static void |
|
1035 print_long_listing (ostrstream& output_buf, symbol_record_info *s) |
|
1036 { |
|
1037 if (s == (symbol_record_info *) NULL) |
|
1038 return; |
|
1039 |
|
1040 symbol_record_info *ptr = s; |
|
1041 while (ptr->is_defined ()) |
|
1042 { |
|
1043 print_symbol_info_line (output_buf, *ptr); |
|
1044 ptr++; |
|
1045 } |
|
1046 } |
|
1047 |
|
1048 static int |
|
1049 maybe_list (const char *header, ostrstream& output_buf, |
|
1050 int show_verbose, symbol_table *sym_tab, unsigned type, |
|
1051 unsigned scope) |
|
1052 { |
|
1053 int count; |
|
1054 int status = 0; |
|
1055 if (show_verbose) |
|
1056 { |
|
1057 symbol_record_info *symbols; |
|
1058 symbols = sym_tab->long_list (count, 1, type, scope); |
|
1059 if (symbols != (symbol_record_info *) NULL && count > 0) |
|
1060 { |
|
1061 output_buf << "\n" << header << "\n\n" |
|
1062 << "prot type rows cols name\n" |
|
1063 << "==== ==== ==== ==== ====\n"; |
|
1064 |
|
1065 print_long_listing (output_buf, symbols); |
|
1066 status = 1; |
|
1067 } |
|
1068 delete [] symbols; |
|
1069 } |
|
1070 else |
|
1071 { |
|
1072 char **symbols = sym_tab->list (count, 1, type, scope); |
|
1073 if (symbols != (char **) NULL && count > 0) |
|
1074 { |
|
1075 output_buf << "\n" << header << "\n\n"; |
|
1076 list_in_columns (output_buf, symbols); |
|
1077 status = 1; |
|
1078 } |
|
1079 delete [] symbols; |
|
1080 } |
|
1081 return status; |
|
1082 } |
|
1083 |
1
|
1084 tree_constant |
|
1085 builtin_who (int argc, char **argv) |
|
1086 { |
|
1087 tree_constant retval; |
195
|
1088 |
|
1089 int show_builtins = 0; |
|
1090 int show_functions = (curr_sym_tab == top_level_sym_tab); |
|
1091 int show_variables = 1; |
|
1092 int show_verbose = 0; |
1
|
1093 |
|
1094 if (argc > 1) |
195
|
1095 { |
|
1096 show_functions = 0; |
|
1097 show_variables = 0; |
|
1098 } |
1
|
1099 |
|
1100 for (int i = 1; i < argc; i++) |
|
1101 { |
|
1102 argv++; |
195
|
1103 if (strcmp (*argv, "-all") == 0 || strcmp (*argv, "-a") == 0) |
1
|
1104 { |
195
|
1105 show_builtins++; |
|
1106 show_functions++; |
|
1107 show_variables++; |
1
|
1108 } |
195
|
1109 else if (strcmp (*argv, "-builtins") == 0 |
|
1110 || strcmp (*argv, "-b") == 0) |
|
1111 show_builtins++; |
|
1112 else if (strcmp (*argv, "-functions") == 0 |
|
1113 || strcmp (*argv, "-f") == 0) |
|
1114 show_functions++; |
|
1115 else if (strcmp (*argv, "-long") == 0 |
|
1116 || strcmp (*argv, "-l") == 0) |
|
1117 show_verbose++; |
|
1118 else if (strcmp (*argv, "-variables") == 0 |
|
1119 || strcmp (*argv, "-v") == 0) |
|
1120 show_variables++; |
1
|
1121 else |
195
|
1122 warning ("who: unrecognized option `%s'", *argv); |
|
1123 } |
|
1124 |
|
1125 // If the user specified -l and nothing else, show variables. If |
|
1126 // evaluating this at the top level, also show functions. |
|
1127 |
|
1128 if (show_verbose && ! (show_builtins || show_functions || show_variables)) |
|
1129 { |
|
1130 show_functions = (curr_sym_tab == top_level_sym_tab); |
|
1131 show_variables = 1; |
1
|
1132 } |
|
1133 |
|
1134 ostrstream output_buf; |
|
1135 int pad_after = 0; |
195
|
1136 |
|
1137 if (show_builtins) |
1
|
1138 { |
195
|
1139 pad_after += maybe_list ("*** built-in variables:", |
|
1140 output_buf, show_verbose, global_sym_tab, |
|
1141 symbol_def::BUILTIN_VARIABLE, |
|
1142 SYMTAB_ALL_SCOPES); |
1
|
1143 |
195
|
1144 pad_after += maybe_list ("*** built-in functions:", |
|
1145 output_buf, show_verbose, global_sym_tab, |
|
1146 symbol_def::BUILTIN_FUNCTION, |
|
1147 SYMTAB_ALL_SCOPES); |
1
|
1148 } |
|
1149 |
195
|
1150 if (show_functions) |
1
|
1151 { |
195
|
1152 pad_after += maybe_list ("*** currently compiled functions:", |
|
1153 output_buf, show_verbose, global_sym_tab, |
|
1154 symbol_def::USER_FUNCTION, |
|
1155 SYMTAB_ALL_SCOPES); |
1
|
1156 } |
|
1157 |
195
|
1158 if (show_variables) |
1
|
1159 { |
195
|
1160 pad_after += maybe_list ("*** local user variables:", |
|
1161 output_buf, show_verbose, curr_sym_tab, |
|
1162 symbol_def::USER_VARIABLE, |
|
1163 SYMTAB_LOCAL_SCOPE); |
|
1164 |
|
1165 pad_after += maybe_list ("*** globally visible user variables:", |
|
1166 output_buf, show_verbose, curr_sym_tab, |
|
1167 symbol_def::USER_VARIABLE, |
|
1168 SYMTAB_GLOBAL_SCOPE); |
1
|
1169 } |
|
1170 |
|
1171 if (pad_after) |
|
1172 output_buf << "\n"; |
|
1173 |
|
1174 output_buf << ends; |
|
1175 maybe_page_output (output_buf); |
|
1176 |
|
1177 return retval; |
|
1178 } |
|
1179 |
|
1180 /* |
|
1181 ;;; Local Variables: *** |
|
1182 ;;; mode: C++ *** |
|
1183 ;;; page-delimiter: "^/\\*" *** |
|
1184 ;;; End: *** |
|
1185 */ |