Mercurial > octave-nkf
annotate scripts/miscellaneous/run.m @ 11723:b785c56df58f release-3-0-x
run.m: fix check for file existence
author | Ben Abbott <bpabbott@mac.com> |
---|---|
date | Mon, 31 Mar 2008 16:11:00 -0400 |
parents | 4a853a9138e7 |
children | 5dd06f19e9be 72830070a17b |
rev | line source |
---|---|
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); | |
11723
b785c56df58f
run.m: fix check for file existence
Ben Abbott <bpabbott@mac.com>
parents:
7319
diff
changeset
|
43 if (! exist (f, "file") || ! strcmp (ext, ".m")) |
6863 | 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 | |
7319 | 54 if (exist (f, "file")) |
55 evalin ("caller", [f, ";"], "rethrow (lasterror ())"); | |
6863 | 56 else |
57 error ("run: %s not found", s); | |
58 endif | |
59 endif | |
60 endfunction |