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 |
679
|
209 #if 0 |
1
|
210 char ** |
|
211 pathstring_to_vector (char *pathstring) |
|
212 { |
526
|
213 static char **path = 0; |
1
|
214 |
526
|
215 if (pathstring) |
1
|
216 { |
|
217 int nelem = 0; |
240
|
218 char *tmp_path = strsave (pathstring); |
|
219 if (*tmp_path != '\0') |
1
|
220 { |
|
221 nelem++; |
240
|
222 char *ptr = tmp_path; |
1
|
223 while (*ptr != '\0') |
|
224 { |
|
225 if (*ptr == ':') |
|
226 nelem++; |
|
227 ptr++; |
|
228 } |
|
229 } |
|
230 |
240
|
231 char **foo = path; |
|
232 while (foo && *foo) |
244
|
233 delete [] *foo++; |
1
|
234 delete [] path; |
244
|
235 |
1
|
236 path = new char * [nelem+1]; |
526
|
237 path[nelem] = 0; |
1
|
238 |
|
239 int i = 0; |
240
|
240 char *ptr = tmp_path; |
1
|
241 while (i < nelem) |
|
242 { |
|
243 char *end = strchr (ptr, ':'); |
526
|
244 if (end) |
1
|
245 *end = '\0'; |
|
246 char *result = tilde_expand (ptr); |
|
247 path[i] = strsave (result); |
|
248 free (result); |
|
249 ptr = end + 1; |
|
250 i++; |
|
251 } |
|
252 |
240
|
253 delete [] tmp_path; |
1
|
254 } |
|
255 |
|
256 return path; |
|
257 } |
679
|
258 #endif |
1
|
259 |
581
|
260 // Return to the main command loop in octave.cc. |
|
261 |
189
|
262 void |
1
|
263 jump_to_top_level (void) |
|
264 { |
|
265 run_all_unwind_protects (); |
|
266 |
|
267 longjmp (toplevel, 1); |
|
268 } |
|
269 |
|
270 int |
526
|
271 almost_match (const char *std, const char *s, int min_match_len, |
|
272 int case_sens) |
1
|
273 { |
|
274 int stdlen = strlen (std); |
|
275 int slen = strlen (s); |
|
276 |
|
277 return (slen <= stdlen |
|
278 && slen >= min_match_len |
287
|
279 && (case_sens |
|
280 ? (strncmp (std, s, slen) == 0) |
|
281 : (strncasecmp (std, s, slen) == 0))); |
|
282 } |
|
283 |
581
|
284 // Ugh. |
|
285 |
287
|
286 int |
|
287 keyword_almost_match (const char **std, int *min_len, const char *s, |
|
288 int min_toks_to_match, int max_toks) |
|
289 { |
|
290 int status = 0; |
|
291 int tok_count = 0; |
|
292 int toks_matched = 0; |
|
293 |
526
|
294 if (! s || *s == '\0' || max_toks < 1) |
287
|
295 return status; |
|
296 |
|
297 char *kw = strsave (s); |
|
298 |
|
299 char *t = kw; |
|
300 while (*t != '\0') |
|
301 { |
|
302 if (*t == '\t') |
|
303 *t = ' '; |
|
304 t++; |
|
305 } |
|
306 |
|
307 char *beg = kw; |
|
308 while (*beg == ' ') |
|
309 beg++; |
|
310 |
|
311 if (*beg == '\0') |
|
312 return status; |
|
313 |
|
314 |
|
315 char **to_match = new char * [max_toks + 1]; |
526
|
316 const char **s1 = std; |
287
|
317 char **s2 = to_match; |
|
318 |
526
|
319 if (! s1 || ! s2) |
287
|
320 goto done; |
|
321 |
|
322 s2[tok_count] = beg; |
|
323 char *end; |
526
|
324 while ((end = strchr (beg, ' ')) != 0) |
287
|
325 { |
|
326 *end = '\0'; |
|
327 beg = end + 1; |
|
328 |
|
329 while (*beg == ' ') |
|
330 beg++; |
|
331 |
|
332 if (*beg == '\0') |
|
333 break; |
|
334 |
|
335 tok_count++; |
|
336 if (tok_count >= max_toks) |
|
337 goto done; |
|
338 |
|
339 s2[tok_count] = beg; |
|
340 } |
526
|
341 s2[tok_count+1] = 0; |
287
|
342 |
|
343 s2 = to_match; |
|
344 |
|
345 for (;;) |
|
346 { |
|
347 if (! almost_match (*s1, *s2, min_len[toks_matched], 0)) |
|
348 goto done; |
|
349 |
|
350 toks_matched++; |
|
351 |
|
352 s1++; |
|
353 s2++; |
|
354 |
|
355 if (! *s2) |
|
356 { |
|
357 status = (toks_matched >= min_toks_to_match); |
|
358 goto done; |
|
359 } |
|
360 |
|
361 if (! *s1) |
|
362 goto done; |
|
363 } |
|
364 |
|
365 done: |
|
366 |
|
367 delete [] kw; |
|
368 delete [] to_match; |
|
369 |
|
370 return status; |
1
|
371 } |
|
372 |
|
373 char ** |
338
|
374 get_fcn_file_names (int& num, const char *dir, int no_suffix) |
1
|
375 { |
|
376 static int num_max = 256; |
|
377 char **retval = new char * [num_max]; |
|
378 int i = 0; |
|
379 |
|
380 DIR *dirp = opendir (dir); |
526
|
381 if (dirp) |
1
|
382 { |
|
383 struct dirent *entry; |
526
|
384 while ((entry = readdir (dirp)) != 0) |
1
|
385 { |
|
386 int len = NLENGTH (entry); |
|
387 if (len > 2 |
|
388 && entry->d_name[len-2] == '.' |
|
389 && entry->d_name[len-1] == 'm') |
|
390 { |
|
391 retval[i] = strsave (entry->d_name); |
|
392 if (no_suffix) |
|
393 retval[i][len-2] = '\0'; |
|
394 |
|
395 i++; |
|
396 |
|
397 if (i == num_max - 1) |
|
398 { |
244
|
399 // Reallocate the array. Only copy pointers, not the strings they |
|
400 // point to, then only delete the original array of pointers, and not |
|
401 // the strings they point to. |
|
402 |
1
|
403 num_max += 256; |
|
404 char **tmp = new char * [num_max]; |
|
405 for (int j = 0; j < i; j++) |
|
406 tmp[j] = retval[j]; |
|
407 |
244
|
408 delete [] retval; |
|
409 |
1
|
410 retval = tmp; |
|
411 } |
|
412 } |
|
413 } |
106
|
414 closedir (dirp); |
1
|
415 } |
|
416 |
526
|
417 retval[i] = 0; |
1
|
418 num = i; |
|
419 |
|
420 return retval; |
|
421 } |
|
422 |
|
423 char ** |
338
|
424 get_fcn_file_names (int& num, int no_suffix) |
1
|
425 { |
|
426 static int num_max = 1024; |
|
427 char **retval = new char * [num_max]; |
|
428 int i = 0; |
|
429 |
679
|
430 char *path_elt = kpse_path_element (user_pref.loadpath); |
1
|
431 |
679
|
432 while (path_elt) |
1
|
433 { |
679
|
434 str_llist_type *elt_dirs = kpse_element_dirs (path_elt); |
|
435 |
|
436 str_llist_elt_type *dir; |
|
437 for (dir = *elt_dirs; dir; dir = STR_LLIST_NEXT (*dir)) |
1
|
438 { |
679
|
439 char *elt_dir = STR_LLIST (*dir); |
1
|
440 |
679
|
441 if (elt_dir) |
1
|
442 { |
679
|
443 int tmp_num; |
|
444 char **names = get_fcn_file_names (tmp_num, elt_dir, no_suffix); |
|
445 |
|
446 if (i + tmp_num >= num_max - 1) |
|
447 { |
244
|
448 // Reallocate the array. Only copy pointers, not the strings they |
|
449 // point to, then only delete the original array of pointers, and not |
|
450 // the strings they point to. |
|
451 |
679
|
452 num_max += 1024; |
|
453 char **tmp = new char * [num_max]; |
|
454 for (int j = 0; j < i; j++) |
|
455 tmp[j] = retval[j]; |
1
|
456 |
679
|
457 delete [] retval; |
244
|
458 |
679
|
459 retval = tmp; |
|
460 } |
1
|
461 |
679
|
462 int k = 0; |
|
463 while (k < tmp_num) |
|
464 retval[i++] = names[k++]; |
|
465 } |
|
466 } |
1
|
467 |
679
|
468 path_elt = kpse_path_element (0); |
1
|
469 } |
|
470 |
526
|
471 retval[i] = 0; |
1
|
472 num = i; |
|
473 |
|
474 return retval; |
|
475 } |
|
476 |
|
477 int |
|
478 NINT (double x) |
|
479 { |
|
480 if (x > INT_MAX) |
|
481 return INT_MAX; |
|
482 else if (x < INT_MIN) |
|
483 return INT_MIN; |
|
484 else |
|
485 return (x > 0) ? ((int) (x + 0.5)) : ((int) (x - 0.5)); |
|
486 } |
|
487 |
|
488 double |
|
489 D_NINT (double x) |
|
490 { |
|
491 if (xisinf (x) || xisnan (x)) |
|
492 return x; |
|
493 else |
|
494 return floor (x + 0.5); |
|
495 } |
|
496 |
526
|
497 // XXX FIXME XXX -- put these in some file, and make them extern. |
|
498 |
|
499 static int |
|
500 all_strings (const Octave_object& args) |
|
501 { |
|
502 int n = args.length (); |
|
503 for (int i = 1; i < n; i++) |
610
|
504 if (! args(i).is_string ()) |
526
|
505 return 0; |
|
506 return 1; |
|
507 } |
|
508 |
|
509 char ** |
578
|
510 make_argv (const Octave_object& args, const char *fcn_name) |
526
|
511 { |
|
512 char **argv = 0; |
|
513 if (all_strings (args)) |
|
514 { |
|
515 int n = args.length (); |
|
516 argv = new char * [n + 1]; |
578
|
517 argv[0] = strsave (fcn_name); |
526
|
518 for (int i = 1; i < n; i++) |
|
519 argv[i] = strsave (args(i).string_value ()); |
|
520 } |
|
521 else |
578
|
522 error ("%s: expecting all arguments to be strings", fcn_name); |
526
|
523 |
|
524 return argv; |
|
525 } |
|
526 |
628
|
527 int |
|
528 empty_arg (const char *name, int nr, int nc) |
|
529 { |
|
530 int is_empty = 0; |
|
531 |
|
532 if (nr == 0 || nc == 0) |
|
533 { |
|
534 int flag = user_pref.propagate_empty_matrices; |
|
535 |
|
536 if (flag < 0) |
636
|
537 { |
|
538 gripe_empty_arg (name, 0); |
|
539 is_empty = 1; |
|
540 } |
673
|
541 else if (flag == 0) |
636
|
542 { |
|
543 gripe_empty_arg (name, 1); |
|
544 is_empty = -1; |
|
545 } |
628
|
546 } |
|
547 |
|
548 return is_empty; |
|
549 } |
|
550 |
581
|
551 // Format a list in neat columns. Mostly stolen from GNU ls. This |
|
552 // should maybe be in utils.cc. |
|
553 |
526
|
554 ostrstream& |
|
555 list_in_columns (ostrstream& os, char **list) |
|
556 { |
|
557 // Compute the maximum name length. |
|
558 |
|
559 int max_name_length = 0; |
|
560 int total_names = 0; |
|
561 for (char **names = list; *names; names++) |
|
562 { |
|
563 total_names++; |
|
564 int name_length = strlen (*names); |
|
565 if (name_length > max_name_length) |
|
566 max_name_length = name_length; |
|
567 } |
|
568 |
|
569 // Allow at least two spaces between names. |
|
570 |
|
571 max_name_length += 2; |
|
572 |
|
573 // Calculate the maximum number of columns that will fit. |
|
574 |
|
575 int line_length = terminal_columns (); |
|
576 int cols = line_length / max_name_length; |
|
577 if (cols == 0) |
|
578 cols = 1; |
|
579 |
|
580 // Calculate the number of rows that will be in each column except |
|
581 // possibly for a short column on the right. |
|
582 |
|
583 int rows = total_names / cols + (total_names % cols != 0); |
|
584 |
|
585 // Recalculate columns based on rows. |
|
586 |
|
587 cols = total_names / rows + (total_names % rows != 0); |
|
588 |
|
589 names = list; |
|
590 int count; |
|
591 for (int row = 0; row < rows; row++) |
|
592 { |
|
593 count = row; |
|
594 int pos = 0; |
|
595 |
|
596 // Print the next row. |
|
597 |
|
598 while (1) |
|
599 { |
|
600 os << *(names + count); |
|
601 int name_length = strlen (*(names + count)); |
|
602 |
|
603 count += rows; |
|
604 if (count >= total_names) |
|
605 break; |
|
606 |
|
607 int spaces_to_pad = max_name_length - name_length; |
|
608 for (int i = 0; i < spaces_to_pad; i++) |
|
609 os << " "; |
|
610 pos += max_name_length; |
|
611 } |
|
612 os << "\n"; |
|
613 } |
|
614 |
|
615 return os; |
|
616 } |
|
617 |
581
|
618 // See if the given file is in the path. |
|
619 |
526
|
620 char * |
686
|
621 search_path_for_file (const char *path, const char *name) |
|
622 { |
|
623 char *retval = 0; |
|
624 |
|
625 char *tmp = kpse_path_search (path, name, kpathsea_true); |
|
626 |
|
627 if (tmp) |
|
628 { |
|
629 retval = make_absolute (tmp, the_current_working_directory); |
|
630 free (tmp); |
|
631 } |
|
632 |
|
633 return retval; |
|
634 } |
|
635 |
|
636 DEFUN ("file_in_path", Ffile_in_pat, Sfile_in_path, 3, 1, |
|
637 "file_in_path (PATH, NAME)") |
|
638 { |
|
639 Octave_object retval; |
|
640 |
|
641 DEFINE_ARGV("file_in_path"); |
|
642 |
|
643 if (argc == 3) |
|
644 { |
|
645 char *fname = search_path_for_file (argv[1], argv[2]); |
|
646 |
|
647 if (fname) |
|
648 retval = fname; |
|
649 else |
|
650 retval = Matrix (); |
|
651 } |
|
652 else |
|
653 print_usage ("file_in_path"); |
|
654 |
|
655 DELETE_ARGV; |
|
656 |
|
657 return retval; |
|
658 } |
|
659 |
|
660 |
|
661 char * |
526
|
662 file_in_path (const char *name, const char *suffix) |
|
663 { |
|
664 char *retval = 0; |
|
665 |
679
|
666 char *nm = strsave (name); |
686
|
667 |
526
|
668 if (suffix) |
|
669 { |
|
670 char *tmp = nm; |
|
671 nm = strconcat (tmp, suffix); |
|
672 delete [] tmp; |
|
673 } |
|
674 |
|
675 if (! the_current_working_directory) |
|
676 get_working_directory ("file_in_path"); |
|
677 |
686
|
678 retval = search_path_for_file (user_pref.loadpath, nm); |
526
|
679 |
686
|
680 delete [] nm; |
526
|
681 |
|
682 return retval; |
|
683 } |
|
684 |
581
|
685 // See if there is an function file in the path. If so, return the |
|
686 // full path to the file. |
|
687 |
526
|
688 char * |
|
689 fcn_file_in_path (const char *name) |
|
690 { |
|
691 return file_in_path (name, ".m"); |
|
692 } |
|
693 |
581
|
694 // See if there is an octave file in the path. If so, return the |
|
695 // full path to the file. |
|
696 |
572
|
697 char * |
|
698 oct_file_in_path (const char *name) |
|
699 { |
|
700 return file_in_path (name, ".oct"); |
|
701 } |
|
702 |
|
703 /* |
1
|
704 ;;; Local Variables: *** |
|
705 ;;; mode: C++ *** |
|
706 ;;; page-delimiter: "^/\\*" *** |
|
707 ;;; End: *** |
|
708 */ |