1
|
1 // help.cc -*- C++ -*- |
|
2 /* |
|
3 |
389
|
4 Copyright (C) 1992, 1993, 1994 John W. Eaton |
1
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
1
|
26 #endif |
|
27 |
529
|
28 #include <signal.h> |
|
29 #include <stdlib.h> |
1
|
30 #include <iostream.h> |
529
|
31 #include <strstream.h> |
240
|
32 |
529
|
33 #include "tree.h" |
|
34 #include "sighandlers.h" |
|
35 #include "user-prefs.h" |
|
36 #include "tree-expr.h" |
|
37 #include "variables.h" |
|
38 #include "oct-obj.h" |
|
39 #include "symtab.h" |
|
40 #include "octave.h" |
|
41 #include "dirfns.h" |
|
42 #include "pager.h" |
|
43 #include "error.h" |
242
|
44 #include "utils.h" |
1
|
45 #include "help.h" |
529
|
46 #include "defun.h" |
|
47 |
|
48 extern "C" |
|
49 { |
|
50 #include "info/info.h" |
|
51 #include "info/dribble.h" |
|
52 #include "info/terminal.h" |
|
53 |
|
54 extern int initialize_info_session (); |
|
55 extern int index_entry_exists (); |
|
56 extern int do_info_index_search (); |
|
57 extern void finish_info_session (); |
|
58 extern char *replace_in_documentation (); |
|
59 |
|
60 // XXX FIXME XXX |
|
61 #undef __FUNCTION_DEF |
|
62 #include <readline/tilde.h> |
|
63 } |
1
|
64 |
|
65 static help_list operators[] = |
|
66 { |
|
67 { "!", |
|
68 "Logical not operator. See also `~'.\n", }, |
|
69 |
|
70 { "!=", |
|
71 "Logical not equals operator. See also `~' and `<>'.\n", }, |
|
72 |
|
73 { "\"", |
|
74 "String delimiter.\n", }, |
|
75 |
|
76 { "#", |
|
77 "Begin comment character. See also `%'.\n", }, |
|
78 |
|
79 { "%", |
|
80 "Begin comment charcter. See also `#'.\n", }, |
|
81 |
|
82 { "&", |
|
83 "Logical and operator. See also `&&'.\n", }, |
|
84 |
|
85 { "&&", |
|
86 "Logical and operator. See also `&'.\n", }, |
|
87 |
|
88 { "'", |
|
89 "Matrix transpose operator. For complex matrices, computes the\n\ |
|
90 complex conjugate (Hermitian) transpose. See also `.''\n\ |
|
91 \n\ |
|
92 The single quote character may also be used to delimit strings, but\n\ |
|
93 it is better to use the double quote character, since that is never\n\ |
|
94 ambiguous\n", }, |
|
95 |
|
96 { "(", |
|
97 "Array index or function argument delimiter.\n", }, |
|
98 |
|
99 { ")", |
|
100 "Array index or function argument delimiter.\n", }, |
|
101 |
|
102 { "*", |
|
103 "Multiplication operator. See also `.*'\n", }, |
|
104 |
|
105 { "**", |
|
106 "Power operator. See also `^', `.**', and `.^'\n", }, |
|
107 |
|
108 { "+", |
|
109 "Addition operator.\n", }, |
|
110 |
|
111 { "++", |
|
112 "Increment operator. As in C, may be applied as a prefix or postfix operator.\n", }, |
|
113 |
|
114 { ",", |
|
115 "Array index, function argument, or command separator.\n", }, |
|
116 |
|
117 { "-", |
|
118 "Subtraction or unary negation operator.\n", }, |
|
119 |
|
120 { "--", |
|
121 "Decrement operator. As in C, may be applied as a prefix or postfix operator.\n", }, |
|
122 |
|
123 { ".'", |
|
124 "Matrix transpose operator. For complex matrices, computes the\n\ |
|
125 transpose, *not* the complex conjugate transpose. See also `''.\n", }, |
|
126 |
|
127 { ".*", |
|
128 "Element by element multiplication operator. See also `*'.\n", }, |
|
129 |
|
130 { ".**", |
|
131 "Element by element power operator. See also `**', `^', and `.^'.\n", }, |
|
132 |
|
133 { "./", |
|
134 "Element by element division operator. See also `/' and `\\'.\n", }, |
|
135 |
|
136 { ".^", |
389
|
137 "Element by element power operator. See also `**', `^', and `.^'.\n", }, |
1
|
138 |
|
139 { "/", |
|
140 "Right division. See also `\\' and `./'.\n", }, |
|
141 |
|
142 { ":", |
|
143 "Select entire rows or columns of matrices.\n", }, |
|
144 |
|
145 { ";", |
|
146 "Array row or command separator. See also `,'.\n", }, |
|
147 |
|
148 { "<", |
|
149 "Less than operator.\n", }, |
|
150 |
|
151 { "<=", |
|
152 "Less than or equals operator.\n", }, |
|
153 |
|
154 { "<>", |
|
155 "Logical not equals operator. See also `!=' and `~='.\n", }, |
|
156 |
|
157 { "=", |
|
158 "Assignment operator.\n", }, |
|
159 |
|
160 { "==", |
|
161 "Equality test operator.\n", }, |
|
162 |
|
163 { ">", |
|
164 "Greater than operator.\n", }, |
|
165 |
|
166 { ">=", |
|
167 "Greater than or equals operator.\n", }, |
|
168 |
|
169 { "[", |
|
170 "Return list delimiter. See also `]'.\n", }, |
|
171 |
|
172 { "\\", |
|
173 "Left division operator. See also `/' and `./'.\n", }, |
|
174 |
|
175 { "]", |
|
176 "Return list delimiter. See also `['.\n", }, |
|
177 |
|
178 { "^", |
|
179 "Power operator. See also `**', `.^', and `.**.'\n", }, |
|
180 |
|
181 { "|", |
|
182 "Logical or operator. See also `||'.\n", }, |
|
183 |
|
184 { "||", |
|
185 "Logical or operator. See also `|'.\n", }, |
|
186 |
|
187 { "~", |
|
188 "Logical not operator. See also `!' and `~'.\n", }, |
|
189 |
|
190 { "~=", |
|
191 "Logical not equals operator. See also `<>' and `!='.\n", }, |
|
192 |
529
|
193 { 0, 0, }, |
1
|
194 }; |
|
195 |
|
196 static help_list keywords[] = |
|
197 { |
|
198 { "break", |
|
199 "Exit the innermost enclosing while or for loop.\n", }, |
|
200 |
|
201 { "continue", |
|
202 "Jump to the end of the innermost enclosing while or for loop.\n", }, |
|
203 |
|
204 { "else", |
|
205 "Alternate action for an if block.\n", }, |
|
206 |
|
207 { "elseif", |
|
208 "Alternate conditional test for an if block.\n", }, |
|
209 |
|
210 { "end", |
|
211 "Mark the end of any for, if, while, or function block.\n", }, |
|
212 |
|
213 { "endfor", |
|
214 "Mark the end of a for loop.\n", }, |
|
215 |
|
216 { "endfunction", |
|
217 "Mark the end of a function.\n", }, |
|
218 |
|
219 { "endif", |
|
220 "Mark the end of an if block.\n", }, |
|
221 |
|
222 { "endwhile", |
|
223 "Mark the end of a while loop.\n", }, |
|
224 |
|
225 { "for", |
|
226 "Begin a for loop.\n", }, |
|
227 |
|
228 { "function", |
|
229 "Begin a function body.\n", }, |
|
230 |
|
231 { "global", |
|
232 "Declare variables to have global scope.\n", }, |
|
233 |
|
234 { "gplot", |
|
235 "Produce 2-D plots using gnuplot-like command syntax.\n", }, |
|
236 |
|
237 { "gsplot", |
|
238 "Produce 3-D plots using gnuplot-like command syntax.\n", }, |
|
239 |
|
240 { "if", |
|
241 "Begin an if block.\n", }, |
|
242 |
|
243 { "return", |
|
244 "Return from a function.\n", }, |
|
245 |
|
246 { "while", |
|
247 "Begin a while loop.\n", }, |
|
248 |
529
|
249 { 0, 0, }, |
1
|
250 }; |
|
251 |
242
|
252 /* |
|
253 * Return a copy of the operator or keyword names. |
|
254 */ |
1
|
255 char ** |
|
256 names (help_list *lst, int& count) |
|
257 { |
|
258 count = 0; |
|
259 help_list *ptr = lst; |
529
|
260 while (ptr->name) |
1
|
261 { |
|
262 count++; |
|
263 ptr++; |
|
264 } |
|
265 |
|
266 if (count == 0) |
529
|
267 return 0; |
1
|
268 |
|
269 char **name_list = new char * [count+1]; |
|
270 |
|
271 ptr = lst; |
|
272 int i = 0; |
529
|
273 while (ptr->name) |
1
|
274 { |
242
|
275 name_list[i++] = strsave (ptr->name); |
1
|
276 ptr++; |
|
277 } |
|
278 |
529
|
279 name_list[i] = 0; |
1
|
280 return name_list; |
|
281 } |
|
282 |
|
283 help_list * |
|
284 operator_help (void) |
|
285 { |
|
286 return operators; |
|
287 } |
|
288 |
|
289 help_list * |
|
290 keyword_help (void) |
|
291 { |
|
292 return keywords; |
|
293 } |
|
294 |
542
|
295 void |
|
296 additional_help_message (ostrstream& output_buf) |
|
297 { |
|
298 output_buf |
|
299 << "\n" |
|
300 << "Additional help for builtin functions, operators, and variables\n" |
|
301 << "is available in the on-line version of the manual.\n" |
|
302 << "\n" |
|
303 << "Use the command `help -i <topic>' to search the manual index.\n"; |
|
304 } |
|
305 |
|
306 void |
|
307 print_usage (const char *string, int just_usage) |
|
308 { |
|
309 ostrstream output_buf; |
|
310 |
|
311 symbol_record *sym_rec = global_sym_tab->lookup (string, 0, 0); |
|
312 if (sym_rec) |
|
313 { |
|
314 char *h = sym_rec->help (); |
|
315 if (h && *h) |
|
316 { |
|
317 output_buf << "\n*** " << string << ":\n\n" |
|
318 << h << "\n"; |
|
319 |
|
320 if (! just_usage) |
|
321 additional_help_message (output_buf); |
|
322 output_buf << ends; |
|
323 maybe_page_output (output_buf); |
|
324 } |
|
325 } |
|
326 } |
|
327 |
529
|
328 static void |
542
|
329 display_names_from_help_list (ostrstream& output_buf, help_list *list, |
|
330 const char *desc) |
529
|
331 { |
|
332 int count = 0; |
|
333 char **symbols = names (list, count); |
|
334 output_buf << "\n*** " << desc << ":\n\n"; |
|
335 if (symbols && count > 0) |
|
336 list_in_columns (output_buf, symbols); |
|
337 delete [] symbols; |
|
338 } |
|
339 |
|
340 static void |
542
|
341 display_symtab_names (ostrstream& output_buf, char **names, |
|
342 int count, const char *desc) |
|
343 { |
|
344 output_buf << "\n*** " << desc << ":\n\n"; |
|
345 if (names && count > 0) |
|
346 list_in_columns (output_buf, names); |
|
347 } |
|
348 |
|
349 static void |
529
|
350 simple_help (void) |
|
351 { |
|
352 ostrstream output_buf; |
|
353 |
542
|
354 display_names_from_help_list (output_buf, operator_help (), |
|
355 "operators"); |
529
|
356 |
542
|
357 display_names_from_help_list (output_buf, keyword_help (), |
|
358 "reserved words"); |
529
|
359 |
542
|
360 #ifdef LIST_SYMBOLS |
|
361 #undef LIST_SYMBOLS |
|
362 #endif |
|
363 #define LIST_SYMBOLS(type, msg) \ |
|
364 do \ |
|
365 { \ |
|
366 int count; \ |
|
367 char **names = global_sym_tab->list (count, 1, type); \ |
|
368 display_symtab_names (output_buf, names, count, msg); \ |
|
369 char **ptr = names; \ |
|
370 while (*ptr) \ |
|
371 delete [] *ptr++; \ |
|
372 delete [] names; \ |
|
373 } \ |
|
374 while (0) |
529
|
375 |
542
|
376 // XXX FIXME XXX -- is this distinction needed? |
|
377 LIST_SYMBOLS (symbol_def::TEXT_FUNCTION, |
|
378 "text functions (these names are also reserved)"); |
529
|
379 |
542
|
380 LIST_SYMBOLS (symbol_def::MAPPER_FUNCTION, "mapper functions"); |
|
381 |
|
382 LIST_SYMBOLS (symbol_def::BUILTIN_FUNCTION, "general functions"); |
|
383 |
|
384 LIST_SYMBOLS (symbol_def::BUILTIN_VARIABLE, "builtin variables"); |
|
385 |
529
|
386 // Also need to list variables and currently compiled functions from |
|
387 // the symbol table, if there are any. |
|
388 |
|
389 // Also need to search octave_path for script files. |
|
390 |
|
391 char **path = pathstring_to_vector (user_pref.loadpath); |
|
392 |
|
393 char **ptr = path; |
|
394 if (ptr) |
|
395 { |
|
396 while (*ptr) |
|
397 { |
|
398 int count; |
|
399 char **names = get_fcn_file_names (count, *ptr, 0); |
|
400 output_buf << "\n*** function files in " |
|
401 << make_absolute (*ptr, the_current_working_directory) |
|
402 << ":\n\n"; |
|
403 if (names && count > 0) |
|
404 list_in_columns (output_buf, names); |
|
405 delete [] names; |
|
406 ptr++; |
|
407 } |
|
408 } |
|
409 |
|
410 additional_help_message (output_buf); |
|
411 output_buf << ends; |
|
412 maybe_page_output (output_buf); |
|
413 } |
|
414 |
|
415 static int |
|
416 try_info (const char *string, int force = 0) |
|
417 { |
|
418 int status = 0; |
|
419 |
|
420 char *directory_name = strsave (user_pref.info_file); |
|
421 char *temp = filename_non_directory (directory_name); |
|
422 |
|
423 if (temp != directory_name) |
|
424 { |
|
425 *temp = 0; |
|
426 info_add_path (directory_name, INFOPATH_PREPEND); |
|
427 } |
|
428 |
|
429 delete [] directory_name; |
|
430 |
|
431 NODE *initial_node = info_get_node (user_pref.info_file, 0); |
|
432 |
|
433 if (! initial_node) |
|
434 { |
|
435 warning ("can't find info file!\n"); |
|
436 status = -1; |
|
437 } |
|
438 else |
|
439 { |
|
440 status = initialize_info_session (initial_node, 0); |
|
441 |
|
442 if (status == 0 && (force || index_entry_exists (windows, string))) |
|
443 { |
|
444 terminal_clear_screen (); |
|
445 |
|
446 terminal_prep_terminal (); |
|
447 |
|
448 display_update_display (windows); |
|
449 |
|
450 info_last_executed_command = 0; |
|
451 |
|
452 if (! force) |
|
453 do_info_index_search (windows, 0, string); |
|
454 |
|
455 char *format = replace_in_documentation |
|
456 ("Type \"\\[quit]\" to quit, \"\\[get-help-window]\" for help."); |
|
457 |
|
458 window_message_in_echo_area (format); |
|
459 |
|
460 info_read_and_dispatch (); |
|
461 |
|
462 terminal_goto_xy (0, screenheight - 1); |
|
463 |
|
464 terminal_clear_to_eol (); |
|
465 |
|
466 terminal_unprep_terminal (); |
|
467 |
|
468 status = 1; |
|
469 } |
|
470 |
|
471 finish_info_session (initial_node, 0); |
|
472 } |
|
473 |
|
474 return status; |
|
475 } |
|
476 |
542
|
477 int |
|
478 help_from_list (ostrstream& output_buf, const help_list *list, |
|
479 const char *string, int usage) |
|
480 { |
|
481 char *name; |
|
482 while ((name = list->name) != 0) |
|
483 { |
|
484 if (strcmp (name, string) == 0) |
|
485 { |
|
486 if (usage) |
|
487 output_buf << "\nusage: "; |
|
488 else |
|
489 { |
|
490 output_buf << "\n*** " << string << ":\n\n"; |
|
491 } |
|
492 |
|
493 output_buf << list->help << "\n"; |
|
494 |
|
495 return 1; |
|
496 } |
|
497 list++; |
|
498 } |
|
499 return 0; |
|
500 } |
|
501 |
529
|
502 DEFUN_TEXT ("help", Fhelp, Shelp, -1, 1, |
|
503 "help [-i] [topic ...]\n\ |
|
504 \n\ |
|
505 print cryptic yet witty messages") |
|
506 { |
|
507 Octave_object retval; |
|
508 |
|
509 DEFINE_ARGV("help"); |
|
510 |
|
511 if (argc == 1) |
|
512 { |
|
513 simple_help (); |
|
514 } |
|
515 else |
|
516 { |
|
517 if (argv[1] && strcmp (argv[1], "-i") == 0) |
|
518 { |
|
519 argc--; |
|
520 argv++; |
|
521 |
|
522 if (argc == 1) |
|
523 { |
|
524 volatile sig_handler *old_sigint_handler; |
|
525 old_sigint_handler = signal (SIGINT, SIG_IGN); |
|
526 |
|
527 try_info (0, 1); |
|
528 |
|
529 signal (SIGINT, old_sigint_handler); |
|
530 } |
|
531 else |
|
532 { |
|
533 while (--argc > 0) |
|
534 { |
|
535 argv++; |
|
536 |
|
537 if (! *argv || ! **argv) |
|
538 continue; |
|
539 |
|
540 volatile sig_handler *old_sigint_handler; |
|
541 old_sigint_handler = signal (SIGINT, SIG_IGN); |
|
542 |
|
543 if (! try_info (*argv)) |
|
544 { |
|
545 message ("help", |
|
546 "sorry, `%s' is not indexed in the manual", |
|
547 *argv); |
|
548 sleep (2); |
|
549 } |
|
550 |
|
551 signal (SIGINT, old_sigint_handler); |
|
552 } |
|
553 } |
|
554 } |
|
555 else |
|
556 { |
|
557 ostrstream output_buf; |
|
558 |
|
559 char *fcn_file_name = 0; |
|
560 symbol_record *sym_rec; |
|
561 help_list *op_help_list = operator_help (); |
|
562 help_list *kw_help_list = keyword_help (); |
|
563 |
|
564 while (--argc > 0) |
|
565 { |
|
566 argv++; |
|
567 |
|
568 if (! *argv || ! **argv) |
|
569 continue; |
|
570 |
|
571 if (help_from_list (output_buf, op_help_list, *argv, 0)) |
|
572 continue; |
|
573 |
|
574 if (help_from_list (output_buf, kw_help_list, *argv, 0)) |
|
575 continue; |
|
576 |
|
577 sym_rec = curr_sym_tab->lookup (*argv, 0, 0); |
|
578 if (sym_rec) |
|
579 { |
|
580 char *h = sym_rec->help (); |
|
581 if (h && *h) |
|
582 { |
|
583 output_buf << "\n*** " << *argv << ":\n\n" |
|
584 << h << "\n"; |
|
585 continue; |
|
586 } |
|
587 } |
|
588 |
|
589 sym_rec = global_sym_tab->lookup (*argv, 0, 0); |
|
590 if (sym_rec && ! symbol_out_of_date (sym_rec)) |
|
591 { |
|
592 char *h = sym_rec->help (); |
|
593 if (h && *h) |
|
594 { |
|
595 output_buf << "\n*** " << *argv << ":\n\n" |
|
596 << h << "\n"; |
|
597 continue; |
|
598 } |
|
599 } |
|
600 |
|
601 // Try harder to find function files that might not be defined yet, or |
|
602 // that appear to be out of date. Don\'t execute commands from the |
|
603 // file if it turns out to be a script file. |
|
604 |
|
605 fcn_file_name = fcn_file_in_path (*argv); |
|
606 if (fcn_file_name) |
|
607 { |
|
608 sym_rec = global_sym_tab->lookup (*argv, 1, 0); |
|
609 if (sym_rec) |
|
610 { |
|
611 tree_identifier tmp (sym_rec); |
|
612 tmp.parse_fcn_file (0); |
|
613 char *h = sym_rec->help (); |
|
614 if (h && *h) |
|
615 { |
|
616 output_buf << "\n*** " << *argv << ":\n\n" |
|
617 << h << "\n"; |
|
618 continue; |
|
619 } |
|
620 } |
|
621 } |
|
622 delete [] fcn_file_name; |
|
623 |
|
624 output_buf << "\nhelp: sorry, `" << *argv |
|
625 << "' is not documented\n"; |
|
626 } |
|
627 |
|
628 additional_help_message (output_buf); |
|
629 output_buf << ends; |
|
630 maybe_page_output (output_buf); |
|
631 } |
|
632 } |
|
633 |
|
634 DELETE_ARGV; |
|
635 |
|
636 return retval; |
|
637 } |
|
638 |
1
|
639 /* |
|
640 ;;; Local Variables: *** |
|
641 ;;; mode: C++ *** |
|
642 ;;; page-delimiter: "^/\\*" *** |
|
643 ;;; End: *** |
|
644 */ |