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