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