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" |
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, |
3523
|
167 const std::string& doc, bool is_text_fcn) |
3325
|
168 { |
4009
|
169 symbol_record *sym_rec = fbi_sym_tab->lookup (name, true); |
3325
|
170 |
|
171 unsigned int t = symbol_record::DLD_FUNCTION; |
|
172 |
|
173 if (is_text_fcn) |
4208
|
174 t |= symbol_record::COMMAND; |
3325
|
175 |
|
176 sym_rec->unprotect (); |
|
177 sym_rec->define (new octave_dld_function (f, shl, name, doc), t); |
|
178 sym_rec->document (doc); |
5397
|
179 |
|
180 // Also insert the full name in the symbol table. This way, we can |
5814
|
181 // properly cope with changes to LOAD_PATH. |
5397
|
182 |
|
183 symbol_record *full_sr = fbi_sym_tab->lookup (shl.file_name (), true); |
|
184 |
|
185 full_sr->alias (sym_rec, true); |
|
186 full_sr->hide (); |
3325
|
187 } |
|
188 |
|
189 void |
3523
|
190 alias_builtin (const std::string& alias, const std::string& name) |
2974
|
191 { |
4009
|
192 symbol_record *sr_name = fbi_sym_tab->lookup (name); |
2974
|
193 |
|
194 if (! sr_name) |
|
195 panic ("can't alias to undefined name!"); |
|
196 |
4009
|
197 symbol_record *sr_alias = fbi_sym_tab->lookup (alias, true); |
2974
|
198 |
|
199 if (sr_alias) |
|
200 sr_alias->alias (sr_name); |
|
201 else |
|
202 panic ("can't find symbol record for builtin function `%s'", |
|
203 alias.c_str ()); |
|
204 } |
|
205 |
3606
|
206 #if 0 |
|
207 // This is insufficient to really make it possible to define an alias |
|
208 // for function. There are a number of subtle problems related to |
|
209 // automatically reloading functions. |
|
210 DEFUN (alias, args, , |
|
211 "alias (alias, name)") |
|
212 { |
|
213 octave_value retval; |
|
214 |
|
215 int nargin = args.length (); |
|
216 |
|
217 if (nargin == 2) |
|
218 { |
|
219 string alias = args(0).string_value (); |
|
220 string name = args(1).string_value (); |
|
221 |
|
222 if (! error_state) |
|
223 { |
|
224 symbol_record *sr_name = lookup_by_name (name, false); |
|
225 |
|
226 if (sr_name && sr_name->is_function ()) |
|
227 { |
4009
|
228 symbol_record *sr_alias = fbi_sym_tab->lookup (alias, true); |
3606
|
229 |
|
230 if (sr_alias) |
|
231 sr_alias->alias (sr_name); |
|
232 else |
|
233 error ("alias: unable to insert `%s' in symbol table", |
|
234 alias.c_str ()); |
|
235 } |
|
236 else |
|
237 error ("alias: function `%s' does not exist", name.c_str ()); |
|
238 } |
|
239 } |
|
240 else |
5823
|
241 print_usage (); |
3606
|
242 |
|
243 return retval; |
|
244 } |
|
245 #endif |
|
246 |
2974
|
247 /* |
|
248 ;;; Local Variables: *** |
|
249 ;;; mode: C++ *** |
|
250 ;;; End: *** |
|
251 */ |