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