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