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