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 |
3010
|
471 LIST_SYMBOLS (symbol_record::TEXT_FUNCTION, |
542
|
472 "text functions (these names are also reserved)"); |
529
|
473 |
3010
|
474 LIST_SYMBOLS (symbol_record::MAPPER_FUNCTION, "mapper functions"); |
542
|
475 |
3010
|
476 LIST_SYMBOLS (symbol_record::BUILTIN_FUNCTION, "general functions"); |
542
|
477 |
1358
|
478 // Also need to list variables and currently compiled functions from |
|
479 // the symbol table, if there are any. |
529
|
480 |
1358
|
481 // Also need to search octave_path for script files. |
529
|
482 |
3195
|
483 string_vector dirs = Vload_path_dir_path.all_directories (); |
679
|
484 |
1787
|
485 int len = dirs.length (); |
679
|
486 |
1787
|
487 for (int i = 0; i < len; i++) |
|
488 { |
2233
|
489 string_vector names = octave_fcn_file_name_cache::list (dirs[i]); |
679
|
490 |
1787
|
491 if (! names.empty ()) |
|
492 { |
3523
|
493 std::string dir |
2926
|
494 = octave_env::make_absolute (dirs[i], octave_env::getcwd ()); |
|
495 |
|
496 octave_stdout << "\n*** function files in " << dir << ":\n\n"; |
679
|
497 |
3259
|
498 names.qsort (); |
|
499 |
2095
|
500 names.list_in_columns (octave_stdout); |
529
|
501 } |
|
502 } |
|
503 } |
|
504 |
|
505 static int |
3523
|
506 try_info (const std::string& nm) |
529
|
507 { |
|
508 int status = 0; |
|
509 |
4051
|
510 OSSTREAM cmd_buf; |
1295
|
511 |
2202
|
512 cmd_buf << Vinfo_prog << " --file " << Vinfo_file; |
1295
|
513 |
3523
|
514 std::string directory_name = Vinfo_file; |
1755
|
515 size_t pos = directory_name.rfind ('/'); |
|
516 |
|
517 if (pos != NPOS) |
529
|
518 { |
1755
|
519 directory_name.resize (pos + 1); |
1295
|
520 cmd_buf << " --directory " << directory_name; |
529
|
521 } |
|
522 |
1755
|
523 if (nm.length () > 0) |
|
524 cmd_buf << " --index-search " << nm; |
529
|
525 |
4051
|
526 cmd_buf << OSSTREAM_ENDS; |
529
|
527 |
2705
|
528 volatile octave_interrupt_handler old_interrupt_handler |
2554
|
529 = octave_ignore_interrupts (); |
529
|
530 |
4051
|
531 status = system (OSSTREAM_C_STR (cmd_buf)); |
|
532 |
|
533 OSSTREAM_FREEZE (cmd_buf); |
529
|
534 |
2554
|
535 octave_set_interrupt_handler (old_interrupt_handler); |
529
|
536 |
2694
|
537 if (WIFEXITED (status)) |
|
538 status = WEXITSTATUS (status); |
1295
|
539 else |
|
540 status = 127; |
529
|
541 |
|
542 return status; |
|
543 } |
1140
|
544 |
|
545 static void |
1755
|
546 help_from_info (const string_vector& argv, int idx, int argc) |
1140
|
547 { |
1755
|
548 if (idx == argc) |
3523
|
549 try_info (std::string ()); |
1140
|
550 else |
|
551 { |
1755
|
552 for (int i = idx; i < argc; i++) |
1140
|
553 { |
1755
|
554 int status = try_info (argv[i]); |
1140
|
555 |
1295
|
556 if (status) |
1140
|
557 { |
3295
|
558 if (status == 127) |
1295
|
559 { |
3185
|
560 error ("help: unable to find info"); |
|
561 error ("help: you need info 2.18 or later (texinfo 3.12)"); |
1295
|
562 break; |
|
563 } |
3295
|
564 else |
|
565 { |
|
566 message ("help", "sorry, `%s' is not indexed in the manual", |
|
567 argv[i].c_str ()); |
|
568 } |
1140
|
569 } |
|
570 } |
|
571 } |
2470
|
572 } |
3014
|
573 |
|
574 static bool |
3523
|
575 looks_like_texinfo (const std::string& msg, size_t& p1) |
3295
|
576 { |
|
577 p1 = msg.find ('\n'); |
|
578 |
3523
|
579 std::string t = msg.substr (0, p1); |
3295
|
580 |
|
581 if (p1 == NPOS) |
|
582 p1 = 0; |
|
583 |
|
584 size_t p2 = t.find ("-*- texinfo -*-"); |
|
585 |
|
586 return (p2 != NPOS); |
|
587 } |
|
588 |
3330
|
589 void |
3523
|
590 display_help_text (std::ostream& os, const std::string& msg) |
3295
|
591 { |
|
592 // Look for "-*- texinfo -*-" in first line of help message. If it |
|
593 // is present, use makeinfo to format the rest of the message before |
|
594 // sending it to the output stream. Otherwise, just print the |
|
595 // message. |
|
596 |
|
597 size_t pos; |
|
598 |
|
599 if (looks_like_texinfo (msg, pos)) |
|
600 { |
3523
|
601 std::string tmp_file_name = file_ops::tempnam ("", ""); |
3295
|
602 |
|
603 int cols = command_editor::terminal_cols (); |
|
604 |
|
605 if (cols > 16) |
|
606 cols--; |
|
607 |
|
608 if (cols > 64) |
|
609 cols -= 7; |
|
610 |
|
611 if (cols > 80) |
|
612 cols = 72; |
|
613 |
4051
|
614 OSSTREAM buf; |
|
615 |
4108
|
616 buf << "sed -e 's/^[#%]* *//' -e 's/^ *@/@/' | " |
3686
|
617 << Vmakeinfo_prog |
3303
|
618 << " -D \"VERSION " << OCTAVE_VERSION << "\"" |
|
619 << " -D \"OCTAVEHOME " << OCTAVE_PREFIX << "\"" |
3584
|
620 << " -D \"TARGETHOSTTYPE " << OCTAVE_CANONICAL_HOST_TYPE << "\"" |
3301
|
621 << " --fill-column " << cols |
|
622 << " --no-warn" |
|
623 << " --no-validate" |
|
624 << " --no-headers" |
|
625 << " --force" |
4108
|
626 << " --output \"" << tmp_file_name << "\"" |
3303
|
627 << " > /dev/null 2>&1" |
4051
|
628 << OSSTREAM_ENDS; |
3295
|
629 |
4051
|
630 oprocstream filter (OSSTREAM_STR (buf)); |
3295
|
631 |
4051
|
632 OSSTREAM_FREEZE (buf); |
3295
|
633 |
3686
|
634 if (filter && filter.is_open ()) |
3295
|
635 { |
3405
|
636 filter << "@macro seealso {args}\n" |
3408
|
637 << "\n" |
|
638 << "@noindent\n" |
3405
|
639 << "See also: \\args\\.\n" |
|
640 << "@end macro\n"; |
|
641 |
3769
|
642 filter << msg.substr (pos+1) << std::endl; |
3295
|
643 |
3686
|
644 int status = filter.close (); |
3295
|
645 |
3523
|
646 std::ifstream tmp_file (tmp_file_name.c_str ()); |
3295
|
647 |
3686
|
648 if (WIFEXITED (status) && WEXITSTATUS (status) == 0) |
|
649 { |
|
650 int c; |
|
651 while ((c = tmp_file.get ()) != EOF) |
|
652 os << (char) c; |
3295
|
653 |
3686
|
654 tmp_file.close (); |
|
655 } |
|
656 else |
|
657 { |
|
658 warning ("help: Texinfo formatting filter exited abnormally"); |
|
659 warning ("help: raw Texinfo source of help text follows..."); |
|
660 |
|
661 os << "\n" << msg; |
|
662 } |
3295
|
663 |
|
664 file_ops::unlink (tmp_file_name); |
|
665 } |
|
666 else |
|
667 os << msg; |
|
668 } |
|
669 else |
|
670 os << msg; |
|
671 } |
|
672 |
|
673 static bool |
3523
|
674 help_from_list (std::ostream& os, const help_list *list, |
|
675 const std::string& nm, int usage) |
542
|
676 { |
2804
|
677 const char *name; |
3014
|
678 |
542
|
679 while ((name = list->name) != 0) |
|
680 { |
1755
|
681 if (strcmp (name, nm.c_str ()) == 0) |
542
|
682 { |
|
683 if (usage) |
2095
|
684 os << "\nusage: "; |
542
|
685 else |
|
686 { |
2095
|
687 os << "\n*** " << nm << ":\n\n"; |
542
|
688 } |
|
689 |
3295
|
690 display_help_text (os, list->help); |
|
691 |
|
692 os << "\n"; |
542
|
693 |
3014
|
694 return true; |
542
|
695 } |
|
696 list++; |
|
697 } |
3014
|
698 |
|
699 return false; |
542
|
700 } |
|
701 |
3355
|
702 static bool |
3523
|
703 help_from_symbol_table (std::ostream& os, const std::string& nm) |
3355
|
704 { |
|
705 bool retval = false; |
|
706 |
|
707 symbol_record *sym_rec = lookup_by_name (nm, 0); |
|
708 |
|
709 if (sym_rec && sym_rec->is_defined ()) |
|
710 { |
3523
|
711 std::string h = sym_rec->help (); |
3355
|
712 |
|
713 if (h.length () > 0) |
|
714 { |
|
715 sym_rec->which (os); |
|
716 os << "\n"; |
|
717 display_help_text (os, h); |
|
718 os << "\n"; |
|
719 retval = true; |
|
720 } |
|
721 } |
|
722 |
|
723 return retval; |
|
724 } |
|
725 |
|
726 static bool |
3523
|
727 help_from_file (std::ostream& os, const std::string& nm) |
3355
|
728 { |
|
729 bool retval = false; |
|
730 |
3523
|
731 std::string path = fcn_file_in_path (nm); |
3355
|
732 |
3523
|
733 std::string h = get_help_from_file (path); |
3355
|
734 |
|
735 if (! h.empty ()) |
|
736 { |
|
737 os << nm << " is the file: " << path << "\n\n"; |
|
738 display_help_text (os, h); |
|
739 os << "\n"; |
|
740 retval = true; |
|
741 } |
|
742 |
|
743 return retval; |
|
744 } |
|
745 |
1140
|
746 static void |
1755
|
747 builtin_help (int argc, const string_vector& argv) |
1140
|
748 { |
|
749 help_list *op_help_list = operator_help (); |
|
750 help_list *kw_help_list = keyword_help (); |
|
751 |
1755
|
752 for (int i = 1; i < argc; i++) |
1140
|
753 { |
2095
|
754 if (help_from_list (octave_stdout, op_help_list, argv[i], 0)) |
1140
|
755 continue; |
|
756 |
2095
|
757 if (help_from_list (octave_stdout, kw_help_list, argv[i], 0)) |
1140
|
758 continue; |
|
759 |
3355
|
760 if (help_from_symbol_table (octave_stdout, argv[i])) |
|
761 continue; |
1755
|
762 |
3355
|
763 if (help_from_file (octave_stdout, argv[i])) |
|
764 continue; |
1140
|
765 |
2095
|
766 octave_stdout << "\nhelp: sorry, `" << argv[i] |
|
767 << "' is not documented\n"; |
1140
|
768 } |
|
769 |
2095
|
770 additional_help_message (octave_stdout); |
1140
|
771 } |
|
772 |
1957
|
773 DEFUN_TEXT (help, args, , |
3332
|
774 "-*- texinfo -*-\n\ |
|
775 @deffn {Command} help\n\ |
|
776 Octave's @code{help} command can be used to print brief usage-style\n\ |
|
777 messages, or to display information directly from an on-line version of\n\ |
|
778 the printed manual, using the GNU Info browser. If invoked without any\n\ |
|
779 arguments, @code{help} prints a list of all the available operators,\n\ |
|
780 functions, and built-in variables. If the first argument is @code{-i},\n\ |
|
781 the @code{help} command searches the index of the on-line version of\n\ |
|
782 this manual for the given topics.\n\ |
3168
|
783 \n\ |
3332
|
784 For example, the command @kbd{help help} prints a short message\n\ |
|
785 describing the @code{help} command, and @kbd{help -i help} starts the\n\ |
|
786 GNU Info browser at this node in the on-line version of the manual.\n\ |
|
787 \n\ |
|
788 Once the GNU Info browser is running, help for using it is available\n\ |
|
789 using the command @kbd{C-h}.\n\ |
3333
|
790 @end deffn") |
529
|
791 { |
2086
|
792 octave_value_list retval; |
529
|
793 |
1755
|
794 int argc = args.length () + 1; |
|
795 |
1968
|
796 string_vector argv = args.make_argv ("help"); |
1755
|
797 |
|
798 if (error_state) |
|
799 return retval; |
529
|
800 |
|
801 if (argc == 1) |
3014
|
802 simple_help (); |
529
|
803 else |
|
804 { |
1755
|
805 if (argv[1] == "-i") |
3014
|
806 help_from_info (argv, 2, argc); |
529
|
807 else |
3014
|
808 builtin_help (argc, argv); |
529
|
809 } |
|
810 |
|
811 return retval; |
|
812 } |
|
813 |
3355
|
814 static void |
3523
|
815 do_type (std::ostream& os, const std::string& name, bool pr_type_info, |
3355
|
816 bool quiet, bool pr_orig_txt) |
|
817 { |
|
818 symbol_record *sym_rec = lookup_by_name (name, 0); |
|
819 |
|
820 if (sym_rec && sym_rec->is_defined ()) |
3356
|
821 sym_rec->type (os, pr_type_info, quiet, pr_orig_txt); |
3355
|
822 else |
|
823 { |
3523
|
824 std::string ff = fcn_file_in_path (name); |
3355
|
825 |
|
826 if (! ff.empty ()) |
|
827 { |
3538
|
828 std::ifstream fs (ff.c_str (), std::ios::in); |
3355
|
829 |
|
830 if (fs) |
|
831 { |
|
832 if (pr_type_info && ! quiet) |
|
833 os << name << " is the script file: " << ff << "\n\n"; |
|
834 |
|
835 char ch; |
|
836 |
|
837 while (fs.get (ch)) |
|
838 os << ch; |
|
839 } |
|
840 else |
|
841 os << "unable to open `" << ff << "' for reading!\n"; |
|
842 } |
|
843 else |
|
844 error ("type: `%s' undefined", name.c_str ()); |
|
845 } |
|
846 } |
|
847 |
1957
|
848 DEFUN_TEXT (type, args, nargout, |
3361
|
849 "-*- texinfo -*-\n\ |
|
850 \n\ |
|
851 @deffn {Command} type options name @dots{}\n\ |
|
852 Display the definition of each @var{name} that refers to a function.\n\ |
|
853 \n\ |
|
854 Normally also displays if each @var{name} is user-defined or builtin;\n\ |
|
855 the @code{-q} option suppresses this behaviour.\n\ |
581
|
856 \n\ |
3361
|
857 Currently, Octave can only display functions that can be compiled\n\ |
|
858 cleanly, because it uses its internal representation of the function to\n\ |
|
859 recreate the program text.\n\ |
|
860 \n\ |
|
861 Comments are not displayed because Octave's parser currently discards\n\ |
|
862 them as it converts the text of a function file to its internal\n\ |
|
863 representation. This problem may be fixed in a future release.\n\ |
|
864 @end deffn") |
581
|
865 { |
3584
|
866 octave_value retval; |
1588
|
867 |
1755
|
868 int argc = args.length () + 1; |
|
869 |
1968
|
870 string_vector argv = args.make_argv ("type"); |
1755
|
871 |
3355
|
872 if (! error_state) |
581
|
873 { |
3355
|
874 if (argc > 1) |
|
875 { |
|
876 // XXX FIXME XXX -- we should really use getopt () |
2532
|
877 |
3355
|
878 bool quiet = false; |
|
879 bool pr_orig_txt = true; |
2532
|
880 |
3355
|
881 int idx; |
581
|
882 |
3355
|
883 for (idx = 1; idx < argc; idx++) |
1281
|
884 { |
3355
|
885 if (argv[idx] == "-q" || argv[idx] == "-quiet") |
|
886 quiet = true; |
|
887 else if (argv[idx] == "-t" || argv[idx] == "-transformed") |
|
888 pr_orig_txt = false; |
|
889 else |
|
890 break; |
1281
|
891 } |
|
892 |
3355
|
893 if (idx < argc) |
|
894 { |
4051
|
895 OSSTREAM output_buf; |
581
|
896 |
3355
|
897 for (int i = idx; i < argc; i++) |
581
|
898 { |
3523
|
899 std::string id = argv[i]; |
2534
|
900 |
3355
|
901 if (nargout == 0) |
|
902 do_type (octave_stdout, id, true, quiet, pr_orig_txt); |
|
903 else |
|
904 do_type (output_buf, id, false, quiet, pr_orig_txt); |
581
|
905 |
3355
|
906 if (error_state) |
|
907 goto abort; |
581
|
908 } |
|
909 |
3584
|
910 if (nargout != 0) |
625
|
911 { |
4051
|
912 output_buf << OSSTREAM_ENDS; |
3164
|
913 |
4051
|
914 retval = OSSTREAM_STR (output_buf); |
3164
|
915 |
4051
|
916 OSSTREAM_FREEZE (output_buf); |
625
|
917 } |
581
|
918 } |
|
919 else |
3355
|
920 print_usage ("type"); |
|
921 } |
|
922 else |
|
923 print_usage ("type"); |
|
924 } |
3141
|
925 |
3355
|
926 abort: |
581
|
927 |
|
928 return retval; |
|
929 } |
|
930 |
3536
|
931 static std::string |
3523
|
932 do_which (const std::string& name) |
3355
|
933 { |
3523
|
934 std::string retval; |
3355
|
935 |
|
936 symbol_record *sym_rec = lookup_by_name (name, 0); |
|
937 |
|
938 if (sym_rec && sym_rec->is_defined ()) |
|
939 retval = sym_rec->which (); |
|
940 else |
|
941 { |
3523
|
942 std::string path = fcn_file_in_path (name); |
3355
|
943 |
|
944 if (! path.empty ()) |
|
945 retval = path; |
|
946 else |
|
947 retval = "undefined"; |
|
948 } |
|
949 |
|
950 return retval; |
|
951 } |
|
952 |
|
953 static void |
3523
|
954 do_which (std::ostream& os, const std::string& name) |
3355
|
955 { |
|
956 symbol_record *sym_rec = lookup_by_name (name, 0); |
|
957 |
|
958 if (sym_rec && sym_rec->is_defined ()) |
|
959 sym_rec->which (os); |
|
960 else |
|
961 { |
3523
|
962 std::string path = fcn_file_in_path (name); |
3355
|
963 |
|
964 if (! path.empty ()) |
|
965 os << "which: `" << name << "' is the script file\n" |
|
966 << path << "\n"; |
|
967 else |
|
968 os << "which: `" << name << "' is undefined\n"; |
|
969 } |
|
970 } |
|
971 |
1957
|
972 DEFUN_TEXT (which, args, nargout, |
3361
|
973 "-*- texinfo -*-\n\ |
|
974 @deffn {Command} which name @dots{}\n\ |
|
975 Display the type of each @var{name}. If @var{name} is defined from a\n\ |
|
976 function file, the full name of the file is also displayed.\n\ |
|
977 @end deffn") |
581
|
978 { |
2086
|
979 octave_value_list retval; |
581
|
980 |
1968
|
981 string_vector argv = args.make_argv ("which"); |
1755
|
982 |
3355
|
983 if (! error_state) |
|
984 { |
|
985 int argc = argv.length (); |
581
|
986 |
|
987 if (nargout > 0) |
|
988 retval.resize (argc-1, Matrix ()); |
|
989 |
3355
|
990 if (argc > 1) |
581
|
991 { |
3355
|
992 for (int i = 1; i < argc; i++) |
581
|
993 { |
3523
|
994 std::string id = argv[i]; |
3141
|
995 |
3355
|
996 if (nargout == 0) |
|
997 do_which (octave_stdout, id); |
581
|
998 else |
3355
|
999 retval(i-1) = do_which (id); |
581
|
1000 } |
|
1001 } |
3355
|
1002 else |
|
1003 print_usage (argv[0]); |
581
|
1004 } |
|
1005 |
|
1006 return retval; |
|
1007 } |
|
1008 |
2189
|
1009 static int |
2202
|
1010 info_file (void) |
|
1011 { |
|
1012 int status = 0; |
|
1013 |
3523
|
1014 std::string s = builtin_string_variable ("INFO_FILE"); |
2202
|
1015 |
|
1016 if (s.empty ()) |
|
1017 { |
|
1018 gripe_invalid_value_specified ("INFO_FILE"); |
|
1019 status = -1; |
|
1020 } |
|
1021 else |
|
1022 Vinfo_file = s; |
|
1023 |
|
1024 return status; |
|
1025 } |
|
1026 |
|
1027 static int |
|
1028 info_prog (void) |
|
1029 { |
|
1030 int status = 0; |
|
1031 |
3523
|
1032 std::string s = builtin_string_variable ("INFO_PROGRAM"); |
2202
|
1033 |
|
1034 if (s.empty ()) |
|
1035 { |
|
1036 gripe_invalid_value_specified ("INFO_PROGRAM"); |
|
1037 status = -1; |
|
1038 } |
|
1039 else |
|
1040 Vinfo_prog = s; |
|
1041 |
|
1042 return status; |
|
1043 } |
|
1044 |
3014
|
1045 static int |
3686
|
1046 makeinfo_prog (void) |
|
1047 { |
|
1048 int status = 0; |
|
1049 |
|
1050 std::string s = builtin_string_variable ("MAKEINFO_PROGRAM"); |
|
1051 |
|
1052 if (s.empty ()) |
|
1053 { |
|
1054 gripe_invalid_value_specified ("MAKEINFO_PROGRAM"); |
|
1055 status = -1; |
|
1056 } |
|
1057 else |
|
1058 Vmakeinfo_prog = s; |
|
1059 |
|
1060 return status; |
|
1061 } |
|
1062 |
|
1063 static int |
3014
|
1064 suppress_verbose_help_message (void) |
|
1065 { |
|
1066 Vsuppress_verbose_help_message |
|
1067 = check_preference ("suppress_verbose_help_message"); |
|
1068 |
|
1069 return 0; |
|
1070 } |
|
1071 |
2189
|
1072 void |
|
1073 symbols_of_help (void) |
|
1074 { |
3258
|
1075 DEFVAR (INFO_FILE, Vinfo_file, info_file, |
3332
|
1076 "-*- texinfo -*-\n\ |
|
1077 @defvr {Built-in Variable} INFO_FILE\n\ |
|
1078 The variable @code{INFO_FILE} names the location of the Octave info file.\n\ |
3686
|
1079 The default value is @code{\"@var{octave-home}/info/octave.info\"}, in\n\ |
|
1080 which @var{octave-home} is the directory where all of Octave is installed.\n\ |
3333
|
1081 @end defvr"); |
2202
|
1082 |
3258
|
1083 DEFVAR (INFO_PROGRAM, Vinfo_prog, info_prog, |
3332
|
1084 "-*- texinfo -*-\n\ |
3686
|
1085 @defvr {Built-in Variable} INFO_PROGRAM\n\ |
|
1086 The variable @code{INFO_PROGRAM} names the info program to run. Its\n\ |
|
1087 default initial value is\n\ |
|
1088 @code{\"@var{octave-home}/libexec/octave/@var{version}/exec/@var{arch}/info\"}\n\ |
|
1089 in which @var{octave-home} is the directory where all of Octave is\n\ |
|
1090 installed, @var{version} is the Octave version number, and @var{arch}\n\ |
|
1091 is the system type (for example, @code{i686-pc-linux-gnu}). The\n\ |
|
1092 default initial value may be overridden by the environment variable\n\ |
|
1093 @code{OCTAVE_INFO_PROGRAM}, or the command line argument\n\ |
|
1094 @code{--info-program NAME}, or by setting the value of\n\ |
|
1095 @code{INFO_PROGRAM} in a startup script\n\ |
|
1096 @end defvr"); |
|
1097 |
|
1098 DEFVAR (MAKEINFO_PROGRAM, Vmakeinfo_prog, makeinfo_prog, |
|
1099 "-*- texinfo -*-\n\ |
|
1100 @defvr {Built-in Variable} MAKEINFO_PROGRAM\n\ |
|
1101 The variable @code{MAKEINFO_PROGRAM} names the makeinfo program that\n\ |
|
1102 Octave runs to format help text that contains Texinfo markup commands.\n\ |
|
1103 Its default initial value is @code{\"makeinfo\"}.\n\ |
3333
|
1104 @end defvr"); |
2202
|
1105 |
3258
|
1106 DEFVAR (suppress_verbose_help_message, 0.0, suppress_verbose_help_message, |
3332
|
1107 "-*- texinfo -*-\n\ |
|
1108 @defvr {Built-in Variable} suppress_verbose_help_message\n\ |
|
1109 If the value of @code{suppress_verbose_help_message} is nonzero, Octave\n\ |
|
1110 will not add additional help information to the end of the output from\n\ |
|
1111 the @code{help} command and usage messages for built-in commands.\n\ |
3333
|
1112 @end defvr"); |
2189
|
1113 } |
|
1114 |
1
|
1115 /* |
|
1116 ;;; Local Variables: *** |
|
1117 ;;; mode: C++ *** |
|
1118 ;;; End: *** |
|
1119 */ |