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