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