2974
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
7016
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
2974
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
7016
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
2974
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
5765
|
27 #include <sstream> |
3503
|
28 #include <iostream> |
3014
|
29 #include <string> |
|
30 |
3606
|
31 #include "defun.h" |
3325
|
32 #include "dynamic-ld.h" |
3014
|
33 #include "error.h" |
|
34 #include "help.h" |
2974
|
35 #include "ov.h" |
|
36 #include "ov-builtin.h" |
3325
|
37 #include "ov-dld-fcn.h" |
5823
|
38 #include "ov-fcn.h" |
2974
|
39 #include "ov-mapper.h" |
5864
|
40 #include "ov-mex-fcn.h" |
5823
|
41 #include "ov-usr-fcn.h" |
3606
|
42 #include "oct-obj.h" |
3014
|
43 #include "pager.h" |
2974
|
44 #include "symtab.h" |
5823
|
45 #include "toplev.h" |
2974
|
46 #include "variables.h" |
|
47 |
5823
|
48 // Print the usage part of the doc string of FCN (user-defined or DEFUN). |
5399
|
49 |
5823
|
50 static void |
|
51 print_usage (octave_function *fcn) |
3014
|
52 { |
5823
|
53 if (fcn) |
3014
|
54 { |
5823
|
55 std::string nm = fcn->name (); |
3014
|
56 |
5823
|
57 std::string doc = fcn->doc_string (); |
|
58 |
|
59 if (doc.length () > 0) |
3014
|
60 { |
5765
|
61 std::ostringstream buf; |
4732
|
62 |
5750
|
63 buf << "\nInvalid call to " << nm << ". Correct usage is:\n\n"; |
3330
|
64 |
5823
|
65 display_usage_text (buf, doc); |
3330
|
66 |
5823
|
67 buf << "\n"; |
3014
|
68 |
5823
|
69 additional_help_message (buf); |
4732
|
70 |
5765
|
71 defun_usage_message (buf.str ()); |
3014
|
72 } |
5823
|
73 else |
|
74 error ("no usage message found for `%s'", nm.c_str ()); |
3014
|
75 } |
|
76 else |
5823
|
77 error ("print_usage: invalid function"); |
|
78 } |
|
79 |
|
80 // Print the usage part of the doc string of the current function |
|
81 // (user-defined or DEFUN). |
|
82 |
|
83 void |
|
84 print_usage (void) |
|
85 { |
|
86 print_usage (octave_call_stack::current ()); |
|
87 } |
|
88 |
|
89 // Deprecated. |
|
90 void |
|
91 print_usage (const std::string&) |
|
92 { |
|
93 print_usage (); |
3014
|
94 } |
|
95 |
5800
|
96 DEFUN (print_usage, args, , |
|
97 "-*- texinfo -*-\n\ |
5823
|
98 @deftypefn {Loadable Function} {} print_usage ()\n\ |
|
99 Print the usage message for the currently executing function. The\n\ |
|
100 @code{print_usage} function is only intended to work inside a\n\ |
|
101 user-defined function.\n\ |
5800
|
102 @seealso{help}\n\ |
|
103 @end deftypefn") |
|
104 { |
|
105 octave_value retval; |
|
106 |
5823
|
107 if (args.length () == 0) |
|
108 print_usage (octave_call_stack::caller_user_function ()); |
5800
|
109 else |
5823
|
110 print_usage (); |
5800
|
111 |
|
112 return retval; |
|
113 } |
|
114 |
3015
|
115 void |
3523
|
116 check_version (const std::string& version, const std::string& fcn) |
3015
|
117 { |
4448
|
118 if (version != OCTAVE_API_VERSION) |
3986
|
119 { |
4448
|
120 error ("API version %s found in .oct file function `%s'\n" |
|
121 " does not match the running Octave (API version %s)\n" |
|
122 " this can lead to incorrect results or other failures\n" |
|
123 " you can fix this problem by recompiling this .oct file", |
|
124 version.c_str (), fcn.c_str (), OCTAVE_API_VERSION); |
3986
|
125 } |
3015
|
126 } |
|
127 |
2974
|
128 // Install variables and functions in the symbol tables. |
|
129 |
|
130 void |
3145
|
131 install_builtin_mapper (octave_mapper *mf) |
2974
|
132 { |
4009
|
133 symbol_record *sym_rec = fbi_sym_tab->lookup (mf->name (), true); |
2974
|
134 |
|
135 unsigned int t |
3010
|
136 = symbol_record::BUILTIN_FUNCTION | symbol_record::MAPPER_FUNCTION; |
2974
|
137 |
|
138 sym_rec->unprotect (); |
|
139 sym_rec->define (mf, t); |
|
140 sym_rec->document (mf->doc_string ()); |
|
141 sym_rec->make_eternal (); |
|
142 sym_rec->protect (); |
|
143 } |
|
144 |
|
145 void |
3523
|
146 install_builtin_function (octave_builtin::fcn f, const std::string& name, |
4234
|
147 const std::string& doc, bool is_text_fcn, |
|
148 bool /* can_hide_function -- not yet implemented */) |
2974
|
149 { |
4009
|
150 symbol_record *sym_rec = fbi_sym_tab->lookup (name, true); |
2974
|
151 |
3010
|
152 unsigned int t = symbol_record::BUILTIN_FUNCTION; |
2974
|
153 |
|
154 if (is_text_fcn) |
4208
|
155 t |= symbol_record::COMMAND; |
2974
|
156 |
|
157 sym_rec->unprotect (); |
|
158 sym_rec->define (new octave_builtin (f, name, doc), t); |
|
159 sym_rec->document (doc); |
|
160 sym_rec->make_eternal (); |
|
161 sym_rec->protect (); |
|
162 } |
|
163 |
3258
|
164 void |
3523
|
165 install_dld_function (octave_dld_function::fcn f, const std::string& name, |
3325
|
166 const octave_shlib& shl, |
6323
|
167 const std::string& doc, bool is_text_fcn, |
|
168 bool relative) |
3325
|
169 { |
4009
|
170 symbol_record *sym_rec = fbi_sym_tab->lookup (name, true); |
3325
|
171 |
|
172 unsigned int t = symbol_record::DLD_FUNCTION; |
|
173 |
|
174 if (is_text_fcn) |
4208
|
175 t |= symbol_record::COMMAND; |
3325
|
176 |
|
177 sym_rec->unprotect (); |
6323
|
178 |
|
179 octave_dld_function *df = new octave_dld_function (f, shl, name, doc); |
|
180 |
|
181 if (relative) |
|
182 df->mark_relative (); |
|
183 |
|
184 sym_rec->define (df, t); |
3325
|
185 sym_rec->document (doc); |
5397
|
186 |
|
187 // Also insert the full name in the symbol table. This way, we can |
5814
|
188 // properly cope with changes to LOAD_PATH. |
5397
|
189 |
|
190 symbol_record *full_sr = fbi_sym_tab->lookup (shl.file_name (), true); |
|
191 |
|
192 full_sr->alias (sym_rec, true); |
|
193 full_sr->hide (); |
3325
|
194 } |
|
195 |
|
196 void |
5864
|
197 install_mex_function (void *fptr, bool fmex, const std::string& name, |
6323
|
198 const octave_shlib& shl, bool is_text_fcn, |
|
199 bool relative) |
5864
|
200 { |
|
201 symbol_record *sym_rec = fbi_sym_tab->lookup (name, true); |
|
202 |
|
203 unsigned int t = symbol_record::MEX_FUNCTION; |
|
204 |
|
205 if (is_text_fcn) |
|
206 t |= symbol_record::COMMAND; |
|
207 |
|
208 sym_rec->unprotect (); |
6323
|
209 |
|
210 octave_mex_function *mf = new octave_mex_function (fptr, fmex, shl, name); |
|
211 |
|
212 if (relative) |
|
213 mf->mark_relative (); |
|
214 |
|
215 sym_rec->define (mf, t); |
5864
|
216 |
|
217 // Also insert the full name in the symbol table. This way, we can |
|
218 // properly cope with changes to LOAD_PATH. |
|
219 |
|
220 symbol_record *full_sr = fbi_sym_tab->lookup (shl.file_name (), true); |
|
221 |
|
222 full_sr->alias (sym_rec, true); |
|
223 full_sr->hide (); |
|
224 } |
|
225 |
|
226 void |
3523
|
227 alias_builtin (const std::string& alias, const std::string& name) |
2974
|
228 { |
4009
|
229 symbol_record *sr_name = fbi_sym_tab->lookup (name); |
2974
|
230 |
|
231 if (! sr_name) |
|
232 panic ("can't alias to undefined name!"); |
|
233 |
4009
|
234 symbol_record *sr_alias = fbi_sym_tab->lookup (alias, true); |
2974
|
235 |
|
236 if (sr_alias) |
|
237 sr_alias->alias (sr_name); |
|
238 else |
|
239 panic ("can't find symbol record for builtin function `%s'", |
|
240 alias.c_str ()); |
|
241 } |
|
242 |
3606
|
243 #if 0 |
|
244 // This is insufficient to really make it possible to define an alias |
|
245 // for function. There are a number of subtle problems related to |
|
246 // automatically reloading functions. |
|
247 DEFUN (alias, args, , |
|
248 "alias (alias, name)") |
|
249 { |
|
250 octave_value retval; |
|
251 |
|
252 int nargin = args.length (); |
|
253 |
|
254 if (nargin == 2) |
|
255 { |
|
256 string alias = args(0).string_value (); |
|
257 string name = args(1).string_value (); |
|
258 |
|
259 if (! error_state) |
|
260 { |
|
261 symbol_record *sr_name = lookup_by_name (name, false); |
|
262 |
|
263 if (sr_name && sr_name->is_function ()) |
|
264 { |
4009
|
265 symbol_record *sr_alias = fbi_sym_tab->lookup (alias, true); |
3606
|
266 |
|
267 if (sr_alias) |
|
268 sr_alias->alias (sr_name); |
|
269 else |
|
270 error ("alias: unable to insert `%s' in symbol table", |
|
271 alias.c_str ()); |
|
272 } |
|
273 else |
|
274 error ("alias: function `%s' does not exist", name.c_str ()); |
|
275 } |
|
276 } |
|
277 else |
5823
|
278 print_usage (); |
3606
|
279 |
|
280 return retval; |
|
281 } |
|
282 #endif |
|
283 |
2974
|
284 /* |
|
285 ;;; Local Variables: *** |
|
286 ;;; mode: C++ *** |
|
287 ;;; End: *** |
|
288 */ |