Mercurial > octave-antonio
annotate 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 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
1 ## Copyright (C) 2007-2012 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 -*- | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11395
diff
changeset
|
20 ## @deftypefn {Command} {} run @var{script} |
17711
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
21 ## @deftypefnx {Function File} {} run ("@var{script}") |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
22 ## Run @var{script} in the current workspace. |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
23 ## |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
24 ## Scripts which reside in directories specified in Octave's load |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
25 ## path, and which end with the extension @file{".m"}, can be run simply by |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
26 ## typing their name. For scripts not located on the load path, use @code{run}. |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
27 ## |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
28 ## The file name @var{script} can be a bare, fully qualified, or relative |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
29 ## filename and with or without a file extension. If no extension is specified, |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
30 ## Octave will first search for a script with the @file{".m"} extension before |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
31 ## falling back to the script name without an extension. |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
32 ## |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
33 ## Implementation Note: If @var{script} includes a path component, then |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
34 ## @code{run} first changes the directory to the directory where @var{script} |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
35 ## is found. @code{run} then executes the script, and returns to the original |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
36 ## directory. |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
37 ## @seealso{path, addpath, source} |
6863 | 38 ## @end deftypefn |
39 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11395
diff
changeset
|
40 function run (script) |
7125 | 41 |
42 if (nargin != 1) | |
43 print_usage (); | |
44 endif | |
45 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11395
diff
changeset
|
46 [d, f, ext] = fileparts (script); |
17711
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
47 if (! strcmp (ext, ".m")) |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
48 ## prefer files with .m extension for compatibility with Matlab |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
49 if (exist ([script ".m"], "file")) |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
50 f = [f ext]; |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
51 ext = ".m"; |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
52 script = [script ".m"]; |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
53 endif |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
54 endif |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
55 |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
56 if (! exist (script, "file")) |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
57 error ("run: file SCRIPT must exist and be a valid Octave scriptfile"); |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
58 endif |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
59 |
6863 | 60 if (! isempty (d)) |
61 if (exist (d, "dir")) | |
62 wd = pwd (); | |
63 unwind_protect | |
10549 | 64 cd (d); |
17711
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
65 evalin ("caller", sprintf ('source ("%s%s");', f, ext), |
11395
0754a4e271f4
run.m: use source to execute script
John W. Eaton <jwe@octave.org>
parents:
10549
diff
changeset
|
66 "rethrow (lasterror ())"); |
6863 | 67 unwind_protect_cleanup |
10549 | 68 cd (wd); |
6863 | 69 end_unwind_protect |
70 else | |
71 error ("run: the path %s doesn't exist", d); | |
72 endif | |
73 else | |
17711
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
74 if (! isempty (ext)) |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
75 script = which (script); |
6863 | 76 else |
17711
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
77 ## Search PATH with null extension ('.' will be stripped and ext = "") |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
78 script = which ([script "."]); |
6863 | 79 endif |
17711
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
80 evalin ("caller", sprintf ('source ("%s");', script), |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
81 "rethrow (lasterror ())"); |
6863 | 82 endif |
83 endfunction | |
17338
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
84 |
17711
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
85 |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
86 %% Test input validation |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
87 %!error run () |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
88 %!error run ("a", "b") |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
89 %!error <SCRIPT must exist> run ("__A_very_#unlikely#_file_name__") |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
90 |