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