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