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 |
3072
|
113 keyword_almost_match (const char * const *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 |
3072
|
141 const char **to_match = new const char * [max_toks + 1]; |
|
142 const char * const *s1 = std; |
|
143 const char **s2 = to_match; |
287
|
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 |
3174
|
231 search_path_for_file (const string& path, const string& name) |
686
|
232 { |
3174
|
233 dir_path p (path); |
686
|
234 |
2926
|
235 return octave_env::make_absolute (p.find (name), octave_env::getcwd ()); |
686
|
236 } |
|
237 |
3195
|
238 DEFUN (file_in_loadpath, args, , |
|
239 "file_in_loadpath (NAME)\n\ |
|
240 \n\ |
|
241 Look up NAME in LOADPATH. See also file_in_path") |
|
242 { |
|
243 octave_value_list retval; |
|
244 |
|
245 int argc = args.length () + 1; |
|
246 |
|
247 string_vector argv = args.make_argv ("file_in_loadpath"); |
|
248 |
|
249 if (error_state) |
|
250 return retval; |
|
251 |
3225
|
252 if (argc == 2) |
3195
|
253 retval = octave_env::make_absolute (Vload_path_dir_path.find (argv[1]), |
|
254 octave_env::getcwd ()); |
|
255 else |
|
256 print_usage ("file_in_loadpath"); |
|
257 |
|
258 return retval; |
|
259 } |
|
260 |
1957
|
261 DEFUN (file_in_path, args, , |
686
|
262 "file_in_path (PATH, NAME)") |
|
263 { |
2086
|
264 octave_value_list retval; |
686
|
265 |
1755
|
266 int argc = args.length () + 1; |
|
267 |
1968
|
268 string_vector argv = args.make_argv ("file_in_path"); |
1755
|
269 |
|
270 if (error_state) |
|
271 return retval; |
686
|
272 |
|
273 if (argc == 3) |
|
274 { |
3195
|
275 string fname = search_path_for_file (argv[1], argv[2]); |
686
|
276 |
1755
|
277 if (fname.empty ()) |
|
278 retval = Matrix (); |
|
279 else |
686
|
280 retval = fname; |
|
281 } |
|
282 else |
|
283 print_usage ("file_in_path"); |
|
284 |
|
285 return retval; |
|
286 } |
|
287 |
1755
|
288 string |
|
289 file_in_path (const string& name, const string& suffix) |
526
|
290 { |
1755
|
291 string nm = name; |
526
|
292 |
1755
|
293 if (! suffix.empty ()) |
|
294 nm.append (suffix); |
686
|
295 |
3174
|
296 return octave_env::make_absolute (Vload_path_dir_path.find (nm), |
|
297 octave_env::getcwd ()); |
526
|
298 } |
|
299 |
581
|
300 // See if there is an function file in the path. If so, return the |
|
301 // full path to the file. |
|
302 |
1755
|
303 string |
|
304 fcn_file_in_path (const string& name) |
526
|
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 - 2] == '.' && name [len - 1] == 'm') |
|
313 retval = file_in_path (name, ""); |
908
|
314 else |
1755
|
315 retval = file_in_path (name, ".m"); |
908
|
316 } |
1755
|
317 |
|
318 return retval; |
526
|
319 } |
|
320 |
581
|
321 // See if there is an octave file in the path. If so, return the |
|
322 // full path to the file. |
|
323 |
1755
|
324 string |
|
325 oct_file_in_path (const string& name) |
572
|
326 { |
1755
|
327 string retval; |
908
|
328 |
1755
|
329 int len = name.length (); |
|
330 |
|
331 if (len > 0) |
|
332 { |
|
333 if (len > 2 && name [len - 4] == '.' && name [len - 3] == 'o' |
908
|
334 && name [len - 2] == 'c' && name [len - 1] == 't') |
1755
|
335 retval = file_in_path (name, ""); |
908
|
336 else |
1755
|
337 retval = file_in_path (name, ".oct"); |
908
|
338 } |
1755
|
339 |
|
340 return retval; |
572
|
341 } |
|
342 |
3103
|
343 // Replace backslash escapes in a string with the real values. |
|
344 |
|
345 string |
|
346 do_string_escapes (const string& s) |
|
347 { |
|
348 string retval; |
|
349 |
|
350 size_t i = 0; |
|
351 size_t j = 0; |
|
352 size_t len = s.length (); |
|
353 |
|
354 retval.resize (len); |
|
355 |
|
356 while (j < len) |
|
357 { |
|
358 if (s[j] == '\\' && j+1 < len) |
|
359 { |
|
360 switch (s[++j]) |
|
361 { |
|
362 case 'a': |
|
363 retval[i] = '\a'; |
|
364 break; |
|
365 |
|
366 case 'b': // backspace |
|
367 retval[i] = '\b'; |
|
368 break; |
|
369 |
|
370 case 'f': // formfeed |
|
371 retval[i] = '\f'; |
|
372 break; |
|
373 |
|
374 case 'n': // newline |
|
375 retval[i] = '\n'; |
|
376 break; |
|
377 |
|
378 case 'r': // carriage return |
|
379 retval[i] = '\r'; |
|
380 break; |
|
381 |
|
382 case 't': // horizontal tab |
|
383 retval[i] = '\t'; |
|
384 break; |
|
385 |
|
386 case 'v': // vertical tab |
|
387 retval[i] = '\v'; |
|
388 break; |
|
389 |
|
390 case '\\': // backslash |
|
391 retval[i] = '\\'; |
|
392 break; |
|
393 |
|
394 case '\'': // quote |
|
395 retval[i] = '\''; |
|
396 break; |
|
397 |
|
398 case '"': // double quote |
|
399 retval[i] = '"'; |
|
400 break; |
|
401 |
|
402 default: |
|
403 warning ("unrecognized escape sequence `\\%c' --\ |
|
404 converting to `%c'", s[j], s[j]); |
|
405 retval[i] = s[j]; |
|
406 break; |
|
407 } |
|
408 } |
|
409 else |
|
410 { |
|
411 retval[i] = s[j]; |
|
412 } |
|
413 |
|
414 i++; |
|
415 j++; |
|
416 } |
|
417 |
3105
|
418 retval.resize (i); |
3103
|
419 |
|
420 return retval; |
|
421 } |
|
422 |
|
423 DEFUN (do_string_escapes, args, , |
|
424 "do_string_escapes (STRING)") |
|
425 { |
|
426 octave_value retval; |
|
427 |
|
428 int nargin = args.length (); |
|
429 |
|
430 if (nargin == 1) |
|
431 { |
|
432 if (args(0).is_string ()) |
|
433 retval = do_string_escapes (args(0).string_value ()); |
|
434 else |
|
435 error ("do_string_escapes: argument must be a string"); |
|
436 } |
|
437 else |
|
438 print_usage ("do_string_escapes"); |
|
439 |
|
440 return retval; |
|
441 } |
|
442 |
1755
|
443 const char * |
801
|
444 undo_string_escape (char c) |
|
445 { |
|
446 if (! c) |
1755
|
447 return ""; |
801
|
448 |
|
449 switch (c) |
|
450 { |
|
451 case '\a': |
|
452 return "\\a"; |
|
453 |
|
454 case '\b': // backspace |
|
455 return "\\b"; |
|
456 |
|
457 case '\f': // formfeed |
|
458 return "\\f"; |
|
459 |
|
460 case '\n': // newline |
|
461 return "\\n"; |
|
462 |
|
463 case '\r': // carriage return |
|
464 return "\\r"; |
|
465 |
|
466 case '\t': // horizontal tab |
|
467 return "\\t"; |
|
468 |
|
469 case '\v': // vertical tab |
|
470 return "\\v"; |
|
471 |
|
472 case '\\': // backslash |
|
473 return "\\\\"; |
|
474 |
|
475 case '"': // double quote |
|
476 return "\\\""; |
|
477 |
|
478 default: |
1755
|
479 { |
|
480 static char retval[2]; |
|
481 retval[0] = c; |
|
482 retval[1] = '\0'; |
|
483 return retval; |
|
484 } |
801
|
485 } |
|
486 } |
|
487 |
1755
|
488 string |
|
489 undo_string_escapes (const string& s) |
801
|
490 { |
1755
|
491 string retval; |
801
|
492 |
1755
|
493 for (size_t i = 0; i < s.length (); i++) |
|
494 retval.append (undo_string_escape (s[i])); |
801
|
495 |
1755
|
496 return retval; |
801
|
497 } |
|
498 |
1957
|
499 DEFUN (undo_string_escapes, args, , |
801
|
500 "undo_string_escapes (STRING)") |
|
501 { |
2086
|
502 octave_value retval; |
801
|
503 |
|
504 int nargin = args.length (); |
|
505 |
3103
|
506 if (nargin == 1) |
|
507 { |
|
508 if (args(0).is_string ()) |
|
509 retval = undo_string_escapes (args(0).string_value ()); |
|
510 else |
|
511 error ("undo_string_escapes: argument must be a string"); |
|
512 } |
801
|
513 else |
1023
|
514 print_usage ("undo_string_escapes"); |
801
|
515 |
|
516 return retval; |
|
517 } |
|
518 |
2285
|
519 static void |
|
520 warn_old_style_preference (bool val, const string& sval) |
|
521 { |
|
522 warning |
|
523 ("preference of \"%s\" is obsolete -- use numeric value of %d instead", |
|
524 sval.c_str (), (val ? 1 : 0)); |
|
525 } |
|
526 |
2204
|
527 // Check the value of a string variable to see if it it's ok to do |
|
528 // something. |
|
529 // |
|
530 // return of 1 => always ok. |
|
531 // return of 0 => never ok. |
|
532 // return of -1 => ok, but give me warning (default). |
|
533 |
|
534 int |
|
535 check_preference (const string& var) |
|
536 { |
|
537 int pref = -1; |
|
538 |
|
539 string val = builtin_string_variable (var); |
|
540 |
|
541 if (val.empty ()) |
|
542 { |
|
543 double dval = 0; |
|
544 if (builtin_real_scalar_variable (var, dval)) |
|
545 pref = NINT (dval); |
|
546 } |
|
547 else |
|
548 { |
|
549 if (val.compare ("yes", 0, 3) == 0 |
|
550 || val.compare ("true", 0, 4) == 0) |
2285
|
551 { |
|
552 warn_old_style_preference (true, val); |
|
553 pref = 1; |
|
554 } |
2204
|
555 else if (val.compare ("never", 0, 5) == 0 |
|
556 || val.compare ("no", 0, 2) == 0 |
|
557 || val.compare ("false", 0, 5) == 0) |
2285
|
558 { |
|
559 warn_old_style_preference (false, val); |
|
560 pref = 0; |
|
561 } |
2204
|
562 } |
|
563 |
|
564 return pref; |
|
565 } |
|
566 |
572
|
567 /* |
1
|
568 ;;; Local Variables: *** |
|
569 ;;; mode: C++ *** |
|
570 ;;; End: *** |
|
571 */ |