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