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