1
|
1 // help.cc -*- C++ -*- |
|
2 /* |
|
3 |
1009
|
4 Copyright (C) 1992, 1993, 1994, 1995 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 |
240
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
1
|
26 #endif |
|
27 |
529
|
28 #include <signal.h> |
|
29 #include <stdlib.h> |
1
|
30 #include <iostream.h> |
529
|
31 #include <strstream.h> |
240
|
32 |
581
|
33 #include "tree-expr.h" |
|
34 #include "tree-const.h" |
529
|
35 #include "sighandlers.h" |
|
36 #include "user-prefs.h" |
|
37 #include "tree-expr.h" |
|
38 #include "variables.h" |
|
39 #include "oct-obj.h" |
|
40 #include "symtab.h" |
|
41 #include "octave.h" |
|
42 #include "dirfns.h" |
|
43 #include "pager.h" |
|
44 #include "error.h" |
242
|
45 #include "utils.h" |
1
|
46 #include "help.h" |
529
|
47 #include "defun.h" |
|
48 |
|
49 extern "C" |
|
50 { |
|
51 #include "info/info.h" |
|
52 #include "info/dribble.h" |
|
53 #include "info/terminal.h" |
|
54 |
|
55 extern int initialize_info_session (); |
|
56 extern int index_entry_exists (); |
|
57 extern int do_info_index_search (); |
|
58 extern void finish_info_session (); |
|
59 extern char *replace_in_documentation (); |
|
60 |
|
61 // XXX FIXME XXX |
|
62 #undef __FUNCTION_DEF |
|
63 #include <readline/tilde.h> |
679
|
64 |
|
65 #define boolean kpathsea_boolean |
|
66 #define false kpathsea_false |
|
67 #define true kpathsea_true |
|
68 #include <kpathsea/pathsearch.h> |
529
|
69 } |
1
|
70 |
|
71 static help_list operators[] = |
|
72 { |
|
73 { "!", |
|
74 "Logical not operator. See also `~'.\n", }, |
|
75 |
|
76 { "!=", |
|
77 "Logical not equals operator. See also `~' and `<>'.\n", }, |
|
78 |
|
79 { "\"", |
|
80 "String delimiter.\n", }, |
|
81 |
|
82 { "#", |
928
|
83 "Begin comment character. See also `%'.", }, |
1
|
84 |
|
85 { "%", |
928
|
86 "Begin comment charcter. See also `#'.", }, |
1
|
87 |
|
88 { "&", |
928
|
89 "Logical and operator. See also `&&'.", }, |
1
|
90 |
|
91 { "&&", |
928
|
92 "Logical and operator. See also `&'.", }, |
1
|
93 |
|
94 { "'", |
|
95 "Matrix transpose operator. For complex matrices, computes the\n\ |
|
96 complex conjugate (Hermitian) transpose. See also `.''\n\ |
|
97 \n\ |
|
98 The single quote character may also be used to delimit strings, but\n\ |
|
99 it is better to use the double quote character, since that is never\n\ |
928
|
100 ambiguous", }, |
1
|
101 |
|
102 { "(", |
928
|
103 "Array index or function argument delimiter.", }, |
1
|
104 |
|
105 { ")", |
928
|
106 "Array index or function argument delimiter.", }, |
1
|
107 |
|
108 { "*", |
928
|
109 "Multiplication operator. See also `.*'", }, |
1
|
110 |
|
111 { "**", |
928
|
112 "Power operator. See also `^', `.**', and `.^'", }, |
1
|
113 |
|
114 { "+", |
928
|
115 "Addition operator.", }, |
1
|
116 |
|
117 { "++", |
928
|
118 "Increment operator. As in C, may be applied as a prefix or postfix operator.", }, |
1
|
119 |
|
120 { ",", |
928
|
121 "Array index, function argument, or command separator.", }, |
1
|
122 |
|
123 { "-", |
928
|
124 "Subtraction or unary negation operator.", }, |
1
|
125 |
|
126 { "--", |
928
|
127 "Decrement operator. As in C, may be applied as a prefix or postfix operator.", }, |
1
|
128 |
|
129 { ".'", |
|
130 "Matrix transpose operator. For complex matrices, computes the\n\ |
928
|
131 transpose, *not* the complex conjugate transpose. See also `''.", }, |
1
|
132 |
|
133 { ".*", |
928
|
134 "Element by element multiplication operator. See also `*'.", }, |
1
|
135 |
|
136 { ".**", |
928
|
137 "Element by element power operator. See also `**', `^', and `.^'.", }, |
1
|
138 |
|
139 { "./", |
928
|
140 "Element by element division operator. See also `/' and `\\'.", }, |
1
|
141 |
|
142 { ".^", |
928
|
143 "Element by element power operator. See also `**', `^', and `.^'.", }, |
1
|
144 |
|
145 { "/", |
928
|
146 "Right division. See also `\\' and `./'.", }, |
1
|
147 |
|
148 { ":", |
928
|
149 "Select entire rows or columns of matrices.", }, |
1
|
150 |
|
151 { ";", |
928
|
152 "Array row or command separator. See also `,'.", }, |
1
|
153 |
|
154 { "<", |
928
|
155 "Less than operator.", }, |
1
|
156 |
|
157 { "<=", |
928
|
158 "Less than or equals operator.", }, |
1
|
159 |
|
160 { "<>", |
928
|
161 "Logical not equals operator. See also `!=' and `~='.", }, |
1
|
162 |
|
163 { "=", |
928
|
164 "Assignment operator.", }, |
1
|
165 |
|
166 { "==", |
928
|
167 "Equality test operator.", }, |
1
|
168 |
|
169 { ">", |
928
|
170 "Greater than operator.", }, |
1
|
171 |
|
172 { ">=", |
928
|
173 "Greater than or equals operator.", }, |
1
|
174 |
|
175 { "[", |
928
|
176 "Return list delimiter. See also `]'.", }, |
1
|
177 |
|
178 { "\\", |
928
|
179 "Left division operator. See also `/' and `./'.", }, |
1
|
180 |
|
181 { "]", |
928
|
182 "Return list delimiter. See also `['.", }, |
1
|
183 |
|
184 { "^", |
928
|
185 "Power operator. See also `**', `.^', and `.**.'", }, |
1
|
186 |
|
187 { "|", |
928
|
188 "Logical or operator. See also `||'.", }, |
1
|
189 |
|
190 { "||", |
928
|
191 "Logical or operator. See also `|'.", }, |
1
|
192 |
|
193 { "~", |
928
|
194 "Logical not operator. See also `!' and `~'.", }, |
1
|
195 |
|
196 { "~=", |
928
|
197 "Logical not equals operator. See also `<>' and `!='.", }, |
1
|
198 |
529
|
199 { 0, 0, }, |
1
|
200 }; |
|
201 |
|
202 static help_list keywords[] = |
|
203 { |
928
|
204 { "all_va_args", |
|
205 "Pass all unnamed arguments to another function call.", }, |
|
206 |
1
|
207 { "break", |
928
|
208 "Exit the innermost enclosing while or for loop.", }, |
1
|
209 |
|
210 { "continue", |
928
|
211 "Jump to the end of the innermost enclosing while or for loop.", }, |
1
|
212 |
|
213 { "else", |
928
|
214 "Alternate action for an if block.", }, |
1
|
215 |
|
216 { "elseif", |
928
|
217 "Alternate conditional test for an if block.", }, |
1
|
218 |
|
219 { "end", |
928
|
220 "Mark the end of any for, if, while, or function block.", }, |
|
221 |
|
222 { "end_unwind_protect", |
|
223 "Mark the end of an unwind_protect block.", }, |
1
|
224 |
|
225 { "endfor", |
928
|
226 "Mark the end of a for loop.", }, |
1
|
227 |
|
228 { "endfunction", |
928
|
229 "Mark the end of a function.", }, |
1
|
230 |
|
231 { "endif", |
928
|
232 "Mark the end of an if block.", }, |
1
|
233 |
|
234 { "endwhile", |
928
|
235 "Mark the end of a while loop.", }, |
1
|
236 |
|
237 { "for", |
928
|
238 "Begin a for loop.", }, |
1
|
239 |
|
240 { "function", |
928
|
241 "Begin a function body.", }, |
1
|
242 |
|
243 { "global", |
928
|
244 "Declare variables to have global scope.", }, |
1
|
245 |
|
246 { "gplot", |
928
|
247 "Produce 2-D plots using gnuplot-like command syntax.", }, |
1
|
248 |
|
249 { "gsplot", |
928
|
250 "Produce 3-D plots using gnuplot-like command syntax.", }, |
1
|
251 |
|
252 { "if", |
928
|
253 "Begin an if block.", }, |
1
|
254 |
|
255 { "return", |
928
|
256 "Return from a function.", }, |
|
257 |
|
258 { "unwind_protect", |
|
259 "Begin an unwind_protect block.", }, |
|
260 |
|
261 { "unwind_protect_cleanup", |
|
262 "Begin the cleanup section of an unwind_protect block.", }, |
1
|
263 |
|
264 { "while", |
928
|
265 "Begin a while loop.", }, |
1
|
266 |
529
|
267 { 0, 0, }, |
1
|
268 }; |
|
269 |
581
|
270 // Return a copy of the operator or keyword names. |
|
271 |
1
|
272 char ** |
|
273 names (help_list *lst, int& count) |
|
274 { |
|
275 count = 0; |
|
276 help_list *ptr = lst; |
529
|
277 while (ptr->name) |
1
|
278 { |
|
279 count++; |
|
280 ptr++; |
|
281 } |
|
282 |
|
283 if (count == 0) |
529
|
284 return 0; |
1
|
285 |
|
286 char **name_list = new char * [count+1]; |
|
287 |
|
288 ptr = lst; |
|
289 int i = 0; |
529
|
290 while (ptr->name) |
1
|
291 { |
242
|
292 name_list[i++] = strsave (ptr->name); |
1
|
293 ptr++; |
|
294 } |
|
295 |
529
|
296 name_list[i] = 0; |
1
|
297 return name_list; |
|
298 } |
|
299 |
|
300 help_list * |
|
301 operator_help (void) |
|
302 { |
|
303 return operators; |
|
304 } |
|
305 |
|
306 help_list * |
|
307 keyword_help (void) |
|
308 { |
|
309 return keywords; |
|
310 } |
|
311 |
1115
|
312 #define VERBOSE_HELP_MESSAGE \ |
|
313 "\n\ |
|
314 Additional help for builtin functions, operators, and variables\n\ |
|
315 is available in the on-line version of the manual.\n\ |
|
316 \n\ |
|
317 Use the command `help -i <topic>' to search the manual index.\n" |
|
318 |
|
319 static void |
1117
|
320 additional_help_message (ostrstream& output_buf) |
542
|
321 { |
1117
|
322 if (! user_pref.suppress_verbose_help_message) |
1115
|
323 output_buf << VERBOSE_HELP_MESSAGE; |
542
|
324 } |
|
325 |
|
326 void |
|
327 print_usage (const char *string, int just_usage) |
|
328 { |
|
329 ostrstream output_buf; |
|
330 |
|
331 symbol_record *sym_rec = global_sym_tab->lookup (string, 0, 0); |
|
332 if (sym_rec) |
|
333 { |
|
334 char *h = sym_rec->help (); |
|
335 if (h && *h) |
|
336 { |
|
337 output_buf << "\n*** " << string << ":\n\n" |
|
338 << h << "\n"; |
|
339 |
1117
|
340 if (! just_usage) |
|
341 additional_help_message (output_buf) |
542
|
342 output_buf << ends; |
|
343 maybe_page_output (output_buf); |
|
344 } |
|
345 } |
1023
|
346 else |
|
347 warning ("no usage message found for `%s'", string); |
542
|
348 } |
|
349 |
529
|
350 static void |
542
|
351 display_names_from_help_list (ostrstream& output_buf, help_list *list, |
|
352 const char *desc) |
529
|
353 { |
|
354 int count = 0; |
|
355 char **symbols = names (list, count); |
|
356 output_buf << "\n*** " << desc << ":\n\n"; |
|
357 if (symbols && count > 0) |
|
358 list_in_columns (output_buf, symbols); |
|
359 delete [] symbols; |
|
360 } |
|
361 |
742
|
362 static char * |
|
363 print_symbol_type (ostrstream& output_buf, symbol_record *sym_rec, |
|
364 char *name, int print) |
|
365 { |
|
366 char *retval = 0; |
|
367 |
|
368 if (sym_rec->is_user_function ()) |
|
369 { |
|
370 tree_fvc *defn = sym_rec->def (); |
|
371 char *fn = defn->fcn_file_name (); |
|
372 if (fn) |
|
373 { |
|
374 char *ff = fcn_file_in_path (fn); |
|
375 ff = ff ? ff : fn; |
|
376 |
|
377 if (print) |
|
378 output_buf << name |
|
379 << " is the function defined from:\n" |
|
380 << ff << "\n"; |
|
381 else |
|
382 retval = ff; |
|
383 } |
|
384 else |
|
385 { |
|
386 if (print) |
|
387 output_buf << name << " is a user-defined function\n"; |
|
388 else |
|
389 retval = "user-defined function"; |
|
390 } |
|
391 } |
|
392 else if (sym_rec->is_text_function ()) |
|
393 { |
|
394 if (print) |
|
395 output_buf << name << " is a builtin text-function\n"; |
|
396 else |
|
397 retval = "builtin text-function"; |
|
398 } |
|
399 else if (sym_rec->is_builtin_function ()) |
|
400 { |
|
401 if (print) |
|
402 output_buf << name << " is a builtin function\n"; |
|
403 else |
|
404 retval = "builtin function"; |
|
405 } |
|
406 else if (sym_rec->is_user_variable ()) |
|
407 { |
|
408 if (print) |
|
409 output_buf << name << " is a user-defined variable\n"; |
|
410 else |
|
411 retval = "user-defined variable"; |
|
412 } |
|
413 else if (sym_rec->is_builtin_variable ()) |
|
414 { |
|
415 if (print) |
|
416 output_buf << name << " is a builtin variable\n"; |
|
417 else |
|
418 retval = "builtin variable"; |
|
419 } |
|
420 else |
|
421 { |
|
422 if (print) |
|
423 output_buf << "which: `" << name |
|
424 << "' has unknown type\n"; |
|
425 else |
|
426 retval = "unknown type"; |
|
427 } |
|
428 |
|
429 return retval; |
|
430 } |
|
431 |
529
|
432 static void |
542
|
433 display_symtab_names (ostrstream& output_buf, char **names, |
|
434 int count, const char *desc) |
|
435 { |
|
436 output_buf << "\n*** " << desc << ":\n\n"; |
|
437 if (names && count > 0) |
|
438 list_in_columns (output_buf, names); |
|
439 } |
|
440 |
|
441 static void |
529
|
442 simple_help (void) |
|
443 { |
|
444 ostrstream output_buf; |
|
445 |
542
|
446 display_names_from_help_list (output_buf, operator_help (), |
|
447 "operators"); |
529
|
448 |
542
|
449 display_names_from_help_list (output_buf, keyword_help (), |
|
450 "reserved words"); |
529
|
451 |
542
|
452 #ifdef LIST_SYMBOLS |
|
453 #undef LIST_SYMBOLS |
|
454 #endif |
|
455 #define LIST_SYMBOLS(type, msg) \ |
|
456 do \ |
|
457 { \ |
|
458 int count; \ |
867
|
459 char **names = global_sym_tab->list (count, 0, 0, 1, type); \ |
542
|
460 display_symtab_names (output_buf, names, count, msg); \ |
|
461 char **ptr = names; \ |
|
462 while (*ptr) \ |
|
463 delete [] *ptr++; \ |
|
464 delete [] names; \ |
|
465 } \ |
|
466 while (0) |
529
|
467 |
542
|
468 // XXX FIXME XXX -- is this distinction needed? |
|
469 LIST_SYMBOLS (symbol_def::TEXT_FUNCTION, |
|
470 "text functions (these names are also reserved)"); |
529
|
471 |
542
|
472 LIST_SYMBOLS (symbol_def::MAPPER_FUNCTION, "mapper functions"); |
|
473 |
|
474 LIST_SYMBOLS (symbol_def::BUILTIN_FUNCTION, "general functions"); |
|
475 |
|
476 LIST_SYMBOLS (symbol_def::BUILTIN_VARIABLE, "builtin variables"); |
|
477 |
529
|
478 // Also need to list variables and currently compiled functions from |
|
479 // the symbol table, if there are any. |
|
480 |
|
481 // Also need to search octave_path for script files. |
|
482 |
679
|
483 char *path_elt = kpse_path_element (user_pref.loadpath); |
529
|
484 |
679
|
485 while (path_elt) |
529
|
486 { |
679
|
487 str_llist_type *elt_dirs = kpse_element_dirs (path_elt); |
|
488 |
|
489 str_llist_elt_type *dir; |
|
490 for (dir = *elt_dirs; dir; dir = STR_LLIST_NEXT (*dir)) |
529
|
491 { |
679
|
492 char *elt_dir = STR_LLIST (*dir); |
|
493 |
|
494 if (elt_dir) |
|
495 { |
|
496 int count; |
|
497 char **names = get_fcn_file_names (count, elt_dir, 0); |
|
498 |
|
499 output_buf << "\n*** function files in " |
|
500 << make_absolute (elt_dir, the_current_working_directory) |
|
501 << ":\n\n"; |
|
502 |
|
503 if (names && count > 0) |
|
504 list_in_columns (output_buf, names); |
|
505 |
|
506 delete [] names; |
|
507 } |
529
|
508 } |
679
|
509 path_elt = kpse_path_element (0); |
529
|
510 } |
|
511 |
|
512 additional_help_message (output_buf); |
|
513 output_buf << ends; |
|
514 maybe_page_output (output_buf); |
|
515 } |
|
516 |
|
517 static int |
|
518 try_info (const char *string, int force = 0) |
|
519 { |
|
520 int status = 0; |
|
521 |
|
522 char *directory_name = strsave (user_pref.info_file); |
|
523 char *temp = filename_non_directory (directory_name); |
|
524 |
|
525 if (temp != directory_name) |
|
526 { |
|
527 *temp = 0; |
|
528 info_add_path (directory_name, INFOPATH_PREPEND); |
|
529 } |
|
530 |
|
531 delete [] directory_name; |
|
532 |
|
533 NODE *initial_node = info_get_node (user_pref.info_file, 0); |
|
534 |
|
535 if (! initial_node) |
|
536 { |
|
537 warning ("can't find info file!\n"); |
|
538 status = -1; |
|
539 } |
|
540 else |
|
541 { |
|
542 status = initialize_info_session (initial_node, 0); |
|
543 |
|
544 if (status == 0 && (force || index_entry_exists (windows, string))) |
|
545 { |
|
546 terminal_clear_screen (); |
|
547 |
|
548 terminal_prep_terminal (); |
|
549 |
|
550 display_update_display (windows); |
|
551 |
|
552 info_last_executed_command = 0; |
|
553 |
|
554 if (! force) |
|
555 do_info_index_search (windows, 0, string); |
|
556 |
|
557 char *format = replace_in_documentation |
|
558 ("Type \"\\[quit]\" to quit, \"\\[get-help-window]\" for help."); |
|
559 |
|
560 window_message_in_echo_area (format); |
|
561 |
|
562 info_read_and_dispatch (); |
|
563 |
|
564 terminal_goto_xy (0, screenheight - 1); |
|
565 |
|
566 terminal_clear_to_eol (); |
|
567 |
|
568 terminal_unprep_terminal (); |
|
569 |
|
570 status = 1; |
|
571 } |
|
572 |
|
573 finish_info_session (initial_node, 0); |
|
574 } |
|
575 |
|
576 return status; |
|
577 } |
|
578 |
542
|
579 int |
|
580 help_from_list (ostrstream& output_buf, const help_list *list, |
|
581 const char *string, int usage) |
|
582 { |
|
583 char *name; |
|
584 while ((name = list->name) != 0) |
|
585 { |
|
586 if (strcmp (name, string) == 0) |
|
587 { |
|
588 if (usage) |
|
589 output_buf << "\nusage: "; |
|
590 else |
|
591 { |
|
592 output_buf << "\n*** " << string << ":\n\n"; |
|
593 } |
|
594 |
|
595 output_buf << list->help << "\n"; |
|
596 |
|
597 return 1; |
|
598 } |
|
599 list++; |
|
600 } |
|
601 return 0; |
|
602 } |
|
603 |
529
|
604 DEFUN_TEXT ("help", Fhelp, Shelp, -1, 1, |
|
605 "help [-i] [topic ...]\n\ |
|
606 \n\ |
|
607 print cryptic yet witty messages") |
|
608 { |
|
609 Octave_object retval; |
|
610 |
|
611 DEFINE_ARGV("help"); |
|
612 |
|
613 if (argc == 1) |
|
614 { |
|
615 simple_help (); |
|
616 } |
|
617 else |
|
618 { |
|
619 if (argv[1] && strcmp (argv[1], "-i") == 0) |
|
620 { |
|
621 argc--; |
|
622 argv++; |
|
623 |
|
624 if (argc == 1) |
|
625 { |
|
626 volatile sig_handler *old_sigint_handler; |
654
|
627 old_sigint_handler = signal (SIGINT, SIG_IGN); |
529
|
628 |
|
629 try_info (0, 1); |
|
630 |
|
631 signal (SIGINT, old_sigint_handler); |
|
632 } |
|
633 else |
|
634 { |
|
635 while (--argc > 0) |
|
636 { |
|
637 argv++; |
|
638 |
|
639 if (! *argv || ! **argv) |
|
640 continue; |
|
641 |
|
642 volatile sig_handler *old_sigint_handler; |
|
643 old_sigint_handler = signal (SIGINT, SIG_IGN); |
|
644 |
|
645 if (! try_info (*argv)) |
|
646 { |
|
647 message ("help", |
|
648 "sorry, `%s' is not indexed in the manual", |
|
649 *argv); |
|
650 sleep (2); |
|
651 } |
|
652 |
|
653 signal (SIGINT, old_sigint_handler); |
|
654 } |
|
655 } |
|
656 } |
|
657 else |
|
658 { |
|
659 ostrstream output_buf; |
|
660 |
|
661 help_list *op_help_list = operator_help (); |
|
662 help_list *kw_help_list = keyword_help (); |
|
663 |
|
664 while (--argc > 0) |
|
665 { |
|
666 argv++; |
|
667 |
|
668 if (! *argv || ! **argv) |
|
669 continue; |
|
670 |
|
671 if (help_from_list (output_buf, op_help_list, *argv, 0)) |
|
672 continue; |
|
673 |
|
674 if (help_from_list (output_buf, kw_help_list, *argv, 0)) |
|
675 continue; |
|
676 |
962
|
677 symbol_record *sym_rec = lookup_by_name (*argv, 0); |
581
|
678 |
991
|
679 if (sym_rec && sym_rec->is_defined ()) |
529
|
680 { |
|
681 char *h = sym_rec->help (); |
|
682 if (h && *h) |
|
683 { |
742
|
684 print_symbol_type (output_buf, sym_rec, *argv, 1); |
|
685 output_buf << "\n" << h << "\n"; |
529
|
686 continue; |
|
687 } |
|
688 } |
|
689 |
993
|
690 char *path = fcn_file_in_path (*argv); |
|
691 char *h = get_help_from_file (path); |
991
|
692 if (h && *h) |
|
693 { |
993
|
694 output_buf << *argv << " is the file:\n" |
|
695 << path << "\n\n" << h << "\n"; |
991
|
696 delete [] h; |
993
|
697 delete [] path; |
991
|
698 continue; |
|
699 } |
993
|
700 delete [] path; |
991
|
701 |
529
|
702 output_buf << "\nhelp: sorry, `" << *argv |
581
|
703 << "' is not documented\n"; |
529
|
704 } |
|
705 |
|
706 additional_help_message (output_buf); |
|
707 output_buf << ends; |
|
708 maybe_page_output (output_buf); |
|
709 } |
|
710 } |
|
711 |
|
712 DELETE_ARGV; |
|
713 |
|
714 return retval; |
|
715 } |
|
716 |
581
|
717 DEFUN_TEXT ("type", Ftype, Stype, -1, 1, |
|
718 "type NAME ...]\n\ |
|
719 \n\ |
|
720 display the definition of each NAME that refers to a function") |
|
721 { |
|
722 Octave_object retval; |
|
723 |
|
724 DEFINE_ARGV("type"); |
|
725 |
|
726 if (argc > 1) |
|
727 { |
|
728 // XXX FIXME XXX -- we should really use getopt () |
|
729 int quiet = 0; |
|
730 if (argv[1] && strcmp (argv[1], "-q") == 0) |
|
731 { |
|
732 quiet = 1; |
|
733 argc--; |
|
734 argv++; |
|
735 } |
|
736 |
|
737 ostrstream output_buf; |
|
738 |
|
739 while (--argc > 0) |
|
740 { |
|
741 argv++; |
|
742 |
|
743 if (! *argv || ! **argv) |
|
744 continue; |
|
745 |
962
|
746 symbol_record *sym_rec = lookup_by_name (*argv, 0); |
581
|
747 |
|
748 if (sym_rec) |
|
749 { |
|
750 if (sym_rec->is_user_function ()) |
|
751 { |
|
752 tree_fvc *defn = sym_rec->def (); |
|
753 |
|
754 if (nargout == 0 && ! quiet) |
|
755 output_buf << *argv << " is a user-defined function\n"; |
|
756 |
|
757 defn->print_code (output_buf); |
|
758 } |
|
759 |
|
760 // XXX FIXME XXX -- this code should be shared with Fwhich |
|
761 |
|
762 else if (sym_rec->is_text_function ()) |
|
763 output_buf << *argv << " is a builtin text-function\n"; |
|
764 else if (sym_rec->is_builtin_function ()) |
|
765 output_buf << *argv << " is a builtin function\n"; |
|
766 else if (sym_rec->is_user_variable ()) |
625
|
767 { |
|
768 tree_fvc *defn = sym_rec->def (); |
|
769 |
|
770 if (nargout == 0 && ! quiet) |
|
771 output_buf << *argv << " is a user-defined variable\n"; |
|
772 |
|
773 defn->print_code (output_buf); |
|
774 |
|
775 if (nargout == 0) |
|
776 output_buf << "\n"; |
|
777 } |
581
|
778 else if (sym_rec->is_builtin_variable ()) |
625
|
779 { |
|
780 tree_fvc *defn = sym_rec->def (); |
|
781 |
|
782 if (nargout == 0 && ! quiet) |
|
783 output_buf << *argv << " is a builtin variable\n"; |
|
784 |
|
785 defn->print_code (output_buf); |
|
786 |
|
787 if (nargout == 0) |
|
788 output_buf << "\n"; |
|
789 } |
581
|
790 else |
|
791 output_buf << "type: `" << *argv << "' has unknown type!\n"; |
|
792 } |
|
793 else |
|
794 output_buf << "type: `" << *argv << "' undefined\n"; |
|
795 } |
|
796 |
|
797 output_buf << ends; |
|
798 |
|
799 if (nargout == 0) |
|
800 maybe_page_output (output_buf); |
|
801 else |
|
802 { |
|
803 char *s = output_buf.str (); |
|
804 retval = s; |
|
805 delete s; |
|
806 } |
|
807 } |
|
808 else |
|
809 print_usage ("type"); |
|
810 |
|
811 DELETE_ARGV; |
|
812 |
|
813 return retval; |
|
814 } |
|
815 |
|
816 DEFUN_TEXT ("which", Fwhich, Swhich, -1, 1, |
|
817 "which NAME ...]\n\ |
|
818 \n\ |
|
819 display the type of each NAME. If NAME is defined from an function\n\ |
|
820 file, print the full name of the file.") |
|
821 { |
|
822 Octave_object retval; |
|
823 |
|
824 DEFINE_ARGV("which"); |
|
825 |
|
826 if (argc > 1) |
|
827 { |
|
828 if (nargout > 0) |
|
829 retval.resize (argc-1, Matrix ()); |
|
830 |
|
831 ostrstream output_buf; |
|
832 |
|
833 for (int i = 0; i < argc-1; i++) |
|
834 { |
|
835 argv++; |
|
836 |
|
837 if (! *argv || ! **argv) |
|
838 continue; |
|
839 |
962
|
840 symbol_record *sym_rec = lookup_by_name (*argv, 0); |
581
|
841 |
|
842 if (sym_rec) |
|
843 { |
742
|
844 int print = (nargout == 0); |
|
845 char *tmp = print_symbol_type (output_buf, sym_rec, |
|
846 *argv, print); |
|
847 if (! print) |
|
848 retval(i) = tmp; |
581
|
849 } |
|
850 else |
|
851 { |
|
852 if (nargout == 0) |
|
853 output_buf << "which: `" << *argv << "' is undefined\n"; |
|
854 else |
|
855 retval(i) = "undefined"; |
|
856 } |
|
857 } |
|
858 output_buf << ends; |
|
859 maybe_page_output (output_buf); |
|
860 } |
|
861 else |
|
862 print_usage ("which"); |
|
863 |
|
864 DELETE_ARGV; |
|
865 |
|
866 return retval; |
|
867 } |
|
868 |
1
|
869 /* |
|
870 ;;; Local Variables: *** |
|
871 ;;; mode: C++ *** |
|
872 ;;; page-delimiter: "^/\\*" *** |
|
873 ;;; End: *** |
|
874 */ |