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