1
|
1 /* |
|
2 |
1884
|
3 Copyright (C) 1996 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> |
|
34 #include <strstream.h> |
|
35 |
|
36 #ifdef HAVE_UNISTD_H |
1295
|
37 #include <sys/types.h> |
|
38 #include <unistd.h> |
|
39 #endif |
1343
|
40 |
1755
|
41 #include "str-vec.h" |
|
42 |
1352
|
43 #include "defun.h" |
|
44 #include "dirfns.h" |
|
45 #include "error.h" |
|
46 #include "help.h" |
1755
|
47 #include "oct-obj.h" |
1352
|
48 #include "pager.h" |
1466
|
49 #include "pathsearch.h" |
1755
|
50 #include "pt-const.h" |
|
51 #include "pt-exp.h" |
2124
|
52 #include "pt-pr-code.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 |
2095
|
319 additional_help_message (ostream& os) |
542
|
320 { |
1140
|
321 #ifdef USE_GNU_INFO |
1117
|
322 if (! user_pref.suppress_verbose_help_message) |
2095
|
323 os << VERBOSE_HELP_MESSAGE; |
1140
|
324 #endif |
542
|
325 } |
|
326 |
|
327 void |
1755
|
328 print_usage (const string& nm, int just_usage) |
542
|
329 { |
1755
|
330 symbol_record *sym_rec = global_sym_tab->lookup (nm, 0, 0); |
542
|
331 if (sym_rec) |
|
332 { |
1755
|
333 string h = sym_rec->help (); |
|
334 |
|
335 if (h.length () > 0) |
542
|
336 { |
2095
|
337 octave_stdout << "\n*** " << nm << ":\n\n" |
542
|
338 << h << "\n"; |
|
339 |
1117
|
340 if (! just_usage) |
2095
|
341 additional_help_message (octave_stdout); |
542
|
342 } |
|
343 } |
1023
|
344 else |
1755
|
345 warning ("no usage message found for `%s'", nm.c_str ()); |
542
|
346 } |
|
347 |
529
|
348 static void |
2095
|
349 display_names_from_help_list (ostream& os, help_list *list, |
542
|
350 const char *desc) |
529
|
351 { |
|
352 int count = 0; |
1755
|
353 string_vector symbols = names (list, count); |
1808
|
354 if (! symbols.empty ()) |
|
355 { |
2095
|
356 os << "\n*** " << desc << ":\n\n"; |
|
357 symbols.list_in_columns (os); |
1808
|
358 } |
529
|
359 } |
|
360 |
1755
|
361 static string |
2095
|
362 print_symbol_type (ostream& os, symbol_record *sym_rec, |
1755
|
363 const string& name, int print) |
742
|
364 { |
1755
|
365 string retval; |
742
|
366 |
|
367 if (sym_rec->is_user_function ()) |
|
368 { |
|
369 tree_fvc *defn = sym_rec->def (); |
1755
|
370 |
|
371 string fn = defn->fcn_file_name (); |
|
372 |
|
373 if (! fn.empty ()) |
742
|
374 { |
1755
|
375 string ff = fcn_file_in_path (fn); |
|
376 |
|
377 ff = ff.length () > 0 ? ff : fn; |
742
|
378 |
|
379 if (print) |
2095
|
380 os << name |
|
381 << " is the function defined from:\n" |
|
382 << ff << "\n"; |
742
|
383 else |
|
384 retval = ff; |
|
385 } |
|
386 else |
|
387 { |
|
388 if (print) |
2095
|
389 os << name << " is a user-defined function\n"; |
742
|
390 else |
|
391 retval = "user-defined function"; |
|
392 } |
|
393 } |
|
394 else if (sym_rec->is_text_function ()) |
|
395 { |
|
396 if (print) |
2095
|
397 os << name << " is a builtin text-function\n"; |
742
|
398 else |
|
399 retval = "builtin text-function"; |
|
400 } |
|
401 else if (sym_rec->is_builtin_function ()) |
|
402 { |
|
403 if (print) |
2095
|
404 os << name << " is a builtin function\n"; |
742
|
405 else |
|
406 retval = "builtin function"; |
|
407 } |
|
408 else if (sym_rec->is_user_variable ()) |
|
409 { |
|
410 if (print) |
2095
|
411 os << name << " is a user-defined variable\n"; |
742
|
412 else |
|
413 retval = "user-defined variable"; |
|
414 } |
|
415 else if (sym_rec->is_builtin_variable ()) |
|
416 { |
|
417 if (print) |
2095
|
418 os << name << " is a builtin variable\n"; |
742
|
419 else |
|
420 retval = "builtin variable"; |
|
421 } |
|
422 else |
|
423 { |
|
424 if (print) |
2095
|
425 os << "which: `" << name << "' has unknown type\n"; |
742
|
426 else |
|
427 retval = "unknown type"; |
|
428 } |
|
429 |
|
430 return retval; |
|
431 } |
|
432 |
529
|
433 static void |
2095
|
434 display_symtab_names (ostream& os, const string_vector& names, |
1755
|
435 int /* count */, const string& desc) |
542
|
436 { |
1808
|
437 if (! names.empty ()) |
|
438 { |
2095
|
439 os << "\n*** " << desc << ":\n\n"; |
|
440 names.list_in_columns (os); |
1808
|
441 } |
542
|
442 } |
|
443 |
|
444 static void |
529
|
445 simple_help (void) |
|
446 { |
2095
|
447 display_names_from_help_list (octave_stdout, operator_help (), |
542
|
448 "operators"); |
529
|
449 |
2095
|
450 display_names_from_help_list (octave_stdout, keyword_help (), |
542
|
451 "reserved words"); |
529
|
452 |
542
|
453 #ifdef LIST_SYMBOLS |
|
454 #undef LIST_SYMBOLS |
|
455 #endif |
|
456 #define LIST_SYMBOLS(type, msg) \ |
|
457 do \ |
|
458 { \ |
|
459 int count; \ |
1755
|
460 string_vector names = global_sym_tab->list (count, 0, 0, 1, type); \ |
2095
|
461 display_symtab_names (octave_stdout, names, count, msg); \ |
542
|
462 } \ |
|
463 while (0) |
529
|
464 |
1358
|
465 // XXX FIXME XXX -- is this distinction needed? |
|
466 |
542
|
467 LIST_SYMBOLS (symbol_def::TEXT_FUNCTION, |
|
468 "text functions (these names are also reserved)"); |
529
|
469 |
542
|
470 LIST_SYMBOLS (symbol_def::MAPPER_FUNCTION, "mapper functions"); |
|
471 |
|
472 LIST_SYMBOLS (symbol_def::BUILTIN_FUNCTION, "general functions"); |
|
473 |
|
474 LIST_SYMBOLS (symbol_def::BUILTIN_VARIABLE, "builtin variables"); |
|
475 |
1358
|
476 // Also need to list variables and currently compiled functions from |
|
477 // the symbol table, if there are any. |
529
|
478 |
1358
|
479 // Also need to search octave_path for script files. |
529
|
480 |
1787
|
481 dir_path p (user_pref.loadpath); |
529
|
482 |
1787
|
483 string_vector dirs = p.all_directories (); |
679
|
484 |
1787
|
485 int len = dirs.length (); |
679
|
486 |
1787
|
487 for (int i = 0; i < len; i++) |
|
488 { |
|
489 string_vector names = get_fcn_file_names (dirs[i]); |
679
|
490 |
1787
|
491 if (! names.empty ()) |
|
492 { |
2095
|
493 octave_stdout << "\n*** function files in " |
|
494 << make_absolute (dirs[i], |
|
495 the_current_working_directory) |
|
496 << ":\n\n"; |
679
|
497 |
2095
|
498 names.list_in_columns (octave_stdout); |
529
|
499 } |
|
500 } |
|
501 |
2095
|
502 additional_help_message (octave_stdout); |
529
|
503 } |
|
504 |
1140
|
505 #ifdef USE_GNU_INFO |
529
|
506 static int |
1755
|
507 try_info (const string& nm) |
529
|
508 { |
|
509 int status = 0; |
|
510 |
1295
|
511 static char *cmd_str = 0; |
|
512 |
|
513 delete [] cmd_str; |
|
514 cmd_str = 0; |
|
515 |
|
516 ostrstream cmd_buf; |
|
517 |
1613
|
518 cmd_buf << user_pref.info_prog << " --file " << user_pref.info_file; |
1295
|
519 |
1755
|
520 string directory_name = user_pref.info_file; |
|
521 size_t pos = directory_name.rfind ('/'); |
|
522 |
|
523 if (pos != NPOS) |
529
|
524 { |
1755
|
525 directory_name.resize (pos + 1); |
1295
|
526 cmd_buf << " --directory " << directory_name; |
529
|
527 } |
|
528 |
1755
|
529 if (nm.length () > 0) |
|
530 cmd_buf << " --index-search " << nm; |
529
|
531 |
1295
|
532 cmd_buf << ends; |
529
|
533 |
1295
|
534 cmd_str = cmd_buf.str (); |
529
|
535 |
1295
|
536 volatile sig_handler *old_sigint_handler; |
1443
|
537 old_sigint_handler = octave_set_signal_handler (SIGINT, SIG_IGN); |
529
|
538 |
1295
|
539 status = system (cmd_str); |
529
|
540 |
1443
|
541 octave_set_signal_handler (SIGINT, old_sigint_handler); |
529
|
542 |
1295
|
543 if ((status & 0xff) == 0) |
|
544 status = (signed char) ((status & 0xff00) >> 8); |
|
545 else |
|
546 status = 127; |
529
|
547 |
|
548 return status; |
|
549 } |
1140
|
550 #endif |
|
551 |
|
552 static void |
1755
|
553 help_from_info (const string_vector& argv, int idx, int argc) |
1140
|
554 { |
|
555 #ifdef USE_GNU_INFO |
1755
|
556 if (idx == argc) |
|
557 try_info (string ()); |
1140
|
558 else |
|
559 { |
1755
|
560 for (int i = idx; i < argc; i++) |
1140
|
561 { |
1755
|
562 int status = try_info (argv[i]); |
1140
|
563 |
1295
|
564 if (status) |
1140
|
565 { |
1295
|
566 if (status < 0) |
|
567 { |
|
568 message ("help", |
|
569 "sorry, `%s' is not indexed in the manual", |
1755
|
570 argv[i].c_str ()); |
1295
|
571 sleep (2); |
|
572 } |
|
573 else |
|
574 { |
|
575 error ("help: unable to find info!"); |
|
576 break; |
|
577 } |
1140
|
578 } |
|
579 } |
|
580 } |
|
581 #else |
|
582 message (0, "sorry, help -i is not available in this version of Octave"); |
|
583 #endif |
|
584 } |
529
|
585 |
542
|
586 int |
2095
|
587 help_from_list (ostream& os, const help_list *list, |
1755
|
588 const string& nm, int usage) |
542
|
589 { |
|
590 char *name; |
|
591 while ((name = list->name) != 0) |
|
592 { |
1755
|
593 if (strcmp (name, nm.c_str ()) == 0) |
542
|
594 { |
|
595 if (usage) |
2095
|
596 os << "\nusage: "; |
542
|
597 else |
|
598 { |
2095
|
599 os << "\n*** " << nm << ":\n\n"; |
542
|
600 } |
|
601 |
2095
|
602 os << list->help << "\n"; |
542
|
603 |
|
604 return 1; |
|
605 } |
|
606 list++; |
|
607 } |
|
608 return 0; |
|
609 } |
|
610 |
1140
|
611 static void |
1755
|
612 builtin_help (int argc, const string_vector& argv) |
1140
|
613 { |
|
614 help_list *op_help_list = operator_help (); |
|
615 help_list *kw_help_list = keyword_help (); |
|
616 |
1755
|
617 for (int i = 1; i < argc; i++) |
1140
|
618 { |
2095
|
619 if (help_from_list (octave_stdout, op_help_list, argv[i], 0)) |
1140
|
620 continue; |
|
621 |
2095
|
622 if (help_from_list (octave_stdout, kw_help_list, argv[i], 0)) |
1140
|
623 continue; |
|
624 |
1755
|
625 symbol_record *sym_rec = lookup_by_name (argv[i], 0); |
1140
|
626 |
|
627 if (sym_rec && sym_rec->is_defined ()) |
|
628 { |
1755
|
629 string h = sym_rec->help (); |
|
630 |
|
631 if (h.length () > 0) |
1140
|
632 { |
2095
|
633 print_symbol_type (octave_stdout, sym_rec, argv[i], 1); |
|
634 octave_stdout << "\n" << h << "\n"; |
1140
|
635 continue; |
|
636 } |
|
637 } |
|
638 |
1755
|
639 string path = fcn_file_in_path (argv[i]); |
|
640 |
|
641 string h = get_help_from_file (path); |
|
642 |
|
643 if (! h.empty ()) |
1140
|
644 { |
2095
|
645 octave_stdout << argv[i] << " is the file:\n" |
1140
|
646 << path << "\n\n" << h << "\n"; |
1755
|
647 |
1140
|
648 continue; |
|
649 } |
|
650 |
2095
|
651 octave_stdout << "\nhelp: sorry, `" << argv[i] |
|
652 << "' is not documented\n"; |
1140
|
653 } |
|
654 |
2095
|
655 additional_help_message (octave_stdout); |
1140
|
656 } |
|
657 |
|
658 #ifdef USE_GNU_INFO |
1957
|
659 DEFUN_TEXT (help, args, , |
529
|
660 "help [-i] [topic ...]\n\ |
|
661 \n\ |
|
662 print cryptic yet witty messages") |
1140
|
663 #else |
1957
|
664 DEFUN_TEXT (help, args, , |
1140
|
665 "help [topic ...]\n\ |
|
666 \n\ |
|
667 print cryptic yet witty messages") |
|
668 #endif |
529
|
669 { |
2086
|
670 octave_value_list retval; |
529
|
671 |
1755
|
672 int argc = args.length () + 1; |
|
673 |
1968
|
674 string_vector argv = args.make_argv ("help"); |
1755
|
675 |
|
676 if (error_state) |
|
677 return retval; |
529
|
678 |
|
679 if (argc == 1) |
|
680 { |
|
681 simple_help (); |
|
682 } |
|
683 else |
|
684 { |
1755
|
685 if (argv[1] == "-i") |
529
|
686 { |
1755
|
687 help_from_info (argv, 2, argc); |
529
|
688 } |
|
689 else |
|
690 { |
1140
|
691 builtin_help (argc, argv); |
529
|
692 } |
|
693 } |
|
694 |
|
695 return retval; |
|
696 } |
|
697 |
1957
|
698 DEFUN_TEXT (type, args, nargout, |
581
|
699 "type NAME ...]\n\ |
|
700 \n\ |
|
701 display the definition of each NAME that refers to a function") |
|
702 { |
2086
|
703 octave_value_list retval; |
581
|
704 |
1588
|
705 begin_unwind_frame ("Ftype"); |
|
706 |
1951
|
707 unwind_protect_str (user_pref.ps4); |
|
708 user_pref.ps4 = ""; |
1588
|
709 |
1755
|
710 int argc = args.length () + 1; |
|
711 |
1968
|
712 string_vector argv = args.make_argv ("type"); |
1755
|
713 |
|
714 if (error_state) |
|
715 return retval; |
581
|
716 |
|
717 if (argc > 1) |
|
718 { |
1358
|
719 // XXX FIXME XXX -- we should really use getopt () |
|
720 |
581
|
721 int quiet = 0; |
1755
|
722 int idx = 1; |
|
723 if (argv[idx] == "-q") |
581
|
724 { |
|
725 quiet = 1; |
1755
|
726 idx++; |
581
|
727 } |
|
728 |
|
729 ostrstream output_buf; |
|
730 |
1755
|
731 for (int i = idx; i < argc; i++) |
581
|
732 { |
1755
|
733 string id = argv[i]; |
|
734 string elts; |
581
|
735 |
1755
|
736 if (id[id.length () - 1] != '.') |
1281
|
737 { |
1755
|
738 size_t pos = id.find ('.'); |
|
739 |
|
740 if (pos != NPOS) |
1288
|
741 { |
1889
|
742 elts = id.substr (pos+1); |
1755
|
743 id = id.substr (0, pos); |
1288
|
744 } |
1281
|
745 } |
|
746 |
|
747 symbol_record *sym_rec = lookup_by_name (id, 0); |
581
|
748 |
|
749 if (sym_rec) |
|
750 { |
|
751 if (sym_rec->is_user_function ()) |
|
752 { |
|
753 tree_fvc *defn = sym_rec->def (); |
|
754 |
|
755 if (nargout == 0 && ! quiet) |
1755
|
756 output_buf << argv[i] << " is a user-defined function\n"; |
581
|
757 |
2124
|
758 tree_print_code tpc (output_buf); |
|
759 |
|
760 defn->accept (tpc); |
581
|
761 } |
|
762 |
1358
|
763 // XXX FIXME XXX -- this code should be shared with |
|
764 // Fwhich. |
581
|
765 |
|
766 else if (sym_rec->is_text_function ()) |
1755
|
767 output_buf << argv[i] << " is a builtin text-function\n"; |
581
|
768 else if (sym_rec->is_builtin_function ()) |
1755
|
769 output_buf << argv[i] << " is a builtin function\n"; |
1281
|
770 else if (sym_rec->is_user_variable () |
|
771 || sym_rec->is_builtin_variable ()) |
625
|
772 { |
|
773 tree_fvc *defn = sym_rec->def (); |
|
774 |
1288
|
775 assert (defn && defn->is_constant ()); |
1281
|
776 |
2086
|
777 octave_value *tmp = (octave_value *) defn; |
625
|
778 |
1281
|
779 int var_ok = 1; |
|
780 if (tmp && tmp->is_map ()) |
|
781 { |
1755
|
782 if (! elts.empty ()) |
1281
|
783 { |
2086
|
784 octave_value ult = |
1288
|
785 tmp->lookup_map_element (elts, 0, 1); |
625
|
786 |
1281
|
787 if (! ult.is_defined ()) |
|
788 var_ok = 0; |
|
789 } |
|
790 } |
1288
|
791 |
1281
|
792 if (nargout == 0 && ! quiet) |
|
793 { |
1288
|
794 if (var_ok) |
|
795 { |
1755
|
796 output_buf << argv[i]; |
1288
|
797 if (sym_rec->is_user_variable ()) |
|
798 output_buf << " is a user-defined variable\n"; |
|
799 else |
|
800 output_buf << " is a built-in variable\n"; |
|
801 } |
1281
|
802 else |
1288
|
803 { |
1755
|
804 if (! elts.empty ()) |
1288
|
805 output_buf << "type: structure `" << id |
|
806 << "' has no member `" << elts << "'\n"; |
|
807 else |
|
808 output_buf << "type: `" << id |
|
809 << "' has unknown type!"; |
|
810 } |
1281
|
811 } |
|
812 if (! tmp->is_map ()) |
|
813 { |
2124
|
814 tree_print_code tpc (output_buf); |
|
815 |
|
816 tmp->accept (tpc); |
625
|
817 |
1281
|
818 if (nargout == 0) |
|
819 output_buf << "\n"; |
|
820 } |
625
|
821 } |
581
|
822 else |
1755
|
823 output_buf << "type: `" << argv[i] << "' has unknown type!\n"; |
581
|
824 } |
|
825 else |
1755
|
826 output_buf << "type: `" << argv[i] << "' undefined\n"; |
581
|
827 } |
|
828 |
|
829 output_buf << ends; |
|
830 |
2095
|
831 char *s = output_buf.str (); |
|
832 |
581
|
833 if (nargout == 0) |
2095
|
834 octave_stdout << s; |
581
|
835 else |
2095
|
836 retval = s; |
|
837 |
|
838 delete [] s; |
581
|
839 } |
|
840 else |
|
841 print_usage ("type"); |
|
842 |
1588
|
843 run_unwind_frame ("Ftype"); |
|
844 |
581
|
845 return retval; |
|
846 } |
|
847 |
1957
|
848 DEFUN_TEXT (which, args, nargout, |
1565
|
849 "which NAME ...\n\ |
581
|
850 \n\ |
|
851 display the type of each NAME. If NAME is defined from an function\n\ |
|
852 file, print the full name of the file.") |
|
853 { |
2086
|
854 octave_value_list retval; |
581
|
855 |
1755
|
856 int argc = args.length () + 1; |
|
857 |
1968
|
858 string_vector argv = args.make_argv ("which"); |
1755
|
859 |
|
860 if (error_state) |
|
861 return retval; |
581
|
862 |
|
863 if (argc > 1) |
|
864 { |
|
865 if (nargout > 0) |
|
866 retval.resize (argc-1, Matrix ()); |
|
867 |
1755
|
868 for (int i = 1; i < argc; i++) |
581
|
869 { |
1755
|
870 symbol_record *sym_rec = lookup_by_name (argv[i], 0); |
581
|
871 |
|
872 if (sym_rec) |
|
873 { |
742
|
874 int print = (nargout == 0); |
2095
|
875 string tmp = print_symbol_type (octave_stdout, sym_rec, |
1755
|
876 argv[i], print); |
742
|
877 if (! print) |
|
878 retval(i) = tmp; |
581
|
879 } |
|
880 else |
|
881 { |
|
882 if (nargout == 0) |
2095
|
883 octave_stdout << "which: `" << argv[i] << "' is undefined\n"; |
581
|
884 else |
|
885 retval(i) = "undefined"; |
|
886 } |
|
887 } |
|
888 } |
|
889 else |
|
890 print_usage ("which"); |
|
891 |
|
892 return retval; |
|
893 } |
|
894 |
1
|
895 /* |
|
896 ;;; Local Variables: *** |
|
897 ;;; mode: C++ *** |
|
898 ;;; End: *** |
|
899 */ |