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