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