1
|
1 // help.cc -*- C++ -*- |
|
2 /* |
|
3 |
1009
|
4 Copyright (C) 1992, 1993, 1994, 1995 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 |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
1
|
26 #endif |
|
27 |
1343
|
28 #include <csignal> |
|
29 #include <cstdlib> |
|
30 #include <cstring> |
|
31 |
1755
|
32 #include <string> |
|
33 |
1350
|
34 #include <iostream.h> |
|
35 #include <strstream.h> |
|
36 |
|
37 #ifdef HAVE_UNISTD_H |
1295
|
38 #include <sys/types.h> |
|
39 #include <unistd.h> |
|
40 #endif |
1343
|
41 |
1755
|
42 #include "str-vec.h" |
|
43 |
1352
|
44 #include "defun.h" |
|
45 #include "dirfns.h" |
|
46 #include "error.h" |
|
47 #include "help.h" |
1755
|
48 #include "oct-obj.h" |
1352
|
49 #include "pager.h" |
1466
|
50 #include "pathsearch.h" |
1755
|
51 #include "pt-const.h" |
|
52 #include "pt-exp.h" |
529
|
53 #include "sighandlers.h" |
|
54 #include "symtab.h" |
1755
|
55 #include "toplev.h" |
1588
|
56 #include "unwind-prot.h" |
1352
|
57 #include "user-prefs.h" |
242
|
58 #include "utils.h" |
1352
|
59 #include "variables.h" |
529
|
60 |
1
|
61 static help_list operators[] = |
|
62 { |
|
63 { "!", |
|
64 "Logical not operator. See also `~'.\n", }, |
|
65 |
|
66 { "!=", |
|
67 "Logical not equals operator. See also `~' and `<>'.\n", }, |
|
68 |
|
69 { "\"", |
|
70 "String delimiter.\n", }, |
|
71 |
|
72 { "#", |
928
|
73 "Begin comment character. See also `%'.", }, |
1
|
74 |
|
75 { "%", |
928
|
76 "Begin comment charcter. See also `#'.", }, |
1
|
77 |
|
78 { "&", |
928
|
79 "Logical and operator. See also `&&'.", }, |
1
|
80 |
|
81 { "&&", |
928
|
82 "Logical and operator. See also `&'.", }, |
1
|
83 |
|
84 { "'", |
|
85 "Matrix transpose operator. For complex matrices, computes the\n\ |
|
86 complex conjugate (Hermitian) transpose. See also `.''\n\ |
|
87 \n\ |
|
88 The single quote character may also be used to delimit strings, but\n\ |
|
89 it is better to use the double quote character, since that is never\n\ |
928
|
90 ambiguous", }, |
1
|
91 |
|
92 { "(", |
928
|
93 "Array index or function argument delimiter.", }, |
1
|
94 |
|
95 { ")", |
928
|
96 "Array index or function argument delimiter.", }, |
1
|
97 |
|
98 { "*", |
928
|
99 "Multiplication operator. See also `.*'", }, |
1
|
100 |
|
101 { "**", |
928
|
102 "Power operator. See also `^', `.**', and `.^'", }, |
1
|
103 |
|
104 { "+", |
928
|
105 "Addition operator.", }, |
1
|
106 |
|
107 { "++", |
928
|
108 "Increment operator. As in C, may be applied as a prefix or postfix operator.", }, |
1
|
109 |
|
110 { ",", |
928
|
111 "Array index, function argument, or command separator.", }, |
1
|
112 |
|
113 { "-", |
928
|
114 "Subtraction or unary negation operator.", }, |
1
|
115 |
|
116 { "--", |
928
|
117 "Decrement operator. As in C, may be applied as a prefix or postfix operator.", }, |
1
|
118 |
|
119 { ".'", |
|
120 "Matrix transpose operator. For complex matrices, computes the\n\ |
928
|
121 transpose, *not* the complex conjugate transpose. See also `''.", }, |
1
|
122 |
|
123 { ".*", |
928
|
124 "Element by element multiplication operator. See also `*'.", }, |
1
|
125 |
|
126 { ".**", |
928
|
127 "Element by element power operator. See also `**', `^', and `.^'.", }, |
1
|
128 |
|
129 { "./", |
928
|
130 "Element by element division operator. See also `/' and `\\'.", }, |
1
|
131 |
|
132 { ".^", |
928
|
133 "Element by element power operator. See also `**', `^', and `.^'.", }, |
1
|
134 |
|
135 { "/", |
928
|
136 "Right division. See also `\\' and `./'.", }, |
1
|
137 |
|
138 { ":", |
928
|
139 "Select entire rows or columns of matrices.", }, |
1
|
140 |
|
141 { ";", |
928
|
142 "Array row or command separator. See also `,'.", }, |
1
|
143 |
|
144 { "<", |
928
|
145 "Less than operator.", }, |
1
|
146 |
|
147 { "<=", |
928
|
148 "Less than or equals operator.", }, |
1
|
149 |
|
150 { "<>", |
928
|
151 "Logical not equals operator. See also `!=' and `~='.", }, |
1
|
152 |
|
153 { "=", |
928
|
154 "Assignment operator.", }, |
1
|
155 |
|
156 { "==", |
928
|
157 "Equality test operator.", }, |
1
|
158 |
|
159 { ">", |
928
|
160 "Greater than operator.", }, |
1
|
161 |
|
162 { ">=", |
928
|
163 "Greater than or equals operator.", }, |
1
|
164 |
|
165 { "[", |
928
|
166 "Return list delimiter. See also `]'.", }, |
1
|
167 |
|
168 { "\\", |
928
|
169 "Left division operator. See also `/' and `./'.", }, |
1
|
170 |
|
171 { "]", |
928
|
172 "Return list delimiter. See also `['.", }, |
1
|
173 |
|
174 { "^", |
928
|
175 "Power operator. See also `**', `.^', and `.**.'", }, |
1
|
176 |
|
177 { "|", |
928
|
178 "Logical or operator. See also `||'.", }, |
1
|
179 |
|
180 { "||", |
928
|
181 "Logical or operator. See also `|'.", }, |
1
|
182 |
|
183 { "~", |
928
|
184 "Logical not operator. See also `!' and `~'.", }, |
1
|
185 |
|
186 { "~=", |
928
|
187 "Logical not equals operator. See also `<>' and `!='.", }, |
1
|
188 |
529
|
189 { 0, 0, }, |
1
|
190 }; |
|
191 |
|
192 static help_list keywords[] = |
|
193 { |
928
|
194 { "all_va_args", |
|
195 "Pass all unnamed arguments to another function call.", }, |
|
196 |
1
|
197 { "break", |
928
|
198 "Exit the innermost enclosing while or for loop.", }, |
1
|
199 |
1489
|
200 { "catch", |
|
201 "begin the cleanup part of a try-catch block", }, |
|
202 |
1
|
203 { "continue", |
928
|
204 "Jump to the end of the innermost enclosing while or for loop.", }, |
1
|
205 |
|
206 { "else", |
928
|
207 "Alternate action for an if block.", }, |
1
|
208 |
|
209 { "elseif", |
928
|
210 "Alternate conditional test for an if block.", }, |
1
|
211 |
|
212 { "end", |
928
|
213 "Mark the end of any for, if, while, or function block.", }, |
|
214 |
1489
|
215 { "end_try_catch", |
|
216 "Mark the end of an try-catch block.", }, |
|
217 |
928
|
218 { "end_unwind_protect", |
|
219 "Mark the end of an unwind_protect block.", }, |
1
|
220 |
|
221 { "endfor", |
928
|
222 "Mark the end of a for loop.", }, |
1
|
223 |
|
224 { "endfunction", |
928
|
225 "Mark the end of a function.", }, |
1
|
226 |
|
227 { "endif", |
928
|
228 "Mark the end of an if block.", }, |
1
|
229 |
|
230 { "endwhile", |
928
|
231 "Mark the end of a while loop.", }, |
1
|
232 |
|
233 { "for", |
928
|
234 "Begin a for loop.", }, |
1
|
235 |
|
236 { "function", |
928
|
237 "Begin a function body.", }, |
1
|
238 |
|
239 { "global", |
928
|
240 "Declare variables to have global scope.", }, |
1
|
241 |
|
242 { "gplot", |
928
|
243 "Produce 2-D plots using gnuplot-like command syntax.", }, |
1
|
244 |
|
245 { "gsplot", |
928
|
246 "Produce 3-D plots using gnuplot-like command syntax.", }, |
1
|
247 |
|
248 { "if", |
928
|
249 "Begin an if block.", }, |
1
|
250 |
|
251 { "return", |
928
|
252 "Return from a function.", }, |
|
253 |
1489
|
254 { "try", |
|
255 "Begin a try-catch block.", }, |
|
256 |
928
|
257 { "unwind_protect", |
|
258 "Begin an unwind_protect block.", }, |
|
259 |
|
260 { "unwind_protect_cleanup", |
|
261 "Begin the cleanup section of an unwind_protect block.", }, |
1
|
262 |
|
263 { "while", |
928
|
264 "Begin a while loop.", }, |
1
|
265 |
529
|
266 { 0, 0, }, |
1
|
267 }; |
|
268 |
581
|
269 // Return a copy of the operator or keyword names. |
|
270 |
1755
|
271 string_vector |
1
|
272 names (help_list *lst, int& count) |
|
273 { |
1755
|
274 string_vector retval; |
|
275 |
1
|
276 count = 0; |
|
277 help_list *ptr = lst; |
529
|
278 while (ptr->name) |
1
|
279 { |
|
280 count++; |
|
281 ptr++; |
|
282 } |
|
283 |
1755
|
284 if (count > 0) |
|
285 { |
|
286 retval.resize (count); |
1
|
287 |
1755
|
288 ptr = lst; |
|
289 for (int i = 0; i < count; i++) |
|
290 { |
|
291 retval[i] = ptr->name; |
|
292 ptr++; |
|
293 } |
1
|
294 } |
|
295 |
1755
|
296 return retval; |
1
|
297 } |
|
298 |
|
299 help_list * |
|
300 operator_help (void) |
|
301 { |
|
302 return operators; |
|
303 } |
|
304 |
|
305 help_list * |
|
306 keyword_help (void) |
|
307 { |
|
308 return keywords; |
|
309 } |
|
310 |
1115
|
311 #define VERBOSE_HELP_MESSAGE \ |
|
312 "\n\ |
|
313 Additional help for builtin functions, operators, and variables\n\ |
|
314 is available in the on-line version of the manual.\n\ |
|
315 \n\ |
|
316 Use the command `help -i <topic>' to search the manual index.\n" |
|
317 |
|
318 static void |
1117
|
319 additional_help_message (ostrstream& output_buf) |
542
|
320 { |
1140
|
321 #ifdef USE_GNU_INFO |
1117
|
322 if (! user_pref.suppress_verbose_help_message) |
1115
|
323 output_buf << VERBOSE_HELP_MESSAGE; |
1140
|
324 #endif |
542
|
325 } |
|
326 |
|
327 void |
1755
|
328 print_usage (const string& nm, int just_usage) |
542
|
329 { |
|
330 ostrstream output_buf; |
|
331 |
1755
|
332 symbol_record *sym_rec = global_sym_tab->lookup (nm, 0, 0); |
542
|
333 if (sym_rec) |
|
334 { |
1755
|
335 string h = sym_rec->help (); |
|
336 |
|
337 if (h.length () > 0) |
542
|
338 { |
1755
|
339 output_buf << "\n*** " << nm << ":\n\n" |
542
|
340 << h << "\n"; |
|
341 |
1117
|
342 if (! just_usage) |
1118
|
343 additional_help_message (output_buf); |
542
|
344 output_buf << ends; |
|
345 maybe_page_output (output_buf); |
|
346 } |
|
347 } |
1023
|
348 else |
1755
|
349 warning ("no usage message found for `%s'", nm.c_str ()); |
542
|
350 } |
|
351 |
529
|
352 static void |
542
|
353 display_names_from_help_list (ostrstream& output_buf, help_list *list, |
|
354 const char *desc) |
529
|
355 { |
|
356 int count = 0; |
1755
|
357 string_vector symbols = names (list, count); |
1808
|
358 if (! symbols.empty ()) |
|
359 { |
|
360 output_buf << "\n*** " << desc << ":\n\n"; |
|
361 symbols.list_in_columns (output_buf); |
|
362 } |
529
|
363 } |
|
364 |
1755
|
365 static string |
742
|
366 print_symbol_type (ostrstream& output_buf, symbol_record *sym_rec, |
1755
|
367 const string& name, int print) |
742
|
368 { |
1755
|
369 string retval; |
742
|
370 |
|
371 if (sym_rec->is_user_function ()) |
|
372 { |
|
373 tree_fvc *defn = sym_rec->def (); |
1755
|
374 |
|
375 string fn = defn->fcn_file_name (); |
|
376 |
|
377 if (! fn.empty ()) |
742
|
378 { |
1755
|
379 string ff = fcn_file_in_path (fn); |
|
380 |
|
381 ff = ff.length () > 0 ? ff : fn; |
742
|
382 |
|
383 if (print) |
|
384 output_buf << name |
|
385 << " is the function defined from:\n" |
|
386 << ff << "\n"; |
|
387 else |
|
388 retval = ff; |
|
389 } |
|
390 else |
|
391 { |
|
392 if (print) |
|
393 output_buf << name << " is a user-defined function\n"; |
|
394 else |
|
395 retval = "user-defined function"; |
|
396 } |
|
397 } |
|
398 else if (sym_rec->is_text_function ()) |
|
399 { |
|
400 if (print) |
|
401 output_buf << name << " is a builtin text-function\n"; |
|
402 else |
|
403 retval = "builtin text-function"; |
|
404 } |
|
405 else if (sym_rec->is_builtin_function ()) |
|
406 { |
|
407 if (print) |
|
408 output_buf << name << " is a builtin function\n"; |
|
409 else |
|
410 retval = "builtin function"; |
|
411 } |
|
412 else if (sym_rec->is_user_variable ()) |
|
413 { |
|
414 if (print) |
|
415 output_buf << name << " is a user-defined variable\n"; |
|
416 else |
|
417 retval = "user-defined variable"; |
|
418 } |
|
419 else if (sym_rec->is_builtin_variable ()) |
|
420 { |
|
421 if (print) |
|
422 output_buf << name << " is a builtin variable\n"; |
|
423 else |
|
424 retval = "builtin variable"; |
|
425 } |
|
426 else |
|
427 { |
|
428 if (print) |
|
429 output_buf << "which: `" << name |
|
430 << "' has unknown type\n"; |
|
431 else |
|
432 retval = "unknown type"; |
|
433 } |
|
434 |
|
435 return retval; |
|
436 } |
|
437 |
529
|
438 static void |
1755
|
439 display_symtab_names (ostrstream& output_buf, const string_vector& names, |
|
440 int /* count */, const string& desc) |
542
|
441 { |
1808
|
442 if (! names.empty ()) |
|
443 { |
|
444 output_buf << "\n*** " << desc << ":\n\n"; |
|
445 names.list_in_columns (output_buf); |
|
446 } |
542
|
447 } |
|
448 |
|
449 static void |
529
|
450 simple_help (void) |
|
451 { |
|
452 ostrstream output_buf; |
|
453 |
542
|
454 display_names_from_help_list (output_buf, operator_help (), |
|
455 "operators"); |
529
|
456 |
542
|
457 display_names_from_help_list (output_buf, keyword_help (), |
|
458 "reserved words"); |
529
|
459 |
542
|
460 #ifdef LIST_SYMBOLS |
|
461 #undef LIST_SYMBOLS |
|
462 #endif |
|
463 #define LIST_SYMBOLS(type, msg) \ |
|
464 do \ |
|
465 { \ |
|
466 int count; \ |
1755
|
467 string_vector names = global_sym_tab->list (count, 0, 0, 1, type); \ |
542
|
468 display_symtab_names (output_buf, names, count, msg); \ |
|
469 } \ |
|
470 while (0) |
529
|
471 |
1358
|
472 // XXX FIXME XXX -- is this distinction needed? |
|
473 |
542
|
474 LIST_SYMBOLS (symbol_def::TEXT_FUNCTION, |
|
475 "text functions (these names are also reserved)"); |
529
|
476 |
542
|
477 LIST_SYMBOLS (symbol_def::MAPPER_FUNCTION, "mapper functions"); |
|
478 |
|
479 LIST_SYMBOLS (symbol_def::BUILTIN_FUNCTION, "general functions"); |
|
480 |
|
481 LIST_SYMBOLS (symbol_def::BUILTIN_VARIABLE, "builtin variables"); |
|
482 |
1358
|
483 // Also need to list variables and currently compiled functions from |
|
484 // the symbol table, if there are any. |
529
|
485 |
1358
|
486 // Also need to search octave_path for script files. |
529
|
487 |
1787
|
488 dir_path p (user_pref.loadpath); |
529
|
489 |
1787
|
490 string_vector dirs = p.all_directories (); |
679
|
491 |
1787
|
492 int len = dirs.length (); |
679
|
493 |
1787
|
494 for (int i = 0; i < len; i++) |
|
495 { |
|
496 string_vector names = get_fcn_file_names (dirs[i]); |
679
|
497 |
1787
|
498 if (! names.empty ()) |
|
499 { |
|
500 output_buf << "\n*** function files in " |
|
501 << make_absolute (dirs[i], the_current_working_directory) |
|
502 << ":\n\n"; |
679
|
503 |
1808
|
504 names.list_in_columns (output_buf); |
529
|
505 } |
|
506 } |
|
507 |
|
508 additional_help_message (output_buf); |
|
509 output_buf << ends; |
|
510 maybe_page_output (output_buf); |
|
511 } |
|
512 |
1140
|
513 #ifdef USE_GNU_INFO |
529
|
514 static int |
1755
|
515 try_info (const string& nm) |
529
|
516 { |
|
517 int status = 0; |
|
518 |
1295
|
519 static char *cmd_str = 0; |
|
520 |
|
521 delete [] cmd_str; |
|
522 cmd_str = 0; |
|
523 |
|
524 ostrstream cmd_buf; |
|
525 |
1613
|
526 cmd_buf << user_pref.info_prog << " --file " << user_pref.info_file; |
1295
|
527 |
1755
|
528 string directory_name = user_pref.info_file; |
|
529 size_t pos = directory_name.rfind ('/'); |
|
530 |
|
531 if (pos != NPOS) |
529
|
532 { |
1755
|
533 directory_name.resize (pos + 1); |
1295
|
534 cmd_buf << " --directory " << directory_name; |
529
|
535 } |
|
536 |
1755
|
537 if (nm.length () > 0) |
|
538 cmd_buf << " --index-search " << nm; |
529
|
539 |
1295
|
540 cmd_buf << ends; |
529
|
541 |
1295
|
542 cmd_str = cmd_buf.str (); |
529
|
543 |
1295
|
544 volatile sig_handler *old_sigint_handler; |
1443
|
545 old_sigint_handler = octave_set_signal_handler (SIGINT, SIG_IGN); |
529
|
546 |
1295
|
547 status = system (cmd_str); |
529
|
548 |
1443
|
549 octave_set_signal_handler (SIGINT, old_sigint_handler); |
529
|
550 |
1295
|
551 if ((status & 0xff) == 0) |
|
552 status = (signed char) ((status & 0xff00) >> 8); |
|
553 else |
|
554 status = 127; |
529
|
555 |
|
556 return status; |
|
557 } |
1140
|
558 #endif |
|
559 |
|
560 static void |
1755
|
561 help_from_info (const string_vector& argv, int idx, int argc) |
1140
|
562 { |
|
563 #ifdef USE_GNU_INFO |
1755
|
564 if (idx == argc) |
|
565 try_info (string ()); |
1140
|
566 else |
|
567 { |
1755
|
568 for (int i = idx; i < argc; i++) |
1140
|
569 { |
1755
|
570 int status = try_info (argv[i]); |
1140
|
571 |
1295
|
572 if (status) |
1140
|
573 { |
1295
|
574 if (status < 0) |
|
575 { |
|
576 message ("help", |
|
577 "sorry, `%s' is not indexed in the manual", |
1755
|
578 argv[i].c_str ()); |
1295
|
579 sleep (2); |
|
580 } |
|
581 else |
|
582 { |
|
583 error ("help: unable to find info!"); |
|
584 break; |
|
585 } |
1140
|
586 } |
|
587 } |
|
588 } |
|
589 #else |
|
590 message (0, "sorry, help -i is not available in this version of Octave"); |
|
591 #endif |
|
592 } |
529
|
593 |
542
|
594 int |
|
595 help_from_list (ostrstream& output_buf, const help_list *list, |
1755
|
596 const string& nm, int usage) |
542
|
597 { |
|
598 char *name; |
|
599 while ((name = list->name) != 0) |
|
600 { |
1755
|
601 if (strcmp (name, nm.c_str ()) == 0) |
542
|
602 { |
|
603 if (usage) |
|
604 output_buf << "\nusage: "; |
|
605 else |
|
606 { |
1755
|
607 output_buf << "\n*** " << nm << ":\n\n"; |
542
|
608 } |
|
609 |
|
610 output_buf << list->help << "\n"; |
|
611 |
|
612 return 1; |
|
613 } |
|
614 list++; |
|
615 } |
|
616 return 0; |
|
617 } |
|
618 |
1140
|
619 static void |
1755
|
620 builtin_help (int argc, const string_vector& argv) |
1140
|
621 { |
|
622 ostrstream output_buf; |
|
623 |
|
624 help_list *op_help_list = operator_help (); |
|
625 help_list *kw_help_list = keyword_help (); |
|
626 |
1755
|
627 for (int i = 1; i < argc; i++) |
1140
|
628 { |
1755
|
629 if (help_from_list (output_buf, op_help_list, argv[i], 0)) |
1140
|
630 continue; |
|
631 |
1755
|
632 if (help_from_list (output_buf, kw_help_list, argv[i], 0)) |
1140
|
633 continue; |
|
634 |
1755
|
635 symbol_record *sym_rec = lookup_by_name (argv[i], 0); |
1140
|
636 |
|
637 if (sym_rec && sym_rec->is_defined ()) |
|
638 { |
1755
|
639 string h = sym_rec->help (); |
|
640 |
|
641 if (h.length () > 0) |
1140
|
642 { |
1755
|
643 print_symbol_type (output_buf, sym_rec, argv[i], 1); |
1140
|
644 output_buf << "\n" << h << "\n"; |
|
645 continue; |
|
646 } |
|
647 } |
|
648 |
1755
|
649 string path = fcn_file_in_path (argv[i]); |
|
650 |
|
651 string h = get_help_from_file (path); |
|
652 |
|
653 if (! h.empty ()) |
1140
|
654 { |
1755
|
655 output_buf << argv[i] << " is the file:\n" |
1140
|
656 << path << "\n\n" << h << "\n"; |
1755
|
657 |
1140
|
658 continue; |
|
659 } |
|
660 |
1755
|
661 output_buf << "\nhelp: sorry, `" << argv[i] << "' is not documented\n"; |
1140
|
662 } |
|
663 |
|
664 additional_help_message (output_buf); |
|
665 output_buf << ends; |
|
666 maybe_page_output (output_buf); |
|
667 } |
|
668 |
|
669 #ifdef USE_GNU_INFO |
1488
|
670 DEFUN_TEXT ("help", Fhelp, Shelp, 10, |
529
|
671 "help [-i] [topic ...]\n\ |
|
672 \n\ |
|
673 print cryptic yet witty messages") |
1140
|
674 #else |
1488
|
675 DEFUN_TEXT ("help", Fhelp, Shelp, 10, |
1140
|
676 "help [topic ...]\n\ |
|
677 \n\ |
|
678 print cryptic yet witty messages") |
|
679 #endif |
529
|
680 { |
|
681 Octave_object retval; |
|
682 |
1755
|
683 int argc = args.length () + 1; |
|
684 |
|
685 string_vector argv = make_argv (args, "help"); |
|
686 |
|
687 if (error_state) |
|
688 return retval; |
529
|
689 |
|
690 if (argc == 1) |
|
691 { |
|
692 simple_help (); |
|
693 } |
|
694 else |
|
695 { |
1755
|
696 if (argv[1] == "-i") |
529
|
697 { |
1755
|
698 help_from_info (argv, 2, argc); |
529
|
699 } |
|
700 else |
|
701 { |
1140
|
702 builtin_help (argc, argv); |
529
|
703 } |
|
704 } |
|
705 |
|
706 return retval; |
|
707 } |
|
708 |
1488
|
709 DEFUN_TEXT ("type", Ftype, Stype, 11, |
581
|
710 "type NAME ...]\n\ |
|
711 \n\ |
|
712 display the definition of each NAME that refers to a function") |
|
713 { |
|
714 Octave_object retval; |
|
715 |
1588
|
716 begin_unwind_frame ("Ftype"); |
|
717 |
1755
|
718 // XXX FIXME XXX -- need a way to protect strings. |
|
719 // unwind_protect_ptr (user_pref.ps4); |
|
720 // user_pref.ps4 = ""; |
1588
|
721 |
1755
|
722 int argc = args.length () + 1; |
|
723 |
|
724 string_vector argv = make_argv (args, "type"); |
|
725 |
|
726 if (error_state) |
|
727 return retval; |
581
|
728 |
|
729 if (argc > 1) |
|
730 { |
1358
|
731 // XXX FIXME XXX -- we should really use getopt () |
|
732 |
581
|
733 int quiet = 0; |
1755
|
734 int idx = 1; |
|
735 if (argv[idx] == "-q") |
581
|
736 { |
|
737 quiet = 1; |
1755
|
738 idx++; |
581
|
739 } |
|
740 |
|
741 ostrstream output_buf; |
|
742 |
1755
|
743 for (int i = idx; i < argc; i++) |
581
|
744 { |
1755
|
745 string id = argv[i]; |
|
746 string elts; |
581
|
747 |
1755
|
748 if (id[id.length () - 1] != '.') |
1281
|
749 { |
1755
|
750 size_t pos = id.find ('.'); |
|
751 |
|
752 if (pos != NPOS) |
1288
|
753 { |
1755
|
754 id = id.substr (0, pos); |
|
755 elts = id.substr (pos+1); |
1288
|
756 } |
1281
|
757 } |
|
758 |
|
759 symbol_record *sym_rec = lookup_by_name (id, 0); |
581
|
760 |
|
761 if (sym_rec) |
|
762 { |
|
763 if (sym_rec->is_user_function ()) |
|
764 { |
|
765 tree_fvc *defn = sym_rec->def (); |
|
766 |
|
767 if (nargout == 0 && ! quiet) |
1755
|
768 output_buf << argv[i] << " is a user-defined function\n"; |
581
|
769 |
|
770 defn->print_code (output_buf); |
|
771 } |
|
772 |
1358
|
773 // XXX FIXME XXX -- this code should be shared with |
|
774 // Fwhich. |
581
|
775 |
|
776 else if (sym_rec->is_text_function ()) |
1755
|
777 output_buf << argv[i] << " is a builtin text-function\n"; |
581
|
778 else if (sym_rec->is_builtin_function ()) |
1755
|
779 output_buf << argv[i] << " is a builtin function\n"; |
1281
|
780 else if (sym_rec->is_user_variable () |
|
781 || sym_rec->is_builtin_variable ()) |
625
|
782 { |
|
783 tree_fvc *defn = sym_rec->def (); |
|
784 |
1288
|
785 assert (defn && defn->is_constant ()); |
1281
|
786 |
|
787 tree_constant *tmp = (tree_constant *) defn; |
625
|
788 |
1281
|
789 int var_ok = 1; |
|
790 if (tmp && tmp->is_map ()) |
|
791 { |
1755
|
792 if (! elts.empty ()) |
1281
|
793 { |
1288
|
794 tree_constant ult = |
|
795 tmp->lookup_map_element (elts, 0, 1); |
625
|
796 |
1281
|
797 if (! ult.is_defined ()) |
|
798 var_ok = 0; |
|
799 } |
|
800 } |
1288
|
801 |
1281
|
802 if (nargout == 0 && ! quiet) |
|
803 { |
1288
|
804 if (var_ok) |
|
805 { |
1755
|
806 output_buf << argv[i]; |
1288
|
807 if (sym_rec->is_user_variable ()) |
|
808 output_buf << " is a user-defined variable\n"; |
|
809 else |
|
810 output_buf << " is a built-in variable\n"; |
|
811 } |
1281
|
812 else |
1288
|
813 { |
1755
|
814 if (! elts.empty ()) |
1288
|
815 output_buf << "type: structure `" << id |
|
816 << "' has no member `" << elts << "'\n"; |
|
817 else |
|
818 output_buf << "type: `" << id |
|
819 << "' has unknown type!"; |
|
820 } |
1281
|
821 } |
|
822 if (! tmp->is_map ()) |
|
823 { |
|
824 tmp->print_code (output_buf); |
625
|
825 |
1281
|
826 if (nargout == 0) |
|
827 output_buf << "\n"; |
|
828 } |
625
|
829 } |
581
|
830 else |
1755
|
831 output_buf << "type: `" << argv[i] << "' has unknown type!\n"; |
581
|
832 } |
|
833 else |
1755
|
834 output_buf << "type: `" << argv[i] << "' undefined\n"; |
581
|
835 } |
|
836 |
|
837 output_buf << ends; |
|
838 |
|
839 if (nargout == 0) |
|
840 maybe_page_output (output_buf); |
|
841 else |
|
842 { |
|
843 char *s = output_buf.str (); |
|
844 retval = s; |
1288
|
845 delete [] s; |
581
|
846 } |
|
847 } |
|
848 else |
|
849 print_usage ("type"); |
|
850 |
1588
|
851 run_unwind_frame ("Ftype"); |
|
852 |
581
|
853 return retval; |
|
854 } |
|
855 |
1488
|
856 DEFUN_TEXT ("which", Fwhich, Swhich, 11, |
1565
|
857 "which NAME ...\n\ |
581
|
858 \n\ |
|
859 display the type of each NAME. If NAME is defined from an function\n\ |
|
860 file, print the full name of the file.") |
|
861 { |
|
862 Octave_object retval; |
|
863 |
1755
|
864 int argc = args.length () + 1; |
|
865 |
|
866 string_vector argv = make_argv (args, "which"); |
|
867 |
|
868 if (error_state) |
|
869 return retval; |
581
|
870 |
|
871 if (argc > 1) |
|
872 { |
|
873 if (nargout > 0) |
|
874 retval.resize (argc-1, Matrix ()); |
|
875 |
|
876 ostrstream output_buf; |
|
877 |
1755
|
878 for (int i = 1; i < argc; i++) |
581
|
879 { |
1755
|
880 symbol_record *sym_rec = lookup_by_name (argv[i], 0); |
581
|
881 |
|
882 if (sym_rec) |
|
883 { |
742
|
884 int print = (nargout == 0); |
1755
|
885 string tmp = print_symbol_type (output_buf, sym_rec, |
|
886 argv[i], print); |
742
|
887 if (! print) |
|
888 retval(i) = tmp; |
581
|
889 } |
|
890 else |
|
891 { |
|
892 if (nargout == 0) |
1755
|
893 output_buf << "which: `" << argv[i] << "' is undefined\n"; |
581
|
894 else |
|
895 retval(i) = "undefined"; |
|
896 } |
|
897 } |
|
898 output_buf << ends; |
|
899 maybe_page_output (output_buf); |
|
900 } |
|
901 else |
|
902 print_usage ("which"); |
|
903 |
|
904 return retval; |
|
905 } |
|
906 |
1
|
907 /* |
|
908 ;;; Local Variables: *** |
|
909 ;;; mode: C++ *** |
|
910 ;;; page-delimiter: "^/\\*" *** |
|
911 ;;; End: *** |
|
912 */ |