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