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" |
1466
|
58 #include "pathsearch.h" |
2124
|
59 #include "pt-pr-code.h" |
529
|
60 #include "sighandlers.h" |
|
61 #include "symtab.h" |
2694
|
62 #include "syswait.h" |
1755
|
63 #include "toplev.h" |
1588
|
64 #include "unwind-prot.h" |
242
|
65 #include "utils.h" |
1352
|
66 #include "variables.h" |
529
|
67 |
2202
|
68 // Name of the info file specified on command line. |
|
69 // (--info-file file) |
|
70 string Vinfo_file; |
|
71 |
|
72 // Name of the info reader we'd like to use. |
|
73 // (--info-program program) |
|
74 string Vinfo_prog; |
|
75 |
2189
|
76 // If TRUE, don't print additional help message in help and usage |
|
77 // functions. |
|
78 static bool Vsuppress_verbose_help_message; |
|
79 |
3016
|
80 // XXX FIXME XXX -- maybe this should use string instead of char*. |
|
81 |
|
82 struct help_list |
|
83 { |
|
84 const char *name; |
|
85 const char *help; |
|
86 }; |
|
87 |
1
|
88 static help_list operators[] = |
|
89 { |
|
90 { "!", |
|
91 "Logical not operator. See also `~'.\n", }, |
|
92 |
|
93 { "!=", |
|
94 "Logical not equals operator. See also `~' and `<>'.\n", }, |
|
95 |
|
96 { "\"", |
|
97 "String delimiter.\n", }, |
|
98 |
|
99 { "#", |
928
|
100 "Begin comment character. See also `%'.", }, |
1
|
101 |
|
102 { "%", |
928
|
103 "Begin comment charcter. See also `#'.", }, |
1
|
104 |
|
105 { "&", |
928
|
106 "Logical and operator. See also `&&'.", }, |
1
|
107 |
|
108 { "&&", |
928
|
109 "Logical and operator. See also `&'.", }, |
1
|
110 |
|
111 { "'", |
|
112 "Matrix transpose operator. For complex matrices, computes the\n\ |
|
113 complex conjugate (Hermitian) transpose. See also `.''\n\ |
|
114 \n\ |
|
115 The single quote character may also be used to delimit strings, but\n\ |
|
116 it is better to use the double quote character, since that is never\n\ |
928
|
117 ambiguous", }, |
1
|
118 |
|
119 { "(", |
928
|
120 "Array index or function argument delimiter.", }, |
1
|
121 |
|
122 { ")", |
928
|
123 "Array index or function argument delimiter.", }, |
1
|
124 |
|
125 { "*", |
928
|
126 "Multiplication operator. See also `.*'", }, |
1
|
127 |
|
128 { "**", |
928
|
129 "Power operator. See also `^', `.**', and `.^'", }, |
1
|
130 |
|
131 { "+", |
928
|
132 "Addition operator.", }, |
1
|
133 |
|
134 { "++", |
928
|
135 "Increment operator. As in C, may be applied as a prefix or postfix operator.", }, |
1
|
136 |
|
137 { ",", |
928
|
138 "Array index, function argument, or command separator.", }, |
1
|
139 |
|
140 { "-", |
928
|
141 "Subtraction or unary negation operator.", }, |
1
|
142 |
|
143 { "--", |
928
|
144 "Decrement operator. As in C, may be applied as a prefix or postfix operator.", }, |
1
|
145 |
|
146 { ".'", |
|
147 "Matrix transpose operator. For complex matrices, computes the\n\ |
928
|
148 transpose, *not* the complex conjugate transpose. See also `''.", }, |
1
|
149 |
|
150 { ".*", |
928
|
151 "Element by element multiplication operator. See also `*'.", }, |
1
|
152 |
|
153 { ".**", |
928
|
154 "Element by element power operator. See also `**', `^', and `.^'.", }, |
1
|
155 |
|
156 { "./", |
928
|
157 "Element by element division operator. See also `/' and `\\'.", }, |
1
|
158 |
|
159 { ".^", |
928
|
160 "Element by element power operator. See also `**', `^', and `.^'.", }, |
1
|
161 |
|
162 { "/", |
928
|
163 "Right division. See also `\\' and `./'.", }, |
1
|
164 |
|
165 { ":", |
928
|
166 "Select entire rows or columns of matrices.", }, |
1
|
167 |
|
168 { ";", |
928
|
169 "Array row or command separator. See also `,'.", }, |
1
|
170 |
|
171 { "<", |
928
|
172 "Less than operator.", }, |
1
|
173 |
|
174 { "<=", |
928
|
175 "Less than or equals operator.", }, |
1
|
176 |
|
177 { "<>", |
928
|
178 "Logical not equals operator. See also `!=' and `~='.", }, |
1
|
179 |
|
180 { "=", |
928
|
181 "Assignment operator.", }, |
1
|
182 |
|
183 { "==", |
928
|
184 "Equality test operator.", }, |
1
|
185 |
|
186 { ">", |
928
|
187 "Greater than operator.", }, |
1
|
188 |
|
189 { ">=", |
928
|
190 "Greater than or equals operator.", }, |
1
|
191 |
|
192 { "[", |
928
|
193 "Return list delimiter. See also `]'.", }, |
1
|
194 |
|
195 { "\\", |
928
|
196 "Left division operator. See also `/' and `./'.", }, |
1
|
197 |
|
198 { "]", |
928
|
199 "Return list delimiter. See also `['.", }, |
1
|
200 |
|
201 { "^", |
928
|
202 "Power operator. See also `**', `.^', and `.**.'", }, |
1
|
203 |
|
204 { "|", |
928
|
205 "Logical or operator. See also `||'.", }, |
1
|
206 |
|
207 { "||", |
928
|
208 "Logical or operator. See also `|'.", }, |
1
|
209 |
|
210 { "~", |
928
|
211 "Logical not operator. See also `!' and `~'.", }, |
1
|
212 |
|
213 { "~=", |
928
|
214 "Logical not equals operator. See also `<>' and `!='.", }, |
1
|
215 |
529
|
216 { 0, 0, }, |
1
|
217 }; |
|
218 |
|
219 static help_list keywords[] = |
|
220 { |
928
|
221 { "all_va_args", |
|
222 "Pass all unnamed arguments to another function call.", }, |
|
223 |
1
|
224 { "break", |
928
|
225 "Exit the innermost enclosing while or for loop.", }, |
1
|
226 |
1489
|
227 { "catch", |
|
228 "begin the cleanup part of a try-catch block", }, |
|
229 |
1
|
230 { "continue", |
928
|
231 "Jump to the end of the innermost enclosing while or for loop.", }, |
1
|
232 |
|
233 { "else", |
928
|
234 "Alternate action for an if block.", }, |
1
|
235 |
|
236 { "elseif", |
928
|
237 "Alternate conditional test for an if block.", }, |
1
|
238 |
|
239 { "end", |
928
|
240 "Mark the end of any for, if, while, or function block.", }, |
|
241 |
1489
|
242 { "end_try_catch", |
|
243 "Mark the end of an try-catch block.", }, |
|
244 |
928
|
245 { "end_unwind_protect", |
|
246 "Mark the end of an unwind_protect block.", }, |
1
|
247 |
|
248 { "endfor", |
928
|
249 "Mark the end of a for loop.", }, |
1
|
250 |
|
251 { "endfunction", |
928
|
252 "Mark the end of a function.", }, |
1
|
253 |
|
254 { "endif", |
928
|
255 "Mark the end of an if block.", }, |
1
|
256 |
|
257 { "endwhile", |
928
|
258 "Mark the end of a while loop.", }, |
1
|
259 |
|
260 { "for", |
928
|
261 "Begin a for loop.", }, |
1
|
262 |
|
263 { "function", |
928
|
264 "Begin a function body.", }, |
1
|
265 |
|
266 { "global", |
928
|
267 "Declare variables to have global scope.", }, |
1
|
268 |
|
269 { "gplot", |
928
|
270 "Produce 2-D plots using gnuplot-like command syntax.", }, |
1
|
271 |
|
272 { "gsplot", |
928
|
273 "Produce 3-D plots using gnuplot-like command syntax.", }, |
1
|
274 |
|
275 { "if", |
928
|
276 "Begin an if block.", }, |
1
|
277 |
|
278 { "return", |
928
|
279 "Return from a function.", }, |
|
280 |
1489
|
281 { "try", |
|
282 "Begin a try-catch block.", }, |
|
283 |
928
|
284 { "unwind_protect", |
|
285 "Begin an unwind_protect block.", }, |
|
286 |
|
287 { "unwind_protect_cleanup", |
|
288 "Begin the cleanup section of an unwind_protect block.", }, |
1
|
289 |
|
290 { "while", |
928
|
291 "Begin a while loop.", }, |
1
|
292 |
529
|
293 { 0, 0, }, |
1
|
294 }; |
|
295 |
581
|
296 // Return a copy of the operator or keyword names. |
|
297 |
3016
|
298 static string_vector |
1
|
299 names (help_list *lst, int& count) |
|
300 { |
1755
|
301 string_vector retval; |
|
302 |
1
|
303 count = 0; |
|
304 help_list *ptr = lst; |
529
|
305 while (ptr->name) |
1
|
306 { |
|
307 count++; |
|
308 ptr++; |
|
309 } |
|
310 |
1755
|
311 if (count > 0) |
|
312 { |
|
313 retval.resize (count); |
1
|
314 |
1755
|
315 ptr = lst; |
|
316 for (int i = 0; i < count; i++) |
|
317 { |
|
318 retval[i] = ptr->name; |
|
319 ptr++; |
|
320 } |
1
|
321 } |
|
322 |
1755
|
323 return retval; |
1
|
324 } |
|
325 |
3014
|
326 static help_list * |
1
|
327 operator_help (void) |
|
328 { |
|
329 return operators; |
|
330 } |
|
331 |
3016
|
332 static help_list * |
1
|
333 keyword_help (void) |
|
334 { |
|
335 return keywords; |
|
336 } |
|
337 |
3016
|
338 // It's not likely that this does the right thing now. XXX FIXME XXX |
|
339 |
|
340 string_vector |
|
341 make_name_list (void) |
|
342 { |
|
343 int key_len = 0; |
|
344 int glb_len = 0; |
|
345 int top_len = 0; |
|
346 int lcl_len = 0; |
|
347 |
|
348 string_vector key; |
|
349 string_vector glb; |
|
350 string_vector top; |
|
351 string_vector lcl; |
|
352 string_vector ffl; |
|
353 |
|
354 // Each of these functions returns a new vector of pointers to new |
|
355 // strings. |
|
356 |
|
357 key = names (keyword_help (), key_len); |
|
358 |
|
359 glb = global_sym_tab->name_list (glb_len); |
|
360 |
|
361 top = top_level_sym_tab->name_list (top_len); |
|
362 |
|
363 if (top_level_sym_tab != curr_sym_tab) |
|
364 lcl = curr_sym_tab->name_list (lcl_len); |
|
365 |
|
366 ffl = octave_fcn_file_name_cache::list_no_suffix (); |
|
367 int ffl_len = ffl.length (); |
|
368 |
|
369 int total_len = key_len + glb_len + top_len + lcl_len + ffl_len; |
|
370 |
|
371 string_vector list (total_len); |
|
372 |
|
373 // Put all the symbols in one big list. |
|
374 |
|
375 int j = 0; |
|
376 int i = 0; |
|
377 for (i = 0; i < key_len; i++) |
|
378 list[j++] = key[i]; |
|
379 |
|
380 for (i = 0; i < glb_len; i++) |
|
381 list[j++] = glb[i]; |
|
382 |
|
383 for (i = 0; i < top_len; i++) |
|
384 list[j++] = top[i]; |
|
385 |
|
386 for (i = 0; i < lcl_len; i++) |
|
387 list[j++] = lcl[i]; |
|
388 |
|
389 for (i = 0; i < ffl_len; i++) |
|
390 list[j++] = ffl[i]; |
|
391 |
|
392 return list; |
|
393 } |
|
394 |
2470
|
395 #if defined (USE_GNU_INFO) |
3014
|
396 |
|
397 void |
2470
|
398 additional_help_message (ostream& os) |
|
399 { |
|
400 if (! Vsuppress_verbose_help_message) |
|
401 os << "\n\ |
1115
|
402 Additional help for builtin functions, operators, and variables\n\ |
|
403 is available in the on-line version of the manual.\n\ |
|
404 \n\ |
2470
|
405 Use the command `help -i <topic>' to search the manual index.\n"; |
|
406 } |
3014
|
407 |
2470
|
408 #else |
3014
|
409 |
|
410 void |
2470
|
411 additional_help_message (ostream&) |
542
|
412 { |
2470
|
413 } |
3014
|
414 |
1140
|
415 #endif |
542
|
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 |
|
455 << " is the function defined from:\n" |
|
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 { |
|
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 |
2202
|
555 dir_path p (Vload_path); |
529
|
556 |
1787
|
557 string_vector dirs = p.all_directories (); |
679
|
558 |
1787
|
559 int len = dirs.length (); |
679
|
560 |
1787
|
561 for (int i = 0; i < len; i++) |
|
562 { |
2233
|
563 string_vector names = octave_fcn_file_name_cache::list (dirs[i]); |
679
|
564 |
1787
|
565 if (! names.empty ()) |
|
566 { |
2926
|
567 string dir |
|
568 = octave_env::make_absolute (dirs[i], octave_env::getcwd ()); |
|
569 |
|
570 octave_stdout << "\n*** function files in " << dir << ":\n\n"; |
679
|
571 |
2095
|
572 names.list_in_columns (octave_stdout); |
529
|
573 } |
|
574 } |
|
575 |
2095
|
576 additional_help_message (octave_stdout); |
529
|
577 } |
|
578 |
2470
|
579 #if defined (USE_GNU_INFO) |
3014
|
580 |
529
|
581 static int |
1755
|
582 try_info (const string& nm) |
529
|
583 { |
|
584 int status = 0; |
|
585 |
1295
|
586 static char *cmd_str = 0; |
|
587 |
|
588 delete [] cmd_str; |
|
589 cmd_str = 0; |
|
590 |
|
591 ostrstream cmd_buf; |
|
592 |
2202
|
593 cmd_buf << Vinfo_prog << " --file " << Vinfo_file; |
1295
|
594 |
2202
|
595 string directory_name = Vinfo_file; |
1755
|
596 size_t pos = directory_name.rfind ('/'); |
|
597 |
|
598 if (pos != NPOS) |
529
|
599 { |
1755
|
600 directory_name.resize (pos + 1); |
1295
|
601 cmd_buf << " --directory " << directory_name; |
529
|
602 } |
|
603 |
1755
|
604 if (nm.length () > 0) |
|
605 cmd_buf << " --index-search " << nm; |
529
|
606 |
1295
|
607 cmd_buf << ends; |
529
|
608 |
1295
|
609 cmd_str = cmd_buf.str (); |
529
|
610 |
2705
|
611 volatile octave_interrupt_handler old_interrupt_handler |
2554
|
612 = octave_ignore_interrupts (); |
529
|
613 |
1295
|
614 status = system (cmd_str); |
529
|
615 |
2554
|
616 octave_set_interrupt_handler (old_interrupt_handler); |
529
|
617 |
2694
|
618 if (WIFEXITED (status)) |
|
619 status = WEXITSTATUS (status); |
1295
|
620 else |
|
621 status = 127; |
529
|
622 |
|
623 return status; |
|
624 } |
1140
|
625 |
|
626 static void |
1755
|
627 help_from_info (const string_vector& argv, int idx, int argc) |
1140
|
628 { |
1755
|
629 if (idx == argc) |
|
630 try_info (string ()); |
1140
|
631 else |
|
632 { |
1755
|
633 for (int i = idx; i < argc; i++) |
1140
|
634 { |
1755
|
635 int status = try_info (argv[i]); |
1140
|
636 |
1295
|
637 if (status) |
1140
|
638 { |
1295
|
639 if (status < 0) |
|
640 { |
3014
|
641 message ("help", "sorry, `%s' is not indexed in the manual", |
1755
|
642 argv[i].c_str ()); |
1295
|
643 sleep (2); |
|
644 } |
|
645 else |
|
646 { |
|
647 error ("help: unable to find info!"); |
|
648 break; |
|
649 } |
1140
|
650 } |
|
651 } |
|
652 } |
2470
|
653 } |
3014
|
654 |
1140
|
655 #else |
3014
|
656 |
2470
|
657 static void |
|
658 help_from_info (const string_vector&, int, int) |
|
659 { |
3014
|
660 message ("help", "Info help is not available in this version of Octave"); |
2470
|
661 } |
3014
|
662 |
1140
|
663 #endif |
529
|
664 |
3014
|
665 static bool |
2095
|
666 help_from_list (ostream& os, const help_list *list, |
1755
|
667 const string& nm, int usage) |
542
|
668 { |
2804
|
669 const char *name; |
3014
|
670 |
542
|
671 while ((name = list->name) != 0) |
|
672 { |
1755
|
673 if (strcmp (name, nm.c_str ()) == 0) |
542
|
674 { |
|
675 if (usage) |
2095
|
676 os << "\nusage: "; |
542
|
677 else |
|
678 { |
2095
|
679 os << "\n*** " << nm << ":\n\n"; |
542
|
680 } |
|
681 |
2095
|
682 os << list->help << "\n"; |
542
|
683 |
3014
|
684 return true; |
542
|
685 } |
|
686 list++; |
|
687 } |
3014
|
688 |
|
689 return false; |
542
|
690 } |
|
691 |
1140
|
692 static void |
1755
|
693 builtin_help (int argc, const string_vector& argv) |
1140
|
694 { |
|
695 help_list *op_help_list = operator_help (); |
|
696 help_list *kw_help_list = keyword_help (); |
|
697 |
1755
|
698 for (int i = 1; i < argc; i++) |
1140
|
699 { |
2095
|
700 if (help_from_list (octave_stdout, op_help_list, argv[i], 0)) |
1140
|
701 continue; |
|
702 |
2095
|
703 if (help_from_list (octave_stdout, kw_help_list, argv[i], 0)) |
1140
|
704 continue; |
|
705 |
1755
|
706 symbol_record *sym_rec = lookup_by_name (argv[i], 0); |
1140
|
707 |
|
708 if (sym_rec && sym_rec->is_defined ()) |
|
709 { |
1755
|
710 string h = sym_rec->help (); |
|
711 |
|
712 if (h.length () > 0) |
1140
|
713 { |
2095
|
714 print_symbol_type (octave_stdout, sym_rec, argv[i], 1); |
|
715 octave_stdout << "\n" << h << "\n"; |
1140
|
716 continue; |
|
717 } |
|
718 } |
|
719 |
1755
|
720 string path = fcn_file_in_path (argv[i]); |
|
721 |
|
722 string h = get_help_from_file (path); |
|
723 |
|
724 if (! h.empty ()) |
1140
|
725 { |
2095
|
726 octave_stdout << argv[i] << " is the file:\n" |
1140
|
727 << path << "\n\n" << h << "\n"; |
1755
|
728 |
1140
|
729 continue; |
|
730 } |
|
731 |
2095
|
732 octave_stdout << "\nhelp: sorry, `" << argv[i] |
|
733 << "' is not documented\n"; |
1140
|
734 } |
|
735 |
2095
|
736 additional_help_message (octave_stdout); |
1140
|
737 } |
|
738 |
2470
|
739 #if defined (USE_GNU_INFO) |
3014
|
740 #define HELP_DOC_STRING \ |
|
741 "help [-i] [topic ...]\n\nprint cryptic yet witty messages" |
1140
|
742 #else |
3014
|
743 #define HELP_DOC_STRING \ |
|
744 "help [topic ...]\n\nprint cryptic yet witty messages" |
|
745 #endif |
|
746 |
1957
|
747 DEFUN_TEXT (help, args, , |
3014
|
748 HELP_DOC_STRING) |
529
|
749 { |
2086
|
750 octave_value_list retval; |
529
|
751 |
1755
|
752 int argc = args.length () + 1; |
|
753 |
1968
|
754 string_vector argv = args.make_argv ("help"); |
1755
|
755 |
|
756 if (error_state) |
|
757 return retval; |
529
|
758 |
|
759 if (argc == 1) |
3014
|
760 simple_help (); |
529
|
761 else |
|
762 { |
1755
|
763 if (argv[1] == "-i") |
3014
|
764 help_from_info (argv, 2, argc); |
529
|
765 else |
3014
|
766 builtin_help (argc, argv); |
529
|
767 } |
|
768 |
|
769 return retval; |
|
770 } |
|
771 |
1957
|
772 DEFUN_TEXT (type, args, nargout, |
2962
|
773 "type NAME\n\ |
581
|
774 \n\ |
|
775 display the definition of each NAME that refers to a function") |
|
776 { |
2086
|
777 octave_value_list retval; |
581
|
778 |
2985
|
779 unwind_protect::begin_frame ("Ftype"); |
1588
|
780 |
1755
|
781 int argc = args.length () + 1; |
|
782 |
1968
|
783 string_vector argv = args.make_argv ("type"); |
1755
|
784 |
|
785 if (error_state) |
|
786 return retval; |
581
|
787 |
|
788 if (argc > 1) |
|
789 { |
1358
|
790 // XXX FIXME XXX -- we should really use getopt () |
|
791 |
2532
|
792 bool quiet = false; |
|
793 bool pr_orig_txt = true; |
|
794 |
|
795 int idx; |
|
796 |
|
797 for (idx = 1; idx < argc; idx++) |
581
|
798 { |
2532
|
799 if (argv[idx] == "-q" || argv[idx] == "-quiet") |
|
800 quiet = true; |
|
801 else if (argv[idx] == "-t" || argv[idx] == "-transformed") |
|
802 pr_orig_txt = false; |
|
803 else |
|
804 break; |
|
805 } |
|
806 |
|
807 if (idx == argc) |
|
808 { |
|
809 print_usage ("type"); |
|
810 return retval; |
581
|
811 } |
|
812 |
|
813 ostrstream output_buf; |
|
814 |
1755
|
815 for (int i = idx; i < argc; i++) |
581
|
816 { |
1755
|
817 string id = argv[i]; |
|
818 string elts; |
581
|
819 |
1755
|
820 if (id[id.length () - 1] != '.') |
1281
|
821 { |
1755
|
822 size_t pos = id.find ('.'); |
|
823 |
|
824 if (pos != NPOS) |
1288
|
825 { |
1889
|
826 elts = id.substr (pos+1); |
1755
|
827 id = id.substr (0, pos); |
1288
|
828 } |
1281
|
829 } |
|
830 |
|
831 symbol_record *sym_rec = lookup_by_name (id, 0); |
581
|
832 |
|
833 if (sym_rec) |
|
834 { |
|
835 if (sym_rec->is_user_function ()) |
|
836 { |
2976
|
837 octave_value tmp = sym_rec->def (); |
|
838 |
|
839 octave_function *defn = tmp.function_value (); |
2962
|
840 |
2976
|
841 string fn = defn ? defn->fcn_file_name () : string (); |
581
|
842 |
2976
|
843 string ff = fn.empty () ? string () : fcn_file_in_path (fn); |
2534
|
844 |
|
845 if (pr_orig_txt && ! ff.empty ()) |
|
846 { |
|
847 ifstream fs (ff.c_str (), ios::in); |
|
848 |
|
849 if (fs) |
|
850 { |
|
851 if (nargout == 0 && ! quiet) |
|
852 output_buf << argv[i] |
|
853 << " is the function defined from:\n" |
|
854 << ff << "\n\n"; |
|
855 |
|
856 char ch; |
581
|
857 |
2534
|
858 while (fs.get (ch)) |
|
859 output_buf << ch; |
|
860 } |
|
861 else |
|
862 output_buf << "unable to open `" << ff |
|
863 << "' for reading!\n"; |
|
864 } |
|
865 else |
|
866 { |
|
867 if (nargout == 0 && ! quiet) |
|
868 output_buf << argv[i] |
|
869 << " is a user-defined function:\n\n"; |
2124
|
870 |
2534
|
871 tree_print_code tpc (output_buf, "", pr_orig_txt); |
|
872 |
2962
|
873 defn->accept (tpc); |
2534
|
874 } |
581
|
875 } |
|
876 |
1358
|
877 // XXX FIXME XXX -- this code should be shared with |
|
878 // Fwhich. |
581
|
879 |
|
880 else if (sym_rec->is_text_function ()) |
1755
|
881 output_buf << argv[i] << " is a builtin text-function\n"; |
581
|
882 else if (sym_rec->is_builtin_function ()) |
1755
|
883 output_buf << argv[i] << " is a builtin function\n"; |
1281
|
884 else if (sym_rec->is_user_variable () |
|
885 || sym_rec->is_builtin_variable ()) |
625
|
886 { |
2976
|
887 octave_value defn = sym_rec->def (); |
625
|
888 |
2976
|
889 assert (defn.is_constant ()); |
1281
|
890 |
|
891 int var_ok = 1; |
2875
|
892 |
|
893 // XXX FIXME XXX -- need to handle structure |
|
894 // references correctly. |
|
895 |
2976
|
896 if (defn.is_map ()) |
|
897 error ("type: operations on structs not implemented"); |
1288
|
898 |
2875
|
899 if (! error_state) |
1281
|
900 { |
2875
|
901 if (nargout == 0 && ! quiet) |
1288
|
902 { |
2875
|
903 if (var_ok) |
|
904 { |
|
905 output_buf << argv[i]; |
3014
|
906 |
2875
|
907 if (sym_rec->is_user_variable ()) |
|
908 output_buf << " is a user-defined variable\n"; |
|
909 else |
|
910 output_buf << " is a built-in variable\n"; |
|
911 } |
1288
|
912 else |
2875
|
913 { |
|
914 if (! elts.empty ()) |
|
915 output_buf << "type: structure `" << id |
|
916 << "' has no member `" << elts |
|
917 << "'\n"; |
|
918 else |
|
919 output_buf << "type: `" << id |
|
920 << "' has unknown type!\n"; |
|
921 } |
1288
|
922 } |
1281
|
923 } |
625
|
924 } |
581
|
925 else |
1755
|
926 output_buf << "type: `" << argv[i] << "' has unknown type!\n"; |
581
|
927 } |
|
928 else |
1755
|
929 output_buf << "type: `" << argv[i] << "' undefined\n"; |
581
|
930 } |
|
931 |
|
932 output_buf << ends; |
|
933 |
2095
|
934 char *s = output_buf.str (); |
|
935 |
581
|
936 if (nargout == 0) |
2095
|
937 octave_stdout << s; |
581
|
938 else |
2095
|
939 retval = s; |
|
940 |
|
941 delete [] s; |
581
|
942 } |
|
943 else |
|
944 print_usage ("type"); |
|
945 |
|
946 return retval; |
|
947 } |
|
948 |
1957
|
949 DEFUN_TEXT (which, args, nargout, |
1565
|
950 "which NAME ...\n\ |
581
|
951 \n\ |
|
952 display the type of each NAME. If NAME is defined from an function\n\ |
|
953 file, print the full name of the file.") |
|
954 { |
2086
|
955 octave_value_list retval; |
581
|
956 |
1755
|
957 int argc = args.length () + 1; |
|
958 |
1968
|
959 string_vector argv = args.make_argv ("which"); |
1755
|
960 |
|
961 if (error_state) |
|
962 return retval; |
581
|
963 |
|
964 if (argc > 1) |
|
965 { |
|
966 if (nargout > 0) |
|
967 retval.resize (argc-1, Matrix ()); |
|
968 |
1755
|
969 for (int i = 1; i < argc; i++) |
581
|
970 { |
1755
|
971 symbol_record *sym_rec = lookup_by_name (argv[i], 0); |
581
|
972 |
|
973 if (sym_rec) |
|
974 { |
742
|
975 int print = (nargout == 0); |
3014
|
976 |
2095
|
977 string tmp = print_symbol_type (octave_stdout, sym_rec, |
1755
|
978 argv[i], print); |
742
|
979 if (! print) |
|
980 retval(i) = tmp; |
581
|
981 } |
|
982 else |
|
983 { |
|
984 if (nargout == 0) |
2095
|
985 octave_stdout << "which: `" << argv[i] << "' is undefined\n"; |
581
|
986 else |
|
987 retval(i) = "undefined"; |
|
988 } |
|
989 } |
|
990 } |
|
991 else |
|
992 print_usage ("which"); |
|
993 |
|
994 return retval; |
|
995 } |
|
996 |
2189
|
997 static int |
2202
|
998 info_file (void) |
|
999 { |
|
1000 int status = 0; |
|
1001 |
|
1002 string s = builtin_string_variable ("INFO_FILE"); |
|
1003 |
|
1004 if (s.empty ()) |
|
1005 { |
|
1006 gripe_invalid_value_specified ("INFO_FILE"); |
|
1007 status = -1; |
|
1008 } |
|
1009 else |
|
1010 Vinfo_file = s; |
|
1011 |
|
1012 return status; |
|
1013 } |
|
1014 |
|
1015 static int |
|
1016 info_prog (void) |
|
1017 { |
|
1018 int status = 0; |
|
1019 |
|
1020 string s = builtin_string_variable ("INFO_PROGRAM"); |
|
1021 |
|
1022 if (s.empty ()) |
|
1023 { |
|
1024 gripe_invalid_value_specified ("INFO_PROGRAM"); |
|
1025 status = -1; |
|
1026 } |
|
1027 else |
|
1028 Vinfo_prog = s; |
|
1029 |
|
1030 return status; |
|
1031 } |
|
1032 |
3014
|
1033 static int |
|
1034 suppress_verbose_help_message (void) |
|
1035 { |
|
1036 Vsuppress_verbose_help_message |
|
1037 = check_preference ("suppress_verbose_help_message"); |
|
1038 |
|
1039 return 0; |
|
1040 } |
|
1041 |
2189
|
1042 void |
|
1043 symbols_of_help (void) |
|
1044 { |
2202
|
1045 DEFVAR (INFO_FILE, Vinfo_file, 0, info_file, |
|
1046 "name of the Octave info file"); |
|
1047 |
|
1048 DEFVAR (INFO_PROGRAM, Vinfo_prog, 0, info_prog, |
|
1049 "name of the Octave info reader"); |
|
1050 |
2189
|
1051 DEFVAR (suppress_verbose_help_message, 0.0, 0, suppress_verbose_help_message, |
|
1052 "suppress printing of message pointing to additional help in the\n\ |
|
1053 help and usage functions"); |
|
1054 } |
|
1055 |
1
|
1056 /* |
|
1057 ;;; Local Variables: *** |
|
1058 ;;; mode: C++ *** |
|
1059 ;;; End: *** |
|
1060 */ |