1
|
1 // utils.cc -*- C++ -*- |
|
2 /* |
|
3 |
296
|
4 Copyright (C) 1992, 1993, 1994 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 |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
1
|
26 #endif |
|
27 |
|
28 #include <sys/types.h> |
|
29 #ifdef HAVE_UNISTD_H |
|
30 #include <unistd.h> |
|
31 #endif |
|
32 #include <sys/param.h> |
|
33 #include <string.h> |
|
34 #include <limits.h> |
|
35 #include <iostream.h> |
|
36 #include <strstream.h> |
|
37 #include <fstream.h> |
367
|
38 |
526
|
39 #include <Complex.h> |
287
|
40 |
|
41 extern "C" |
|
42 { |
636
|
43 #include <setjmp.h> |
|
44 |
|
45 #ifndef HAVE_STRNCASECMP |
287
|
46 extern int strncasecmp (const char*, const char*, size_t); |
636
|
47 #endif |
287
|
48 |
679
|
49 #define boolean kpathsea_boolean |
|
50 #define false kpathsea_false |
|
51 #define true kpathsea_true |
|
52 #include <kpathsea/pathsearch.h> |
|
53 |
138
|
54 #if defined (HAVE_TERMIOS_H) |
|
55 #include <termios.h> |
|
56 #elif defined (HAVE_TERMIO_H) |
1
|
57 #include <termio.h> |
138
|
58 #elif defined (HAVE_SGTTY_H) |
1
|
59 #include <sgtty.h> |
|
60 #else |
|
61 LOSE! LOSE! |
|
62 #endif |
|
63 |
581
|
64 #include <readline/tilde.h> |
1
|
65 } |
|
66 |
526
|
67 // This mess suggested by the autoconf manual. |
|
68 // unistd.h defines _POSIX_VERSION on POSIX.1 systems. |
|
69 #if defined(DIRENT) || defined(_POSIX_VERSION) |
|
70 #include <dirent.h> |
|
71 #define NLENGTH(dirent) (strlen((dirent)->d_name)) |
|
72 #else /* not (DIRENT or _POSIX_VERSION) */ |
|
73 #define dirent direct |
|
74 #define NLENGTH(dirent) ((dirent)->d_namlen) |
|
75 #ifdef SYSNDIR |
|
76 #include <sys/ndir.h> |
|
77 #endif /* SYSNDIR */ |
|
78 #ifdef SYSDIR |
|
79 #include <sys/dir.h> |
|
80 #endif /* SYSDIR */ |
|
81 #ifdef NDIR |
|
82 #include <ndir.h> |
|
83 #endif /* NDIR */ |
|
84 #endif /* not (DIRENT or _POSIX_VERSION) */ |
|
85 |
1
|
86 #include "SLStack.h" |
|
87 |
|
88 #include "procstream.h" |
|
89 #include "user-prefs.h" |
|
90 #include "variables.h" |
526
|
91 #include "dirfns.h" |
686
|
92 #include "defun.h" |
1
|
93 #include "error.h" |
686
|
94 #include "help.h" |
628
|
95 #include "gripes.h" |
526
|
96 #include "pager.h" |
1
|
97 #include "utils.h" |
175
|
98 #include "input.h" |
1
|
99 #include "octave.h" |
526
|
100 #include "oct-obj.h" |
1
|
101 #include "mappers.h" |
|
102 #include "version.h" |
|
103 #include "tree-const.h" |
|
104 #include "unwind-prot.h" |
|
105 #include "octave-hist.h" |
|
106 |
|
107 // Top level context (?) |
|
108 extern jmp_buf toplevel; |
|
109 |
581
|
110 // Save a string. |
|
111 |
1
|
112 char * |
|
113 strsave (const char *s) |
|
114 { |
526
|
115 if (! s) |
|
116 return 0; |
1
|
117 |
|
118 int len = strlen (s); |
|
119 char *tmp = new char [len+1]; |
|
120 tmp = strcpy (tmp, s); |
|
121 return tmp; |
|
122 } |
|
123 |
581
|
124 // Concatenate two strings. |
|
125 |
1
|
126 char * |
|
127 strconcat (const char *s, const char *t) |
|
128 { |
|
129 int len = strlen (s) + strlen (t); |
|
130 char *tmp = new char [len+1]; |
|
131 strcpy (tmp, s); |
|
132 return strcat (tmp, t); |
|
133 } |
|
134 |
581
|
135 // Throw away input until a given character is read. |
|
136 |
1
|
137 void |
|
138 discard_until (istream& stream, char character) |
|
139 { |
|
140 int c; |
|
141 for (;;) |
|
142 { |
|
143 stream >> c; |
|
144 if (c == EOF || c == character) |
|
145 break; |
|
146 } |
|
147 if (c != EOF) |
|
148 stream.putback ((char) c); |
|
149 } |
|
150 |
607
|
151 #if 0 |
|
152 |
|
153 // XXX UNTESTED XXX |
|
154 |
|
155 // Read input until a given character is read. Returns characters |
|
156 // read in a new string that must be freed by the caller. |
|
157 |
|
158 char * |
|
159 read_until (istream& stream, char character) |
|
160 { |
|
161 int grow_size = 8; |
|
162 int limit = grow_size; |
|
163 char *buf = new char [limit]; |
|
164 char *bp = buf; |
|
165 |
|
166 get_more: |
|
167 is.getline (bp, limit, character); |
|
168 |
|
169 if (is.gcount () == 0) |
|
170 { |
|
171 delete [] buf; |
|
172 return 0; |
|
173 } |
|
174 |
|
175 if (is.gcount () == limit && buf[limit-1] != '\0') |
|
176 { |
|
177 char *tmp = new char [limit + grow_size]; |
|
178 strcpy (tmp, buf); |
|
179 delete [] buf; |
|
180 buf = tmp; |
|
181 bp = tmp + limit - 1; |
|
182 limit += grow_size; |
|
183 grow_size *= 2; |
|
184 goto get_more; |
|
185 } |
|
186 |
|
187 return buf; |
|
188 } |
|
189 #endif |
|
190 |
661
|
191 // Get a temporary file name. |
643
|
192 |
|
193 char * |
|
194 octave_tmp_file_name (void) |
|
195 { |
662
|
196 static char *retval = 0; |
|
197 |
|
198 if (retval) |
|
199 free (retval); |
|
200 |
|
201 retval = tempnam (0, "oct-"); |
|
202 |
661
|
203 if (! retval) |
|
204 error ("can't open temporary file!"); |
662
|
205 |
661
|
206 return retval; |
643
|
207 } |
|
208 |
1
|
209 char ** |
|
210 pathstring_to_vector (char *pathstring) |
|
211 { |
526
|
212 static char **path = 0; |
1
|
213 |
526
|
214 if (pathstring) |
1
|
215 { |
|
216 int nelem = 0; |
240
|
217 char *tmp_path = strsave (pathstring); |
|
218 if (*tmp_path != '\0') |
1
|
219 { |
|
220 nelem++; |
240
|
221 char *ptr = tmp_path; |
1
|
222 while (*ptr != '\0') |
|
223 { |
|
224 if (*ptr == ':') |
|
225 nelem++; |
|
226 ptr++; |
|
227 } |
|
228 } |
|
229 |
240
|
230 char **foo = path; |
|
231 while (foo && *foo) |
244
|
232 delete [] *foo++; |
1
|
233 delete [] path; |
244
|
234 |
1
|
235 path = new char * [nelem+1]; |
526
|
236 path[nelem] = 0; |
1
|
237 |
|
238 int i = 0; |
240
|
239 char *ptr = tmp_path; |
1
|
240 while (i < nelem) |
|
241 { |
|
242 char *end = strchr (ptr, ':'); |
526
|
243 if (end) |
1
|
244 *end = '\0'; |
|
245 char *result = tilde_expand (ptr); |
|
246 path[i] = strsave (result); |
|
247 free (result); |
|
248 ptr = end + 1; |
|
249 i++; |
|
250 } |
|
251 |
240
|
252 delete [] tmp_path; |
1
|
253 } |
|
254 |
|
255 return path; |
|
256 } |
|
257 |
581
|
258 // Return to the main command loop in octave.cc. |
|
259 |
189
|
260 void |
1
|
261 jump_to_top_level (void) |
|
262 { |
|
263 run_all_unwind_protects (); |
|
264 |
|
265 longjmp (toplevel, 1); |
|
266 } |
|
267 |
|
268 int |
526
|
269 almost_match (const char *std, const char *s, int min_match_len, |
|
270 int case_sens) |
1
|
271 { |
|
272 int stdlen = strlen (std); |
|
273 int slen = strlen (s); |
|
274 |
|
275 return (slen <= stdlen |
|
276 && slen >= min_match_len |
287
|
277 && (case_sens |
|
278 ? (strncmp (std, s, slen) == 0) |
|
279 : (strncasecmp (std, s, slen) == 0))); |
|
280 } |
|
281 |
581
|
282 // Ugh. |
|
283 |
287
|
284 int |
|
285 keyword_almost_match (const char **std, int *min_len, const char *s, |
|
286 int min_toks_to_match, int max_toks) |
|
287 { |
|
288 int status = 0; |
|
289 int tok_count = 0; |
|
290 int toks_matched = 0; |
|
291 |
526
|
292 if (! s || *s == '\0' || max_toks < 1) |
287
|
293 return status; |
|
294 |
|
295 char *kw = strsave (s); |
|
296 |
|
297 char *t = kw; |
|
298 while (*t != '\0') |
|
299 { |
|
300 if (*t == '\t') |
|
301 *t = ' '; |
|
302 t++; |
|
303 } |
|
304 |
|
305 char *beg = kw; |
|
306 while (*beg == ' ') |
|
307 beg++; |
|
308 |
|
309 if (*beg == '\0') |
|
310 return status; |
|
311 |
|
312 |
|
313 char **to_match = new char * [max_toks + 1]; |
526
|
314 const char **s1 = std; |
287
|
315 char **s2 = to_match; |
|
316 |
526
|
317 if (! s1 || ! s2) |
287
|
318 goto done; |
|
319 |
|
320 s2[tok_count] = beg; |
|
321 char *end; |
526
|
322 while ((end = strchr (beg, ' ')) != 0) |
287
|
323 { |
|
324 *end = '\0'; |
|
325 beg = end + 1; |
|
326 |
|
327 while (*beg == ' ') |
|
328 beg++; |
|
329 |
|
330 if (*beg == '\0') |
|
331 break; |
|
332 |
|
333 tok_count++; |
|
334 if (tok_count >= max_toks) |
|
335 goto done; |
|
336 |
|
337 s2[tok_count] = beg; |
|
338 } |
526
|
339 s2[tok_count+1] = 0; |
287
|
340 |
|
341 s2 = to_match; |
|
342 |
|
343 for (;;) |
|
344 { |
|
345 if (! almost_match (*s1, *s2, min_len[toks_matched], 0)) |
|
346 goto done; |
|
347 |
|
348 toks_matched++; |
|
349 |
|
350 s1++; |
|
351 s2++; |
|
352 |
|
353 if (! *s2) |
|
354 { |
|
355 status = (toks_matched >= min_toks_to_match); |
|
356 goto done; |
|
357 } |
|
358 |
|
359 if (! *s1) |
|
360 goto done; |
|
361 } |
|
362 |
|
363 done: |
|
364 |
|
365 delete [] kw; |
|
366 delete [] to_match; |
|
367 |
|
368 return status; |
1
|
369 } |
|
370 |
|
371 char ** |
338
|
372 get_fcn_file_names (int& num, const char *dir, int no_suffix) |
1
|
373 { |
|
374 static int num_max = 256; |
|
375 char **retval = new char * [num_max]; |
|
376 int i = 0; |
|
377 |
|
378 DIR *dirp = opendir (dir); |
526
|
379 if (dirp) |
1
|
380 { |
|
381 struct dirent *entry; |
526
|
382 while ((entry = readdir (dirp)) != 0) |
1
|
383 { |
|
384 int len = NLENGTH (entry); |
|
385 if (len > 2 |
|
386 && entry->d_name[len-2] == '.' |
|
387 && entry->d_name[len-1] == 'm') |
|
388 { |
|
389 retval[i] = strsave (entry->d_name); |
|
390 if (no_suffix) |
|
391 retval[i][len-2] = '\0'; |
|
392 |
|
393 i++; |
|
394 |
|
395 if (i == num_max - 1) |
|
396 { |
244
|
397 // Reallocate the array. Only copy pointers, not the strings they |
|
398 // point to, then only delete the original array of pointers, and not |
|
399 // the strings they point to. |
|
400 |
1
|
401 num_max += 256; |
|
402 char **tmp = new char * [num_max]; |
|
403 for (int j = 0; j < i; j++) |
|
404 tmp[j] = retval[j]; |
|
405 |
244
|
406 delete [] retval; |
|
407 |
1
|
408 retval = tmp; |
|
409 } |
|
410 } |
|
411 } |
106
|
412 closedir (dirp); |
1
|
413 } |
|
414 |
526
|
415 retval[i] = 0; |
1
|
416 num = i; |
|
417 |
|
418 return retval; |
|
419 } |
|
420 |
|
421 char ** |
338
|
422 get_fcn_file_names (int& num, int no_suffix) |
1
|
423 { |
|
424 static int num_max = 1024; |
|
425 char **retval = new char * [num_max]; |
|
426 int i = 0; |
|
427 |
679
|
428 char *path_elt = kpse_path_element (user_pref.loadpath); |
1
|
429 |
679
|
430 while (path_elt) |
1
|
431 { |
679
|
432 str_llist_type *elt_dirs = kpse_element_dirs (path_elt); |
|
433 |
|
434 str_llist_elt_type *dir; |
|
435 for (dir = *elt_dirs; dir; dir = STR_LLIST_NEXT (*dir)) |
1
|
436 { |
679
|
437 char *elt_dir = STR_LLIST (*dir); |
1
|
438 |
679
|
439 if (elt_dir) |
1
|
440 { |
679
|
441 int tmp_num; |
|
442 char **names = get_fcn_file_names (tmp_num, elt_dir, no_suffix); |
|
443 |
|
444 if (i + tmp_num >= num_max - 1) |
|
445 { |
244
|
446 // Reallocate the array. Only copy pointers, not the strings they |
|
447 // point to, then only delete the original array of pointers, and not |
|
448 // the strings they point to. |
|
449 |
679
|
450 num_max += 1024; |
|
451 char **tmp = new char * [num_max]; |
|
452 for (int j = 0; j < i; j++) |
|
453 tmp[j] = retval[j]; |
1
|
454 |
679
|
455 delete [] retval; |
244
|
456 |
679
|
457 retval = tmp; |
|
458 } |
1
|
459 |
679
|
460 int k = 0; |
|
461 while (k < tmp_num) |
|
462 retval[i++] = names[k++]; |
|
463 } |
|
464 } |
1
|
465 |
679
|
466 path_elt = kpse_path_element (0); |
1
|
467 } |
|
468 |
526
|
469 retval[i] = 0; |
1
|
470 num = i; |
|
471 |
|
472 return retval; |
|
473 } |
|
474 |
|
475 int |
|
476 NINT (double x) |
|
477 { |
|
478 if (x > INT_MAX) |
|
479 return INT_MAX; |
|
480 else if (x < INT_MIN) |
|
481 return INT_MIN; |
|
482 else |
|
483 return (x > 0) ? ((int) (x + 0.5)) : ((int) (x - 0.5)); |
|
484 } |
|
485 |
|
486 double |
|
487 D_NINT (double x) |
|
488 { |
|
489 if (xisinf (x) || xisnan (x)) |
|
490 return x; |
|
491 else |
|
492 return floor (x + 0.5); |
|
493 } |
|
494 |
526
|
495 // XXX FIXME XXX -- put these in some file, and make them extern. |
|
496 |
|
497 static int |
|
498 all_strings (const Octave_object& args) |
|
499 { |
|
500 int n = args.length (); |
712
|
501 for (int i = 0; i < n; i++) |
610
|
502 if (! args(i).is_string ()) |
526
|
503 return 0; |
|
504 return 1; |
|
505 } |
|
506 |
|
507 char ** |
578
|
508 make_argv (const Octave_object& args, const char *fcn_name) |
526
|
509 { |
|
510 char **argv = 0; |
|
511 if (all_strings (args)) |
|
512 { |
|
513 int n = args.length (); |
|
514 argv = new char * [n + 1]; |
578
|
515 argv[0] = strsave (fcn_name); |
712
|
516 for (int i = 0; i < n; i++) |
|
517 argv[i+1] = strsave (args(i).string_value ()); |
526
|
518 } |
|
519 else |
578
|
520 error ("%s: expecting all arguments to be strings", fcn_name); |
526
|
521 |
|
522 return argv; |
|
523 } |
|
524 |
719
|
525 // Return non-zero if either NR or NC is zero. Return -1 if this |
|
526 // should be considered fatal; return 1 if this is ok. |
|
527 |
628
|
528 int |
|
529 empty_arg (const char *name, int nr, int nc) |
|
530 { |
|
531 int is_empty = 0; |
|
532 |
|
533 if (nr == 0 || nc == 0) |
|
534 { |
|
535 int flag = user_pref.propagate_empty_matrices; |
|
536 |
|
537 if (flag < 0) |
636
|
538 { |
|
539 gripe_empty_arg (name, 0); |
|
540 is_empty = 1; |
|
541 } |
673
|
542 else if (flag == 0) |
636
|
543 { |
|
544 gripe_empty_arg (name, 1); |
|
545 is_empty = -1; |
|
546 } |
719
|
547 else |
|
548 is_empty = 1; |
628
|
549 } |
|
550 |
|
551 return is_empty; |
|
552 } |
|
553 |
581
|
554 // Format a list in neat columns. Mostly stolen from GNU ls. This |
|
555 // should maybe be in utils.cc. |
|
556 |
526
|
557 ostrstream& |
|
558 list_in_columns (ostrstream& os, char **list) |
|
559 { |
|
560 // Compute the maximum name length. |
|
561 |
|
562 int max_name_length = 0; |
|
563 int total_names = 0; |
|
564 for (char **names = list; *names; names++) |
|
565 { |
|
566 total_names++; |
|
567 int name_length = strlen (*names); |
|
568 if (name_length > max_name_length) |
|
569 max_name_length = name_length; |
|
570 } |
|
571 |
|
572 // Allow at least two spaces between names. |
|
573 |
|
574 max_name_length += 2; |
|
575 |
|
576 // Calculate the maximum number of columns that will fit. |
|
577 |
|
578 int line_length = terminal_columns (); |
|
579 int cols = line_length / max_name_length; |
|
580 if (cols == 0) |
|
581 cols = 1; |
|
582 |
|
583 // Calculate the number of rows that will be in each column except |
|
584 // possibly for a short column on the right. |
|
585 |
|
586 int rows = total_names / cols + (total_names % cols != 0); |
|
587 |
|
588 // Recalculate columns based on rows. |
|
589 |
|
590 cols = total_names / rows + (total_names % rows != 0); |
|
591 |
|
592 names = list; |
|
593 int count; |
|
594 for (int row = 0; row < rows; row++) |
|
595 { |
|
596 count = row; |
|
597 int pos = 0; |
|
598 |
|
599 // Print the next row. |
|
600 |
|
601 while (1) |
|
602 { |
|
603 os << *(names + count); |
|
604 int name_length = strlen (*(names + count)); |
|
605 |
|
606 count += rows; |
|
607 if (count >= total_names) |
|
608 break; |
|
609 |
|
610 int spaces_to_pad = max_name_length - name_length; |
|
611 for (int i = 0; i < spaces_to_pad; i++) |
|
612 os << " "; |
|
613 pos += max_name_length; |
|
614 } |
|
615 os << "\n"; |
|
616 } |
|
617 |
|
618 return os; |
|
619 } |
|
620 |
581
|
621 // See if the given file is in the path. |
|
622 |
526
|
623 char * |
686
|
624 search_path_for_file (const char *path, const char *name) |
|
625 { |
|
626 char *retval = 0; |
|
627 |
|
628 char *tmp = kpse_path_search (path, name, kpathsea_true); |
|
629 |
|
630 if (tmp) |
|
631 { |
|
632 retval = make_absolute (tmp, the_current_working_directory); |
|
633 free (tmp); |
|
634 } |
|
635 |
|
636 return retval; |
|
637 } |
|
638 |
|
639 DEFUN ("file_in_path", Ffile_in_pat, Sfile_in_path, 3, 1, |
|
640 "file_in_path (PATH, NAME)") |
|
641 { |
|
642 Octave_object retval; |
|
643 |
|
644 DEFINE_ARGV("file_in_path"); |
|
645 |
|
646 if (argc == 3) |
|
647 { |
|
648 char *fname = search_path_for_file (argv[1], argv[2]); |
|
649 |
|
650 if (fname) |
|
651 retval = fname; |
|
652 else |
|
653 retval = Matrix (); |
|
654 } |
|
655 else |
|
656 print_usage ("file_in_path"); |
|
657 |
|
658 DELETE_ARGV; |
|
659 |
|
660 return retval; |
|
661 } |
|
662 |
|
663 |
|
664 char * |
526
|
665 file_in_path (const char *name, const char *suffix) |
|
666 { |
|
667 char *retval = 0; |
|
668 |
679
|
669 char *nm = strsave (name); |
686
|
670 |
526
|
671 if (suffix) |
|
672 { |
|
673 char *tmp = nm; |
|
674 nm = strconcat (tmp, suffix); |
|
675 delete [] tmp; |
|
676 } |
|
677 |
|
678 if (! the_current_working_directory) |
|
679 get_working_directory ("file_in_path"); |
|
680 |
686
|
681 retval = search_path_for_file (user_pref.loadpath, nm); |
526
|
682 |
686
|
683 delete [] nm; |
526
|
684 |
|
685 return retval; |
|
686 } |
|
687 |
581
|
688 // See if there is an function file in the path. If so, return the |
|
689 // full path to the file. |
|
690 |
526
|
691 char * |
|
692 fcn_file_in_path (const char *name) |
|
693 { |
|
694 return file_in_path (name, ".m"); |
|
695 } |
|
696 |
581
|
697 // See if there is an octave file in the path. If so, return the |
|
698 // full path to the file. |
|
699 |
572
|
700 char * |
|
701 oct_file_in_path (const char *name) |
|
702 { |
|
703 return file_in_path (name, ".oct"); |
|
704 } |
|
705 |
|
706 /* |
1
|
707 ;;; Local Variables: *** |
|
708 ;;; mode: C++ *** |
|
709 ;;; page-delimiter: "^/\\*" *** |
|
710 ;;; End: *** |
|
711 */ |