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