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