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