7017
|
1 ## Copyright (C) 2007 David Bateman |
6863
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
6863
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
6863
|
18 |
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {} run (@var{f}) |
|
21 ## @deftypefnx {Command} {} run @var{f} |
|
22 ## Run scripts in the current workspace that are not necessarily on the |
|
23 ## path. If @var{f} is the script to run, including its path, then @code{run} |
|
24 ## change the directory to the directory where @var{f} is found. @code{run} |
|
25 ## then executes the script, and returns to the original directory. |
|
26 ## @seealso{system} |
|
27 ## @end deftypefn |
|
28 |
6864
|
29 ## PKG_ADD: mark_as_command run |
6863
|
30 |
|
31 function run (s) |
7125
|
32 |
|
33 if (nargin != 1) |
|
34 print_usage (); |
|
35 endif |
|
36 |
6863
|
37 [d, f, ext] = fileparts (s); |
|
38 if (! isempty (d)) |
|
39 if (exist (d, "dir")) |
|
40 wd = pwd (); |
|
41 unwind_protect |
|
42 cd (d); |
|
43 if (! exist (s, "file") || ! strcmp (ext, ".m")) |
|
44 error ("run: file must exist and be a valid Octave script file"); |
|
45 endif |
|
46 evalin ("caller", [f, ";"], "rethrow (lasterror ())"); |
|
47 unwind_protect_cleanup |
|
48 cd (wd); |
|
49 end_unwind_protect |
|
50 else |
|
51 error ("run: the path %s doesn't exist", d); |
|
52 endif |
|
53 else |
|
54 if (exist (script, "file")) |
|
55 evalin ("caller", [script, ";"], "rethrow (lasterror ())"); |
|
56 else |
|
57 error ("run: %s not found", s); |
|
58 endif |
|
59 endif |
|
60 endfunction |