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