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