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