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