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