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 |
542
|
312 void |
|
313 additional_help_message (ostrstream& output_buf) |
|
314 { |
|
315 output_buf |
|
316 << "\n" |
|
317 << "Additional help for builtin functions, operators, and variables\n" |
|
318 << "is available in the on-line version of the manual.\n" |
|
319 << "\n" |
|
320 << "Use the command `help -i <topic>' to search the manual index.\n"; |
|
321 } |
|
322 |
|
323 void |
|
324 print_usage (const char *string, int just_usage) |
|
325 { |
|
326 ostrstream output_buf; |
|
327 |
|
328 symbol_record *sym_rec = global_sym_tab->lookup (string, 0, 0); |
|
329 if (sym_rec) |
|
330 { |
|
331 char *h = sym_rec->help (); |
|
332 if (h && *h) |
|
333 { |
|
334 output_buf << "\n*** " << string << ":\n\n" |
|
335 << h << "\n"; |
|
336 |
|
337 if (! just_usage) |
|
338 additional_help_message (output_buf); |
|
339 output_buf << ends; |
|
340 maybe_page_output (output_buf); |
|
341 } |
|
342 } |
1023
|
343 else |
|
344 warning ("no usage message found for `%s'", string); |
542
|
345 } |
|
346 |
529
|
347 static void |
542
|
348 display_names_from_help_list (ostrstream& output_buf, help_list *list, |
|
349 const char *desc) |
529
|
350 { |
|
351 int count = 0; |
|
352 char **symbols = names (list, count); |
|
353 output_buf << "\n*** " << desc << ":\n\n"; |
|
354 if (symbols && count > 0) |
|
355 list_in_columns (output_buf, symbols); |
|
356 delete [] symbols; |
|
357 } |
|
358 |
742
|
359 static char * |
|
360 print_symbol_type (ostrstream& output_buf, symbol_record *sym_rec, |
|
361 char *name, int print) |
|
362 { |
|
363 char *retval = 0; |
|
364 |
|
365 if (sym_rec->is_user_function ()) |
|
366 { |
|
367 tree_fvc *defn = sym_rec->def (); |
|
368 char *fn = defn->fcn_file_name (); |
|
369 if (fn) |
|
370 { |
|
371 char *ff = fcn_file_in_path (fn); |
|
372 ff = ff ? ff : fn; |
|
373 |
|
374 if (print) |
|
375 output_buf << name |
|
376 << " is the function defined from:\n" |
|
377 << ff << "\n"; |
|
378 else |
|
379 retval = ff; |
|
380 } |
|
381 else |
|
382 { |
|
383 if (print) |
|
384 output_buf << name << " is a user-defined function\n"; |
|
385 else |
|
386 retval = "user-defined function"; |
|
387 } |
|
388 } |
|
389 else if (sym_rec->is_text_function ()) |
|
390 { |
|
391 if (print) |
|
392 output_buf << name << " is a builtin text-function\n"; |
|
393 else |
|
394 retval = "builtin text-function"; |
|
395 } |
|
396 else if (sym_rec->is_builtin_function ()) |
|
397 { |
|
398 if (print) |
|
399 output_buf << name << " is a builtin function\n"; |
|
400 else |
|
401 retval = "builtin function"; |
|
402 } |
|
403 else if (sym_rec->is_user_variable ()) |
|
404 { |
|
405 if (print) |
|
406 output_buf << name << " is a user-defined variable\n"; |
|
407 else |
|
408 retval = "user-defined variable"; |
|
409 } |
|
410 else if (sym_rec->is_builtin_variable ()) |
|
411 { |
|
412 if (print) |
|
413 output_buf << name << " is a builtin variable\n"; |
|
414 else |
|
415 retval = "builtin variable"; |
|
416 } |
|
417 else |
|
418 { |
|
419 if (print) |
|
420 output_buf << "which: `" << name |
|
421 << "' has unknown type\n"; |
|
422 else |
|
423 retval = "unknown type"; |
|
424 } |
|
425 |
|
426 return retval; |
|
427 } |
|
428 |
529
|
429 static void |
542
|
430 display_symtab_names (ostrstream& output_buf, char **names, |
|
431 int count, const char *desc) |
|
432 { |
|
433 output_buf << "\n*** " << desc << ":\n\n"; |
|
434 if (names && count > 0) |
|
435 list_in_columns (output_buf, names); |
|
436 } |
|
437 |
|
438 static void |
529
|
439 simple_help (void) |
|
440 { |
|
441 ostrstream output_buf; |
|
442 |
542
|
443 display_names_from_help_list (output_buf, operator_help (), |
|
444 "operators"); |
529
|
445 |
542
|
446 display_names_from_help_list (output_buf, keyword_help (), |
|
447 "reserved words"); |
529
|
448 |
542
|
449 #ifdef LIST_SYMBOLS |
|
450 #undef LIST_SYMBOLS |
|
451 #endif |
|
452 #define LIST_SYMBOLS(type, msg) \ |
|
453 do \ |
|
454 { \ |
|
455 int count; \ |
867
|
456 char **names = global_sym_tab->list (count, 0, 0, 1, type); \ |
542
|
457 display_symtab_names (output_buf, names, count, msg); \ |
|
458 char **ptr = names; \ |
|
459 while (*ptr) \ |
|
460 delete [] *ptr++; \ |
|
461 delete [] names; \ |
|
462 } \ |
|
463 while (0) |
529
|
464 |
542
|
465 // XXX FIXME XXX -- is this distinction needed? |
|
466 LIST_SYMBOLS (symbol_def::TEXT_FUNCTION, |
|
467 "text functions (these names are also reserved)"); |
529
|
468 |
542
|
469 LIST_SYMBOLS (symbol_def::MAPPER_FUNCTION, "mapper functions"); |
|
470 |
|
471 LIST_SYMBOLS (symbol_def::BUILTIN_FUNCTION, "general functions"); |
|
472 |
|
473 LIST_SYMBOLS (symbol_def::BUILTIN_VARIABLE, "builtin variables"); |
|
474 |
529
|
475 // Also need to list variables and currently compiled functions from |
|
476 // the symbol table, if there are any. |
|
477 |
|
478 // Also need to search octave_path for script files. |
|
479 |
679
|
480 char *path_elt = kpse_path_element (user_pref.loadpath); |
529
|
481 |
679
|
482 while (path_elt) |
529
|
483 { |
679
|
484 str_llist_type *elt_dirs = kpse_element_dirs (path_elt); |
|
485 |
|
486 str_llist_elt_type *dir; |
|
487 for (dir = *elt_dirs; dir; dir = STR_LLIST_NEXT (*dir)) |
529
|
488 { |
679
|
489 char *elt_dir = STR_LLIST (*dir); |
|
490 |
|
491 if (elt_dir) |
|
492 { |
|
493 int count; |
|
494 char **names = get_fcn_file_names (count, elt_dir, 0); |
|
495 |
|
496 output_buf << "\n*** function files in " |
|
497 << make_absolute (elt_dir, the_current_working_directory) |
|
498 << ":\n\n"; |
|
499 |
|
500 if (names && count > 0) |
|
501 list_in_columns (output_buf, names); |
|
502 |
|
503 delete [] names; |
|
504 } |
529
|
505 } |
679
|
506 path_elt = kpse_path_element (0); |
529
|
507 } |
|
508 |
|
509 additional_help_message (output_buf); |
|
510 output_buf << ends; |
|
511 maybe_page_output (output_buf); |
|
512 } |
|
513 |
|
514 static int |
|
515 try_info (const char *string, int force = 0) |
|
516 { |
|
517 int status = 0; |
|
518 |
|
519 char *directory_name = strsave (user_pref.info_file); |
|
520 char *temp = filename_non_directory (directory_name); |
|
521 |
|
522 if (temp != directory_name) |
|
523 { |
|
524 *temp = 0; |
|
525 info_add_path (directory_name, INFOPATH_PREPEND); |
|
526 } |
|
527 |
|
528 delete [] directory_name; |
|
529 |
|
530 NODE *initial_node = info_get_node (user_pref.info_file, 0); |
|
531 |
|
532 if (! initial_node) |
|
533 { |
|
534 warning ("can't find info file!\n"); |
|
535 status = -1; |
|
536 } |
|
537 else |
|
538 { |
|
539 status = initialize_info_session (initial_node, 0); |
|
540 |
|
541 if (status == 0 && (force || index_entry_exists (windows, string))) |
|
542 { |
|
543 terminal_clear_screen (); |
|
544 |
|
545 terminal_prep_terminal (); |
|
546 |
|
547 display_update_display (windows); |
|
548 |
|
549 info_last_executed_command = 0; |
|
550 |
|
551 if (! force) |
|
552 do_info_index_search (windows, 0, string); |
|
553 |
|
554 char *format = replace_in_documentation |
|
555 ("Type \"\\[quit]\" to quit, \"\\[get-help-window]\" for help."); |
|
556 |
|
557 window_message_in_echo_area (format); |
|
558 |
|
559 info_read_and_dispatch (); |
|
560 |
|
561 terminal_goto_xy (0, screenheight - 1); |
|
562 |
|
563 terminal_clear_to_eol (); |
|
564 |
|
565 terminal_unprep_terminal (); |
|
566 |
|
567 status = 1; |
|
568 } |
|
569 |
|
570 finish_info_session (initial_node, 0); |
|
571 } |
|
572 |
|
573 return status; |
|
574 } |
|
575 |
542
|
576 int |
|
577 help_from_list (ostrstream& output_buf, const help_list *list, |
|
578 const char *string, int usage) |
|
579 { |
|
580 char *name; |
|
581 while ((name = list->name) != 0) |
|
582 { |
|
583 if (strcmp (name, string) == 0) |
|
584 { |
|
585 if (usage) |
|
586 output_buf << "\nusage: "; |
|
587 else |
|
588 { |
|
589 output_buf << "\n*** " << string << ":\n\n"; |
|
590 } |
|
591 |
|
592 output_buf << list->help << "\n"; |
|
593 |
|
594 return 1; |
|
595 } |
|
596 list++; |
|
597 } |
|
598 return 0; |
|
599 } |
|
600 |
529
|
601 DEFUN_TEXT ("help", Fhelp, Shelp, -1, 1, |
|
602 "help [-i] [topic ...]\n\ |
|
603 \n\ |
|
604 print cryptic yet witty messages") |
|
605 { |
|
606 Octave_object retval; |
|
607 |
|
608 DEFINE_ARGV("help"); |
|
609 |
|
610 if (argc == 1) |
|
611 { |
|
612 simple_help (); |
|
613 } |
|
614 else |
|
615 { |
|
616 if (argv[1] && strcmp (argv[1], "-i") == 0) |
|
617 { |
|
618 argc--; |
|
619 argv++; |
|
620 |
|
621 if (argc == 1) |
|
622 { |
|
623 volatile sig_handler *old_sigint_handler; |
654
|
624 old_sigint_handler = signal (SIGINT, SIG_IGN); |
529
|
625 |
|
626 try_info (0, 1); |
|
627 |
|
628 signal (SIGINT, old_sigint_handler); |
|
629 } |
|
630 else |
|
631 { |
|
632 while (--argc > 0) |
|
633 { |
|
634 argv++; |
|
635 |
|
636 if (! *argv || ! **argv) |
|
637 continue; |
|
638 |
|
639 volatile sig_handler *old_sigint_handler; |
|
640 old_sigint_handler = signal (SIGINT, SIG_IGN); |
|
641 |
|
642 if (! try_info (*argv)) |
|
643 { |
|
644 message ("help", |
|
645 "sorry, `%s' is not indexed in the manual", |
|
646 *argv); |
|
647 sleep (2); |
|
648 } |
|
649 |
|
650 signal (SIGINT, old_sigint_handler); |
|
651 } |
|
652 } |
|
653 } |
|
654 else |
|
655 { |
|
656 ostrstream output_buf; |
|
657 |
|
658 help_list *op_help_list = operator_help (); |
|
659 help_list *kw_help_list = keyword_help (); |
|
660 |
|
661 while (--argc > 0) |
|
662 { |
|
663 argv++; |
|
664 |
|
665 if (! *argv || ! **argv) |
|
666 continue; |
|
667 |
|
668 if (help_from_list (output_buf, op_help_list, *argv, 0)) |
|
669 continue; |
|
670 |
|
671 if (help_from_list (output_buf, kw_help_list, *argv, 0)) |
|
672 continue; |
|
673 |
962
|
674 symbol_record *sym_rec = lookup_by_name (*argv, 0); |
581
|
675 |
991
|
676 if (sym_rec && sym_rec->is_defined ()) |
529
|
677 { |
|
678 char *h = sym_rec->help (); |
|
679 if (h && *h) |
|
680 { |
742
|
681 print_symbol_type (output_buf, sym_rec, *argv, 1); |
|
682 output_buf << "\n" << h << "\n"; |
529
|
683 continue; |
|
684 } |
|
685 } |
|
686 |
993
|
687 char *path = fcn_file_in_path (*argv); |
|
688 char *h = get_help_from_file (path); |
991
|
689 if (h && *h) |
|
690 { |
993
|
691 output_buf << *argv << " is the file:\n" |
|
692 << path << "\n\n" << h << "\n"; |
991
|
693 delete [] h; |
993
|
694 delete [] path; |
991
|
695 continue; |
|
696 } |
993
|
697 delete [] path; |
991
|
698 |
529
|
699 output_buf << "\nhelp: sorry, `" << *argv |
581
|
700 << "' is not documented\n"; |
529
|
701 } |
|
702 |
|
703 additional_help_message (output_buf); |
|
704 output_buf << ends; |
|
705 maybe_page_output (output_buf); |
|
706 } |
|
707 } |
|
708 |
|
709 DELETE_ARGV; |
|
710 |
|
711 return retval; |
|
712 } |
|
713 |
581
|
714 DEFUN_TEXT ("type", Ftype, Stype, -1, 1, |
|
715 "type NAME ...]\n\ |
|
716 \n\ |
|
717 display the definition of each NAME that refers to a function") |
|
718 { |
|
719 Octave_object retval; |
|
720 |
|
721 DEFINE_ARGV("type"); |
|
722 |
|
723 if (argc > 1) |
|
724 { |
|
725 // XXX FIXME XXX -- we should really use getopt () |
|
726 int quiet = 0; |
|
727 if (argv[1] && strcmp (argv[1], "-q") == 0) |
|
728 { |
|
729 quiet = 1; |
|
730 argc--; |
|
731 argv++; |
|
732 } |
|
733 |
|
734 ostrstream output_buf; |
|
735 |
|
736 while (--argc > 0) |
|
737 { |
|
738 argv++; |
|
739 |
|
740 if (! *argv || ! **argv) |
|
741 continue; |
|
742 |
962
|
743 symbol_record *sym_rec = lookup_by_name (*argv, 0); |
581
|
744 |
|
745 if (sym_rec) |
|
746 { |
|
747 if (sym_rec->is_user_function ()) |
|
748 { |
|
749 tree_fvc *defn = sym_rec->def (); |
|
750 |
|
751 if (nargout == 0 && ! quiet) |
|
752 output_buf << *argv << " is a user-defined function\n"; |
|
753 |
|
754 defn->print_code (output_buf); |
|
755 } |
|
756 |
|
757 // XXX FIXME XXX -- this code should be shared with Fwhich |
|
758 |
|
759 else if (sym_rec->is_text_function ()) |
|
760 output_buf << *argv << " is a builtin text-function\n"; |
|
761 else if (sym_rec->is_builtin_function ()) |
|
762 output_buf << *argv << " is a builtin function\n"; |
|
763 else if (sym_rec->is_user_variable ()) |
625
|
764 { |
|
765 tree_fvc *defn = sym_rec->def (); |
|
766 |
|
767 if (nargout == 0 && ! quiet) |
|
768 output_buf << *argv << " is a user-defined variable\n"; |
|
769 |
|
770 defn->print_code (output_buf); |
|
771 |
|
772 if (nargout == 0) |
|
773 output_buf << "\n"; |
|
774 } |
581
|
775 else if (sym_rec->is_builtin_variable ()) |
625
|
776 { |
|
777 tree_fvc *defn = sym_rec->def (); |
|
778 |
|
779 if (nargout == 0 && ! quiet) |
|
780 output_buf << *argv << " is a builtin variable\n"; |
|
781 |
|
782 defn->print_code (output_buf); |
|
783 |
|
784 if (nargout == 0) |
|
785 output_buf << "\n"; |
|
786 } |
581
|
787 else |
|
788 output_buf << "type: `" << *argv << "' has unknown type!\n"; |
|
789 } |
|
790 else |
|
791 output_buf << "type: `" << *argv << "' undefined\n"; |
|
792 } |
|
793 |
|
794 output_buf << ends; |
|
795 |
|
796 if (nargout == 0) |
|
797 maybe_page_output (output_buf); |
|
798 else |
|
799 { |
|
800 char *s = output_buf.str (); |
|
801 retval = s; |
|
802 delete s; |
|
803 } |
|
804 } |
|
805 else |
|
806 print_usage ("type"); |
|
807 |
|
808 DELETE_ARGV; |
|
809 |
|
810 return retval; |
|
811 } |
|
812 |
|
813 DEFUN_TEXT ("which", Fwhich, Swhich, -1, 1, |
|
814 "which NAME ...]\n\ |
|
815 \n\ |
|
816 display the type of each NAME. If NAME is defined from an function\n\ |
|
817 file, print the full name of the file.") |
|
818 { |
|
819 Octave_object retval; |
|
820 |
|
821 DEFINE_ARGV("which"); |
|
822 |
|
823 if (argc > 1) |
|
824 { |
|
825 if (nargout > 0) |
|
826 retval.resize (argc-1, Matrix ()); |
|
827 |
|
828 ostrstream output_buf; |
|
829 |
|
830 for (int i = 0; i < argc-1; i++) |
|
831 { |
|
832 argv++; |
|
833 |
|
834 if (! *argv || ! **argv) |
|
835 continue; |
|
836 |
962
|
837 symbol_record *sym_rec = lookup_by_name (*argv, 0); |
581
|
838 |
|
839 if (sym_rec) |
|
840 { |
742
|
841 int print = (nargout == 0); |
|
842 char *tmp = print_symbol_type (output_buf, sym_rec, |
|
843 *argv, print); |
|
844 if (! print) |
|
845 retval(i) = tmp; |
581
|
846 } |
|
847 else |
|
848 { |
|
849 if (nargout == 0) |
|
850 output_buf << "which: `" << *argv << "' is undefined\n"; |
|
851 else |
|
852 retval(i) = "undefined"; |
|
853 } |
|
854 } |
|
855 output_buf << ends; |
|
856 maybe_page_output (output_buf); |
|
857 } |
|
858 else |
|
859 print_usage ("which"); |
|
860 |
|
861 DELETE_ARGV; |
|
862 |
|
863 return retval; |
|
864 } |
|
865 |
1
|
866 /* |
|
867 ;;; Local Variables: *** |
|
868 ;;; mode: C++ *** |
|
869 ;;; page-delimiter: "^/\\*" *** |
|
870 ;;; End: *** |
|
871 */ |