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