1 ## Copyright (C) 2005-2012 William Poetra Yoga Hadisoeseno
3 ## This file is part of Octave.
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
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
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.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {} license
21 ## Display the license of Octave.
23 ## @deftypefnx {Function File} {} license ("inuse")
24 ## Display a list of packages currently being used.
26 ## @deftypefnx {Function File} {@var{retval} =} license ("inuse")
27 ## Return a structure containing the fields @code{feature} and @code{user}.
29 ## @deftypefnx {Function File} {@var{retval} =} license ("test", @var{feature})
30 ## Return 1 if a license exists for the product identified by the string
31 ## @var{feature} and 0 otherwise. The argument @var{feature} is case
32 ## insensitive and only the first 27 characters are checked.
34 ## @deftypefnx {Function File} {} license ("test", @var{feature}, @var{toggle})
35 ## Enable or disable license testing for @var{feature}, depending on
36 ## @var{toggle}, which may be one of:
40 ## Future tests for the specified license of @var{feature} are conducted
44 ## Future tests for the specified license of @var{feature} return 0.
47 ## @deftypefnx {Function File} {@var{retval} =} license ("checkout", @var{feature})
48 ## Check out a license for @var{feature}, returning 1 on success and 0
51 ## This function is provided for compatibility with @sc{matlab}.
52 ## @seealso{ver, version}
55 ## Author: William Poetra Yoga Hadisoeseno <williampoetra@gmail.com>
57 function retval = license (varargin)
59 persistent __octave_licenses__;
61 if (isempty (__octave_licenses__))
62 __octave_licenses__ = cell ();
63 __octave_licenses__{1,1} = "Octave";
64 __octave_licenses__{1,2} = "GNU General Public License";
65 __octave_licenses__{1,3} = true;
66 if (exist ("OCTAVE_FORGE_VERSION"))
67 __octave_licenses__{2,1} = "octave-forge";
68 __octave_licenses__{2,2} = "<various licenses>";
69 __octave_licenses__{2,3} = true;
75 nr_licenses = rows (__octave_licenses__);
77 if (nout > 1 || nin > 3)
83 found = find (strcmp (__octave_licenses__(:,1), "Octave"), 1);
85 if (! isempty (found))
86 result = __octave_licenses__{found,2};
92 printf ("%s\n", result);
101 if (! strcmp (varargin{1}, "inuse"))
102 usage ('license ("inuse")');
105 printf ("%s\n", __octave_licenses__{:,1});
109 if (! strcmp (varargin{1}, "inuse"))
110 usage ('retval = license ("inuse")');
113 pw = getpwuid (getuid ());
117 username = "octave_user";
120 retval = struct ("feature", __octave_licenses__(:,1), "user", username);
126 feature = varargin{2}(1:(min ([(length (varargin{2})), 27])));
128 if (strcmp (varargin{1}, "test"))
130 found = find (strcmpi (__octave_licenses__(:,1), feature), 1);
133 retval = ! isempty (found) && __octave_licenses__{found,3};
135 if (! isempty (found))
136 if (strcmp (varargin{3}, "enable"))
137 __octave_licenses__{found,3} = true;
138 elseif (strcmp (varargin{3}, "disable"))
139 __octave_licenses__{found,3} = false;
141 error ("license: TOGGLE must be either `enable' or `disable'");
144 error ("license: FEATURE `%s' not found", feature);
148 elseif (strcmp (varargin{1}, "checkout"))
151 usage ('retval = license ("checkout", feature)');
154 found = find (strcmpi (__octave_licenses__(:,1), feature), 1);
156 retval = ! isempty (found) && __octave_licenses__{found,3};
167 %!assert (license(), "GNU General Public License")
168 %!assert ((license ("inuse")).feature, "Octave")
171 %! lstate = license ("test", "Octave");
172 %! license ("test", "Octave", "disable");
173 %! assert (license ("test", "Octave"), false);
174 %! license ("test", "Octave", "enable");
175 %! assert (license ("test", "Octave"), true);
176 %! if (lstate == false)
177 %! license ("test", "Octave", "disable");
180 %!assert (license ("checkout", "Octave"), true)
182 %% Test input validation
183 %!error license ("not_inuse")
184 %!error <TOGGLE must be either> license ("test", "Octave", "not_enable")
185 %!error <FEATURE `INVALID' not found> license ("test", "INVALID", "enable")
186 %!error license ("not_test", "Octave", "enable")