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