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