comparison scripts/miscellaneous/run.m @ 17711:9ccb48d39ce9

run.m: append .m extension when none was provided. Also, look for file in path if it is not in pwd. * scripts/miscellaneous/run.m: Redo docstring. Test early for missing '.m' extension and use exist to search load path. Use which to check for script in load path.
author Pantxo Diribarne <pantxo.diribarne@gmail.com>
date Sun, 20 Oct 2013 22:23:31 +0200
parents 1c89599167a6
children d63878346099
comparison
equal deleted inserted replaced
17710:3dec0a57ab55 17711:9ccb48d39ce9
16 ## along with Octave; see the file COPYING. If not, see 16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>. 17 ## <http://www.gnu.org/licenses/>.
18 18
19 ## -*- texinfo -*- 19 ## -*- texinfo -*-
20 ## @deftypefn {Command} {} run @var{script} 20 ## @deftypefn {Command} {} run @var{script}
21 ## @deftypefnx {Function File} {} run (@var{script}) 21 ## @deftypefnx {Function File} {} run ("@var{script}")
22 ## Run scripts in the current workspace that are not necessarily on the 22 ## Run @var{script} in the current workspace.
23 ## path. If @var{script} is the script to run, including its path, then 23 ##
24 ## @code{run} changes the directory to the directory where @var{script} is 24 ## Scripts which reside in directories specified in Octave's load
25 ## found. @code{run} then executes the script, and returns to the original 25 ## path, and which end with the extension @file{".m"}, can be run simply by
26 ## directory. 26 ## typing their name. For scripts not located on the load path, use @code{run}.
27 ## @seealso{system} 27 ##
28 ## The file name @var{script} can be a bare, fully qualified, or relative
29 ## filename and with or without a file extension. If no extension is specified,
30 ## Octave will first search for a script with the @file{".m"} extension before
31 ## falling back to the script name without an extension.
32 ##
33 ## Implementation Note: If @var{script} includes a path component, then
34 ## @code{run} first changes the directory to the directory where @var{script}
35 ## is found. @code{run} then executes the script, and returns to the original
36 ## directory.
37 ## @seealso{path, addpath, source}
28 ## @end deftypefn 38 ## @end deftypefn
29 39
30 function run (script) 40 function run (script)
31 41
32 if (nargin != 1) 42 if (nargin != 1)
33 print_usage (); 43 print_usage ();
34 endif 44 endif
35 45
36 [d, f, ext] = fileparts (script); 46 [d, f, ext] = fileparts (script);
47 if (! strcmp (ext, ".m"))
48 ## prefer files with .m extension for compatibility with Matlab
49 if (exist ([script ".m"], "file"))
50 f = [f ext];
51 ext = ".m";
52 script = [script ".m"];
53 endif
54 endif
55
56 if (! exist (script, "file"))
57 error ("run: file SCRIPT must exist and be a valid Octave scriptfile");
58 endif
59
37 if (! isempty (d)) 60 if (! isempty (d))
38 if (exist (d, "dir")) 61 if (exist (d, "dir"))
39 wd = pwd (); 62 wd = pwd ();
40 unwind_protect 63 unwind_protect
41 cd (d); 64 cd (d);
42 if (! exist ([f ext], "file")) 65 evalin ("caller", sprintf ('source ("%s%s");', f, ext),
43 error ("run: file SCRIPT must exist and be a valid Octave scriptfile");
44 endif
45 evalin ("caller", sprintf ("source (\"%s%s\");", f, ext),
46 "rethrow (lasterror ())"); 66 "rethrow (lasterror ())");
47 unwind_protect_cleanup 67 unwind_protect_cleanup
48 cd (wd); 68 cd (wd);
49 end_unwind_protect 69 end_unwind_protect
50 else 70 else
51 error ("run: the path %s doesn't exist", d); 71 error ("run: the path %s doesn't exist", d);
52 endif 72 endif
53 else 73 else
54 if (exist (script, "file")) 74 if (! isempty (ext))
55 evalin ("caller", sprintf ("source (\"%s\");", script), 75 script = which (script);
56 "rethrow (lasterror ())");
57 else 76 else
58 error ("run: %s not found", script); 77 ## Search PATH with null extension ('.' will be stripped and ext = "")
78 script = which ([script "."]);
59 endif 79 endif
80 evalin ("caller", sprintf ('source ("%s");', script),
81 "rethrow (lasterror ())");
60 endif 82 endif
61 endfunction 83 endfunction
62 84
85
86 %% Test input validation
87 %!error run ()
88 %!error run ("a", "b")
89 %!error <SCRIPT must exist> run ("__A_very_#unlikely#_file_name__")
90