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