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