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