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