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", |
928
|
295 "Begin an if block.", }, |
1
|
296 |
5040
|
297 { "otherwise", |
|
298 "The default statement in a switch block.", }, |
|
299 |
4686
|
300 { "persistent", |
|
301 "Declare variables as persistent.", }, |
|
302 |
5040
|
303 { "replot", |
|
304 "Replot a graphic.", }, |
|
305 |
1
|
306 { "return", |
928
|
307 "Return from a function.", }, |
|
308 |
5040
|
309 { "static", |
|
310 "Declare variables as persistent.", }, |
|
311 |
|
312 { "switch", |
|
313 "Begin a switch statement.",}, |
|
314 |
1489
|
315 { "try", |
|
316 "Begin a try-catch block.", }, |
|
317 |
5040
|
318 { "until", |
|
319 "End a do-until loop.",}, |
|
320 |
928
|
321 { "unwind_protect", |
|
322 "Begin an unwind_protect block.", }, |
|
323 |
|
324 { "unwind_protect_cleanup", |
|
325 "Begin the cleanup section of an unwind_protect block.", }, |
1
|
326 |
5040
|
327 { "varargin", |
5339
|
328 "Pass an arbitrary number of arguments into a function. See also\n\ |
|
329 varargout, nargin, and nargout.",}, |
5040
|
330 |
|
331 { "varargout", |
5339
|
332 "Pass an arbitrary number of arguments out of a function. See also\n\ |
|
333 varargin, nargin, and nargout.",}, |
5040
|
334 |
1
|
335 { "while", |
928
|
336 "Begin a while loop.", }, |
1
|
337 |
529
|
338 { 0, 0, }, |
1
|
339 }; |
|
340 |
581
|
341 // Return a copy of the operator or keyword names. |
|
342 |
3016
|
343 static string_vector |
3355
|
344 names (help_list *lst) |
1
|
345 { |
1755
|
346 string_vector retval; |
|
347 |
3355
|
348 int count = 0; |
1
|
349 help_list *ptr = lst; |
529
|
350 while (ptr->name) |
1
|
351 { |
|
352 count++; |
|
353 ptr++; |
|
354 } |
|
355 |
1755
|
356 if (count > 0) |
|
357 { |
|
358 retval.resize (count); |
1
|
359 |
1755
|
360 ptr = lst; |
|
361 for (int i = 0; i < count; i++) |
|
362 { |
|
363 retval[i] = ptr->name; |
|
364 ptr++; |
|
365 } |
1
|
366 } |
|
367 |
1755
|
368 return retval; |
1
|
369 } |
|
370 |
3014
|
371 static help_list * |
1
|
372 operator_help (void) |
|
373 { |
|
374 return operators; |
|
375 } |
|
376 |
3016
|
377 static help_list * |
1
|
378 keyword_help (void) |
|
379 { |
|
380 return keywords; |
|
381 } |
|
382 |
3016
|
383 // It's not likely that this does the right thing now. XXX FIXME XXX |
|
384 |
|
385 string_vector |
|
386 make_name_list (void) |
|
387 { |
3355
|
388 string_vector key = names (keyword_help ()); |
|
389 int key_len = key.length (); |
3016
|
390 |
4009
|
391 string_vector fbi = fbi_sym_tab->name_list (); |
|
392 int fbi_len = fbi.length (); |
|
393 |
3355
|
394 string_vector glb = global_sym_tab->name_list (); |
|
395 int glb_len = glb.length (); |
3016
|
396 |
3355
|
397 string_vector top = top_level_sym_tab->name_list (); |
|
398 int top_len = top.length (); |
3016
|
399 |
3355
|
400 string_vector lcl; |
3016
|
401 if (top_level_sym_tab != curr_sym_tab) |
3355
|
402 lcl = curr_sym_tab->name_list (); |
|
403 int lcl_len = lcl.length (); |
3016
|
404 |
3355
|
405 string_vector ffl = octave_fcn_file_name_cache::list_no_suffix (); |
3016
|
406 int ffl_len = ffl.length (); |
|
407 |
4009
|
408 int total_len = key_len + fbi_len + glb_len + top_len + lcl_len + ffl_len; |
3016
|
409 |
|
410 string_vector list (total_len); |
|
411 |
|
412 // Put all the symbols in one big list. |
|
413 |
|
414 int j = 0; |
|
415 int i = 0; |
|
416 for (i = 0; i < key_len; i++) |
|
417 list[j++] = key[i]; |
|
418 |
4009
|
419 for (i = 0; i < fbi_len; i++) |
|
420 list[j++] = fbi[i]; |
|
421 |
3016
|
422 for (i = 0; i < glb_len; i++) |
|
423 list[j++] = glb[i]; |
|
424 |
|
425 for (i = 0; i < top_len; i++) |
|
426 list[j++] = top[i]; |
|
427 |
|
428 for (i = 0; i < lcl_len; i++) |
|
429 list[j++] = lcl[i]; |
|
430 |
|
431 for (i = 0; i < ffl_len; i++) |
|
432 list[j++] = ffl[i]; |
|
433 |
|
434 return list; |
|
435 } |
|
436 |
3014
|
437 void |
3523
|
438 additional_help_message (std::ostream& os) |
2470
|
439 { |
|
440 if (! Vsuppress_verbose_help_message) |
|
441 os << "\n\ |
3259
|
442 Additional help for built-in functions, operators, and variables\n\ |
3160
|
443 is available in the on-line version of the manual. Use the command\n\ |
3295
|
444 `help -i <topic>' to search the manual index.\n\ |
|
445 \n\ |
3160
|
446 Help and information about Octave is also available on the WWW\n\ |
5041
|
447 at http://www.octave.org and via the help@octave.org\n\ |
3917
|
448 mailing list.\n"; |
3160
|
449 } |
|
450 |
2976
|
451 // XXX FIXME XXX -- this needs a major overhaul to cope with new |
|
452 // symbol table stuff. |
|
453 |
529
|
454 static void |
3523
|
455 display_names_from_help_list (std::ostream& os, help_list *list, |
542
|
456 const char *desc) |
529
|
457 { |
3355
|
458 string_vector symbols = names (list); |
3259
|
459 |
1808
|
460 if (! symbols.empty ()) |
|
461 { |
2095
|
462 os << "\n*** " << desc << ":\n\n"; |
3259
|
463 |
|
464 symbols.qsort (); |
|
465 |
2095
|
466 symbols.list_in_columns (os); |
1808
|
467 } |
529
|
468 } |
|
469 |
|
470 static void |
3523
|
471 display_symtab_names (std::ostream& os, const string_vector& names, |
|
472 const std::string& desc) |
542
|
473 { |
1808
|
474 if (! names.empty ()) |
|
475 { |
2095
|
476 os << "\n*** " << desc << ":\n\n"; |
|
477 names.list_in_columns (os); |
1808
|
478 } |
542
|
479 } |
|
480 |
|
481 #ifdef LIST_SYMBOLS |
|
482 #undef LIST_SYMBOLS |
|
483 #endif |
|
484 #define LIST_SYMBOLS(type, msg) \ |
|
485 do \ |
|
486 { \ |
3259
|
487 string_vector names \ |
4009
|
488 = fbi_sym_tab->name_list (string_vector (), true, type); \ |
3355
|
489 display_symtab_names (octave_stdout, names, msg); \ |
542
|
490 } \ |
|
491 while (0) |
529
|
492 |
3014
|
493 static void |
|
494 simple_help (void) |
|
495 { |
3160
|
496 octave_stdout << "Help is available for the topics listed below.\n"; |
|
497 |
|
498 additional_help_message (octave_stdout); |
|
499 |
3014
|
500 display_names_from_help_list (octave_stdout, operator_help (), |
|
501 "operators"); |
|
502 |
|
503 display_names_from_help_list (octave_stdout, keyword_help (), |
|
504 "reserved words"); |
|
505 |
1358
|
506 // XXX FIXME XXX -- is this distinction needed? |
|
507 |
3259
|
508 LIST_SYMBOLS (symbol_record::BUILTIN_CONSTANT, "built-in constants"); |
|
509 |
|
510 LIST_SYMBOLS (symbol_record::BUILTIN_VARIABLE, "built-in variables"); |
|
511 |
4208
|
512 LIST_SYMBOLS (symbol_record::COMMAND, "commands"); |
529
|
513 |
3010
|
514 LIST_SYMBOLS (symbol_record::MAPPER_FUNCTION, "mapper functions"); |
542
|
515 |
3010
|
516 LIST_SYMBOLS (symbol_record::BUILTIN_FUNCTION, "general functions"); |
542
|
517 |
1358
|
518 // Also need to list variables and currently compiled functions from |
|
519 // the symbol table, if there are any. |
529
|
520 |
1358
|
521 // Also need to search octave_path for script files. |
529
|
522 |
3195
|
523 string_vector dirs = Vload_path_dir_path.all_directories (); |
679
|
524 |
1787
|
525 int len = dirs.length (); |
679
|
526 |
1787
|
527 for (int i = 0; i < len; i++) |
|
528 { |
2233
|
529 string_vector names = octave_fcn_file_name_cache::list (dirs[i]); |
679
|
530 |
1787
|
531 if (! names.empty ()) |
|
532 { |
3523
|
533 std::string dir |
2926
|
534 = octave_env::make_absolute (dirs[i], octave_env::getcwd ()); |
|
535 |
|
536 octave_stdout << "\n*** function files in " << dir << ":\n\n"; |
679
|
537 |
3259
|
538 names.qsort (); |
|
539 |
2095
|
540 names.list_in_columns (octave_stdout); |
529
|
541 } |
|
542 } |
|
543 } |
|
544 |
|
545 static int |
3523
|
546 try_info (const std::string& nm) |
529
|
547 { |
|
548 int status = 0; |
|
549 |
4051
|
550 OSSTREAM cmd_buf; |
1295
|
551 |
4469
|
552 cmd_buf << "\"" << Vinfo_prog << "\" --file \"" << Vinfo_file << "\""; |
1295
|
553 |
3523
|
554 std::string directory_name = Vinfo_file; |
1755
|
555 size_t pos = directory_name.rfind ('/'); |
|
556 |
|
557 if (pos != NPOS) |
529
|
558 { |
1755
|
559 directory_name.resize (pos + 1); |
4469
|
560 cmd_buf << " --directory \"" << directory_name << "\""; |
529
|
561 } |
|
562 |
1755
|
563 if (nm.length () > 0) |
|
564 cmd_buf << " --index-search " << nm; |
529
|
565 |
4051
|
566 cmd_buf << OSSTREAM_ENDS; |
529
|
567 |
2705
|
568 volatile octave_interrupt_handler old_interrupt_handler |
2554
|
569 = octave_ignore_interrupts (); |
529
|
570 |
4051
|
571 status = system (OSSTREAM_C_STR (cmd_buf)); |
|
572 |
|
573 OSSTREAM_FREEZE (cmd_buf); |
529
|
574 |
2554
|
575 octave_set_interrupt_handler (old_interrupt_handler); |
529
|
576 |
2694
|
577 if (WIFEXITED (status)) |
|
578 status = WEXITSTATUS (status); |
1295
|
579 else |
|
580 status = 127; |
529
|
581 |
|
582 return status; |
|
583 } |
1140
|
584 |
|
585 static void |
1755
|
586 help_from_info (const string_vector& argv, int idx, int argc) |
1140
|
587 { |
1755
|
588 if (idx == argc) |
3523
|
589 try_info (std::string ()); |
1140
|
590 else |
|
591 { |
1755
|
592 for (int i = idx; i < argc; i++) |
1140
|
593 { |
1755
|
594 int status = try_info (argv[i]); |
1140
|
595 |
1295
|
596 if (status) |
1140
|
597 { |
3295
|
598 if (status == 127) |
1295
|
599 { |
3185
|
600 error ("help: unable to find info"); |
|
601 error ("help: you need info 2.18 or later (texinfo 3.12)"); |
1295
|
602 break; |
|
603 } |
3295
|
604 else |
|
605 { |
|
606 message ("help", "sorry, `%s' is not indexed in the manual", |
|
607 argv[i].c_str ()); |
|
608 } |
1140
|
609 } |
|
610 } |
|
611 } |
2470
|
612 } |
3014
|
613 |
|
614 static bool |
3523
|
615 looks_like_texinfo (const std::string& msg, size_t& p1) |
3295
|
616 { |
|
617 p1 = msg.find ('\n'); |
|
618 |
3523
|
619 std::string t = msg.substr (0, p1); |
3295
|
620 |
|
621 if (p1 == NPOS) |
|
622 p1 = 0; |
|
623 |
|
624 size_t p2 = t.find ("-*- texinfo -*-"); |
|
625 |
|
626 return (p2 != NPOS); |
|
627 } |
|
628 |
3330
|
629 void |
3523
|
630 display_help_text (std::ostream& os, const std::string& msg) |
3295
|
631 { |
|
632 // Look for "-*- texinfo -*-" in first line of help message. If it |
|
633 // is present, use makeinfo to format the rest of the message before |
|
634 // sending it to the output stream. Otherwise, just print the |
|
635 // message. |
|
636 |
|
637 size_t pos; |
|
638 |
|
639 if (looks_like_texinfo (msg, pos)) |
|
640 { |
3523
|
641 std::string tmp_file_name = file_ops::tempnam ("", ""); |
3295
|
642 |
|
643 int cols = command_editor::terminal_cols (); |
|
644 |
|
645 if (cols > 16) |
|
646 cols--; |
|
647 |
|
648 if (cols > 64) |
|
649 cols -= 7; |
|
650 |
|
651 if (cols > 80) |
|
652 cols = 72; |
|
653 |
4051
|
654 OSSTREAM buf; |
|
655 |
4115
|
656 buf << "sed -e 's/^[#%][#%]* *//' -e 's/^ *@/@/' | " |
4469
|
657 << "\"" << Vmakeinfo_prog << "\"" |
3303
|
658 << " -D \"VERSION " << OCTAVE_VERSION << "\"" |
|
659 << " -D \"OCTAVEHOME " << OCTAVE_PREFIX << "\"" |
3584
|
660 << " -D \"TARGETHOSTTYPE " << OCTAVE_CANONICAL_HOST_TYPE << "\"" |
3301
|
661 << " --fill-column " << cols |
|
662 << " --no-warn" |
|
663 << " --no-validate" |
|
664 << " --no-headers" |
|
665 << " --force" |
4108
|
666 << " --output \"" << tmp_file_name << "\"" |
3303
|
667 << " > /dev/null 2>&1" |
4051
|
668 << OSSTREAM_ENDS; |
3295
|
669 |
4051
|
670 oprocstream filter (OSSTREAM_STR (buf)); |
3295
|
671 |
4051
|
672 OSSTREAM_FREEZE (buf); |
3295
|
673 |
3686
|
674 if (filter && filter.is_open ()) |
3295
|
675 { |
3405
|
676 filter << "@macro seealso {args}\n" |
3408
|
677 << "\n" |
|
678 << "@noindent\n" |
3405
|
679 << "See also: \\args\\.\n" |
|
680 << "@end macro\n"; |
|
681 |
3769
|
682 filter << msg.substr (pos+1) << std::endl; |
3295
|
683 |
3686
|
684 int status = filter.close (); |
3295
|
685 |
3523
|
686 std::ifstream tmp_file (tmp_file_name.c_str ()); |
3295
|
687 |
3686
|
688 if (WIFEXITED (status) && WEXITSTATUS (status) == 0) |
|
689 { |
|
690 int c; |
|
691 while ((c = tmp_file.get ()) != EOF) |
|
692 os << (char) c; |
3295
|
693 |
3686
|
694 tmp_file.close (); |
|
695 } |
|
696 else |
|
697 { |
|
698 warning ("help: Texinfo formatting filter exited abnormally"); |
|
699 warning ("help: raw Texinfo source of help text follows..."); |
|
700 |
|
701 os << "\n" << msg; |
|
702 } |
3295
|
703 |
|
704 file_ops::unlink (tmp_file_name); |
|
705 } |
|
706 else |
|
707 os << msg; |
|
708 } |
|
709 else |
|
710 os << msg; |
|
711 } |
|
712 |
|
713 static bool |
3523
|
714 help_from_list (std::ostream& os, const help_list *list, |
|
715 const std::string& nm, int usage) |
542
|
716 { |
2804
|
717 const char *name; |
3014
|
718 |
542
|
719 while ((name = list->name) != 0) |
|
720 { |
1755
|
721 if (strcmp (name, nm.c_str ()) == 0) |
542
|
722 { |
|
723 if (usage) |
2095
|
724 os << "\nusage: "; |
542
|
725 else |
|
726 { |
2095
|
727 os << "\n*** " << nm << ":\n\n"; |
542
|
728 } |
|
729 |
3295
|
730 display_help_text (os, list->help); |
|
731 |
|
732 os << "\n"; |
542
|
733 |
3014
|
734 return true; |
542
|
735 } |
|
736 list++; |
|
737 } |
3014
|
738 |
|
739 return false; |
542
|
740 } |
|
741 |
3355
|
742 static bool |
3523
|
743 help_from_symbol_table (std::ostream& os, const std::string& nm) |
3355
|
744 { |
|
745 bool retval = false; |
|
746 |
|
747 symbol_record *sym_rec = lookup_by_name (nm, 0); |
|
748 |
|
749 if (sym_rec && sym_rec->is_defined ()) |
|
750 { |
3523
|
751 std::string h = sym_rec->help (); |
3355
|
752 |
|
753 if (h.length () > 0) |
|
754 { |
|
755 sym_rec->which (os); |
|
756 os << "\n"; |
|
757 display_help_text (os, h); |
|
758 os << "\n"; |
|
759 retval = true; |
|
760 } |
|
761 } |
|
762 |
|
763 return retval; |
|
764 } |
|
765 |
|
766 static bool |
3523
|
767 help_from_file (std::ostream& os, const std::string& nm) |
3355
|
768 { |
|
769 bool retval = false; |
|
770 |
3523
|
771 std::string path = fcn_file_in_path (nm); |
3355
|
772 |
3523
|
773 std::string h = get_help_from_file (path); |
3355
|
774 |
|
775 if (! h.empty ()) |
|
776 { |
|
777 os << nm << " is the file: " << path << "\n\n"; |
|
778 display_help_text (os, h); |
|
779 os << "\n"; |
|
780 retval = true; |
|
781 } |
|
782 |
|
783 return retval; |
|
784 } |
|
785 |
1140
|
786 static void |
1755
|
787 builtin_help (int argc, const string_vector& argv) |
1140
|
788 { |
|
789 help_list *op_help_list = operator_help (); |
|
790 help_list *kw_help_list = keyword_help (); |
|
791 |
1755
|
792 for (int i = 1; i < argc; i++) |
1140
|
793 { |
2095
|
794 if (help_from_list (octave_stdout, op_help_list, argv[i], 0)) |
1140
|
795 continue; |
|
796 |
2095
|
797 if (help_from_list (octave_stdout, kw_help_list, argv[i], 0)) |
1140
|
798 continue; |
|
799 |
3355
|
800 if (help_from_symbol_table (octave_stdout, argv[i])) |
|
801 continue; |
1755
|
802 |
3355
|
803 if (help_from_file (octave_stdout, argv[i])) |
|
804 continue; |
1140
|
805 |
2095
|
806 octave_stdout << "\nhelp: sorry, `" << argv[i] |
|
807 << "' is not documented\n"; |
1140
|
808 } |
|
809 |
2095
|
810 additional_help_message (octave_stdout); |
1140
|
811 } |
|
812 |
4208
|
813 DEFCMD (help, args, , |
3332
|
814 "-*- texinfo -*-\n\ |
|
815 @deffn {Command} help\n\ |
|
816 Octave's @code{help} command can be used to print brief usage-style\n\ |
|
817 messages, or to display information directly from an on-line version of\n\ |
|
818 the printed manual, using the GNU Info browser. If invoked without any\n\ |
|
819 arguments, @code{help} prints a list of all the available operators,\n\ |
|
820 functions, and built-in variables. If the first argument is @code{-i},\n\ |
|
821 the @code{help} command searches the index of the on-line version of\n\ |
|
822 this manual for the given topics.\n\ |
3168
|
823 \n\ |
3332
|
824 For example, the command @kbd{help help} prints a short message\n\ |
|
825 describing the @code{help} command, and @kbd{help -i help} starts the\n\ |
|
826 GNU Info browser at this node in the on-line version of the manual.\n\ |
|
827 \n\ |
|
828 Once the GNU Info browser is running, help for using it is available\n\ |
|
829 using the command @kbd{C-h}.\n\ |
3333
|
830 @end deffn") |
529
|
831 { |
2086
|
832 octave_value_list retval; |
529
|
833 |
1755
|
834 int argc = args.length () + 1; |
|
835 |
1968
|
836 string_vector argv = args.make_argv ("help"); |
1755
|
837 |
|
838 if (error_state) |
|
839 return retval; |
529
|
840 |
|
841 if (argc == 1) |
3014
|
842 simple_help (); |
529
|
843 else |
|
844 { |
1755
|
845 if (argv[1] == "-i") |
3014
|
846 help_from_info (argv, 2, argc); |
529
|
847 else |
3014
|
848 builtin_help (argc, argv); |
529
|
849 } |
|
850 |
|
851 return retval; |
|
852 } |
|
853 |
3355
|
854 static void |
3523
|
855 do_type (std::ostream& os, const std::string& name, bool pr_type_info, |
3355
|
856 bool quiet, bool pr_orig_txt) |
|
857 { |
|
858 symbol_record *sym_rec = lookup_by_name (name, 0); |
|
859 |
|
860 if (sym_rec && sym_rec->is_defined ()) |
3356
|
861 sym_rec->type (os, pr_type_info, quiet, pr_orig_txt); |
3355
|
862 else |
|
863 { |
3523
|
864 std::string ff = fcn_file_in_path (name); |
3355
|
865 |
|
866 if (! ff.empty ()) |
|
867 { |
3538
|
868 std::ifstream fs (ff.c_str (), std::ios::in); |
3355
|
869 |
|
870 if (fs) |
|
871 { |
|
872 if (pr_type_info && ! quiet) |
|
873 os << name << " is the script file: " << ff << "\n\n"; |
|
874 |
|
875 char ch; |
|
876 |
|
877 while (fs.get (ch)) |
|
878 os << ch; |
|
879 } |
|
880 else |
|
881 os << "unable to open `" << ff << "' for reading!\n"; |
|
882 } |
|
883 else |
|
884 error ("type: `%s' undefined", name.c_str ()); |
|
885 } |
|
886 } |
|
887 |
4208
|
888 DEFCMD (type, args, nargout, |
3361
|
889 "-*- texinfo -*-\n\ |
|
890 \n\ |
|
891 @deffn {Command} type options name @dots{}\n\ |
|
892 Display the definition of each @var{name} that refers to a function.\n\ |
|
893 \n\ |
|
894 Normally also displays if each @var{name} is user-defined or builtin;\n\ |
|
895 the @code{-q} option suppresses this behaviour.\n\ |
581
|
896 \n\ |
3361
|
897 Currently, Octave can only display functions that can be compiled\n\ |
|
898 cleanly, because it uses its internal representation of the function to\n\ |
|
899 recreate the program text.\n\ |
|
900 \n\ |
|
901 Comments are not displayed because Octave's parser currently discards\n\ |
|
902 them as it converts the text of a function file to its internal\n\ |
|
903 representation. This problem may be fixed in a future release.\n\ |
|
904 @end deffn") |
581
|
905 { |
3584
|
906 octave_value retval; |
1588
|
907 |
1755
|
908 int argc = args.length () + 1; |
|
909 |
1968
|
910 string_vector argv = args.make_argv ("type"); |
1755
|
911 |
3355
|
912 if (! error_state) |
581
|
913 { |
3355
|
914 if (argc > 1) |
|
915 { |
|
916 // XXX FIXME XXX -- we should really use getopt () |
2532
|
917 |
3355
|
918 bool quiet = false; |
|
919 bool pr_orig_txt = true; |
2532
|
920 |
3355
|
921 int idx; |
581
|
922 |
3355
|
923 for (idx = 1; idx < argc; idx++) |
1281
|
924 { |
3355
|
925 if (argv[idx] == "-q" || argv[idx] == "-quiet") |
|
926 quiet = true; |
|
927 else if (argv[idx] == "-t" || argv[idx] == "-transformed") |
|
928 pr_orig_txt = false; |
|
929 else |
|
930 break; |
1281
|
931 } |
|
932 |
3355
|
933 if (idx < argc) |
|
934 { |
4051
|
935 OSSTREAM output_buf; |
581
|
936 |
3355
|
937 for (int i = idx; i < argc; i++) |
581
|
938 { |
3523
|
939 std::string id = argv[i]; |
2534
|
940 |
3355
|
941 if (nargout == 0) |
|
942 do_type (octave_stdout, id, true, quiet, pr_orig_txt); |
|
943 else |
|
944 do_type (output_buf, id, false, quiet, pr_orig_txt); |
581
|
945 |
3355
|
946 if (error_state) |
|
947 goto abort; |
581
|
948 } |
|
949 |
3584
|
950 if (nargout != 0) |
625
|
951 { |
4051
|
952 output_buf << OSSTREAM_ENDS; |
3164
|
953 |
4051
|
954 retval = OSSTREAM_STR (output_buf); |
3164
|
955 |
4051
|
956 OSSTREAM_FREEZE (output_buf); |
625
|
957 } |
581
|
958 } |
|
959 else |
3355
|
960 print_usage ("type"); |
|
961 } |
|
962 else |
|
963 print_usage ("type"); |
|
964 } |
3141
|
965 |
3355
|
966 abort: |
581
|
967 |
|
968 return retval; |
|
969 } |
|
970 |
3536
|
971 static std::string |
3523
|
972 do_which (const std::string& name) |
3355
|
973 { |
3523
|
974 std::string retval; |
3355
|
975 |
|
976 symbol_record *sym_rec = lookup_by_name (name, 0); |
|
977 |
|
978 if (sym_rec && sym_rec->is_defined ()) |
|
979 retval = sym_rec->which (); |
|
980 else |
|
981 { |
3523
|
982 std::string path = fcn_file_in_path (name); |
3355
|
983 |
|
984 if (! path.empty ()) |
|
985 retval = path; |
|
986 else |
|
987 retval = "undefined"; |
|
988 } |
|
989 |
|
990 return retval; |
|
991 } |
|
992 |
|
993 static void |
3523
|
994 do_which (std::ostream& os, const std::string& name) |
3355
|
995 { |
|
996 symbol_record *sym_rec = lookup_by_name (name, 0); |
|
997 |
|
998 if (sym_rec && sym_rec->is_defined ()) |
|
999 sym_rec->which (os); |
|
1000 else |
|
1001 { |
3523
|
1002 std::string path = fcn_file_in_path (name); |
3355
|
1003 |
|
1004 if (! path.empty ()) |
|
1005 os << "which: `" << name << "' is the script file\n" |
|
1006 << path << "\n"; |
|
1007 else |
|
1008 os << "which: `" << name << "' is undefined\n"; |
|
1009 } |
|
1010 } |
|
1011 |
4208
|
1012 DEFCMD (which, args, nargout, |
3361
|
1013 "-*- texinfo -*-\n\ |
|
1014 @deffn {Command} which name @dots{}\n\ |
|
1015 Display the type of each @var{name}. If @var{name} is defined from a\n\ |
|
1016 function file, the full name of the file is also displayed.\n\ |
|
1017 @end deffn") |
581
|
1018 { |
2086
|
1019 octave_value_list retval; |
581
|
1020 |
1968
|
1021 string_vector argv = args.make_argv ("which"); |
1755
|
1022 |
3355
|
1023 if (! error_state) |
|
1024 { |
|
1025 int argc = argv.length (); |
581
|
1026 |
|
1027 if (nargout > 0) |
|
1028 retval.resize (argc-1, Matrix ()); |
|
1029 |
3355
|
1030 if (argc > 1) |
581
|
1031 { |
3355
|
1032 for (int i = 1; i < argc; i++) |
581
|
1033 { |
3523
|
1034 std::string id = argv[i]; |
3141
|
1035 |
3355
|
1036 if (nargout == 0) |
|
1037 do_which (octave_stdout, id); |
581
|
1038 else |
3355
|
1039 retval(i-1) = do_which (id); |
581
|
1040 } |
|
1041 } |
3355
|
1042 else |
|
1043 print_usage (argv[0]); |
581
|
1044 } |
|
1045 |
|
1046 return retval; |
|
1047 } |
|
1048 |
2189
|
1049 static int |
2202
|
1050 info_file (void) |
|
1051 { |
|
1052 int status = 0; |
|
1053 |
3523
|
1054 std::string s = builtin_string_variable ("INFO_FILE"); |
2202
|
1055 |
|
1056 if (s.empty ()) |
|
1057 { |
|
1058 gripe_invalid_value_specified ("INFO_FILE"); |
|
1059 status = -1; |
|
1060 } |
|
1061 else |
|
1062 Vinfo_file = s; |
|
1063 |
|
1064 return status; |
|
1065 } |
|
1066 |
|
1067 static int |
|
1068 info_prog (void) |
|
1069 { |
|
1070 int status = 0; |
|
1071 |
3523
|
1072 std::string s = builtin_string_variable ("INFO_PROGRAM"); |
2202
|
1073 |
|
1074 if (s.empty ()) |
|
1075 { |
|
1076 gripe_invalid_value_specified ("INFO_PROGRAM"); |
|
1077 status = -1; |
|
1078 } |
|
1079 else |
|
1080 Vinfo_prog = s; |
|
1081 |
|
1082 return status; |
|
1083 } |
|
1084 |
3014
|
1085 static int |
3686
|
1086 makeinfo_prog (void) |
|
1087 { |
|
1088 int status = 0; |
|
1089 |
|
1090 std::string s = builtin_string_variable ("MAKEINFO_PROGRAM"); |
|
1091 |
|
1092 if (s.empty ()) |
|
1093 { |
|
1094 gripe_invalid_value_specified ("MAKEINFO_PROGRAM"); |
|
1095 status = -1; |
|
1096 } |
|
1097 else |
|
1098 Vmakeinfo_prog = s; |
|
1099 |
|
1100 return status; |
|
1101 } |
|
1102 |
|
1103 static int |
3014
|
1104 suppress_verbose_help_message (void) |
|
1105 { |
|
1106 Vsuppress_verbose_help_message |
|
1107 = check_preference ("suppress_verbose_help_message"); |
|
1108 |
|
1109 return 0; |
|
1110 } |
|
1111 |
2189
|
1112 void |
|
1113 symbols_of_help (void) |
|
1114 { |
3258
|
1115 DEFVAR (INFO_FILE, Vinfo_file, info_file, |
3332
|
1116 "-*- texinfo -*-\n\ |
|
1117 @defvr {Built-in Variable} INFO_FILE\n\ |
|
1118 The variable @code{INFO_FILE} names the location of the Octave info file.\n\ |
3686
|
1119 The default value is @code{\"@var{octave-home}/info/octave.info\"}, in\n\ |
|
1120 which @var{octave-home} is the directory where all of Octave is installed.\n\ |
3333
|
1121 @end defvr"); |
2202
|
1122 |
3258
|
1123 DEFVAR (INFO_PROGRAM, Vinfo_prog, info_prog, |
3332
|
1124 "-*- texinfo -*-\n\ |
3686
|
1125 @defvr {Built-in Variable} INFO_PROGRAM\n\ |
|
1126 The variable @code{INFO_PROGRAM} names the info program to run. Its\n\ |
|
1127 default initial value is\n\ |
|
1128 @code{\"@var{octave-home}/libexec/octave/@var{version}/exec/@var{arch}/info\"}\n\ |
|
1129 in which @var{octave-home} is the directory where all of Octave is\n\ |
|
1130 installed, @var{version} is the Octave version number, and @var{arch}\n\ |
|
1131 is the system type (for example, @code{i686-pc-linux-gnu}). The\n\ |
|
1132 default initial value may be overridden by the environment variable\n\ |
|
1133 @code{OCTAVE_INFO_PROGRAM}, or the command line argument\n\ |
|
1134 @code{--info-program NAME}, or by setting the value of\n\ |
|
1135 @code{INFO_PROGRAM} in a startup script\n\ |
|
1136 @end defvr"); |
|
1137 |
|
1138 DEFVAR (MAKEINFO_PROGRAM, Vmakeinfo_prog, makeinfo_prog, |
|
1139 "-*- texinfo -*-\n\ |
|
1140 @defvr {Built-in Variable} MAKEINFO_PROGRAM\n\ |
|
1141 The variable @code{MAKEINFO_PROGRAM} names the makeinfo program that\n\ |
|
1142 Octave runs to format help text that contains Texinfo markup commands.\n\ |
|
1143 Its default initial value is @code{\"makeinfo\"}.\n\ |
3333
|
1144 @end defvr"); |
2202
|
1145 |
4233
|
1146 DEFVAR (suppress_verbose_help_message, false, suppress_verbose_help_message, |
3332
|
1147 "-*- texinfo -*-\n\ |
|
1148 @defvr {Built-in Variable} suppress_verbose_help_message\n\ |
|
1149 If the value of @code{suppress_verbose_help_message} is nonzero, Octave\n\ |
|
1150 will not add additional help information to the end of the output from\n\ |
|
1151 the @code{help} command and usage messages for built-in commands.\n\ |
3333
|
1152 @end defvr"); |
2189
|
1153 } |
|
1154 |
1
|
1155 /* |
|
1156 ;;; Local Variables: *** |
|
1157 ;;; mode: C++ *** |
|
1158 ;;; End: *** |
|
1159 */ |