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