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