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