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