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