1
|
1 // utils.cc -*- C++ -*- |
|
2 /* |
|
3 |
1884
|
4 Copyright (C) 1996 John W. Eaton |
1
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
1
|
26 #endif |
|
27 |
1343
|
28 #include <climits> |
1345
|
29 #include <csetjmp> |
1346
|
30 #include <cstring> |
1343
|
31 |
1728
|
32 #include <string> |
|
33 |
1349
|
34 #include <fstream.h> |
|
35 #include <iostream.h> |
|
36 #include <strstream.h> |
|
37 |
1350
|
38 #ifdef HAVE_UNISTD_H |
1
|
39 #include <sys/types.h> |
|
40 #include <unistd.h> |
|
41 #endif |
367
|
42 |
138
|
43 #if defined (HAVE_TERMIOS_H) |
|
44 #include <termios.h> |
|
45 #elif defined (HAVE_TERMIO_H) |
1
|
46 #include <termio.h> |
138
|
47 #elif defined (HAVE_SGTTY_H) |
1
|
48 #include <sgtty.h> |
|
49 #else |
|
50 LOSE! LOSE! |
|
51 #endif |
|
52 |
1465
|
53 #ifndef HAVE_STRNCASECMP |
|
54 extern "C" int strncasecmp (const char*, const char*, size_t); |
|
55 #endif |
1
|
56 |
|
57 #include "SLStack.h" |
|
58 |
1651
|
59 #include "oct-cmplx.h" |
1755
|
60 #include "str-vec.h" |
1651
|
61 |
1352
|
62 #include "defun.h" |
1781
|
63 #include "dir-ops.h" |
1352
|
64 #include "dirfns.h" |
|
65 #include "error.h" |
|
66 #include "gripes.h" |
|
67 #include "help.h" |
|
68 #include "input.h" |
|
69 #include "mappers.h" |
1742
|
70 #include "oct-hist.h" |
1750
|
71 #include "oct-obj.h" |
1352
|
72 #include "pager.h" |
1155
|
73 #include "pathsearch.h" |
1690
|
74 #include "sysdep.h" |
1750
|
75 #include "toplev.h" |
1
|
76 #include "unwind-prot.h" |
1352
|
77 #include "user-prefs.h" |
|
78 #include "utils.h" |
|
79 #include "variables.h" |
1
|
80 |
|
81 // Top level context (?) |
|
82 extern jmp_buf toplevel; |
|
83 |
581
|
84 // Save a string. |
|
85 |
1
|
86 char * |
|
87 strsave (const char *s) |
|
88 { |
526
|
89 if (! s) |
|
90 return 0; |
1
|
91 |
|
92 int len = strlen (s); |
|
93 char *tmp = new char [len+1]; |
|
94 tmp = strcpy (tmp, s); |
|
95 return tmp; |
|
96 } |
|
97 |
581
|
98 // Return to the main command loop in octave.cc. |
|
99 |
1618
|
100 extern "C" void |
1
|
101 jump_to_top_level (void) |
|
102 { |
|
103 run_all_unwind_protects (); |
|
104 |
|
105 longjmp (toplevel, 1); |
|
106 } |
|
107 |
|
108 int |
1755
|
109 almost_match (const string& std, const string& s, int min_match_len, |
526
|
110 int case_sens) |
1
|
111 { |
1755
|
112 int stdlen = std.length (); |
|
113 int slen = s.length (); |
1
|
114 |
|
115 return (slen <= stdlen |
|
116 && slen >= min_match_len |
287
|
117 && (case_sens |
1755
|
118 ? (strncmp (std.c_str (), s.c_str (), slen) == 0) |
|
119 : (strncasecmp (std.c_str (), s.c_str (), slen) == 0))); |
287
|
120 } |
|
121 |
581
|
122 // Ugh. |
|
123 |
287
|
124 int |
1755
|
125 keyword_almost_match (const char **std, int *min_len, const string& s, |
287
|
126 int min_toks_to_match, int max_toks) |
|
127 { |
|
128 int status = 0; |
|
129 int tok_count = 0; |
|
130 int toks_matched = 0; |
|
131 |
1755
|
132 if (s.empty () || max_toks < 1) |
287
|
133 return status; |
|
134 |
1755
|
135 char *kw = strsave (s.c_str ()); |
287
|
136 |
|
137 char *t = kw; |
|
138 while (*t != '\0') |
|
139 { |
|
140 if (*t == '\t') |
|
141 *t = ' '; |
|
142 t++; |
|
143 } |
|
144 |
|
145 char *beg = kw; |
|
146 while (*beg == ' ') |
|
147 beg++; |
|
148 |
|
149 if (*beg == '\0') |
|
150 return status; |
|
151 |
|
152 |
|
153 char **to_match = new char * [max_toks + 1]; |
526
|
154 const char **s1 = std; |
287
|
155 char **s2 = to_match; |
|
156 |
526
|
157 if (! s1 || ! s2) |
287
|
158 goto done; |
|
159 |
|
160 s2[tok_count] = beg; |
|
161 char *end; |
526
|
162 while ((end = strchr (beg, ' ')) != 0) |
287
|
163 { |
|
164 *end = '\0'; |
|
165 beg = end + 1; |
|
166 |
|
167 while (*beg == ' ') |
|
168 beg++; |
|
169 |
|
170 if (*beg == '\0') |
|
171 break; |
|
172 |
|
173 tok_count++; |
|
174 if (tok_count >= max_toks) |
|
175 goto done; |
|
176 |
|
177 s2[tok_count] = beg; |
|
178 } |
526
|
179 s2[tok_count+1] = 0; |
287
|
180 |
|
181 s2 = to_match; |
|
182 |
|
183 for (;;) |
|
184 { |
|
185 if (! almost_match (*s1, *s2, min_len[toks_matched], 0)) |
|
186 goto done; |
|
187 |
|
188 toks_matched++; |
|
189 |
|
190 s1++; |
|
191 s2++; |
|
192 |
|
193 if (! *s2) |
|
194 { |
|
195 status = (toks_matched >= min_toks_to_match); |
|
196 goto done; |
|
197 } |
|
198 |
|
199 if (! *s1) |
|
200 goto done; |
|
201 } |
|
202 |
|
203 done: |
|
204 |
|
205 delete [] kw; |
|
206 delete [] to_match; |
|
207 |
|
208 return status; |
1
|
209 } |
|
210 |
1755
|
211 string_vector |
1781
|
212 get_fcn_file_names (const string& name, int no_suffix) |
1
|
213 { |
1781
|
214 string_vector retval; |
|
215 |
|
216 dir_entry dir (name); |
|
217 |
|
218 if (dir) |
|
219 { |
|
220 string_vector tmp = dir.read (); |
1
|
221 |
1781
|
222 int max_len = tmp.length (); |
|
223 |
|
224 retval.resize (max_len); |
|
225 |
|
226 int k = 0; |
|
227 int i; |
|
228 for (i = 0; i < max_len; i++) |
1
|
229 { |
1781
|
230 string entry = tmp[i]; |
|
231 |
|
232 int len = entry.length (); |
|
233 |
1664
|
234 #if defined (WITH_DYNAMIC_LINKING) |
826
|
235 if ((len > 2 |
1781
|
236 && entry[len-2] == '.' && entry[len-1] == 'm') |
826
|
237 || (len > 4 |
1781
|
238 && entry[len-4] == '.' && entry[len-3] == 'o' |
|
239 && entry[len-2] == 'c' && entry[len-1] == 't')) |
826
|
240 #else |
1
|
241 if (len > 2 |
1781
|
242 && entry[len-2] == '.' && entry[len-1] == 'm') |
826
|
243 #endif |
1
|
244 { |
|
245 if (no_suffix) |
826
|
246 { |
1781
|
247 if (entry[len-1] == 'm') |
|
248 entry.resize (len-2); |
826
|
249 else |
1781
|
250 entry.resize (len-4); |
826
|
251 } |
1
|
252 |
1781
|
253 retval[k++] = entry; |
1
|
254 } |
|
255 } |
1781
|
256 |
|
257 retval.resize (i); |
1
|
258 } |
|
259 |
|
260 return retval; |
|
261 } |
|
262 |
1755
|
263 string_vector |
1795
|
264 get_fcn_file_names (int no_suffix) |
1
|
265 { |
|
266 static int num_max = 1024; |
1787
|
267 |
1755
|
268 string_vector retval (num_max); |
1
|
269 |
1787
|
270 dir_path p (user_pref.loadpath); |
1
|
271 |
1787
|
272 string_vector dirs = p.all_directories (); |
679
|
273 |
1787
|
274 int len = dirs.length (); |
|
275 |
|
276 int k = 0; |
1
|
277 |
1787
|
278 for (int i = 0; i < len; i++) |
|
279 { |
|
280 string_vector names = get_fcn_file_names (dirs[i], no_suffix); |
679
|
281 |
1787
|
282 int tmp_num = names.length (); |
1
|
283 |
1787
|
284 if (k + tmp_num > num_max) |
|
285 { |
|
286 num_max += tmp_num; |
|
287 retval.resize (num_max); |
679
|
288 } |
1
|
289 |
1787
|
290 for (int j = 0; j < tmp_num; j++) |
|
291 retval[k++] = names[j++]; |
1
|
292 } |
|
293 |
1787
|
294 retval.resize (k); |
1
|
295 |
|
296 return retval; |
|
297 } |
|
298 |
1086
|
299 // Convert X to the nearest integer value. Should not pass NaN to |
|
300 // this function. |
|
301 |
1
|
302 int |
|
303 NINT (double x) |
|
304 { |
|
305 if (x > INT_MAX) |
|
306 return INT_MAX; |
|
307 else if (x < INT_MIN) |
|
308 return INT_MIN; |
|
309 else |
|
310 return (x > 0) ? ((int) (x + 0.5)) : ((int) (x - 0.5)); |
|
311 } |
|
312 |
|
313 double |
|
314 D_NINT (double x) |
|
315 { |
|
316 if (xisinf (x) || xisnan (x)) |
|
317 return x; |
|
318 else |
|
319 return floor (x + 0.5); |
|
320 } |
|
321 |
526
|
322 // XXX FIXME XXX -- put these in some file, and make them extern. |
|
323 |
|
324 static int |
|
325 all_strings (const Octave_object& args) |
|
326 { |
|
327 int n = args.length (); |
712
|
328 for (int i = 0; i < n; i++) |
610
|
329 if (! args(i).is_string ()) |
526
|
330 return 0; |
|
331 return 1; |
|
332 } |
|
333 |
1755
|
334 string_vector |
|
335 make_argv (const Octave_object& args, const string& fcn_name) |
526
|
336 { |
1755
|
337 string_vector argv; |
|
338 |
526
|
339 if (all_strings (args)) |
|
340 { |
|
341 int n = args.length (); |
1755
|
342 argv.resize (n+1); |
|
343 argv[0] = fcn_name; |
|
344 |
712
|
345 for (int i = 0; i < n; i++) |
1755
|
346 argv[i+1] = args(i).string_value (); |
526
|
347 } |
|
348 else |
1755
|
349 error ("%s: expecting all arguments to be strings", fcn_name.c_str ()); |
526
|
350 |
|
351 return argv; |
|
352 } |
|
353 |
719
|
354 // Return non-zero if either NR or NC is zero. Return -1 if this |
|
355 // should be considered fatal; return 1 if this is ok. |
|
356 |
628
|
357 int |
|
358 empty_arg (const char *name, int nr, int nc) |
|
359 { |
|
360 int is_empty = 0; |
|
361 |
|
362 if (nr == 0 || nc == 0) |
|
363 { |
|
364 int flag = user_pref.propagate_empty_matrices; |
|
365 |
|
366 if (flag < 0) |
636
|
367 { |
|
368 gripe_empty_arg (name, 0); |
|
369 is_empty = 1; |
|
370 } |
673
|
371 else if (flag == 0) |
636
|
372 { |
|
373 gripe_empty_arg (name, 1); |
|
374 is_empty = -1; |
|
375 } |
719
|
376 else |
|
377 is_empty = 1; |
628
|
378 } |
|
379 |
|
380 return is_empty; |
|
381 } |
|
382 |
581
|
383 // See if the given file is in the path. |
|
384 |
1755
|
385 string |
|
386 search_path_for_file (const string& path, const string& name) |
686
|
387 { |
1787
|
388 dir_path p (path); |
686
|
389 |
1787
|
390 return make_absolute (p.find (name), the_current_working_directory); |
686
|
391 } |
|
392 |
1957
|
393 DEFUN (file_in_path, args, , |
686
|
394 "file_in_path (PATH, NAME)") |
|
395 { |
|
396 Octave_object retval; |
|
397 |
1755
|
398 int argc = args.length () + 1; |
|
399 |
|
400 string_vector argv = make_argv (args, "file_in_path"); |
|
401 |
|
402 if (error_state) |
|
403 return retval; |
686
|
404 |
|
405 if (argc == 3) |
|
406 { |
1755
|
407 string fname = search_path_for_file (argv[1], argv[2]); |
686
|
408 |
1755
|
409 if (fname.empty ()) |
|
410 retval = Matrix (); |
|
411 else |
686
|
412 retval = fname; |
|
413 } |
|
414 else |
|
415 print_usage ("file_in_path"); |
|
416 |
|
417 return retval; |
|
418 } |
|
419 |
1755
|
420 string |
|
421 file_in_path (const string& name, const string& suffix) |
526
|
422 { |
1755
|
423 string nm = name; |
526
|
424 |
1755
|
425 if (! suffix.empty ()) |
|
426 nm.append (suffix); |
686
|
427 |
1755
|
428 if (the_current_working_directory.empty ()) |
526
|
429 get_working_directory ("file_in_path"); |
|
430 |
1755
|
431 return search_path_for_file (user_pref.loadpath, nm); |
526
|
432 } |
|
433 |
581
|
434 // See if there is an function file in the path. If so, return the |
|
435 // full path to the file. |
|
436 |
1755
|
437 string |
|
438 fcn_file_in_path (const string& name) |
526
|
439 { |
1755
|
440 string retval; |
908
|
441 |
1755
|
442 int len = name.length (); |
|
443 |
|
444 if (len > 0) |
|
445 { |
|
446 if (len > 2 && name [len - 2] == '.' && name [len - 1] == 'm') |
|
447 retval = file_in_path (name, ""); |
908
|
448 else |
1755
|
449 retval = file_in_path (name, ".m"); |
908
|
450 } |
1755
|
451 |
|
452 return retval; |
526
|
453 } |
|
454 |
581
|
455 // See if there is an octave file in the path. If so, return the |
|
456 // full path to the file. |
|
457 |
1755
|
458 string |
|
459 oct_file_in_path (const string& name) |
572
|
460 { |
1755
|
461 string retval; |
908
|
462 |
1755
|
463 int len = name.length (); |
|
464 |
|
465 if (len > 0) |
|
466 { |
|
467 if (len > 2 && name [len - 4] == '.' && name [len - 3] == 'o' |
908
|
468 && name [len - 2] == 'c' && name [len - 1] == 't') |
1755
|
469 retval = file_in_path (name, ""); |
908
|
470 else |
1755
|
471 retval = file_in_path (name, ".oct"); |
908
|
472 } |
1755
|
473 |
|
474 return retval; |
572
|
475 } |
|
476 |
1755
|
477 const char * |
801
|
478 undo_string_escape (char c) |
|
479 { |
|
480 if (! c) |
1755
|
481 return ""; |
801
|
482 |
|
483 switch (c) |
|
484 { |
|
485 case '\a': |
|
486 return "\\a"; |
|
487 |
|
488 case '\b': // backspace |
|
489 return "\\b"; |
|
490 |
|
491 case '\f': // formfeed |
|
492 return "\\f"; |
|
493 |
|
494 case '\n': // newline |
|
495 return "\\n"; |
|
496 |
|
497 case '\r': // carriage return |
|
498 return "\\r"; |
|
499 |
|
500 case '\t': // horizontal tab |
|
501 return "\\t"; |
|
502 |
|
503 case '\v': // vertical tab |
|
504 return "\\v"; |
|
505 |
|
506 case '\\': // backslash |
|
507 return "\\\\"; |
|
508 |
|
509 case '"': // double quote |
|
510 return "\\\""; |
|
511 |
|
512 default: |
1755
|
513 { |
|
514 static char retval[2]; |
|
515 retval[0] = c; |
|
516 retval[1] = '\0'; |
|
517 return retval; |
|
518 } |
801
|
519 } |
|
520 } |
|
521 |
1755
|
522 string |
|
523 undo_string_escapes (const string& s) |
801
|
524 { |
1755
|
525 string retval; |
801
|
526 |
1755
|
527 for (size_t i = 0; i < s.length (); i++) |
|
528 retval.append (undo_string_escape (s[i])); |
801
|
529 |
1755
|
530 return retval; |
801
|
531 } |
|
532 |
1957
|
533 DEFUN (undo_string_escapes, args, , |
801
|
534 "undo_string_escapes (STRING)") |
|
535 { |
|
536 tree_constant retval; |
|
537 |
|
538 int nargin = args.length (); |
|
539 |
|
540 if (nargin == 1 && args(0).is_string ()) |
1755
|
541 retval = undo_string_escapes (args(0).string_value ()); |
801
|
542 else |
1023
|
543 print_usage ("undo_string_escapes"); |
801
|
544 |
|
545 return retval; |
|
546 } |
|
547 |
1711
|
548 // This function was adapted from xputenv from Karl Berry's kpathsearch |
|
549 // library. |
|
550 |
|
551 void |
|
552 oct_putenv (const char *var_name, const char *value) |
|
553 { |
|
554 static const char **saved_env_items = 0; |
|
555 static unsigned saved_len; |
|
556 char *old_item = 0; |
|
557 |
|
558 int new_len = strlen (var_name) + strlen (value) + 2; |
|
559 |
|
560 char *new_item = new char [new_len]; |
|
561 |
|
562 sprintf (new_item, "%s=%s", var_name, value); |
|
563 |
|
564 #ifndef SMART_PUTENV |
|
565 |
|
566 // Check if we have saved anything yet. |
|
567 |
|
568 if (! saved_env_items) |
|
569 { |
|
570 saved_env_items = new const char * [1]; |
|
571 saved_env_items[0] = var_name; |
|
572 saved_len = 1; |
|
573 } |
|
574 else |
|
575 { |
|
576 // Check if we've assigned VAR_NAME before. |
|
577 |
|
578 unsigned len = strlen (var_name); |
|
579 |
|
580 for (unsigned i = 0; i < saved_len && ! old_item; i++) |
|
581 { |
|
582 if (strcmp (saved_env_items[i], var_name) == 0) |
|
583 { |
|
584 old_item = getenv (var_name); |
|
585 |
|
586 assert (old_item); |
|
587 |
|
588 // Back up to the `NAME=' in the environment before the |
|
589 // value that getenv returns. |
|
590 |
|
591 old_item -= (len + 1); |
|
592 } |
|
593 } |
|
594 |
|
595 if (! old_item) |
|
596 { |
|
597 // If we haven't seen VAR_NAME before, save it. Assume it |
|
598 // is in safe storage. |
|
599 |
|
600 saved_len++; |
|
601 |
|
602 const char **tmp = new const char * [saved_len]; |
|
603 |
|
604 for (unsigned i = 0; i < saved_len - 1; i++) |
|
605 tmp[i] = saved_env_items[i]; |
|
606 |
|
607 tmp[saved_len - 1] = var_name; |
|
608 |
|
609 delete [] saved_env_items; |
|
610 |
|
611 saved_env_items = tmp; |
|
612 } |
|
613 } |
|
614 |
|
615 #endif |
|
616 |
|
617 // As far as I can see there's no way to distinguish between the |
|
618 // various errors; putenv doesn't have errno values. |
|
619 |
|
620 if (putenv (new_item) < 0) |
|
621 error ("putenv (%s) failed", new_item); |
|
622 |
|
623 #ifndef SMART_PUTENV |
|
624 |
|
625 // Can't free `new_item' because its contained value is now in |
|
626 // `environ', but we can free `old_item', since it's been replaced. |
|
627 |
|
628 delete [] old_item; |
|
629 |
|
630 #endif |
|
631 } |
|
632 |
572
|
633 /* |
1
|
634 ;;; Local Variables: *** |
|
635 ;;; mode: C++ *** |
|
636 ;;; page-delimiter: "^/\\*" *** |
|
637 ;;; End: *** |
|
638 */ |