Mercurial > octave
annotate scripts/miscellaneous/run.m @ 23220:092078913d54
maint: Merge stable to default.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 22 Feb 2017 12:58:07 -0500 |
parents | ef4d915df748 3ac9f9ecfae5 |
children | 0d5fc6e4a96c |
rev | line source |
---|---|
23219
3ac9f9ecfae5
maint: Update copyright dates.
John W. Eaton <jwe@octave.org>
parents:
23083
diff
changeset
|
1 ## Copyright (C) 2007-2017 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 | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
7 ## the Free Software Foundation; either version 3 of the License, or |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
8 ## (at 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 | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
13 ## GNU General Public License for more details. |
6863 | 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 -*- | |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20713
diff
changeset
|
20 ## @deftypefn {} {} run @var{script} |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20713
diff
changeset
|
21 ## @deftypefnx {} {} run ("@var{script}") |
17711
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 ## |
20164
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19996
diff
changeset
|
24 ## Scripts which reside in directories specified in Octave's load path, and |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19996
diff
changeset
|
25 ## which end with the extension @file{".m"}, can be run simply by typing |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19996
diff
changeset
|
26 ## their name. For scripts not located on the load path, use @code{run}. |
17711
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
27 ## |
20713
2469d78a1d8b
Consistently use 'filename' rather than 'file name' throughout code base.
Rik <rik@octave.org>
parents:
20251
diff
changeset
|
28 ## The filename @var{script} can be a bare, fully qualified, or relative |
21546
f7f97d7e9294
doc: Wrap m-file docstrings to 79 characters + newline (80 total).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
29 ## filename and with or without a file extension. If no extension is |
f7f97d7e9294
doc: Wrap m-file docstrings to 79 characters + newline (80 total).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
30 ## specified, Octave will first search for a script with the @file{".m"} |
f7f97d7e9294
doc: Wrap m-file docstrings to 79 characters + newline (80 total).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
31 ## extension before falling back to the script name without an extension. |
17711
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 |
18445
1363d909c577
run.m: Don't undo changes to current dir done by script that was run (bug #41543).
Felipe G. Nievinski <fgnievinski@gmail.com>
parents:
17744
diff
changeset
|
34 ## @code{run} first changes the working directory to the directory where |
1363d909c577
run.m: Don't undo changes to current dir done by script that was run (bug #41543).
Felipe G. Nievinski <fgnievinski@gmail.com>
parents:
17744
diff
changeset
|
35 ## @var{script} is found. Next, the script is executed. Finally, @code{run} |
1363d909c577
run.m: Don't undo changes to current dir done by script that was run (bug #41543).
Felipe G. Nievinski <fgnievinski@gmail.com>
parents:
17744
diff
changeset
|
36 ## returns to the original working directory unless @code{script} has |
1363d909c577
run.m: Don't undo changes to current dir done by script that was run (bug #41543).
Felipe G. Nievinski <fgnievinski@gmail.com>
parents:
17744
diff
changeset
|
37 ## specifically changed directories. |
17711
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
38 ## @seealso{path, addpath, source} |
6863 | 39 ## @end deftypefn |
40 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11395
diff
changeset
|
41 function run (script) |
7125 | 42 |
43 if (nargin != 1) | |
44 print_usage (); | |
45 endif | |
46 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11395
diff
changeset
|
47 [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
|
48 if (! strcmp (ext, ".m")) |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
49 ## 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
|
50 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
|
51 f = [f ext]; |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
52 ext = ".m"; |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
53 script = [script ".m"]; |
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 endif |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
56 |
17711
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
57 if (! exist (script, "file")) |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
58 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
|
59 endif |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
60 |
6863 | 61 if (! isempty (d)) |
22033
98ee8b1ebbeb
run.m: Return to original directory after executing script (bug #48351).
Rik <rik@octave.org>
parents:
21758
diff
changeset
|
62 if (! exist (d, "dir")) |
6863 | 63 error ("run: the path %s doesn't exist", d); |
64 endif | |
22033
98ee8b1ebbeb
run.m: Return to original directory after executing script (bug #48351).
Rik <rik@octave.org>
parents:
21758
diff
changeset
|
65 |
98ee8b1ebbeb
run.m: Return to original directory after executing script (bug #48351).
Rik <rik@octave.org>
parents:
21758
diff
changeset
|
66 startdir = pwd (); |
98ee8b1ebbeb
run.m: Return to original directory after executing script (bug #48351).
Rik <rik@octave.org>
parents:
21758
diff
changeset
|
67 scriptdir = ""; |
98ee8b1ebbeb
run.m: Return to original directory after executing script (bug #48351).
Rik <rik@octave.org>
parents:
21758
diff
changeset
|
68 unwind_protect |
98ee8b1ebbeb
run.m: Return to original directory after executing script (bug #48351).
Rik <rik@octave.org>
parents:
21758
diff
changeset
|
69 cd (d); |
98ee8b1ebbeb
run.m: Return to original directory after executing script (bug #48351).
Rik <rik@octave.org>
parents:
21758
diff
changeset
|
70 scriptdir = pwd (); |
98ee8b1ebbeb
run.m: Return to original directory after executing script (bug #48351).
Rik <rik@octave.org>
parents:
21758
diff
changeset
|
71 evalin ("caller", sprintf ("source ('%s%s');", f, ext), |
98ee8b1ebbeb
run.m: Return to original directory after executing script (bug #48351).
Rik <rik@octave.org>
parents:
21758
diff
changeset
|
72 "rethrow (lasterror ())"); |
98ee8b1ebbeb
run.m: Return to original directory after executing script (bug #48351).
Rik <rik@octave.org>
parents:
21758
diff
changeset
|
73 unwind_protect_cleanup |
98ee8b1ebbeb
run.m: Return to original directory after executing script (bug #48351).
Rik <rik@octave.org>
parents:
21758
diff
changeset
|
74 if (strcmp (scriptdir, pwd ())) |
98ee8b1ebbeb
run.m: Return to original directory after executing script (bug #48351).
Rik <rik@octave.org>
parents:
21758
diff
changeset
|
75 cd (startdir); |
98ee8b1ebbeb
run.m: Return to original directory after executing script (bug #48351).
Rik <rik@octave.org>
parents:
21758
diff
changeset
|
76 endif |
98ee8b1ebbeb
run.m: Return to original directory after executing script (bug #48351).
Rik <rik@octave.org>
parents:
21758
diff
changeset
|
77 end_unwind_protect |
98ee8b1ebbeb
run.m: Return to original directory after executing script (bug #48351).
Rik <rik@octave.org>
parents:
21758
diff
changeset
|
78 |
6863 | 79 else |
17711
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
80 if (! isempty (ext)) |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
81 script = which (script); |
6863 | 82 else |
17711
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
83 ## 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
|
84 script = which ([script "."]); |
6863 | 85 endif |
19996
10392528396c
run.m: Use single quotes so that '\' isn't an escape sequence (bug #44671).
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
86 evalin ("caller", sprintf ("source ('%s');", script), |
17711
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
87 "rethrow (lasterror ())"); |
6863 | 88 endif |
21758
ffad2baa90f7
maint: Use newlines to make code more readable.
Rik <rik@octave.org>
parents:
21546
diff
changeset
|
89 |
6863 | 90 endfunction |
17338
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
91 |
17711
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
92 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
93 ## Test input validation |
17711
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
94 %!error run () |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
95 %!error run ("a", "b") |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
96 %!error <SCRIPT must exist> run ("__A_very_#unlikely#_file_name__") |