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 |
3503
|
31 #include <fstream> |
|
32 #include <iostream> |
|
33 #include <strstream> |
1728
|
34 #include <string> |
|
35 |
1350
|
36 #ifdef HAVE_UNISTD_H |
2442
|
37 #ifdef HAVE_SYS_TYPES_H |
1
|
38 #include <sys/types.h> |
2442
|
39 #endif |
1
|
40 #include <unistd.h> |
|
41 #endif |
367
|
42 |
138
|
43 #if defined (HAVE_TERMIOS_H) |
|
44 #include <termios.h> |
|
45 #elif defined (HAVE_TERMIO_H) |
1
|
46 #include <termio.h> |
138
|
47 #elif defined (HAVE_SGTTY_H) |
1
|
48 #include <sgtty.h> |
|
49 #else |
|
50 LOSE! LOSE! |
|
51 #endif |
|
52 |
|
53 #include "SLStack.h" |
|
54 |
3040
|
55 #include "dir-ops.h" |
|
56 #include "file-ops.h" |
2926
|
57 #include "file-stat.h" |
1651
|
58 #include "oct-cmplx.h" |
2926
|
59 #include "oct-env.h" |
3040
|
60 #include "pathsearch.h" |
1755
|
61 #include "str-vec.h" |
1651
|
62 |
2492
|
63 #include <defaults.h> |
1352
|
64 #include "defun.h" |
|
65 #include "dirfns.h" |
|
66 #include "error.h" |
|
67 #include "gripes.h" |
|
68 #include "input.h" |
1742
|
69 #include "oct-hist.h" |
1750
|
70 #include "oct-obj.h" |
1352
|
71 #include "pager.h" |
1690
|
72 #include "sysdep.h" |
1750
|
73 #include "toplev.h" |
1
|
74 #include "unwind-prot.h" |
1352
|
75 #include "utils.h" |
|
76 #include "variables.h" |
1
|
77 |
3354
|
78 // Should expressions like ones (-1, 5) result in an empty matrix or |
|
79 // an error? A positive value means yes. A negative value means |
|
80 // yes, but print a warning message. Zero means it should be |
|
81 // considered an error. |
|
82 static int Vtreat_neg_dim_as_zero; |
|
83 |
1
|
84 // Top level context (?) |
|
85 extern jmp_buf toplevel; |
|
86 |
581
|
87 // Return to the main command loop in octave.cc. |
|
88 |
3350
|
89 void |
1
|
90 jump_to_top_level (void) |
|
91 { |
2985
|
92 unwind_protect::run_all (); |
1
|
93 |
|
94 longjmp (toplevel, 1); |
|
95 } |
|
96 |
|
97 int |
3523
|
98 almost_match (const std::string& std, const std::string& s, int min_match_len, |
526
|
99 int case_sens) |
1
|
100 { |
1755
|
101 int stdlen = std.length (); |
|
102 int slen = s.length (); |
1
|
103 |
|
104 return (slen <= stdlen |
|
105 && slen >= min_match_len |
287
|
106 && (case_sens |
1755
|
107 ? (strncmp (std.c_str (), s.c_str (), slen) == 0) |
3350
|
108 : (octave_strncasecmp (std.c_str (), s.c_str (), slen) == 0))); |
287
|
109 } |
|
110 |
581
|
111 // Ugh. |
|
112 |
287
|
113 int |
3523
|
114 keyword_almost_match (const char * const *std, int *min_len, const std::string& s, |
287
|
115 int min_toks_to_match, int max_toks) |
|
116 { |
|
117 int status = 0; |
|
118 int tok_count = 0; |
|
119 int toks_matched = 0; |
|
120 |
1755
|
121 if (s.empty () || max_toks < 1) |
287
|
122 return status; |
|
123 |
1755
|
124 char *kw = strsave (s.c_str ()); |
287
|
125 |
|
126 char *t = kw; |
|
127 while (*t != '\0') |
|
128 { |
|
129 if (*t == '\t') |
|
130 *t = ' '; |
|
131 t++; |
|
132 } |
|
133 |
|
134 char *beg = kw; |
|
135 while (*beg == ' ') |
|
136 beg++; |
|
137 |
|
138 if (*beg == '\0') |
|
139 return status; |
|
140 |
|
141 |
3072
|
142 const char **to_match = new const char * [max_toks + 1]; |
|
143 const char * const *s1 = std; |
|
144 const char **s2 = to_match; |
287
|
145 |
526
|
146 if (! s1 || ! s2) |
287
|
147 goto done; |
|
148 |
|
149 s2[tok_count] = beg; |
|
150 char *end; |
526
|
151 while ((end = strchr (beg, ' ')) != 0) |
287
|
152 { |
|
153 *end = '\0'; |
|
154 beg = end + 1; |
|
155 |
|
156 while (*beg == ' ') |
|
157 beg++; |
|
158 |
|
159 if (*beg == '\0') |
|
160 break; |
|
161 |
|
162 tok_count++; |
|
163 if (tok_count >= max_toks) |
|
164 goto done; |
|
165 |
|
166 s2[tok_count] = beg; |
|
167 } |
526
|
168 s2[tok_count+1] = 0; |
287
|
169 |
|
170 s2 = to_match; |
|
171 |
|
172 for (;;) |
|
173 { |
|
174 if (! almost_match (*s1, *s2, min_len[toks_matched], 0)) |
|
175 goto done; |
|
176 |
|
177 toks_matched++; |
|
178 |
|
179 s1++; |
|
180 s2++; |
|
181 |
|
182 if (! *s2) |
|
183 { |
|
184 status = (toks_matched >= min_toks_to_match); |
|
185 goto done; |
|
186 } |
|
187 |
|
188 if (! *s1) |
|
189 goto done; |
|
190 } |
|
191 |
|
192 done: |
|
193 |
|
194 delete [] kw; |
|
195 delete [] to_match; |
|
196 |
|
197 return status; |
1
|
198 } |
|
199 |
2234
|
200 // Return non-zero if either NR or NC is zero. Return -1 if this |
|
201 // should be considered fatal; return 1 if this is ok. |
|
202 |
|
203 int |
|
204 empty_arg (const char *name, int nr, int nc) |
|
205 { |
|
206 int is_empty = 0; |
|
207 |
|
208 if (nr == 0 || nc == 0) |
|
209 { |
|
210 int flag = Vpropagate_empty_matrices; |
|
211 |
|
212 if (flag < 0) |
|
213 { |
|
214 gripe_empty_arg (name, 0); |
|
215 is_empty = 1; |
|
216 } |
|
217 else if (flag == 0) |
|
218 { |
|
219 gripe_empty_arg (name, 1); |
|
220 is_empty = -1; |
|
221 } |
|
222 else |
|
223 is_empty = 1; |
|
224 } |
|
225 |
|
226 return is_empty; |
|
227 } |
|
228 |
581
|
229 // See if the given file is in the path. |
|
230 |
1755
|
231 string |
3523
|
232 search_path_for_file (const std::string& path, const std::string& name) |
686
|
233 { |
3174
|
234 dir_path p (path); |
686
|
235 |
2926
|
236 return octave_env::make_absolute (p.find (name), octave_env::getcwd ()); |
686
|
237 } |
|
238 |
3195
|
239 DEFUN (file_in_loadpath, args, , |
3446
|
240 "-*- texinfo -*-\n\ |
|
241 @deftypefn {Built-in Function} {} file_in_loadpath (@var{name})\n\ |
3195
|
242 \n\ |
3446
|
243 Look up @var{name} in Octave's @code{LOADPATH}.\n\ |
|
244 @end deftypefn\n\ |
|
245 @seealso{file_in_path}") |
3195
|
246 { |
|
247 octave_value_list retval; |
|
248 |
|
249 int argc = args.length () + 1; |
|
250 |
|
251 string_vector argv = args.make_argv ("file_in_loadpath"); |
|
252 |
|
253 if (error_state) |
|
254 return retval; |
|
255 |
3225
|
256 if (argc == 2) |
3195
|
257 retval = octave_env::make_absolute (Vload_path_dir_path.find (argv[1]), |
|
258 octave_env::getcwd ()); |
|
259 else |
|
260 print_usage ("file_in_loadpath"); |
|
261 |
|
262 return retval; |
|
263 } |
|
264 |
1957
|
265 DEFUN (file_in_path, args, , |
3301
|
266 "-*- texinfo -*-\n\ |
|
267 @deftypefn {Built-in Function} {} file_in_path (@var{path}, @var{file})\n\ |
|
268 Return the absolute name name of @var{file} if it can be found in\n\ |
|
269 @var{path}. The value of @var{path} should be a colon-separated list of\n\ |
|
270 directories in the format described for the built-in variable\n\ |
|
271 @code{LOADPATH}.\n\ |
|
272 \n\ |
|
273 If the file cannot be found in the path, an empty matrix is returned.\n\ |
|
274 For example,\n\ |
|
275 \n\ |
|
276 @example\n\ |
|
277 file_in_path (LOADPATH, \"nargchk.m\")\n\ |
|
278 @result{} \"@value{OCTAVEHOME}/share/octave/2.0/m/general/nargchk.m\"\n\ |
|
279 @end example\n\ |
|
280 @end deftypefn") |
686
|
281 { |
2086
|
282 octave_value_list retval; |
686
|
283 |
1755
|
284 int argc = args.length () + 1; |
|
285 |
1968
|
286 string_vector argv = args.make_argv ("file_in_path"); |
1755
|
287 |
|
288 if (error_state) |
|
289 return retval; |
686
|
290 |
|
291 if (argc == 3) |
|
292 { |
3523
|
293 std::string fname = search_path_for_file (argv[1], argv[2]); |
686
|
294 |
1755
|
295 if (fname.empty ()) |
|
296 retval = Matrix (); |
|
297 else |
686
|
298 retval = fname; |
|
299 } |
|
300 else |
|
301 print_usage ("file_in_path"); |
|
302 |
|
303 return retval; |
|
304 } |
|
305 |
1755
|
306 string |
3523
|
307 file_in_path (const std::string& name, const std::string& suffix) |
526
|
308 { |
3523
|
309 std::string nm = name; |
526
|
310 |
1755
|
311 if (! suffix.empty ()) |
|
312 nm.append (suffix); |
686
|
313 |
3174
|
314 return octave_env::make_absolute (Vload_path_dir_path.find (nm), |
|
315 octave_env::getcwd ()); |
526
|
316 } |
|
317 |
581
|
318 // See if there is an function file in the path. If so, return the |
|
319 // full path to the file. |
|
320 |
1755
|
321 string |
3523
|
322 fcn_file_in_path (const std::string& name) |
526
|
323 { |
3523
|
324 std::string retval; |
908
|
325 |
1755
|
326 int len = name.length (); |
|
327 |
|
328 if (len > 0) |
|
329 { |
|
330 if (len > 2 && name [len - 2] == '.' && name [len - 1] == 'm') |
|
331 retval = file_in_path (name, ""); |
908
|
332 else |
1755
|
333 retval = file_in_path (name, ".m"); |
908
|
334 } |
1755
|
335 |
|
336 return retval; |
526
|
337 } |
|
338 |
581
|
339 // See if there is an octave file in the path. If so, return the |
|
340 // full path to the file. |
|
341 |
1755
|
342 string |
3523
|
343 oct_file_in_path (const std::string& name) |
572
|
344 { |
3523
|
345 std::string retval; |
908
|
346 |
1755
|
347 int len = name.length (); |
|
348 |
|
349 if (len > 0) |
|
350 { |
|
351 if (len > 2 && name [len - 4] == '.' && name [len - 3] == 'o' |
908
|
352 && name [len - 2] == 'c' && name [len - 1] == 't') |
1755
|
353 retval = file_in_path (name, ""); |
908
|
354 else |
1755
|
355 retval = file_in_path (name, ".oct"); |
908
|
356 } |
1755
|
357 |
|
358 return retval; |
572
|
359 } |
|
360 |
3103
|
361 // Replace backslash escapes in a string with the real values. |
|
362 |
|
363 string |
3523
|
364 do_string_escapes (const std::string& s) |
3103
|
365 { |
3523
|
366 std::string retval; |
3103
|
367 |
|
368 size_t i = 0; |
|
369 size_t j = 0; |
|
370 size_t len = s.length (); |
|
371 |
|
372 retval.resize (len); |
|
373 |
|
374 while (j < len) |
|
375 { |
|
376 if (s[j] == '\\' && j+1 < len) |
|
377 { |
|
378 switch (s[++j]) |
|
379 { |
|
380 case 'a': |
|
381 retval[i] = '\a'; |
|
382 break; |
|
383 |
|
384 case 'b': // backspace |
|
385 retval[i] = '\b'; |
|
386 break; |
|
387 |
|
388 case 'f': // formfeed |
|
389 retval[i] = '\f'; |
|
390 break; |
|
391 |
|
392 case 'n': // newline |
|
393 retval[i] = '\n'; |
|
394 break; |
|
395 |
|
396 case 'r': // carriage return |
|
397 retval[i] = '\r'; |
|
398 break; |
|
399 |
|
400 case 't': // horizontal tab |
|
401 retval[i] = '\t'; |
|
402 break; |
|
403 |
|
404 case 'v': // vertical tab |
|
405 retval[i] = '\v'; |
|
406 break; |
|
407 |
|
408 case '\\': // backslash |
|
409 retval[i] = '\\'; |
|
410 break; |
|
411 |
|
412 case '\'': // quote |
|
413 retval[i] = '\''; |
|
414 break; |
|
415 |
|
416 case '"': // double quote |
|
417 retval[i] = '"'; |
|
418 break; |
|
419 |
|
420 default: |
|
421 warning ("unrecognized escape sequence `\\%c' --\ |
|
422 converting to `%c'", s[j], s[j]); |
|
423 retval[i] = s[j]; |
|
424 break; |
|
425 } |
|
426 } |
|
427 else |
|
428 { |
|
429 retval[i] = s[j]; |
|
430 } |
|
431 |
|
432 i++; |
|
433 j++; |
|
434 } |
|
435 |
3105
|
436 retval.resize (i); |
3103
|
437 |
|
438 return retval; |
|
439 } |
|
440 |
|
441 DEFUN (do_string_escapes, args, , |
3446
|
442 "-*- texinfo -*-\n\ |
|
443 @deftypefn {Built-in Function} {} do_string_escapes (@var{string})\n\ |
|
444 Convert special characters in @var{string} to their escaped forms.\n\ |
|
445 @end deftypefn") |
3103
|
446 { |
|
447 octave_value retval; |
|
448 |
|
449 int nargin = args.length (); |
|
450 |
|
451 if (nargin == 1) |
|
452 { |
|
453 if (args(0).is_string ()) |
|
454 retval = do_string_escapes (args(0).string_value ()); |
|
455 else |
|
456 error ("do_string_escapes: argument must be a string"); |
|
457 } |
|
458 else |
|
459 print_usage ("do_string_escapes"); |
|
460 |
|
461 return retval; |
|
462 } |
|
463 |
1755
|
464 const char * |
801
|
465 undo_string_escape (char c) |
|
466 { |
|
467 if (! c) |
1755
|
468 return ""; |
801
|
469 |
|
470 switch (c) |
|
471 { |
|
472 case '\a': |
|
473 return "\\a"; |
|
474 |
|
475 case '\b': // backspace |
|
476 return "\\b"; |
|
477 |
|
478 case '\f': // formfeed |
|
479 return "\\f"; |
|
480 |
|
481 case '\n': // newline |
|
482 return "\\n"; |
|
483 |
|
484 case '\r': // carriage return |
|
485 return "\\r"; |
|
486 |
|
487 case '\t': // horizontal tab |
|
488 return "\\t"; |
|
489 |
|
490 case '\v': // vertical tab |
|
491 return "\\v"; |
|
492 |
|
493 case '\\': // backslash |
|
494 return "\\\\"; |
|
495 |
|
496 case '"': // double quote |
|
497 return "\\\""; |
|
498 |
|
499 default: |
1755
|
500 { |
|
501 static char retval[2]; |
|
502 retval[0] = c; |
|
503 retval[1] = '\0'; |
|
504 return retval; |
|
505 } |
801
|
506 } |
|
507 } |
|
508 |
1755
|
509 string |
3523
|
510 undo_string_escapes (const std::string& s) |
801
|
511 { |
3523
|
512 std::string retval; |
801
|
513 |
1755
|
514 for (size_t i = 0; i < s.length (); i++) |
|
515 retval.append (undo_string_escape (s[i])); |
801
|
516 |
1755
|
517 return retval; |
801
|
518 } |
|
519 |
1957
|
520 DEFUN (undo_string_escapes, args, , |
3361
|
521 "-*- texinfo -*-\n\ |
|
522 @deftypefn {Built-in Function} {} undo_string_escapes (@var{s})\n\ |
|
523 Converts special characters in strings back to their escaped forms. For\n\ |
|
524 example, the expression\n\ |
|
525 \n\ |
|
526 @example\n\ |
|
527 bell = \"\\a\";\n\ |
|
528 @end example\n\ |
|
529 \n\ |
|
530 @noindent\n\ |
|
531 assigns the value of the alert character (control-g, ASCII code 7) to\n\ |
|
532 the string variable @code{bell}. If this string is printed, the\n\ |
|
533 system will ring the terminal bell (if it is possible). This is\n\ |
|
534 normally the desired outcome. However, sometimes it is useful to be\n\ |
|
535 able to print the original representation of the string, with the\n\ |
|
536 special characters replaced by their escape sequences. For example,\n\ |
|
537 \n\ |
|
538 @example\n\ |
|
539 octave:13> undo_string_escapes (bell)\n\ |
|
540 ans = \\a\n\ |
|
541 @end example\n\ |
|
542 \n\ |
|
543 @noindent\n\ |
|
544 replaces the unprintable alert character with its printable\n\ |
|
545 representation.\n\ |
|
546 @end deftypefn") |
801
|
547 { |
2086
|
548 octave_value retval; |
801
|
549 |
|
550 int nargin = args.length (); |
|
551 |
3103
|
552 if (nargin == 1) |
|
553 { |
|
554 if (args(0).is_string ()) |
|
555 retval = undo_string_escapes (args(0).string_value ()); |
|
556 else |
|
557 error ("undo_string_escapes: argument must be a string"); |
|
558 } |
801
|
559 else |
1023
|
560 print_usage ("undo_string_escapes"); |
801
|
561 |
|
562 return retval; |
|
563 } |
|
564 |
2285
|
565 static void |
3523
|
566 warn_old_style_preference (bool val, const std::string& sval) |
2285
|
567 { |
|
568 warning |
|
569 ("preference of \"%s\" is obsolete -- use numeric value of %d instead", |
|
570 sval.c_str (), (val ? 1 : 0)); |
|
571 } |
|
572 |
2204
|
573 // Check the value of a string variable to see if it it's ok to do |
|
574 // something. |
|
575 // |
|
576 // return of 1 => always ok. |
|
577 // return of 0 => never ok. |
|
578 // return of -1 => ok, but give me warning (default). |
|
579 |
|
580 int |
3523
|
581 check_preference (const std::string& var) |
2204
|
582 { |
|
583 int pref = -1; |
|
584 |
3523
|
585 std::string val = builtin_string_variable (var); |
2204
|
586 |
|
587 if (val.empty ()) |
|
588 { |
|
589 double dval = 0; |
|
590 if (builtin_real_scalar_variable (var, dval)) |
|
591 pref = NINT (dval); |
|
592 } |
|
593 else |
|
594 { |
|
595 if (val.compare ("yes", 0, 3) == 0 |
|
596 || val.compare ("true", 0, 4) == 0) |
2285
|
597 { |
|
598 warn_old_style_preference (true, val); |
|
599 pref = 1; |
|
600 } |
2204
|
601 else if (val.compare ("never", 0, 5) == 0 |
|
602 || val.compare ("no", 0, 2) == 0 |
|
603 || val.compare ("false", 0, 5) == 0) |
2285
|
604 { |
|
605 warn_old_style_preference (false, val); |
|
606 pref = 0; |
|
607 } |
2204
|
608 } |
|
609 |
|
610 return pref; |
|
611 } |
|
612 |
3354
|
613 static void |
|
614 check_dimensions (int& nr, int& nc, const char *warnfor) |
|
615 { |
|
616 if (nr < 0 || nc < 0) |
|
617 { |
|
618 if (Vtreat_neg_dim_as_zero) |
|
619 { |
|
620 nr = (nr < 0) ? 0 : nr; |
|
621 nc = (nc < 0) ? 0 : nc; |
|
622 |
|
623 if (Vtreat_neg_dim_as_zero < 0) |
|
624 warning ("%s: converting negative dimension to zero", |
|
625 warnfor); |
|
626 } |
|
627 else |
|
628 error ("%s: can't create a matrix with negative dimensions", |
|
629 warnfor); |
|
630 } |
|
631 } |
|
632 |
|
633 void |
|
634 get_dimensions (const octave_value& a, const char *warn_for, |
|
635 int& nr, int& nc) |
|
636 { |
|
637 if (a.is_scalar_type ()) |
|
638 { |
|
639 nr = nc = a.nint_value (); |
|
640 } |
|
641 else |
|
642 { |
|
643 nr = a.rows (); |
|
644 nc = a.columns (); |
|
645 |
|
646 if ((nr == 1 && nc == 2) || (nr == 2 && nc == 1)) |
|
647 { |
3419
|
648 Array<double> v = a.vector_value (); |
3354
|
649 |
|
650 if (error_state) |
|
651 return; |
|
652 |
|
653 nr = NINT (v (0)); |
|
654 nc = NINT (v (1)); |
|
655 } |
|
656 else |
|
657 warning ("%s (A): use %s (size (A)) instead", warn_for, warn_for); |
|
658 } |
|
659 |
|
660 check_dimensions (nr, nc, warn_for); // May set error_state. |
|
661 } |
|
662 |
|
663 void |
|
664 get_dimensions (const octave_value& a, const octave_value& b, |
|
665 const char *warn_for, int& nr, int& nc) |
|
666 { |
|
667 nr = a.is_empty () ? 0 : a.nint_value (); |
|
668 nc = b.is_empty () ? 0 : b.nint_value (); |
|
669 |
|
670 if (error_state) |
|
671 error ("%s: expecting two scalar arguments", warn_for); |
|
672 else |
|
673 check_dimensions (nr, nc, warn_for); // May set error_state. |
|
674 } |
|
675 |
|
676 static int |
|
677 treat_neg_dim_as_zero (void) |
|
678 { |
|
679 Vtreat_neg_dim_as_zero = check_preference ("treat_neg_dim_as_zero"); |
|
680 |
|
681 return 0; |
|
682 } |
|
683 |
|
684 void |
|
685 symbols_of_utils (void) |
|
686 { |
|
687 DEFVAR (treat_neg_dim_as_zero, 0.0, treat_neg_dim_as_zero, |
3369
|
688 "-*- texinfo -*-\n\ |
|
689 @defvr {Built-in Variable} treat_neg_dim_as_zero\n\ |
|
690 If the value of @code{treat_neg_dim_as_zero} is nonzero, expressions\n\ |
|
691 like\n\ |
|
692 \n\ |
|
693 @example\n\ |
|
694 eye (-1)\n\ |
|
695 @end example\n\ |
|
696 \n\ |
|
697 @noindent\n\ |
|
698 produce an empty matrix (i.e., row and column dimensions are zero).\n\ |
|
699 Otherwise, an error message is printed and control is returned to the\n\ |
|
700 top level. The default value is 0.\n\ |
|
701 @end defvr"); |
3354
|
702 } |
|
703 |
572
|
704 /* |
1
|
705 ;;; Local Variables: *** |
|
706 ;;; mode: C++ *** |
|
707 ;;; End: *** |
|
708 */ |