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