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 |
|
682 static void |
|
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, , |
3168
|
830 "help [-i] [topic ...]\n\ |
|
831 \n\ |
|
832 print cryptic yet witty messages") |
529
|
833 { |
2086
|
834 octave_value_list retval; |
529
|
835 |
1755
|
836 int argc = args.length () + 1; |
|
837 |
1968
|
838 string_vector argv = args.make_argv ("help"); |
1755
|
839 |
|
840 if (error_state) |
|
841 return retval; |
529
|
842 |
|
843 if (argc == 1) |
3014
|
844 simple_help (); |
529
|
845 else |
|
846 { |
1755
|
847 if (argv[1] == "-i") |
3014
|
848 help_from_info (argv, 2, argc); |
529
|
849 else |
3014
|
850 builtin_help (argc, argv); |
529
|
851 } |
|
852 |
|
853 return retval; |
|
854 } |
|
855 |
1957
|
856 DEFUN_TEXT (type, args, nargout, |
2962
|
857 "type NAME\n\ |
581
|
858 \n\ |
|
859 display the definition of each NAME that refers to a function") |
|
860 { |
2086
|
861 octave_value_list retval; |
581
|
862 |
2985
|
863 unwind_protect::begin_frame ("Ftype"); |
1588
|
864 |
1755
|
865 int argc = args.length () + 1; |
|
866 |
1968
|
867 string_vector argv = args.make_argv ("type"); |
1755
|
868 |
|
869 if (error_state) |
|
870 return retval; |
581
|
871 |
|
872 if (argc > 1) |
|
873 { |
1358
|
874 // XXX FIXME XXX -- we should really use getopt () |
|
875 |
2532
|
876 bool quiet = false; |
|
877 bool pr_orig_txt = true; |
|
878 |
|
879 int idx; |
|
880 |
|
881 for (idx = 1; idx < argc; idx++) |
581
|
882 { |
2532
|
883 if (argv[idx] == "-q" || argv[idx] == "-quiet") |
|
884 quiet = true; |
|
885 else if (argv[idx] == "-t" || argv[idx] == "-transformed") |
|
886 pr_orig_txt = false; |
|
887 else |
|
888 break; |
|
889 } |
|
890 |
|
891 if (idx == argc) |
|
892 { |
|
893 print_usage ("type"); |
|
894 return retval; |
581
|
895 } |
|
896 |
|
897 ostrstream output_buf; |
|
898 |
1755
|
899 for (int i = idx; i < argc; i++) |
581
|
900 { |
1755
|
901 string id = argv[i]; |
|
902 string elts; |
581
|
903 |
1755
|
904 if (id[id.length () - 1] != '.') |
1281
|
905 { |
1755
|
906 size_t pos = id.find ('.'); |
|
907 |
|
908 if (pos != NPOS) |
1288
|
909 { |
1889
|
910 elts = id.substr (pos+1); |
1755
|
911 id = id.substr (0, pos); |
1288
|
912 } |
1281
|
913 } |
|
914 |
|
915 symbol_record *sym_rec = lookup_by_name (id, 0); |
581
|
916 |
3141
|
917 if (sym_rec && sym_rec->is_defined ()) |
581
|
918 { |
|
919 if (sym_rec->is_user_function ()) |
|
920 { |
2976
|
921 octave_value tmp = sym_rec->def (); |
|
922 |
|
923 octave_function *defn = tmp.function_value (); |
2962
|
924 |
2976
|
925 string fn = defn ? defn->fcn_file_name () : string (); |
581
|
926 |
2976
|
927 string ff = fn.empty () ? string () : fcn_file_in_path (fn); |
2534
|
928 |
|
929 if (pr_orig_txt && ! ff.empty ()) |
|
930 { |
|
931 ifstream fs (ff.c_str (), ios::in); |
|
932 |
|
933 if (fs) |
|
934 { |
|
935 if (nargout == 0 && ! quiet) |
|
936 output_buf << argv[i] |
3141
|
937 << " is the function defined from: " |
2534
|
938 << ff << "\n\n"; |
|
939 |
|
940 char ch; |
581
|
941 |
2534
|
942 while (fs.get (ch)) |
|
943 output_buf << ch; |
|
944 } |
|
945 else |
|
946 output_buf << "unable to open `" << ff |
|
947 << "' for reading!\n"; |
|
948 } |
|
949 else |
|
950 { |
|
951 if (nargout == 0 && ! quiet) |
|
952 output_buf << argv[i] |
|
953 << " is a user-defined function:\n\n"; |
2124
|
954 |
2534
|
955 tree_print_code tpc (output_buf, "", pr_orig_txt); |
|
956 |
2962
|
957 defn->accept (tpc); |
2534
|
958 } |
581
|
959 } |
|
960 |
1358
|
961 // XXX FIXME XXX -- this code should be shared with |
|
962 // Fwhich. |
581
|
963 |
|
964 else if (sym_rec->is_text_function ()) |
3259
|
965 output_buf << argv[i] << " is a built-in text-function\n"; |
581
|
966 else if (sym_rec->is_builtin_function ()) |
3259
|
967 output_buf << argv[i] << " is a built-in function\n"; |
1281
|
968 else if (sym_rec->is_user_variable () |
3259
|
969 || sym_rec->is_builtin_variable () |
|
970 || sym_rec->is_builtin_constant ()) |
625
|
971 { |
2976
|
972 octave_value defn = sym_rec->def (); |
625
|
973 |
1281
|
974 int var_ok = 1; |
2875
|
975 |
|
976 // XXX FIXME XXX -- need to handle structure |
|
977 // references correctly. |
|
978 |
3246
|
979 //if (defn.is_map ()) |
|
980 // error ("type: operations on structs not implemented"); |
1288
|
981 |
2875
|
982 if (! error_state) |
1281
|
983 { |
2875
|
984 if (nargout == 0 && ! quiet) |
1288
|
985 { |
2875
|
986 if (var_ok) |
|
987 { |
|
988 output_buf << argv[i]; |
3014
|
989 |
2875
|
990 if (sym_rec->is_user_variable ()) |
|
991 output_buf << " is a user-defined variable\n"; |
3259
|
992 else if (sym_rec->is_builtin_variable ()) |
|
993 output_buf << " is a built-in variable\n"; |
|
994 else if (sym_rec->is_builtin_constant ()) |
|
995 output_buf << " is a built-in constant\n"; |
2875
|
996 else |
3259
|
997 panic_impossible (); |
2875
|
998 } |
1288
|
999 else |
2875
|
1000 { |
|
1001 if (! elts.empty ()) |
|
1002 output_buf << "type: structure `" << id |
|
1003 << "' has no member `" << elts |
|
1004 << "'\n"; |
|
1005 else |
|
1006 output_buf << "type: `" << id |
|
1007 << "' has unknown type!\n"; |
|
1008 } |
1288
|
1009 } |
3164
|
1010 |
|
1011 defn.print_raw (output_buf, true); |
|
1012 |
|
1013 if (nargout == 0) |
|
1014 output_buf << "\n"; |
1281
|
1015 } |
625
|
1016 } |
581
|
1017 else |
3164
|
1018 error ("type: `%s' has unknown type!", argv[i].c_str ()); |
581
|
1019 } |
|
1020 else |
3141
|
1021 { |
|
1022 string ff = fcn_file_in_path (argv[i]); |
|
1023 |
|
1024 if (! ff.empty ()) |
|
1025 { |
|
1026 ifstream fs (ff.c_str (), ios::in); |
|
1027 |
|
1028 if (fs) |
|
1029 { |
|
1030 if (nargout == 0 && ! quiet) |
|
1031 output_buf << argv[i] << " is the script file: " |
|
1032 << ff << "\n\n"; |
|
1033 |
|
1034 char ch; |
|
1035 |
|
1036 while (fs.get (ch)) |
|
1037 output_buf << ch; |
|
1038 } |
|
1039 else |
|
1040 output_buf << "unable to open `" << ff |
|
1041 << "' for reading!\n"; |
|
1042 } |
|
1043 else |
3164
|
1044 error ("type: `%s' undefined", argv[i].c_str ()); |
3141
|
1045 } |
581
|
1046 } |
|
1047 |
|
1048 output_buf << ends; |
|
1049 |
2095
|
1050 char *s = output_buf.str (); |
|
1051 |
581
|
1052 if (nargout == 0) |
2095
|
1053 octave_stdout << s; |
581
|
1054 else |
2095
|
1055 retval = s; |
|
1056 |
|
1057 delete [] s; |
581
|
1058 } |
|
1059 else |
|
1060 print_usage ("type"); |
|
1061 |
|
1062 return retval; |
|
1063 } |
|
1064 |
1957
|
1065 DEFUN_TEXT (which, args, nargout, |
1565
|
1066 "which NAME ...\n\ |
581
|
1067 \n\ |
|
1068 display the type of each NAME. If NAME is defined from an function\n\ |
|
1069 file, print the full name of the file.") |
|
1070 { |
2086
|
1071 octave_value_list retval; |
581
|
1072 |
1755
|
1073 int argc = args.length () + 1; |
|
1074 |
1968
|
1075 string_vector argv = args.make_argv ("which"); |
1755
|
1076 |
|
1077 if (error_state) |
|
1078 return retval; |
581
|
1079 |
|
1080 if (argc > 1) |
|
1081 { |
|
1082 if (nargout > 0) |
|
1083 retval.resize (argc-1, Matrix ()); |
|
1084 |
1755
|
1085 for (int i = 1; i < argc; i++) |
581
|
1086 { |
1755
|
1087 symbol_record *sym_rec = lookup_by_name (argv[i], 0); |
581
|
1088 |
3141
|
1089 if (sym_rec && sym_rec->is_defined ()) |
581
|
1090 { |
742
|
1091 int print = (nargout == 0); |
3014
|
1092 |
2095
|
1093 string tmp = print_symbol_type (octave_stdout, sym_rec, |
1755
|
1094 argv[i], print); |
742
|
1095 if (! print) |
|
1096 retval(i) = tmp; |
581
|
1097 } |
|
1098 else |
|
1099 { |
3141
|
1100 string path = fcn_file_in_path (argv[i]); |
|
1101 |
|
1102 if (! path.empty ()) |
|
1103 { |
|
1104 if (nargout == 0) |
|
1105 octave_stdout << "which: `" << argv[i] |
|
1106 << "' is the script file: " |
|
1107 << path << "\n"; |
|
1108 else |
|
1109 retval(i) = path; |
|
1110 } |
581
|
1111 else |
3141
|
1112 { |
|
1113 if (nargout == 0) |
|
1114 octave_stdout << "which: `" << argv[i] |
|
1115 << "' is undefined\n"; |
|
1116 else |
|
1117 retval(i) = "undefined"; |
|
1118 } |
581
|
1119 } |
|
1120 } |
|
1121 } |
|
1122 else |
|
1123 print_usage ("which"); |
|
1124 |
|
1125 return retval; |
|
1126 } |
|
1127 |
2189
|
1128 static int |
2202
|
1129 info_file (void) |
|
1130 { |
|
1131 int status = 0; |
|
1132 |
|
1133 string s = builtin_string_variable ("INFO_FILE"); |
|
1134 |
|
1135 if (s.empty ()) |
|
1136 { |
|
1137 gripe_invalid_value_specified ("INFO_FILE"); |
|
1138 status = -1; |
|
1139 } |
|
1140 else |
|
1141 Vinfo_file = s; |
|
1142 |
|
1143 return status; |
|
1144 } |
|
1145 |
|
1146 static int |
|
1147 info_prog (void) |
|
1148 { |
|
1149 int status = 0; |
|
1150 |
|
1151 string s = builtin_string_variable ("INFO_PROGRAM"); |
|
1152 |
|
1153 if (s.empty ()) |
|
1154 { |
|
1155 gripe_invalid_value_specified ("INFO_PROGRAM"); |
|
1156 status = -1; |
|
1157 } |
|
1158 else |
|
1159 Vinfo_prog = s; |
|
1160 |
|
1161 return status; |
|
1162 } |
|
1163 |
3014
|
1164 static int |
|
1165 suppress_verbose_help_message (void) |
|
1166 { |
|
1167 Vsuppress_verbose_help_message |
|
1168 = check_preference ("suppress_verbose_help_message"); |
|
1169 |
|
1170 return 0; |
|
1171 } |
|
1172 |
2189
|
1173 void |
|
1174 symbols_of_help (void) |
|
1175 { |
3258
|
1176 DEFVAR (INFO_FILE, Vinfo_file, info_file, |
2202
|
1177 "name of the Octave info file"); |
|
1178 |
3258
|
1179 DEFVAR (INFO_PROGRAM, Vinfo_prog, info_prog, |
2202
|
1180 "name of the Octave info reader"); |
|
1181 |
3258
|
1182 DEFVAR (suppress_verbose_help_message, 0.0, suppress_verbose_help_message, |
2189
|
1183 "suppress printing of message pointing to additional help in the\n\ |
|
1184 help and usage functions"); |
|
1185 } |
|
1186 |
1
|
1187 /* |
|
1188 ;;; Local Variables: *** |
|
1189 ;;; mode: C++ *** |
|
1190 ;;; End: *** |
|
1191 */ |