Scilab Bag Of Tricks: The Scilab-2.5 IAQ (Infrequently Asked Questions) | ||
---|---|---|
Prev | Chapter 9. Longer Examples | Next |
cat.sci defines the function cat which prints the source of a macro (function) if it is available. The argument-, type-, and size-checking part is used in Example 4-1.
function [res] = cat(macname) // Print definition of function 'macname' // if it has been loaded via a library. // AUTHOR // Lydia van Dijk // // Copyright 1999, 2000 by Lydia van Dijk. // cat is free software distributed under the terms // of the GNU General Public License, version 2. [nl, nr] = argn(0); if nr ~= 1 then error("Call with: cat(macro_name)"); end if type(macname) ~= 10 then error("Expecting a string, got a " + typeof(macname)); end if size(macname, "*") ~= 1 then sz = size(macname); error("Expecting a scalar, got a " .. + sz(1) + "x" + sz(2) + " matrix") end [res, err] = evstr(macname); if err ~= 0 then select err case 4 then disp(macname + " is undefined."); return; case 25 then disp(macname + " is a builtin function"); return; else error("unexpected error", err); end // select err end // err ~= 0 name = whereis(macname); //disp("name = <" + name + ">"); if name == [] then disp(macname + " is defined, but its definition is unaccessible"); clear ans; return; end cont = string(evstr(name)); // path (1) and contents (2..$) of library fpath = cont(1); if part(fpath, 1:4) == "SCI/" then fpath = SCI + "/" + part(fpath, 5:length(fpath)); end fname = fpath + macname + ".sci"; [fh, err] = file("open", fname, "old"); if err ~= 0 then error("Could not open file " + fname, err); end text = read(fh, -1, 1, "(a)"); file("close", fh); write(%io(2), text);