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