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