]> Creatis software - CreaPhase.git/blob - octave_packages/m/miscellaneous/license.m
update packages
[CreaPhase.git] / octave_packages / m / miscellaneous / license.m
1 ## Copyright (C) 2005-2012 William Poetra Yoga Hadisoeseno
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
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
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
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {} license
21 ## Display the license of Octave.
22 ##
23 ## @deftypefnx {Function File} {} license ("inuse")
24 ## Display a list of packages currently being used.
25 ##
26 ## @deftypefnx {Function File} {@var{retval} =} license ("inuse")
27 ## Return a structure containing the fields @code{feature} and @code{user}.
28 ##
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.
33 ##
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:
37 ##
38 ## @table @asis
39 ## @item "enable"
40 ## Future tests for the specified license of @var{feature} are conducted
41 ## as usual.
42 ##
43 ## @item "disable"
44 ## Future tests for the specified license of @var{feature} return 0.
45 ## @end table
46 ##
47 ## @deftypefnx {Function File} {@var{retval} =} license ("checkout", @var{feature})
48 ## Check out a license for @var{feature}, returning 1 on success and 0
49 ## on failure.
50 ##
51 ## This function is provided for compatibility with @sc{matlab}.
52 ## @seealso{ver, version}
53 ## @end deftypefn
54
55 ## Author: William Poetra Yoga Hadisoeseno <williampoetra@gmail.com>
56
57 function retval = license (varargin)
58
59   persistent __octave_licenses__;
60
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;
70     endif
71   endif
72
73   nout = nargout;
74   nin = nargin;
75   nr_licenses = rows (__octave_licenses__);
76
77   if (nout > 1 || nin > 3)
78     print_usage ();
79   endif
80
81   if (nin == 0)  
82
83     found = find (strcmp (__octave_licenses__(:,1), "Octave"), 1);
84
85     if (! isempty (found))
86       result = __octave_licenses__{found,2};
87     else
88       result = "unknown";
89     endif
90
91     if (nout == 0)
92       printf ("%s\n", result);
93     else
94       retval = result;
95     endif
96
97   elseif (nin == 1)
98
99     if (nout == 0)
100
101       if (! strcmp (varargin{1}, "inuse"))
102         usage ('license ("inuse")');
103       endif
104
105       printf ("%s\n", __octave_licenses__{:,1});
106
107     else
108
109       if (! strcmp (varargin{1}, "inuse"))
110         usage ('retval = license ("inuse")');
111       endif
112
113       pw = getpwuid (getuid ());
114       if (isstruct (pw))
115         username = pw.name;
116       else
117         username = "octave_user";
118       endif
119
120       retval = struct ("feature", __octave_licenses__(:,1), "user", username);
121
122     endif
123
124   else
125
126     feature = varargin{2}(1:(min ([(length (varargin{2})), 27])));
127
128     if (strcmp (varargin{1}, "test"))
129
130       found = find (strcmpi (__octave_licenses__(:,1), feature), 1);
131
132       if (nin == 2)
133         retval = ! isempty (found) && __octave_licenses__{found,3};
134       else
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;
140           else
141             error ("license: TOGGLE must be either `enable' or `disable'");
142           endif
143         else
144           error ("license: FEATURE `%s' not found", feature);
145         endif
146       endif
147
148     elseif (strcmp (varargin{1}, "checkout"))
149
150       if (nin != 2)
151         usage ('retval = license ("checkout", feature)');
152       endif
153
154       found = find (strcmpi (__octave_licenses__(:,1), feature), 1);
155
156       retval = ! isempty (found) && __octave_licenses__{found,3};
157
158     else
159       print_usage ();
160     endif
161
162   endif
163
164 endfunction
165
166
167 %!assert (license(), "GNU General Public License")
168 %!assert ((license ("inuse")).feature, "Octave")
169
170 %!test
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");
178 %! endif
179
180 %!assert (license ("checkout", "Octave"), true)
181
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")
187