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