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