annotate scripts/miscellaneous/run.m @ 19596:0e1f5a750d00

maint: Periodic merge of gui-release to default.
author John W. Eaton <jwe@octave.org>
date Tue, 20 Jan 2015 10:24:46 -0500
parents a834b3151c7e 446c46af4b42
children 4197fc428c7d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17744
d63878346099 maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents: 17711
diff changeset
1 ## Copyright (C) 2007-2013 David Bateman
6863
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
2 ##
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
3 ## This file is part of Octave.
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
4 ##
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6864
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6864
diff changeset
8 ## your option) any later version.
6863
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
9 ##
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
13 ## General Public License for more details.
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
14 ##
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6864
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6864
diff changeset
17 ## <http://www.gnu.org/licenses/>.
6863
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
18
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
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
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
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
39 ## @end deftypefn
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
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
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
42
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
43 if (nargin != 1)
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
44 print_usage ();
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
45 endif
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
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
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
61 if (! isempty (d))
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
62 if (exist (d, "dir"))
19282
a834b3151c7e run.m: Return to the intial directory, unless the pwd is changed (Bug #43426).
Andy Weber <andreas.weber@hs-offenburg.de>
parents: 18445
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: 18445
diff changeset
64 d = canonicalize_file_name (d);
6863
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
65 unwind_protect
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9724
diff changeset
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
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
69 unwind_protect_cleanup
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
70 if (strcmp (d, pwd ()))
19282
a834b3151c7e run.m: Return to the intial directory, unless the pwd is changed (Bug #43426).
Andy Weber <andreas.weber@hs-offenburg.de>
parents: 18445
diff changeset
71 cd (startdir);
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
72 endif
6863
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
73 end_unwind_protect
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
74 else
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
75 error ("run: the path %s doesn't exist", d);
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
76 endif
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
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
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
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
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
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
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
86 endif
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
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