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