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\ |
5814
|
272 the list of directories specified by @code{path}.\n\ |
4216
|
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\ |
5814
|
282 @seealso{file_in_path, path}\n\ |
5642
|
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\ |
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 |
|
389 print_usage ("file_in_path"); |
|
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 |
3174
|
402 return octave_env::make_absolute (Vload_path_dir_path.find (nm), |
|
403 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 { |
|
418 if (len > 2 && name [len - 2] == '.' && name [len - 1] == 'm') |
|
419 retval = file_in_path (name, ""); |
908
|
420 else |
1755
|
421 retval = file_in_path (name, ".m"); |
908
|
422 } |
1755
|
423 |
|
424 return retval; |
526
|
425 } |
|
426 |
581
|
427 // See if there is an octave file in the path. If so, return the |
|
428 // full path to the file. |
|
429 |
3536
|
430 std::string |
3523
|
431 oct_file_in_path (const std::string& name) |
572
|
432 { |
3523
|
433 std::string retval; |
908
|
434 |
1755
|
435 int len = name.length (); |
|
436 |
|
437 if (len > 0) |
|
438 { |
5612
|
439 if (len > 4 && name [len - 4] == '.' && name [len - 3] == 'o' |
908
|
440 && name [len - 2] == 'c' && name [len - 1] == 't') |
1755
|
441 retval = file_in_path (name, ""); |
908
|
442 else |
1755
|
443 retval = file_in_path (name, ".oct"); |
908
|
444 } |
1755
|
445 |
|
446 return retval; |
572
|
447 } |
|
448 |
3103
|
449 // Replace backslash escapes in a string with the real values. |
|
450 |
3536
|
451 std::string |
3523
|
452 do_string_escapes (const std::string& s) |
3103
|
453 { |
3523
|
454 std::string retval; |
3103
|
455 |
|
456 size_t i = 0; |
|
457 size_t j = 0; |
|
458 size_t len = s.length (); |
|
459 |
|
460 retval.resize (len); |
|
461 |
|
462 while (j < len) |
|
463 { |
|
464 if (s[j] == '\\' && j+1 < len) |
|
465 { |
|
466 switch (s[++j]) |
|
467 { |
3893
|
468 case '0': |
|
469 retval[i] = '\0'; |
|
470 break; |
|
471 |
3103
|
472 case 'a': |
|
473 retval[i] = '\a'; |
|
474 break; |
|
475 |
|
476 case 'b': // backspace |
|
477 retval[i] = '\b'; |
|
478 break; |
|
479 |
|
480 case 'f': // formfeed |
|
481 retval[i] = '\f'; |
|
482 break; |
|
483 |
|
484 case 'n': // newline |
|
485 retval[i] = '\n'; |
|
486 break; |
|
487 |
|
488 case 'r': // carriage return |
|
489 retval[i] = '\r'; |
|
490 break; |
|
491 |
|
492 case 't': // horizontal tab |
|
493 retval[i] = '\t'; |
|
494 break; |
|
495 |
|
496 case 'v': // vertical tab |
|
497 retval[i] = '\v'; |
|
498 break; |
|
499 |
|
500 case '\\': // backslash |
|
501 retval[i] = '\\'; |
|
502 break; |
|
503 |
|
504 case '\'': // quote |
|
505 retval[i] = '\''; |
|
506 break; |
|
507 |
|
508 case '"': // double quote |
|
509 retval[i] = '"'; |
|
510 break; |
|
511 |
|
512 default: |
|
513 warning ("unrecognized escape sequence `\\%c' --\ |
|
514 converting to `%c'", s[j], s[j]); |
|
515 retval[i] = s[j]; |
|
516 break; |
|
517 } |
|
518 } |
|
519 else |
|
520 { |
|
521 retval[i] = s[j]; |
|
522 } |
|
523 |
|
524 i++; |
|
525 j++; |
|
526 } |
|
527 |
3105
|
528 retval.resize (i); |
3103
|
529 |
|
530 return retval; |
|
531 } |
|
532 |
|
533 DEFUN (do_string_escapes, args, , |
3446
|
534 "-*- texinfo -*-\n\ |
|
535 @deftypefn {Built-in Function} {} do_string_escapes (@var{string})\n\ |
|
536 Convert special characters in @var{string} to their escaped forms.\n\ |
|
537 @end deftypefn") |
3103
|
538 { |
|
539 octave_value retval; |
|
540 |
|
541 int nargin = args.length (); |
|
542 |
|
543 if (nargin == 1) |
|
544 { |
|
545 if (args(0).is_string ()) |
|
546 retval = do_string_escapes (args(0).string_value ()); |
|
547 else |
|
548 error ("do_string_escapes: argument must be a string"); |
|
549 } |
|
550 else |
|
551 print_usage ("do_string_escapes"); |
|
552 |
|
553 return retval; |
|
554 } |
|
555 |
1755
|
556 const char * |
801
|
557 undo_string_escape (char c) |
|
558 { |
|
559 if (! c) |
1755
|
560 return ""; |
801
|
561 |
|
562 switch (c) |
|
563 { |
3893
|
564 case '\0': |
|
565 return "\\0"; |
|
566 |
801
|
567 case '\a': |
|
568 return "\\a"; |
|
569 |
|
570 case '\b': // backspace |
|
571 return "\\b"; |
|
572 |
|
573 case '\f': // formfeed |
|
574 return "\\f"; |
|
575 |
|
576 case '\n': // newline |
|
577 return "\\n"; |
|
578 |
|
579 case '\r': // carriage return |
|
580 return "\\r"; |
|
581 |
|
582 case '\t': // horizontal tab |
|
583 return "\\t"; |
|
584 |
|
585 case '\v': // vertical tab |
|
586 return "\\v"; |
|
587 |
|
588 case '\\': // backslash |
|
589 return "\\\\"; |
|
590 |
|
591 case '"': // double quote |
|
592 return "\\\""; |
|
593 |
|
594 default: |
1755
|
595 { |
|
596 static char retval[2]; |
|
597 retval[0] = c; |
|
598 retval[1] = '\0'; |
|
599 return retval; |
|
600 } |
801
|
601 } |
|
602 } |
|
603 |
3536
|
604 std::string |
3523
|
605 undo_string_escapes (const std::string& s) |
801
|
606 { |
3523
|
607 std::string retval; |
801
|
608 |
1755
|
609 for (size_t i = 0; i < s.length (); i++) |
|
610 retval.append (undo_string_escape (s[i])); |
801
|
611 |
1755
|
612 return retval; |
801
|
613 } |
|
614 |
1957
|
615 DEFUN (undo_string_escapes, args, , |
3361
|
616 "-*- texinfo -*-\n\ |
|
617 @deftypefn {Built-in Function} {} undo_string_escapes (@var{s})\n\ |
|
618 Converts special characters in strings back to their escaped forms. For\n\ |
|
619 example, the expression\n\ |
|
620 \n\ |
|
621 @example\n\ |
|
622 bell = \"\\a\";\n\ |
|
623 @end example\n\ |
|
624 \n\ |
|
625 @noindent\n\ |
|
626 assigns the value of the alert character (control-g, ASCII code 7) to\n\ |
|
627 the string variable @code{bell}. If this string is printed, the\n\ |
|
628 system will ring the terminal bell (if it is possible). This is\n\ |
|
629 normally the desired outcome. However, sometimes it is useful to be\n\ |
|
630 able to print the original representation of the string, with the\n\ |
|
631 special characters replaced by their escape sequences. For example,\n\ |
|
632 \n\ |
|
633 @example\n\ |
|
634 octave:13> undo_string_escapes (bell)\n\ |
|
635 ans = \\a\n\ |
|
636 @end example\n\ |
|
637 \n\ |
|
638 @noindent\n\ |
|
639 replaces the unprintable alert character with its printable\n\ |
|
640 representation.\n\ |
|
641 @end deftypefn") |
801
|
642 { |
2086
|
643 octave_value retval; |
801
|
644 |
|
645 int nargin = args.length (); |
|
646 |
3103
|
647 if (nargin == 1) |
|
648 { |
|
649 if (args(0).is_string ()) |
|
650 retval = undo_string_escapes (args(0).string_value ()); |
|
651 else |
|
652 error ("undo_string_escapes: argument must be a string"); |
|
653 } |
801
|
654 else |
1023
|
655 print_usage ("undo_string_escapes"); |
801
|
656 |
|
657 return retval; |
|
658 } |
|
659 |
4243
|
660 DEFUN (find_first_of_in_loadpath, args, , "") |
|
661 { |
|
662 octave_value retval; |
|
663 |
|
664 if (args.length () == 1) |
|
665 { |
|
666 string_vector names = args(0).all_strings (); |
|
667 |
|
668 if (! error_state) |
|
669 retval = Vload_path_dir_path.find_all_first_of (names); |
|
670 } |
|
671 else |
|
672 print_usage ("find_first_of_in_loadpath"); |
|
673 |
|
674 return retval; |
|
675 } |
|
676 |
5465
|
677 DEFUNX ("errno", Ferrno, args, , |
3716
|
678 "-*- texinfo -*-\n\ |
5465
|
679 @deftypefn {Built-in Function} {@var{err} =} errno ()\n\ |
|
680 @deftypefnx {Built-in Function} {@var{err} =} errno (@var{val})\n\ |
|
681 @deftypefnx {Built-in Function} {@var{err} =} errno (@var{name})\n\ |
|
682 Return the current value of the system-dependent variable errno,\n\ |
|
683 set its value to @var{val} and return the previous value, or return\n\ |
|
684 the named error code given @var{name} as a character string, or -1\n\ |
|
685 if @var{name} is not found.\n\ |
3716
|
686 @end deftypefn") |
|
687 { |
|
688 octave_value retval; |
|
689 |
5465
|
690 int nargin = args.length (); |
|
691 |
|
692 if (nargin == 1) |
|
693 { |
|
694 if (args(0).is_string ()) |
|
695 { |
|
696 std::string nm = args(0).string_value (); |
|
697 |
|
698 if (! error_state) |
|
699 retval = octave_errno::lookup (nm); |
|
700 else |
|
701 error ("errno: expecting character string argument"); |
|
702 } |
|
703 else |
|
704 { |
|
705 int val = args(0).int_value (); |
|
706 |
|
707 if (! error_state) |
|
708 retval = octave_errno::set (val); |
|
709 else |
|
710 error ("errno: expecting integer argument"); |
|
711 } |
|
712 } |
|
713 else if (nargin == 0) |
|
714 retval = octave_errno::get (); |
3716
|
715 else |
|
716 print_usage ("errno"); |
|
717 |
|
718 return retval; |
|
719 } |
|
720 |
5465
|
721 DEFUN (errno_list, args, , |
|
722 "-*- texinfo -*-\n\ |
|
723 @deftypefn {Built-in Function} {} errno_list ()\n\ |
|
724 Return a structure containing the system-dependent errno values.\n\ |
|
725 @end deftypefn") |
|
726 { |
|
727 octave_value retval; |
|
728 |
|
729 if (args.length () == 0) |
|
730 retval = octave_errno::list (); |
|
731 else |
|
732 print_usage ("errno_list"); |
|
733 |
|
734 return retval; |
|
735 } |
3716
|
736 |
2285
|
737 static void |
5275
|
738 check_dimensions (octave_idx_type& nr, octave_idx_type& nc, const char *warnfor) |
3354
|
739 { |
|
740 if (nr < 0 || nc < 0) |
|
741 { |
5781
|
742 warning_with_id ("Octave:neg-dim-as-zero", |
|
743 "%s: converting negative dimension to zero", warnfor); |
3354
|
744 |
4457
|
745 nr = (nr < 0) ? 0 : nr; |
|
746 nc = (nc < 0) ? 0 : nc; |
3354
|
747 } |
|
748 } |
|
749 |
|
750 void |
4513
|
751 check_dimensions (dim_vector& dim, const char *warnfor) |
4481
|
752 { |
|
753 bool neg = false; |
|
754 |
|
755 for (int i = 0; i < dim.length (); i++) |
|
756 { |
|
757 if (dim(i) < 0) |
|
758 { |
|
759 dim(i) = 0; |
|
760 neg = true; |
|
761 } |
|
762 } |
|
763 |
5781
|
764 if (neg) |
|
765 warning_with_id ("Octave:neg-dim-as-zero", |
|
766 "%s: converting negative dimension to zero", warnfor); |
4481
|
767 } |
|
768 |
|
769 |
|
770 void |
|
771 get_dimensions (const octave_value& a, const char *warn_for, |
4513
|
772 dim_vector& dim) |
4481
|
773 { |
|
774 if (a.is_scalar_type ()) |
|
775 { |
|
776 dim.resize (2); |
4732
|
777 dim(0) = a.int_value (); |
4481
|
778 dim(1) = dim(0); |
|
779 } |
|
780 else |
|
781 { |
5275
|
782 octave_idx_type nr = a.rows (); |
|
783 octave_idx_type nc = a.columns (); |
4481
|
784 |
|
785 if (nr == 1 || nc == 1) |
|
786 { |
|
787 Array<double> v = a.vector_value (); |
|
788 |
|
789 if (error_state) |
|
790 return; |
|
791 |
5275
|
792 octave_idx_type n = v.length (); |
4481
|
793 dim.resize (n); |
5275
|
794 for (octave_idx_type i = 0; i < n; i++) |
4783
|
795 dim(i) = static_cast<int> (fix (v(i))); |
4481
|
796 } |
|
797 else |
5257
|
798 error ("%s (A): use %s (size (A)) instead", warn_for, warn_for); |
4481
|
799 } |
|
800 |
5258
|
801 if (! error_state) |
|
802 check_dimensions (dim, warn_for); // May set error_state. |
4481
|
803 } |
|
804 |
|
805 |
|
806 void |
3354
|
807 get_dimensions (const octave_value& a, const char *warn_for, |
5275
|
808 octave_idx_type& nr, octave_idx_type& nc) |
3354
|
809 { |
|
810 if (a.is_scalar_type ()) |
|
811 { |
4732
|
812 nr = nc = a.int_value (); |
3354
|
813 } |
|
814 else |
|
815 { |
|
816 nr = a.rows (); |
|
817 nc = a.columns (); |
|
818 |
|
819 if ((nr == 1 && nc == 2) || (nr == 2 && nc == 1)) |
|
820 { |
3419
|
821 Array<double> v = a.vector_value (); |
3354
|
822 |
|
823 if (error_state) |
|
824 return; |
|
825 |
5275
|
826 nr = static_cast<octave_idx_type> (fix (v (0))); |
|
827 nc = static_cast<octave_idx_type> (fix (v (1))); |
3354
|
828 } |
|
829 else |
5257
|
830 error ("%s (A): use %s (size (A)) instead", warn_for, warn_for); |
3354
|
831 } |
|
832 |
5258
|
833 if (! error_state) |
|
834 check_dimensions (nr, nc, warn_for); // May set error_state. |
3354
|
835 } |
|
836 |
|
837 void |
|
838 get_dimensions (const octave_value& a, const octave_value& b, |
5275
|
839 const char *warn_for, octave_idx_type& nr, octave_idx_type& nc) |
3354
|
840 { |
4732
|
841 nr = a.is_empty () ? 0 : a.int_value (); |
|
842 nc = b.is_empty () ? 0 : b.int_value (); |
3354
|
843 |
|
844 if (error_state) |
|
845 error ("%s: expecting two scalar arguments", warn_for); |
|
846 else |
|
847 check_dimensions (nr, nc, warn_for); // May set error_state. |
|
848 } |
|
849 |
4478
|
850 Matrix |
5275
|
851 identity_matrix (octave_idx_type nr, octave_idx_type nc) |
4478
|
852 { |
|
853 Matrix m (nr, nc, 0.0); |
|
854 |
|
855 if (nr > 0 && nc > 0) |
|
856 { |
5275
|
857 octave_idx_type n = std::min (nr, nc); |
4478
|
858 |
5275
|
859 for (octave_idx_type i = 0; i < n; i++) |
4478
|
860 m (i, i) = 1.0; |
|
861 } |
|
862 |
|
863 return m; |
|
864 } |
|
865 |
3620
|
866 extern int |
3622
|
867 octave_format (std::ostream& os, const char *fmt, ...) |
3620
|
868 { |
|
869 int retval = -1; |
|
870 |
|
871 va_list args; |
|
872 va_start (args, fmt); |
|
873 |
|
874 retval = octave_vformat (os, fmt, args); |
|
875 |
|
876 va_end (args); |
|
877 |
|
878 return retval; |
|
879 } |
|
880 |
|
881 extern int |
3622
|
882 octave_vformat (std::ostream& os, const char *fmt, va_list args) |
3620
|
883 { |
|
884 int retval = -1; |
|
885 |
3775
|
886 #if defined (__GNUG__) && !CXX_ISO_COMPLIANT_LIBRARY |
3620
|
887 |
4135
|
888 std::streambuf *sb = os.rdbuf (); |
|
889 |
|
890 if (sb) |
4302
|
891 { |
|
892 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
893 |
|
894 retval = sb->vform (fmt, args); |
|
895 |
|
896 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
897 } |
3620
|
898 |
|
899 #else |
|
900 |
|
901 char *s = octave_vsnprintf (fmt, args); |
|
902 |
|
903 if (s) |
|
904 { |
|
905 os << s; |
|
906 |
|
907 retval = strlen (s); |
|
908 } |
|
909 |
|
910 #endif |
|
911 |
|
912 return retval; |
|
913 } |
|
914 |
5775
|
915 /* FIXME -- we really need a configure test for this. */ |
4302
|
916 |
|
917 #if defined __GNUC__ && __GNUC__ >= 3 |
|
918 #define HAVE_C99_VSNPRINTF 1 |
|
919 #endif |
|
920 |
|
921 // We manage storage. User should not free it, and its contents are |
|
922 // only valid until next call to vsnprintf. |
|
923 |
|
924 // Interrupts might happen if someone makes a call with something that |
|
925 // will require a very large buffer. If we are interrupted in that |
|
926 // case, we should make the buffer size smaller for the next call. |
|
927 |
|
928 #define BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_FOR_VSNPRINTF \ |
|
929 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_1; \ |
|
930 delete [] buf; \ |
|
931 buf = 0; \ |
|
932 size = initial_size; \ |
|
933 octave_throw_interrupt_exception (); \ |
|
934 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_2 |
|
935 |
4485
|
936 #if defined __GNUC__ && defined __va_copy |
|
937 #define SAVE_ARGS(saved_args, args) __va_copy (saved_args, args) |
|
938 #elif defined va_copy |
|
939 #define SAVE_ARGS(saved_args, args) va_copy (saved_args, args) |
|
940 #else |
|
941 #define SAVE_ARGS(saved_args, args) saved_args = args |
|
942 #endif |
|
943 |
4302
|
944 char * |
|
945 octave_vsnprintf (const char *fmt, va_list args) |
|
946 { |
|
947 static const size_t initial_size = 100; |
|
948 |
|
949 static size_t size = initial_size; |
|
950 |
|
951 static char *buf = 0; |
|
952 |
4482
|
953 #if defined (HAVE_C99_VSNPRINTF) |
|
954 size_t nchars; |
|
955 #else |
4351
|
956 int nchars; |
4482
|
957 #endif |
4302
|
958 |
|
959 if (! buf) |
|
960 buf = new char [size]; |
|
961 |
|
962 if (! buf) |
|
963 return 0; |
|
964 |
|
965 #if defined (HAVE_C99_VSNPRINTF) |
|
966 |
4485
|
967 // Note that the caller is responsible for calling va_end on args. |
|
968 // We will do it for saved_args. |
|
969 |
|
970 va_list saved_args; |
|
971 |
|
972 SAVE_ARGS (saved_args, args); |
|
973 |
4302
|
974 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_FOR_VSNPRINTF; |
|
975 |
|
976 nchars = octave_raw_vsnprintf (buf, size, fmt, args); |
|
977 |
|
978 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
979 |
|
980 if (nchars >= size) |
|
981 { |
|
982 size = nchars + 1; |
|
983 |
|
984 delete [] buf; |
|
985 |
|
986 buf = new char [size]; |
|
987 |
|
988 if (buf) |
|
989 { |
|
990 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_FOR_VSNPRINTF; |
|
991 |
4485
|
992 octave_raw_vsnprintf (buf, size, fmt, saved_args); |
4302
|
993 |
|
994 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
995 } |
|
996 } |
|
997 |
4485
|
998 va_end (saved_args); |
|
999 |
4302
|
1000 #else |
|
1001 |
|
1002 while (1) |
|
1003 { |
4485
|
1004 va_list saved_args; |
|
1005 |
|
1006 SAVE_ARGS (saved_args, args); |
|
1007 |
4302
|
1008 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_FOR_VSNPRINTF; |
|
1009 |
4485
|
1010 nchars = octave_raw_vsnprintf (buf, size, fmt, saved_args); |
|
1011 |
|
1012 va_end (saved_args); |
4302
|
1013 |
|
1014 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
1015 |
4351
|
1016 if (nchars > -1 && nchars < size-1) |
4302
|
1017 return buf; |
|
1018 else |
|
1019 { |
|
1020 delete [] buf; |
|
1021 |
|
1022 size *= 2; |
|
1023 |
|
1024 buf = new char [size]; |
|
1025 |
|
1026 if (! buf) |
|
1027 return 0; |
|
1028 } |
|
1029 } |
|
1030 |
|
1031 #endif |
|
1032 |
|
1033 return buf; |
|
1034 } |
|
1035 |
|
1036 char * |
|
1037 octave_snprintf (const char *fmt, ...) |
|
1038 { |
|
1039 char *retval = 0; |
|
1040 |
|
1041 va_list args; |
|
1042 va_start (args, fmt); |
|
1043 |
|
1044 retval = octave_vsnprintf (fmt, args); |
|
1045 |
|
1046 va_end (args); |
|
1047 |
|
1048 return retval; |
|
1049 } |
|
1050 |
4086
|
1051 void |
|
1052 octave_sleep (double seconds) |
|
1053 { |
|
1054 if (seconds > 0) |
|
1055 { |
|
1056 double t; |
|
1057 |
4093
|
1058 unsigned int usec |
4783
|
1059 = static_cast<unsigned int> (modf (seconds, &t) * 1000000); |
4086
|
1060 |
4093
|
1061 unsigned int sec |
4783
|
1062 = (t > UINT_MAX) ? UINT_MAX : static_cast<unsigned int> (t); |
4086
|
1063 |
5770
|
1064 // Versions of these functions that accept unsigned int args are |
|
1065 // defined in cutils.c. |
4086
|
1066 octave_sleep (sec); |
|
1067 octave_usleep (usec); |
|
1068 } |
|
1069 } |
|
1070 |
572
|
1071 /* |
1
|
1072 ;;; Local Variables: *** |
|
1073 ;;; mode: C++ *** |
|
1074 ;;; End: *** |
|
1075 */ |