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 |
5483
|
560 #if __MINGW32__ |
|
561 cmd_buf << Vinfo_prog << " --file \"" << Vinfo_file << "\""; |
|
562 #else |
4469
|
563 cmd_buf << "\"" << Vinfo_prog << "\" --file \"" << Vinfo_file << "\""; |
5483
|
564 #endif |
1295
|
565 |
3523
|
566 std::string directory_name = Vinfo_file; |
1755
|
567 size_t pos = directory_name.rfind ('/'); |
|
568 |
|
569 if (pos != NPOS) |
529
|
570 { |
1755
|
571 directory_name.resize (pos + 1); |
4469
|
572 cmd_buf << " --directory \"" << directory_name << "\""; |
529
|
573 } |
|
574 |
1755
|
575 if (nm.length () > 0) |
|
576 cmd_buf << " --index-search " << nm; |
529
|
577 |
4051
|
578 cmd_buf << OSSTREAM_ENDS; |
529
|
579 |
2705
|
580 volatile octave_interrupt_handler old_interrupt_handler |
2554
|
581 = octave_ignore_interrupts (); |
529
|
582 |
4051
|
583 status = system (OSSTREAM_C_STR (cmd_buf)); |
|
584 |
|
585 OSSTREAM_FREEZE (cmd_buf); |
529
|
586 |
2554
|
587 octave_set_interrupt_handler (old_interrupt_handler); |
529
|
588 |
2694
|
589 if (WIFEXITED (status)) |
|
590 status = WEXITSTATUS (status); |
1295
|
591 else |
|
592 status = 127; |
529
|
593 |
|
594 return status; |
|
595 } |
1140
|
596 |
|
597 static void |
1755
|
598 help_from_info (const string_vector& argv, int idx, int argc) |
1140
|
599 { |
1755
|
600 if (idx == argc) |
3523
|
601 try_info (std::string ()); |
1140
|
602 else |
|
603 { |
1755
|
604 for (int i = idx; i < argc; i++) |
1140
|
605 { |
1755
|
606 int status = try_info (argv[i]); |
1140
|
607 |
1295
|
608 if (status) |
1140
|
609 { |
3295
|
610 if (status == 127) |
1295
|
611 { |
3185
|
612 error ("help: unable to find info"); |
|
613 error ("help: you need info 2.18 or later (texinfo 3.12)"); |
1295
|
614 break; |
|
615 } |
3295
|
616 else |
|
617 { |
5399
|
618 message ("help", "`%s' is not indexed in the manual", |
3295
|
619 argv[i].c_str ()); |
|
620 } |
1140
|
621 } |
|
622 } |
|
623 } |
2470
|
624 } |
3014
|
625 |
|
626 static bool |
3523
|
627 looks_like_texinfo (const std::string& msg, size_t& p1) |
3295
|
628 { |
|
629 p1 = msg.find ('\n'); |
|
630 |
3523
|
631 std::string t = msg.substr (0, p1); |
3295
|
632 |
|
633 if (p1 == NPOS) |
|
634 p1 = 0; |
|
635 |
|
636 size_t p2 = t.find ("-*- texinfo -*-"); |
|
637 |
|
638 return (p2 != NPOS); |
|
639 } |
|
640 |
3330
|
641 void |
3523
|
642 display_help_text (std::ostream& os, const std::string& msg) |
3295
|
643 { |
|
644 // Look for "-*- texinfo -*-" in first line of help message. If it |
|
645 // is present, use makeinfo to format the rest of the message before |
|
646 // sending it to the output stream. Otherwise, just print the |
|
647 // message. |
|
648 |
|
649 size_t pos; |
|
650 |
|
651 if (looks_like_texinfo (msg, pos)) |
|
652 { |
3523
|
653 std::string tmp_file_name = file_ops::tempnam ("", ""); |
3295
|
654 |
|
655 int cols = command_editor::terminal_cols (); |
|
656 |
|
657 if (cols > 16) |
|
658 cols--; |
|
659 |
|
660 if (cols > 64) |
|
661 cols -= 7; |
|
662 |
|
663 if (cols > 80) |
|
664 cols = 72; |
|
665 |
4051
|
666 OSSTREAM buf; |
|
667 |
4115
|
668 buf << "sed -e 's/^[#%][#%]* *//' -e 's/^ *@/@/' | " |
4469
|
669 << "\"" << Vmakeinfo_prog << "\"" |
3303
|
670 << " -D \"VERSION " << OCTAVE_VERSION << "\"" |
|
671 << " -D \"OCTAVEHOME " << OCTAVE_PREFIX << "\"" |
3584
|
672 << " -D \"TARGETHOSTTYPE " << OCTAVE_CANONICAL_HOST_TYPE << "\"" |
3301
|
673 << " --fill-column " << cols |
|
674 << " --no-warn" |
|
675 << " --no-validate" |
|
676 << " --no-headers" |
|
677 << " --force" |
4108
|
678 << " --output \"" << tmp_file_name << "\"" |
5451
|
679 #if !defined (__MINGW32__) |
3303
|
680 << " > /dev/null 2>&1" |
5451
|
681 #endif |
4051
|
682 << OSSTREAM_ENDS; |
3295
|
683 |
4051
|
684 oprocstream filter (OSSTREAM_STR (buf)); |
3295
|
685 |
4051
|
686 OSSTREAM_FREEZE (buf); |
3295
|
687 |
3686
|
688 if (filter && filter.is_open ()) |
3295
|
689 { |
3405
|
690 filter << "@macro seealso {args}\n" |
3408
|
691 << "\n" |
|
692 << "@noindent\n" |
3405
|
693 << "See also: \\args\\.\n" |
|
694 << "@end macro\n"; |
|
695 |
3769
|
696 filter << msg.substr (pos+1) << std::endl; |
3295
|
697 |
3686
|
698 int status = filter.close (); |
3295
|
699 |
3523
|
700 std::ifstream tmp_file (tmp_file_name.c_str ()); |
3295
|
701 |
3686
|
702 if (WIFEXITED (status) && WEXITSTATUS (status) == 0) |
|
703 { |
|
704 int c; |
|
705 while ((c = tmp_file.get ()) != EOF) |
|
706 os << (char) c; |
3295
|
707 |
3686
|
708 tmp_file.close (); |
|
709 } |
|
710 else |
|
711 { |
|
712 warning ("help: Texinfo formatting filter exited abnormally"); |
|
713 warning ("help: raw Texinfo source of help text follows..."); |
|
714 |
|
715 os << "\n" << msg; |
|
716 } |
3295
|
717 |
|
718 file_ops::unlink (tmp_file_name); |
|
719 } |
|
720 else |
|
721 os << msg; |
|
722 } |
|
723 else |
|
724 os << msg; |
|
725 } |
|
726 |
|
727 static bool |
3523
|
728 help_from_list (std::ostream& os, const help_list *list, |
5399
|
729 const std::string& nm, int usage, bool& symbol_found) |
542
|
730 { |
5399
|
731 bool retval = false; |
|
732 |
2804
|
733 const char *name; |
3014
|
734 |
542
|
735 while ((name = list->name) != 0) |
|
736 { |
1755
|
737 if (strcmp (name, nm.c_str ()) == 0) |
542
|
738 { |
5399
|
739 symbol_found = true; |
|
740 |
|
741 std::string h = list->help; |
|
742 |
|
743 if (h.length () > 0) |
542
|
744 { |
5399
|
745 if (usage) |
|
746 os << "\nusage: "; |
|
747 else |
|
748 os << "\n*** " << nm << ":\n\n"; |
|
749 |
|
750 display_help_text (os, h); |
|
751 |
|
752 os << "\n"; |
|
753 |
|
754 retval = true; |
542
|
755 } |
5399
|
756 break; |
542
|
757 } |
|
758 list++; |
|
759 } |
3014
|
760 |
5399
|
761 return retval;; |
|
762 } |
|
763 |
|
764 std::string |
|
765 extract_help_from_dispatch (const std::string& nm) |
|
766 { |
|
767 std::string retval; |
|
768 |
|
769 symbol_record *builtin = fbi_sym_tab->lookup ("builtin:" + nm, 0); |
|
770 |
|
771 if (builtin) |
|
772 { |
|
773 // Check that builtin is up to date. |
|
774 |
|
775 // Don't try to fight octave's function name handling |
|
776 // mechanism. Instead, move dispatch record out of the way, |
|
777 // and restore the builtin to its original name. |
|
778 symbol_record *dispatch = fbi_sym_tab->lookup (nm, 0); |
|
779 |
|
780 if (dispatch) |
|
781 { |
|
782 dispatch->unprotect (); |
|
783 |
|
784 fbi_sym_tab->rename (nm, "dispatch:" + nm); |
|
785 fbi_sym_tab->rename ("builtin:" + nm, nm); |
|
786 |
|
787 // Check for updates to builtin function; ignore errors |
|
788 // that appear (they interfere with renaming), and remove |
|
789 // the updated name from the current symbol table. XXX |
|
790 // FIXME XXX check that updating a function updates it in |
|
791 // all contexts --- it may be that it is updated only in the |
|
792 // current symbol table, and not the caller. I believe this |
|
793 // won't be a problem because the caller will go through the |
|
794 // same logic and end up with the newer version. |
|
795 |
|
796 octave_function *f = is_valid_function (nm); |
|
797 |
|
798 if (f) |
|
799 retval = builtin->help (); |
|
800 |
|
801 curr_sym_tab->clear_function (nm); |
|
802 |
|
803 // Move the builtin function out of the way and restore the |
|
804 // dispatch fuction. XXX FIXME XXX what if builtin wants to |
|
805 // protect itself? |
|
806 |
|
807 fbi_sym_tab->rename (nm, "builtin:" + nm); |
|
808 fbi_sym_tab->rename ("dispatch:" + nm, nm); |
|
809 |
|
810 dispatch->protect (); |
|
811 } |
|
812 else |
|
813 error ("failed to find dispatch record for `builtin:%s'", nm.c_str ()); |
|
814 } |
|
815 |
|
816 return retval; |
542
|
817 } |
|
818 |
3355
|
819 static bool |
5399
|
820 help_from_symbol_table (std::ostream& os, const std::string& nm, |
|
821 bool& symbol_found) |
3355
|
822 { |
|
823 bool retval = false; |
|
824 |
|
825 symbol_record *sym_rec = lookup_by_name (nm, 0); |
|
826 |
|
827 if (sym_rec && sym_rec->is_defined ()) |
|
828 { |
5399
|
829 symbol_found = true; |
|
830 |
3523
|
831 std::string h = sym_rec->help (); |
3355
|
832 |
|
833 if (h.length () > 0) |
|
834 { |
|
835 sym_rec->which (os); |
|
836 os << "\n"; |
5399
|
837 h = extract_help_from_dispatch (nm) + h; |
3355
|
838 display_help_text (os, h); |
|
839 os << "\n"; |
|
840 retval = true; |
|
841 } |
|
842 } |
|
843 |
|
844 return retval; |
|
845 } |
|
846 |
|
847 static bool |
5399
|
848 help_from_file (std::ostream& os, const std::string& nm, bool& symbol_found) |
3355
|
849 { |
|
850 bool retval = false; |
|
851 |
5484
|
852 std::string h = get_help_from_file (nm, symbol_found, true); |
3355
|
853 |
5484
|
854 if (h.length () > 0) |
|
855 { |
|
856 display_help_text (os, h); |
|
857 os << "\n"; |
|
858 retval = true; |
3355
|
859 } |
|
860 |
|
861 return retval; |
|
862 } |
|
863 |
1140
|
864 static void |
1755
|
865 builtin_help (int argc, const string_vector& argv) |
1140
|
866 { |
|
867 help_list *op_help_list = operator_help (); |
|
868 help_list *kw_help_list = keyword_help (); |
|
869 |
1755
|
870 for (int i = 1; i < argc; i++) |
1140
|
871 { |
5399
|
872 bool symbol_found = false; |
|
873 |
|
874 if (help_from_list (octave_stdout, op_help_list, argv[i], 0, |
|
875 symbol_found)) |
1140
|
876 continue; |
|
877 |
5399
|
878 if (help_from_list (octave_stdout, kw_help_list, argv[i], 0, |
|
879 symbol_found)) |
1140
|
880 continue; |
|
881 |
5399
|
882 if (help_from_symbol_table (octave_stdout, argv[i], symbol_found)) |
|
883 continue; |
|
884 |
|
885 if (help_from_file (octave_stdout, argv[i], symbol_found)) |
3355
|
886 continue; |
1755
|
887 |
5399
|
888 if (symbol_found) |
|
889 octave_stdout << "\nhelp: `" << argv[i] |
|
890 << "' is not documented\n"; |
|
891 else |
|
892 octave_stdout << "\nhelp: `" << argv[i] |
|
893 << "' not found\n"; |
1140
|
894 } |
|
895 |
2095
|
896 additional_help_message (octave_stdout); |
1140
|
897 } |
|
898 |
4208
|
899 DEFCMD (help, args, , |
3332
|
900 "-*- texinfo -*-\n\ |
|
901 @deffn {Command} help\n\ |
|
902 Octave's @code{help} command can be used to print brief usage-style\n\ |
|
903 messages, or to display information directly from an on-line version of\n\ |
|
904 the printed manual, using the GNU Info browser. If invoked without any\n\ |
|
905 arguments, @code{help} prints a list of all the available operators,\n\ |
|
906 functions, and built-in variables. If the first argument is @code{-i},\n\ |
|
907 the @code{help} command searches the index of the on-line version of\n\ |
|
908 this manual for the given topics.\n\ |
3168
|
909 \n\ |
3332
|
910 For example, the command @kbd{help help} prints a short message\n\ |
|
911 describing the @code{help} command, and @kbd{help -i help} starts the\n\ |
|
912 GNU Info browser at this node in the on-line version of the manual.\n\ |
|
913 \n\ |
|
914 Once the GNU Info browser is running, help for using it is available\n\ |
|
915 using the command @kbd{C-h}.\n\ |
3333
|
916 @end deffn") |
529
|
917 { |
2086
|
918 octave_value_list retval; |
529
|
919 |
1755
|
920 int argc = args.length () + 1; |
|
921 |
1968
|
922 string_vector argv = args.make_argv ("help"); |
1755
|
923 |
|
924 if (error_state) |
|
925 return retval; |
529
|
926 |
|
927 if (argc == 1) |
3014
|
928 simple_help (); |
529
|
929 else |
|
930 { |
1755
|
931 if (argv[1] == "-i") |
3014
|
932 help_from_info (argv, 2, argc); |
529
|
933 else |
3014
|
934 builtin_help (argc, argv); |
529
|
935 } |
|
936 |
|
937 return retval; |
|
938 } |
|
939 |
3355
|
940 static void |
3523
|
941 do_type (std::ostream& os, const std::string& name, bool pr_type_info, |
3355
|
942 bool quiet, bool pr_orig_txt) |
|
943 { |
|
944 symbol_record *sym_rec = lookup_by_name (name, 0); |
|
945 |
|
946 if (sym_rec && sym_rec->is_defined ()) |
3356
|
947 sym_rec->type (os, pr_type_info, quiet, pr_orig_txt); |
3355
|
948 else |
|
949 { |
3523
|
950 std::string ff = fcn_file_in_path (name); |
3355
|
951 |
|
952 if (! ff.empty ()) |
|
953 { |
3538
|
954 std::ifstream fs (ff.c_str (), std::ios::in); |
3355
|
955 |
|
956 if (fs) |
|
957 { |
|
958 if (pr_type_info && ! quiet) |
|
959 os << name << " is the script file: " << ff << "\n\n"; |
|
960 |
|
961 char ch; |
|
962 |
|
963 while (fs.get (ch)) |
|
964 os << ch; |
|
965 } |
|
966 else |
|
967 os << "unable to open `" << ff << "' for reading!\n"; |
|
968 } |
|
969 else |
|
970 error ("type: `%s' undefined", name.c_str ()); |
|
971 } |
|
972 } |
|
973 |
4208
|
974 DEFCMD (type, args, nargout, |
3361
|
975 "-*- texinfo -*-\n\ |
|
976 \n\ |
|
977 @deffn {Command} type options name @dots{}\n\ |
|
978 Display the definition of each @var{name} that refers to a function.\n\ |
|
979 \n\ |
|
980 Normally also displays if each @var{name} is user-defined or builtin;\n\ |
|
981 the @code{-q} option suppresses this behaviour.\n\ |
581
|
982 \n\ |
3361
|
983 Currently, Octave can only display functions that can be compiled\n\ |
|
984 cleanly, because it uses its internal representation of the function to\n\ |
|
985 recreate the program text.\n\ |
|
986 \n\ |
|
987 Comments are not displayed because Octave's parser currently discards\n\ |
|
988 them as it converts the text of a function file to its internal\n\ |
|
989 representation. This problem may be fixed in a future release.\n\ |
|
990 @end deffn") |
581
|
991 { |
3584
|
992 octave_value retval; |
1588
|
993 |
1755
|
994 int argc = args.length () + 1; |
|
995 |
1968
|
996 string_vector argv = args.make_argv ("type"); |
1755
|
997 |
3355
|
998 if (! error_state) |
581
|
999 { |
3355
|
1000 if (argc > 1) |
|
1001 { |
|
1002 // XXX FIXME XXX -- we should really use getopt () |
2532
|
1003 |
3355
|
1004 bool quiet = false; |
|
1005 bool pr_orig_txt = true; |
2532
|
1006 |
3355
|
1007 int idx; |
581
|
1008 |
3355
|
1009 for (idx = 1; idx < argc; idx++) |
1281
|
1010 { |
3355
|
1011 if (argv[idx] == "-q" || argv[idx] == "-quiet") |
|
1012 quiet = true; |
|
1013 else if (argv[idx] == "-t" || argv[idx] == "-transformed") |
|
1014 pr_orig_txt = false; |
|
1015 else |
|
1016 break; |
1281
|
1017 } |
|
1018 |
3355
|
1019 if (idx < argc) |
|
1020 { |
4051
|
1021 OSSTREAM output_buf; |
581
|
1022 |
3355
|
1023 for (int i = idx; i < argc; i++) |
581
|
1024 { |
3523
|
1025 std::string id = argv[i]; |
2534
|
1026 |
3355
|
1027 if (nargout == 0) |
|
1028 do_type (octave_stdout, id, true, quiet, pr_orig_txt); |
|
1029 else |
|
1030 do_type (output_buf, id, false, quiet, pr_orig_txt); |
581
|
1031 |
3355
|
1032 if (error_state) |
|
1033 goto abort; |
581
|
1034 } |
|
1035 |
3584
|
1036 if (nargout != 0) |
625
|
1037 { |
4051
|
1038 output_buf << OSSTREAM_ENDS; |
3164
|
1039 |
4051
|
1040 retval = OSSTREAM_STR (output_buf); |
3164
|
1041 |
4051
|
1042 OSSTREAM_FREEZE (output_buf); |
625
|
1043 } |
581
|
1044 } |
|
1045 else |
3355
|
1046 print_usage ("type"); |
|
1047 } |
|
1048 else |
|
1049 print_usage ("type"); |
|
1050 } |
3141
|
1051 |
3355
|
1052 abort: |
581
|
1053 |
|
1054 return retval; |
|
1055 } |
|
1056 |
3536
|
1057 static std::string |
3523
|
1058 do_which (const std::string& name) |
3355
|
1059 { |
3523
|
1060 std::string retval; |
3355
|
1061 |
|
1062 symbol_record *sym_rec = lookup_by_name (name, 0); |
|
1063 |
|
1064 if (sym_rec && sym_rec->is_defined ()) |
|
1065 retval = sym_rec->which (); |
|
1066 else |
|
1067 { |
3523
|
1068 std::string path = fcn_file_in_path (name); |
3355
|
1069 |
|
1070 if (! path.empty ()) |
|
1071 retval = path; |
|
1072 else |
|
1073 retval = "undefined"; |
|
1074 } |
|
1075 |
|
1076 return retval; |
|
1077 } |
|
1078 |
|
1079 static void |
3523
|
1080 do_which (std::ostream& os, const std::string& name) |
3355
|
1081 { |
|
1082 symbol_record *sym_rec = lookup_by_name (name, 0); |
|
1083 |
|
1084 if (sym_rec && sym_rec->is_defined ()) |
|
1085 sym_rec->which (os); |
|
1086 else |
|
1087 { |
3523
|
1088 std::string path = fcn_file_in_path (name); |
3355
|
1089 |
|
1090 if (! path.empty ()) |
|
1091 os << "which: `" << name << "' is the script file\n" |
|
1092 << path << "\n"; |
|
1093 else |
|
1094 os << "which: `" << name << "' is undefined\n"; |
|
1095 } |
|
1096 } |
|
1097 |
4208
|
1098 DEFCMD (which, args, nargout, |
3361
|
1099 "-*- texinfo -*-\n\ |
|
1100 @deffn {Command} which name @dots{}\n\ |
|
1101 Display the type of each @var{name}. If @var{name} is defined from a\n\ |
|
1102 function file, the full name of the file is also displayed.\n\ |
|
1103 @end deffn") |
581
|
1104 { |
2086
|
1105 octave_value_list retval; |
581
|
1106 |
1968
|
1107 string_vector argv = args.make_argv ("which"); |
1755
|
1108 |
3355
|
1109 if (! error_state) |
|
1110 { |
|
1111 int argc = argv.length (); |
581
|
1112 |
|
1113 if (nargout > 0) |
|
1114 retval.resize (argc-1, Matrix ()); |
|
1115 |
3355
|
1116 if (argc > 1) |
581
|
1117 { |
3355
|
1118 for (int i = 1; i < argc; i++) |
581
|
1119 { |
3523
|
1120 std::string id = argv[i]; |
3141
|
1121 |
3355
|
1122 if (nargout == 0) |
|
1123 do_which (octave_stdout, id); |
581
|
1124 else |
3355
|
1125 retval(i-1) = do_which (id); |
581
|
1126 } |
|
1127 } |
3355
|
1128 else |
|
1129 print_usage (argv[0]); |
581
|
1130 } |
|
1131 |
|
1132 return retval; |
|
1133 } |
|
1134 |
5447
|
1135 // XXX FIXME XXX |
|
1136 // This function attempts to find the first sentence of a help string, though |
|
1137 // given that the user can create the help in an arbitrary format, your |
|
1138 // success might vary.. it works much better with help string formated in |
|
1139 // texinfo. Using regex might make this function much simpler. |
|
1140 |
|
1141 std::string |
|
1142 first_help_sentence (const std::string& h, bool short_sentence = true) |
|
1143 { |
|
1144 size_t pos = 0; |
|
1145 |
|
1146 if (looks_like_texinfo (h, pos)) |
|
1147 { |
|
1148 // Get the parsed help string. |
|
1149 pos = 0; |
|
1150 OSSTREAM os; |
|
1151 display_help_text (os, h); |
|
1152 std::string h2 = os.str (); |
|
1153 |
|
1154 while (1) |
|
1155 { |
|
1156 // Skip leading whitespace and get new line |
|
1157 pos = h2.find_first_not_of ("\n\t ", pos); |
|
1158 |
|
1159 if (pos == NPOS) |
|
1160 break; |
|
1161 |
|
1162 size_t new_pos = h2.find_first_of ('\n', pos); |
|
1163 std::string line = h2.substr (pos, new_pos-pos); |
|
1164 |
|
1165 // Skip lines starting in "-" |
|
1166 if (line.find_first_of ('-') == 0) |
|
1167 { |
|
1168 pos = new_pos + 1; |
|
1169 continue; |
|
1170 } |
|
1171 |
|
1172 break; |
|
1173 } |
|
1174 |
|
1175 if (pos == NPOS) |
|
1176 return std::string (); |
|
1177 |
|
1178 // At start of real text. Get first line with the sentence |
|
1179 size_t new_pos = h2.find_first_of ('\n', pos); |
|
1180 std::string line = h2.substr (pos, new_pos-pos); |
|
1181 size_t dot_pos; |
|
1182 |
|
1183 while ((dot_pos = line.find_first_of ('.')) == NPOS) |
|
1184 { |
|
1185 // Trim trailing blanks on line |
|
1186 line.substr (0, line.find_last_not_of ("\n\t ") + 1); |
|
1187 |
|
1188 // Append next line |
|
1189 size_t tmp_pos = h2.find_first_not_of ("\n\t ", new_pos + 1); |
|
1190 if (tmp_pos == NPOS || h2.substr (tmp_pos, 1) == "\n") |
|
1191 break; |
|
1192 |
|
1193 new_pos = h2.find_first_of ('\n', tmp_pos); |
|
1194 std::string next = h2.substr (tmp_pos, new_pos-tmp_pos); |
|
1195 |
|
1196 if (short_sentence) |
|
1197 { |
|
1198 if ((tmp_pos = next.find_first_of ('.')) != NPOS) |
|
1199 { |
|
1200 line = line + " " + next; |
|
1201 dot_pos = line.find_first_of ('.'); |
|
1202 } |
|
1203 break; |
|
1204 } |
|
1205 else |
|
1206 line = line + " " + next; |
|
1207 } |
|
1208 |
|
1209 if (dot_pos == NPOS) |
|
1210 return line; |
|
1211 else |
|
1212 return line.substr (0, dot_pos + 1); |
|
1213 } |
|
1214 else |
|
1215 { |
|
1216 std::string _upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
|
1217 std::string _lower = "abcdefghijklmnopqrstuvwxyz"; |
|
1218 std::string _alpha = _upper + _lower + "_"; |
|
1219 std::string _alphanum = _alpha + "1234567890"; |
|
1220 pos = 0; |
|
1221 |
|
1222 while (1) |
|
1223 { |
|
1224 // Skip leading whitespace and get new line |
|
1225 pos = h.find_first_not_of ("\n\t ", pos); |
|
1226 |
|
1227 if (pos == NPOS) |
|
1228 break; |
|
1229 |
|
1230 size_t new_pos = h.find_first_of ('\n', pos); |
|
1231 std::string line = h.substr (pos, new_pos-pos); |
|
1232 |
|
1233 // Make a lower case copy to simplify some tests |
|
1234 std::string lower = line; |
|
1235 transform (lower.begin (), lower.end (), lower.begin (), tolower); |
|
1236 |
|
1237 // Skip lines starting in "-" or "Usage" |
|
1238 if (lower.find_first_of ('-') == 0 |
|
1239 || lower.substr (0, 5) == "usage") |
|
1240 { |
|
1241 pos = new_pos + 1; |
|
1242 continue; |
|
1243 } |
|
1244 |
|
1245 size_t line_pos = 0; |
|
1246 size_t tmp_pos = 0; |
|
1247 |
|
1248 // chop " blah : " |
|
1249 tmp_pos = line.find_first_not_of ("\t ", line.find_first_not_of |
|
1250 (_alphanum, line_pos)); |
|
1251 if (tmp_pos != NPOS && line.substr (tmp_pos, 1) == ":") |
|
1252 line_pos = line.find_first_not_of ("\t ", tmp_pos + 1); |
|
1253 |
|
1254 if (line_pos == NPOS) |
|
1255 { |
|
1256 pos = new_pos + 1; |
|
1257 continue; |
|
1258 } |
|
1259 |
|
1260 // chop " function " |
|
1261 if (lower.substr (line_pos, 8) == "function") |
|
1262 line_pos = line.find_first_not_of ("\t ", line_pos + 8); |
|
1263 |
|
1264 if (line_pos == NPOS) |
|
1265 { |
|
1266 pos = new_pos + 1; |
|
1267 continue; |
|
1268 } |
|
1269 |
|
1270 // chop " [a,b] = " |
|
1271 if (line.substr (line_pos, 1) == "[") |
|
1272 { |
|
1273 tmp_pos = line.find_first_not_of |
|
1274 ("\t ", line.find_first_of ("]", line_pos) + 1); |
|
1275 |
|
1276 if (tmp_pos != NPOS && line.substr (tmp_pos, 1) == "=") |
|
1277 line_pos = line.find_first_not_of ("\t ",tmp_pos + 1); |
|
1278 } |
|
1279 |
|
1280 if (line_pos == NPOS) |
|
1281 { |
|
1282 pos = new_pos + 1; |
|
1283 continue; |
|
1284 } |
|
1285 |
|
1286 // chop " a = " |
|
1287 if (line.find_first_not_of (_alpha, line_pos) != line_pos) |
|
1288 { |
|
1289 tmp_pos = line.find_first_not_of ("\t ", line.find_first_not_of |
|
1290 (_alphanum, line_pos)); |
|
1291 if (tmp_pos != NPOS && line.substr (tmp_pos, 1) == "=") |
|
1292 line_pos = line.find_first_not_of ("\t ", tmp_pos + 1); |
|
1293 } |
|
1294 |
|
1295 if (line_pos == NPOS) |
|
1296 { |
|
1297 pos = new_pos + 1; |
|
1298 continue; |
|
1299 } |
|
1300 |
|
1301 // chop " f(x) " |
|
1302 if (line.find_first_not_of (_alpha, line_pos) != line_pos) |
|
1303 { |
|
1304 tmp_pos = line.find_first_not_of ("\t ", line.find_first_not_of |
|
1305 (_alphanum, line_pos)); |
|
1306 if (tmp_pos != NPOS && line.substr (tmp_pos, 1) == "(") |
|
1307 line_pos = line.find_first_not_of ("\t ", line.find_first_of |
|
1308 (")", tmp_pos) + 1); |
|
1309 } |
|
1310 |
|
1311 if (line_pos == NPOS) |
|
1312 { |
|
1313 pos = new_pos + 1; |
|
1314 continue; |
|
1315 } |
|
1316 |
|
1317 // chop " ; " |
|
1318 if (line.substr (line_pos, 1) == ":" |
|
1319 || line.substr (line_pos, 1) == ";") |
|
1320 line_pos = line.find_first_not_of ("\t ", line_pos + 1); |
|
1321 |
|
1322 if (line_pos == NPOS) |
|
1323 { |
|
1324 pos = new_pos + 1; |
|
1325 continue; |
|
1326 } |
|
1327 |
|
1328 // chop " BLAH " |
|
1329 if (line.length () > line_pos + 2 |
|
1330 && line.find_first_of (_upper, line_pos) == line_pos |
|
1331 && line.find_first_of (_upper, line_pos+1) == line_pos + 1) |
|
1332 line_pos = line.find_first_not_of ("\t ", line.find_first_not_of |
|
1333 (_upper + "0123456789_", line_pos)); |
|
1334 |
|
1335 if (line_pos == NPOS) |
|
1336 { |
|
1337 pos = new_pos + 1; |
|
1338 continue; |
|
1339 } |
|
1340 |
|
1341 // chop " blah --- " |
|
1342 tmp_pos = line.find_first_not_of ("\t ", line.find_first_not_of |
|
1343 (_alphanum, line_pos)); |
|
1344 if (tmp_pos != NPOS && line.substr (tmp_pos, 1) == "-") |
|
1345 { |
|
1346 tmp_pos = line.find_first_not_of ("-", tmp_pos); |
|
1347 if (line.substr (tmp_pos, 1) == " " |
|
1348 || line.substr (tmp_pos, 1) == "\t") |
|
1349 line_pos = line.find_first_not_of ("\t ", tmp_pos); |
|
1350 } |
|
1351 |
|
1352 if (line_pos == NPOS) |
|
1353 { |
|
1354 pos = new_pos + 1; |
|
1355 continue; |
|
1356 } |
|
1357 |
|
1358 // chop " blah <TAB> " |
|
1359 if (line.find_first_not_of (_alpha, line_pos) != line_pos) |
|
1360 { |
|
1361 tmp_pos = line.find_first_not_of (" ", line.find_first_not_of |
|
1362 (_alphanum, line_pos)); |
|
1363 if (tmp_pos != NPOS && line.substr (tmp_pos, 1) == "\t") |
|
1364 line_pos = line.find_first_not_of ("\t ", line.find_first_of |
|
1365 (")", tmp_pos) + 1); |
|
1366 } |
|
1367 |
|
1368 if (line_pos == NPOS) |
|
1369 { |
|
1370 pos = new_pos + 1; |
|
1371 continue; |
|
1372 } |
|
1373 |
|
1374 // chop " blah " |
|
1375 if (line.find_first_not_of (_alpha, line_pos) != line_pos) |
|
1376 { |
|
1377 tmp_pos = line.find_first_not_of (_alphanum, line_pos); |
|
1378 |
|
1379 if (tmp_pos != NPOS |
|
1380 && (line.substr (tmp_pos, 2) == "\t\t" |
|
1381 || line.substr (tmp_pos, 2) == "\t " |
|
1382 || line.substr (tmp_pos, 2) == " \t" |
|
1383 || line.substr (tmp_pos, 2) == " ")) |
|
1384 line_pos = line.find_first_not_of ("\t ", tmp_pos); |
|
1385 } |
|
1386 |
|
1387 if (line_pos == NPOS) |
|
1388 { |
|
1389 pos = new_pos + 1; |
|
1390 continue; |
|
1391 } |
|
1392 |
|
1393 // skip blah \n or \n blah |
|
1394 // skip blank line |
|
1395 // skip "# !/usr/bin/octave" |
|
1396 if ((line.substr (line_pos , 2) == "or" |
|
1397 && line.find_first_not_of ("\n\t ", line_pos + 2) == NPOS) |
|
1398 || line.find_first_not_of ("\n\t ", line_pos) == NPOS |
|
1399 || line.substr (line_pos, 2) == "!/") |
|
1400 { |
|
1401 pos = new_pos + 1; |
|
1402 continue; |
|
1403 } |
|
1404 |
|
1405 // Got the start of first sentence, break. |
|
1406 pos = pos + line_pos; |
|
1407 break; |
|
1408 } |
|
1409 |
|
1410 if (pos == NPOS) |
|
1411 return std::string (); |
|
1412 |
|
1413 // At start of real text. Get first line with the sentence |
|
1414 size_t new_pos = h.find_first_of ('\n', pos); |
|
1415 std::string line = h.substr (pos, new_pos-pos); |
|
1416 size_t dot_pos; |
|
1417 |
|
1418 while ((dot_pos = line.find_first_of ('.')) == NPOS) |
|
1419 { |
|
1420 // Trim trailing blanks on line |
|
1421 line = line.substr (0, line.find_last_not_of ("\n\t ") + 1); |
|
1422 |
|
1423 // Append next line |
|
1424 size_t tmp_pos = h.find_first_not_of ("\t ", new_pos + 1); |
|
1425 if (tmp_pos == NPOS || h.substr (tmp_pos, 1) == "\n") |
|
1426 break; |
|
1427 |
|
1428 new_pos = h.find_first_of ('\n', tmp_pos); |
|
1429 std::string next = h.substr (tmp_pos, new_pos-tmp_pos); |
|
1430 |
|
1431 if (short_sentence) |
|
1432 { |
|
1433 // Only add the next line if it terminates the sentence, then break |
|
1434 if ((tmp_pos = next.find_first_of ('.')) != NPOS) |
|
1435 { |
|
1436 line = line + " " + next; |
|
1437 dot_pos = line.find_first_of ('.'); |
|
1438 } |
|
1439 break; |
|
1440 } |
|
1441 else |
|
1442 line = line + " " + next; |
|
1443 } |
|
1444 |
|
1445 if (dot_pos == NPOS) |
|
1446 return line; |
|
1447 else |
|
1448 return line.substr (0, dot_pos + 1); |
|
1449 } |
|
1450 } |
|
1451 |
|
1452 static void |
|
1453 print_lookfor (const std::string& name, const std::string& line) |
|
1454 { |
|
1455 const size_t deflen = 20; |
|
1456 |
|
1457 size_t max_width = command_editor::terminal_cols () - deflen; |
|
1458 if (max_width < deflen) |
|
1459 max_width = deflen; |
|
1460 |
|
1461 size_t name_len = name.length (); |
|
1462 |
|
1463 size_t width = max_width; |
|
1464 if (name_len > deflen) |
|
1465 { |
|
1466 width = command_editor::terminal_cols () - name_len; |
|
1467 if (width < deflen) |
|
1468 width = deflen; |
|
1469 } |
|
1470 |
|
1471 size_t pad_len = deflen > name_len ? deflen - name_len + 1 : 1; |
|
1472 octave_stdout << name << std::string (pad_len, ' '); |
|
1473 |
|
1474 size_t pos = 0; |
|
1475 |
|
1476 while (1) |
|
1477 { |
|
1478 size_t new_pos = line.find_first_of ("\n\t ", pos); |
|
1479 size_t end_pos = new_pos; |
|
1480 |
|
1481 if (line.length () - pos < width) |
|
1482 new_pos = end_pos = NPOS; |
|
1483 else |
|
1484 while (new_pos != NPOS && new_pos - pos < width) |
|
1485 { |
|
1486 end_pos = new_pos; |
|
1487 new_pos = line.find_first_of ("\n\t ", new_pos + 1); |
|
1488 } |
|
1489 |
|
1490 octave_stdout << line.substr (pos, end_pos-pos) << std::endl; |
|
1491 |
|
1492 if (end_pos == NPOS) |
|
1493 break; |
|
1494 |
|
1495 pos = end_pos + 1; |
|
1496 width = max_width; |
|
1497 octave_stdout << std::string (deflen + 1, ' '); |
|
1498 } |
|
1499 } |
|
1500 |
|
1501 DEFCMD (lookfor, args, nargout, |
|
1502 "-*- texinfo -*-\n\ |
|
1503 @deffn {Command} lookfor @var{str}\n\ |
|
1504 @deffnx {Command} lookfor -all @var{str}\n\ |
|
1505 @deffnx {Function} {[@var{fun}, @var{helpstring}] = } lookfor (@var{str})\n\ |
|
1506 @deffnx {Function} {[@var{fun}, @var{helpstring}] = } lookfor ('-all', @var{str})\n\ |
|
1507 Search for the string @var{str} in all of the functions found in\n\ |
|
1508 @var{LOADPATH}. By default @code{lookfor} searchs for @var{str} in the\n\ |
|
1509 first sentence of the help string of each function found. The entire\n\ |
|
1510 help string of each function found of @var{LOADPATH} can be search if\n\ |
|
1511 the '-all' argument is supplied. All searches are case insensitive.\n\ |
|
1512 \n\ |
|
1513 Called with no output arguments, @code{lookfor} prints the list of matching\n\ |
|
1514 functions to the terminal. Otherwise the output arguments @var{fun} and\n\ |
|
1515 @var{helpstring} define the matching functions and the first sentence of\n\ |
|
1516 each of their help strings.\n\ |
|
1517 \n\ |
|
1518 Note that the ability of @code{lookfor} to correctly identify the first\n\ |
|
1519 sentence of the help of the functions is dependent on the format of the\n\ |
|
1520 functions help. All of the functions in octave itself will correctly\n\ |
|
1521 find the first sentence, but the same can not be guaranteed for other\n\ |
|
1522 functions. Therefore the use of the '-all' argument might be necessary\n\ |
|
1523 to find related functions that are not part of octave.\n\ |
|
1524 @end deffn\n\ |
|
1525 @seealso{which, help}") |
|
1526 { |
|
1527 octave_value_list retval; |
|
1528 int nargin = args.length (); |
|
1529 bool first_sentence_only = true; |
|
1530 |
|
1531 if (nargin != 1 && nargin != 2) |
|
1532 { |
|
1533 usage ("lookfor"); |
|
1534 return retval; |
|
1535 } |
|
1536 |
|
1537 string_vector ret[2]; |
|
1538 |
|
1539 std::string txt; |
|
1540 |
|
1541 if (args(0).is_string ()) |
|
1542 { |
|
1543 txt = args(0).string_value (); |
|
1544 |
|
1545 if (nargin == 2) |
|
1546 { |
|
1547 if (args(1).is_string ()) |
|
1548 { |
|
1549 std::string tmp = args(1).string_value (); |
|
1550 |
|
1551 if (txt.substr(0,1) == "-") |
|
1552 { |
|
1553 txt = tmp; |
|
1554 tmp = args(0).string_value (); |
|
1555 } |
|
1556 |
|
1557 if (tmp == "-all") |
|
1558 first_sentence_only = false; |
|
1559 else |
|
1560 error ("lookfor: unrecognized option argument"); |
|
1561 } |
|
1562 else |
|
1563 error ("lookfor: arguments must be a string"); |
|
1564 } |
|
1565 } |
|
1566 else |
|
1567 error ("lookfor: argument must be a string"); |
|
1568 |
|
1569 if (!error_state) |
|
1570 { |
|
1571 // All tests in lower case |
|
1572 transform (txt.begin (), txt.end (), txt.begin (), tolower); |
|
1573 |
|
1574 help_list *ptr = keyword_help (); |
|
1575 while (ptr->name) |
|
1576 { |
|
1577 std::string name = ptr->name; |
|
1578 std::string h = ptr->help; |
|
1579 |
|
1580 std::string s; |
|
1581 if (first_sentence_only) |
|
1582 s = first_help_sentence (h); |
|
1583 else |
|
1584 s = h; |
|
1585 |
|
1586 transform (s.begin (), s.end (), s.begin (), tolower); |
|
1587 |
|
1588 if (s.length () > 0 && s.find (txt) != NPOS) |
|
1589 { |
|
1590 if (nargout) |
|
1591 { |
|
1592 ret[0].append (name); |
|
1593 ret[1].append (first_help_sentence (h)); |
|
1594 } |
|
1595 else |
|
1596 print_lookfor (name, first_help_sentence (h)); |
|
1597 } |
|
1598 |
|
1599 OCTAVE_QUIT; |
|
1600 |
|
1601 ptr++; |
|
1602 } |
|
1603 |
|
1604 ptr = operator_help (); |
|
1605 while (ptr->name) |
|
1606 { |
|
1607 std::string name = ptr->name; |
|
1608 std::string h = ptr->help; |
|
1609 |
|
1610 std::string s; |
|
1611 if (first_sentence_only) |
|
1612 s = first_help_sentence (h); |
|
1613 else |
|
1614 s = h; |
|
1615 |
|
1616 transform (s.begin (), s.end (), s.begin (), tolower); |
|
1617 |
|
1618 if (s.length () > 0 && s.find (txt) != NPOS) |
|
1619 { |
|
1620 if (nargout) |
|
1621 { |
|
1622 ret[0].append (name); |
|
1623 ret[1].append (first_help_sentence (h)); |
|
1624 } |
|
1625 else |
|
1626 print_lookfor (name, first_help_sentence (h)); |
|
1627 } |
|
1628 |
|
1629 OCTAVE_QUIT; |
|
1630 |
|
1631 ptr++; |
|
1632 } |
|
1633 |
|
1634 // Check the symbol record table |
|
1635 string_vector names |
|
1636 = fbi_sym_tab->name_list (string_vector (), true); |
|
1637 |
|
1638 for (octave_idx_type i = 0; i < names.length (); i++) |
|
1639 { |
|
1640 std::string name = names (i); |
|
1641 |
|
1642 OCTAVE_QUIT; |
|
1643 |
|
1644 symbol_record *sr = lookup_by_name (name, 0); |
|
1645 if (sr && sr->is_defined ()) |
|
1646 { |
|
1647 std::string h = sr->help (); |
|
1648 std::string s; |
|
1649 if (first_sentence_only) |
|
1650 s = first_help_sentence (h); |
|
1651 else |
|
1652 s = h; |
|
1653 |
|
1654 transform (s.begin (), s.end (), s.begin (), tolower); |
|
1655 |
|
1656 if (s.length () > 0 && s.find (txt) != NPOS) |
|
1657 { |
|
1658 if (nargout) |
|
1659 { |
|
1660 ret[0].append (name); |
|
1661 ret[1].append (first_help_sentence (h)); |
|
1662 } |
|
1663 else |
|
1664 print_lookfor (name, first_help_sentence (h)); |
|
1665 } |
|
1666 } |
|
1667 } |
|
1668 |
|
1669 string_vector dirs = Vload_path_dir_path.all_directories (); |
|
1670 |
|
1671 int len = dirs.length (); |
|
1672 |
|
1673 for (int i = 0; i < len; i++) |
|
1674 { |
|
1675 names = octave_fcn_file_name_cache::list (dirs[i]); |
|
1676 |
|
1677 if (! names.empty ()) |
|
1678 { |
|
1679 for (int j = 0; j < names.length (); j++) |
|
1680 { |
|
1681 std::string name = names (j); |
|
1682 |
|
1683 OCTAVE_QUIT; |
|
1684 |
|
1685 // Strip extension |
|
1686 size_t l = name.length (); |
|
1687 if (l > 4 && name.substr (l-4) == ".oct") |
|
1688 name = name.substr (0, l - 4); |
|
1689 else if (l > 2 && name.substr (l-2) == ".m") |
|
1690 name = name.substr (0, l - 2); |
|
1691 else |
|
1692 continue; |
|
1693 |
|
1694 // Check if already in symbol table |
|
1695 symbol_record *sr = fbi_sym_tab->lookup (name); |
|
1696 |
|
1697 if (!sr) |
|
1698 { |
|
1699 // Check if this version is first in the path |
|
1700 string_vector tmp (2); |
|
1701 tmp(0) = name + ".oct"; |
|
1702 tmp(1) = name + ".m"; |
|
1703 std::string file_name = |
|
1704 Vload_path_dir_path.find_first_of (tmp); |
|
1705 |
|
1706 |
|
1707 if (file_name == dirs[i] + tmp(0) |
|
1708 || file_name == dirs[i] + tmp(1)) |
|
1709 { |
5484
|
1710 bool symbol_found; |
|
1711 |
|
1712 std::string h |
|
1713 = get_help_from_file (file_name, symbol_found); |
5447
|
1714 |
|
1715 std::string s; |
|
1716 if (first_sentence_only) |
|
1717 s = first_help_sentence (h); |
|
1718 else |
|
1719 s = h; |
|
1720 |
|
1721 transform (s.begin (), s.end (), s.begin (), tolower); |
|
1722 |
|
1723 if (s.length () > 0 && s.find (txt) != NPOS) |
|
1724 { |
|
1725 if (nargout) |
|
1726 { |
|
1727 ret[0].append (name); |
|
1728 ret[1].append (first_help_sentence (h)); |
|
1729 } |
|
1730 else |
|
1731 print_lookfor (name, first_help_sentence (h)); |
|
1732 } |
|
1733 } |
|
1734 } |
|
1735 } |
|
1736 } |
|
1737 } |
|
1738 |
|
1739 |
|
1740 if (nargout != 0) |
|
1741 { |
|
1742 retval (1) = ret[1]; |
|
1743 retval (0) = ret[0]; |
|
1744 } |
|
1745 } |
|
1746 else |
|
1747 { |
|
1748 error ("lookfor: argument must be a string"); |
|
1749 } |
|
1750 |
|
1751 return retval; |
|
1752 } |
|
1753 |
2189
|
1754 static int |
2202
|
1755 info_file (void) |
|
1756 { |
|
1757 int status = 0; |
|
1758 |
3523
|
1759 std::string s = builtin_string_variable ("INFO_FILE"); |
2202
|
1760 |
|
1761 if (s.empty ()) |
|
1762 { |
|
1763 gripe_invalid_value_specified ("INFO_FILE"); |
|
1764 status = -1; |
|
1765 } |
|
1766 else |
|
1767 Vinfo_file = s; |
|
1768 |
|
1769 return status; |
|
1770 } |
|
1771 |
|
1772 static int |
|
1773 info_prog (void) |
|
1774 { |
|
1775 int status = 0; |
|
1776 |
3523
|
1777 std::string s = builtin_string_variable ("INFO_PROGRAM"); |
2202
|
1778 |
|
1779 if (s.empty ()) |
|
1780 { |
|
1781 gripe_invalid_value_specified ("INFO_PROGRAM"); |
|
1782 status = -1; |
|
1783 } |
|
1784 else |
|
1785 Vinfo_prog = s; |
|
1786 |
|
1787 return status; |
|
1788 } |
|
1789 |
3014
|
1790 static int |
3686
|
1791 makeinfo_prog (void) |
|
1792 { |
|
1793 int status = 0; |
|
1794 |
|
1795 std::string s = builtin_string_variable ("MAKEINFO_PROGRAM"); |
|
1796 |
|
1797 if (s.empty ()) |
|
1798 { |
|
1799 gripe_invalid_value_specified ("MAKEINFO_PROGRAM"); |
|
1800 status = -1; |
|
1801 } |
|
1802 else |
|
1803 Vmakeinfo_prog = s; |
|
1804 |
|
1805 return status; |
|
1806 } |
|
1807 |
|
1808 static int |
3014
|
1809 suppress_verbose_help_message (void) |
|
1810 { |
|
1811 Vsuppress_verbose_help_message |
|
1812 = check_preference ("suppress_verbose_help_message"); |
|
1813 |
|
1814 return 0; |
|
1815 } |
|
1816 |
2189
|
1817 void |
|
1818 symbols_of_help (void) |
|
1819 { |
3258
|
1820 DEFVAR (INFO_FILE, Vinfo_file, info_file, |
3332
|
1821 "-*- texinfo -*-\n\ |
|
1822 @defvr {Built-in Variable} INFO_FILE\n\ |
|
1823 The variable @code{INFO_FILE} names the location of the Octave info file.\n\ |
3686
|
1824 The default value is @code{\"@var{octave-home}/info/octave.info\"}, in\n\ |
|
1825 which @var{octave-home} is the directory where all of Octave is installed.\n\ |
3333
|
1826 @end defvr"); |
2202
|
1827 |
3258
|
1828 DEFVAR (INFO_PROGRAM, Vinfo_prog, info_prog, |
3332
|
1829 "-*- texinfo -*-\n\ |
3686
|
1830 @defvr {Built-in Variable} INFO_PROGRAM\n\ |
|
1831 The variable @code{INFO_PROGRAM} names the info program to run. Its\n\ |
|
1832 default initial value is\n\ |
|
1833 @code{\"@var{octave-home}/libexec/octave/@var{version}/exec/@var{arch}/info\"}\n\ |
|
1834 in which @var{octave-home} is the directory where all of Octave is\n\ |
|
1835 installed, @var{version} is the Octave version number, and @var{arch}\n\ |
|
1836 is the system type (for example, @code{i686-pc-linux-gnu}). The\n\ |
|
1837 default initial value may be overridden by the environment variable\n\ |
|
1838 @code{OCTAVE_INFO_PROGRAM}, or the command line argument\n\ |
|
1839 @code{--info-program NAME}, or by setting the value of\n\ |
|
1840 @code{INFO_PROGRAM} in a startup script\n\ |
|
1841 @end defvr"); |
|
1842 |
|
1843 DEFVAR (MAKEINFO_PROGRAM, Vmakeinfo_prog, makeinfo_prog, |
|
1844 "-*- texinfo -*-\n\ |
|
1845 @defvr {Built-in Variable} MAKEINFO_PROGRAM\n\ |
|
1846 The variable @code{MAKEINFO_PROGRAM} names the makeinfo program that\n\ |
|
1847 Octave runs to format help text that contains Texinfo markup commands.\n\ |
|
1848 Its default initial value is @code{\"makeinfo\"}.\n\ |
3333
|
1849 @end defvr"); |
2202
|
1850 |
4233
|
1851 DEFVAR (suppress_verbose_help_message, false, suppress_verbose_help_message, |
3332
|
1852 "-*- texinfo -*-\n\ |
|
1853 @defvr {Built-in Variable} suppress_verbose_help_message\n\ |
|
1854 If the value of @code{suppress_verbose_help_message} is nonzero, Octave\n\ |
|
1855 will not add additional help information to the end of the output from\n\ |
|
1856 the @code{help} command and usage messages for built-in commands.\n\ |
3333
|
1857 @end defvr"); |
2189
|
1858 } |
|
1859 |
1
|
1860 /* |
|
1861 ;;; Local Variables: *** |
|
1862 ;;; mode: C++ *** |
|
1863 ;;; End: *** |
|
1864 */ |