1
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1
|
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 |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
240
|
23 #ifdef HAVE_CONFIG_H |
1192
|
24 #include <config.h> |
1
|
25 #endif |
|
26 |
1343
|
27 #include <climits> |
1345
|
28 #include <csetjmp> |
1346
|
29 #include <cstring> |
1343
|
30 |
1728
|
31 #include <string> |
|
32 |
1349
|
33 #include <fstream.h> |
|
34 #include <iostream.h> |
|
35 #include <strstream.h> |
|
36 |
1350
|
37 #ifdef HAVE_UNISTD_H |
2442
|
38 #ifdef HAVE_SYS_TYPES_H |
1
|
39 #include <sys/types.h> |
2442
|
40 #endif |
1
|
41 #include <unistd.h> |
|
42 #endif |
367
|
43 |
138
|
44 #if defined (HAVE_TERMIOS_H) |
|
45 #include <termios.h> |
|
46 #elif defined (HAVE_TERMIO_H) |
1
|
47 #include <termio.h> |
138
|
48 #elif defined (HAVE_SGTTY_H) |
1
|
49 #include <sgtty.h> |
|
50 #else |
|
51 LOSE! LOSE! |
|
52 #endif |
|
53 |
1465
|
54 #ifndef HAVE_STRNCASECMP |
|
55 extern "C" int strncasecmp (const char*, const char*, size_t); |
|
56 #endif |
1
|
57 |
|
58 #include "SLStack.h" |
|
59 |
3040
|
60 #include "dir-ops.h" |
|
61 #include "file-ops.h" |
2926
|
62 #include "file-stat.h" |
1651
|
63 #include "oct-cmplx.h" |
2926
|
64 #include "oct-env.h" |
3040
|
65 #include "pathsearch.h" |
1755
|
66 #include "str-vec.h" |
1651
|
67 |
2492
|
68 #include <defaults.h> |
1352
|
69 #include "defun.h" |
|
70 #include "dirfns.h" |
|
71 #include "error.h" |
|
72 #include "gripes.h" |
|
73 #include "input.h" |
1742
|
74 #include "oct-hist.h" |
1750
|
75 #include "oct-obj.h" |
1352
|
76 #include "pager.h" |
1690
|
77 #include "sysdep.h" |
1750
|
78 #include "toplev.h" |
1
|
79 #include "unwind-prot.h" |
1352
|
80 #include "utils.h" |
|
81 #include "variables.h" |
1
|
82 |
|
83 // Top level context (?) |
|
84 extern jmp_buf toplevel; |
|
85 |
581
|
86 // Return to the main command loop in octave.cc. |
|
87 |
1618
|
88 extern "C" void |
1
|
89 jump_to_top_level (void) |
|
90 { |
2985
|
91 unwind_protect::run_all (); |
1
|
92 |
|
93 longjmp (toplevel, 1); |
|
94 } |
|
95 |
|
96 int |
1755
|
97 almost_match (const string& std, const string& s, int min_match_len, |
526
|
98 int case_sens) |
1
|
99 { |
1755
|
100 int stdlen = std.length (); |
|
101 int slen = s.length (); |
1
|
102 |
|
103 return (slen <= stdlen |
|
104 && slen >= min_match_len |
287
|
105 && (case_sens |
1755
|
106 ? (strncmp (std.c_str (), s.c_str (), slen) == 0) |
|
107 : (strncasecmp (std.c_str (), s.c_str (), slen) == 0))); |
287
|
108 } |
|
109 |
581
|
110 // Ugh. |
|
111 |
287
|
112 int |
1755
|
113 keyword_almost_match (const char **std, int *min_len, const string& s, |
287
|
114 int min_toks_to_match, int max_toks) |
|
115 { |
|
116 int status = 0; |
|
117 int tok_count = 0; |
|
118 int toks_matched = 0; |
|
119 |
1755
|
120 if (s.empty () || max_toks < 1) |
287
|
121 return status; |
|
122 |
1755
|
123 char *kw = strsave (s.c_str ()); |
287
|
124 |
|
125 char *t = kw; |
|
126 while (*t != '\0') |
|
127 { |
|
128 if (*t == '\t') |
|
129 *t = ' '; |
|
130 t++; |
|
131 } |
|
132 |
|
133 char *beg = kw; |
|
134 while (*beg == ' ') |
|
135 beg++; |
|
136 |
|
137 if (*beg == '\0') |
|
138 return status; |
|
139 |
|
140 |
|
141 char **to_match = new char * [max_toks + 1]; |
526
|
142 const char **s1 = std; |
287
|
143 char **s2 = to_match; |
|
144 |
526
|
145 if (! s1 || ! s2) |
287
|
146 goto done; |
|
147 |
|
148 s2[tok_count] = beg; |
|
149 char *end; |
526
|
150 while ((end = strchr (beg, ' ')) != 0) |
287
|
151 { |
|
152 *end = '\0'; |
|
153 beg = end + 1; |
|
154 |
|
155 while (*beg == ' ') |
|
156 beg++; |
|
157 |
|
158 if (*beg == '\0') |
|
159 break; |
|
160 |
|
161 tok_count++; |
|
162 if (tok_count >= max_toks) |
|
163 goto done; |
|
164 |
|
165 s2[tok_count] = beg; |
|
166 } |
526
|
167 s2[tok_count+1] = 0; |
287
|
168 |
|
169 s2 = to_match; |
|
170 |
|
171 for (;;) |
|
172 { |
|
173 if (! almost_match (*s1, *s2, min_len[toks_matched], 0)) |
|
174 goto done; |
|
175 |
|
176 toks_matched++; |
|
177 |
|
178 s1++; |
|
179 s2++; |
|
180 |
|
181 if (! *s2) |
|
182 { |
|
183 status = (toks_matched >= min_toks_to_match); |
|
184 goto done; |
|
185 } |
|
186 |
|
187 if (! *s1) |
|
188 goto done; |
|
189 } |
|
190 |
|
191 done: |
|
192 |
|
193 delete [] kw; |
|
194 delete [] to_match; |
|
195 |
|
196 return status; |
1
|
197 } |
|
198 |
2234
|
199 // Return non-zero if either NR or NC is zero. Return -1 if this |
|
200 // should be considered fatal; return 1 if this is ok. |
|
201 |
|
202 int |
|
203 empty_arg (const char *name, int nr, int nc) |
|
204 { |
|
205 int is_empty = 0; |
|
206 |
|
207 if (nr == 0 || nc == 0) |
|
208 { |
|
209 int flag = Vpropagate_empty_matrices; |
|
210 |
|
211 if (flag < 0) |
|
212 { |
|
213 gripe_empty_arg (name, 0); |
|
214 is_empty = 1; |
|
215 } |
|
216 else if (flag == 0) |
|
217 { |
|
218 gripe_empty_arg (name, 1); |
|
219 is_empty = -1; |
|
220 } |
|
221 else |
|
222 is_empty = 1; |
|
223 } |
|
224 |
|
225 return is_empty; |
|
226 } |
|
227 |
581
|
228 // See if the given file is in the path. |
|
229 |
1755
|
230 string |
3040
|
231 search_path_for_file (const string& path, const string& name, |
|
232 bool do_tilde_expansion) |
686
|
233 { |
3040
|
234 string tmp_path = do_tilde_expansion ? file_ops::tilde_expand (path) : path; |
|
235 |
|
236 dir_path p (tmp_path); |
686
|
237 |
2926
|
238 return octave_env::make_absolute (p.find (name), octave_env::getcwd ()); |
686
|
239 } |
|
240 |
1957
|
241 DEFUN (file_in_path, args, , |
686
|
242 "file_in_path (PATH, NAME)") |
|
243 { |
2086
|
244 octave_value_list retval; |
686
|
245 |
1755
|
246 int argc = args.length () + 1; |
|
247 |
1968
|
248 string_vector argv = args.make_argv ("file_in_path"); |
1755
|
249 |
|
250 if (error_state) |
|
251 return retval; |
686
|
252 |
|
253 if (argc == 3) |
|
254 { |
1755
|
255 string fname = search_path_for_file (argv[1], argv[2]); |
686
|
256 |
1755
|
257 if (fname.empty ()) |
|
258 retval = Matrix (); |
|
259 else |
686
|
260 retval = fname; |
|
261 } |
|
262 else |
|
263 print_usage ("file_in_path"); |
|
264 |
|
265 return retval; |
|
266 } |
|
267 |
1755
|
268 string |
|
269 file_in_path (const string& name, const string& suffix) |
526
|
270 { |
1755
|
271 string nm = name; |
526
|
272 |
1755
|
273 if (! suffix.empty ()) |
|
274 nm.append (suffix); |
686
|
275 |
3040
|
276 return search_path_for_file (Vload_path, nm, false); |
526
|
277 } |
|
278 |
581
|
279 // See if there is an function file in the path. If so, return the |
|
280 // full path to the file. |
|
281 |
1755
|
282 string |
|
283 fcn_file_in_path (const string& name) |
526
|
284 { |
1755
|
285 string retval; |
908
|
286 |
1755
|
287 int len = name.length (); |
|
288 |
|
289 if (len > 0) |
|
290 { |
|
291 if (len > 2 && name [len - 2] == '.' && name [len - 1] == 'm') |
|
292 retval = file_in_path (name, ""); |
908
|
293 else |
1755
|
294 retval = file_in_path (name, ".m"); |
908
|
295 } |
1755
|
296 |
|
297 return retval; |
526
|
298 } |
|
299 |
581
|
300 // See if there is an octave file in the path. If so, return the |
|
301 // full path to the file. |
|
302 |
1755
|
303 string |
|
304 oct_file_in_path (const string& name) |
572
|
305 { |
1755
|
306 string retval; |
908
|
307 |
1755
|
308 int len = name.length (); |
|
309 |
|
310 if (len > 0) |
|
311 { |
|
312 if (len > 2 && name [len - 4] == '.' && name [len - 3] == 'o' |
908
|
313 && name [len - 2] == 'c' && name [len - 1] == 't') |
1755
|
314 retval = file_in_path (name, ""); |
908
|
315 else |
1755
|
316 retval = file_in_path (name, ".oct"); |
908
|
317 } |
1755
|
318 |
|
319 return retval; |
572
|
320 } |
|
321 |
1755
|
322 const char * |
801
|
323 undo_string_escape (char c) |
|
324 { |
|
325 if (! c) |
1755
|
326 return ""; |
801
|
327 |
|
328 switch (c) |
|
329 { |
|
330 case '\a': |
|
331 return "\\a"; |
|
332 |
|
333 case '\b': // backspace |
|
334 return "\\b"; |
|
335 |
|
336 case '\f': // formfeed |
|
337 return "\\f"; |
|
338 |
|
339 case '\n': // newline |
|
340 return "\\n"; |
|
341 |
|
342 case '\r': // carriage return |
|
343 return "\\r"; |
|
344 |
|
345 case '\t': // horizontal tab |
|
346 return "\\t"; |
|
347 |
|
348 case '\v': // vertical tab |
|
349 return "\\v"; |
|
350 |
|
351 case '\\': // backslash |
|
352 return "\\\\"; |
|
353 |
|
354 case '"': // double quote |
|
355 return "\\\""; |
|
356 |
|
357 default: |
1755
|
358 { |
|
359 static char retval[2]; |
|
360 retval[0] = c; |
|
361 retval[1] = '\0'; |
|
362 return retval; |
|
363 } |
801
|
364 } |
|
365 } |
|
366 |
1755
|
367 string |
|
368 undo_string_escapes (const string& s) |
801
|
369 { |
1755
|
370 string retval; |
801
|
371 |
1755
|
372 for (size_t i = 0; i < s.length (); i++) |
|
373 retval.append (undo_string_escape (s[i])); |
801
|
374 |
1755
|
375 return retval; |
801
|
376 } |
|
377 |
1957
|
378 DEFUN (undo_string_escapes, args, , |
801
|
379 "undo_string_escapes (STRING)") |
|
380 { |
2086
|
381 octave_value retval; |
801
|
382 |
|
383 int nargin = args.length (); |
|
384 |
|
385 if (nargin == 1 && args(0).is_string ()) |
1755
|
386 retval = undo_string_escapes (args(0).string_value ()); |
801
|
387 else |
1023
|
388 print_usage ("undo_string_escapes"); |
801
|
389 |
|
390 return retval; |
|
391 } |
|
392 |
2285
|
393 static void |
|
394 warn_old_style_preference (bool val, const string& sval) |
|
395 { |
|
396 warning |
|
397 ("preference of \"%s\" is obsolete -- use numeric value of %d instead", |
|
398 sval.c_str (), (val ? 1 : 0)); |
|
399 } |
|
400 |
2204
|
401 // Check the value of a string variable to see if it it's ok to do |
|
402 // something. |
|
403 // |
|
404 // return of 1 => always ok. |
|
405 // return of 0 => never ok. |
|
406 // return of -1 => ok, but give me warning (default). |
|
407 |
|
408 int |
|
409 check_preference (const string& var) |
|
410 { |
|
411 int pref = -1; |
|
412 |
|
413 string val = builtin_string_variable (var); |
|
414 |
|
415 if (val.empty ()) |
|
416 { |
|
417 double dval = 0; |
|
418 if (builtin_real_scalar_variable (var, dval)) |
|
419 pref = NINT (dval); |
|
420 } |
|
421 else |
|
422 { |
|
423 if (val.compare ("yes", 0, 3) == 0 |
|
424 || val.compare ("true", 0, 4) == 0) |
2285
|
425 { |
|
426 warn_old_style_preference (true, val); |
|
427 pref = 1; |
|
428 } |
2204
|
429 else if (val.compare ("never", 0, 5) == 0 |
|
430 || val.compare ("no", 0, 2) == 0 |
|
431 || val.compare ("false", 0, 5) == 0) |
2285
|
432 { |
|
433 warn_old_style_preference (false, val); |
|
434 pref = 0; |
|
435 } |
2204
|
436 } |
|
437 |
|
438 return pref; |
|
439 } |
|
440 |
572
|
441 /* |
1
|
442 ;;; Local Variables: *** |
|
443 ;;; mode: C++ *** |
|
444 ;;; End: *** |
|
445 */ |