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