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