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