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