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