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 { |
|
399 if (! Vsuppress_verbose_help_message) |
|
400 os << "\n\ |
1115
|
401 Additional help for builtin functions, operators, and variables\n\ |
3160
|
402 is available in the on-line version of the manual. Use the command\n\ |
|
403 `help -i <topic>' to search the manual index.\n"; |
3014
|
404 |
3160
|
405 if (! Vsuppress_verbose_help_message) |
|
406 os << "\n\ |
|
407 Help and information about Octave is also available on the WWW\n\ |
|
408 at http://www.che.wisc.edu/octave/octave.html and via the\n\ |
|
409 help-octave@bevo.che.wisc.edu mailing list.\n"; |
|
410 |
|
411 } |
|
412 |
2976
|
413 // XXX FIXME XXX -- this needs a major overhaul to cope with new |
|
414 // symbol table stuff. |
|
415 |
529
|
416 static void |
2095
|
417 display_names_from_help_list (ostream& os, help_list *list, |
542
|
418 const char *desc) |
529
|
419 { |
|
420 int count = 0; |
1755
|
421 string_vector symbols = names (list, count); |
1808
|
422 if (! symbols.empty ()) |
|
423 { |
2095
|
424 os << "\n*** " << desc << ":\n\n"; |
|
425 symbols.list_in_columns (os); |
1808
|
426 } |
529
|
427 } |
|
428 |
1755
|
429 static string |
2095
|
430 print_symbol_type (ostream& os, symbol_record *sym_rec, |
1755
|
431 const string& name, int print) |
742
|
432 { |
1755
|
433 string retval; |
742
|
434 |
|
435 if (sym_rec->is_user_function ()) |
|
436 { |
2976
|
437 octave_value tmp = sym_rec->def (); |
1755
|
438 |
2976
|
439 octave_function *defn = tmp.function_value (); |
|
440 |
|
441 string fn = defn ? defn->fcn_file_name () : string (); |
1755
|
442 |
|
443 if (! fn.empty ()) |
742
|
444 { |
1755
|
445 string ff = fcn_file_in_path (fn); |
|
446 |
|
447 ff = ff.length () > 0 ? ff : fn; |
742
|
448 |
|
449 if (print) |
2095
|
450 os << name |
3141
|
451 << " is the function defined from: " |
2095
|
452 << ff << "\n"; |
742
|
453 else |
|
454 retval = ff; |
|
455 } |
|
456 else |
|
457 { |
|
458 if (print) |
2095
|
459 os << name << " is a user-defined function\n"; |
742
|
460 else |
|
461 retval = "user-defined function"; |
|
462 } |
|
463 } |
|
464 else if (sym_rec->is_text_function ()) |
|
465 { |
|
466 if (print) |
2095
|
467 os << name << " is a builtin text-function\n"; |
742
|
468 else |
|
469 retval = "builtin text-function"; |
|
470 } |
|
471 else if (sym_rec->is_builtin_function ()) |
|
472 { |
|
473 if (print) |
2095
|
474 os << name << " is a builtin function\n"; |
742
|
475 else |
|
476 retval = "builtin function"; |
|
477 } |
|
478 else if (sym_rec->is_user_variable ()) |
|
479 { |
|
480 if (print) |
2095
|
481 os << name << " is a user-defined variable\n"; |
742
|
482 else |
|
483 retval = "user-defined variable"; |
|
484 } |
|
485 else if (sym_rec->is_builtin_variable ()) |
|
486 { |
|
487 if (print) |
2095
|
488 os << name << " is a builtin variable\n"; |
742
|
489 else |
|
490 retval = "builtin variable"; |
|
491 } |
|
492 else |
|
493 { |
|
494 if (print) |
2095
|
495 os << "which: `" << name << "' has unknown type\n"; |
742
|
496 else |
|
497 retval = "unknown type"; |
|
498 } |
|
499 |
|
500 return retval; |
|
501 } |
|
502 |
529
|
503 static void |
2095
|
504 display_symtab_names (ostream& os, const string_vector& names, |
1755
|
505 int /* count */, const string& desc) |
542
|
506 { |
1808
|
507 if (! names.empty ()) |
|
508 { |
2095
|
509 os << "\n*** " << desc << ":\n\n"; |
|
510 names.list_in_columns (os); |
1808
|
511 } |
542
|
512 } |
|
513 |
|
514 #ifdef LIST_SYMBOLS |
|
515 #undef LIST_SYMBOLS |
|
516 #endif |
|
517 #define LIST_SYMBOLS(type, msg) \ |
|
518 do \ |
|
519 { \ |
|
520 int count; \ |
3013
|
521 string_vector names = global_sym_tab->name_list (count, 0, 0, 1, type); \ |
2095
|
522 display_symtab_names (octave_stdout, names, count, msg); \ |
542
|
523 } \ |
|
524 while (0) |
529
|
525 |
3014
|
526 static void |
|
527 simple_help (void) |
|
528 { |
3160
|
529 octave_stdout << "Help is available for the topics listed below.\n"; |
|
530 |
|
531 additional_help_message (octave_stdout); |
|
532 |
3014
|
533 display_names_from_help_list (octave_stdout, operator_help (), |
|
534 "operators"); |
|
535 |
|
536 display_names_from_help_list (octave_stdout, keyword_help (), |
|
537 "reserved words"); |
|
538 |
1358
|
539 // XXX FIXME XXX -- is this distinction needed? |
|
540 |
3010
|
541 LIST_SYMBOLS (symbol_record::TEXT_FUNCTION, |
542
|
542 "text functions (these names are also reserved)"); |
529
|
543 |
3010
|
544 LIST_SYMBOLS (symbol_record::MAPPER_FUNCTION, "mapper functions"); |
542
|
545 |
3010
|
546 LIST_SYMBOLS (symbol_record::BUILTIN_FUNCTION, "general functions"); |
542
|
547 |
3010
|
548 LIST_SYMBOLS (symbol_record::BUILTIN_VARIABLE, "builtin variables"); |
542
|
549 |
1358
|
550 // Also need to list variables and currently compiled functions from |
|
551 // the symbol table, if there are any. |
529
|
552 |
1358
|
553 // Also need to search octave_path for script files. |
529
|
554 |
3195
|
555 string_vector dirs = Vload_path_dir_path.all_directories (); |
679
|
556 |
1787
|
557 int len = dirs.length (); |
679
|
558 |
1787
|
559 for (int i = 0; i < len; i++) |
|
560 { |
2233
|
561 string_vector names = octave_fcn_file_name_cache::list (dirs[i]); |
679
|
562 |
1787
|
563 if (! names.empty ()) |
|
564 { |
2926
|
565 string dir |
|
566 = octave_env::make_absolute (dirs[i], octave_env::getcwd ()); |
|
567 |
|
568 octave_stdout << "\n*** function files in " << dir << ":\n\n"; |
679
|
569 |
2095
|
570 names.list_in_columns (octave_stdout); |
529
|
571 } |
|
572 } |
|
573 } |
|
574 |
|
575 static int |
1755
|
576 try_info (const string& nm) |
529
|
577 { |
|
578 int status = 0; |
|
579 |
1295
|
580 static char *cmd_str = 0; |
|
581 |
|
582 delete [] cmd_str; |
|
583 cmd_str = 0; |
|
584 |
|
585 ostrstream cmd_buf; |
|
586 |
2202
|
587 cmd_buf << Vinfo_prog << " --file " << Vinfo_file; |
1295
|
588 |
2202
|
589 string directory_name = Vinfo_file; |
1755
|
590 size_t pos = directory_name.rfind ('/'); |
|
591 |
|
592 if (pos != NPOS) |
529
|
593 { |
1755
|
594 directory_name.resize (pos + 1); |
1295
|
595 cmd_buf << " --directory " << directory_name; |
529
|
596 } |
|
597 |
1755
|
598 if (nm.length () > 0) |
|
599 cmd_buf << " --index-search " << nm; |
529
|
600 |
1295
|
601 cmd_buf << ends; |
529
|
602 |
1295
|
603 cmd_str = cmd_buf.str (); |
529
|
604 |
2705
|
605 volatile octave_interrupt_handler old_interrupt_handler |
2554
|
606 = octave_ignore_interrupts (); |
529
|
607 |
1295
|
608 status = system (cmd_str); |
529
|
609 |
2554
|
610 octave_set_interrupt_handler (old_interrupt_handler); |
529
|
611 |
2694
|
612 if (WIFEXITED (status)) |
|
613 status = WEXITSTATUS (status); |
1295
|
614 else |
|
615 status = 127; |
529
|
616 |
|
617 return status; |
|
618 } |
1140
|
619 |
|
620 static void |
1755
|
621 help_from_info (const string_vector& argv, int idx, int argc) |
1140
|
622 { |
1755
|
623 if (idx == argc) |
|
624 try_info (string ()); |
1140
|
625 else |
|
626 { |
1755
|
627 for (int i = idx; i < argc; i++) |
1140
|
628 { |
1755
|
629 int status = try_info (argv[i]); |
1140
|
630 |
1295
|
631 if (status) |
1140
|
632 { |
1295
|
633 if (status < 0) |
|
634 { |
3014
|
635 message ("help", "sorry, `%s' is not indexed in the manual", |
1755
|
636 argv[i].c_str ()); |
1295
|
637 sleep (2); |
|
638 } |
|
639 else |
|
640 { |
3185
|
641 error ("help: unable to find info"); |
|
642 error ("help: you need info 2.18 or later (texinfo 3.12)"); |
1295
|
643 break; |
|
644 } |
1140
|
645 } |
|
646 } |
|
647 } |
2470
|
648 } |
3014
|
649 |
|
650 static bool |
2095
|
651 help_from_list (ostream& os, const help_list *list, |
1755
|
652 const string& nm, int usage) |
542
|
653 { |
2804
|
654 const char *name; |
3014
|
655 |
542
|
656 while ((name = list->name) != 0) |
|
657 { |
1755
|
658 if (strcmp (name, nm.c_str ()) == 0) |
542
|
659 { |
|
660 if (usage) |
2095
|
661 os << "\nusage: "; |
542
|
662 else |
|
663 { |
2095
|
664 os << "\n*** " << nm << ":\n\n"; |
542
|
665 } |
|
666 |
2095
|
667 os << list->help << "\n"; |
542
|
668 |
3014
|
669 return true; |
542
|
670 } |
|
671 list++; |
|
672 } |
3014
|
673 |
|
674 return false; |
542
|
675 } |
|
676 |
1140
|
677 static void |
1755
|
678 builtin_help (int argc, const string_vector& argv) |
1140
|
679 { |
|
680 help_list *op_help_list = operator_help (); |
|
681 help_list *kw_help_list = keyword_help (); |
|
682 |
1755
|
683 for (int i = 1; i < argc; i++) |
1140
|
684 { |
2095
|
685 if (help_from_list (octave_stdout, op_help_list, argv[i], 0)) |
1140
|
686 continue; |
|
687 |
2095
|
688 if (help_from_list (octave_stdout, kw_help_list, argv[i], 0)) |
1140
|
689 continue; |
|
690 |
1755
|
691 symbol_record *sym_rec = lookup_by_name (argv[i], 0); |
1140
|
692 |
|
693 if (sym_rec && sym_rec->is_defined ()) |
|
694 { |
1755
|
695 string h = sym_rec->help (); |
|
696 |
|
697 if (h.length () > 0) |
1140
|
698 { |
2095
|
699 print_symbol_type (octave_stdout, sym_rec, argv[i], 1); |
|
700 octave_stdout << "\n" << h << "\n"; |
1140
|
701 continue; |
|
702 } |
|
703 } |
|
704 |
1755
|
705 string path = fcn_file_in_path (argv[i]); |
|
706 |
|
707 string h = get_help_from_file (path); |
|
708 |
|
709 if (! h.empty ()) |
1140
|
710 { |
3141
|
711 octave_stdout << argv[i] << " is the file: " |
1140
|
712 << path << "\n\n" << h << "\n"; |
1755
|
713 |
1140
|
714 continue; |
|
715 } |
|
716 |
2095
|
717 octave_stdout << "\nhelp: sorry, `" << argv[i] |
|
718 << "' is not documented\n"; |
1140
|
719 } |
|
720 |
2095
|
721 additional_help_message (octave_stdout); |
1140
|
722 } |
|
723 |
1957
|
724 DEFUN_TEXT (help, args, , |
3168
|
725 "help [-i] [topic ...]\n\ |
|
726 \n\ |
|
727 print cryptic yet witty messages") |
529
|
728 { |
2086
|
729 octave_value_list retval; |
529
|
730 |
1755
|
731 int argc = args.length () + 1; |
|
732 |
1968
|
733 string_vector argv = args.make_argv ("help"); |
1755
|
734 |
|
735 if (error_state) |
|
736 return retval; |
529
|
737 |
|
738 if (argc == 1) |
3014
|
739 simple_help (); |
529
|
740 else |
|
741 { |
1755
|
742 if (argv[1] == "-i") |
3014
|
743 help_from_info (argv, 2, argc); |
529
|
744 else |
3014
|
745 builtin_help (argc, argv); |
529
|
746 } |
|
747 |
|
748 return retval; |
|
749 } |
|
750 |
1957
|
751 DEFUN_TEXT (type, args, nargout, |
2962
|
752 "type NAME\n\ |
581
|
753 \n\ |
|
754 display the definition of each NAME that refers to a function") |
|
755 { |
2086
|
756 octave_value_list retval; |
581
|
757 |
2985
|
758 unwind_protect::begin_frame ("Ftype"); |
1588
|
759 |
1755
|
760 int argc = args.length () + 1; |
|
761 |
1968
|
762 string_vector argv = args.make_argv ("type"); |
1755
|
763 |
|
764 if (error_state) |
|
765 return retval; |
581
|
766 |
|
767 if (argc > 1) |
|
768 { |
1358
|
769 // XXX FIXME XXX -- we should really use getopt () |
|
770 |
2532
|
771 bool quiet = false; |
|
772 bool pr_orig_txt = true; |
|
773 |
|
774 int idx; |
|
775 |
|
776 for (idx = 1; idx < argc; idx++) |
581
|
777 { |
2532
|
778 if (argv[idx] == "-q" || argv[idx] == "-quiet") |
|
779 quiet = true; |
|
780 else if (argv[idx] == "-t" || argv[idx] == "-transformed") |
|
781 pr_orig_txt = false; |
|
782 else |
|
783 break; |
|
784 } |
|
785 |
|
786 if (idx == argc) |
|
787 { |
|
788 print_usage ("type"); |
|
789 return retval; |
581
|
790 } |
|
791 |
|
792 ostrstream output_buf; |
|
793 |
1755
|
794 for (int i = idx; i < argc; i++) |
581
|
795 { |
1755
|
796 string id = argv[i]; |
|
797 string elts; |
581
|
798 |
1755
|
799 if (id[id.length () - 1] != '.') |
1281
|
800 { |
1755
|
801 size_t pos = id.find ('.'); |
|
802 |
|
803 if (pos != NPOS) |
1288
|
804 { |
1889
|
805 elts = id.substr (pos+1); |
1755
|
806 id = id.substr (0, pos); |
1288
|
807 } |
1281
|
808 } |
|
809 |
|
810 symbol_record *sym_rec = lookup_by_name (id, 0); |
581
|
811 |
3141
|
812 if (sym_rec && sym_rec->is_defined ()) |
581
|
813 { |
|
814 if (sym_rec->is_user_function ()) |
|
815 { |
2976
|
816 octave_value tmp = sym_rec->def (); |
|
817 |
|
818 octave_function *defn = tmp.function_value (); |
2962
|
819 |
2976
|
820 string fn = defn ? defn->fcn_file_name () : string (); |
581
|
821 |
2976
|
822 string ff = fn.empty () ? string () : fcn_file_in_path (fn); |
2534
|
823 |
|
824 if (pr_orig_txt && ! ff.empty ()) |
|
825 { |
|
826 ifstream fs (ff.c_str (), ios::in); |
|
827 |
|
828 if (fs) |
|
829 { |
|
830 if (nargout == 0 && ! quiet) |
|
831 output_buf << argv[i] |
3141
|
832 << " is the function defined from: " |
2534
|
833 << ff << "\n\n"; |
|
834 |
|
835 char ch; |
581
|
836 |
2534
|
837 while (fs.get (ch)) |
|
838 output_buf << ch; |
|
839 } |
|
840 else |
|
841 output_buf << "unable to open `" << ff |
|
842 << "' for reading!\n"; |
|
843 } |
|
844 else |
|
845 { |
|
846 if (nargout == 0 && ! quiet) |
|
847 output_buf << argv[i] |
|
848 << " is a user-defined function:\n\n"; |
2124
|
849 |
2534
|
850 tree_print_code tpc (output_buf, "", pr_orig_txt); |
|
851 |
2962
|
852 defn->accept (tpc); |
2534
|
853 } |
581
|
854 } |
|
855 |
1358
|
856 // XXX FIXME XXX -- this code should be shared with |
|
857 // Fwhich. |
581
|
858 |
|
859 else if (sym_rec->is_text_function ()) |
1755
|
860 output_buf << argv[i] << " is a builtin text-function\n"; |
581
|
861 else if (sym_rec->is_builtin_function ()) |
1755
|
862 output_buf << argv[i] << " is a builtin function\n"; |
1281
|
863 else if (sym_rec->is_user_variable () |
|
864 || sym_rec->is_builtin_variable ()) |
625
|
865 { |
2976
|
866 octave_value defn = sym_rec->def (); |
625
|
867 |
1281
|
868 int var_ok = 1; |
2875
|
869 |
|
870 // XXX FIXME XXX -- need to handle structure |
|
871 // references correctly. |
|
872 |
3246
|
873 //if (defn.is_map ()) |
|
874 // error ("type: operations on structs not implemented"); |
1288
|
875 |
2875
|
876 if (! error_state) |
1281
|
877 { |
2875
|
878 if (nargout == 0 && ! quiet) |
1288
|
879 { |
2875
|
880 if (var_ok) |
|
881 { |
|
882 output_buf << argv[i]; |
3014
|
883 |
2875
|
884 if (sym_rec->is_user_variable ()) |
|
885 output_buf << " is a user-defined variable\n"; |
|
886 else |
|
887 output_buf << " is a built-in variable\n"; |
|
888 } |
1288
|
889 else |
2875
|
890 { |
|
891 if (! elts.empty ()) |
|
892 output_buf << "type: structure `" << id |
|
893 << "' has no member `" << elts |
|
894 << "'\n"; |
|
895 else |
|
896 output_buf << "type: `" << id |
|
897 << "' has unknown type!\n"; |
|
898 } |
1288
|
899 } |
3164
|
900 |
|
901 defn.print_raw (output_buf, true); |
|
902 |
|
903 if (nargout == 0) |
|
904 output_buf << "\n"; |
1281
|
905 } |
625
|
906 } |
581
|
907 else |
3164
|
908 error ("type: `%s' has unknown type!", argv[i].c_str ()); |
581
|
909 } |
|
910 else |
3141
|
911 { |
|
912 string ff = fcn_file_in_path (argv[i]); |
|
913 |
|
914 if (! ff.empty ()) |
|
915 { |
|
916 ifstream fs (ff.c_str (), ios::in); |
|
917 |
|
918 if (fs) |
|
919 { |
|
920 if (nargout == 0 && ! quiet) |
|
921 output_buf << argv[i] << " is the script file: " |
|
922 << ff << "\n\n"; |
|
923 |
|
924 char ch; |
|
925 |
|
926 while (fs.get (ch)) |
|
927 output_buf << ch; |
|
928 } |
|
929 else |
|
930 output_buf << "unable to open `" << ff |
|
931 << "' for reading!\n"; |
|
932 } |
|
933 else |
3164
|
934 error ("type: `%s' undefined", argv[i].c_str ()); |
3141
|
935 } |
581
|
936 } |
|
937 |
|
938 output_buf << ends; |
|
939 |
2095
|
940 char *s = output_buf.str (); |
|
941 |
581
|
942 if (nargout == 0) |
2095
|
943 octave_stdout << s; |
581
|
944 else |
2095
|
945 retval = s; |
|
946 |
|
947 delete [] s; |
581
|
948 } |
|
949 else |
|
950 print_usage ("type"); |
|
951 |
|
952 return retval; |
|
953 } |
|
954 |
1957
|
955 DEFUN_TEXT (which, args, nargout, |
1565
|
956 "which NAME ...\n\ |
581
|
957 \n\ |
|
958 display the type of each NAME. If NAME is defined from an function\n\ |
|
959 file, print the full name of the file.") |
|
960 { |
2086
|
961 octave_value_list retval; |
581
|
962 |
1755
|
963 int argc = args.length () + 1; |
|
964 |
1968
|
965 string_vector argv = args.make_argv ("which"); |
1755
|
966 |
|
967 if (error_state) |
|
968 return retval; |
581
|
969 |
|
970 if (argc > 1) |
|
971 { |
|
972 if (nargout > 0) |
|
973 retval.resize (argc-1, Matrix ()); |
|
974 |
1755
|
975 for (int i = 1; i < argc; i++) |
581
|
976 { |
1755
|
977 symbol_record *sym_rec = lookup_by_name (argv[i], 0); |
581
|
978 |
3141
|
979 if (sym_rec && sym_rec->is_defined ()) |
581
|
980 { |
742
|
981 int print = (nargout == 0); |
3014
|
982 |
2095
|
983 string tmp = print_symbol_type (octave_stdout, sym_rec, |
1755
|
984 argv[i], print); |
742
|
985 if (! print) |
|
986 retval(i) = tmp; |
581
|
987 } |
|
988 else |
|
989 { |
3141
|
990 string path = fcn_file_in_path (argv[i]); |
|
991 |
|
992 if (! path.empty ()) |
|
993 { |
|
994 if (nargout == 0) |
|
995 octave_stdout << "which: `" << argv[i] |
|
996 << "' is the script file: " |
|
997 << path << "\n"; |
|
998 else |
|
999 retval(i) = path; |
|
1000 } |
581
|
1001 else |
3141
|
1002 { |
|
1003 if (nargout == 0) |
|
1004 octave_stdout << "which: `" << argv[i] |
|
1005 << "' is undefined\n"; |
|
1006 else |
|
1007 retval(i) = "undefined"; |
|
1008 } |
581
|
1009 } |
|
1010 } |
|
1011 } |
|
1012 else |
|
1013 print_usage ("which"); |
|
1014 |
|
1015 return retval; |
|
1016 } |
|
1017 |
2189
|
1018 static int |
2202
|
1019 info_file (void) |
|
1020 { |
|
1021 int status = 0; |
|
1022 |
|
1023 string s = builtin_string_variable ("INFO_FILE"); |
|
1024 |
|
1025 if (s.empty ()) |
|
1026 { |
|
1027 gripe_invalid_value_specified ("INFO_FILE"); |
|
1028 status = -1; |
|
1029 } |
|
1030 else |
|
1031 Vinfo_file = s; |
|
1032 |
|
1033 return status; |
|
1034 } |
|
1035 |
|
1036 static int |
|
1037 info_prog (void) |
|
1038 { |
|
1039 int status = 0; |
|
1040 |
|
1041 string s = builtin_string_variable ("INFO_PROGRAM"); |
|
1042 |
|
1043 if (s.empty ()) |
|
1044 { |
|
1045 gripe_invalid_value_specified ("INFO_PROGRAM"); |
|
1046 status = -1; |
|
1047 } |
|
1048 else |
|
1049 Vinfo_prog = s; |
|
1050 |
|
1051 return status; |
|
1052 } |
|
1053 |
3014
|
1054 static int |
|
1055 suppress_verbose_help_message (void) |
|
1056 { |
|
1057 Vsuppress_verbose_help_message |
|
1058 = check_preference ("suppress_verbose_help_message"); |
|
1059 |
|
1060 return 0; |
|
1061 } |
|
1062 |
2189
|
1063 void |
|
1064 symbols_of_help (void) |
|
1065 { |
3258
|
1066 DEFVAR (INFO_FILE, Vinfo_file, info_file, |
2202
|
1067 "name of the Octave info file"); |
|
1068 |
3258
|
1069 DEFVAR (INFO_PROGRAM, Vinfo_prog, info_prog, |
2202
|
1070 "name of the Octave info reader"); |
|
1071 |
3258
|
1072 DEFVAR (suppress_verbose_help_message, 0.0, suppress_verbose_help_message, |
2189
|
1073 "suppress printing of message pointing to additional help in the\n\ |
|
1074 help and usage functions"); |
|
1075 } |
|
1076 |
1
|
1077 /* |
|
1078 ;;; Local Variables: *** |
|
1079 ;;; mode: C++ *** |
|
1080 ;;; End: *** |
|
1081 */ |