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