1######################################################################## 2## 3## Copyright (C) %!-2019 The Octave Project Developers 4## 5## See the file COPYRIGHT.md in the top-level directory of this 6## distribution or <https://octave.org/copyright/>. 7## 8## This file is part of Octave. 9## 10## Octave is free software: you can redistribute it and/or modify it 11## under the terms of the GNU General Public License as published by 12## the Free Software Foundation, either version 3 of the License, or 13## (at your option) any later version. 14## 15## Octave is distributed in the hope that it will be useful, but 16## WITHOUT ANY WARRANTY; without even the implied warranty of 17## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18## GNU General Public License for more details. 19## 20## You should have received a copy of the GNU General Public License 21## along with Octave; see the file COPYING. If not, see 22## <https://www.gnu.org/licenses/>. 23## 24######################################################################## 25 26## -*- texinfo -*- 27## @deftypefn {} {} type @var{name} @dots{} 28## @deftypefnx {} {} type -q @var{name} @dots{} 29## @deftypefnx {} {text =} type ("@var{name}", @dots{}) 30## Display the contents of @var{name} which may be a file, function (m-file), 31## variable, operator, or keyword. 32## 33## @code{type} normally prepends a header line describing the category of 34## @var{name} such as function or variable; The @option{-q} option suppresses 35## this behavior. 36## 37## If no output variable is used the contents are displayed on screen. 38## Otherwise, a cell array of strings is returned, where each element 39## corresponds to the contents of each requested function. 40## @end deftypefn 41 42function text = type (varargin) 43 44 if (nargin == 0) 45 print_usage (); 46 elseif (! iscellstr (varargin)) 47 error ("type: input arguments must be strings"); 48 endif 49 50 quiet = false; 51 idx = strcmpi (varargin, "-q") | strcmpi (varargin, "-quiet"); 52 if (any (idx)) 53 quiet = true; 54 varargin(idx) = []; 55 endif 56 57 if (nargout > 0) 58 text = cell (size (varargin)); 59 endif 60 61 for n = 1:length (varargin) 62 name = varargin{n}; 63 64 ## Find function and get its code 65 txt = ""; 66 cmd = sprintf ("exist ('%s')", name); 67 e = evalin ("caller", cmd); 68 if (e == 1) 69 ## Variable 70 cmd = sprintf ("disp (%s);", name); 71 desc = evalin ("caller", cmd); 72 if (quiet) 73 txt = desc; 74 else 75 txt = sprintf ("%s is a variable\n%s", name, desc); 76 endif 77 elseif (e == 2) 78 ## m-file or ordinary file 79 file = which (name); 80 if (length (file) > 2) 81 ext = file(end-1:end); 82 endif 83 if (isempty (file) || ! strcmpi (ext, ".m")) 84 ## 'name' is an ordinary file, and not a function name. 85 file = file_in_loadpath (name); 86 quiet = true; 87 endif 88 89 ## Read the file 90 fid = fopen (file, "r"); 91 if (fid < 0) 92 error ("type: couldn't open '%s' for reading", file); 93 endif 94 contents = char (fread (fid).'); 95 fclose (fid); 96 97 if (quiet) 98 txt = contents; 99 else 100 txt = sprintf ("%s is the user-defined function defined from: %s\n\n%s", 101 name, file, contents); 102 endif 103 elseif (e == 3) 104 txt = sprintf ("%s is a dynamically-linked function", name); 105 elseif (e == 5) 106 txt = sprintf ("%s is a built-in function", name); 107 elseif (e == 103) 108 contents = __get_cmdline_fcn_txt__ (name); 109 if (isempty (contents)) 110 txt = sprintf ("%s is a command-line function with no definition", 111 name); 112 else 113 if (quiet) 114 txt = contents; 115 else 116 txt = sprintf ("%s is the command-line function:\n\n%s", 117 name, contents); 118 endif 119 endif 120 elseif (any (strcmp (__operators__ (), name))) 121 txt = sprintf ("%s is an operator", name); 122 elseif (any (strcmp (__keywords__ (), name))) 123 txt = sprintf ("%s is a keyword", name); 124 else 125 error ("type: '%s' undefined\n", name); 126 endif 127 128 if (nargout == 0) 129 disp (txt); 130 else 131 text{n} = txt; 132 endif 133 endfor 134 135endfunction 136 137 138%!test 139%! var = 1; 140%! txt = type ("var"); 141%! typestr = txt{1}(1:17); 142%! assert (typestr, "var is a variable"); 143 144%!test 145%! txt = type ("ls"); 146%! typestr = txt{1}(1:31); 147%! assert (typestr, "ls is the user-defined function"); 148 149%!test 150%! txt = type ("ls", "-q"); 151%! assert (regexp (txt{1}, '[#\s]*Copyright \(C\) 2006')); 152 153%!assert (type ("fftw"){1}, "fftw is a dynamically-linked function") 154%!assert (type ("cat"){1}, "cat is a built-in function") 155%!assert (type ("+"){1}, "+ is an operator") 156%!assert (type ("end"){1}, "end is a keyword") 157 158%!error type () 159%!error <'__NO_NAME__' undefined> type ('__NO_NAME__') 160