6863
|
1 ## Copyright (C) 2007 David Bateman |
|
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 |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
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 |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
|
19 |
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} run (@var{f}) |
|
22 ## @deftypefnx {Command} {} run @var{f} |
|
23 ## Run scripts in the current workspace that are not necessarily on the |
|
24 ## path. If @var{f} is the script to run, including its path, then @code{run} |
|
25 ## change the directory to the directory where @var{f} is found. @code{run} |
|
26 ## then executes the script, and returns to the original directory. |
|
27 ## @seealso{system} |
|
28 ## @end deftypefn |
|
29 |
6864
|
30 ## PKG_ADD: mark_as_command run |
6863
|
31 |
|
32 function run (s) |
|
33 [d, f, ext] = fileparts (s); |
|
34 if (! isempty (d)) |
|
35 if (exist (d, "dir")) |
|
36 wd = pwd (); |
|
37 unwind_protect |
|
38 cd (d); |
|
39 if (! exist (s, "file") || ! strcmp (ext, ".m")) |
|
40 error ("run: file must exist and be a valid Octave script file"); |
|
41 endif |
|
42 evalin ("caller", [f, ";"], "rethrow (lasterror ())"); |
|
43 unwind_protect_cleanup |
|
44 cd (wd); |
|
45 end_unwind_protect |
|
46 else |
|
47 error ("run: the path %s doesn't exist", d); |
|
48 endif |
|
49 else |
|
50 if (exist (script, "file")) |
|
51 evalin ("caller", [script, ";"], "rethrow (lasterror ())"); |
|
52 else |
|
53 error ("run: %s not found", s); |
|
54 endif |
|
55 endif |
|
56 endfunction |