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 { "if", |
5393
|
290 "-*- texinfo -*-\n\ |
|
291 @deffn Keyword if (@var{cond}) @dots{} endif\n\ |
|
292 @deffnx Keyword if (@var{cond}) @dots{} else @dots{} endif\n\ |
|
293 @deffnx Keyword if (@var{cond}) @dots{} elseif (@var{cond}) @dots{} endif\n\ |
|
294 @deffnx Keyword if (@var{cond}) @dots{} elseif (@var{cond}) @dots{} else @dots{} endif\n\ |
|
295 Begin an if block.\n\ |
|
296 @end deffn\n\ |
|
297 @seealso{switch}", }, |
1
|
298 |
5040
|
299 { "otherwise", |
|
300 "The default statement in a switch block.", }, |
|
301 |
4686
|
302 { "persistent", |
|
303 "Declare variables as persistent.", }, |
|
304 |
5040
|
305 { "replot", |
|
306 "Replot a graphic.", }, |
|
307 |
1
|
308 { "return", |
928
|
309 "Return from a function.", }, |
|
310 |
5040
|
311 { "static", |
|
312 "Declare variables as persistent.", }, |
|
313 |
|
314 { "switch", |
|
315 "Begin a switch statement.",}, |
|
316 |
1489
|
317 { "try", |
|
318 "Begin a try-catch block.", }, |
|
319 |
5040
|
320 { "until", |
|
321 "End a do-until loop.",}, |
|
322 |
928
|
323 { "unwind_protect", |
|
324 "Begin an unwind_protect block.", }, |
|
325 |
|
326 { "unwind_protect_cleanup", |
|
327 "Begin the cleanup section of an unwind_protect block.", }, |
1
|
328 |
5040
|
329 { "varargin", |
5339
|
330 "Pass an arbitrary number of arguments into a function. See also\n\ |
|
331 varargout, nargin, and nargout.",}, |
5040
|
332 |
|
333 { "varargout", |
5339
|
334 "Pass an arbitrary number of arguments out of a function. See also\n\ |
|
335 varargin, nargin, and nargout.",}, |
5040
|
336 |
1
|
337 { "while", |
928
|
338 "Begin a while loop.", }, |
1
|
339 |
529
|
340 { 0, 0, }, |
1
|
341 }; |
|
342 |
581
|
343 // Return a copy of the operator or keyword names. |
|
344 |
3016
|
345 static string_vector |
3355
|
346 names (help_list *lst) |
1
|
347 { |
1755
|
348 string_vector retval; |
|
349 |
3355
|
350 int count = 0; |
1
|
351 help_list *ptr = lst; |
529
|
352 while (ptr->name) |
1
|
353 { |
|
354 count++; |
|
355 ptr++; |
|
356 } |
|
357 |
1755
|
358 if (count > 0) |
|
359 { |
|
360 retval.resize (count); |
1
|
361 |
1755
|
362 ptr = lst; |
|
363 for (int i = 0; i < count; i++) |
|
364 { |
|
365 retval[i] = ptr->name; |
|
366 ptr++; |
|
367 } |
1
|
368 } |
|
369 |
1755
|
370 return retval; |
1
|
371 } |
|
372 |
3014
|
373 static help_list * |
1
|
374 operator_help (void) |
|
375 { |
|
376 return operators; |
|
377 } |
|
378 |
3016
|
379 static help_list * |
1
|
380 keyword_help (void) |
|
381 { |
|
382 return keywords; |
|
383 } |
|
384 |
3016
|
385 // It's not likely that this does the right thing now. XXX FIXME XXX |
|
386 |
|
387 string_vector |
|
388 make_name_list (void) |
|
389 { |
3355
|
390 string_vector key = names (keyword_help ()); |
|
391 int key_len = key.length (); |
3016
|
392 |
4009
|
393 string_vector fbi = fbi_sym_tab->name_list (); |
|
394 int fbi_len = fbi.length (); |
|
395 |
3355
|
396 string_vector glb = global_sym_tab->name_list (); |
|
397 int glb_len = glb.length (); |
3016
|
398 |
3355
|
399 string_vector top = top_level_sym_tab->name_list (); |
|
400 int top_len = top.length (); |
3016
|
401 |
3355
|
402 string_vector lcl; |
3016
|
403 if (top_level_sym_tab != curr_sym_tab) |
3355
|
404 lcl = curr_sym_tab->name_list (); |
|
405 int lcl_len = lcl.length (); |
3016
|
406 |
3355
|
407 string_vector ffl = octave_fcn_file_name_cache::list_no_suffix (); |
3016
|
408 int ffl_len = ffl.length (); |
|
409 |
4009
|
410 int total_len = key_len + fbi_len + glb_len + top_len + lcl_len + ffl_len; |
3016
|
411 |
|
412 string_vector list (total_len); |
|
413 |
|
414 // Put all the symbols in one big list. |
|
415 |
|
416 int j = 0; |
|
417 int i = 0; |
|
418 for (i = 0; i < key_len; i++) |
|
419 list[j++] = key[i]; |
|
420 |
4009
|
421 for (i = 0; i < fbi_len; i++) |
|
422 list[j++] = fbi[i]; |
|
423 |
3016
|
424 for (i = 0; i < glb_len; i++) |
|
425 list[j++] = glb[i]; |
|
426 |
|
427 for (i = 0; i < top_len; i++) |
|
428 list[j++] = top[i]; |
|
429 |
|
430 for (i = 0; i < lcl_len; i++) |
|
431 list[j++] = lcl[i]; |
|
432 |
|
433 for (i = 0; i < ffl_len; i++) |
|
434 list[j++] = ffl[i]; |
|
435 |
|
436 return list; |
|
437 } |
|
438 |
3014
|
439 void |
3523
|
440 additional_help_message (std::ostream& os) |
2470
|
441 { |
|
442 if (! Vsuppress_verbose_help_message) |
|
443 os << "\n\ |
3259
|
444 Additional help for built-in functions, operators, and variables\n\ |
3160
|
445 is available in the on-line version of the manual. Use the command\n\ |
3295
|
446 `help -i <topic>' to search the manual index.\n\ |
|
447 \n\ |
3160
|
448 Help and information about Octave is also available on the WWW\n\ |
5041
|
449 at http://www.octave.org and via the help@octave.org\n\ |
3917
|
450 mailing list.\n"; |
3160
|
451 } |
|
452 |
2976
|
453 // XXX FIXME XXX -- this needs a major overhaul to cope with new |
|
454 // symbol table stuff. |
|
455 |
529
|
456 static void |
3523
|
457 display_names_from_help_list (std::ostream& os, help_list *list, |
542
|
458 const char *desc) |
529
|
459 { |
3355
|
460 string_vector symbols = names (list); |
3259
|
461 |
1808
|
462 if (! symbols.empty ()) |
|
463 { |
2095
|
464 os << "\n*** " << desc << ":\n\n"; |
3259
|
465 |
|
466 symbols.qsort (); |
|
467 |
2095
|
468 symbols.list_in_columns (os); |
1808
|
469 } |
529
|
470 } |
|
471 |
|
472 static void |
3523
|
473 display_symtab_names (std::ostream& os, const string_vector& names, |
|
474 const std::string& desc) |
542
|
475 { |
1808
|
476 if (! names.empty ()) |
|
477 { |
2095
|
478 os << "\n*** " << desc << ":\n\n"; |
|
479 names.list_in_columns (os); |
1808
|
480 } |
542
|
481 } |
|
482 |
|
483 #ifdef LIST_SYMBOLS |
|
484 #undef LIST_SYMBOLS |
|
485 #endif |
|
486 #define LIST_SYMBOLS(type, msg) \ |
|
487 do \ |
|
488 { \ |
3259
|
489 string_vector names \ |
4009
|
490 = fbi_sym_tab->name_list (string_vector (), true, type); \ |
3355
|
491 display_symtab_names (octave_stdout, names, msg); \ |
542
|
492 } \ |
|
493 while (0) |
529
|
494 |
3014
|
495 static void |
|
496 simple_help (void) |
|
497 { |
3160
|
498 octave_stdout << "Help is available for the topics listed below.\n"; |
|
499 |
|
500 additional_help_message (octave_stdout); |
|
501 |
3014
|
502 display_names_from_help_list (octave_stdout, operator_help (), |
|
503 "operators"); |
|
504 |
|
505 display_names_from_help_list (octave_stdout, keyword_help (), |
|
506 "reserved words"); |
|
507 |
1358
|
508 // XXX FIXME XXX -- is this distinction needed? |
|
509 |
3259
|
510 LIST_SYMBOLS (symbol_record::BUILTIN_CONSTANT, "built-in constants"); |
|
511 |
|
512 LIST_SYMBOLS (symbol_record::BUILTIN_VARIABLE, "built-in variables"); |
|
513 |
4208
|
514 LIST_SYMBOLS (symbol_record::COMMAND, "commands"); |
529
|
515 |
3010
|
516 LIST_SYMBOLS (symbol_record::MAPPER_FUNCTION, "mapper functions"); |
542
|
517 |
3010
|
518 LIST_SYMBOLS (symbol_record::BUILTIN_FUNCTION, "general functions"); |
542
|
519 |
1358
|
520 // Also need to list variables and currently compiled functions from |
|
521 // the symbol table, if there are any. |
529
|
522 |
1358
|
523 // Also need to search octave_path for script files. |
529
|
524 |
3195
|
525 string_vector dirs = Vload_path_dir_path.all_directories (); |
679
|
526 |
1787
|
527 int len = dirs.length (); |
679
|
528 |
1787
|
529 for (int i = 0; i < len; i++) |
|
530 { |
2233
|
531 string_vector names = octave_fcn_file_name_cache::list (dirs[i]); |
679
|
532 |
1787
|
533 if (! names.empty ()) |
|
534 { |
3523
|
535 std::string dir |
2926
|
536 = octave_env::make_absolute (dirs[i], octave_env::getcwd ()); |
|
537 |
|
538 octave_stdout << "\n*** function files in " << dir << ":\n\n"; |
679
|
539 |
3259
|
540 names.qsort (); |
|
541 |
2095
|
542 names.list_in_columns (octave_stdout); |
529
|
543 } |
|
544 } |
|
545 } |
|
546 |
|
547 static int |
3523
|
548 try_info (const std::string& nm) |
529
|
549 { |
|
550 int status = 0; |
|
551 |
4051
|
552 OSSTREAM cmd_buf; |
1295
|
553 |
5483
|
554 #if __MINGW32__ |
|
555 cmd_buf << Vinfo_prog << " --file \"" << Vinfo_file << "\""; |
|
556 #else |
4469
|
557 cmd_buf << "\"" << Vinfo_prog << "\" --file \"" << Vinfo_file << "\""; |
5483
|
558 #endif |
1295
|
559 |
3523
|
560 std::string directory_name = Vinfo_file; |
1755
|
561 size_t pos = directory_name.rfind ('/'); |
|
562 |
|
563 if (pos != NPOS) |
529
|
564 { |
1755
|
565 directory_name.resize (pos + 1); |
4469
|
566 cmd_buf << " --directory \"" << directory_name << "\""; |
529
|
567 } |
|
568 |
1755
|
569 if (nm.length () > 0) |
|
570 cmd_buf << " --index-search " << nm; |
529
|
571 |
4051
|
572 cmd_buf << OSSTREAM_ENDS; |
529
|
573 |
2705
|
574 volatile octave_interrupt_handler old_interrupt_handler |
2554
|
575 = octave_ignore_interrupts (); |
529
|
576 |
4051
|
577 status = system (OSSTREAM_C_STR (cmd_buf)); |
|
578 |
|
579 OSSTREAM_FREEZE (cmd_buf); |
529
|
580 |
2554
|
581 octave_set_interrupt_handler (old_interrupt_handler); |
529
|
582 |
2694
|
583 if (WIFEXITED (status)) |
|
584 status = WEXITSTATUS (status); |
1295
|
585 else |
|
586 status = 127; |
529
|
587 |
|
588 return status; |
|
589 } |
1140
|
590 |
|
591 static void |
1755
|
592 help_from_info (const string_vector& argv, int idx, int argc) |
1140
|
593 { |
1755
|
594 if (idx == argc) |
3523
|
595 try_info (std::string ()); |
1140
|
596 else |
|
597 { |
1755
|
598 for (int i = idx; i < argc; i++) |
1140
|
599 { |
1755
|
600 int status = try_info (argv[i]); |
1140
|
601 |
1295
|
602 if (status) |
1140
|
603 { |
3295
|
604 if (status == 127) |
1295
|
605 { |
3185
|
606 error ("help: unable to find info"); |
|
607 error ("help: you need info 2.18 or later (texinfo 3.12)"); |
1295
|
608 break; |
|
609 } |
3295
|
610 else |
|
611 { |
5399
|
612 message ("help", "`%s' is not indexed in the manual", |
3295
|
613 argv[i].c_str ()); |
|
614 } |
1140
|
615 } |
|
616 } |
|
617 } |
2470
|
618 } |
3014
|
619 |
|
620 static bool |
3523
|
621 looks_like_texinfo (const std::string& msg, size_t& p1) |
3295
|
622 { |
|
623 p1 = msg.find ('\n'); |
|
624 |
3523
|
625 std::string t = msg.substr (0, p1); |
3295
|
626 |
|
627 if (p1 == NPOS) |
|
628 p1 = 0; |
|
629 |
|
630 size_t p2 = t.find ("-*- texinfo -*-"); |
|
631 |
|
632 return (p2 != NPOS); |
|
633 } |
|
634 |
3330
|
635 void |
3523
|
636 display_help_text (std::ostream& os, const std::string& msg) |
3295
|
637 { |
|
638 // Look for "-*- texinfo -*-" in first line of help message. If it |
|
639 // is present, use makeinfo to format the rest of the message before |
|
640 // sending it to the output stream. Otherwise, just print the |
|
641 // message. |
|
642 |
|
643 size_t pos; |
|
644 |
|
645 if (looks_like_texinfo (msg, pos)) |
|
646 { |
3523
|
647 std::string tmp_file_name = file_ops::tempnam ("", ""); |
3295
|
648 |
|
649 int cols = command_editor::terminal_cols (); |
|
650 |
|
651 if (cols > 16) |
|
652 cols--; |
|
653 |
|
654 if (cols > 64) |
|
655 cols -= 7; |
|
656 |
|
657 if (cols > 80) |
|
658 cols = 72; |
|
659 |
4051
|
660 OSSTREAM buf; |
|
661 |
4115
|
662 buf << "sed -e 's/^[#%][#%]* *//' -e 's/^ *@/@/' | " |
4469
|
663 << "\"" << Vmakeinfo_prog << "\"" |
3303
|
664 << " -D \"VERSION " << OCTAVE_VERSION << "\"" |
|
665 << " -D \"OCTAVEHOME " << OCTAVE_PREFIX << "\"" |
3584
|
666 << " -D \"TARGETHOSTTYPE " << OCTAVE_CANONICAL_HOST_TYPE << "\"" |
3301
|
667 << " --fill-column " << cols |
|
668 << " --no-warn" |
|
669 << " --no-validate" |
|
670 << " --no-headers" |
|
671 << " --force" |
4108
|
672 << " --output \"" << tmp_file_name << "\"" |
5451
|
673 #if !defined (__MINGW32__) |
3303
|
674 << " > /dev/null 2>&1" |
5451
|
675 #endif |
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 |
5484
|
846 std::string h = get_help_from_file (nm, symbol_found, true); |
3355
|
847 |
5484
|
848 if (h.length () > 0) |
|
849 { |
|
850 display_help_text (os, h); |
|
851 os << "\n"; |
|
852 retval = true; |
3355
|
853 } |
|
854 |
|
855 return retval; |
|
856 } |
|
857 |
1140
|
858 static void |
1755
|
859 builtin_help (int argc, const string_vector& argv) |
1140
|
860 { |
|
861 help_list *op_help_list = operator_help (); |
|
862 help_list *kw_help_list = keyword_help (); |
|
863 |
1755
|
864 for (int i = 1; i < argc; i++) |
1140
|
865 { |
5399
|
866 bool symbol_found = false; |
|
867 |
|
868 if (help_from_list (octave_stdout, op_help_list, argv[i], 0, |
|
869 symbol_found)) |
1140
|
870 continue; |
|
871 |
5399
|
872 if (help_from_list (octave_stdout, kw_help_list, argv[i], 0, |
|
873 symbol_found)) |
1140
|
874 continue; |
|
875 |
5399
|
876 if (help_from_symbol_table (octave_stdout, argv[i], symbol_found)) |
|
877 continue; |
|
878 |
|
879 if (help_from_file (octave_stdout, argv[i], symbol_found)) |
3355
|
880 continue; |
1755
|
881 |
5399
|
882 if (symbol_found) |
|
883 octave_stdout << "\nhelp: `" << argv[i] |
|
884 << "' is not documented\n"; |
|
885 else |
|
886 octave_stdout << "\nhelp: `" << argv[i] |
|
887 << "' not found\n"; |
1140
|
888 } |
|
889 |
2095
|
890 additional_help_message (octave_stdout); |
1140
|
891 } |
|
892 |
4208
|
893 DEFCMD (help, args, , |
3332
|
894 "-*- texinfo -*-\n\ |
|
895 @deffn {Command} help\n\ |
|
896 Octave's @code{help} command can be used to print brief usage-style\n\ |
|
897 messages, or to display information directly from an on-line version of\n\ |
|
898 the printed manual, using the GNU Info browser. If invoked without any\n\ |
|
899 arguments, @code{help} prints a list of all the available operators,\n\ |
|
900 functions, and built-in variables. If the first argument is @code{-i},\n\ |
|
901 the @code{help} command searches the index of the on-line version of\n\ |
|
902 this manual for the given topics.\n\ |
3168
|
903 \n\ |
3332
|
904 For example, the command @kbd{help help} prints a short message\n\ |
|
905 describing the @code{help} command, and @kbd{help -i help} starts the\n\ |
|
906 GNU Info browser at this node in the on-line version of the manual.\n\ |
|
907 \n\ |
|
908 Once the GNU Info browser is running, help for using it is available\n\ |
|
909 using the command @kbd{C-h}.\n\ |
3333
|
910 @end deffn") |
529
|
911 { |
2086
|
912 octave_value_list retval; |
529
|
913 |
1755
|
914 int argc = args.length () + 1; |
|
915 |
1968
|
916 string_vector argv = args.make_argv ("help"); |
1755
|
917 |
|
918 if (error_state) |
|
919 return retval; |
529
|
920 |
|
921 if (argc == 1) |
3014
|
922 simple_help (); |
529
|
923 else |
|
924 { |
1755
|
925 if (argv[1] == "-i") |
3014
|
926 help_from_info (argv, 2, argc); |
529
|
927 else |
3014
|
928 builtin_help (argc, argv); |
529
|
929 } |
|
930 |
|
931 return retval; |
|
932 } |
|
933 |
3355
|
934 static void |
3523
|
935 do_type (std::ostream& os, const std::string& name, bool pr_type_info, |
3355
|
936 bool quiet, bool pr_orig_txt) |
|
937 { |
|
938 symbol_record *sym_rec = lookup_by_name (name, 0); |
|
939 |
|
940 if (sym_rec && sym_rec->is_defined ()) |
3356
|
941 sym_rec->type (os, pr_type_info, quiet, pr_orig_txt); |
3355
|
942 else |
|
943 { |
3523
|
944 std::string ff = fcn_file_in_path (name); |
3355
|
945 |
|
946 if (! ff.empty ()) |
|
947 { |
3538
|
948 std::ifstream fs (ff.c_str (), std::ios::in); |
3355
|
949 |
|
950 if (fs) |
|
951 { |
|
952 if (pr_type_info && ! quiet) |
|
953 os << name << " is the script file: " << ff << "\n\n"; |
|
954 |
|
955 char ch; |
|
956 |
|
957 while (fs.get (ch)) |
|
958 os << ch; |
|
959 } |
|
960 else |
|
961 os << "unable to open `" << ff << "' for reading!\n"; |
|
962 } |
|
963 else |
|
964 error ("type: `%s' undefined", name.c_str ()); |
|
965 } |
|
966 } |
|
967 |
4208
|
968 DEFCMD (type, args, nargout, |
3361
|
969 "-*- texinfo -*-\n\ |
|
970 \n\ |
|
971 @deffn {Command} type options name @dots{}\n\ |
|
972 Display the definition of each @var{name} that refers to a function.\n\ |
|
973 \n\ |
|
974 Normally also displays if each @var{name} is user-defined or builtin;\n\ |
|
975 the @code{-q} option suppresses this behaviour.\n\ |
581
|
976 \n\ |
3361
|
977 Currently, Octave can only display functions that can be compiled\n\ |
|
978 cleanly, because it uses its internal representation of the function to\n\ |
|
979 recreate the program text.\n\ |
|
980 \n\ |
|
981 Comments are not displayed because Octave's parser currently discards\n\ |
|
982 them as it converts the text of a function file to its internal\n\ |
|
983 representation. This problem may be fixed in a future release.\n\ |
|
984 @end deffn") |
581
|
985 { |
3584
|
986 octave_value retval; |
1588
|
987 |
1755
|
988 int argc = args.length () + 1; |
|
989 |
1968
|
990 string_vector argv = args.make_argv ("type"); |
1755
|
991 |
3355
|
992 if (! error_state) |
581
|
993 { |
3355
|
994 if (argc > 1) |
|
995 { |
|
996 // XXX FIXME XXX -- we should really use getopt () |
2532
|
997 |
3355
|
998 bool quiet = false; |
|
999 bool pr_orig_txt = true; |
2532
|
1000 |
3355
|
1001 int idx; |
581
|
1002 |
3355
|
1003 for (idx = 1; idx < argc; idx++) |
1281
|
1004 { |
3355
|
1005 if (argv[idx] == "-q" || argv[idx] == "-quiet") |
|
1006 quiet = true; |
|
1007 else if (argv[idx] == "-t" || argv[idx] == "-transformed") |
|
1008 pr_orig_txt = false; |
|
1009 else |
|
1010 break; |
1281
|
1011 } |
|
1012 |
3355
|
1013 if (idx < argc) |
|
1014 { |
4051
|
1015 OSSTREAM output_buf; |
581
|
1016 |
3355
|
1017 for (int i = idx; i < argc; i++) |
581
|
1018 { |
3523
|
1019 std::string id = argv[i]; |
2534
|
1020 |
3355
|
1021 if (nargout == 0) |
|
1022 do_type (octave_stdout, id, true, quiet, pr_orig_txt); |
|
1023 else |
|
1024 do_type (output_buf, id, false, quiet, pr_orig_txt); |
581
|
1025 |
3355
|
1026 if (error_state) |
|
1027 goto abort; |
581
|
1028 } |
|
1029 |
3584
|
1030 if (nargout != 0) |
625
|
1031 { |
4051
|
1032 output_buf << OSSTREAM_ENDS; |
3164
|
1033 |
4051
|
1034 retval = OSSTREAM_STR (output_buf); |
3164
|
1035 |
4051
|
1036 OSSTREAM_FREEZE (output_buf); |
625
|
1037 } |
581
|
1038 } |
|
1039 else |
3355
|
1040 print_usage ("type"); |
|
1041 } |
|
1042 else |
|
1043 print_usage ("type"); |
|
1044 } |
3141
|
1045 |
3355
|
1046 abort: |
581
|
1047 |
|
1048 return retval; |
|
1049 } |
|
1050 |
3536
|
1051 static std::string |
3523
|
1052 do_which (const std::string& name) |
3355
|
1053 { |
3523
|
1054 std::string retval; |
3355
|
1055 |
|
1056 symbol_record *sym_rec = lookup_by_name (name, 0); |
|
1057 |
|
1058 if (sym_rec && sym_rec->is_defined ()) |
|
1059 retval = sym_rec->which (); |
|
1060 else |
|
1061 { |
3523
|
1062 std::string path = fcn_file_in_path (name); |
3355
|
1063 |
|
1064 if (! path.empty ()) |
|
1065 retval = path; |
|
1066 else |
|
1067 retval = "undefined"; |
|
1068 } |
|
1069 |
|
1070 return retval; |
|
1071 } |
|
1072 |
|
1073 static void |
3523
|
1074 do_which (std::ostream& os, const std::string& name) |
3355
|
1075 { |
|
1076 symbol_record *sym_rec = lookup_by_name (name, 0); |
|
1077 |
|
1078 if (sym_rec && sym_rec->is_defined ()) |
|
1079 sym_rec->which (os); |
|
1080 else |
|
1081 { |
3523
|
1082 std::string path = fcn_file_in_path (name); |
3355
|
1083 |
|
1084 if (! path.empty ()) |
|
1085 os << "which: `" << name << "' is the script file\n" |
|
1086 << path << "\n"; |
|
1087 else |
|
1088 os << "which: `" << name << "' is undefined\n"; |
|
1089 } |
|
1090 } |
|
1091 |
4208
|
1092 DEFCMD (which, args, nargout, |
3361
|
1093 "-*- texinfo -*-\n\ |
|
1094 @deffn {Command} which name @dots{}\n\ |
|
1095 Display the type of each @var{name}. If @var{name} is defined from a\n\ |
|
1096 function file, the full name of the file is also displayed.\n\ |
|
1097 @end deffn") |
581
|
1098 { |
2086
|
1099 octave_value_list retval; |
581
|
1100 |
1968
|
1101 string_vector argv = args.make_argv ("which"); |
1755
|
1102 |
3355
|
1103 if (! error_state) |
|
1104 { |
|
1105 int argc = argv.length (); |
581
|
1106 |
|
1107 if (nargout > 0) |
|
1108 retval.resize (argc-1, Matrix ()); |
|
1109 |
3355
|
1110 if (argc > 1) |
581
|
1111 { |
3355
|
1112 for (int i = 1; i < argc; i++) |
581
|
1113 { |
3523
|
1114 std::string id = argv[i]; |
3141
|
1115 |
3355
|
1116 if (nargout == 0) |
|
1117 do_which (octave_stdout, id); |
581
|
1118 else |
3355
|
1119 retval(i-1) = do_which (id); |
581
|
1120 } |
|
1121 } |
3355
|
1122 else |
|
1123 print_usage (argv[0]); |
581
|
1124 } |
|
1125 |
|
1126 return retval; |
|
1127 } |
|
1128 |
5447
|
1129 // XXX FIXME XXX |
|
1130 // This function attempts to find the first sentence of a help string, though |
|
1131 // given that the user can create the help in an arbitrary format, your |
|
1132 // success might vary.. it works much better with help string formated in |
|
1133 // texinfo. Using regex might make this function much simpler. |
|
1134 |
|
1135 std::string |
|
1136 first_help_sentence (const std::string& h, bool short_sentence = true) |
|
1137 { |
|
1138 size_t pos = 0; |
|
1139 |
|
1140 if (looks_like_texinfo (h, pos)) |
|
1141 { |
|
1142 // Get the parsed help string. |
|
1143 pos = 0; |
|
1144 OSSTREAM os; |
|
1145 display_help_text (os, h); |
|
1146 std::string h2 = os.str (); |
|
1147 |
|
1148 while (1) |
|
1149 { |
|
1150 // Skip leading whitespace and get new line |
|
1151 pos = h2.find_first_not_of ("\n\t ", pos); |
|
1152 |
|
1153 if (pos == NPOS) |
|
1154 break; |
|
1155 |
|
1156 size_t new_pos = h2.find_first_of ('\n', pos); |
|
1157 std::string line = h2.substr (pos, new_pos-pos); |
|
1158 |
|
1159 // Skip lines starting in "-" |
|
1160 if (line.find_first_of ('-') == 0) |
|
1161 { |
|
1162 pos = new_pos + 1; |
|
1163 continue; |
|
1164 } |
|
1165 |
|
1166 break; |
|
1167 } |
|
1168 |
|
1169 if (pos == NPOS) |
|
1170 return std::string (); |
|
1171 |
|
1172 // At start of real text. Get first line with the sentence |
|
1173 size_t new_pos = h2.find_first_of ('\n', pos); |
|
1174 std::string line = h2.substr (pos, new_pos-pos); |
|
1175 size_t dot_pos; |
|
1176 |
|
1177 while ((dot_pos = line.find_first_of ('.')) == NPOS) |
|
1178 { |
|
1179 // Trim trailing blanks on line |
|
1180 line.substr (0, line.find_last_not_of ("\n\t ") + 1); |
|
1181 |
|
1182 // Append next line |
|
1183 size_t tmp_pos = h2.find_first_not_of ("\n\t ", new_pos + 1); |
|
1184 if (tmp_pos == NPOS || h2.substr (tmp_pos, 1) == "\n") |
|
1185 break; |
|
1186 |
|
1187 new_pos = h2.find_first_of ('\n', tmp_pos); |
|
1188 std::string next = h2.substr (tmp_pos, new_pos-tmp_pos); |
|
1189 |
|
1190 if (short_sentence) |
|
1191 { |
|
1192 if ((tmp_pos = next.find_first_of ('.')) != NPOS) |
|
1193 { |
|
1194 line = line + " " + next; |
|
1195 dot_pos = line.find_first_of ('.'); |
|
1196 } |
|
1197 break; |
|
1198 } |
|
1199 else |
|
1200 line = line + " " + next; |
|
1201 } |
|
1202 |
|
1203 if (dot_pos == NPOS) |
|
1204 return line; |
|
1205 else |
|
1206 return line.substr (0, dot_pos + 1); |
|
1207 } |
|
1208 else |
|
1209 { |
|
1210 std::string _upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
|
1211 std::string _lower = "abcdefghijklmnopqrstuvwxyz"; |
|
1212 std::string _alpha = _upper + _lower + "_"; |
|
1213 std::string _alphanum = _alpha + "1234567890"; |
|
1214 pos = 0; |
|
1215 |
|
1216 while (1) |
|
1217 { |
|
1218 // Skip leading whitespace and get new line |
|
1219 pos = h.find_first_not_of ("\n\t ", pos); |
|
1220 |
|
1221 if (pos == NPOS) |
|
1222 break; |
|
1223 |
|
1224 size_t new_pos = h.find_first_of ('\n', pos); |
|
1225 std::string line = h.substr (pos, new_pos-pos); |
|
1226 |
|
1227 // Make a lower case copy to simplify some tests |
|
1228 std::string lower = line; |
|
1229 transform (lower.begin (), lower.end (), lower.begin (), tolower); |
|
1230 |
|
1231 // Skip lines starting in "-" or "Usage" |
|
1232 if (lower.find_first_of ('-') == 0 |
|
1233 || lower.substr (0, 5) == "usage") |
|
1234 { |
|
1235 pos = new_pos + 1; |
|
1236 continue; |
|
1237 } |
|
1238 |
|
1239 size_t line_pos = 0; |
|
1240 size_t tmp_pos = 0; |
|
1241 |
|
1242 // chop " blah : " |
|
1243 tmp_pos = line.find_first_not_of ("\t ", line.find_first_not_of |
|
1244 (_alphanum, line_pos)); |
|
1245 if (tmp_pos != NPOS && line.substr (tmp_pos, 1) == ":") |
|
1246 line_pos = line.find_first_not_of ("\t ", tmp_pos + 1); |
|
1247 |
|
1248 if (line_pos == NPOS) |
|
1249 { |
|
1250 pos = new_pos + 1; |
|
1251 continue; |
|
1252 } |
|
1253 |
|
1254 // chop " function " |
|
1255 if (lower.substr (line_pos, 8) == "function") |
|
1256 line_pos = line.find_first_not_of ("\t ", line_pos + 8); |
|
1257 |
|
1258 if (line_pos == NPOS) |
|
1259 { |
|
1260 pos = new_pos + 1; |
|
1261 continue; |
|
1262 } |
|
1263 |
|
1264 // chop " [a,b] = " |
|
1265 if (line.substr (line_pos, 1) == "[") |
|
1266 { |
|
1267 tmp_pos = line.find_first_not_of |
|
1268 ("\t ", line.find_first_of ("]", line_pos) + 1); |
|
1269 |
|
1270 if (tmp_pos != NPOS && line.substr (tmp_pos, 1) == "=") |
|
1271 line_pos = line.find_first_not_of ("\t ",tmp_pos + 1); |
|
1272 } |
|
1273 |
|
1274 if (line_pos == NPOS) |
|
1275 { |
|
1276 pos = new_pos + 1; |
|
1277 continue; |
|
1278 } |
|
1279 |
|
1280 // chop " a = " |
|
1281 if (line.find_first_not_of (_alpha, line_pos) != line_pos) |
|
1282 { |
|
1283 tmp_pos = line.find_first_not_of ("\t ", line.find_first_not_of |
|
1284 (_alphanum, line_pos)); |
|
1285 if (tmp_pos != NPOS && line.substr (tmp_pos, 1) == "=") |
|
1286 line_pos = line.find_first_not_of ("\t ", tmp_pos + 1); |
|
1287 } |
|
1288 |
|
1289 if (line_pos == NPOS) |
|
1290 { |
|
1291 pos = new_pos + 1; |
|
1292 continue; |
|
1293 } |
|
1294 |
|
1295 // chop " f(x) " |
|
1296 if (line.find_first_not_of (_alpha, line_pos) != line_pos) |
|
1297 { |
|
1298 tmp_pos = line.find_first_not_of ("\t ", line.find_first_not_of |
|
1299 (_alphanum, line_pos)); |
|
1300 if (tmp_pos != NPOS && line.substr (tmp_pos, 1) == "(") |
|
1301 line_pos = line.find_first_not_of ("\t ", line.find_first_of |
|
1302 (")", tmp_pos) + 1); |
|
1303 } |
|
1304 |
|
1305 if (line_pos == NPOS) |
|
1306 { |
|
1307 pos = new_pos + 1; |
|
1308 continue; |
|
1309 } |
|
1310 |
|
1311 // chop " ; " |
|
1312 if (line.substr (line_pos, 1) == ":" |
|
1313 || line.substr (line_pos, 1) == ";") |
|
1314 line_pos = line.find_first_not_of ("\t ", line_pos + 1); |
|
1315 |
|
1316 if (line_pos == NPOS) |
|
1317 { |
|
1318 pos = new_pos + 1; |
|
1319 continue; |
|
1320 } |
|
1321 |
|
1322 // chop " BLAH " |
|
1323 if (line.length () > line_pos + 2 |
|
1324 && line.find_first_of (_upper, line_pos) == line_pos |
|
1325 && line.find_first_of (_upper, line_pos+1) == line_pos + 1) |
|
1326 line_pos = line.find_first_not_of ("\t ", line.find_first_not_of |
|
1327 (_upper + "0123456789_", line_pos)); |
|
1328 |
|
1329 if (line_pos == NPOS) |
|
1330 { |
|
1331 pos = new_pos + 1; |
|
1332 continue; |
|
1333 } |
|
1334 |
|
1335 // chop " blah --- " |
|
1336 tmp_pos = line.find_first_not_of ("\t ", line.find_first_not_of |
|
1337 (_alphanum, line_pos)); |
|
1338 if (tmp_pos != NPOS && line.substr (tmp_pos, 1) == "-") |
|
1339 { |
|
1340 tmp_pos = line.find_first_not_of ("-", tmp_pos); |
|
1341 if (line.substr (tmp_pos, 1) == " " |
|
1342 || line.substr (tmp_pos, 1) == "\t") |
|
1343 line_pos = line.find_first_not_of ("\t ", tmp_pos); |
|
1344 } |
|
1345 |
|
1346 if (line_pos == NPOS) |
|
1347 { |
|
1348 pos = new_pos + 1; |
|
1349 continue; |
|
1350 } |
|
1351 |
|
1352 // chop " blah <TAB> " |
|
1353 if (line.find_first_not_of (_alpha, line_pos) != line_pos) |
|
1354 { |
|
1355 tmp_pos = line.find_first_not_of (" ", line.find_first_not_of |
|
1356 (_alphanum, line_pos)); |
|
1357 if (tmp_pos != NPOS && line.substr (tmp_pos, 1) == "\t") |
|
1358 line_pos = line.find_first_not_of ("\t ", line.find_first_of |
|
1359 (")", tmp_pos) + 1); |
|
1360 } |
|
1361 |
|
1362 if (line_pos == NPOS) |
|
1363 { |
|
1364 pos = new_pos + 1; |
|
1365 continue; |
|
1366 } |
|
1367 |
|
1368 // chop " blah " |
|
1369 if (line.find_first_not_of (_alpha, line_pos) != line_pos) |
|
1370 { |
|
1371 tmp_pos = line.find_first_not_of (_alphanum, line_pos); |
|
1372 |
|
1373 if (tmp_pos != NPOS |
|
1374 && (line.substr (tmp_pos, 2) == "\t\t" |
|
1375 || line.substr (tmp_pos, 2) == "\t " |
|
1376 || line.substr (tmp_pos, 2) == " \t" |
|
1377 || line.substr (tmp_pos, 2) == " ")) |
|
1378 line_pos = line.find_first_not_of ("\t ", tmp_pos); |
|
1379 } |
|
1380 |
|
1381 if (line_pos == NPOS) |
|
1382 { |
|
1383 pos = new_pos + 1; |
|
1384 continue; |
|
1385 } |
|
1386 |
|
1387 // skip blah \n or \n blah |
|
1388 // skip blank line |
|
1389 // skip "# !/usr/bin/octave" |
|
1390 if ((line.substr (line_pos , 2) == "or" |
|
1391 && line.find_first_not_of ("\n\t ", line_pos + 2) == NPOS) |
|
1392 || line.find_first_not_of ("\n\t ", line_pos) == NPOS |
|
1393 || line.substr (line_pos, 2) == "!/") |
|
1394 { |
|
1395 pos = new_pos + 1; |
|
1396 continue; |
|
1397 } |
|
1398 |
|
1399 // Got the start of first sentence, break. |
|
1400 pos = pos + line_pos; |
|
1401 break; |
|
1402 } |
|
1403 |
|
1404 if (pos == NPOS) |
|
1405 return std::string (); |
|
1406 |
|
1407 // At start of real text. Get first line with the sentence |
|
1408 size_t new_pos = h.find_first_of ('\n', pos); |
|
1409 std::string line = h.substr (pos, new_pos-pos); |
|
1410 size_t dot_pos; |
|
1411 |
|
1412 while ((dot_pos = line.find_first_of ('.')) == NPOS) |
|
1413 { |
|
1414 // Trim trailing blanks on line |
|
1415 line = line.substr (0, line.find_last_not_of ("\n\t ") + 1); |
|
1416 |
|
1417 // Append next line |
|
1418 size_t tmp_pos = h.find_first_not_of ("\t ", new_pos + 1); |
|
1419 if (tmp_pos == NPOS || h.substr (tmp_pos, 1) == "\n") |
|
1420 break; |
|
1421 |
|
1422 new_pos = h.find_first_of ('\n', tmp_pos); |
|
1423 std::string next = h.substr (tmp_pos, new_pos-tmp_pos); |
|
1424 |
|
1425 if (short_sentence) |
|
1426 { |
|
1427 // Only add the next line if it terminates the sentence, then break |
|
1428 if ((tmp_pos = next.find_first_of ('.')) != NPOS) |
|
1429 { |
|
1430 line = line + " " + next; |
|
1431 dot_pos = line.find_first_of ('.'); |
|
1432 } |
|
1433 break; |
|
1434 } |
|
1435 else |
|
1436 line = line + " " + next; |
|
1437 } |
|
1438 |
|
1439 if (dot_pos == NPOS) |
|
1440 return line; |
|
1441 else |
|
1442 return line.substr (0, dot_pos + 1); |
|
1443 } |
|
1444 } |
|
1445 |
|
1446 static void |
|
1447 print_lookfor (const std::string& name, const std::string& line) |
|
1448 { |
|
1449 const size_t deflen = 20; |
|
1450 |
|
1451 size_t max_width = command_editor::terminal_cols () - deflen; |
|
1452 if (max_width < deflen) |
|
1453 max_width = deflen; |
|
1454 |
|
1455 size_t name_len = name.length (); |
|
1456 |
|
1457 size_t width = max_width; |
|
1458 if (name_len > deflen) |
|
1459 { |
|
1460 width = command_editor::terminal_cols () - name_len; |
|
1461 if (width < deflen) |
|
1462 width = deflen; |
|
1463 } |
|
1464 |
|
1465 size_t pad_len = deflen > name_len ? deflen - name_len + 1 : 1; |
|
1466 octave_stdout << name << std::string (pad_len, ' '); |
|
1467 |
|
1468 size_t pos = 0; |
|
1469 |
|
1470 while (1) |
|
1471 { |
|
1472 size_t new_pos = line.find_first_of ("\n\t ", pos); |
|
1473 size_t end_pos = new_pos; |
|
1474 |
|
1475 if (line.length () - pos < width) |
|
1476 new_pos = end_pos = NPOS; |
|
1477 else |
|
1478 while (new_pos != NPOS && new_pos - pos < width) |
|
1479 { |
|
1480 end_pos = new_pos; |
|
1481 new_pos = line.find_first_of ("\n\t ", new_pos + 1); |
|
1482 } |
|
1483 |
|
1484 octave_stdout << line.substr (pos, end_pos-pos) << std::endl; |
|
1485 |
|
1486 if (end_pos == NPOS) |
|
1487 break; |
|
1488 |
|
1489 pos = end_pos + 1; |
|
1490 width = max_width; |
|
1491 octave_stdout << std::string (deflen + 1, ' '); |
|
1492 } |
|
1493 } |
|
1494 |
|
1495 DEFCMD (lookfor, args, nargout, |
|
1496 "-*- texinfo -*-\n\ |
|
1497 @deffn {Command} lookfor @var{str}\n\ |
|
1498 @deffnx {Command} lookfor -all @var{str}\n\ |
|
1499 @deffnx {Function} {[@var{fun}, @var{helpstring}] = } lookfor (@var{str})\n\ |
|
1500 @deffnx {Function} {[@var{fun}, @var{helpstring}] = } lookfor ('-all', @var{str})\n\ |
|
1501 Search for the string @var{str} in all of the functions found in\n\ |
|
1502 @var{LOADPATH}. By default @code{lookfor} searchs for @var{str} in the\n\ |
|
1503 first sentence of the help string of each function found. The entire\n\ |
|
1504 help string of each function found of @var{LOADPATH} can be search if\n\ |
|
1505 the '-all' argument is supplied. All searches are case insensitive.\n\ |
|
1506 \n\ |
|
1507 Called with no output arguments, @code{lookfor} prints the list of matching\n\ |
|
1508 functions to the terminal. Otherwise the output arguments @var{fun} and\n\ |
|
1509 @var{helpstring} define the matching functions and the first sentence of\n\ |
|
1510 each of their help strings.\n\ |
|
1511 \n\ |
|
1512 Note that the ability of @code{lookfor} to correctly identify the first\n\ |
|
1513 sentence of the help of the functions is dependent on the format of the\n\ |
|
1514 functions help. All of the functions in octave itself will correctly\n\ |
|
1515 find the first sentence, but the same can not be guaranteed for other\n\ |
|
1516 functions. Therefore the use of the '-all' argument might be necessary\n\ |
|
1517 to find related functions that are not part of octave.\n\ |
|
1518 @end deffn\n\ |
|
1519 @seealso{which, help}") |
|
1520 { |
|
1521 octave_value_list retval; |
|
1522 int nargin = args.length (); |
|
1523 bool first_sentence_only = true; |
|
1524 |
|
1525 if (nargin != 1 && nargin != 2) |
|
1526 { |
|
1527 usage ("lookfor"); |
|
1528 return retval; |
|
1529 } |
|
1530 |
|
1531 string_vector ret[2]; |
|
1532 |
|
1533 std::string txt; |
|
1534 |
|
1535 if (args(0).is_string ()) |
|
1536 { |
|
1537 txt = args(0).string_value (); |
|
1538 |
|
1539 if (nargin == 2) |
|
1540 { |
|
1541 if (args(1).is_string ()) |
|
1542 { |
|
1543 std::string tmp = args(1).string_value (); |
|
1544 |
|
1545 if (txt.substr(0,1) == "-") |
|
1546 { |
|
1547 txt = tmp; |
|
1548 tmp = args(0).string_value (); |
|
1549 } |
|
1550 |
|
1551 if (tmp == "-all") |
|
1552 first_sentence_only = false; |
|
1553 else |
|
1554 error ("lookfor: unrecognized option argument"); |
|
1555 } |
|
1556 else |
|
1557 error ("lookfor: arguments must be a string"); |
|
1558 } |
|
1559 } |
|
1560 else |
|
1561 error ("lookfor: argument must be a string"); |
|
1562 |
|
1563 if (!error_state) |
|
1564 { |
|
1565 // All tests in lower case |
|
1566 transform (txt.begin (), txt.end (), txt.begin (), tolower); |
|
1567 |
|
1568 help_list *ptr = keyword_help (); |
|
1569 while (ptr->name) |
|
1570 { |
|
1571 std::string name = ptr->name; |
|
1572 std::string h = ptr->help; |
|
1573 |
|
1574 std::string s; |
|
1575 if (first_sentence_only) |
|
1576 s = first_help_sentence (h); |
|
1577 else |
|
1578 s = h; |
|
1579 |
|
1580 transform (s.begin (), s.end (), s.begin (), tolower); |
|
1581 |
|
1582 if (s.length () > 0 && s.find (txt) != NPOS) |
|
1583 { |
|
1584 if (nargout) |
|
1585 { |
|
1586 ret[0].append (name); |
|
1587 ret[1].append (first_help_sentence (h)); |
|
1588 } |
|
1589 else |
|
1590 print_lookfor (name, first_help_sentence (h)); |
|
1591 } |
|
1592 |
|
1593 OCTAVE_QUIT; |
|
1594 |
|
1595 ptr++; |
|
1596 } |
|
1597 |
|
1598 ptr = operator_help (); |
|
1599 while (ptr->name) |
|
1600 { |
|
1601 std::string name = ptr->name; |
|
1602 std::string h = ptr->help; |
|
1603 |
|
1604 std::string s; |
|
1605 if (first_sentence_only) |
|
1606 s = first_help_sentence (h); |
|
1607 else |
|
1608 s = h; |
|
1609 |
|
1610 transform (s.begin (), s.end (), s.begin (), tolower); |
|
1611 |
|
1612 if (s.length () > 0 && s.find (txt) != NPOS) |
|
1613 { |
|
1614 if (nargout) |
|
1615 { |
|
1616 ret[0].append (name); |
|
1617 ret[1].append (first_help_sentence (h)); |
|
1618 } |
|
1619 else |
|
1620 print_lookfor (name, first_help_sentence (h)); |
|
1621 } |
|
1622 |
|
1623 OCTAVE_QUIT; |
|
1624 |
|
1625 ptr++; |
|
1626 } |
|
1627 |
|
1628 // Check the symbol record table |
|
1629 string_vector names |
|
1630 = fbi_sym_tab->name_list (string_vector (), true); |
|
1631 |
|
1632 for (octave_idx_type i = 0; i < names.length (); i++) |
|
1633 { |
|
1634 std::string name = names (i); |
|
1635 |
|
1636 OCTAVE_QUIT; |
|
1637 |
|
1638 symbol_record *sr = lookup_by_name (name, 0); |
|
1639 if (sr && sr->is_defined ()) |
|
1640 { |
|
1641 std::string h = sr->help (); |
|
1642 std::string s; |
|
1643 if (first_sentence_only) |
|
1644 s = first_help_sentence (h); |
|
1645 else |
|
1646 s = h; |
|
1647 |
|
1648 transform (s.begin (), s.end (), s.begin (), tolower); |
|
1649 |
|
1650 if (s.length () > 0 && s.find (txt) != NPOS) |
|
1651 { |
|
1652 if (nargout) |
|
1653 { |
|
1654 ret[0].append (name); |
|
1655 ret[1].append (first_help_sentence (h)); |
|
1656 } |
|
1657 else |
|
1658 print_lookfor (name, first_help_sentence (h)); |
|
1659 } |
|
1660 } |
|
1661 } |
|
1662 |
|
1663 string_vector dirs = Vload_path_dir_path.all_directories (); |
|
1664 |
|
1665 int len = dirs.length (); |
|
1666 |
|
1667 for (int i = 0; i < len; i++) |
|
1668 { |
|
1669 names = octave_fcn_file_name_cache::list (dirs[i]); |
|
1670 |
|
1671 if (! names.empty ()) |
|
1672 { |
|
1673 for (int j = 0; j < names.length (); j++) |
|
1674 { |
|
1675 std::string name = names (j); |
|
1676 |
|
1677 OCTAVE_QUIT; |
|
1678 |
|
1679 // Strip extension |
|
1680 size_t l = name.length (); |
|
1681 if (l > 4 && name.substr (l-4) == ".oct") |
|
1682 name = name.substr (0, l - 4); |
|
1683 else if (l > 2 && name.substr (l-2) == ".m") |
|
1684 name = name.substr (0, l - 2); |
|
1685 else |
|
1686 continue; |
|
1687 |
|
1688 // Check if already in symbol table |
|
1689 symbol_record *sr = fbi_sym_tab->lookup (name); |
|
1690 |
|
1691 if (!sr) |
|
1692 { |
|
1693 // Check if this version is first in the path |
|
1694 string_vector tmp (2); |
|
1695 tmp(0) = name + ".oct"; |
|
1696 tmp(1) = name + ".m"; |
|
1697 std::string file_name = |
|
1698 Vload_path_dir_path.find_first_of (tmp); |
|
1699 |
|
1700 |
|
1701 if (file_name == dirs[i] + tmp(0) |
|
1702 || file_name == dirs[i] + tmp(1)) |
|
1703 { |
5484
|
1704 bool symbol_found; |
|
1705 |
|
1706 std::string h |
|
1707 = get_help_from_file (file_name, symbol_found); |
5447
|
1708 |
|
1709 std::string s; |
|
1710 if (first_sentence_only) |
|
1711 s = first_help_sentence (h); |
|
1712 else |
|
1713 s = h; |
|
1714 |
|
1715 transform (s.begin (), s.end (), s.begin (), tolower); |
|
1716 |
|
1717 if (s.length () > 0 && s.find (txt) != NPOS) |
|
1718 { |
|
1719 if (nargout) |
|
1720 { |
|
1721 ret[0].append (name); |
|
1722 ret[1].append (first_help_sentence (h)); |
|
1723 } |
|
1724 else |
|
1725 print_lookfor (name, first_help_sentence (h)); |
|
1726 } |
|
1727 } |
|
1728 } |
|
1729 } |
|
1730 } |
|
1731 } |
|
1732 |
|
1733 |
|
1734 if (nargout != 0) |
|
1735 { |
|
1736 retval (1) = ret[1]; |
|
1737 retval (0) = ret[0]; |
|
1738 } |
|
1739 } |
|
1740 else |
|
1741 { |
|
1742 error ("lookfor: argument must be a string"); |
|
1743 } |
|
1744 |
|
1745 return retval; |
|
1746 } |
|
1747 |
2189
|
1748 static int |
2202
|
1749 info_file (void) |
|
1750 { |
|
1751 int status = 0; |
|
1752 |
3523
|
1753 std::string s = builtin_string_variable ("INFO_FILE"); |
2202
|
1754 |
|
1755 if (s.empty ()) |
|
1756 { |
|
1757 gripe_invalid_value_specified ("INFO_FILE"); |
|
1758 status = -1; |
|
1759 } |
|
1760 else |
|
1761 Vinfo_file = s; |
|
1762 |
|
1763 return status; |
|
1764 } |
|
1765 |
|
1766 static int |
|
1767 info_prog (void) |
|
1768 { |
|
1769 int status = 0; |
|
1770 |
3523
|
1771 std::string s = builtin_string_variable ("INFO_PROGRAM"); |
2202
|
1772 |
|
1773 if (s.empty ()) |
|
1774 { |
|
1775 gripe_invalid_value_specified ("INFO_PROGRAM"); |
|
1776 status = -1; |
|
1777 } |
|
1778 else |
|
1779 Vinfo_prog = s; |
|
1780 |
|
1781 return status; |
|
1782 } |
|
1783 |
3014
|
1784 static int |
3686
|
1785 makeinfo_prog (void) |
|
1786 { |
|
1787 int status = 0; |
|
1788 |
|
1789 std::string s = builtin_string_variable ("MAKEINFO_PROGRAM"); |
|
1790 |
|
1791 if (s.empty ()) |
|
1792 { |
|
1793 gripe_invalid_value_specified ("MAKEINFO_PROGRAM"); |
|
1794 status = -1; |
|
1795 } |
|
1796 else |
|
1797 Vmakeinfo_prog = s; |
|
1798 |
|
1799 return status; |
|
1800 } |
|
1801 |
|
1802 static int |
3014
|
1803 suppress_verbose_help_message (void) |
|
1804 { |
|
1805 Vsuppress_verbose_help_message |
|
1806 = check_preference ("suppress_verbose_help_message"); |
|
1807 |
|
1808 return 0; |
|
1809 } |
|
1810 |
2189
|
1811 void |
|
1812 symbols_of_help (void) |
|
1813 { |
3258
|
1814 DEFVAR (INFO_FILE, Vinfo_file, info_file, |
3332
|
1815 "-*- texinfo -*-\n\ |
|
1816 @defvr {Built-in Variable} INFO_FILE\n\ |
|
1817 The variable @code{INFO_FILE} names the location of the Octave info file.\n\ |
3686
|
1818 The default value is @code{\"@var{octave-home}/info/octave.info\"}, in\n\ |
|
1819 which @var{octave-home} is the directory where all of Octave is installed.\n\ |
3333
|
1820 @end defvr"); |
2202
|
1821 |
3258
|
1822 DEFVAR (INFO_PROGRAM, Vinfo_prog, info_prog, |
3332
|
1823 "-*- texinfo -*-\n\ |
3686
|
1824 @defvr {Built-in Variable} INFO_PROGRAM\n\ |
|
1825 The variable @code{INFO_PROGRAM} names the info program to run. Its\n\ |
|
1826 default initial value is\n\ |
|
1827 @code{\"@var{octave-home}/libexec/octave/@var{version}/exec/@var{arch}/info\"}\n\ |
|
1828 in which @var{octave-home} is the directory where all of Octave is\n\ |
|
1829 installed, @var{version} is the Octave version number, and @var{arch}\n\ |
|
1830 is the system type (for example, @code{i686-pc-linux-gnu}). The\n\ |
|
1831 default initial value may be overridden by the environment variable\n\ |
|
1832 @code{OCTAVE_INFO_PROGRAM}, or the command line argument\n\ |
|
1833 @code{--info-program NAME}, or by setting the value of\n\ |
|
1834 @code{INFO_PROGRAM} in a startup script\n\ |
|
1835 @end defvr"); |
|
1836 |
|
1837 DEFVAR (MAKEINFO_PROGRAM, Vmakeinfo_prog, makeinfo_prog, |
|
1838 "-*- texinfo -*-\n\ |
|
1839 @defvr {Built-in Variable} MAKEINFO_PROGRAM\n\ |
|
1840 The variable @code{MAKEINFO_PROGRAM} names the makeinfo program that\n\ |
|
1841 Octave runs to format help text that contains Texinfo markup commands.\n\ |
|
1842 Its default initial value is @code{\"makeinfo\"}.\n\ |
3333
|
1843 @end defvr"); |
2202
|
1844 |
4233
|
1845 DEFVAR (suppress_verbose_help_message, false, suppress_verbose_help_message, |
3332
|
1846 "-*- texinfo -*-\n\ |
|
1847 @defvr {Built-in Variable} suppress_verbose_help_message\n\ |
|
1848 If the value of @code{suppress_verbose_help_message} is nonzero, Octave\n\ |
|
1849 will not add additional help information to the end of the output from\n\ |
|
1850 the @code{help} command and usage messages for built-in commands.\n\ |
3333
|
1851 @end defvr"); |
2189
|
1852 } |
|
1853 |
1
|
1854 /* |
|
1855 ;;; Local Variables: *** |
|
1856 ;;; mode: C++ *** |
|
1857 ;;; End: *** |
|
1858 */ |