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