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