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 |
|
54 #include "SLStack.h" |
|
55 |
3040
|
56 #include "dir-ops.h" |
|
57 #include "file-ops.h" |
2926
|
58 #include "file-stat.h" |
1651
|
59 #include "oct-cmplx.h" |
2926
|
60 #include "oct-env.h" |
3040
|
61 #include "pathsearch.h" |
1755
|
62 #include "str-vec.h" |
1651
|
63 |
2492
|
64 #include <defaults.h> |
1352
|
65 #include "defun.h" |
|
66 #include "dirfns.h" |
|
67 #include "error.h" |
|
68 #include "gripes.h" |
|
69 #include "input.h" |
1742
|
70 #include "oct-hist.h" |
1750
|
71 #include "oct-obj.h" |
1352
|
72 #include "pager.h" |
1690
|
73 #include "sysdep.h" |
1750
|
74 #include "toplev.h" |
1
|
75 #include "unwind-prot.h" |
1352
|
76 #include "utils.h" |
|
77 #include "variables.h" |
1
|
78 |
|
79 // Top level context (?) |
|
80 extern jmp_buf toplevel; |
|
81 |
581
|
82 // Return to the main command loop in octave.cc. |
|
83 |
3350
|
84 void |
1
|
85 jump_to_top_level (void) |
|
86 { |
2985
|
87 unwind_protect::run_all (); |
1
|
88 |
|
89 longjmp (toplevel, 1); |
|
90 } |
|
91 |
|
92 int |
1755
|
93 almost_match (const string& std, const string& s, int min_match_len, |
526
|
94 int case_sens) |
1
|
95 { |
1755
|
96 int stdlen = std.length (); |
|
97 int slen = s.length (); |
1
|
98 |
|
99 return (slen <= stdlen |
|
100 && slen >= min_match_len |
287
|
101 && (case_sens |
1755
|
102 ? (strncmp (std.c_str (), s.c_str (), slen) == 0) |
3350
|
103 : (octave_strncasecmp (std.c_str (), s.c_str (), slen) == 0))); |
287
|
104 } |
|
105 |
581
|
106 // Ugh. |
|
107 |
287
|
108 int |
3072
|
109 keyword_almost_match (const char * const *std, int *min_len, const string& s, |
287
|
110 int min_toks_to_match, int max_toks) |
|
111 { |
|
112 int status = 0; |
|
113 int tok_count = 0; |
|
114 int toks_matched = 0; |
|
115 |
1755
|
116 if (s.empty () || max_toks < 1) |
287
|
117 return status; |
|
118 |
1755
|
119 char *kw = strsave (s.c_str ()); |
287
|
120 |
|
121 char *t = kw; |
|
122 while (*t != '\0') |
|
123 { |
|
124 if (*t == '\t') |
|
125 *t = ' '; |
|
126 t++; |
|
127 } |
|
128 |
|
129 char *beg = kw; |
|
130 while (*beg == ' ') |
|
131 beg++; |
|
132 |
|
133 if (*beg == '\0') |
|
134 return status; |
|
135 |
|
136 |
3072
|
137 const char **to_match = new const char * [max_toks + 1]; |
|
138 const char * const *s1 = std; |
|
139 const char **s2 = to_match; |
287
|
140 |
526
|
141 if (! s1 || ! s2) |
287
|
142 goto done; |
|
143 |
|
144 s2[tok_count] = beg; |
|
145 char *end; |
526
|
146 while ((end = strchr (beg, ' ')) != 0) |
287
|
147 { |
|
148 *end = '\0'; |
|
149 beg = end + 1; |
|
150 |
|
151 while (*beg == ' ') |
|
152 beg++; |
|
153 |
|
154 if (*beg == '\0') |
|
155 break; |
|
156 |
|
157 tok_count++; |
|
158 if (tok_count >= max_toks) |
|
159 goto done; |
|
160 |
|
161 s2[tok_count] = beg; |
|
162 } |
526
|
163 s2[tok_count+1] = 0; |
287
|
164 |
|
165 s2 = to_match; |
|
166 |
|
167 for (;;) |
|
168 { |
|
169 if (! almost_match (*s1, *s2, min_len[toks_matched], 0)) |
|
170 goto done; |
|
171 |
|
172 toks_matched++; |
|
173 |
|
174 s1++; |
|
175 s2++; |
|
176 |
|
177 if (! *s2) |
|
178 { |
|
179 status = (toks_matched >= min_toks_to_match); |
|
180 goto done; |
|
181 } |
|
182 |
|
183 if (! *s1) |
|
184 goto done; |
|
185 } |
|
186 |
|
187 done: |
|
188 |
|
189 delete [] kw; |
|
190 delete [] to_match; |
|
191 |
|
192 return status; |
1
|
193 } |
|
194 |
2234
|
195 // Return non-zero if either NR or NC is zero. Return -1 if this |
|
196 // should be considered fatal; return 1 if this is ok. |
|
197 |
|
198 int |
|
199 empty_arg (const char *name, int nr, int nc) |
|
200 { |
|
201 int is_empty = 0; |
|
202 |
|
203 if (nr == 0 || nc == 0) |
|
204 { |
|
205 int flag = Vpropagate_empty_matrices; |
|
206 |
|
207 if (flag < 0) |
|
208 { |
|
209 gripe_empty_arg (name, 0); |
|
210 is_empty = 1; |
|
211 } |
|
212 else if (flag == 0) |
|
213 { |
|
214 gripe_empty_arg (name, 1); |
|
215 is_empty = -1; |
|
216 } |
|
217 else |
|
218 is_empty = 1; |
|
219 } |
|
220 |
|
221 return is_empty; |
|
222 } |
|
223 |
581
|
224 // See if the given file is in the path. |
|
225 |
1755
|
226 string |
3174
|
227 search_path_for_file (const string& path, const string& name) |
686
|
228 { |
3174
|
229 dir_path p (path); |
686
|
230 |
2926
|
231 return octave_env::make_absolute (p.find (name), octave_env::getcwd ()); |
686
|
232 } |
|
233 |
3195
|
234 DEFUN (file_in_loadpath, args, , |
|
235 "file_in_loadpath (NAME)\n\ |
|
236 \n\ |
|
237 Look up NAME in LOADPATH. See also file_in_path") |
|
238 { |
|
239 octave_value_list retval; |
|
240 |
|
241 int argc = args.length () + 1; |
|
242 |
|
243 string_vector argv = args.make_argv ("file_in_loadpath"); |
|
244 |
|
245 if (error_state) |
|
246 return retval; |
|
247 |
3225
|
248 if (argc == 2) |
3195
|
249 retval = octave_env::make_absolute (Vload_path_dir_path.find (argv[1]), |
|
250 octave_env::getcwd ()); |
|
251 else |
|
252 print_usage ("file_in_loadpath"); |
|
253 |
|
254 return retval; |
|
255 } |
|
256 |
1957
|
257 DEFUN (file_in_path, args, , |
3301
|
258 "-*- texinfo -*-\n\ |
|
259 @deftypefn {Built-in Function} {} file_in_path (@var{path}, @var{file})\n\ |
|
260 Return the absolute name name of @var{file} if it can be found in\n\ |
|
261 @var{path}. The value of @var{path} should be a colon-separated list of\n\ |
|
262 directories in the format described for the built-in variable\n\ |
|
263 @code{LOADPATH}.\n\ |
|
264 \n\ |
|
265 If the file cannot be found in the path, an empty matrix is returned.\n\ |
|
266 For example,\n\ |
|
267 \n\ |
|
268 @example\n\ |
|
269 file_in_path (LOADPATH, \"nargchk.m\")\n\ |
|
270 @result{} \"@value{OCTAVEHOME}/share/octave/2.0/m/general/nargchk.m\"\n\ |
|
271 @end example\n\ |
|
272 @end deftypefn") |
686
|
273 { |
2086
|
274 octave_value_list retval; |
686
|
275 |
1755
|
276 int argc = args.length () + 1; |
|
277 |
1968
|
278 string_vector argv = args.make_argv ("file_in_path"); |
1755
|
279 |
|
280 if (error_state) |
|
281 return retval; |
686
|
282 |
|
283 if (argc == 3) |
|
284 { |
3195
|
285 string fname = search_path_for_file (argv[1], argv[2]); |
686
|
286 |
1755
|
287 if (fname.empty ()) |
|
288 retval = Matrix (); |
|
289 else |
686
|
290 retval = fname; |
|
291 } |
|
292 else |
|
293 print_usage ("file_in_path"); |
|
294 |
|
295 return retval; |
|
296 } |
|
297 |
1755
|
298 string |
|
299 file_in_path (const string& name, const string& suffix) |
526
|
300 { |
1755
|
301 string nm = name; |
526
|
302 |
1755
|
303 if (! suffix.empty ()) |
|
304 nm.append (suffix); |
686
|
305 |
3174
|
306 return octave_env::make_absolute (Vload_path_dir_path.find (nm), |
|
307 octave_env::getcwd ()); |
526
|
308 } |
|
309 |
581
|
310 // See if there is an function file in the path. If so, return the |
|
311 // full path to the file. |
|
312 |
1755
|
313 string |
|
314 fcn_file_in_path (const string& name) |
526
|
315 { |
1755
|
316 string retval; |
908
|
317 |
1755
|
318 int len = name.length (); |
|
319 |
|
320 if (len > 0) |
|
321 { |
|
322 if (len > 2 && name [len - 2] == '.' && name [len - 1] == 'm') |
|
323 retval = file_in_path (name, ""); |
908
|
324 else |
1755
|
325 retval = file_in_path (name, ".m"); |
908
|
326 } |
1755
|
327 |
|
328 return retval; |
526
|
329 } |
|
330 |
581
|
331 // See if there is an octave file in the path. If so, return the |
|
332 // full path to the file. |
|
333 |
1755
|
334 string |
|
335 oct_file_in_path (const string& name) |
572
|
336 { |
1755
|
337 string retval; |
908
|
338 |
1755
|
339 int len = name.length (); |
|
340 |
|
341 if (len > 0) |
|
342 { |
|
343 if (len > 2 && name [len - 4] == '.' && name [len - 3] == 'o' |
908
|
344 && name [len - 2] == 'c' && name [len - 1] == 't') |
1755
|
345 retval = file_in_path (name, ""); |
908
|
346 else |
1755
|
347 retval = file_in_path (name, ".oct"); |
908
|
348 } |
1755
|
349 |
|
350 return retval; |
572
|
351 } |
|
352 |
3103
|
353 // Replace backslash escapes in a string with the real values. |
|
354 |
|
355 string |
|
356 do_string_escapes (const string& s) |
|
357 { |
|
358 string retval; |
|
359 |
|
360 size_t i = 0; |
|
361 size_t j = 0; |
|
362 size_t len = s.length (); |
|
363 |
|
364 retval.resize (len); |
|
365 |
|
366 while (j < len) |
|
367 { |
|
368 if (s[j] == '\\' && j+1 < len) |
|
369 { |
|
370 switch (s[++j]) |
|
371 { |
|
372 case 'a': |
|
373 retval[i] = '\a'; |
|
374 break; |
|
375 |
|
376 case 'b': // backspace |
|
377 retval[i] = '\b'; |
|
378 break; |
|
379 |
|
380 case 'f': // formfeed |
|
381 retval[i] = '\f'; |
|
382 break; |
|
383 |
|
384 case 'n': // newline |
|
385 retval[i] = '\n'; |
|
386 break; |
|
387 |
|
388 case 'r': // carriage return |
|
389 retval[i] = '\r'; |
|
390 break; |
|
391 |
|
392 case 't': // horizontal tab |
|
393 retval[i] = '\t'; |
|
394 break; |
|
395 |
|
396 case 'v': // vertical tab |
|
397 retval[i] = '\v'; |
|
398 break; |
|
399 |
|
400 case '\\': // backslash |
|
401 retval[i] = '\\'; |
|
402 break; |
|
403 |
|
404 case '\'': // quote |
|
405 retval[i] = '\''; |
|
406 break; |
|
407 |
|
408 case '"': // double quote |
|
409 retval[i] = '"'; |
|
410 break; |
|
411 |
|
412 default: |
|
413 warning ("unrecognized escape sequence `\\%c' --\ |
|
414 converting to `%c'", s[j], s[j]); |
|
415 retval[i] = s[j]; |
|
416 break; |
|
417 } |
|
418 } |
|
419 else |
|
420 { |
|
421 retval[i] = s[j]; |
|
422 } |
|
423 |
|
424 i++; |
|
425 j++; |
|
426 } |
|
427 |
3105
|
428 retval.resize (i); |
3103
|
429 |
|
430 return retval; |
|
431 } |
|
432 |
|
433 DEFUN (do_string_escapes, args, , |
|
434 "do_string_escapes (STRING)") |
|
435 { |
|
436 octave_value retval; |
|
437 |
|
438 int nargin = args.length (); |
|
439 |
|
440 if (nargin == 1) |
|
441 { |
|
442 if (args(0).is_string ()) |
|
443 retval = do_string_escapes (args(0).string_value ()); |
|
444 else |
|
445 error ("do_string_escapes: argument must be a string"); |
|
446 } |
|
447 else |
|
448 print_usage ("do_string_escapes"); |
|
449 |
|
450 return retval; |
|
451 } |
|
452 |
1755
|
453 const char * |
801
|
454 undo_string_escape (char c) |
|
455 { |
|
456 if (! c) |
1755
|
457 return ""; |
801
|
458 |
|
459 switch (c) |
|
460 { |
|
461 case '\a': |
|
462 return "\\a"; |
|
463 |
|
464 case '\b': // backspace |
|
465 return "\\b"; |
|
466 |
|
467 case '\f': // formfeed |
|
468 return "\\f"; |
|
469 |
|
470 case '\n': // newline |
|
471 return "\\n"; |
|
472 |
|
473 case '\r': // carriage return |
|
474 return "\\r"; |
|
475 |
|
476 case '\t': // horizontal tab |
|
477 return "\\t"; |
|
478 |
|
479 case '\v': // vertical tab |
|
480 return "\\v"; |
|
481 |
|
482 case '\\': // backslash |
|
483 return "\\\\"; |
|
484 |
|
485 case '"': // double quote |
|
486 return "\\\""; |
|
487 |
|
488 default: |
1755
|
489 { |
|
490 static char retval[2]; |
|
491 retval[0] = c; |
|
492 retval[1] = '\0'; |
|
493 return retval; |
|
494 } |
801
|
495 } |
|
496 } |
|
497 |
1755
|
498 string |
|
499 undo_string_escapes (const string& s) |
801
|
500 { |
1755
|
501 string retval; |
801
|
502 |
1755
|
503 for (size_t i = 0; i < s.length (); i++) |
|
504 retval.append (undo_string_escape (s[i])); |
801
|
505 |
1755
|
506 return retval; |
801
|
507 } |
|
508 |
1957
|
509 DEFUN (undo_string_escapes, args, , |
801
|
510 "undo_string_escapes (STRING)") |
|
511 { |
2086
|
512 octave_value retval; |
801
|
513 |
|
514 int nargin = args.length (); |
|
515 |
3103
|
516 if (nargin == 1) |
|
517 { |
|
518 if (args(0).is_string ()) |
|
519 retval = undo_string_escapes (args(0).string_value ()); |
|
520 else |
|
521 error ("undo_string_escapes: argument must be a string"); |
|
522 } |
801
|
523 else |
1023
|
524 print_usage ("undo_string_escapes"); |
801
|
525 |
|
526 return retval; |
|
527 } |
|
528 |
2285
|
529 static void |
|
530 warn_old_style_preference (bool val, const string& sval) |
|
531 { |
|
532 warning |
|
533 ("preference of \"%s\" is obsolete -- use numeric value of %d instead", |
|
534 sval.c_str (), (val ? 1 : 0)); |
|
535 } |
|
536 |
2204
|
537 // Check the value of a string variable to see if it it's ok to do |
|
538 // something. |
|
539 // |
|
540 // return of 1 => always ok. |
|
541 // return of 0 => never ok. |
|
542 // return of -1 => ok, but give me warning (default). |
|
543 |
|
544 int |
|
545 check_preference (const string& var) |
|
546 { |
|
547 int pref = -1; |
|
548 |
|
549 string val = builtin_string_variable (var); |
|
550 |
|
551 if (val.empty ()) |
|
552 { |
|
553 double dval = 0; |
|
554 if (builtin_real_scalar_variable (var, dval)) |
|
555 pref = NINT (dval); |
|
556 } |
|
557 else |
|
558 { |
|
559 if (val.compare ("yes", 0, 3) == 0 |
|
560 || val.compare ("true", 0, 4) == 0) |
2285
|
561 { |
|
562 warn_old_style_preference (true, val); |
|
563 pref = 1; |
|
564 } |
2204
|
565 else if (val.compare ("never", 0, 5) == 0 |
|
566 || val.compare ("no", 0, 2) == 0 |
|
567 || val.compare ("false", 0, 5) == 0) |
2285
|
568 { |
|
569 warn_old_style_preference (false, val); |
|
570 pref = 0; |
|
571 } |
2204
|
572 } |
|
573 |
|
574 return pref; |
|
575 } |
|
576 |
572
|
577 /* |
1
|
578 ;;; Local Variables: *** |
|
579 ;;; mode: C++ *** |
|
580 ;;; End: *** |
|
581 */ |