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 |
3295
|
44 #include "cmd-edit.h" |
|
45 #include "file-ops.h" |
2926
|
46 #include "oct-env.h" |
1755
|
47 #include "str-vec.h" |
|
48 |
2492
|
49 #include <defaults.h> |
1352
|
50 #include "defun.h" |
|
51 #include "dirfns.h" |
|
52 #include "error.h" |
2233
|
53 #include "fn-cache.h" |
2202
|
54 #include "gripes.h" |
1352
|
55 #include "help.h" |
2177
|
56 #include "input.h" |
1755
|
57 #include "oct-obj.h" |
2976
|
58 #include "ov-usr-fcn.h" |
1352
|
59 #include "pager.h" |
3018
|
60 #include "parse.h" |
1466
|
61 #include "pathsearch.h" |
3295
|
62 #include "procstream.h" |
2124
|
63 #include "pt-pr-code.h" |
529
|
64 #include "sighandlers.h" |
|
65 #include "symtab.h" |
2694
|
66 #include "syswait.h" |
1755
|
67 #include "toplev.h" |
1588
|
68 #include "unwind-prot.h" |
242
|
69 #include "utils.h" |
1352
|
70 #include "variables.h" |
3301
|
71 #include "version.h" |
529
|
72 |
2202
|
73 // Name of the info file specified on command line. |
|
74 // (--info-file file) |
|
75 string Vinfo_file; |
|
76 |
|
77 // Name of the info reader we'd like to use. |
|
78 // (--info-program program) |
|
79 string Vinfo_prog; |
|
80 |
2189
|
81 // If TRUE, don't print additional help message in help and usage |
|
82 // functions. |
|
83 static bool Vsuppress_verbose_help_message; |
|
84 |
3016
|
85 // XXX FIXME XXX -- maybe this should use string instead of char*. |
|
86 |
|
87 struct help_list |
|
88 { |
|
89 const char *name; |
|
90 const char *help; |
|
91 }; |
|
92 |
1
|
93 static help_list operators[] = |
|
94 { |
|
95 { "!", |
|
96 "Logical not operator. See also `~'.\n", }, |
|
97 |
|
98 { "!=", |
|
99 "Logical not equals operator. See also `~' and `<>'.\n", }, |
|
100 |
|
101 { "\"", |
|
102 "String delimiter.\n", }, |
|
103 |
|
104 { "#", |
928
|
105 "Begin comment character. See also `%'.", }, |
1
|
106 |
|
107 { "%", |
928
|
108 "Begin comment charcter. See also `#'.", }, |
1
|
109 |
|
110 { "&", |
928
|
111 "Logical and operator. See also `&&'.", }, |
1
|
112 |
|
113 { "&&", |
928
|
114 "Logical and operator. See also `&'.", }, |
1
|
115 |
|
116 { "'", |
|
117 "Matrix transpose operator. For complex matrices, computes the\n\ |
|
118 complex conjugate (Hermitian) transpose. See also `.''\n\ |
|
119 \n\ |
|
120 The single quote character may also be used to delimit strings, but\n\ |
|
121 it is better to use the double quote character, since that is never\n\ |
928
|
122 ambiguous", }, |
1
|
123 |
|
124 { "(", |
928
|
125 "Array index or function argument delimiter.", }, |
1
|
126 |
|
127 { ")", |
928
|
128 "Array index or function argument delimiter.", }, |
1
|
129 |
|
130 { "*", |
928
|
131 "Multiplication operator. See also `.*'", }, |
1
|
132 |
|
133 { "**", |
928
|
134 "Power operator. See also `^', `.**', and `.^'", }, |
1
|
135 |
|
136 { "+", |
928
|
137 "Addition operator.", }, |
1
|
138 |
|
139 { "++", |
928
|
140 "Increment operator. As in C, may be applied as a prefix or postfix operator.", }, |
1
|
141 |
|
142 { ",", |
928
|
143 "Array index, function argument, or command separator.", }, |
1
|
144 |
|
145 { "-", |
928
|
146 "Subtraction or unary negation operator.", }, |
1
|
147 |
|
148 { "--", |
928
|
149 "Decrement operator. As in C, may be applied as a prefix or postfix operator.", }, |
1
|
150 |
|
151 { ".'", |
|
152 "Matrix transpose operator. For complex matrices, computes the\n\ |
928
|
153 transpose, *not* the complex conjugate transpose. See also `''.", }, |
1
|
154 |
|
155 { ".*", |
928
|
156 "Element by element multiplication operator. See also `*'.", }, |
1
|
157 |
|
158 { ".**", |
928
|
159 "Element by element power operator. See also `**', `^', and `.^'.", }, |
1
|
160 |
|
161 { "./", |
928
|
162 "Element by element division operator. See also `/' and `\\'.", }, |
1
|
163 |
|
164 { ".^", |
928
|
165 "Element by element power operator. See also `**', `^', and `.^'.", }, |
1
|
166 |
|
167 { "/", |
928
|
168 "Right division. See also `\\' and `./'.", }, |
1
|
169 |
|
170 { ":", |
928
|
171 "Select entire rows or columns of matrices.", }, |
1
|
172 |
|
173 { ";", |
928
|
174 "Array row or command separator. See also `,'.", }, |
1
|
175 |
|
176 { "<", |
928
|
177 "Less than operator.", }, |
1
|
178 |
|
179 { "<=", |
928
|
180 "Less than or equals operator.", }, |
1
|
181 |
|
182 { "<>", |
928
|
183 "Logical not equals operator. See also `!=' and `~='.", }, |
1
|
184 |
|
185 { "=", |
928
|
186 "Assignment operator.", }, |
1
|
187 |
|
188 { "==", |
928
|
189 "Equality test operator.", }, |
1
|
190 |
|
191 { ">", |
928
|
192 "Greater than operator.", }, |
1
|
193 |
|
194 { ">=", |
928
|
195 "Greater than or equals operator.", }, |
1
|
196 |
|
197 { "[", |
928
|
198 "Return list delimiter. See also `]'.", }, |
1
|
199 |
|
200 { "\\", |
928
|
201 "Left division operator. See also `/' and `./'.", }, |
1
|
202 |
|
203 { "]", |
928
|
204 "Return list delimiter. See also `['.", }, |
1
|
205 |
|
206 { "^", |
928
|
207 "Power operator. See also `**', `.^', and `.**.'", }, |
1
|
208 |
|
209 { "|", |
928
|
210 "Logical or operator. See also `||'.", }, |
1
|
211 |
|
212 { "||", |
928
|
213 "Logical or operator. See also `|'.", }, |
1
|
214 |
|
215 { "~", |
928
|
216 "Logical not operator. See also `!' and `~'.", }, |
1
|
217 |
|
218 { "~=", |
928
|
219 "Logical not equals operator. See also `<>' and `!='.", }, |
1
|
220 |
529
|
221 { 0, 0, }, |
1
|
222 }; |
|
223 |
|
224 static help_list keywords[] = |
|
225 { |
928
|
226 { "all_va_args", |
|
227 "Pass all unnamed arguments to another function call.", }, |
|
228 |
1
|
229 { "break", |
928
|
230 "Exit the innermost enclosing while or for loop.", }, |
1
|
231 |
1489
|
232 { "catch", |
|
233 "begin the cleanup part of a try-catch block", }, |
|
234 |
1
|
235 { "continue", |
928
|
236 "Jump to the end of the innermost enclosing while or for loop.", }, |
1
|
237 |
|
238 { "else", |
928
|
239 "Alternate action for an if block.", }, |
1
|
240 |
|
241 { "elseif", |
928
|
242 "Alternate conditional test for an if block.", }, |
1
|
243 |
|
244 { "end", |
928
|
245 "Mark the end of any for, if, while, or function block.", }, |
|
246 |
1489
|
247 { "end_try_catch", |
|
248 "Mark the end of an try-catch block.", }, |
|
249 |
928
|
250 { "end_unwind_protect", |
|
251 "Mark the end of an unwind_protect block.", }, |
1
|
252 |
|
253 { "endfor", |
928
|
254 "Mark the end of a for loop.", }, |
1
|
255 |
|
256 { "endfunction", |
928
|
257 "Mark the end of a function.", }, |
1
|
258 |
|
259 { "endif", |
928
|
260 "Mark the end of an if block.", }, |
1
|
261 |
|
262 { "endwhile", |
928
|
263 "Mark the end of a while loop.", }, |
1
|
264 |
|
265 { "for", |
928
|
266 "Begin a for loop.", }, |
1
|
267 |
|
268 { "function", |
928
|
269 "Begin a function body.", }, |
1
|
270 |
|
271 { "global", |
928
|
272 "Declare variables to have global scope.", }, |
1
|
273 |
|
274 { "gplot", |
928
|
275 "Produce 2-D plots using gnuplot-like command syntax.", }, |
1
|
276 |
|
277 { "gsplot", |
928
|
278 "Produce 3-D plots using gnuplot-like command syntax.", }, |
1
|
279 |
|
280 { "if", |
928
|
281 "Begin an if block.", }, |
1
|
282 |
|
283 { "return", |
928
|
284 "Return from a function.", }, |
|
285 |
1489
|
286 { "try", |
|
287 "Begin a try-catch block.", }, |
|
288 |
928
|
289 { "unwind_protect", |
|
290 "Begin an unwind_protect block.", }, |
|
291 |
|
292 { "unwind_protect_cleanup", |
|
293 "Begin the cleanup section of an unwind_protect block.", }, |
1
|
294 |
|
295 { "while", |
928
|
296 "Begin a while loop.", }, |
1
|
297 |
529
|
298 { 0, 0, }, |
1
|
299 }; |
|
300 |
581
|
301 // Return a copy of the operator or keyword names. |
|
302 |
3016
|
303 static string_vector |
3355
|
304 names (help_list *lst) |
1
|
305 { |
1755
|
306 string_vector retval; |
|
307 |
3355
|
308 int count = 0; |
1
|
309 help_list *ptr = lst; |
529
|
310 while (ptr->name) |
1
|
311 { |
|
312 count++; |
|
313 ptr++; |
|
314 } |
|
315 |
1755
|
316 if (count > 0) |
|
317 { |
|
318 retval.resize (count); |
1
|
319 |
1755
|
320 ptr = lst; |
|
321 for (int i = 0; i < count; i++) |
|
322 { |
|
323 retval[i] = ptr->name; |
|
324 ptr++; |
|
325 } |
1
|
326 } |
|
327 |
1755
|
328 return retval; |
1
|
329 } |
|
330 |
3014
|
331 static help_list * |
1
|
332 operator_help (void) |
|
333 { |
|
334 return operators; |
|
335 } |
|
336 |
3016
|
337 static help_list * |
1
|
338 keyword_help (void) |
|
339 { |
|
340 return keywords; |
|
341 } |
|
342 |
3016
|
343 // It's not likely that this does the right thing now. XXX FIXME XXX |
|
344 |
|
345 string_vector |
|
346 make_name_list (void) |
|
347 { |
3355
|
348 string_vector key = names (keyword_help ()); |
|
349 int key_len = key.length (); |
3016
|
350 |
3355
|
351 string_vector glb = global_sym_tab->name_list (); |
|
352 int glb_len = glb.length (); |
3016
|
353 |
3355
|
354 string_vector top = top_level_sym_tab->name_list (); |
|
355 int top_len = top.length (); |
3016
|
356 |
3355
|
357 string_vector lcl; |
3016
|
358 if (top_level_sym_tab != curr_sym_tab) |
3355
|
359 lcl = curr_sym_tab->name_list (); |
|
360 int lcl_len = lcl.length (); |
3016
|
361 |
3355
|
362 string_vector ffl = octave_fcn_file_name_cache::list_no_suffix (); |
3016
|
363 int ffl_len = ffl.length (); |
|
364 |
|
365 int total_len = key_len + glb_len + top_len + lcl_len + ffl_len; |
|
366 |
|
367 string_vector list (total_len); |
|
368 |
|
369 // Put all the symbols in one big list. |
|
370 |
|
371 int j = 0; |
|
372 int i = 0; |
|
373 for (i = 0; i < key_len; i++) |
|
374 list[j++] = key[i]; |
|
375 |
|
376 for (i = 0; i < glb_len; i++) |
|
377 list[j++] = glb[i]; |
|
378 |
|
379 for (i = 0; i < top_len; i++) |
|
380 list[j++] = top[i]; |
|
381 |
|
382 for (i = 0; i < lcl_len; i++) |
|
383 list[j++] = lcl[i]; |
|
384 |
|
385 for (i = 0; i < ffl_len; i++) |
|
386 list[j++] = ffl[i]; |
|
387 |
|
388 return list; |
|
389 } |
|
390 |
3014
|
391 void |
2470
|
392 additional_help_message (ostream& os) |
|
393 { |
|
394 if (! Vsuppress_verbose_help_message) |
|
395 os << "\n\ |
3259
|
396 Additional help for built-in functions, operators, and variables\n\ |
3160
|
397 is available in the on-line version of the manual. Use the command\n\ |
3295
|
398 `help -i <topic>' to search the manual index.\n\ |
|
399 \n\ |
3160
|
400 Help and information about Octave is also available on the WWW\n\ |
|
401 at http://www.che.wisc.edu/octave/octave.html and via the\n\ |
|
402 help-octave@bevo.che.wisc.edu mailing list.\n"; |
|
403 } |
|
404 |
2976
|
405 // XXX FIXME XXX -- this needs a major overhaul to cope with new |
|
406 // symbol table stuff. |
|
407 |
529
|
408 static void |
2095
|
409 display_names_from_help_list (ostream& os, help_list *list, |
542
|
410 const char *desc) |
529
|
411 { |
3355
|
412 string_vector symbols = names (list); |
3259
|
413 |
1808
|
414 if (! symbols.empty ()) |
|
415 { |
2095
|
416 os << "\n*** " << desc << ":\n\n"; |
3259
|
417 |
|
418 symbols.qsort (); |
|
419 |
2095
|
420 symbols.list_in_columns (os); |
1808
|
421 } |
529
|
422 } |
|
423 |
|
424 static void |
2095
|
425 display_symtab_names (ostream& os, const string_vector& names, |
3355
|
426 const string& desc) |
542
|
427 { |
1808
|
428 if (! names.empty ()) |
|
429 { |
2095
|
430 os << "\n*** " << desc << ":\n\n"; |
|
431 names.list_in_columns (os); |
1808
|
432 } |
542
|
433 } |
|
434 |
|
435 #ifdef LIST_SYMBOLS |
|
436 #undef LIST_SYMBOLS |
|
437 #endif |
|
438 #define LIST_SYMBOLS(type, msg) \ |
|
439 do \ |
|
440 { \ |
3259
|
441 string_vector names \ |
3355
|
442 = global_sym_tab->name_list (string_vector (), true, type); \ |
|
443 display_symtab_names (octave_stdout, names, msg); \ |
542
|
444 } \ |
|
445 while (0) |
529
|
446 |
3014
|
447 static void |
|
448 simple_help (void) |
|
449 { |
3160
|
450 octave_stdout << "Help is available for the topics listed below.\n"; |
|
451 |
|
452 additional_help_message (octave_stdout); |
|
453 |
3014
|
454 display_names_from_help_list (octave_stdout, operator_help (), |
|
455 "operators"); |
|
456 |
|
457 display_names_from_help_list (octave_stdout, keyword_help (), |
|
458 "reserved words"); |
|
459 |
1358
|
460 // XXX FIXME XXX -- is this distinction needed? |
|
461 |
3259
|
462 LIST_SYMBOLS (symbol_record::BUILTIN_CONSTANT, "built-in constants"); |
|
463 |
|
464 LIST_SYMBOLS (symbol_record::BUILTIN_VARIABLE, "built-in variables"); |
|
465 |
3010
|
466 LIST_SYMBOLS (symbol_record::TEXT_FUNCTION, |
542
|
467 "text functions (these names are also reserved)"); |
529
|
468 |
3010
|
469 LIST_SYMBOLS (symbol_record::MAPPER_FUNCTION, "mapper functions"); |
542
|
470 |
3010
|
471 LIST_SYMBOLS (symbol_record::BUILTIN_FUNCTION, "general functions"); |
542
|
472 |
1358
|
473 // Also need to list variables and currently compiled functions from |
|
474 // the symbol table, if there are any. |
529
|
475 |
1358
|
476 // Also need to search octave_path for script files. |
529
|
477 |
3195
|
478 string_vector dirs = Vload_path_dir_path.all_directories (); |
679
|
479 |
1787
|
480 int len = dirs.length (); |
679
|
481 |
1787
|
482 for (int i = 0; i < len; i++) |
|
483 { |
2233
|
484 string_vector names = octave_fcn_file_name_cache::list (dirs[i]); |
679
|
485 |
1787
|
486 if (! names.empty ()) |
|
487 { |
2926
|
488 string dir |
|
489 = octave_env::make_absolute (dirs[i], octave_env::getcwd ()); |
|
490 |
|
491 octave_stdout << "\n*** function files in " << dir << ":\n\n"; |
679
|
492 |
3259
|
493 names.qsort (); |
|
494 |
2095
|
495 names.list_in_columns (octave_stdout); |
529
|
496 } |
|
497 } |
|
498 } |
|
499 |
|
500 static int |
1755
|
501 try_info (const string& nm) |
529
|
502 { |
|
503 int status = 0; |
|
504 |
1295
|
505 static char *cmd_str = 0; |
|
506 |
|
507 delete [] cmd_str; |
|
508 cmd_str = 0; |
|
509 |
|
510 ostrstream cmd_buf; |
|
511 |
2202
|
512 cmd_buf << Vinfo_prog << " --file " << Vinfo_file; |
1295
|
513 |
2202
|
514 string directory_name = Vinfo_file; |
1755
|
515 size_t pos = directory_name.rfind ('/'); |
|
516 |
|
517 if (pos != NPOS) |
529
|
518 { |
1755
|
519 directory_name.resize (pos + 1); |
1295
|
520 cmd_buf << " --directory " << directory_name; |
529
|
521 } |
|
522 |
1755
|
523 if (nm.length () > 0) |
|
524 cmd_buf << " --index-search " << nm; |
529
|
525 |
1295
|
526 cmd_buf << ends; |
529
|
527 |
1295
|
528 cmd_str = cmd_buf.str (); |
529
|
529 |
2705
|
530 volatile octave_interrupt_handler old_interrupt_handler |
2554
|
531 = octave_ignore_interrupts (); |
529
|
532 |
1295
|
533 status = system (cmd_str); |
529
|
534 |
2554
|
535 octave_set_interrupt_handler (old_interrupt_handler); |
529
|
536 |
2694
|
537 if (WIFEXITED (status)) |
|
538 status = WEXITSTATUS (status); |
1295
|
539 else |
|
540 status = 127; |
529
|
541 |
|
542 return status; |
|
543 } |
1140
|
544 |
|
545 static void |
1755
|
546 help_from_info (const string_vector& argv, int idx, int argc) |
1140
|
547 { |
1755
|
548 if (idx == argc) |
|
549 try_info (string ()); |
1140
|
550 else |
|
551 { |
1755
|
552 for (int i = idx; i < argc; i++) |
1140
|
553 { |
1755
|
554 int status = try_info (argv[i]); |
1140
|
555 |
1295
|
556 if (status) |
1140
|
557 { |
3295
|
558 if (status == 127) |
1295
|
559 { |
3185
|
560 error ("help: unable to find info"); |
|
561 error ("help: you need info 2.18 or later (texinfo 3.12)"); |
1295
|
562 break; |
|
563 } |
3295
|
564 else |
|
565 { |
|
566 message ("help", "sorry, `%s' is not indexed in the manual", |
|
567 argv[i].c_str ()); |
|
568 } |
1140
|
569 } |
|
570 } |
|
571 } |
2470
|
572 } |
3014
|
573 |
|
574 static bool |
3295
|
575 looks_like_texinfo (const string& msg, size_t& p1) |
|
576 { |
|
577 p1 = msg.find ('\n'); |
|
578 |
|
579 string t = msg.substr (0, p1); |
|
580 |
|
581 if (p1 == NPOS) |
|
582 p1 = 0; |
|
583 |
|
584 size_t p2 = t.find ("-*- texinfo -*-"); |
|
585 |
|
586 return (p2 != NPOS); |
|
587 } |
|
588 |
3330
|
589 void |
3295
|
590 display_help_text (ostream& os, const string& msg) |
|
591 { |
|
592 // Look for "-*- texinfo -*-" in first line of help message. If it |
|
593 // is present, use makeinfo to format the rest of the message before |
|
594 // sending it to the output stream. Otherwise, just print the |
|
595 // message. |
|
596 |
|
597 size_t pos; |
|
598 |
|
599 if (looks_like_texinfo (msg, pos)) |
|
600 { |
|
601 string tmp_file_name = file_ops::tempnam ("", ""); |
|
602 |
|
603 int cols = command_editor::terminal_cols (); |
|
604 |
|
605 if (cols > 16) |
|
606 cols--; |
|
607 |
|
608 if (cols > 64) |
|
609 cols -= 7; |
|
610 |
|
611 if (cols > 80) |
|
612 cols = 72; |
|
613 |
|
614 ostrstream buf; |
3301
|
615 buf << "sed 's/^[#%]+ *//' | makeinfo" |
3303
|
616 << " -D \"VERSION " << OCTAVE_VERSION << "\"" |
|
617 << " -D \"OCTAVEHOME " << OCTAVE_PREFIX << "\"" |
|
618 << " -D \"TARGETHOSTTYPE " << CANONICAL_HOST_TYPE << "\"" |
3301
|
619 << " --fill-column " << cols |
|
620 << " --no-warn" |
|
621 << " --no-validate" |
|
622 << " --no-headers" |
|
623 << " --force" |
|
624 << " --output " << tmp_file_name |
3303
|
625 << " > /dev/null 2>&1" |
3295
|
626 << ends; |
|
627 |
|
628 char *cmd = buf.str (); |
|
629 |
|
630 oprocstream filter (cmd); |
|
631 |
|
632 delete [] cmd; |
|
633 |
|
634 if (filter) |
|
635 { |
|
636 filter << msg.substr (pos+1); |
|
637 |
|
638 filter.close (); |
|
639 |
|
640 ifstream tmp_file (tmp_file_name.c_str ()); |
|
641 |
|
642 int c; |
|
643 while ((c = tmp_file.get ()) != EOF) |
|
644 os << (char) c; |
|
645 |
|
646 tmp_file.close (); |
|
647 |
|
648 file_ops::unlink (tmp_file_name); |
|
649 } |
|
650 else |
|
651 os << msg; |
|
652 } |
|
653 else |
|
654 os << msg; |
|
655 } |
|
656 |
|
657 static bool |
2095
|
658 help_from_list (ostream& os, const help_list *list, |
1755
|
659 const string& nm, int usage) |
542
|
660 { |
2804
|
661 const char *name; |
3014
|
662 |
542
|
663 while ((name = list->name) != 0) |
|
664 { |
1755
|
665 if (strcmp (name, nm.c_str ()) == 0) |
542
|
666 { |
|
667 if (usage) |
2095
|
668 os << "\nusage: "; |
542
|
669 else |
|
670 { |
2095
|
671 os << "\n*** " << nm << ":\n\n"; |
542
|
672 } |
|
673 |
3295
|
674 display_help_text (os, list->help); |
|
675 |
|
676 os << "\n"; |
542
|
677 |
3014
|
678 return true; |
542
|
679 } |
|
680 list++; |
|
681 } |
3014
|
682 |
|
683 return false; |
542
|
684 } |
|
685 |
3355
|
686 static bool |
|
687 help_from_symbol_table (ostream& os, const string& nm) |
|
688 { |
|
689 bool retval = false; |
|
690 |
|
691 symbol_record *sym_rec = lookup_by_name (nm, 0); |
|
692 |
|
693 if (sym_rec && sym_rec->is_defined ()) |
|
694 { |
|
695 string h = sym_rec->help (); |
|
696 |
|
697 if (h.length () > 0) |
|
698 { |
|
699 sym_rec->which (os); |
|
700 os << "\n"; |
|
701 display_help_text (os, h); |
|
702 os << "\n"; |
|
703 retval = true; |
|
704 } |
|
705 } |
|
706 |
|
707 return retval; |
|
708 } |
|
709 |
|
710 static bool |
|
711 help_from_file (ostream& os, const string& nm) |
|
712 { |
|
713 bool retval = false; |
|
714 |
|
715 string path = fcn_file_in_path (nm); |
|
716 |
|
717 string h = get_help_from_file (path); |
|
718 |
|
719 if (! h.empty ()) |
|
720 { |
|
721 os << nm << " is the file: " << path << "\n\n"; |
|
722 display_help_text (os, h); |
|
723 os << "\n"; |
|
724 retval = true; |
|
725 } |
|
726 |
|
727 return retval; |
|
728 } |
|
729 |
1140
|
730 static void |
1755
|
731 builtin_help (int argc, const string_vector& argv) |
1140
|
732 { |
|
733 help_list *op_help_list = operator_help (); |
|
734 help_list *kw_help_list = keyword_help (); |
|
735 |
1755
|
736 for (int i = 1; i < argc; i++) |
1140
|
737 { |
2095
|
738 if (help_from_list (octave_stdout, op_help_list, argv[i], 0)) |
1140
|
739 continue; |
|
740 |
2095
|
741 if (help_from_list (octave_stdout, kw_help_list, argv[i], 0)) |
1140
|
742 continue; |
|
743 |
3355
|
744 if (help_from_symbol_table (octave_stdout, argv[i])) |
|
745 continue; |
1755
|
746 |
3355
|
747 if (help_from_file (octave_stdout, argv[i])) |
|
748 continue; |
1140
|
749 |
2095
|
750 octave_stdout << "\nhelp: sorry, `" << argv[i] |
|
751 << "' is not documented\n"; |
1140
|
752 } |
|
753 |
2095
|
754 additional_help_message (octave_stdout); |
1140
|
755 } |
|
756 |
1957
|
757 DEFUN_TEXT (help, args, , |
3332
|
758 "-*- texinfo -*-\n\ |
|
759 @deffn {Command} help\n\ |
|
760 Octave's @code{help} command can be used to print brief usage-style\n\ |
|
761 messages, or to display information directly from an on-line version of\n\ |
|
762 the printed manual, using the GNU Info browser. If invoked without any\n\ |
|
763 arguments, @code{help} prints a list of all the available operators,\n\ |
|
764 functions, and built-in variables. If the first argument is @code{-i},\n\ |
|
765 the @code{help} command searches the index of the on-line version of\n\ |
|
766 this manual for the given topics.\n\ |
3168
|
767 \n\ |
3332
|
768 For example, the command @kbd{help help} prints a short message\n\ |
|
769 describing the @code{help} command, and @kbd{help -i help} starts the\n\ |
|
770 GNU Info browser at this node in the on-line version of the manual.\n\ |
|
771 \n\ |
|
772 Once the GNU Info browser is running, help for using it is available\n\ |
|
773 using the command @kbd{C-h}.\n\ |
3333
|
774 @end deffn") |
529
|
775 { |
2086
|
776 octave_value_list retval; |
529
|
777 |
1755
|
778 int argc = args.length () + 1; |
|
779 |
1968
|
780 string_vector argv = args.make_argv ("help"); |
1755
|
781 |
|
782 if (error_state) |
|
783 return retval; |
529
|
784 |
|
785 if (argc == 1) |
3014
|
786 simple_help (); |
529
|
787 else |
|
788 { |
1755
|
789 if (argv[1] == "-i") |
3014
|
790 help_from_info (argv, 2, argc); |
529
|
791 else |
3014
|
792 builtin_help (argc, argv); |
529
|
793 } |
|
794 |
|
795 return retval; |
|
796 } |
|
797 |
3355
|
798 static void |
|
799 do_type (ostream& os, const string& name, bool pr_type_info, |
|
800 bool quiet, bool pr_orig_txt) |
|
801 { |
|
802 symbol_record *sym_rec = lookup_by_name (name, 0); |
|
803 |
|
804 if (sym_rec && sym_rec->is_defined ()) |
|
805 { |
|
806 if (sym_rec->is_user_function ()) |
|
807 { |
|
808 octave_value tmp = sym_rec->def (); |
|
809 |
|
810 octave_function *defn = tmp.function_value (); |
|
811 |
|
812 string fn = defn ? defn->fcn_file_name () : string (); |
|
813 |
|
814 if (pr_orig_txt && ! fn.empty ()) |
|
815 { |
|
816 ifstream fs (fn.c_str (), ios::in); |
|
817 |
|
818 if (fs) |
|
819 { |
|
820 if (pr_type_info && ! quiet) |
|
821 os << name << " is the function defined from: " |
|
822 << fn << "\n\n"; |
|
823 |
|
824 char ch; |
|
825 |
|
826 while (fs.get (ch)) |
|
827 os << ch; |
|
828 } |
|
829 else |
|
830 os << "unable to open `" << fn << "' for reading!\n"; |
|
831 } |
|
832 else |
|
833 { |
|
834 if (pr_type_info && ! quiet) |
|
835 os << name << " is a user-defined function:\n\n"; |
|
836 |
|
837 tree_print_code tpc (os, "", pr_orig_txt); |
|
838 |
|
839 defn->accept (tpc); |
|
840 } |
|
841 } |
|
842 |
|
843 // XXX FIXME XXX -- this code should be shared with |
|
844 // Fwhich. |
|
845 |
|
846 else if (sym_rec->is_text_function ()) |
|
847 os << name << " is a built-in text-function\n"; |
|
848 else if (sym_rec->is_builtin_function ()) |
|
849 os << name << " is a built-in function\n"; |
|
850 else if (sym_rec->is_user_variable () |
|
851 || sym_rec->is_builtin_variable () |
|
852 || sym_rec->is_builtin_constant ()) |
|
853 { |
|
854 octave_value defn = sym_rec->def (); |
|
855 |
|
856 int var_ok = 1; |
|
857 |
|
858 if (! error_state) |
|
859 { |
|
860 if (pr_type_info && ! quiet) |
|
861 { |
|
862 if (var_ok) |
|
863 { |
|
864 os << name; |
|
865 |
|
866 if (sym_rec->is_user_variable ()) |
|
867 os << " is a user-defined variable\n"; |
|
868 else if (sym_rec->is_builtin_variable ()) |
|
869 os << " is a built-in variable\n"; |
|
870 else if (sym_rec->is_builtin_constant ()) |
|
871 os << " is a built-in constant\n"; |
|
872 else |
|
873 panic_impossible (); |
|
874 } |
|
875 else |
|
876 os << "type: `" << name << "' has unknown type!\n"; |
|
877 } |
|
878 |
|
879 defn.print_raw (os, true); |
|
880 |
|
881 if (pr_type_info) |
|
882 os << "\n"; |
|
883 } |
|
884 } |
|
885 else |
|
886 error ("type: `%s' has unknown type!", name.c_str ()); |
|
887 } |
|
888 else |
|
889 { |
|
890 string ff = fcn_file_in_path (name); |
|
891 |
|
892 if (! ff.empty ()) |
|
893 { |
|
894 ifstream fs (ff.c_str (), ios::in); |
|
895 |
|
896 if (fs) |
|
897 { |
|
898 if (pr_type_info && ! quiet) |
|
899 os << name << " is the script file: " << ff << "\n\n"; |
|
900 |
|
901 char ch; |
|
902 |
|
903 while (fs.get (ch)) |
|
904 os << ch; |
|
905 } |
|
906 else |
|
907 os << "unable to open `" << ff << "' for reading!\n"; |
|
908 } |
|
909 else |
|
910 error ("type: `%s' undefined", name.c_str ()); |
|
911 } |
|
912 } |
|
913 |
1957
|
914 DEFUN_TEXT (type, args, nargout, |
2962
|
915 "type NAME\n\ |
581
|
916 \n\ |
|
917 display the definition of each NAME that refers to a function") |
|
918 { |
2086
|
919 octave_value_list retval; |
581
|
920 |
2985
|
921 unwind_protect::begin_frame ("Ftype"); |
1588
|
922 |
1755
|
923 int argc = args.length () + 1; |
|
924 |
1968
|
925 string_vector argv = args.make_argv ("type"); |
1755
|
926 |
3355
|
927 if (! error_state) |
581
|
928 { |
3355
|
929 if (argc > 1) |
|
930 { |
|
931 // XXX FIXME XXX -- we should really use getopt () |
2532
|
932 |
3355
|
933 bool quiet = false; |
|
934 bool pr_orig_txt = true; |
2532
|
935 |
3355
|
936 int idx; |
581
|
937 |
3355
|
938 for (idx = 1; idx < argc; idx++) |
1281
|
939 { |
3355
|
940 if (argv[idx] == "-q" || argv[idx] == "-quiet") |
|
941 quiet = true; |
|
942 else if (argv[idx] == "-t" || argv[idx] == "-transformed") |
|
943 pr_orig_txt = false; |
|
944 else |
|
945 break; |
1281
|
946 } |
|
947 |
3355
|
948 if (idx < argc) |
|
949 { |
|
950 ostrstream output_buf; |
581
|
951 |
3355
|
952 for (int i = idx; i < argc; i++) |
581
|
953 { |
3355
|
954 string id = argv[i]; |
2534
|
955 |
3355
|
956 if (nargout == 0) |
|
957 do_type (octave_stdout, id, true, quiet, pr_orig_txt); |
|
958 else |
|
959 do_type (output_buf, id, false, quiet, pr_orig_txt); |
581
|
960 |
3355
|
961 if (error_state) |
|
962 goto abort; |
581
|
963 } |
|
964 |
3355
|
965 if (nargout == 0) |
625
|
966 { |
3355
|
967 output_buf << ends; |
3014
|
968 |
3355
|
969 char *s = output_buf.str (); |
3164
|
970 |
3355
|
971 retval = s; |
3164
|
972 |
3355
|
973 delete [] s; |
625
|
974 } |
581
|
975 } |
|
976 else |
3355
|
977 print_usage ("type"); |
|
978 } |
|
979 else |
|
980 print_usage ("type"); |
|
981 } |
3141
|
982 |
3355
|
983 abort: |
581
|
984 |
|
985 return retval; |
|
986 } |
|
987 |
3355
|
988 static string |
|
989 do_which (const string& name) |
|
990 { |
|
991 string retval; |
|
992 |
|
993 symbol_record *sym_rec = lookup_by_name (name, 0); |
|
994 |
|
995 if (sym_rec && sym_rec->is_defined ()) |
|
996 retval = sym_rec->which (); |
|
997 else |
|
998 { |
|
999 string path = fcn_file_in_path (name); |
|
1000 |
|
1001 if (! path.empty ()) |
|
1002 retval = path; |
|
1003 else |
|
1004 retval = "undefined"; |
|
1005 } |
|
1006 |
|
1007 return retval; |
|
1008 } |
|
1009 |
|
1010 static void |
|
1011 do_which (ostream& os, const string& name) |
|
1012 { |
|
1013 symbol_record *sym_rec = lookup_by_name (name, 0); |
|
1014 |
|
1015 if (sym_rec && sym_rec->is_defined ()) |
|
1016 sym_rec->which (os); |
|
1017 else |
|
1018 { |
|
1019 string path = fcn_file_in_path (name); |
|
1020 |
|
1021 if (! path.empty ()) |
|
1022 os << "which: `" << name << "' is the script file\n" |
|
1023 << path << "\n"; |
|
1024 else |
|
1025 os << "which: `" << name << "' is undefined\n"; |
|
1026 } |
|
1027 } |
|
1028 |
1957
|
1029 DEFUN_TEXT (which, args, nargout, |
1565
|
1030 "which NAME ...\n\ |
581
|
1031 \n\ |
|
1032 display the type of each NAME. If NAME is defined from an function\n\ |
|
1033 file, print the full name of the file.") |
|
1034 { |
2086
|
1035 octave_value_list retval; |
581
|
1036 |
1968
|
1037 string_vector argv = args.make_argv ("which"); |
1755
|
1038 |
3355
|
1039 if (! error_state) |
|
1040 { |
|
1041 int argc = argv.length (); |
581
|
1042 |
|
1043 if (nargout > 0) |
|
1044 retval.resize (argc-1, Matrix ()); |
|
1045 |
3355
|
1046 if (argc > 1) |
581
|
1047 { |
3355
|
1048 for (int i = 1; i < argc; i++) |
581
|
1049 { |
3355
|
1050 string id = argv[i]; |
3141
|
1051 |
3355
|
1052 if (nargout == 0) |
|
1053 do_which (octave_stdout, id); |
581
|
1054 else |
3355
|
1055 retval(i-1) = do_which (id); |
581
|
1056 } |
|
1057 } |
3355
|
1058 else |
|
1059 print_usage (argv[0]); |
581
|
1060 } |
|
1061 |
|
1062 return retval; |
|
1063 } |
|
1064 |
2189
|
1065 static int |
2202
|
1066 info_file (void) |
|
1067 { |
|
1068 int status = 0; |
|
1069 |
|
1070 string s = builtin_string_variable ("INFO_FILE"); |
|
1071 |
|
1072 if (s.empty ()) |
|
1073 { |
|
1074 gripe_invalid_value_specified ("INFO_FILE"); |
|
1075 status = -1; |
|
1076 } |
|
1077 else |
|
1078 Vinfo_file = s; |
|
1079 |
|
1080 return status; |
|
1081 } |
|
1082 |
|
1083 static int |
|
1084 info_prog (void) |
|
1085 { |
|
1086 int status = 0; |
|
1087 |
|
1088 string s = builtin_string_variable ("INFO_PROGRAM"); |
|
1089 |
|
1090 if (s.empty ()) |
|
1091 { |
|
1092 gripe_invalid_value_specified ("INFO_PROGRAM"); |
|
1093 status = -1; |
|
1094 } |
|
1095 else |
|
1096 Vinfo_prog = s; |
|
1097 |
|
1098 return status; |
|
1099 } |
|
1100 |
3014
|
1101 static int |
|
1102 suppress_verbose_help_message (void) |
|
1103 { |
|
1104 Vsuppress_verbose_help_message |
|
1105 = check_preference ("suppress_verbose_help_message"); |
|
1106 |
|
1107 return 0; |
|
1108 } |
|
1109 |
2189
|
1110 void |
|
1111 symbols_of_help (void) |
|
1112 { |
3258
|
1113 DEFVAR (INFO_FILE, Vinfo_file, info_file, |
3332
|
1114 "-*- texinfo -*-\n\ |
|
1115 @defvr {Built-in Variable} INFO_FILE\n\ |
|
1116 The variable @code{INFO_FILE} names the location of the Octave info file.\n\ |
|
1117 The default value is @code{\"@var{octave-home}/info/octave.info\"}, where\n\ |
|
1118 @var{octave-home} is the directory where all of Octave is installed.\n\ |
3333
|
1119 @end defvr"); |
2202
|
1120 |
3258
|
1121 DEFVAR (INFO_PROGRAM, Vinfo_prog, info_prog, |
3332
|
1122 "-*- texinfo -*-\n\ |
|
1123 @defvr {Built-in Variable} INFO_FILE\n\ |
|
1124 The variable @code{INFO_FILE} names the location of the Octave info file.\n\ |
|
1125 The default value is @code{\"@var{octave-home}/info/octave.info\"}, where\n\ |
|
1126 @var{octave-home} is the directory where all of Octave is installed.\n\ |
3333
|
1127 @end defvr"); |
2202
|
1128 |
3258
|
1129 DEFVAR (suppress_verbose_help_message, 0.0, suppress_verbose_help_message, |
3332
|
1130 "-*- texinfo -*-\n\ |
|
1131 @defvr {Built-in Variable} suppress_verbose_help_message\n\ |
|
1132 If the value of @code{suppress_verbose_help_message} is nonzero, Octave\n\ |
|
1133 will not add additional help information to the end of the output from\n\ |
|
1134 the @code{help} command and usage messages for built-in commands.\n\ |
3333
|
1135 @end defvr"); |
|
1136 |
2189
|
1137 } |
|
1138 |
1
|
1139 /* |
|
1140 ;;; Local Variables: *** |
|
1141 ;;; mode: C++ *** |
|
1142 ;;; End: *** |
|
1143 */ |