Mercurial > hg > octave-nkf
annotate scripts/miscellaneous/run.m @ 19898:4197fc428c7d
maint: Update copyright notices for 2015.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 11 Feb 2015 14:19:08 -0500 |
parents | 0e1f5a750d00 |
children | 9fc020886ae9 |
rev | line source |
---|---|
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19793
diff
changeset
|
1 ## Copyright (C) 2007-2015 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 |
18513
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 |
19790
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 |
19790
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
60 |
6863 | 61 if (! isempty (d)) |
62 if (exist (d, "dir")) | |
19474
a834b3151c7e
run.m: Return to the intial directory, unless the pwd is changed (Bug #43426).
Andy Weber <andreas.weber@hs-offenburg.de>
parents:
18513
diff
changeset
|
63 startdir = pwd (); |
a834b3151c7e
run.m: Return to the intial directory, unless the pwd is changed (Bug #43426).
Andy Weber <andreas.weber@hs-offenburg.de>
parents:
18513
diff
changeset
|
64 d = canonicalize_file_name (d); |
6863 | 65 unwind_protect |
10549 | 66 cd (d); |
17711
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
67 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
|
68 "rethrow (lasterror ())"); |
6863 | 69 unwind_protect_cleanup |
18513
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
|
70 if (strcmp (d, pwd ())) |
19474
a834b3151c7e
run.m: Return to the intial directory, unless the pwd is changed (Bug #43426).
Andy Weber <andreas.weber@hs-offenburg.de>
parents:
18513
diff
changeset
|
71 cd (startdir); |
18513
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
|
72 endif |
6863 | 73 end_unwind_protect |
74 else | |
75 error ("run: the path %s doesn't exist", d); | |
76 endif | |
77 else | |
17711
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
78 if (! isempty (ext)) |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
79 script = which (script); |
6863 | 80 else |
17711
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
81 ## 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
|
82 script = which ([script "."]); |
6863 | 83 endif |
17711
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
84 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
|
85 "rethrow (lasterror ())"); |
6863 | 86 endif |
87 endfunction | |
17338
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
88 |
17711
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
89 |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
90 %% Test input validation |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
91 %!error run () |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
92 %!error run ("a", "b") |
9ccb48d39ce9
run.m: append .m extension when none was provided.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
17338
diff
changeset
|
93 %!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
|
94 |