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\ |
5456
|
294 @seealso{file_in_path, LOADPATH}") |
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\ |
5456
|
347 file_in_path (EXEC_PATH, \"sh\")\n\ |
|
348 @result{} \"/bin/sh\"\n\ |
3301
|
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\ |
5456
|
358 \n\ |
5458
|
359 Note that @code{file_in_path} does not expand leading, trailing,\n\ |
5456
|
360 or doubled colons the way that @code{file_in_loadpath} does. See\n\ |
|
361 also @xref{LOADPATH}, for an explanation of how colon expansion\n\ |
|
362 is applied to @code{LOADPATH}.\n\ |
4249
|
363 @seealso{file_in_loadpath}\n\ |
3301
|
364 @end deftypefn") |
686
|
365 { |
4216
|
366 octave_value retval; |
686
|
367 |
4243
|
368 int nargin = args.length (); |
1755
|
369 |
4243
|
370 if (nargin == 2 || nargin == 3) |
686
|
371 { |
4243
|
372 std::string path = args(0).string_value (); |
|
373 |
|
374 if (! error_state) |
|
375 { |
4249
|
376 string_vector names = args(1).all_strings (); |
4243
|
377 |
|
378 if (! error_state && names.length () > 0) |
|
379 { |
|
380 if (nargin == 2) |
|
381 { |
|
382 std::string fname = search_path_for_file (path, names); |
686
|
383 |
4243
|
384 if (fname.empty ()) |
|
385 retval = Matrix (); |
|
386 else |
|
387 retval = fname; |
|
388 } |
|
389 else if (nargin == 3) |
|
390 { |
4249
|
391 std::string opt = args(2).string_value (); |
4243
|
392 |
|
393 if (! error_state && opt == "all") |
|
394 retval = Cell (make_absolute (search_path_for_all_files (path, names))); |
|
395 else |
4249
|
396 error ("file_in_path: invalid option"); |
4243
|
397 } |
|
398 } |
|
399 else |
|
400 error ("file_in_path: expecting string as second argument"); |
|
401 } |
1755
|
402 else |
4243
|
403 error ("file_in_path: expecting string as first argument"); |
686
|
404 } |
|
405 else |
|
406 print_usage ("file_in_path"); |
|
407 |
|
408 return retval; |
|
409 } |
|
410 |
3536
|
411 std::string |
3523
|
412 file_in_path (const std::string& name, const std::string& suffix) |
526
|
413 { |
3523
|
414 std::string nm = name; |
526
|
415 |
1755
|
416 if (! suffix.empty ()) |
|
417 nm.append (suffix); |
686
|
418 |
3174
|
419 return octave_env::make_absolute (Vload_path_dir_path.find (nm), |
|
420 octave_env::getcwd ()); |
526
|
421 } |
|
422 |
581
|
423 // See if there is an function file in the path. If so, return the |
|
424 // full path to the file. |
|
425 |
3536
|
426 std::string |
3523
|
427 fcn_file_in_path (const std::string& name) |
526
|
428 { |
3523
|
429 std::string retval; |
908
|
430 |
1755
|
431 int len = name.length (); |
|
432 |
|
433 if (len > 0) |
|
434 { |
|
435 if (len > 2 && name [len - 2] == '.' && name [len - 1] == 'm') |
|
436 retval = file_in_path (name, ""); |
908
|
437 else |
1755
|
438 retval = file_in_path (name, ".m"); |
908
|
439 } |
1755
|
440 |
|
441 return retval; |
526
|
442 } |
|
443 |
581
|
444 // See if there is an octave file in the path. If so, return the |
|
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 { |
|
456 if (len > 2 && name [len - 4] == '.' && name [len - 3] == 'o' |
908
|
457 && name [len - 2] == 'c' && name [len - 1] == 't') |
1755
|
458 retval = file_in_path (name, ""); |
908
|
459 else |
1755
|
460 retval = file_in_path (name, ".oct"); |
908
|
461 } |
1755
|
462 |
|
463 return retval; |
572
|
464 } |
|
465 |
3103
|
466 // Replace backslash escapes in a string with the real values. |
|
467 |
3536
|
468 std::string |
3523
|
469 do_string_escapes (const std::string& s) |
3103
|
470 { |
3523
|
471 std::string retval; |
3103
|
472 |
|
473 size_t i = 0; |
|
474 size_t j = 0; |
|
475 size_t len = s.length (); |
|
476 |
|
477 retval.resize (len); |
|
478 |
|
479 while (j < len) |
|
480 { |
|
481 if (s[j] == '\\' && j+1 < len) |
|
482 { |
|
483 switch (s[++j]) |
|
484 { |
3893
|
485 case '0': |
|
486 retval[i] = '\0'; |
|
487 break; |
|
488 |
3103
|
489 case 'a': |
|
490 retval[i] = '\a'; |
|
491 break; |
|
492 |
|
493 case 'b': // backspace |
|
494 retval[i] = '\b'; |
|
495 break; |
|
496 |
|
497 case 'f': // formfeed |
|
498 retval[i] = '\f'; |
|
499 break; |
|
500 |
|
501 case 'n': // newline |
|
502 retval[i] = '\n'; |
|
503 break; |
|
504 |
|
505 case 'r': // carriage return |
|
506 retval[i] = '\r'; |
|
507 break; |
|
508 |
|
509 case 't': // horizontal tab |
|
510 retval[i] = '\t'; |
|
511 break; |
|
512 |
|
513 case 'v': // vertical tab |
|
514 retval[i] = '\v'; |
|
515 break; |
|
516 |
|
517 case '\\': // backslash |
|
518 retval[i] = '\\'; |
|
519 break; |
|
520 |
|
521 case '\'': // quote |
|
522 retval[i] = '\''; |
|
523 break; |
|
524 |
|
525 case '"': // double quote |
|
526 retval[i] = '"'; |
|
527 break; |
|
528 |
|
529 default: |
|
530 warning ("unrecognized escape sequence `\\%c' --\ |
|
531 converting to `%c'", s[j], s[j]); |
|
532 retval[i] = s[j]; |
|
533 break; |
|
534 } |
|
535 } |
|
536 else |
|
537 { |
|
538 retval[i] = s[j]; |
|
539 } |
|
540 |
|
541 i++; |
|
542 j++; |
|
543 } |
|
544 |
3105
|
545 retval.resize (i); |
3103
|
546 |
|
547 return retval; |
|
548 } |
|
549 |
|
550 DEFUN (do_string_escapes, args, , |
3446
|
551 "-*- texinfo -*-\n\ |
|
552 @deftypefn {Built-in Function} {} do_string_escapes (@var{string})\n\ |
|
553 Convert special characters in @var{string} to their escaped forms.\n\ |
|
554 @end deftypefn") |
3103
|
555 { |
|
556 octave_value retval; |
|
557 |
|
558 int nargin = args.length (); |
|
559 |
|
560 if (nargin == 1) |
|
561 { |
|
562 if (args(0).is_string ()) |
|
563 retval = do_string_escapes (args(0).string_value ()); |
|
564 else |
|
565 error ("do_string_escapes: argument must be a string"); |
|
566 } |
|
567 else |
|
568 print_usage ("do_string_escapes"); |
|
569 |
|
570 return retval; |
|
571 } |
|
572 |
1755
|
573 const char * |
801
|
574 undo_string_escape (char c) |
|
575 { |
|
576 if (! c) |
1755
|
577 return ""; |
801
|
578 |
|
579 switch (c) |
|
580 { |
3893
|
581 case '\0': |
|
582 return "\\0"; |
|
583 |
801
|
584 case '\a': |
|
585 return "\\a"; |
|
586 |
|
587 case '\b': // backspace |
|
588 return "\\b"; |
|
589 |
|
590 case '\f': // formfeed |
|
591 return "\\f"; |
|
592 |
|
593 case '\n': // newline |
|
594 return "\\n"; |
|
595 |
|
596 case '\r': // carriage return |
|
597 return "\\r"; |
|
598 |
|
599 case '\t': // horizontal tab |
|
600 return "\\t"; |
|
601 |
|
602 case '\v': // vertical tab |
|
603 return "\\v"; |
|
604 |
|
605 case '\\': // backslash |
|
606 return "\\\\"; |
|
607 |
|
608 case '"': // double quote |
|
609 return "\\\""; |
|
610 |
|
611 default: |
1755
|
612 { |
|
613 static char retval[2]; |
|
614 retval[0] = c; |
|
615 retval[1] = '\0'; |
|
616 return retval; |
|
617 } |
801
|
618 } |
|
619 } |
|
620 |
3536
|
621 std::string |
3523
|
622 undo_string_escapes (const std::string& s) |
801
|
623 { |
3523
|
624 std::string retval; |
801
|
625 |
1755
|
626 for (size_t i = 0; i < s.length (); i++) |
|
627 retval.append (undo_string_escape (s[i])); |
801
|
628 |
1755
|
629 return retval; |
801
|
630 } |
|
631 |
1957
|
632 DEFUN (undo_string_escapes, args, , |
3361
|
633 "-*- texinfo -*-\n\ |
|
634 @deftypefn {Built-in Function} {} undo_string_escapes (@var{s})\n\ |
|
635 Converts special characters in strings back to their escaped forms. For\n\ |
|
636 example, the expression\n\ |
|
637 \n\ |
|
638 @example\n\ |
|
639 bell = \"\\a\";\n\ |
|
640 @end example\n\ |
|
641 \n\ |
|
642 @noindent\n\ |
|
643 assigns the value of the alert character (control-g, ASCII code 7) to\n\ |
|
644 the string variable @code{bell}. If this string is printed, the\n\ |
|
645 system will ring the terminal bell (if it is possible). This is\n\ |
|
646 normally the desired outcome. However, sometimes it is useful to be\n\ |
|
647 able to print the original representation of the string, with the\n\ |
|
648 special characters replaced by their escape sequences. For example,\n\ |
|
649 \n\ |
|
650 @example\n\ |
|
651 octave:13> undo_string_escapes (bell)\n\ |
|
652 ans = \\a\n\ |
|
653 @end example\n\ |
|
654 \n\ |
|
655 @noindent\n\ |
|
656 replaces the unprintable alert character with its printable\n\ |
|
657 representation.\n\ |
|
658 @end deftypefn") |
801
|
659 { |
2086
|
660 octave_value retval; |
801
|
661 |
|
662 int nargin = args.length (); |
|
663 |
3103
|
664 if (nargin == 1) |
|
665 { |
|
666 if (args(0).is_string ()) |
|
667 retval = undo_string_escapes (args(0).string_value ()); |
|
668 else |
|
669 error ("undo_string_escapes: argument must be a string"); |
|
670 } |
801
|
671 else |
1023
|
672 print_usage ("undo_string_escapes"); |
801
|
673 |
|
674 return retval; |
|
675 } |
|
676 |
4243
|
677 DEFUN (find_first_of_in_loadpath, args, , "") |
|
678 { |
|
679 octave_value retval; |
|
680 |
|
681 if (args.length () == 1) |
|
682 { |
|
683 string_vector names = args(0).all_strings (); |
|
684 |
|
685 if (! error_state) |
|
686 retval = Vload_path_dir_path.find_all_first_of (names); |
|
687 } |
|
688 else |
|
689 print_usage ("find_first_of_in_loadpath"); |
|
690 |
|
691 return retval; |
|
692 } |
|
693 |
|
694 |
3879
|
695 // #if 0 |
3716
|
696 |
|
697 // Octave could use some way to access the value of ERRNO, but this is |
|
698 // probably not the best interface, so don't depend on it... |
|
699 |
|
700 DEFUN (ERRNO, args, , |
|
701 "-*- texinfo -*-\n\ |
|
702 @deftypefn {Built-in Function} {@var{system_error_number}} errno ()\n\ |
|
703 Return the current value of the system-dependent variable errno.\n\ |
|
704 @end deftypefn") |
|
705 { |
|
706 octave_value retval; |
|
707 |
|
708 if (args.length () == 0) |
4254
|
709 retval = errno; |
3716
|
710 else |
|
711 print_usage ("errno"); |
|
712 |
|
713 return retval; |
|
714 } |
|
715 |
3879
|
716 // #endif |
3716
|
717 |
2285
|
718 static void |
3523
|
719 warn_old_style_preference (bool val, const std::string& sval) |
2285
|
720 { |
|
721 warning |
|
722 ("preference of \"%s\" is obsolete -- use numeric value of %d instead", |
|
723 sval.c_str (), (val ? 1 : 0)); |
|
724 } |
|
725 |
2204
|
726 // Check the value of a string variable to see if it it's ok to do |
|
727 // something. |
|
728 // |
|
729 // return of 1 => always ok. |
|
730 // return of 0 => never ok. |
|
731 // return of -1 => ok, but give me warning (default). |
|
732 |
|
733 int |
3523
|
734 check_preference (const std::string& var) |
2204
|
735 { |
|
736 int pref = -1; |
|
737 |
3523
|
738 std::string val = builtin_string_variable (var); |
2204
|
739 |
|
740 if (val.empty ()) |
|
741 { |
|
742 double dval = 0; |
|
743 if (builtin_real_scalar_variable (var, dval)) |
|
744 pref = NINT (dval); |
|
745 } |
|
746 else |
|
747 { |
3565
|
748 if (val == "yes" || val == "true") |
2285
|
749 { |
|
750 warn_old_style_preference (true, val); |
|
751 pref = 1; |
|
752 } |
3565
|
753 else if (val == "never" || val == "no" || val == "false") |
2285
|
754 { |
|
755 warn_old_style_preference (false, val); |
|
756 pref = 0; |
|
757 } |
2204
|
758 } |
|
759 |
|
760 return pref; |
|
761 } |
|
762 |
3354
|
763 static void |
5275
|
764 check_dimensions (octave_idx_type& nr, octave_idx_type& nc, const char *warnfor) |
3354
|
765 { |
|
766 if (nr < 0 || nc < 0) |
|
767 { |
4457
|
768 if (Vwarn_neg_dim_as_zero) |
|
769 warning ("%s: converting negative dimension to zero", warnfor); |
3354
|
770 |
4457
|
771 nr = (nr < 0) ? 0 : nr; |
|
772 nc = (nc < 0) ? 0 : nc; |
3354
|
773 } |
|
774 } |
|
775 |
|
776 void |
4513
|
777 check_dimensions (dim_vector& dim, const char *warnfor) |
4481
|
778 { |
|
779 bool neg = false; |
|
780 |
|
781 for (int i = 0; i < dim.length (); i++) |
|
782 { |
|
783 if (dim(i) < 0) |
|
784 { |
|
785 dim(i) = 0; |
|
786 neg = true; |
|
787 } |
|
788 } |
|
789 |
|
790 if (neg && Vwarn_neg_dim_as_zero) |
4564
|
791 warning ("%s: converting negative dimension to zero", warnfor); |
4481
|
792 } |
|
793 |
|
794 |
|
795 void |
|
796 get_dimensions (const octave_value& a, const char *warn_for, |
4513
|
797 dim_vector& dim) |
4481
|
798 { |
|
799 if (a.is_scalar_type ()) |
|
800 { |
|
801 dim.resize (2); |
4732
|
802 dim(0) = a.int_value (); |
4481
|
803 dim(1) = dim(0); |
|
804 } |
|
805 else |
|
806 { |
5275
|
807 octave_idx_type nr = a.rows (); |
|
808 octave_idx_type nc = a.columns (); |
4481
|
809 |
|
810 if (nr == 1 || nc == 1) |
|
811 { |
|
812 Array<double> v = a.vector_value (); |
|
813 |
|
814 if (error_state) |
|
815 return; |
|
816 |
5275
|
817 octave_idx_type n = v.length (); |
4481
|
818 dim.resize (n); |
5275
|
819 for (octave_idx_type i = 0; i < n; i++) |
4783
|
820 dim(i) = static_cast<int> (fix (v(i))); |
4481
|
821 } |
|
822 else |
5257
|
823 error ("%s (A): use %s (size (A)) instead", warn_for, warn_for); |
4481
|
824 } |
|
825 |
5258
|
826 if (! error_state) |
|
827 check_dimensions (dim, warn_for); // May set error_state. |
4481
|
828 } |
|
829 |
|
830 |
|
831 void |
3354
|
832 get_dimensions (const octave_value& a, const char *warn_for, |
5275
|
833 octave_idx_type& nr, octave_idx_type& nc) |
3354
|
834 { |
|
835 if (a.is_scalar_type ()) |
|
836 { |
4732
|
837 nr = nc = a.int_value (); |
3354
|
838 } |
|
839 else |
|
840 { |
|
841 nr = a.rows (); |
|
842 nc = a.columns (); |
|
843 |
|
844 if ((nr == 1 && nc == 2) || (nr == 2 && nc == 1)) |
|
845 { |
3419
|
846 Array<double> v = a.vector_value (); |
3354
|
847 |
|
848 if (error_state) |
|
849 return; |
|
850 |
5275
|
851 nr = static_cast<octave_idx_type> (fix (v (0))); |
|
852 nc = static_cast<octave_idx_type> (fix (v (1))); |
3354
|
853 } |
|
854 else |
5257
|
855 error ("%s (A): use %s (size (A)) instead", warn_for, warn_for); |
3354
|
856 } |
|
857 |
5258
|
858 if (! error_state) |
|
859 check_dimensions (nr, nc, warn_for); // May set error_state. |
3354
|
860 } |
|
861 |
|
862 void |
|
863 get_dimensions (const octave_value& a, const octave_value& b, |
5275
|
864 const char *warn_for, octave_idx_type& nr, octave_idx_type& nc) |
3354
|
865 { |
4732
|
866 nr = a.is_empty () ? 0 : a.int_value (); |
|
867 nc = b.is_empty () ? 0 : b.int_value (); |
3354
|
868 |
|
869 if (error_state) |
|
870 error ("%s: expecting two scalar arguments", warn_for); |
|
871 else |
|
872 check_dimensions (nr, nc, warn_for); // May set error_state. |
|
873 } |
|
874 |
4478
|
875 Matrix |
5275
|
876 identity_matrix (octave_idx_type nr, octave_idx_type nc) |
4478
|
877 { |
|
878 Matrix m (nr, nc, 0.0); |
|
879 |
|
880 if (nr > 0 && nc > 0) |
|
881 { |
5275
|
882 octave_idx_type n = std::min (nr, nc); |
4478
|
883 |
5275
|
884 for (octave_idx_type i = 0; i < n; i++) |
4478
|
885 m (i, i) = 1.0; |
|
886 } |
|
887 |
|
888 return m; |
|
889 } |
|
890 |
3620
|
891 extern int |
3622
|
892 octave_format (std::ostream& os, const char *fmt, ...) |
3620
|
893 { |
|
894 int retval = -1; |
|
895 |
|
896 va_list args; |
|
897 va_start (args, fmt); |
|
898 |
|
899 retval = octave_vformat (os, fmt, args); |
|
900 |
|
901 va_end (args); |
|
902 |
|
903 return retval; |
|
904 } |
|
905 |
|
906 extern int |
3622
|
907 octave_vformat (std::ostream& os, const char *fmt, va_list args) |
3620
|
908 { |
|
909 int retval = -1; |
|
910 |
3775
|
911 #if defined (__GNUG__) && !CXX_ISO_COMPLIANT_LIBRARY |
3620
|
912 |
4135
|
913 std::streambuf *sb = os.rdbuf (); |
|
914 |
|
915 if (sb) |
4302
|
916 { |
|
917 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
918 |
|
919 retval = sb->vform (fmt, args); |
|
920 |
|
921 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
922 } |
3620
|
923 |
|
924 #else |
|
925 |
|
926 char *s = octave_vsnprintf (fmt, args); |
|
927 |
|
928 if (s) |
|
929 { |
|
930 os << s; |
|
931 |
|
932 retval = strlen (s); |
|
933 } |
|
934 |
|
935 #endif |
|
936 |
|
937 return retval; |
|
938 } |
|
939 |
4302
|
940 /* XXX FIXME XXX -- we really need a configure test for this. */ |
|
941 |
|
942 #if defined __GNUC__ && __GNUC__ >= 3 |
|
943 #define HAVE_C99_VSNPRINTF 1 |
|
944 #endif |
|
945 |
|
946 // We manage storage. User should not free it, and its contents are |
|
947 // only valid until next call to vsnprintf. |
|
948 |
|
949 // Interrupts might happen if someone makes a call with something that |
|
950 // will require a very large buffer. If we are interrupted in that |
|
951 // case, we should make the buffer size smaller for the next call. |
|
952 |
|
953 #define BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_FOR_VSNPRINTF \ |
|
954 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_1; \ |
|
955 delete [] buf; \ |
|
956 buf = 0; \ |
|
957 size = initial_size; \ |
|
958 octave_throw_interrupt_exception (); \ |
|
959 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_2 |
|
960 |
4485
|
961 #if defined __GNUC__ && defined __va_copy |
|
962 #define SAVE_ARGS(saved_args, args) __va_copy (saved_args, args) |
|
963 #elif defined va_copy |
|
964 #define SAVE_ARGS(saved_args, args) va_copy (saved_args, args) |
|
965 #else |
|
966 #define SAVE_ARGS(saved_args, args) saved_args = args |
|
967 #endif |
|
968 |
4302
|
969 char * |
|
970 octave_vsnprintf (const char *fmt, va_list args) |
|
971 { |
|
972 static const size_t initial_size = 100; |
|
973 |
|
974 static size_t size = initial_size; |
|
975 |
|
976 static char *buf = 0; |
|
977 |
4482
|
978 #if defined (HAVE_C99_VSNPRINTF) |
|
979 size_t nchars; |
|
980 #else |
4351
|
981 int nchars; |
4482
|
982 #endif |
4302
|
983 |
|
984 if (! buf) |
|
985 buf = new char [size]; |
|
986 |
|
987 if (! buf) |
|
988 return 0; |
|
989 |
|
990 #if defined (HAVE_C99_VSNPRINTF) |
|
991 |
4485
|
992 // Note that the caller is responsible for calling va_end on args. |
|
993 // We will do it for saved_args. |
|
994 |
|
995 va_list saved_args; |
|
996 |
|
997 SAVE_ARGS (saved_args, args); |
|
998 |
4302
|
999 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_FOR_VSNPRINTF; |
|
1000 |
|
1001 nchars = octave_raw_vsnprintf (buf, size, fmt, args); |
|
1002 |
|
1003 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
1004 |
|
1005 if (nchars >= size) |
|
1006 { |
|
1007 size = nchars + 1; |
|
1008 |
|
1009 delete [] buf; |
|
1010 |
|
1011 buf = new char [size]; |
|
1012 |
|
1013 if (buf) |
|
1014 { |
|
1015 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_FOR_VSNPRINTF; |
|
1016 |
4485
|
1017 octave_raw_vsnprintf (buf, size, fmt, saved_args); |
4302
|
1018 |
|
1019 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
1020 } |
|
1021 } |
|
1022 |
4485
|
1023 va_end (saved_args); |
|
1024 |
4302
|
1025 #else |
|
1026 |
|
1027 while (1) |
|
1028 { |
4485
|
1029 va_list saved_args; |
|
1030 |
|
1031 SAVE_ARGS (saved_args, args); |
|
1032 |
4302
|
1033 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_FOR_VSNPRINTF; |
|
1034 |
4485
|
1035 nchars = octave_raw_vsnprintf (buf, size, fmt, saved_args); |
|
1036 |
|
1037 va_end (saved_args); |
4302
|
1038 |
|
1039 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
1040 |
4351
|
1041 if (nchars > -1 && nchars < size-1) |
4302
|
1042 return buf; |
|
1043 else |
|
1044 { |
|
1045 delete [] buf; |
|
1046 |
|
1047 size *= 2; |
|
1048 |
|
1049 buf = new char [size]; |
|
1050 |
|
1051 if (! buf) |
|
1052 return 0; |
|
1053 } |
|
1054 } |
|
1055 |
|
1056 #endif |
|
1057 |
|
1058 return buf; |
|
1059 } |
|
1060 |
|
1061 char * |
|
1062 octave_snprintf (const char *fmt, ...) |
|
1063 { |
|
1064 char *retval = 0; |
|
1065 |
|
1066 va_list args; |
|
1067 va_start (args, fmt); |
|
1068 |
|
1069 retval = octave_vsnprintf (fmt, args); |
|
1070 |
|
1071 va_end (args); |
|
1072 |
|
1073 return retval; |
|
1074 } |
|
1075 |
4086
|
1076 void |
|
1077 octave_sleep (double seconds) |
|
1078 { |
|
1079 if (seconds > 0) |
|
1080 { |
|
1081 double t; |
|
1082 |
4093
|
1083 unsigned int usec |
4783
|
1084 = static_cast<unsigned int> (modf (seconds, &t) * 1000000); |
4086
|
1085 |
4093
|
1086 unsigned int sec |
4783
|
1087 = (t > UINT_MAX) ? UINT_MAX : static_cast<unsigned int> (t); |
4086
|
1088 |
|
1089 octave_sleep (sec); |
|
1090 octave_usleep (usec); |
|
1091 } |
|
1092 } |
|
1093 |
3354
|
1094 static int |
4457
|
1095 warn_neg_dim_as_zero (void) |
3354
|
1096 { |
4457
|
1097 Vwarn_neg_dim_as_zero = check_preference ("warn_neg_dim_as_zero"); |
3354
|
1098 |
|
1099 return 0; |
|
1100 } |
|
1101 |
|
1102 void |
|
1103 symbols_of_utils (void) |
|
1104 { |
4457
|
1105 DEFVAR (warn_neg_dim_as_zero, false, warn_neg_dim_as_zero, |
3369
|
1106 "-*- texinfo -*-\n\ |
4457
|
1107 @defvr {Built-in Variable} warn_neg_dim_as_zero\n\ |
|
1108 If the value of @code{warn_neg_dim_as_zero} is nonzero, print a warning\n\ |
|
1109 for expressions like\n\ |
3369
|
1110 \n\ |
|
1111 @example\n\ |
|
1112 eye (-1)\n\ |
|
1113 @end example\n\ |
|
1114 \n\ |
|
1115 @noindent\n\ |
4457
|
1116 The default value is 0.\n\ |
3369
|
1117 @end defvr"); |
3354
|
1118 } |
|
1119 |
572
|
1120 /* |
1
|
1121 ;;; Local Variables: *** |
|
1122 ;;; mode: C++ *** |
|
1123 ;;; End: *** |
|
1124 */ |