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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
1
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
1
|
26 #endif |
|
27 |
3716
|
28 #include <cerrno> |
1343
|
29 #include <climits> |
1346
|
30 #include <cstring> |
1343
|
31 |
3503
|
32 #include <fstream> |
|
33 #include <iostream> |
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 |
3566
|
43 // Include setjmp.h, not csetjmp since the latter might only define |
|
44 // the ANSI standard C interface. |
|
45 |
|
46 #include <setjmp.h> |
|
47 |
4302
|
48 #include "quit.h" |
|
49 |
3040
|
50 #include "dir-ops.h" |
|
51 #include "file-ops.h" |
2926
|
52 #include "file-stat.h" |
4732
|
53 #include "lo-mappers.h" |
4051
|
54 #include "lo-sstream.h" |
1651
|
55 #include "oct-cmplx.h" |
2926
|
56 #include "oct-env.h" |
3040
|
57 #include "pathsearch.h" |
1755
|
58 #include "str-vec.h" |
1651
|
59 |
4216
|
60 #include "Cell.h" |
2492
|
61 #include <defaults.h> |
1352
|
62 #include "defun.h" |
|
63 #include "dirfns.h" |
|
64 #include "error.h" |
|
65 #include "gripes.h" |
|
66 #include "input.h" |
1742
|
67 #include "oct-hist.h" |
1750
|
68 #include "oct-obj.h" |
1352
|
69 #include "pager.h" |
1690
|
70 #include "sysdep.h" |
1750
|
71 #include "toplev.h" |
1
|
72 #include "unwind-prot.h" |
1352
|
73 #include "utils.h" |
|
74 #include "variables.h" |
1
|
75 |
4457
|
76 // If TRUE, print a warning for expressions like |
|
77 // |
|
78 // ones (-1, 5) |
|
79 // |
|
80 static int Vwarn_neg_dim_as_zero; |
3354
|
81 |
4143
|
82 // Return TRUE if S is a valid identifier. |
|
83 |
|
84 bool |
|
85 valid_identifier (const char *s) |
|
86 { |
5290
|
87 if (! s || ! (isalpha (*s) || *s == '_' || *s == '$')) |
4143
|
88 return false; |
|
89 |
|
90 while (*++s != '\0') |
5290
|
91 if (! (isalnum (*s) || *s == '_' || *s == '$')) |
4143
|
92 return false; |
|
93 |
|
94 return true; |
|
95 } |
|
96 |
|
97 bool |
|
98 valid_identifier (const std::string& s) |
|
99 { |
|
100 return valid_identifier (s.c_str ()); |
|
101 } |
|
102 |
4264
|
103 DEFCMD (isvarname, args, , |
5040
|
104 "-*- texinfo -*-\n\ |
|
105 @deftypefn {Built-in Function} {} isvarname (@var{name})\n\ |
4264
|
106 Return true if @var{name} is a valid variable name\n\ |
|
107 @end deftypefn") |
|
108 { |
|
109 octave_value retval; |
|
110 |
|
111 int argc = args.length () + 1; |
|
112 |
4610
|
113 string_vector argv = args.make_argv ("isvarname"); |
4264
|
114 |
|
115 if (error_state) |
|
116 return retval; |
|
117 |
|
118 if (argc == 2) |
|
119 retval = valid_identifier (argv[1]); |
|
120 else |
|
121 print_usage ("isvarname"); |
|
122 |
|
123 return retval; |
|
124 } |
|
125 |
1
|
126 int |
3523
|
127 almost_match (const std::string& std, const std::string& s, int min_match_len, |
526
|
128 int case_sens) |
1
|
129 { |
1755
|
130 int stdlen = std.length (); |
|
131 int slen = s.length (); |
1
|
132 |
|
133 return (slen <= stdlen |
|
134 && slen >= min_match_len |
287
|
135 && (case_sens |
1755
|
136 ? (strncmp (std.c_str (), s.c_str (), slen) == 0) |
3350
|
137 : (octave_strncasecmp (std.c_str (), s.c_str (), slen) == 0))); |
287
|
138 } |
|
139 |
581
|
140 // Ugh. |
|
141 |
287
|
142 int |
3523
|
143 keyword_almost_match (const char * const *std, int *min_len, const std::string& s, |
287
|
144 int min_toks_to_match, int max_toks) |
|
145 { |
|
146 int status = 0; |
|
147 int tok_count = 0; |
|
148 int toks_matched = 0; |
|
149 |
1755
|
150 if (s.empty () || max_toks < 1) |
287
|
151 return status; |
|
152 |
1755
|
153 char *kw = strsave (s.c_str ()); |
287
|
154 |
|
155 char *t = kw; |
|
156 while (*t != '\0') |
|
157 { |
|
158 if (*t == '\t') |
|
159 *t = ' '; |
|
160 t++; |
|
161 } |
|
162 |
|
163 char *beg = kw; |
|
164 while (*beg == ' ') |
|
165 beg++; |
|
166 |
|
167 if (*beg == '\0') |
|
168 return status; |
|
169 |
|
170 |
3072
|
171 const char **to_match = new const char * [max_toks + 1]; |
|
172 const char * const *s1 = std; |
|
173 const char **s2 = to_match; |
287
|
174 |
526
|
175 if (! s1 || ! s2) |
287
|
176 goto done; |
|
177 |
|
178 s2[tok_count] = beg; |
|
179 char *end; |
526
|
180 while ((end = strchr (beg, ' ')) != 0) |
287
|
181 { |
|
182 *end = '\0'; |
|
183 beg = end + 1; |
|
184 |
|
185 while (*beg == ' ') |
|
186 beg++; |
|
187 |
|
188 if (*beg == '\0') |
|
189 break; |
|
190 |
|
191 tok_count++; |
|
192 if (tok_count >= max_toks) |
|
193 goto done; |
|
194 |
|
195 s2[tok_count] = beg; |
|
196 } |
526
|
197 s2[tok_count+1] = 0; |
287
|
198 |
|
199 s2 = to_match; |
|
200 |
|
201 for (;;) |
|
202 { |
|
203 if (! almost_match (*s1, *s2, min_len[toks_matched], 0)) |
|
204 goto done; |
|
205 |
|
206 toks_matched++; |
|
207 |
|
208 s1++; |
|
209 s2++; |
|
210 |
|
211 if (! *s2) |
|
212 { |
|
213 status = (toks_matched >= min_toks_to_match); |
|
214 goto done; |
|
215 } |
|
216 |
|
217 if (! *s1) |
|
218 goto done; |
|
219 } |
|
220 |
|
221 done: |
|
222 |
|
223 delete [] kw; |
|
224 delete [] to_match; |
|
225 |
|
226 return status; |
1
|
227 } |
|
228 |
2234
|
229 // Return non-zero if either NR or NC is zero. Return -1 if this |
|
230 // should be considered fatal; return 1 if this is ok. |
|
231 |
|
232 int |
5275
|
233 empty_arg (const char * /* name */, octave_idx_type nr, octave_idx_type nc) |
2234
|
234 { |
4478
|
235 return (nr == 0 || nc == 0); |
2234
|
236 } |
|
237 |
581
|
238 // See if the given file is in the path. |
|
239 |
3536
|
240 std::string |
4243
|
241 search_path_for_file (const std::string& path, const string_vector& names) |
686
|
242 { |
3174
|
243 dir_path p (path); |
686
|
244 |
4243
|
245 return octave_env::make_absolute (p.find_first_of (names), |
|
246 octave_env::getcwd ()); |
686
|
247 } |
|
248 |
4216
|
249 // Find all locations of the given file in the path. |
|
250 |
|
251 string_vector |
4243
|
252 search_path_for_all_files (const std::string& path, const string_vector& names) |
4216
|
253 { |
|
254 dir_path p (path); |
|
255 |
4243
|
256 string_vector sv = p.find_all_first_of (names); |
4216
|
257 |
|
258 int len = sv.length (); |
|
259 |
|
260 for (int i = 0; i < len; i++) |
|
261 sv[i] = octave_env::make_absolute (sv[i], octave_env::getcwd ()); |
|
262 |
|
263 return sv; |
|
264 } |
|
265 |
|
266 static string_vector |
|
267 make_absolute (const string_vector& sv) |
|
268 { |
|
269 int len = sv.length (); |
|
270 |
|
271 for (int i = 0; i < len; i++) |
|
272 sv[i] = octave_env::make_absolute (sv[i], octave_env::getcwd ()); |
|
273 |
|
274 return sv; |
|
275 } |
|
276 |
3195
|
277 DEFUN (file_in_loadpath, args, , |
3446
|
278 "-*- texinfo -*-\n\ |
4216
|
279 @deftypefn {Built-in Function} {} file_in_loadpath (@var{file})\n\ |
|
280 @deftypefnx {Built-in Function} {} file_in_loadpath (@var{file}, \"all\")\n\ |
3195
|
281 \n\ |
5448
|
282 Return the absolute name of @var{file} if it can be found in\n\ |
4216
|
283 the list of directories specified by @code{LOADPATH}.\n\ |
|
284 If no file is found, return an empty matrix.\n\ |
|
285 \n\ |
5448
|
286 If the first argument is a cell array of strings, search each\n\ |
4243
|
287 directory of the loadpath for element of the cell array and return\n\ |
|
288 the first that matches.\n\ |
|
289 \n\ |
4216
|
290 If the second optional argument @code{\"all\"} is supplied, return\n\ |
|
291 a cell array containing the list of all files that have the same\n\ |
|
292 name in the path. If no files are found, return an empty cell array.\n\ |
3446
|
293 @end deftypefn\n\ |
|
294 @seealso{file_in_path}") |
3195
|
295 { |
4216
|
296 octave_value retval; |
3195
|
297 |
4243
|
298 int nargin = args.length (); |
3195
|
299 |
4243
|
300 if (nargin == 1 || nargin == 2) |
4216
|
301 { |
4243
|
302 string_vector names = args(0).all_strings (); |
|
303 |
|
304 if (! error_state && names.length () > 0) |
|
305 { |
|
306 if (nargin == 1) |
|
307 { |
|
308 std::string fname = octave_env::make_absolute |
|
309 (Vload_path_dir_path.find_first_of (names), |
|
310 octave_env::getcwd ()); |
|
311 |
|
312 if (fname.empty ()) |
|
313 retval = Matrix (); |
|
314 else |
|
315 retval = fname; |
|
316 } |
|
317 else if (nargin == 2) |
|
318 { |
|
319 std::string opt = args(1).string_value (); |
|
320 |
|
321 if (! error_state && opt == "all") |
|
322 retval = Cell (make_absolute (Vload_path_dir_path.find_all_first_of (names))); |
|
323 else |
4249
|
324 error ("file_in_loadpath: invalid option"); |
4243
|
325 } |
|
326 } |
4216
|
327 else |
4243
|
328 error ("file_in_loadpath: expecting string as first argument"); |
4216
|
329 } |
3195
|
330 else |
|
331 print_usage ("file_in_loadpath"); |
|
332 |
|
333 return retval; |
|
334 } |
|
335 |
1957
|
336 DEFUN (file_in_path, args, , |
3301
|
337 "-*- texinfo -*-\n\ |
|
338 @deftypefn {Built-in Function} {} file_in_path (@var{path}, @var{file})\n\ |
4216
|
339 @deftypefnx {Built-in Function} {} file_in_path (@var{path}, @var{file}, \"all\")\n\ |
5448
|
340 Return the absolute name of @var{file} if it can be found in\n\ |
3301
|
341 @var{path}. The value of @var{path} should be a colon-separated list of\n\ |
|
342 directories in the format described for the built-in variable\n\ |
4216
|
343 @code{LOADPATH}. If no file is found, return an empty matrix.\n\ |
3301
|
344 For example,\n\ |
|
345 \n\ |
|
346 @example\n\ |
|
347 file_in_path (LOADPATH, \"nargchk.m\")\n\ |
|
348 @result{} \"@value{OCTAVEHOME}/share/octave/2.0/m/general/nargchk.m\"\n\ |
|
349 @end example\n\ |
4216
|
350 \n\ |
5448
|
351 If the second argument is a cell array of strings, search each\n\ |
4243
|
352 directory of the path for element of the cell array and return\n\ |
|
353 the first that matches.\n\ |
|
354 \n\ |
4216
|
355 If the third optional argument @code{\"all\"} is supplied, return\n\ |
|
356 a cell array containing the list of all files that have the same\n\ |
|
357 name in the path. If no files are found, return an empty cell array.\n\ |
4249
|
358 @seealso{file_in_loadpath}\n\ |
3301
|
359 @end deftypefn") |
686
|
360 { |
4216
|
361 octave_value retval; |
686
|
362 |
4243
|
363 int nargin = args.length (); |
1755
|
364 |
4243
|
365 if (nargin == 2 || nargin == 3) |
686
|
366 { |
4243
|
367 std::string path = args(0).string_value (); |
|
368 |
|
369 if (! error_state) |
|
370 { |
4249
|
371 string_vector names = args(1).all_strings (); |
4243
|
372 |
|
373 if (! error_state && names.length () > 0) |
|
374 { |
|
375 if (nargin == 2) |
|
376 { |
|
377 std::string fname = search_path_for_file (path, names); |
686
|
378 |
4243
|
379 if (fname.empty ()) |
|
380 retval = Matrix (); |
|
381 else |
|
382 retval = fname; |
|
383 } |
|
384 else if (nargin == 3) |
|
385 { |
4249
|
386 std::string opt = args(2).string_value (); |
4243
|
387 |
|
388 if (! error_state && opt == "all") |
|
389 retval = Cell (make_absolute (search_path_for_all_files (path, names))); |
|
390 else |
4249
|
391 error ("file_in_path: invalid option"); |
4243
|
392 } |
|
393 } |
|
394 else |
|
395 error ("file_in_path: expecting string as second argument"); |
|
396 } |
1755
|
397 else |
4243
|
398 error ("file_in_path: expecting string as first argument"); |
686
|
399 } |
|
400 else |
|
401 print_usage ("file_in_path"); |
|
402 |
|
403 return retval; |
|
404 } |
|
405 |
3536
|
406 std::string |
3523
|
407 file_in_path (const std::string& name, const std::string& suffix) |
526
|
408 { |
3523
|
409 std::string nm = name; |
526
|
410 |
1755
|
411 if (! suffix.empty ()) |
|
412 nm.append (suffix); |
686
|
413 |
3174
|
414 return octave_env::make_absolute (Vload_path_dir_path.find (nm), |
|
415 octave_env::getcwd ()); |
526
|
416 } |
|
417 |
581
|
418 // See if there is an function file in the path. If so, return the |
|
419 // full path to the file. |
|
420 |
3536
|
421 std::string |
3523
|
422 fcn_file_in_path (const std::string& name) |
526
|
423 { |
3523
|
424 std::string retval; |
908
|
425 |
1755
|
426 int len = name.length (); |
|
427 |
|
428 if (len > 0) |
|
429 { |
|
430 if (len > 2 && name [len - 2] == '.' && name [len - 1] == 'm') |
|
431 retval = file_in_path (name, ""); |
908
|
432 else |
1755
|
433 retval = file_in_path (name, ".m"); |
908
|
434 } |
1755
|
435 |
|
436 return retval; |
526
|
437 } |
|
438 |
581
|
439 // See if there is an octave file in the path. If so, return the |
|
440 // full path to the file. |
|
441 |
3536
|
442 std::string |
3523
|
443 oct_file_in_path (const std::string& name) |
572
|
444 { |
3523
|
445 std::string retval; |
908
|
446 |
1755
|
447 int len = name.length (); |
|
448 |
|
449 if (len > 0) |
|
450 { |
|
451 if (len > 2 && name [len - 4] == '.' && name [len - 3] == 'o' |
908
|
452 && name [len - 2] == 'c' && name [len - 1] == 't') |
1755
|
453 retval = file_in_path (name, ""); |
908
|
454 else |
1755
|
455 retval = file_in_path (name, ".oct"); |
908
|
456 } |
1755
|
457 |
|
458 return retval; |
572
|
459 } |
|
460 |
3103
|
461 // Replace backslash escapes in a string with the real values. |
|
462 |
3536
|
463 std::string |
3523
|
464 do_string_escapes (const std::string& s) |
3103
|
465 { |
3523
|
466 std::string retval; |
3103
|
467 |
|
468 size_t i = 0; |
|
469 size_t j = 0; |
|
470 size_t len = s.length (); |
|
471 |
|
472 retval.resize (len); |
|
473 |
|
474 while (j < len) |
|
475 { |
|
476 if (s[j] == '\\' && j+1 < len) |
|
477 { |
|
478 switch (s[++j]) |
|
479 { |
3893
|
480 case '0': |
|
481 retval[i] = '\0'; |
|
482 break; |
|
483 |
3103
|
484 case 'a': |
|
485 retval[i] = '\a'; |
|
486 break; |
|
487 |
|
488 case 'b': // backspace |
|
489 retval[i] = '\b'; |
|
490 break; |
|
491 |
|
492 case 'f': // formfeed |
|
493 retval[i] = '\f'; |
|
494 break; |
|
495 |
|
496 case 'n': // newline |
|
497 retval[i] = '\n'; |
|
498 break; |
|
499 |
|
500 case 'r': // carriage return |
|
501 retval[i] = '\r'; |
|
502 break; |
|
503 |
|
504 case 't': // horizontal tab |
|
505 retval[i] = '\t'; |
|
506 break; |
|
507 |
|
508 case 'v': // vertical tab |
|
509 retval[i] = '\v'; |
|
510 break; |
|
511 |
|
512 case '\\': // backslash |
|
513 retval[i] = '\\'; |
|
514 break; |
|
515 |
|
516 case '\'': // quote |
|
517 retval[i] = '\''; |
|
518 break; |
|
519 |
|
520 case '"': // double quote |
|
521 retval[i] = '"'; |
|
522 break; |
|
523 |
|
524 default: |
|
525 warning ("unrecognized escape sequence `\\%c' --\ |
|
526 converting to `%c'", s[j], s[j]); |
|
527 retval[i] = s[j]; |
|
528 break; |
|
529 } |
|
530 } |
|
531 else |
|
532 { |
|
533 retval[i] = s[j]; |
|
534 } |
|
535 |
|
536 i++; |
|
537 j++; |
|
538 } |
|
539 |
3105
|
540 retval.resize (i); |
3103
|
541 |
|
542 return retval; |
|
543 } |
|
544 |
|
545 DEFUN (do_string_escapes, args, , |
3446
|
546 "-*- texinfo -*-\n\ |
|
547 @deftypefn {Built-in Function} {} do_string_escapes (@var{string})\n\ |
|
548 Convert special characters in @var{string} to their escaped forms.\n\ |
|
549 @end deftypefn") |
3103
|
550 { |
|
551 octave_value retval; |
|
552 |
|
553 int nargin = args.length (); |
|
554 |
|
555 if (nargin == 1) |
|
556 { |
|
557 if (args(0).is_string ()) |
|
558 retval = do_string_escapes (args(0).string_value ()); |
|
559 else |
|
560 error ("do_string_escapes: argument must be a string"); |
|
561 } |
|
562 else |
|
563 print_usage ("do_string_escapes"); |
|
564 |
|
565 return retval; |
|
566 } |
|
567 |
1755
|
568 const char * |
801
|
569 undo_string_escape (char c) |
|
570 { |
|
571 if (! c) |
1755
|
572 return ""; |
801
|
573 |
|
574 switch (c) |
|
575 { |
3893
|
576 case '\0': |
|
577 return "\\0"; |
|
578 |
801
|
579 case '\a': |
|
580 return "\\a"; |
|
581 |
|
582 case '\b': // backspace |
|
583 return "\\b"; |
|
584 |
|
585 case '\f': // formfeed |
|
586 return "\\f"; |
|
587 |
|
588 case '\n': // newline |
|
589 return "\\n"; |
|
590 |
|
591 case '\r': // carriage return |
|
592 return "\\r"; |
|
593 |
|
594 case '\t': // horizontal tab |
|
595 return "\\t"; |
|
596 |
|
597 case '\v': // vertical tab |
|
598 return "\\v"; |
|
599 |
|
600 case '\\': // backslash |
|
601 return "\\\\"; |
|
602 |
|
603 case '"': // double quote |
|
604 return "\\\""; |
|
605 |
|
606 default: |
1755
|
607 { |
|
608 static char retval[2]; |
|
609 retval[0] = c; |
|
610 retval[1] = '\0'; |
|
611 return retval; |
|
612 } |
801
|
613 } |
|
614 } |
|
615 |
3536
|
616 std::string |
3523
|
617 undo_string_escapes (const std::string& s) |
801
|
618 { |
3523
|
619 std::string retval; |
801
|
620 |
1755
|
621 for (size_t i = 0; i < s.length (); i++) |
|
622 retval.append (undo_string_escape (s[i])); |
801
|
623 |
1755
|
624 return retval; |
801
|
625 } |
|
626 |
1957
|
627 DEFUN (undo_string_escapes, args, , |
3361
|
628 "-*- texinfo -*-\n\ |
|
629 @deftypefn {Built-in Function} {} undo_string_escapes (@var{s})\n\ |
|
630 Converts special characters in strings back to their escaped forms. For\n\ |
|
631 example, the expression\n\ |
|
632 \n\ |
|
633 @example\n\ |
|
634 bell = \"\\a\";\n\ |
|
635 @end example\n\ |
|
636 \n\ |
|
637 @noindent\n\ |
|
638 assigns the value of the alert character (control-g, ASCII code 7) to\n\ |
|
639 the string variable @code{bell}. If this string is printed, the\n\ |
|
640 system will ring the terminal bell (if it is possible). This is\n\ |
|
641 normally the desired outcome. However, sometimes it is useful to be\n\ |
|
642 able to print the original representation of the string, with the\n\ |
|
643 special characters replaced by their escape sequences. For example,\n\ |
|
644 \n\ |
|
645 @example\n\ |
|
646 octave:13> undo_string_escapes (bell)\n\ |
|
647 ans = \\a\n\ |
|
648 @end example\n\ |
|
649 \n\ |
|
650 @noindent\n\ |
|
651 replaces the unprintable alert character with its printable\n\ |
|
652 representation.\n\ |
|
653 @end deftypefn") |
801
|
654 { |
2086
|
655 octave_value retval; |
801
|
656 |
|
657 int nargin = args.length (); |
|
658 |
3103
|
659 if (nargin == 1) |
|
660 { |
|
661 if (args(0).is_string ()) |
|
662 retval = undo_string_escapes (args(0).string_value ()); |
|
663 else |
|
664 error ("undo_string_escapes: argument must be a string"); |
|
665 } |
801
|
666 else |
1023
|
667 print_usage ("undo_string_escapes"); |
801
|
668 |
|
669 return retval; |
|
670 } |
|
671 |
4243
|
672 DEFUN (find_first_of_in_loadpath, args, , "") |
|
673 { |
|
674 octave_value retval; |
|
675 |
|
676 if (args.length () == 1) |
|
677 { |
|
678 string_vector names = args(0).all_strings (); |
|
679 |
|
680 if (! error_state) |
|
681 retval = Vload_path_dir_path.find_all_first_of (names); |
|
682 } |
|
683 else |
|
684 print_usage ("find_first_of_in_loadpath"); |
|
685 |
|
686 return retval; |
|
687 } |
|
688 |
|
689 |
3879
|
690 // #if 0 |
3716
|
691 |
|
692 // Octave could use some way to access the value of ERRNO, but this is |
|
693 // probably not the best interface, so don't depend on it... |
|
694 |
|
695 DEFUN (ERRNO, args, , |
|
696 "-*- texinfo -*-\n\ |
|
697 @deftypefn {Built-in Function} {@var{system_error_number}} errno ()\n\ |
|
698 Return the current value of the system-dependent variable errno.\n\ |
|
699 @end deftypefn") |
|
700 { |
|
701 octave_value retval; |
|
702 |
|
703 if (args.length () == 0) |
4254
|
704 retval = errno; |
3716
|
705 else |
|
706 print_usage ("errno"); |
|
707 |
|
708 return retval; |
|
709 } |
|
710 |
3879
|
711 // #endif |
3716
|
712 |
2285
|
713 static void |
3523
|
714 warn_old_style_preference (bool val, const std::string& sval) |
2285
|
715 { |
|
716 warning |
|
717 ("preference of \"%s\" is obsolete -- use numeric value of %d instead", |
|
718 sval.c_str (), (val ? 1 : 0)); |
|
719 } |
|
720 |
2204
|
721 // Check the value of a string variable to see if it it's ok to do |
|
722 // something. |
|
723 // |
|
724 // return of 1 => always ok. |
|
725 // return of 0 => never ok. |
|
726 // return of -1 => ok, but give me warning (default). |
|
727 |
|
728 int |
3523
|
729 check_preference (const std::string& var) |
2204
|
730 { |
|
731 int pref = -1; |
|
732 |
3523
|
733 std::string val = builtin_string_variable (var); |
2204
|
734 |
|
735 if (val.empty ()) |
|
736 { |
|
737 double dval = 0; |
|
738 if (builtin_real_scalar_variable (var, dval)) |
|
739 pref = NINT (dval); |
|
740 } |
|
741 else |
|
742 { |
3565
|
743 if (val == "yes" || val == "true") |
2285
|
744 { |
|
745 warn_old_style_preference (true, val); |
|
746 pref = 1; |
|
747 } |
3565
|
748 else if (val == "never" || val == "no" || val == "false") |
2285
|
749 { |
|
750 warn_old_style_preference (false, val); |
|
751 pref = 0; |
|
752 } |
2204
|
753 } |
|
754 |
|
755 return pref; |
|
756 } |
|
757 |
3354
|
758 static void |
5275
|
759 check_dimensions (octave_idx_type& nr, octave_idx_type& nc, const char *warnfor) |
3354
|
760 { |
|
761 if (nr < 0 || nc < 0) |
|
762 { |
4457
|
763 if (Vwarn_neg_dim_as_zero) |
|
764 warning ("%s: converting negative dimension to zero", warnfor); |
3354
|
765 |
4457
|
766 nr = (nr < 0) ? 0 : nr; |
|
767 nc = (nc < 0) ? 0 : nc; |
3354
|
768 } |
|
769 } |
|
770 |
|
771 void |
4513
|
772 check_dimensions (dim_vector& dim, const char *warnfor) |
4481
|
773 { |
|
774 bool neg = false; |
|
775 |
|
776 for (int i = 0; i < dim.length (); i++) |
|
777 { |
|
778 if (dim(i) < 0) |
|
779 { |
|
780 dim(i) = 0; |
|
781 neg = true; |
|
782 } |
|
783 } |
|
784 |
|
785 if (neg && Vwarn_neg_dim_as_zero) |
4564
|
786 warning ("%s: converting negative dimension to zero", warnfor); |
4481
|
787 } |
|
788 |
|
789 |
|
790 void |
|
791 get_dimensions (const octave_value& a, const char *warn_for, |
4513
|
792 dim_vector& dim) |
4481
|
793 { |
|
794 if (a.is_scalar_type ()) |
|
795 { |
|
796 dim.resize (2); |
4732
|
797 dim(0) = a.int_value (); |
4481
|
798 dim(1) = dim(0); |
|
799 } |
|
800 else |
|
801 { |
5275
|
802 octave_idx_type nr = a.rows (); |
|
803 octave_idx_type nc = a.columns (); |
4481
|
804 |
|
805 if (nr == 1 || nc == 1) |
|
806 { |
|
807 Array<double> v = a.vector_value (); |
|
808 |
|
809 if (error_state) |
|
810 return; |
|
811 |
5275
|
812 octave_idx_type n = v.length (); |
4481
|
813 dim.resize (n); |
5275
|
814 for (octave_idx_type i = 0; i < n; i++) |
4783
|
815 dim(i) = static_cast<int> (fix (v(i))); |
4481
|
816 } |
|
817 else |
5257
|
818 error ("%s (A): use %s (size (A)) instead", warn_for, warn_for); |
4481
|
819 } |
|
820 |
5258
|
821 if (! error_state) |
|
822 check_dimensions (dim, warn_for); // May set error_state. |
4481
|
823 } |
|
824 |
|
825 |
|
826 void |
3354
|
827 get_dimensions (const octave_value& a, const char *warn_for, |
5275
|
828 octave_idx_type& nr, octave_idx_type& nc) |
3354
|
829 { |
|
830 if (a.is_scalar_type ()) |
|
831 { |
4732
|
832 nr = nc = a.int_value (); |
3354
|
833 } |
|
834 else |
|
835 { |
|
836 nr = a.rows (); |
|
837 nc = a.columns (); |
|
838 |
|
839 if ((nr == 1 && nc == 2) || (nr == 2 && nc == 1)) |
|
840 { |
3419
|
841 Array<double> v = a.vector_value (); |
3354
|
842 |
|
843 if (error_state) |
|
844 return; |
|
845 |
5275
|
846 nr = static_cast<octave_idx_type> (fix (v (0))); |
|
847 nc = static_cast<octave_idx_type> (fix (v (1))); |
3354
|
848 } |
|
849 else |
5257
|
850 error ("%s (A): use %s (size (A)) instead", warn_for, warn_for); |
3354
|
851 } |
|
852 |
5258
|
853 if (! error_state) |
|
854 check_dimensions (nr, nc, warn_for); // May set error_state. |
3354
|
855 } |
|
856 |
|
857 void |
|
858 get_dimensions (const octave_value& a, const octave_value& b, |
5275
|
859 const char *warn_for, octave_idx_type& nr, octave_idx_type& nc) |
3354
|
860 { |
4732
|
861 nr = a.is_empty () ? 0 : a.int_value (); |
|
862 nc = b.is_empty () ? 0 : b.int_value (); |
3354
|
863 |
|
864 if (error_state) |
|
865 error ("%s: expecting two scalar arguments", warn_for); |
|
866 else |
|
867 check_dimensions (nr, nc, warn_for); // May set error_state. |
|
868 } |
|
869 |
4478
|
870 Matrix |
5275
|
871 identity_matrix (octave_idx_type nr, octave_idx_type nc) |
4478
|
872 { |
|
873 Matrix m (nr, nc, 0.0); |
|
874 |
|
875 if (nr > 0 && nc > 0) |
|
876 { |
5275
|
877 octave_idx_type n = std::min (nr, nc); |
4478
|
878 |
5275
|
879 for (octave_idx_type i = 0; i < n; i++) |
4478
|
880 m (i, i) = 1.0; |
|
881 } |
|
882 |
|
883 return m; |
|
884 } |
|
885 |
3620
|
886 extern int |
3622
|
887 octave_format (std::ostream& os, const char *fmt, ...) |
3620
|
888 { |
|
889 int retval = -1; |
|
890 |
|
891 va_list args; |
|
892 va_start (args, fmt); |
|
893 |
|
894 retval = octave_vformat (os, fmt, args); |
|
895 |
|
896 va_end (args); |
|
897 |
|
898 return retval; |
|
899 } |
|
900 |
|
901 extern int |
3622
|
902 octave_vformat (std::ostream& os, const char *fmt, va_list args) |
3620
|
903 { |
|
904 int retval = -1; |
|
905 |
3775
|
906 #if defined (__GNUG__) && !CXX_ISO_COMPLIANT_LIBRARY |
3620
|
907 |
4135
|
908 std::streambuf *sb = os.rdbuf (); |
|
909 |
|
910 if (sb) |
4302
|
911 { |
|
912 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
913 |
|
914 retval = sb->vform (fmt, args); |
|
915 |
|
916 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
917 } |
3620
|
918 |
|
919 #else |
|
920 |
|
921 char *s = octave_vsnprintf (fmt, args); |
|
922 |
|
923 if (s) |
|
924 { |
|
925 os << s; |
|
926 |
|
927 retval = strlen (s); |
|
928 } |
|
929 |
|
930 #endif |
|
931 |
|
932 return retval; |
|
933 } |
|
934 |
4302
|
935 /* XXX FIXME XXX -- we really need a configure test for this. */ |
|
936 |
|
937 #if defined __GNUC__ && __GNUC__ >= 3 |
|
938 #define HAVE_C99_VSNPRINTF 1 |
|
939 #endif |
|
940 |
|
941 // We manage storage. User should not free it, and its contents are |
|
942 // only valid until next call to vsnprintf. |
|
943 |
|
944 // Interrupts might happen if someone makes a call with something that |
|
945 // will require a very large buffer. If we are interrupted in that |
|
946 // case, we should make the buffer size smaller for the next call. |
|
947 |
|
948 #define BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_FOR_VSNPRINTF \ |
|
949 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_1; \ |
|
950 delete [] buf; \ |
|
951 buf = 0; \ |
|
952 size = initial_size; \ |
|
953 octave_throw_interrupt_exception (); \ |
|
954 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_2 |
|
955 |
4485
|
956 #if defined __GNUC__ && defined __va_copy |
|
957 #define SAVE_ARGS(saved_args, args) __va_copy (saved_args, args) |
|
958 #elif defined va_copy |
|
959 #define SAVE_ARGS(saved_args, args) va_copy (saved_args, args) |
|
960 #else |
|
961 #define SAVE_ARGS(saved_args, args) saved_args = args |
|
962 #endif |
|
963 |
4302
|
964 char * |
|
965 octave_vsnprintf (const char *fmt, va_list args) |
|
966 { |
|
967 static const size_t initial_size = 100; |
|
968 |
|
969 static size_t size = initial_size; |
|
970 |
|
971 static char *buf = 0; |
|
972 |
4482
|
973 #if defined (HAVE_C99_VSNPRINTF) |
|
974 size_t nchars; |
|
975 #else |
4351
|
976 int nchars; |
4482
|
977 #endif |
4302
|
978 |
|
979 if (! buf) |
|
980 buf = new char [size]; |
|
981 |
|
982 if (! buf) |
|
983 return 0; |
|
984 |
|
985 #if defined (HAVE_C99_VSNPRINTF) |
|
986 |
4485
|
987 // Note that the caller is responsible for calling va_end on args. |
|
988 // We will do it for saved_args. |
|
989 |
|
990 va_list saved_args; |
|
991 |
|
992 SAVE_ARGS (saved_args, args); |
|
993 |
4302
|
994 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_FOR_VSNPRINTF; |
|
995 |
|
996 nchars = octave_raw_vsnprintf (buf, size, fmt, args); |
|
997 |
|
998 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
999 |
|
1000 if (nchars >= size) |
|
1001 { |
|
1002 size = nchars + 1; |
|
1003 |
|
1004 delete [] buf; |
|
1005 |
|
1006 buf = new char [size]; |
|
1007 |
|
1008 if (buf) |
|
1009 { |
|
1010 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_FOR_VSNPRINTF; |
|
1011 |
4485
|
1012 octave_raw_vsnprintf (buf, size, fmt, saved_args); |
4302
|
1013 |
|
1014 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
1015 } |
|
1016 } |
|
1017 |
4485
|
1018 va_end (saved_args); |
|
1019 |
4302
|
1020 #else |
|
1021 |
|
1022 while (1) |
|
1023 { |
4485
|
1024 va_list saved_args; |
|
1025 |
|
1026 SAVE_ARGS (saved_args, args); |
|
1027 |
4302
|
1028 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_FOR_VSNPRINTF; |
|
1029 |
4485
|
1030 nchars = octave_raw_vsnprintf (buf, size, fmt, saved_args); |
|
1031 |
|
1032 va_end (saved_args); |
4302
|
1033 |
|
1034 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
1035 |
4351
|
1036 if (nchars > -1 && nchars < size-1) |
4302
|
1037 return buf; |
|
1038 else |
|
1039 { |
|
1040 delete [] buf; |
|
1041 |
|
1042 size *= 2; |
|
1043 |
|
1044 buf = new char [size]; |
|
1045 |
|
1046 if (! buf) |
|
1047 return 0; |
|
1048 } |
|
1049 } |
|
1050 |
|
1051 #endif |
|
1052 |
|
1053 return buf; |
|
1054 } |
|
1055 |
|
1056 char * |
|
1057 octave_snprintf (const char *fmt, ...) |
|
1058 { |
|
1059 char *retval = 0; |
|
1060 |
|
1061 va_list args; |
|
1062 va_start (args, fmt); |
|
1063 |
|
1064 retval = octave_vsnprintf (fmt, args); |
|
1065 |
|
1066 va_end (args); |
|
1067 |
|
1068 return retval; |
|
1069 } |
|
1070 |
4086
|
1071 void |
|
1072 octave_sleep (double seconds) |
|
1073 { |
|
1074 if (seconds > 0) |
|
1075 { |
|
1076 double t; |
|
1077 |
4093
|
1078 unsigned int usec |
4783
|
1079 = static_cast<unsigned int> (modf (seconds, &t) * 1000000); |
4086
|
1080 |
4093
|
1081 unsigned int sec |
4783
|
1082 = (t > UINT_MAX) ? UINT_MAX : static_cast<unsigned int> (t); |
4086
|
1083 |
|
1084 octave_sleep (sec); |
|
1085 octave_usleep (usec); |
|
1086 } |
|
1087 } |
|
1088 |
3354
|
1089 static int |
4457
|
1090 warn_neg_dim_as_zero (void) |
3354
|
1091 { |
4457
|
1092 Vwarn_neg_dim_as_zero = check_preference ("warn_neg_dim_as_zero"); |
3354
|
1093 |
|
1094 return 0; |
|
1095 } |
|
1096 |
|
1097 void |
|
1098 symbols_of_utils (void) |
|
1099 { |
4457
|
1100 DEFVAR (warn_neg_dim_as_zero, false, warn_neg_dim_as_zero, |
3369
|
1101 "-*- texinfo -*-\n\ |
4457
|
1102 @defvr {Built-in Variable} warn_neg_dim_as_zero\n\ |
|
1103 If the value of @code{warn_neg_dim_as_zero} is nonzero, print a warning\n\ |
|
1104 for expressions like\n\ |
3369
|
1105 \n\ |
|
1106 @example\n\ |
|
1107 eye (-1)\n\ |
|
1108 @end example\n\ |
|
1109 \n\ |
|
1110 @noindent\n\ |
4457
|
1111 The default value is 0.\n\ |
3369
|
1112 @end defvr"); |
3354
|
1113 } |
|
1114 |
572
|
1115 /* |
1
|
1116 ;;; Local Variables: *** |
|
1117 ;;; mode: C++ *** |
|
1118 ;;; End: *** |
|
1119 */ |