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