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 |
2202
|
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 |
1115
|
328 #define VERBOSE_HELP_MESSAGE \ |
|
329 "\n\ |
|
330 Additional help for builtin functions, operators, and variables\n\ |
|
331 is available in the on-line version of the manual.\n\ |
|
332 \n\ |
|
333 Use the command `help -i <topic>' to search the manual index.\n" |
|
334 |
|
335 static void |
2095
|
336 additional_help_message (ostream& os) |
542
|
337 { |
1140
|
338 #ifdef USE_GNU_INFO |
2189
|
339 if (! Vsuppress_verbose_help_message) |
2095
|
340 os << VERBOSE_HELP_MESSAGE; |
1140
|
341 #endif |
542
|
342 } |
|
343 |
|
344 void |
1755
|
345 print_usage (const string& nm, int just_usage) |
542
|
346 { |
1755
|
347 symbol_record *sym_rec = global_sym_tab->lookup (nm, 0, 0); |
542
|
348 if (sym_rec) |
|
349 { |
1755
|
350 string h = sym_rec->help (); |
|
351 |
|
352 if (h.length () > 0) |
542
|
353 { |
2095
|
354 octave_stdout << "\n*** " << nm << ":\n\n" |
542
|
355 << h << "\n"; |
|
356 |
1117
|
357 if (! just_usage) |
2095
|
358 additional_help_message (octave_stdout); |
542
|
359 } |
|
360 } |
1023
|
361 else |
1755
|
362 warning ("no usage message found for `%s'", nm.c_str ()); |
542
|
363 } |
|
364 |
529
|
365 static void |
2095
|
366 display_names_from_help_list (ostream& os, help_list *list, |
542
|
367 const char *desc) |
529
|
368 { |
|
369 int count = 0; |
1755
|
370 string_vector symbols = names (list, count); |
1808
|
371 if (! symbols.empty ()) |
|
372 { |
2095
|
373 os << "\n*** " << desc << ":\n\n"; |
|
374 symbols.list_in_columns (os); |
1808
|
375 } |
529
|
376 } |
|
377 |
1755
|
378 static string |
2095
|
379 print_symbol_type (ostream& os, symbol_record *sym_rec, |
1755
|
380 const string& name, int print) |
742
|
381 { |
1755
|
382 string retval; |
742
|
383 |
|
384 if (sym_rec->is_user_function ()) |
|
385 { |
|
386 tree_fvc *defn = sym_rec->def (); |
1755
|
387 |
|
388 string fn = defn->fcn_file_name (); |
|
389 |
|
390 if (! fn.empty ()) |
742
|
391 { |
1755
|
392 string ff = fcn_file_in_path (fn); |
|
393 |
|
394 ff = ff.length () > 0 ? ff : fn; |
742
|
395 |
|
396 if (print) |
2095
|
397 os << name |
|
398 << " is the function defined from:\n" |
|
399 << ff << "\n"; |
742
|
400 else |
|
401 retval = ff; |
|
402 } |
|
403 else |
|
404 { |
|
405 if (print) |
2095
|
406 os << name << " is a user-defined function\n"; |
742
|
407 else |
|
408 retval = "user-defined function"; |
|
409 } |
|
410 } |
|
411 else if (sym_rec->is_text_function ()) |
|
412 { |
|
413 if (print) |
2095
|
414 os << name << " is a builtin text-function\n"; |
742
|
415 else |
|
416 retval = "builtin text-function"; |
|
417 } |
|
418 else if (sym_rec->is_builtin_function ()) |
|
419 { |
|
420 if (print) |
2095
|
421 os << name << " is a builtin function\n"; |
742
|
422 else |
|
423 retval = "builtin function"; |
|
424 } |
|
425 else if (sym_rec->is_user_variable ()) |
|
426 { |
|
427 if (print) |
2095
|
428 os << name << " is a user-defined variable\n"; |
742
|
429 else |
|
430 retval = "user-defined variable"; |
|
431 } |
|
432 else if (sym_rec->is_builtin_variable ()) |
|
433 { |
|
434 if (print) |
2095
|
435 os << name << " is a builtin variable\n"; |
742
|
436 else |
|
437 retval = "builtin variable"; |
|
438 } |
|
439 else |
|
440 { |
|
441 if (print) |
2095
|
442 os << "which: `" << name << "' has unknown type\n"; |
742
|
443 else |
|
444 retval = "unknown type"; |
|
445 } |
|
446 |
|
447 return retval; |
|
448 } |
|
449 |
529
|
450 static void |
2095
|
451 display_symtab_names (ostream& os, const string_vector& names, |
1755
|
452 int /* count */, const string& desc) |
542
|
453 { |
1808
|
454 if (! names.empty ()) |
|
455 { |
2095
|
456 os << "\n*** " << desc << ":\n\n"; |
|
457 names.list_in_columns (os); |
1808
|
458 } |
542
|
459 } |
|
460 |
|
461 static void |
529
|
462 simple_help (void) |
|
463 { |
2095
|
464 display_names_from_help_list (octave_stdout, operator_help (), |
542
|
465 "operators"); |
529
|
466 |
2095
|
467 display_names_from_help_list (octave_stdout, keyword_help (), |
542
|
468 "reserved words"); |
529
|
469 |
542
|
470 #ifdef LIST_SYMBOLS |
|
471 #undef LIST_SYMBOLS |
|
472 #endif |
|
473 #define LIST_SYMBOLS(type, msg) \ |
|
474 do \ |
|
475 { \ |
|
476 int count; \ |
1755
|
477 string_vector names = global_sym_tab->list (count, 0, 0, 1, type); \ |
2095
|
478 display_symtab_names (octave_stdout, names, count, msg); \ |
542
|
479 } \ |
|
480 while (0) |
529
|
481 |
1358
|
482 // XXX FIXME XXX -- is this distinction needed? |
|
483 |
542
|
484 LIST_SYMBOLS (symbol_def::TEXT_FUNCTION, |
|
485 "text functions (these names are also reserved)"); |
529
|
486 |
542
|
487 LIST_SYMBOLS (symbol_def::MAPPER_FUNCTION, "mapper functions"); |
|
488 |
|
489 LIST_SYMBOLS (symbol_def::BUILTIN_FUNCTION, "general functions"); |
|
490 |
|
491 LIST_SYMBOLS (symbol_def::BUILTIN_VARIABLE, "builtin variables"); |
|
492 |
1358
|
493 // Also need to list variables and currently compiled functions from |
|
494 // the symbol table, if there are any. |
529
|
495 |
1358
|
496 // Also need to search octave_path for script files. |
529
|
497 |
2202
|
498 dir_path p (Vload_path); |
529
|
499 |
1787
|
500 string_vector dirs = p.all_directories (); |
679
|
501 |
1787
|
502 int len = dirs.length (); |
679
|
503 |
1787
|
504 for (int i = 0; i < len; i++) |
|
505 { |
2233
|
506 string_vector names = octave_fcn_file_name_cache::list (dirs[i]); |
679
|
507 |
1787
|
508 if (! names.empty ()) |
|
509 { |
2095
|
510 octave_stdout << "\n*** function files in " |
2202
|
511 << make_absolute (dirs[i], Vcurrent_directory) |
2095
|
512 << ":\n\n"; |
679
|
513 |
2095
|
514 names.list_in_columns (octave_stdout); |
529
|
515 } |
|
516 } |
|
517 |
2095
|
518 additional_help_message (octave_stdout); |
529
|
519 } |
|
520 |
1140
|
521 #ifdef USE_GNU_INFO |
529
|
522 static int |
1755
|
523 try_info (const string& nm) |
529
|
524 { |
|
525 int status = 0; |
|
526 |
1295
|
527 static char *cmd_str = 0; |
|
528 |
|
529 delete [] cmd_str; |
|
530 cmd_str = 0; |
|
531 |
|
532 ostrstream cmd_buf; |
|
533 |
2202
|
534 cmd_buf << Vinfo_prog << " --file " << Vinfo_file; |
1295
|
535 |
2202
|
536 string directory_name = Vinfo_file; |
1755
|
537 size_t pos = directory_name.rfind ('/'); |
|
538 |
|
539 if (pos != NPOS) |
529
|
540 { |
1755
|
541 directory_name.resize (pos + 1); |
1295
|
542 cmd_buf << " --directory " << directory_name; |
529
|
543 } |
|
544 |
1755
|
545 if (nm.length () > 0) |
|
546 cmd_buf << " --index-search " << nm; |
529
|
547 |
1295
|
548 cmd_buf << ends; |
529
|
549 |
1295
|
550 cmd_str = cmd_buf.str (); |
529
|
551 |
1295
|
552 volatile sig_handler *old_sigint_handler; |
1443
|
553 old_sigint_handler = octave_set_signal_handler (SIGINT, SIG_IGN); |
529
|
554 |
1295
|
555 status = system (cmd_str); |
529
|
556 |
1443
|
557 octave_set_signal_handler (SIGINT, old_sigint_handler); |
529
|
558 |
1295
|
559 if ((status & 0xff) == 0) |
|
560 status = (signed char) ((status & 0xff00) >> 8); |
|
561 else |
|
562 status = 127; |
529
|
563 |
|
564 return status; |
|
565 } |
1140
|
566 #endif |
|
567 |
|
568 static void |
1755
|
569 help_from_info (const string_vector& argv, int idx, int argc) |
1140
|
570 { |
|
571 #ifdef USE_GNU_INFO |
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 } |
|
597 #else |
|
598 message (0, "sorry, help -i is not available in this version of Octave"); |
|
599 #endif |
|
600 } |
529
|
601 |
542
|
602 int |
2095
|
603 help_from_list (ostream& os, const help_list *list, |
1755
|
604 const string& nm, int usage) |
542
|
605 { |
|
606 char *name; |
|
607 while ((name = list->name) != 0) |
|
608 { |
1755
|
609 if (strcmp (name, nm.c_str ()) == 0) |
542
|
610 { |
|
611 if (usage) |
2095
|
612 os << "\nusage: "; |
542
|
613 else |
|
614 { |
2095
|
615 os << "\n*** " << nm << ":\n\n"; |
542
|
616 } |
|
617 |
2095
|
618 os << list->help << "\n"; |
542
|
619 |
|
620 return 1; |
|
621 } |
|
622 list++; |
|
623 } |
|
624 return 0; |
|
625 } |
|
626 |
1140
|
627 static void |
1755
|
628 builtin_help (int argc, const string_vector& argv) |
1140
|
629 { |
|
630 help_list *op_help_list = operator_help (); |
|
631 help_list *kw_help_list = keyword_help (); |
|
632 |
1755
|
633 for (int i = 1; i < argc; i++) |
1140
|
634 { |
2095
|
635 if (help_from_list (octave_stdout, op_help_list, argv[i], 0)) |
1140
|
636 continue; |
|
637 |
2095
|
638 if (help_from_list (octave_stdout, kw_help_list, argv[i], 0)) |
1140
|
639 continue; |
|
640 |
1755
|
641 symbol_record *sym_rec = lookup_by_name (argv[i], 0); |
1140
|
642 |
|
643 if (sym_rec && sym_rec->is_defined ()) |
|
644 { |
1755
|
645 string h = sym_rec->help (); |
|
646 |
|
647 if (h.length () > 0) |
1140
|
648 { |
2095
|
649 print_symbol_type (octave_stdout, sym_rec, argv[i], 1); |
|
650 octave_stdout << "\n" << h << "\n"; |
1140
|
651 continue; |
|
652 } |
|
653 } |
|
654 |
1755
|
655 string path = fcn_file_in_path (argv[i]); |
|
656 |
|
657 string h = get_help_from_file (path); |
|
658 |
|
659 if (! h.empty ()) |
1140
|
660 { |
2095
|
661 octave_stdout << argv[i] << " is the file:\n" |
1140
|
662 << path << "\n\n" << h << "\n"; |
1755
|
663 |
1140
|
664 continue; |
|
665 } |
|
666 |
2095
|
667 octave_stdout << "\nhelp: sorry, `" << argv[i] |
|
668 << "' is not documented\n"; |
1140
|
669 } |
|
670 |
2095
|
671 additional_help_message (octave_stdout); |
1140
|
672 } |
|
673 |
|
674 #ifdef USE_GNU_INFO |
1957
|
675 DEFUN_TEXT (help, args, , |
529
|
676 "help [-i] [topic ...]\n\ |
|
677 \n\ |
|
678 print cryptic yet witty messages") |
1140
|
679 #else |
1957
|
680 DEFUN_TEXT (help, args, , |
1140
|
681 "help [topic ...]\n\ |
|
682 \n\ |
|
683 print cryptic yet witty messages") |
|
684 #endif |
529
|
685 { |
2086
|
686 octave_value_list retval; |
529
|
687 |
1755
|
688 int argc = args.length () + 1; |
|
689 |
1968
|
690 string_vector argv = args.make_argv ("help"); |
1755
|
691 |
|
692 if (error_state) |
|
693 return retval; |
529
|
694 |
|
695 if (argc == 1) |
|
696 { |
|
697 simple_help (); |
|
698 } |
|
699 else |
|
700 { |
1755
|
701 if (argv[1] == "-i") |
529
|
702 { |
1755
|
703 help_from_info (argv, 2, argc); |
529
|
704 } |
|
705 else |
|
706 { |
1140
|
707 builtin_help (argc, argv); |
529
|
708 } |
|
709 } |
|
710 |
|
711 return retval; |
|
712 } |
|
713 |
1957
|
714 DEFUN_TEXT (type, args, nargout, |
581
|
715 "type NAME ...]\n\ |
|
716 \n\ |
|
717 display the definition of each NAME that refers to a function") |
|
718 { |
2086
|
719 octave_value_list retval; |
581
|
720 |
1588
|
721 begin_unwind_frame ("Ftype"); |
|
722 |
2177
|
723 unwind_protect_str (Vps4); |
|
724 Vps4 = ""; |
1588
|
725 |
1755
|
726 int argc = args.length () + 1; |
|
727 |
1968
|
728 string_vector argv = args.make_argv ("type"); |
1755
|
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 { |
1889
|
758 elts = id.substr (pos+1); |
1755
|
759 id = id.substr (0, pos); |
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 |
2124
|
774 tree_print_code tpc (output_buf); |
|
775 |
|
776 defn->accept (tpc); |
581
|
777 } |
|
778 |
1358
|
779 // XXX FIXME XXX -- this code should be shared with |
|
780 // Fwhich. |
581
|
781 |
|
782 else if (sym_rec->is_text_function ()) |
1755
|
783 output_buf << argv[i] << " is a builtin text-function\n"; |
581
|
784 else if (sym_rec->is_builtin_function ()) |
1755
|
785 output_buf << argv[i] << " is a builtin function\n"; |
1281
|
786 else if (sym_rec->is_user_variable () |
|
787 || sym_rec->is_builtin_variable ()) |
625
|
788 { |
|
789 tree_fvc *defn = sym_rec->def (); |
|
790 |
1288
|
791 assert (defn && defn->is_constant ()); |
1281
|
792 |
2371
|
793 tree_constant *tmp = (tree_constant *) defn; |
625
|
794 |
1281
|
795 int var_ok = 1; |
|
796 if (tmp && tmp->is_map ()) |
|
797 { |
1755
|
798 if (! elts.empty ()) |
1281
|
799 { |
2086
|
800 octave_value ult = |
1288
|
801 tmp->lookup_map_element (elts, 0, 1); |
625
|
802 |
1281
|
803 if (! ult.is_defined ()) |
|
804 var_ok = 0; |
|
805 } |
|
806 } |
1288
|
807 |
1281
|
808 if (nargout == 0 && ! quiet) |
|
809 { |
1288
|
810 if (var_ok) |
|
811 { |
1755
|
812 output_buf << argv[i]; |
1288
|
813 if (sym_rec->is_user_variable ()) |
|
814 output_buf << " is a user-defined variable\n"; |
|
815 else |
|
816 output_buf << " is a built-in variable\n"; |
|
817 } |
1281
|
818 else |
1288
|
819 { |
1755
|
820 if (! elts.empty ()) |
1288
|
821 output_buf << "type: structure `" << id |
|
822 << "' has no member `" << elts << "'\n"; |
|
823 else |
|
824 output_buf << "type: `" << id |
|
825 << "' has unknown type!"; |
|
826 } |
1281
|
827 } |
|
828 if (! tmp->is_map ()) |
|
829 { |
2124
|
830 tree_print_code tpc (output_buf); |
|
831 |
|
832 tmp->accept (tpc); |
625
|
833 |
1281
|
834 if (nargout == 0) |
|
835 output_buf << "\n"; |
|
836 } |
625
|
837 } |
581
|
838 else |
1755
|
839 output_buf << "type: `" << argv[i] << "' has unknown type!\n"; |
581
|
840 } |
|
841 else |
1755
|
842 output_buf << "type: `" << argv[i] << "' undefined\n"; |
581
|
843 } |
|
844 |
|
845 output_buf << ends; |
|
846 |
2095
|
847 char *s = output_buf.str (); |
|
848 |
581
|
849 if (nargout == 0) |
2095
|
850 octave_stdout << s; |
581
|
851 else |
2095
|
852 retval = s; |
|
853 |
|
854 delete [] s; |
581
|
855 } |
|
856 else |
|
857 print_usage ("type"); |
|
858 |
1588
|
859 run_unwind_frame ("Ftype"); |
|
860 |
581
|
861 return retval; |
|
862 } |
|
863 |
1957
|
864 DEFUN_TEXT (which, args, nargout, |
1565
|
865 "which NAME ...\n\ |
581
|
866 \n\ |
|
867 display the type of each NAME. If NAME is defined from an function\n\ |
|
868 file, print the full name of the file.") |
|
869 { |
2086
|
870 octave_value_list retval; |
581
|
871 |
1755
|
872 int argc = args.length () + 1; |
|
873 |
1968
|
874 string_vector argv = args.make_argv ("which"); |
1755
|
875 |
|
876 if (error_state) |
|
877 return retval; |
581
|
878 |
|
879 if (argc > 1) |
|
880 { |
|
881 if (nargout > 0) |
|
882 retval.resize (argc-1, Matrix ()); |
|
883 |
1755
|
884 for (int i = 1; i < argc; i++) |
581
|
885 { |
1755
|
886 symbol_record *sym_rec = lookup_by_name (argv[i], 0); |
581
|
887 |
|
888 if (sym_rec) |
|
889 { |
742
|
890 int print = (nargout == 0); |
2095
|
891 string tmp = print_symbol_type (octave_stdout, sym_rec, |
1755
|
892 argv[i], print); |
742
|
893 if (! print) |
|
894 retval(i) = tmp; |
581
|
895 } |
|
896 else |
|
897 { |
|
898 if (nargout == 0) |
2095
|
899 octave_stdout << "which: `" << argv[i] << "' is undefined\n"; |
581
|
900 else |
|
901 retval(i) = "undefined"; |
|
902 } |
|
903 } |
|
904 } |
|
905 else |
|
906 print_usage ("which"); |
|
907 |
|
908 return retval; |
|
909 } |
|
910 |
2189
|
911 static int |
|
912 suppress_verbose_help_message (void) |
|
913 { |
|
914 Vsuppress_verbose_help_message |
|
915 = check_preference ("suppress_verbose_help_message"); |
|
916 |
|
917 return 0; |
|
918 } |
|
919 |
2202
|
920 static int |
|
921 info_file (void) |
|
922 { |
|
923 int status = 0; |
|
924 |
|
925 string s = builtin_string_variable ("INFO_FILE"); |
|
926 |
|
927 if (s.empty ()) |
|
928 { |
|
929 gripe_invalid_value_specified ("INFO_FILE"); |
|
930 status = -1; |
|
931 } |
|
932 else |
|
933 Vinfo_file = s; |
|
934 |
|
935 return status; |
|
936 } |
|
937 |
|
938 static int |
|
939 info_prog (void) |
|
940 { |
|
941 int status = 0; |
|
942 |
|
943 string s = builtin_string_variable ("INFO_PROGRAM"); |
|
944 |
|
945 if (s.empty ()) |
|
946 { |
|
947 gripe_invalid_value_specified ("INFO_PROGRAM"); |
|
948 status = -1; |
|
949 } |
|
950 else |
|
951 Vinfo_prog = s; |
|
952 |
|
953 return status; |
|
954 } |
|
955 |
2189
|
956 void |
|
957 symbols_of_help (void) |
|
958 { |
2202
|
959 DEFVAR (INFO_FILE, Vinfo_file, 0, info_file, |
|
960 "name of the Octave info file"); |
|
961 |
|
962 DEFVAR (INFO_PROGRAM, Vinfo_prog, 0, info_prog, |
|
963 "name of the Octave info reader"); |
|
964 |
2189
|
965 #ifdef USE_GNU_INFO |
|
966 DEFVAR (suppress_verbose_help_message, 0.0, 0, suppress_verbose_help_message, |
|
967 "suppress printing of message pointing to additional help in the\n\ |
|
968 help and usage functions"); |
|
969 #endif |
|
970 } |
|
971 |
1
|
972 /* |
|
973 ;;; Local Variables: *** |
|
974 ;;; mode: C++ *** |
|
975 ;;; End: *** |
|
976 */ |