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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
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
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
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
38 ## @end deftypefn
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
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
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
41
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
42 if (nargin != 1)
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
43 print_usage ();
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
44 endif
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
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
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
60 if (! isempty (d))
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
61 if (exist (d, "dir"))
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
62 wd = pwd ();
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
63 unwind_protect
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9724
diff changeset
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
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
67 unwind_protect_cleanup
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9724
diff changeset
68 cd (wd);
6863
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
69 end_unwind_protect
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
70 else
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
71 error ("run: the path %s doesn't exist", d);
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
72 endif
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
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
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
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
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
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
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
82 endif
3c64128e621c [project @ 2007-09-05 07:52:48 by dbateman]
dbateman
parents:
diff changeset
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