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