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