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