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