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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
1
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
1
|
26 #endif |
|
27 |
1343
|
28 #include <cstdlib> |
|
29 #include <cstring> |
|
30 |
3503
|
31 #include <iostream> |
|
32 #include <fstream> |
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" |
4051
|
44 #include "lo-sstream.h" |
2926
|
45 #include "oct-env.h" |
1755
|
46 #include "str-vec.h" |
|
47 |
2492
|
48 #include <defaults.h> |
1352
|
49 #include "defun.h" |
|
50 #include "dirfns.h" |
|
51 #include "error.h" |
2233
|
52 #include "fn-cache.h" |
2202
|
53 #include "gripes.h" |
1352
|
54 #include "help.h" |
2177
|
55 #include "input.h" |
1755
|
56 #include "oct-obj.h" |
2976
|
57 #include "ov-usr-fcn.h" |
1352
|
58 #include "pager.h" |
3018
|
59 #include "parse.h" |
1466
|
60 #include "pathsearch.h" |
3295
|
61 #include "procstream.h" |
529
|
62 #include "sighandlers.h" |
|
63 #include "symtab.h" |
2694
|
64 #include "syswait.h" |
1755
|
65 #include "toplev.h" |
242
|
66 #include "utils.h" |
1352
|
67 #include "variables.h" |
3301
|
68 #include "version.h" |
5447
|
69 #include "quit.h" |
529
|
70 |
2202
|
71 // Name of the info file specified on command line. |
|
72 // (--info-file file) |
3523
|
73 std::string Vinfo_file; |
2202
|
74 |
|
75 // Name of the info reader we'd like to use. |
|
76 // (--info-program program) |
3523
|
77 std::string Vinfo_prog; |
2202
|
78 |
3686
|
79 // Name of the makeinfo program to run. |
|
80 static std::string Vmakeinfo_prog = "makeinfo"; |
|
81 |
2189
|
82 // If TRUE, don't print additional help message in help and usage |
|
83 // functions. |
|
84 static bool Vsuppress_verbose_help_message; |
|
85 |
3016
|
86 // XXX FIXME XXX -- maybe this should use string instead of char*. |
|
87 |
|
88 struct help_list |
|
89 { |
|
90 const char *name; |
|
91 const char *help; |
|
92 }; |
|
93 |
1
|
94 static help_list operators[] = |
|
95 { |
|
96 { "!", |
|
97 "Logical not operator. See also `~'.\n", }, |
|
98 |
|
99 { "!=", |
|
100 "Logical not equals operator. See also `~' and `<>'.\n", }, |
|
101 |
|
102 { "\"", |
|
103 "String delimiter.\n", }, |
|
104 |
|
105 { "#", |
928
|
106 "Begin comment character. See also `%'.", }, |
1
|
107 |
|
108 { "%", |
928
|
109 "Begin comment charcter. See also `#'.", }, |
1
|
110 |
|
111 { "&", |
928
|
112 "Logical and operator. See also `&&'.", }, |
1
|
113 |
|
114 { "&&", |
928
|
115 "Logical and operator. See also `&'.", }, |
1
|
116 |
|
117 { "'", |
|
118 "Matrix transpose operator. For complex matrices, computes the\n\ |
|
119 complex conjugate (Hermitian) transpose. See also `.''\n\ |
|
120 \n\ |
|
121 The single quote character may also be used to delimit strings, but\n\ |
|
122 it is better to use the double quote character, since that is never\n\ |
928
|
123 ambiguous", }, |
1
|
124 |
|
125 { "(", |
928
|
126 "Array index or function argument delimiter.", }, |
1
|
127 |
|
128 { ")", |
928
|
129 "Array index or function argument delimiter.", }, |
1
|
130 |
|
131 { "*", |
928
|
132 "Multiplication operator. See also `.*'", }, |
1
|
133 |
|
134 { "**", |
928
|
135 "Power operator. See also `^', `.**', and `.^'", }, |
1
|
136 |
|
137 { "+", |
928
|
138 "Addition operator.", }, |
1
|
139 |
|
140 { "++", |
5339
|
141 "Increment operator. As in C, may be applied as a prefix or postfix\n\ |
|
142 operator.", }, |
1
|
143 |
|
144 { ",", |
928
|
145 "Array index, function argument, or command separator.", }, |
1
|
146 |
|
147 { "-", |
928
|
148 "Subtraction or unary negation operator.", }, |
1
|
149 |
|
150 { "--", |
5339
|
151 "Decrement operator. As in C, may be applied as a prefix or postfix\n\ |
|
152 operator.", }, |
1
|
153 |
|
154 { ".'", |
|
155 "Matrix transpose operator. For complex matrices, computes the\n\ |
928
|
156 transpose, *not* the complex conjugate transpose. See also `''.", }, |
1
|
157 |
|
158 { ".*", |
928
|
159 "Element by element multiplication operator. See also `*'.", }, |
1
|
160 |
|
161 { ".**", |
928
|
162 "Element by element power operator. See also `**', `^', and `.^'.", }, |
1
|
163 |
|
164 { "./", |
928
|
165 "Element by element division operator. See also `/' and `\\'.", }, |
1
|
166 |
|
167 { ".^", |
928
|
168 "Element by element power operator. See also `**', `^', and `.^'.", }, |
1
|
169 |
|
170 { "/", |
928
|
171 "Right division. See also `\\' and `./'.", }, |
1
|
172 |
|
173 { ":", |
928
|
174 "Select entire rows or columns of matrices.", }, |
1
|
175 |
|
176 { ";", |
928
|
177 "Array row or command separator. See also `,'.", }, |
1
|
178 |
|
179 { "<", |
928
|
180 "Less than operator.", }, |
1
|
181 |
|
182 { "<=", |
928
|
183 "Less than or equals operator.", }, |
1
|
184 |
|
185 { "<>", |
928
|
186 "Logical not equals operator. See also `!=' and `~='.", }, |
1
|
187 |
|
188 { "=", |
928
|
189 "Assignment operator.", }, |
1
|
190 |
|
191 { "==", |
928
|
192 "Equality test operator.", }, |
1
|
193 |
|
194 { ">", |
928
|
195 "Greater than operator.", }, |
1
|
196 |
|
197 { ">=", |
928
|
198 "Greater than or equals operator.", }, |
1
|
199 |
|
200 { "[", |
928
|
201 "Return list delimiter. See also `]'.", }, |
1
|
202 |
|
203 { "\\", |
928
|
204 "Left division operator. See also `/' and `./'.", }, |
1
|
205 |
|
206 { "]", |
928
|
207 "Return list delimiter. See also `['.", }, |
1
|
208 |
|
209 { "^", |
928
|
210 "Power operator. See also `**', `.^', and `.**.'", }, |
1
|
211 |
|
212 { "|", |
928
|
213 "Logical or operator. See also `||'.", }, |
1
|
214 |
|
215 { "||", |
928
|
216 "Logical or operator. See also `|'.", }, |
1
|
217 |
|
218 { "~", |
928
|
219 "Logical not operator. See also `!' and `~'.", }, |
1
|
220 |
|
221 { "~=", |
928
|
222 "Logical not equals operator. See also `<>' and `!='.", }, |
1
|
223 |
529
|
224 { 0, 0, }, |
1
|
225 }; |
|
226 |
|
227 static help_list keywords[] = |
|
228 { |
928
|
229 { "all_va_args", |
|
230 "Pass all unnamed arguments to another function call.", }, |
|
231 |
1
|
232 { "break", |
5040
|
233 "Exit the innermost enclosing do, while or for loop.", }, |
|
234 |
|
235 { "case", |
|
236 "A case statement in an switch. Octave cases are exclusive and do not\n\ |
|
237 fall-through as do C-language cases. A switch statement must have at least\n\ |
|
238 one case.",}, |
1
|
239 |
1489
|
240 { "catch", |
|
241 "begin the cleanup part of a try-catch block", }, |
|
242 |
1
|
243 { "continue", |
5040
|
244 "Jump to the end of the innermost enclosing do, while or for loop.", }, |
|
245 |
|
246 { "do", |
|
247 "Begin a do-until loop. This differs from a do-while loop in that the\n\ |
|
248 body of the loop is executed at least once.",}, |
1
|
249 |
|
250 { "else", |
928
|
251 "Alternate action for an if block.", }, |
1
|
252 |
|
253 { "elseif", |
928
|
254 "Alternate conditional test for an if block.", }, |
1
|
255 |
|
256 { "end", |
5040
|
257 "Mark the end of any for, if, do, while, or function block.", }, |
928
|
258 |
1489
|
259 { "end_try_catch", |
|
260 "Mark the end of an try-catch block.", }, |
|
261 |
928
|
262 { "end_unwind_protect", |
|
263 "Mark the end of an unwind_protect block.", }, |
1
|
264 |
|
265 { "endfor", |
928
|
266 "Mark the end of a for loop.", }, |
1
|
267 |
|
268 { "endfunction", |
928
|
269 "Mark the end of a function.", }, |
1
|
270 |
|
271 { "endif", |
928
|
272 "Mark the end of an if block.", }, |
1
|
273 |
5122
|
274 { "endswitch", |
|
275 "Mark the end of a switch block.", }, |
|
276 |
1
|
277 { "endwhile", |
928
|
278 "Mark the end of a while loop.", }, |
1
|
279 |
|
280 { "for", |
928
|
281 "Begin a for loop.", }, |
1
|
282 |
|
283 { "function", |
928
|
284 "Begin a function body.", }, |
1
|
285 |
|
286 { "global", |
928
|
287 "Declare variables to have global scope.", }, |
1
|
288 |
|
289 { "gplot", |
928
|
290 "Produce 2-D plots using gnuplot-like command syntax.", }, |
1
|
291 |
|
292 { "gsplot", |
928
|
293 "Produce 3-D plots using gnuplot-like command syntax.", }, |
1
|
294 |
|
295 { "if", |
5393
|
296 "-*- texinfo -*-\n\ |
|
297 @deffn Keyword if (@var{cond}) @dots{} endif\n\ |
|
298 @deffnx Keyword if (@var{cond}) @dots{} else @dots{} endif\n\ |
|
299 @deffnx Keyword if (@var{cond}) @dots{} elseif (@var{cond}) @dots{} endif\n\ |
|
300 @deffnx Keyword if (@var{cond}) @dots{} elseif (@var{cond}) @dots{} else @dots{} endif\n\ |
|
301 Begin an if block.\n\ |
|
302 @end deffn\n\ |
|
303 @seealso{switch}", }, |
1
|
304 |
5040
|
305 { "otherwise", |
|
306 "The default statement in a switch block.", }, |
|
307 |
4686
|
308 { "persistent", |
|
309 "Declare variables as persistent.", }, |
|
310 |
5040
|
311 { "replot", |
|
312 "Replot a graphic.", }, |
|
313 |
1
|
314 { "return", |
928
|
315 "Return from a function.", }, |
|
316 |
5040
|
317 { "static", |
|
318 "Declare variables as persistent.", }, |
|
319 |
|
320 { "switch", |
|
321 "Begin a switch statement.",}, |
|
322 |
1489
|
323 { "try", |
|
324 "Begin a try-catch block.", }, |
|
325 |
5040
|
326 { "until", |
|
327 "End a do-until loop.",}, |
|
328 |
928
|
329 { "unwind_protect", |
|
330 "Begin an unwind_protect block.", }, |
|
331 |
|
332 { "unwind_protect_cleanup", |
|
333 "Begin the cleanup section of an unwind_protect block.", }, |
1
|
334 |
5040
|
335 { "varargin", |
5339
|
336 "Pass an arbitrary number of arguments into a function. See also\n\ |
|
337 varargout, nargin, and nargout.",}, |
5040
|
338 |
|
339 { "varargout", |
5339
|
340 "Pass an arbitrary number of arguments out of a function. See also\n\ |
|
341 varargin, nargin, and nargout.",}, |
5040
|
342 |
1
|
343 { "while", |
928
|
344 "Begin a while loop.", }, |
1
|
345 |
529
|
346 { 0, 0, }, |
1
|
347 }; |
|
348 |
581
|
349 // Return a copy of the operator or keyword names. |
|
350 |
3016
|
351 static string_vector |
3355
|
352 names (help_list *lst) |
1
|
353 { |
1755
|
354 string_vector retval; |
|
355 |
3355
|
356 int count = 0; |
1
|
357 help_list *ptr = lst; |
529
|
358 while (ptr->name) |
1
|
359 { |
|
360 count++; |
|
361 ptr++; |
|
362 } |
|
363 |
1755
|
364 if (count > 0) |
|
365 { |
|
366 retval.resize (count); |
1
|
367 |
1755
|
368 ptr = lst; |
|
369 for (int i = 0; i < count; i++) |
|
370 { |
|
371 retval[i] = ptr->name; |
|
372 ptr++; |
|
373 } |
1
|
374 } |
|
375 |
1755
|
376 return retval; |
1
|
377 } |
|
378 |
3014
|
379 static help_list * |
1
|
380 operator_help (void) |
|
381 { |
|
382 return operators; |
|
383 } |
|
384 |
3016
|
385 static help_list * |
1
|
386 keyword_help (void) |
|
387 { |
|
388 return keywords; |
|
389 } |
|
390 |
3016
|
391 // It's not likely that this does the right thing now. XXX FIXME XXX |
|
392 |
|
393 string_vector |
|
394 make_name_list (void) |
|
395 { |
3355
|
396 string_vector key = names (keyword_help ()); |
|
397 int key_len = key.length (); |
3016
|
398 |
4009
|
399 string_vector fbi = fbi_sym_tab->name_list (); |
|
400 int fbi_len = fbi.length (); |
|
401 |
3355
|
402 string_vector glb = global_sym_tab->name_list (); |
|
403 int glb_len = glb.length (); |
3016
|
404 |
3355
|
405 string_vector top = top_level_sym_tab->name_list (); |
|
406 int top_len = top.length (); |
3016
|
407 |
3355
|
408 string_vector lcl; |
3016
|
409 if (top_level_sym_tab != curr_sym_tab) |
3355
|
410 lcl = curr_sym_tab->name_list (); |
|
411 int lcl_len = lcl.length (); |
3016
|
412 |
3355
|
413 string_vector ffl = octave_fcn_file_name_cache::list_no_suffix (); |
3016
|
414 int ffl_len = ffl.length (); |
|
415 |
4009
|
416 int total_len = key_len + fbi_len + glb_len + top_len + lcl_len + ffl_len; |
3016
|
417 |
|
418 string_vector list (total_len); |
|
419 |
|
420 // Put all the symbols in one big list. |
|
421 |
|
422 int j = 0; |
|
423 int i = 0; |
|
424 for (i = 0; i < key_len; i++) |
|
425 list[j++] = key[i]; |
|
426 |
4009
|
427 for (i = 0; i < fbi_len; i++) |
|
428 list[j++] = fbi[i]; |
|
429 |
3016
|
430 for (i = 0; i < glb_len; i++) |
|
431 list[j++] = glb[i]; |
|
432 |
|
433 for (i = 0; i < top_len; i++) |
|
434 list[j++] = top[i]; |
|
435 |
|
436 for (i = 0; i < lcl_len; i++) |
|
437 list[j++] = lcl[i]; |
|
438 |
|
439 for (i = 0; i < ffl_len; i++) |
|
440 list[j++] = ffl[i]; |
|
441 |
|
442 return list; |
|
443 } |
|
444 |
3014
|
445 void |
3523
|
446 additional_help_message (std::ostream& os) |
2470
|
447 { |
|
448 if (! Vsuppress_verbose_help_message) |
|
449 os << "\n\ |
3259
|
450 Additional help for built-in functions, operators, and variables\n\ |
3160
|
451 is available in the on-line version of the manual. Use the command\n\ |
3295
|
452 `help -i <topic>' to search the manual index.\n\ |
|
453 \n\ |
3160
|
454 Help and information about Octave is also available on the WWW\n\ |
5041
|
455 at http://www.octave.org and via the help@octave.org\n\ |
3917
|
456 mailing list.\n"; |
3160
|
457 } |
|
458 |
2976
|
459 // XXX FIXME XXX -- this needs a major overhaul to cope with new |
|
460 // symbol table stuff. |
|
461 |
529
|
462 static void |
3523
|
463 display_names_from_help_list (std::ostream& os, help_list *list, |
542
|
464 const char *desc) |
529
|
465 { |
3355
|
466 string_vector symbols = names (list); |
3259
|
467 |
1808
|
468 if (! symbols.empty ()) |
|
469 { |
2095
|
470 os << "\n*** " << desc << ":\n\n"; |
3259
|
471 |
|
472 symbols.qsort (); |
|
473 |
2095
|
474 symbols.list_in_columns (os); |
1808
|
475 } |
529
|
476 } |
|
477 |
|
478 static void |
3523
|
479 display_symtab_names (std::ostream& os, const string_vector& names, |
|
480 const std::string& desc) |
542
|
481 { |
1808
|
482 if (! names.empty ()) |
|
483 { |
2095
|
484 os << "\n*** " << desc << ":\n\n"; |
|
485 names.list_in_columns (os); |
1808
|
486 } |
542
|
487 } |
|
488 |
|
489 #ifdef LIST_SYMBOLS |
|
490 #undef LIST_SYMBOLS |
|
491 #endif |
|
492 #define LIST_SYMBOLS(type, msg) \ |
|
493 do \ |
|
494 { \ |
3259
|
495 string_vector names \ |
4009
|
496 = fbi_sym_tab->name_list (string_vector (), true, type); \ |
3355
|
497 display_symtab_names (octave_stdout, names, msg); \ |
542
|
498 } \ |
|
499 while (0) |
529
|
500 |
3014
|
501 static void |
|
502 simple_help (void) |
|
503 { |
3160
|
504 octave_stdout << "Help is available for the topics listed below.\n"; |
|
505 |
|
506 additional_help_message (octave_stdout); |
|
507 |
3014
|
508 display_names_from_help_list (octave_stdout, operator_help (), |
|
509 "operators"); |
|
510 |
|
511 display_names_from_help_list (octave_stdout, keyword_help (), |
|
512 "reserved words"); |
|
513 |
1358
|
514 // XXX FIXME XXX -- is this distinction needed? |
|
515 |
3259
|
516 LIST_SYMBOLS (symbol_record::BUILTIN_CONSTANT, "built-in constants"); |
|
517 |
|
518 LIST_SYMBOLS (symbol_record::BUILTIN_VARIABLE, "built-in variables"); |
|
519 |
4208
|
520 LIST_SYMBOLS (symbol_record::COMMAND, "commands"); |
529
|
521 |
3010
|
522 LIST_SYMBOLS (symbol_record::MAPPER_FUNCTION, "mapper functions"); |
542
|
523 |
3010
|
524 LIST_SYMBOLS (symbol_record::BUILTIN_FUNCTION, "general functions"); |
542
|
525 |
1358
|
526 // Also need to list variables and currently compiled functions from |
|
527 // the symbol table, if there are any. |
529
|
528 |
1358
|
529 // Also need to search octave_path for script files. |
529
|
530 |
3195
|
531 string_vector dirs = Vload_path_dir_path.all_directories (); |
679
|
532 |
1787
|
533 int len = dirs.length (); |
679
|
534 |
1787
|
535 for (int i = 0; i < len; i++) |
|
536 { |
2233
|
537 string_vector names = octave_fcn_file_name_cache::list (dirs[i]); |
679
|
538 |
1787
|
539 if (! names.empty ()) |
|
540 { |
3523
|
541 std::string dir |
2926
|
542 = octave_env::make_absolute (dirs[i], octave_env::getcwd ()); |
|
543 |
|
544 octave_stdout << "\n*** function files in " << dir << ":\n\n"; |
679
|
545 |
3259
|
546 names.qsort (); |
|
547 |
2095
|
548 names.list_in_columns (octave_stdout); |
529
|
549 } |
|
550 } |
|
551 } |
|
552 |
|
553 static int |
3523
|
554 try_info (const std::string& nm) |
529
|
555 { |
|
556 int status = 0; |
|
557 |
4051
|
558 OSSTREAM cmd_buf; |
1295
|
559 |
4469
|
560 cmd_buf << "\"" << Vinfo_prog << "\" --file \"" << Vinfo_file << "\""; |
1295
|
561 |
3523
|
562 std::string directory_name = Vinfo_file; |
1755
|
563 size_t pos = directory_name.rfind ('/'); |
|
564 |
|
565 if (pos != NPOS) |
529
|
566 { |
1755
|
567 directory_name.resize (pos + 1); |
4469
|
568 cmd_buf << " --directory \"" << directory_name << "\""; |
529
|
569 } |
|
570 |
1755
|
571 if (nm.length () > 0) |
|
572 cmd_buf << " --index-search " << nm; |
529
|
573 |
4051
|
574 cmd_buf << OSSTREAM_ENDS; |
529
|
575 |
2705
|
576 volatile octave_interrupt_handler old_interrupt_handler |
2554
|
577 = octave_ignore_interrupts (); |
529
|
578 |
4051
|
579 status = system (OSSTREAM_C_STR (cmd_buf)); |
|
580 |
|
581 OSSTREAM_FREEZE (cmd_buf); |
529
|
582 |
2554
|
583 octave_set_interrupt_handler (old_interrupt_handler); |
529
|
584 |
2694
|
585 if (WIFEXITED (status)) |
|
586 status = WEXITSTATUS (status); |
1295
|
587 else |
|
588 status = 127; |
529
|
589 |
|
590 return status; |
|
591 } |
1140
|
592 |
|
593 static void |
1755
|
594 help_from_info (const string_vector& argv, int idx, int argc) |
1140
|
595 { |
1755
|
596 if (idx == argc) |
3523
|
597 try_info (std::string ()); |
1140
|
598 else |
|
599 { |
1755
|
600 for (int i = idx; i < argc; i++) |
1140
|
601 { |
1755
|
602 int status = try_info (argv[i]); |
1140
|
603 |
1295
|
604 if (status) |
1140
|
605 { |
3295
|
606 if (status == 127) |
1295
|
607 { |
3185
|
608 error ("help: unable to find info"); |
|
609 error ("help: you need info 2.18 or later (texinfo 3.12)"); |
1295
|
610 break; |
|
611 } |
3295
|
612 else |
|
613 { |
5399
|
614 message ("help", "`%s' is not indexed in the manual", |
3295
|
615 argv[i].c_str ()); |
|
616 } |
1140
|
617 } |
|
618 } |
|
619 } |
2470
|
620 } |
3014
|
621 |
|
622 static bool |
3523
|
623 looks_like_texinfo (const std::string& msg, size_t& p1) |
3295
|
624 { |
|
625 p1 = msg.find ('\n'); |
|
626 |
3523
|
627 std::string t = msg.substr (0, p1); |
3295
|
628 |
|
629 if (p1 == NPOS) |
|
630 p1 = 0; |
|
631 |
|
632 size_t p2 = t.find ("-*- texinfo -*-"); |
|
633 |
|
634 return (p2 != NPOS); |
|
635 } |
|
636 |
3330
|
637 void |
3523
|
638 display_help_text (std::ostream& os, const std::string& msg) |
3295
|
639 { |
|
640 // Look for "-*- texinfo -*-" in first line of help message. If it |
|
641 // is present, use makeinfo to format the rest of the message before |
|
642 // sending it to the output stream. Otherwise, just print the |
|
643 // message. |
|
644 |
|
645 size_t pos; |
|
646 |
|
647 if (looks_like_texinfo (msg, pos)) |
|
648 { |
3523
|
649 std::string tmp_file_name = file_ops::tempnam ("", ""); |
3295
|
650 |
|
651 int cols = command_editor::terminal_cols (); |
|
652 |
|
653 if (cols > 16) |
|
654 cols--; |
|
655 |
|
656 if (cols > 64) |
|
657 cols -= 7; |
|
658 |
|
659 if (cols > 80) |
|
660 cols = 72; |
|
661 |
4051
|
662 OSSTREAM buf; |
|
663 |
4115
|
664 buf << "sed -e 's/^[#%][#%]* *//' -e 's/^ *@/@/' | " |
4469
|
665 << "\"" << Vmakeinfo_prog << "\"" |
3303
|
666 << " -D \"VERSION " << OCTAVE_VERSION << "\"" |
|
667 << " -D \"OCTAVEHOME " << OCTAVE_PREFIX << "\"" |
3584
|
668 << " -D \"TARGETHOSTTYPE " << OCTAVE_CANONICAL_HOST_TYPE << "\"" |
3301
|
669 << " --fill-column " << cols |
|
670 << " --no-warn" |
|
671 << " --no-validate" |
|
672 << " --no-headers" |
|
673 << " --force" |
4108
|
674 << " --output \"" << tmp_file_name << "\"" |
3303
|
675 << " > /dev/null 2>&1" |
4051
|
676 << OSSTREAM_ENDS; |
3295
|
677 |
4051
|
678 oprocstream filter (OSSTREAM_STR (buf)); |
3295
|
679 |
4051
|
680 OSSTREAM_FREEZE (buf); |
3295
|
681 |
3686
|
682 if (filter && filter.is_open ()) |
3295
|
683 { |
3405
|
684 filter << "@macro seealso {args}\n" |
3408
|
685 << "\n" |
|
686 << "@noindent\n" |
3405
|
687 << "See also: \\args\\.\n" |
|
688 << "@end macro\n"; |
|
689 |
3769
|
690 filter << msg.substr (pos+1) << std::endl; |
3295
|
691 |
3686
|
692 int status = filter.close (); |
3295
|
693 |
3523
|
694 std::ifstream tmp_file (tmp_file_name.c_str ()); |
3295
|
695 |
3686
|
696 if (WIFEXITED (status) && WEXITSTATUS (status) == 0) |
|
697 { |
|
698 int c; |
|
699 while ((c = tmp_file.get ()) != EOF) |
|
700 os << (char) c; |
3295
|
701 |
3686
|
702 tmp_file.close (); |
|
703 } |
|
704 else |
|
705 { |
|
706 warning ("help: Texinfo formatting filter exited abnormally"); |
|
707 warning ("help: raw Texinfo source of help text follows..."); |
|
708 |
|
709 os << "\n" << msg; |
|
710 } |
3295
|
711 |
|
712 file_ops::unlink (tmp_file_name); |
|
713 } |
|
714 else |
|
715 os << msg; |
|
716 } |
|
717 else |
|
718 os << msg; |
|
719 } |
|
720 |
|
721 static bool |
3523
|
722 help_from_list (std::ostream& os, const help_list *list, |
5399
|
723 const std::string& nm, int usage, bool& symbol_found) |
542
|
724 { |
5399
|
725 bool retval = false; |
|
726 |
2804
|
727 const char *name; |
3014
|
728 |
542
|
729 while ((name = list->name) != 0) |
|
730 { |
1755
|
731 if (strcmp (name, nm.c_str ()) == 0) |
542
|
732 { |
5399
|
733 symbol_found = true; |
|
734 |
|
735 std::string h = list->help; |
|
736 |
|
737 if (h.length () > 0) |
542
|
738 { |
5399
|
739 if (usage) |
|
740 os << "\nusage: "; |
|
741 else |
|
742 os << "\n*** " << nm << ":\n\n"; |
|
743 |
|
744 display_help_text (os, h); |
|
745 |
|
746 os << "\n"; |
|
747 |
|
748 retval = true; |
542
|
749 } |
5399
|
750 break; |
542
|
751 } |
|
752 list++; |
|
753 } |
3014
|
754 |
5399
|
755 return retval;; |
|
756 } |
|
757 |
|
758 std::string |
|
759 extract_help_from_dispatch (const std::string& nm) |
|
760 { |
|
761 std::string retval; |
|
762 |
|
763 symbol_record *builtin = fbi_sym_tab->lookup ("builtin:" + nm, 0); |
|
764 |
|
765 if (builtin) |
|
766 { |
|
767 // Check that builtin is up to date. |
|
768 |
|
769 // Don't try to fight octave's function name handling |
|
770 // mechanism. Instead, move dispatch record out of the way, |
|
771 // and restore the builtin to its original name. |
|
772 symbol_record *dispatch = fbi_sym_tab->lookup (nm, 0); |
|
773 |
|
774 if (dispatch) |
|
775 { |
|
776 dispatch->unprotect (); |
|
777 |
|
778 fbi_sym_tab->rename (nm, "dispatch:" + nm); |
|
779 fbi_sym_tab->rename ("builtin:" + nm, nm); |
|
780 |
|
781 // Check for updates to builtin function; ignore errors |
|
782 // that appear (they interfere with renaming), and remove |
|
783 // the updated name from the current symbol table. XXX |
|
784 // FIXME XXX check that updating a function updates it in |
|
785 // all contexts --- it may be that it is updated only in the |
|
786 // current symbol table, and not the caller. I believe this |
|
787 // won't be a problem because the caller will go through the |
|
788 // same logic and end up with the newer version. |
|
789 |
|
790 octave_function *f = is_valid_function (nm); |
|
791 |
|
792 if (f) |
|
793 retval = builtin->help (); |
|
794 |
|
795 curr_sym_tab->clear_function (nm); |
|
796 |
|
797 // Move the builtin function out of the way and restore the |
|
798 // dispatch fuction. XXX FIXME XXX what if builtin wants to |
|
799 // protect itself? |
|
800 |
|
801 fbi_sym_tab->rename (nm, "builtin:" + nm); |
|
802 fbi_sym_tab->rename ("dispatch:" + nm, nm); |
|
803 |
|
804 dispatch->protect (); |
|
805 } |
|
806 else |
|
807 error ("failed to find dispatch record for `builtin:%s'", nm.c_str ()); |
|
808 } |
|
809 |
|
810 return retval; |
542
|
811 } |
|
812 |
3355
|
813 static bool |
5399
|
814 help_from_symbol_table (std::ostream& os, const std::string& nm, |
|
815 bool& symbol_found) |
3355
|
816 { |
|
817 bool retval = false; |
|
818 |
|
819 symbol_record *sym_rec = lookup_by_name (nm, 0); |
|
820 |
|
821 if (sym_rec && sym_rec->is_defined ()) |
|
822 { |
5399
|
823 symbol_found = true; |
|
824 |
3523
|
825 std::string h = sym_rec->help (); |
3355
|
826 |
|
827 if (h.length () > 0) |
|
828 { |
|
829 sym_rec->which (os); |
|
830 os << "\n"; |
5399
|
831 h = extract_help_from_dispatch (nm) + h; |
3355
|
832 display_help_text (os, h); |
|
833 os << "\n"; |
|
834 retval = true; |
|
835 } |
|
836 } |
|
837 |
|
838 return retval; |
|
839 } |
|
840 |
|
841 static bool |
5399
|
842 help_from_file (std::ostream& os, const std::string& nm, bool& symbol_found) |
3355
|
843 { |
|
844 bool retval = false; |
|
845 |
5399
|
846 std::string file = fcn_file_in_path (nm); |
3355
|
847 |
5399
|
848 if (file.length () > 0) |
|
849 { |
|
850 symbol_found = true; |
|
851 |
|
852 std::string h = get_help_from_file (file); |
3355
|
853 |
5399
|
854 if (h.length () > 0) |
|
855 { |
|
856 os << nm << " is the file: " << file << "\n\n"; |
|
857 display_help_text (os, h); |
|
858 os << "\n"; |
|
859 retval = true; |
|
860 } |
3355
|
861 } |
|
862 |
|
863 return retval; |
|
864 } |
|
865 |
1140
|
866 static void |
1755
|
867 builtin_help (int argc, const string_vector& argv) |
1140
|
868 { |
|
869 help_list *op_help_list = operator_help (); |
|
870 help_list *kw_help_list = keyword_help (); |
|
871 |
1755
|
872 for (int i = 1; i < argc; i++) |
1140
|
873 { |
5399
|
874 bool symbol_found = false; |
|
875 |
|
876 if (help_from_list (octave_stdout, op_help_list, argv[i], 0, |
|
877 symbol_found)) |
1140
|
878 continue; |
|
879 |
5399
|
880 if (help_from_list (octave_stdout, kw_help_list, argv[i], 0, |
|
881 symbol_found)) |
1140
|
882 continue; |
|
883 |
5399
|
884 if (help_from_symbol_table (octave_stdout, argv[i], symbol_found)) |
|
885 continue; |
|
886 |
|
887 if (help_from_file (octave_stdout, argv[i], symbol_found)) |
3355
|
888 continue; |
1755
|
889 |
5399
|
890 if (symbol_found) |
|
891 octave_stdout << "\nhelp: `" << argv[i] |
|
892 << "' is not documented\n"; |
|
893 else |
|
894 octave_stdout << "\nhelp: `" << argv[i] |
|
895 << "' not found\n"; |
1140
|
896 } |
|
897 |
2095
|
898 additional_help_message (octave_stdout); |
1140
|
899 } |
|
900 |
4208
|
901 DEFCMD (help, args, , |
3332
|
902 "-*- texinfo -*-\n\ |
|
903 @deffn {Command} help\n\ |
|
904 Octave's @code{help} command can be used to print brief usage-style\n\ |
|
905 messages, or to display information directly from an on-line version of\n\ |
|
906 the printed manual, using the GNU Info browser. If invoked without any\n\ |
|
907 arguments, @code{help} prints a list of all the available operators,\n\ |
|
908 functions, and built-in variables. If the first argument is @code{-i},\n\ |
|
909 the @code{help} command searches the index of the on-line version of\n\ |
|
910 this manual for the given topics.\n\ |
3168
|
911 \n\ |
3332
|
912 For example, the command @kbd{help help} prints a short message\n\ |
|
913 describing the @code{help} command, and @kbd{help -i help} starts the\n\ |
|
914 GNU Info browser at this node in the on-line version of the manual.\n\ |
|
915 \n\ |
|
916 Once the GNU Info browser is running, help for using it is available\n\ |
|
917 using the command @kbd{C-h}.\n\ |
3333
|
918 @end deffn") |
529
|
919 { |
2086
|
920 octave_value_list retval; |
529
|
921 |
1755
|
922 int argc = args.length () + 1; |
|
923 |
1968
|
924 string_vector argv = args.make_argv ("help"); |
1755
|
925 |
|
926 if (error_state) |
|
927 return retval; |
529
|
928 |
|
929 if (argc == 1) |
3014
|
930 simple_help (); |
529
|
931 else |
|
932 { |
1755
|
933 if (argv[1] == "-i") |
3014
|
934 help_from_info (argv, 2, argc); |
529
|
935 else |
3014
|
936 builtin_help (argc, argv); |
529
|
937 } |
|
938 |
|
939 return retval; |
|
940 } |
|
941 |
3355
|
942 static void |
3523
|
943 do_type (std::ostream& os, const std::string& name, bool pr_type_info, |
3355
|
944 bool quiet, bool pr_orig_txt) |
|
945 { |
|
946 symbol_record *sym_rec = lookup_by_name (name, 0); |
|
947 |
|
948 if (sym_rec && sym_rec->is_defined ()) |
3356
|
949 sym_rec->type (os, pr_type_info, quiet, pr_orig_txt); |
3355
|
950 else |
|
951 { |
3523
|
952 std::string ff = fcn_file_in_path (name); |
3355
|
953 |
|
954 if (! ff.empty ()) |
|
955 { |
3538
|
956 std::ifstream fs (ff.c_str (), std::ios::in); |
3355
|
957 |
|
958 if (fs) |
|
959 { |
|
960 if (pr_type_info && ! quiet) |
|
961 os << name << " is the script file: " << ff << "\n\n"; |
|
962 |
|
963 char ch; |
|
964 |
|
965 while (fs.get (ch)) |
|
966 os << ch; |
|
967 } |
|
968 else |
|
969 os << "unable to open `" << ff << "' for reading!\n"; |
|
970 } |
|
971 else |
|
972 error ("type: `%s' undefined", name.c_str ()); |
|
973 } |
|
974 } |
|
975 |
4208
|
976 DEFCMD (type, args, nargout, |
3361
|
977 "-*- texinfo -*-\n\ |
|
978 \n\ |
|
979 @deffn {Command} type options name @dots{}\n\ |
|
980 Display the definition of each @var{name} that refers to a function.\n\ |
|
981 \n\ |
|
982 Normally also displays if each @var{name} is user-defined or builtin;\n\ |
|
983 the @code{-q} option suppresses this behaviour.\n\ |
581
|
984 \n\ |
3361
|
985 Currently, Octave can only display functions that can be compiled\n\ |
|
986 cleanly, because it uses its internal representation of the function to\n\ |
|
987 recreate the program text.\n\ |
|
988 \n\ |
|
989 Comments are not displayed because Octave's parser currently discards\n\ |
|
990 them as it converts the text of a function file to its internal\n\ |
|
991 representation. This problem may be fixed in a future release.\n\ |
|
992 @end deffn") |
581
|
993 { |
3584
|
994 octave_value retval; |
1588
|
995 |
1755
|
996 int argc = args.length () + 1; |
|
997 |
1968
|
998 string_vector argv = args.make_argv ("type"); |
1755
|
999 |
3355
|
1000 if (! error_state) |
581
|
1001 { |
3355
|
1002 if (argc > 1) |
|
1003 { |
|
1004 // XXX FIXME XXX -- we should really use getopt () |
2532
|
1005 |
3355
|
1006 bool quiet = false; |
|
1007 bool pr_orig_txt = true; |
2532
|
1008 |
3355
|
1009 int idx; |
581
|
1010 |
3355
|
1011 for (idx = 1; idx < argc; idx++) |
1281
|
1012 { |
3355
|
1013 if (argv[idx] == "-q" || argv[idx] == "-quiet") |
|
1014 quiet = true; |
|
1015 else if (argv[idx] == "-t" || argv[idx] == "-transformed") |
|
1016 pr_orig_txt = false; |
|
1017 else |
|
1018 break; |
1281
|
1019 } |
|
1020 |
3355
|
1021 if (idx < argc) |
|
1022 { |
4051
|
1023 OSSTREAM output_buf; |
581
|
1024 |
3355
|
1025 for (int i = idx; i < argc; i++) |
581
|
1026 { |
3523
|
1027 std::string id = argv[i]; |
2534
|
1028 |
3355
|
1029 if (nargout == 0) |
|
1030 do_type (octave_stdout, id, true, quiet, pr_orig_txt); |
|
1031 else |
|
1032 do_type (output_buf, id, false, quiet, pr_orig_txt); |
581
|
1033 |
3355
|
1034 if (error_state) |
|
1035 goto abort; |
581
|
1036 } |
|
1037 |
3584
|
1038 if (nargout != 0) |
625
|
1039 { |
4051
|
1040 output_buf << OSSTREAM_ENDS; |
3164
|
1041 |
4051
|
1042 retval = OSSTREAM_STR (output_buf); |
3164
|
1043 |
4051
|
1044 OSSTREAM_FREEZE (output_buf); |
625
|
1045 } |
581
|
1046 } |
|
1047 else |
3355
|
1048 print_usage ("type"); |
|
1049 } |
|
1050 else |
|
1051 print_usage ("type"); |
|
1052 } |
3141
|
1053 |
3355
|
1054 abort: |
581
|
1055 |
|
1056 return retval; |
|
1057 } |
|
1058 |
3536
|
1059 static std::string |
3523
|
1060 do_which (const std::string& name) |
3355
|
1061 { |
3523
|
1062 std::string retval; |
3355
|
1063 |
|
1064 symbol_record *sym_rec = lookup_by_name (name, 0); |
|
1065 |
|
1066 if (sym_rec && sym_rec->is_defined ()) |
|
1067 retval = sym_rec->which (); |
|
1068 else |
|
1069 { |
3523
|
1070 std::string path = fcn_file_in_path (name); |
3355
|
1071 |
|
1072 if (! path.empty ()) |
|
1073 retval = path; |
|
1074 else |
|
1075 retval = "undefined"; |
|
1076 } |
|
1077 |
|
1078 return retval; |
|
1079 } |
|
1080 |
|
1081 static void |
3523
|
1082 do_which (std::ostream& os, const std::string& name) |
3355
|
1083 { |
|
1084 symbol_record *sym_rec = lookup_by_name (name, 0); |
|
1085 |
|
1086 if (sym_rec && sym_rec->is_defined ()) |
|
1087 sym_rec->which (os); |
|
1088 else |
|
1089 { |
3523
|
1090 std::string path = fcn_file_in_path (name); |
3355
|
1091 |
|
1092 if (! path.empty ()) |
|
1093 os << "which: `" << name << "' is the script file\n" |
|
1094 << path << "\n"; |
|
1095 else |
|
1096 os << "which: `" << name << "' is undefined\n"; |
|
1097 } |
|
1098 } |
|
1099 |
4208
|
1100 DEFCMD (which, args, nargout, |
3361
|
1101 "-*- texinfo -*-\n\ |
|
1102 @deffn {Command} which name @dots{}\n\ |
|
1103 Display the type of each @var{name}. If @var{name} is defined from a\n\ |
|
1104 function file, the full name of the file is also displayed.\n\ |
|
1105 @end deffn") |
581
|
1106 { |
2086
|
1107 octave_value_list retval; |
581
|
1108 |
1968
|
1109 string_vector argv = args.make_argv ("which"); |
1755
|
1110 |
3355
|
1111 if (! error_state) |
|
1112 { |
|
1113 int argc = argv.length (); |
581
|
1114 |
|
1115 if (nargout > 0) |
|
1116 retval.resize (argc-1, Matrix ()); |
|
1117 |
3355
|
1118 if (argc > 1) |
581
|
1119 { |
3355
|
1120 for (int i = 1; i < argc; i++) |
581
|
1121 { |
3523
|
1122 std::string id = argv[i]; |
3141
|
1123 |
3355
|
1124 if (nargout == 0) |
|
1125 do_which (octave_stdout, id); |
581
|
1126 else |
3355
|
1127 retval(i-1) = do_which (id); |
581
|
1128 } |
|
1129 } |
3355
|
1130 else |
|
1131 print_usage (argv[0]); |
581
|
1132 } |
|
1133 |
|
1134 return retval; |
|
1135 } |
|
1136 |
5447
|
1137 // XXX FIXME XXX |
|
1138 // This function attempts to find the first sentence of a help string, though |
|
1139 // given that the user can create the help in an arbitrary format, your |
|
1140 // success might vary.. it works much better with help string formated in |
|
1141 // texinfo. Using regex might make this function much simpler. |
|
1142 |
|
1143 std::string |
|
1144 first_help_sentence (const std::string& h, bool short_sentence = true) |
|
1145 { |
|
1146 size_t pos = 0; |
|
1147 |
|
1148 if (looks_like_texinfo (h, pos)) |
|
1149 { |
|
1150 // Get the parsed help string. |
|
1151 pos = 0; |
|
1152 OSSTREAM os; |
|
1153 display_help_text (os, h); |
|
1154 std::string h2 = os.str (); |
|
1155 |
|
1156 while (1) |
|
1157 { |
|
1158 // Skip leading whitespace and get new line |
|
1159 pos = h2.find_first_not_of ("\n\t ", pos); |
|
1160 |
|
1161 if (pos == NPOS) |
|
1162 break; |
|
1163 |
|
1164 size_t new_pos = h2.find_first_of ('\n', pos); |
|
1165 std::string line = h2.substr (pos, new_pos-pos); |
|
1166 |
|
1167 // Skip lines starting in "-" |
|
1168 if (line.find_first_of ('-') == 0) |
|
1169 { |
|
1170 pos = new_pos + 1; |
|
1171 continue; |
|
1172 } |
|
1173 |
|
1174 break; |
|
1175 } |
|
1176 |
|
1177 if (pos == NPOS) |
|
1178 return std::string (); |
|
1179 |
|
1180 // At start of real text. Get first line with the sentence |
|
1181 size_t new_pos = h2.find_first_of ('\n', pos); |
|
1182 std::string line = h2.substr (pos, new_pos-pos); |
|
1183 size_t dot_pos; |
|
1184 |
|
1185 while ((dot_pos = line.find_first_of ('.')) == NPOS) |
|
1186 { |
|
1187 // Trim trailing blanks on line |
|
1188 line.substr (0, line.find_last_not_of ("\n\t ") + 1); |
|
1189 |
|
1190 // Append next line |
|
1191 size_t tmp_pos = h2.find_first_not_of ("\n\t ", new_pos + 1); |
|
1192 if (tmp_pos == NPOS || h2.substr (tmp_pos, 1) == "\n") |
|
1193 break; |
|
1194 |
|
1195 new_pos = h2.find_first_of ('\n', tmp_pos); |
|
1196 std::string next = h2.substr (tmp_pos, new_pos-tmp_pos); |
|
1197 |
|
1198 if (short_sentence) |
|
1199 { |
|
1200 if ((tmp_pos = next.find_first_of ('.')) != NPOS) |
|
1201 { |
|
1202 line = line + " " + next; |
|
1203 dot_pos = line.find_first_of ('.'); |
|
1204 } |
|
1205 break; |
|
1206 } |
|
1207 else |
|
1208 line = line + " " + next; |
|
1209 } |
|
1210 |
|
1211 if (dot_pos == NPOS) |
|
1212 return line; |
|
1213 else |
|
1214 return line.substr (0, dot_pos + 1); |
|
1215 } |
|
1216 else |
|
1217 { |
|
1218 std::string _upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
|
1219 std::string _lower = "abcdefghijklmnopqrstuvwxyz"; |
|
1220 std::string _alpha = _upper + _lower + "_"; |
|
1221 std::string _alphanum = _alpha + "1234567890"; |
|
1222 pos = 0; |
|
1223 |
|
1224 while (1) |
|
1225 { |
|
1226 // Skip leading whitespace and get new line |
|
1227 pos = h.find_first_not_of ("\n\t ", pos); |
|
1228 |
|
1229 if (pos == NPOS) |
|
1230 break; |
|
1231 |
|
1232 size_t new_pos = h.find_first_of ('\n', pos); |
|
1233 std::string line = h.substr (pos, new_pos-pos); |
|
1234 |
|
1235 // Make a lower case copy to simplify some tests |
|
1236 std::string lower = line; |
|
1237 transform (lower.begin (), lower.end (), lower.begin (), tolower); |
|
1238 |
|
1239 // Skip lines starting in "-" or "Usage" |
|
1240 if (lower.find_first_of ('-') == 0 |
|
1241 || lower.substr (0, 5) == "usage") |
|
1242 { |
|
1243 pos = new_pos + 1; |
|
1244 continue; |
|
1245 } |
|
1246 |
|
1247 size_t line_pos = 0; |
|
1248 size_t tmp_pos = 0; |
|
1249 |
|
1250 // chop " blah : " |
|
1251 tmp_pos = line.find_first_not_of ("\t ", line.find_first_not_of |
|
1252 (_alphanum, line_pos)); |
|
1253 if (tmp_pos != NPOS && line.substr (tmp_pos, 1) == ":") |
|
1254 line_pos = line.find_first_not_of ("\t ", tmp_pos + 1); |
|
1255 |
|
1256 if (line_pos == NPOS) |
|
1257 { |
|
1258 pos = new_pos + 1; |
|
1259 continue; |
|
1260 } |
|
1261 |
|
1262 // chop " function " |
|
1263 if (lower.substr (line_pos, 8) == "function") |
|
1264 line_pos = line.find_first_not_of ("\t ", line_pos + 8); |
|
1265 |
|
1266 if (line_pos == NPOS) |
|
1267 { |
|
1268 pos = new_pos + 1; |
|
1269 continue; |
|
1270 } |
|
1271 |
|
1272 // chop " [a,b] = " |
|
1273 if (line.substr (line_pos, 1) == "[") |
|
1274 { |
|
1275 tmp_pos = line.find_first_not_of |
|
1276 ("\t ", line.find_first_of ("]", line_pos) + 1); |
|
1277 |
|
1278 if (tmp_pos != NPOS && line.substr (tmp_pos, 1) == "=") |
|
1279 line_pos = line.find_first_not_of ("\t ",tmp_pos + 1); |
|
1280 } |
|
1281 |
|
1282 if (line_pos == NPOS) |
|
1283 { |
|
1284 pos = new_pos + 1; |
|
1285 continue; |
|
1286 } |
|
1287 |
|
1288 // chop " a = " |
|
1289 if (line.find_first_not_of (_alpha, line_pos) != line_pos) |
|
1290 { |
|
1291 tmp_pos = line.find_first_not_of ("\t ", line.find_first_not_of |
|
1292 (_alphanum, line_pos)); |
|
1293 if (tmp_pos != NPOS && line.substr (tmp_pos, 1) == "=") |
|
1294 line_pos = line.find_first_not_of ("\t ", tmp_pos + 1); |
|
1295 } |
|
1296 |
|
1297 if (line_pos == NPOS) |
|
1298 { |
|
1299 pos = new_pos + 1; |
|
1300 continue; |
|
1301 } |
|
1302 |
|
1303 // chop " f(x) " |
|
1304 if (line.find_first_not_of (_alpha, line_pos) != line_pos) |
|
1305 { |
|
1306 tmp_pos = line.find_first_not_of ("\t ", line.find_first_not_of |
|
1307 (_alphanum, line_pos)); |
|
1308 if (tmp_pos != NPOS && line.substr (tmp_pos, 1) == "(") |
|
1309 line_pos = line.find_first_not_of ("\t ", line.find_first_of |
|
1310 (")", tmp_pos) + 1); |
|
1311 } |
|
1312 |
|
1313 if (line_pos == NPOS) |
|
1314 { |
|
1315 pos = new_pos + 1; |
|
1316 continue; |
|
1317 } |
|
1318 |
|
1319 // chop " ; " |
|
1320 if (line.substr (line_pos, 1) == ":" |
|
1321 || line.substr (line_pos, 1) == ";") |
|
1322 line_pos = line.find_first_not_of ("\t ", line_pos + 1); |
|
1323 |
|
1324 if (line_pos == NPOS) |
|
1325 { |
|
1326 pos = new_pos + 1; |
|
1327 continue; |
|
1328 } |
|
1329 |
|
1330 // chop " BLAH " |
|
1331 if (line.length () > line_pos + 2 |
|
1332 && line.find_first_of (_upper, line_pos) == line_pos |
|
1333 && line.find_first_of (_upper, line_pos+1) == line_pos + 1) |
|
1334 line_pos = line.find_first_not_of ("\t ", line.find_first_not_of |
|
1335 (_upper + "0123456789_", line_pos)); |
|
1336 |
|
1337 if (line_pos == NPOS) |
|
1338 { |
|
1339 pos = new_pos + 1; |
|
1340 continue; |
|
1341 } |
|
1342 |
|
1343 // chop " blah --- " |
|
1344 tmp_pos = line.find_first_not_of ("\t ", line.find_first_not_of |
|
1345 (_alphanum, line_pos)); |
|
1346 if (tmp_pos != NPOS && line.substr (tmp_pos, 1) == "-") |
|
1347 { |
|
1348 tmp_pos = line.find_first_not_of ("-", tmp_pos); |
|
1349 if (line.substr (tmp_pos, 1) == " " |
|
1350 || line.substr (tmp_pos, 1) == "\t") |
|
1351 line_pos = line.find_first_not_of ("\t ", tmp_pos); |
|
1352 } |
|
1353 |
|
1354 if (line_pos == NPOS) |
|
1355 { |
|
1356 pos = new_pos + 1; |
|
1357 continue; |
|
1358 } |
|
1359 |
|
1360 // chop " blah <TAB> " |
|
1361 if (line.find_first_not_of (_alpha, line_pos) != line_pos) |
|
1362 { |
|
1363 tmp_pos = line.find_first_not_of (" ", line.find_first_not_of |
|
1364 (_alphanum, line_pos)); |
|
1365 if (tmp_pos != NPOS && line.substr (tmp_pos, 1) == "\t") |
|
1366 line_pos = line.find_first_not_of ("\t ", line.find_first_of |
|
1367 (")", tmp_pos) + 1); |
|
1368 } |
|
1369 |
|
1370 if (line_pos == NPOS) |
|
1371 { |
|
1372 pos = new_pos + 1; |
|
1373 continue; |
|
1374 } |
|
1375 |
|
1376 // chop " blah " |
|
1377 if (line.find_first_not_of (_alpha, line_pos) != line_pos) |
|
1378 { |
|
1379 tmp_pos = line.find_first_not_of (_alphanum, line_pos); |
|
1380 |
|
1381 if (tmp_pos != NPOS |
|
1382 && (line.substr (tmp_pos, 2) == "\t\t" |
|
1383 || line.substr (tmp_pos, 2) == "\t " |
|
1384 || line.substr (tmp_pos, 2) == " \t" |
|
1385 || line.substr (tmp_pos, 2) == " ")) |
|
1386 line_pos = line.find_first_not_of ("\t ", tmp_pos); |
|
1387 } |
|
1388 |
|
1389 if (line_pos == NPOS) |
|
1390 { |
|
1391 pos = new_pos + 1; |
|
1392 continue; |
|
1393 } |
|
1394 |
|
1395 // skip blah \n or \n blah |
|
1396 // skip blank line |
|
1397 // skip "# !/usr/bin/octave" |
|
1398 if ((line.substr (line_pos , 2) == "or" |
|
1399 && line.find_first_not_of ("\n\t ", line_pos + 2) == NPOS) |
|
1400 || line.find_first_not_of ("\n\t ", line_pos) == NPOS |
|
1401 || line.substr (line_pos, 2) == "!/") |
|
1402 { |
|
1403 pos = new_pos + 1; |
|
1404 continue; |
|
1405 } |
|
1406 |
|
1407 // Got the start of first sentence, break. |
|
1408 pos = pos + line_pos; |
|
1409 break; |
|
1410 } |
|
1411 |
|
1412 if (pos == NPOS) |
|
1413 return std::string (); |
|
1414 |
|
1415 // At start of real text. Get first line with the sentence |
|
1416 size_t new_pos = h.find_first_of ('\n', pos); |
|
1417 std::string line = h.substr (pos, new_pos-pos); |
|
1418 size_t dot_pos; |
|
1419 |
|
1420 while ((dot_pos = line.find_first_of ('.')) == NPOS) |
|
1421 { |
|
1422 // Trim trailing blanks on line |
|
1423 line = line.substr (0, line.find_last_not_of ("\n\t ") + 1); |
|
1424 |
|
1425 // Append next line |
|
1426 size_t tmp_pos = h.find_first_not_of ("\t ", new_pos + 1); |
|
1427 if (tmp_pos == NPOS || h.substr (tmp_pos, 1) == "\n") |
|
1428 break; |
|
1429 |
|
1430 new_pos = h.find_first_of ('\n', tmp_pos); |
|
1431 std::string next = h.substr (tmp_pos, new_pos-tmp_pos); |
|
1432 |
|
1433 if (short_sentence) |
|
1434 { |
|
1435 // Only add the next line if it terminates the sentence, then break |
|
1436 if ((tmp_pos = next.find_first_of ('.')) != NPOS) |
|
1437 { |
|
1438 line = line + " " + next; |
|
1439 dot_pos = line.find_first_of ('.'); |
|
1440 } |
|
1441 break; |
|
1442 } |
|
1443 else |
|
1444 line = line + " " + next; |
|
1445 } |
|
1446 |
|
1447 if (dot_pos == NPOS) |
|
1448 return line; |
|
1449 else |
|
1450 return line.substr (0, dot_pos + 1); |
|
1451 } |
|
1452 } |
|
1453 |
|
1454 static void |
|
1455 print_lookfor (const std::string& name, const std::string& line) |
|
1456 { |
|
1457 const size_t deflen = 20; |
|
1458 |
|
1459 size_t max_width = command_editor::terminal_cols () - deflen; |
|
1460 if (max_width < deflen) |
|
1461 max_width = deflen; |
|
1462 |
|
1463 size_t name_len = name.length (); |
|
1464 |
|
1465 size_t width = max_width; |
|
1466 if (name_len > deflen) |
|
1467 { |
|
1468 width = command_editor::terminal_cols () - name_len; |
|
1469 if (width < deflen) |
|
1470 width = deflen; |
|
1471 } |
|
1472 |
|
1473 size_t pad_len = deflen > name_len ? deflen - name_len + 1 : 1; |
|
1474 octave_stdout << name << std::string (pad_len, ' '); |
|
1475 |
|
1476 size_t pos = 0; |
|
1477 |
|
1478 while (1) |
|
1479 { |
|
1480 size_t new_pos = line.find_first_of ("\n\t ", pos); |
|
1481 size_t end_pos = new_pos; |
|
1482 |
|
1483 if (line.length () - pos < width) |
|
1484 new_pos = end_pos = NPOS; |
|
1485 else |
|
1486 while (new_pos != NPOS && new_pos - pos < width) |
|
1487 { |
|
1488 end_pos = new_pos; |
|
1489 new_pos = line.find_first_of ("\n\t ", new_pos + 1); |
|
1490 } |
|
1491 |
|
1492 octave_stdout << line.substr (pos, end_pos-pos) << std::endl; |
|
1493 |
|
1494 if (end_pos == NPOS) |
|
1495 break; |
|
1496 |
|
1497 pos = end_pos + 1; |
|
1498 width = max_width; |
|
1499 octave_stdout << std::string (deflen + 1, ' '); |
|
1500 } |
|
1501 } |
|
1502 |
|
1503 DEFCMD (lookfor, args, nargout, |
|
1504 "-*- texinfo -*-\n\ |
|
1505 @deffn {Command} lookfor @var{str}\n\ |
|
1506 @deffnx {Command} lookfor -all @var{str}\n\ |
|
1507 @deffnx {Function} {[@var{fun}, @var{helpstring}] = } lookfor (@var{str})\n\ |
|
1508 @deffnx {Function} {[@var{fun}, @var{helpstring}] = } lookfor ('-all', @var{str})\n\ |
|
1509 Search for the string @var{str} in all of the functions found in\n\ |
|
1510 @var{LOADPATH}. By default @code{lookfor} searchs for @var{str} in the\n\ |
|
1511 first sentence of the help string of each function found. The entire\n\ |
|
1512 help string of each function found of @var{LOADPATH} can be search if\n\ |
|
1513 the '-all' argument is supplied. All searches are case insensitive.\n\ |
|
1514 \n\ |
|
1515 Called with no output arguments, @code{lookfor} prints the list of matching\n\ |
|
1516 functions to the terminal. Otherwise the output arguments @var{fun} and\n\ |
|
1517 @var{helpstring} define the matching functions and the first sentence of\n\ |
|
1518 each of their help strings.\n\ |
|
1519 \n\ |
|
1520 Note that the ability of @code{lookfor} to correctly identify the first\n\ |
|
1521 sentence of the help of the functions is dependent on the format of the\n\ |
|
1522 functions help. All of the functions in octave itself will correctly\n\ |
|
1523 find the first sentence, but the same can not be guaranteed for other\n\ |
|
1524 functions. Therefore the use of the '-all' argument might be necessary\n\ |
|
1525 to find related functions that are not part of octave.\n\ |
|
1526 @end deffn\n\ |
|
1527 @seealso{which, help}") |
|
1528 { |
|
1529 octave_value_list retval; |
|
1530 int nargin = args.length (); |
|
1531 bool first_sentence_only = true; |
|
1532 |
|
1533 if (nargin != 1 && nargin != 2) |
|
1534 { |
|
1535 usage ("lookfor"); |
|
1536 return retval; |
|
1537 } |
|
1538 |
|
1539 string_vector ret[2]; |
|
1540 |
|
1541 std::string txt; |
|
1542 |
|
1543 if (args(0).is_string ()) |
|
1544 { |
|
1545 txt = args(0).string_value (); |
|
1546 |
|
1547 if (nargin == 2) |
|
1548 { |
|
1549 if (args(1).is_string ()) |
|
1550 { |
|
1551 std::string tmp = args(1).string_value (); |
|
1552 |
|
1553 if (txt.substr(0,1) == "-") |
|
1554 { |
|
1555 txt = tmp; |
|
1556 tmp = args(0).string_value (); |
|
1557 } |
|
1558 |
|
1559 if (tmp == "-all") |
|
1560 first_sentence_only = false; |
|
1561 else |
|
1562 error ("lookfor: unrecognized option argument"); |
|
1563 } |
|
1564 else |
|
1565 error ("lookfor: arguments must be a string"); |
|
1566 } |
|
1567 } |
|
1568 else |
|
1569 error ("lookfor: argument must be a string"); |
|
1570 |
|
1571 if (!error_state) |
|
1572 { |
|
1573 // All tests in lower case |
|
1574 transform (txt.begin (), txt.end (), txt.begin (), tolower); |
|
1575 |
|
1576 help_list *ptr = keyword_help (); |
|
1577 while (ptr->name) |
|
1578 { |
|
1579 std::string name = ptr->name; |
|
1580 std::string h = ptr->help; |
|
1581 |
|
1582 std::string s; |
|
1583 if (first_sentence_only) |
|
1584 s = first_help_sentence (h); |
|
1585 else |
|
1586 s = h; |
|
1587 |
|
1588 transform (s.begin (), s.end (), s.begin (), tolower); |
|
1589 |
|
1590 if (s.length () > 0 && s.find (txt) != NPOS) |
|
1591 { |
|
1592 if (nargout) |
|
1593 { |
|
1594 ret[0].append (name); |
|
1595 ret[1].append (first_help_sentence (h)); |
|
1596 } |
|
1597 else |
|
1598 print_lookfor (name, first_help_sentence (h)); |
|
1599 } |
|
1600 |
|
1601 OCTAVE_QUIT; |
|
1602 |
|
1603 ptr++; |
|
1604 } |
|
1605 |
|
1606 ptr = operator_help (); |
|
1607 while (ptr->name) |
|
1608 { |
|
1609 std::string name = ptr->name; |
|
1610 std::string h = ptr->help; |
|
1611 |
|
1612 std::string s; |
|
1613 if (first_sentence_only) |
|
1614 s = first_help_sentence (h); |
|
1615 else |
|
1616 s = h; |
|
1617 |
|
1618 transform (s.begin (), s.end (), s.begin (), tolower); |
|
1619 |
|
1620 if (s.length () > 0 && s.find (txt) != NPOS) |
|
1621 { |
|
1622 if (nargout) |
|
1623 { |
|
1624 ret[0].append (name); |
|
1625 ret[1].append (first_help_sentence (h)); |
|
1626 } |
|
1627 else |
|
1628 print_lookfor (name, first_help_sentence (h)); |
|
1629 } |
|
1630 |
|
1631 OCTAVE_QUIT; |
|
1632 |
|
1633 ptr++; |
|
1634 } |
|
1635 |
|
1636 // Check the symbol record table |
|
1637 string_vector names |
|
1638 = fbi_sym_tab->name_list (string_vector (), true); |
|
1639 |
|
1640 for (octave_idx_type i = 0; i < names.length (); i++) |
|
1641 { |
|
1642 std::string name = names (i); |
|
1643 |
|
1644 OCTAVE_QUIT; |
|
1645 |
|
1646 symbol_record *sr = lookup_by_name (name, 0); |
|
1647 if (sr && sr->is_defined ()) |
|
1648 { |
|
1649 std::string h = sr->help (); |
|
1650 std::string s; |
|
1651 if (first_sentence_only) |
|
1652 s = first_help_sentence (h); |
|
1653 else |
|
1654 s = h; |
|
1655 |
|
1656 transform (s.begin (), s.end (), s.begin (), tolower); |
|
1657 |
|
1658 if (s.length () > 0 && s.find (txt) != NPOS) |
|
1659 { |
|
1660 if (nargout) |
|
1661 { |
|
1662 ret[0].append (name); |
|
1663 ret[1].append (first_help_sentence (h)); |
|
1664 } |
|
1665 else |
|
1666 print_lookfor (name, first_help_sentence (h)); |
|
1667 } |
|
1668 } |
|
1669 } |
|
1670 |
|
1671 string_vector dirs = Vload_path_dir_path.all_directories (); |
|
1672 |
|
1673 int len = dirs.length (); |
|
1674 |
|
1675 for (int i = 0; i < len; i++) |
|
1676 { |
|
1677 names = octave_fcn_file_name_cache::list (dirs[i]); |
|
1678 |
|
1679 if (! names.empty ()) |
|
1680 { |
|
1681 for (int j = 0; j < names.length (); j++) |
|
1682 { |
|
1683 std::string name = names (j); |
|
1684 |
|
1685 OCTAVE_QUIT; |
|
1686 |
|
1687 // Strip extension |
|
1688 size_t l = name.length (); |
|
1689 if (l > 4 && name.substr (l-4) == ".oct") |
|
1690 name = name.substr (0, l - 4); |
|
1691 else if (l > 2 && name.substr (l-2) == ".m") |
|
1692 name = name.substr (0, l - 2); |
|
1693 else |
|
1694 continue; |
|
1695 |
|
1696 // Check if already in symbol table |
|
1697 symbol_record *sr = fbi_sym_tab->lookup (name); |
|
1698 |
|
1699 if (!sr) |
|
1700 { |
|
1701 // Check if this version is first in the path |
|
1702 string_vector tmp (2); |
|
1703 tmp(0) = name + ".oct"; |
|
1704 tmp(1) = name + ".m"; |
|
1705 std::string file_name = |
|
1706 Vload_path_dir_path.find_first_of (tmp); |
|
1707 |
|
1708 |
|
1709 if (file_name == dirs[i] + tmp(0) |
|
1710 || file_name == dirs[i] + tmp(1)) |
|
1711 { |
|
1712 std::string h = get_help_from_file (file_name); |
|
1713 |
|
1714 std::string s; |
|
1715 if (first_sentence_only) |
|
1716 s = first_help_sentence (h); |
|
1717 else |
|
1718 s = h; |
|
1719 |
|
1720 transform (s.begin (), s.end (), s.begin (), tolower); |
|
1721 |
|
1722 if (s.length () > 0 && s.find (txt) != NPOS) |
|
1723 { |
|
1724 if (nargout) |
|
1725 { |
|
1726 ret[0].append (name); |
|
1727 ret[1].append (first_help_sentence (h)); |
|
1728 } |
|
1729 else |
|
1730 print_lookfor (name, first_help_sentence (h)); |
|
1731 } |
|
1732 } |
|
1733 } |
|
1734 } |
|
1735 } |
|
1736 } |
|
1737 |
|
1738 |
|
1739 if (nargout != 0) |
|
1740 { |
|
1741 retval (1) = ret[1]; |
|
1742 retval (0) = ret[0]; |
|
1743 } |
|
1744 } |
|
1745 else |
|
1746 { |
|
1747 error ("lookfor: argument must be a string"); |
|
1748 } |
|
1749 |
|
1750 return retval; |
|
1751 } |
|
1752 |
2189
|
1753 static int |
2202
|
1754 info_file (void) |
|
1755 { |
|
1756 int status = 0; |
|
1757 |
3523
|
1758 std::string s = builtin_string_variable ("INFO_FILE"); |
2202
|
1759 |
|
1760 if (s.empty ()) |
|
1761 { |
|
1762 gripe_invalid_value_specified ("INFO_FILE"); |
|
1763 status = -1; |
|
1764 } |
|
1765 else |
|
1766 Vinfo_file = s; |
|
1767 |
|
1768 return status; |
|
1769 } |
|
1770 |
|
1771 static int |
|
1772 info_prog (void) |
|
1773 { |
|
1774 int status = 0; |
|
1775 |
3523
|
1776 std::string s = builtin_string_variable ("INFO_PROGRAM"); |
2202
|
1777 |
|
1778 if (s.empty ()) |
|
1779 { |
|
1780 gripe_invalid_value_specified ("INFO_PROGRAM"); |
|
1781 status = -1; |
|
1782 } |
|
1783 else |
|
1784 Vinfo_prog = s; |
|
1785 |
|
1786 return status; |
|
1787 } |
|
1788 |
3014
|
1789 static int |
3686
|
1790 makeinfo_prog (void) |
|
1791 { |
|
1792 int status = 0; |
|
1793 |
|
1794 std::string s = builtin_string_variable ("MAKEINFO_PROGRAM"); |
|
1795 |
|
1796 if (s.empty ()) |
|
1797 { |
|
1798 gripe_invalid_value_specified ("MAKEINFO_PROGRAM"); |
|
1799 status = -1; |
|
1800 } |
|
1801 else |
|
1802 Vmakeinfo_prog = s; |
|
1803 |
|
1804 return status; |
|
1805 } |
|
1806 |
|
1807 static int |
3014
|
1808 suppress_verbose_help_message (void) |
|
1809 { |
|
1810 Vsuppress_verbose_help_message |
|
1811 = check_preference ("suppress_verbose_help_message"); |
|
1812 |
|
1813 return 0; |
|
1814 } |
|
1815 |
2189
|
1816 void |
|
1817 symbols_of_help (void) |
|
1818 { |
3258
|
1819 DEFVAR (INFO_FILE, Vinfo_file, info_file, |
3332
|
1820 "-*- texinfo -*-\n\ |
|
1821 @defvr {Built-in Variable} INFO_FILE\n\ |
|
1822 The variable @code{INFO_FILE} names the location of the Octave info file.\n\ |
3686
|
1823 The default value is @code{\"@var{octave-home}/info/octave.info\"}, in\n\ |
|
1824 which @var{octave-home} is the directory where all of Octave is installed.\n\ |
3333
|
1825 @end defvr"); |
2202
|
1826 |
3258
|
1827 DEFVAR (INFO_PROGRAM, Vinfo_prog, info_prog, |
3332
|
1828 "-*- texinfo -*-\n\ |
3686
|
1829 @defvr {Built-in Variable} INFO_PROGRAM\n\ |
|
1830 The variable @code{INFO_PROGRAM} names the info program to run. Its\n\ |
|
1831 default initial value is\n\ |
|
1832 @code{\"@var{octave-home}/libexec/octave/@var{version}/exec/@var{arch}/info\"}\n\ |
|
1833 in which @var{octave-home} is the directory where all of Octave is\n\ |
|
1834 installed, @var{version} is the Octave version number, and @var{arch}\n\ |
|
1835 is the system type (for example, @code{i686-pc-linux-gnu}). The\n\ |
|
1836 default initial value may be overridden by the environment variable\n\ |
|
1837 @code{OCTAVE_INFO_PROGRAM}, or the command line argument\n\ |
|
1838 @code{--info-program NAME}, or by setting the value of\n\ |
|
1839 @code{INFO_PROGRAM} in a startup script\n\ |
|
1840 @end defvr"); |
|
1841 |
|
1842 DEFVAR (MAKEINFO_PROGRAM, Vmakeinfo_prog, makeinfo_prog, |
|
1843 "-*- texinfo -*-\n\ |
|
1844 @defvr {Built-in Variable} MAKEINFO_PROGRAM\n\ |
|
1845 The variable @code{MAKEINFO_PROGRAM} names the makeinfo program that\n\ |
|
1846 Octave runs to format help text that contains Texinfo markup commands.\n\ |
|
1847 Its default initial value is @code{\"makeinfo\"}.\n\ |
3333
|
1848 @end defvr"); |
2202
|
1849 |
4233
|
1850 DEFVAR (suppress_verbose_help_message, false, suppress_verbose_help_message, |
3332
|
1851 "-*- texinfo -*-\n\ |
|
1852 @defvr {Built-in Variable} suppress_verbose_help_message\n\ |
|
1853 If the value of @code{suppress_verbose_help_message} is nonzero, Octave\n\ |
|
1854 will not add additional help information to the end of the output from\n\ |
|
1855 the @code{help} command and usage messages for built-in commands.\n\ |
3333
|
1856 @end defvr"); |
2189
|
1857 } |
|
1858 |
1
|
1859 /* |
|
1860 ;;; Local Variables: *** |
|
1861 ;;; mode: C++ *** |
|
1862 ;;; End: *** |
|
1863 */ |