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 |
|
26 The 11 functions listed below were adapted from a similar functions |
|
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 <time.h> |
|
53 #include <math.h> |
|
54 #include <limits.h> |
|
55 #include <iostream.h> |
|
56 #include <strstream.h> |
|
57 #include <fstream.h> |
367
|
58 |
|
59 // This mess suggested by the autoconf manual. |
|
60 // unistd.h defines _POSIX_VERSION on POSIX.1 systems. |
|
61 #if defined(DIRENT) || defined(_POSIX_VERSION) |
1
|
62 #include <dirent.h> |
367
|
63 #define NLENGTH(dirent) (strlen((dirent)->d_name)) |
|
64 #else /* not (DIRENT or _POSIX_VERSION) */ |
|
65 #define dirent direct |
|
66 #define NLENGTH(dirent) ((dirent)->d_namlen) |
|
67 #ifdef SYSNDIR |
|
68 #include <sys/ndir.h> |
|
69 #endif /* SYSNDIR */ |
|
70 #ifdef SYSDIR |
|
71 #include <sys/dir.h> |
|
72 #endif /* SYSDIR */ |
|
73 #ifdef NDIR |
|
74 #include <ndir.h> |
|
75 #endif /* NDIR */ |
|
76 #endif /* not (DIRENT or _POSIX_VERSION) */ |
1
|
77 |
287
|
78 #ifndef HAVE_STRCASECMP |
|
79 extern "C" |
|
80 { |
|
81 extern int strcasecmp (const char*, const char*); |
|
82 } |
|
83 #endif |
|
84 |
|
85 #ifndef HAVE_STRNCASECMP |
|
86 extern "C" |
|
87 { |
|
88 extern int strncasecmp (const char*, const char*, size_t); |
|
89 } |
|
90 #endif |
|
91 |
138
|
92 extern "C" |
|
93 { |
|
94 #if defined (HAVE_TERMIOS_H) |
|
95 #include <termios.h> |
|
96 #elif defined (HAVE_TERMIO_H) |
1
|
97 #include <termio.h> |
138
|
98 #elif defined (HAVE_SGTTY_H) |
1
|
99 #include <sgtty.h> |
|
100 #else |
|
101 LOSE! LOSE! |
|
102 #endif |
|
103 |
138
|
104 extern int ioctl (int, int, ...); |
|
105 char *tilde_expand (char *s); /* From readline's tilde.c */ |
1
|
106 } |
|
107 |
|
108 #include "SLStack.h" |
|
109 |
|
110 #include "statdefs.h" |
|
111 #include "procstream.h" |
|
112 #include "user-prefs.h" |
|
113 #include "variables.h" |
|
114 #include "error.h" |
|
115 #include "utils.h" |
175
|
116 #include "input.h" |
1
|
117 #include "octave.h" |
|
118 #include "mappers.h" |
|
119 #include "version.h" |
11
|
120 #include "defaults.h" |
1
|
121 #include "tree-const.h" |
|
122 #include "unwind-prot.h" |
|
123 #include "octave-hist.h" |
|
124 |
138
|
125 #ifndef STDIN_FILENO |
|
126 #define STDIN_FILENO 1 |
|
127 #endif |
|
128 |
1
|
129 // Top level context (?) |
|
130 extern jmp_buf toplevel; |
|
131 |
|
132 // Pipe to gnuplot. |
|
133 static oprocstream plot_stream; |
|
134 |
|
135 // Non-zero means follow symbolic links that point to directories just |
|
136 // as if they are real directories. |
|
137 static int follow_symbolic_links = 1; |
|
138 |
|
139 #ifndef MAXPATHLEN |
|
140 #define MAXPATHLEN 1024 |
|
141 #endif |
|
142 |
|
143 // The size that strings change by. |
|
144 #ifndef DEFAULT_ARRAY_SIZE |
|
145 #define DEFAULT_ARRAY_SIZE 512 |
|
146 #endif |
|
147 |
|
148 // The growth rate for the prompt string. |
|
149 #ifndef PROMPT_GROWTH |
|
150 #define PROMPT_GROWTH 50 |
|
151 #endif |
|
152 |
|
153 #ifndef MAX |
|
154 #define MAX(a,b) ((a) > (b) ? (a) : (b)) |
|
155 #endif |
|
156 |
|
157 #ifndef MIN |
|
158 #define MIN(a,b) ((a) < (b) ? (a) : (b)) |
|
159 #endif |
|
160 |
|
161 // Where to find the site-wide configuration file |
|
162 #ifndef OCTAVE_HOME |
|
163 #define OCTAVE_HOME "/usr/local" |
|
164 #endif |
|
165 |
|
166 // Temp storage for a path. |
|
167 static char tdir[MAXPATHLEN]; |
|
168 |
|
169 // List of files to delete when we exit or crash. |
|
170 static SLStack <char *> tmp_files; |
|
171 |
|
172 /* |
|
173 * Save a string. |
|
174 */ |
|
175 char * |
|
176 strsave (const char *s) |
|
177 { |
|
178 if (s == (char *) NULL) |
|
179 return (char *) NULL; |
|
180 |
|
181 int len = strlen (s); |
|
182 char *tmp = new char [len+1]; |
|
183 tmp = strcpy (tmp, s); |
|
184 return tmp; |
|
185 } |
|
186 |
|
187 /* |
|
188 * Concatenate two strings. |
|
189 */ |
|
190 char * |
|
191 strconcat (const char *s, const char *t) |
|
192 { |
|
193 int len = strlen (s) + strlen (t); |
|
194 char *tmp = new char [len+1]; |
|
195 strcpy (tmp, s); |
|
196 return strcat (tmp, t); |
|
197 } |
|
198 |
|
199 /* |
|
200 * Throw away input until a given character is read. |
|
201 */ |
|
202 void |
|
203 discard_until (istream& stream, char character) |
|
204 { |
|
205 int c; |
|
206 for (;;) |
|
207 { |
|
208 stream >> c; |
|
209 if (c == EOF || c == character) |
|
210 break; |
|
211 } |
|
212 if (c != EOF) |
|
213 stream.putback ((char) c); |
|
214 } |
|
215 |
|
216 void |
164
|
217 check_dimensions (int& nr, int& nc, const char *warnfor) |
1
|
218 { |
|
219 if (nr < 0 || nc < 0) |
|
220 { |
|
221 if (user_pref.treat_neg_dim_as_zero) |
|
222 nr = nc = 0; |
|
223 else |
143
|
224 error ("%s: can't create a matrix with negative dimensions", |
|
225 warnfor); |
1
|
226 } |
|
227 } |
|
228 |
|
229 /* |
|
230 * Set terminal in raw mode. From less-177. |
|
231 * |
|
232 * Change terminal to "raw mode", or restore to "normal" mode. |
|
233 * "Raw mode" means |
|
234 * 1. An outstanding read will complete on receipt of a single keystroke. |
|
235 * 2. Input is not echoed. |
|
236 * 3. On output, \n is mapped to \r\n. |
|
237 * 4. \t is NOT expanded into spaces. |
|
238 * 5. Signal-causing characters such as ctrl-C (interrupt), |
|
239 * etc. are NOT disabled. |
|
240 * It doesn't matter whether an input \n is mapped to \r, or vice versa. |
|
241 */ |
|
242 void |
|
243 raw_mode (int on) |
|
244 { |
|
245 static int curr_on = 0; |
|
246 |
138
|
247 int tty_fd = STDIN_FILENO; |
178
|
248 if (! isatty (tty_fd)) |
138
|
249 { |
287
|
250 if (interactive) |
178
|
251 error ("stdin is not a tty!"); |
138
|
252 return; |
|
253 } |
1
|
254 |
|
255 if (on == curr_on) |
|
256 return; |
|
257 |
138
|
258 #if defined (HAVE_TERMIOS_H) |
1
|
259 { |
138
|
260 struct termios s; |
|
261 static struct termios save_term; |
1
|
262 |
|
263 if (on) |
|
264 { |
|
265 // Get terminal modes. |
|
266 |
138
|
267 tcgetattr (tty_fd, &s); |
1
|
268 |
|
269 // Save modes and set certain variables dependent on modes. |
|
270 |
|
271 save_term = s; |
|
272 // ospeed = s.c_cflag & CBAUD; |
|
273 // erase_char = s.c_cc[VERASE]; |
|
274 // kill_char = s.c_cc[VKILL]; |
|
275 |
|
276 // Set the modes to the way we want them. |
|
277 |
|
278 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL); |
169
|
279 s.c_oflag |= (OPOST|ONLCR); |
397
|
280 #if defined (OCRNL) |
|
281 s.c_oflag &= ~(OCRNL); |
|
282 #endif |
|
283 #if defined (ONOCR) |
|
284 s.c_oflag &= ~(ONOCR); |
|
285 #endif |
|
286 #if defined (ONLRET) |
|
287 s.c_oflag &= ~(ONLRET); |
367
|
288 #endif |
1
|
289 s.c_cc[VMIN] = 1; |
|
290 s.c_cc[VTIME] = 0; |
|
291 } |
|
292 else |
|
293 { |
|
294 // Restore saved modes. |
|
295 s = save_term; |
|
296 } |
138
|
297 tcsetattr (tty_fd, TCSAFLUSH, &s); |
1
|
298 } |
138
|
299 #elif defined (HAVE_TERMIO_H) |
|
300 { |
|
301 struct termio s; |
|
302 static struct termio save_term; |
|
303 |
|
304 if (on) |
|
305 { |
|
306 // Get terminal modes. |
|
307 |
|
308 ioctl (tty_fd, TCGETA, &s); |
|
309 |
|
310 // Save modes and set certain variables dependent on modes. |
|
311 |
|
312 save_term = s; |
|
313 // ospeed = s.c_cflag & CBAUD; |
|
314 // erase_char = s.c_cc[VERASE]; |
|
315 // kill_char = s.c_cc[VKILL]; |
|
316 |
|
317 // Set the modes to the way we want them. |
|
318 |
|
319 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL); |
169
|
320 s.c_oflag |= (OPOST|ONLCR); |
397
|
321 #if defined (OCRNL) |
|
322 s.c_oflag &= ~(OCRNL); |
|
323 #endif |
|
324 #if defined (ONOCR) |
|
325 s.c_oflag &= ~(ONOCR); |
|
326 #endif |
|
327 #if defined (ONLRET) |
|
328 s.c_oflag &= ~(ONLRET); |
367
|
329 #endif |
138
|
330 s.c_cc[VMIN] = 1; |
|
331 s.c_cc[VTIME] = 0; |
|
332 } |
|
333 else |
|
334 { |
|
335 // Restore saved modes. |
|
336 s = save_term; |
|
337 } |
|
338 ioctl (tty_fd, TCSETAW, &s); |
|
339 } |
|
340 #elif defined (HAVE_SGTTY_H) |
1
|
341 { |
|
342 struct sgttyb s; |
|
343 static struct sgttyb save_term; |
|
344 |
|
345 if (on) |
|
346 { |
|
347 // Get terminal modes. |
|
348 |
138
|
349 ioctl (tty_fd, TIOCGETP, &s); |
1
|
350 |
|
351 // Save modes and set certain variables dependent on modes. |
|
352 |
|
353 save_term = s; |
|
354 // ospeed = s.sg_ospeed; |
|
355 // erase_char = s.sg_erase; |
|
356 // kill_char = s.sg_kill; |
|
357 |
|
358 // Set the modes to the way we want them. |
|
359 |
|
360 s.sg_flags |= CBREAK; |
169
|
361 s.sg_flags &= ~(ECHO); |
1
|
362 } |
|
363 else |
|
364 { |
|
365 // Restore saved modes. |
|
366 s = save_term; |
|
367 } |
138
|
368 ioctl (tty_fd, TIOCSETN, &s); |
1
|
369 } |
|
370 #else |
|
371 LOSE! LOSE! |
|
372 #endif |
|
373 |
|
374 curr_on = on; |
|
375 } |
|
376 |
|
377 /* |
|
378 * Read one character from the terminal. |
|
379 */ |
|
380 int |
|
381 kbhit (void) |
|
382 { |
|
383 int c; |
|
384 raw_mode (1); |
|
385 c = cin.get (); |
|
386 raw_mode (0); |
|
387 return c; |
|
388 } |
|
389 |
|
390 char ** |
|
391 pathstring_to_vector (char *pathstring) |
|
392 { |
|
393 static char **path = (char **) NULL; |
|
394 |
|
395 if (pathstring != (char *) NULL) |
|
396 { |
|
397 int nelem = 0; |
240
|
398 char *tmp_path = strsave (pathstring); |
|
399 if (*tmp_path != '\0') |
1
|
400 { |
|
401 nelem++; |
240
|
402 char *ptr = tmp_path; |
1
|
403 while (*ptr != '\0') |
|
404 { |
|
405 if (*ptr == ':') |
|
406 nelem++; |
|
407 ptr++; |
|
408 } |
|
409 } |
|
410 |
240
|
411 char **foo = path; |
|
412 while (foo && *foo) |
244
|
413 delete [] *foo++; |
1
|
414 delete [] path; |
244
|
415 |
1
|
416 path = new char * [nelem+1]; |
|
417 path[nelem] = (char *) NULL; |
|
418 |
|
419 int i = 0; |
240
|
420 char *ptr = tmp_path; |
1
|
421 while (i < nelem) |
|
422 { |
|
423 char *end = strchr (ptr, ':'); |
|
424 if (end != (char *) NULL) |
|
425 *end = '\0'; |
|
426 char *result = tilde_expand (ptr); |
|
427 path[i] = strsave (result); |
|
428 free (result); |
|
429 ptr = end + 1; |
|
430 i++; |
|
431 } |
|
432 |
240
|
433 delete [] tmp_path; |
1
|
434 } |
|
435 |
|
436 return path; |
|
437 } |
|
438 |
195
|
439 char * |
1
|
440 octave_home (void) |
|
441 { |
|
442 static char *home = (char *) NULL; |
|
443 delete [] home; |
|
444 char *oh = getenv ("OCTAVE_HOME"); |
|
445 if (oh != (char *) NULL) |
|
446 home = strsave (oh); |
|
447 else |
|
448 home = strsave (OCTAVE_HOME); |
|
449 return home; |
|
450 } |
|
451 |
195
|
452 char * |
1
|
453 octave_lib_dir (void) |
|
454 { |
|
455 static char *ol = (char *) NULL; |
|
456 delete [] ol; |
|
457 char *oh = octave_home (); |
|
458 char *tmp = strconcat (oh, "/lib/octave/"); |
|
459 ol = strconcat (tmp, version_string); |
224
|
460 delete [] tmp; |
1
|
461 return ol; |
|
462 } |
|
463 |
195
|
464 char * |
186
|
465 octave_info_dir (void) |
|
466 { |
|
467 static char *oi = (char *) NULL; |
|
468 delete [] oi; |
|
469 char *oh = octave_home (); |
|
470 oi = strconcat (oh, "/info/"); |
|
471 return oi; |
|
472 } |
|
473 |
224
|
474 /* |
|
475 * Handle OCTAVE_PATH from the environment like TeX handles TEXINPUTS. |
|
476 * If the path starts with `:', prepend the standard path. If it ends |
|
477 * with `:' append the standard path. If it begins and ends with |
|
478 * `:', do both (which is useless, but the luser asked for it...). |
|
479 * |
|
480 * This function may eventually be called more than once, so be |
|
481 * careful not to create memory leaks. |
|
482 */ |
1
|
483 char * |
|
484 default_path (void) |
|
485 { |
|
486 static char *pathstring = (char *) NULL; |
|
487 delete [] pathstring; |
224
|
488 |
|
489 static char *std_path = (char *) NULL; |
|
490 delete [] std_path; |
|
491 |
|
492 char *libdir = octave_lib_dir (); |
|
493 |
|
494 std_path = strconcat (".:", libdir); |
|
495 |
1
|
496 char *oct_path = getenv ("OCTAVE_PATH"); |
224
|
497 |
1
|
498 if (oct_path != (char *) NULL) |
224
|
499 { |
|
500 pathstring = strsave (oct_path); |
|
501 |
|
502 if (pathstring[0] == ':') |
|
503 { |
|
504 char *tmp = pathstring; |
|
505 pathstring = strconcat (std_path, pathstring); |
|
506 delete [] tmp; |
|
507 } |
|
508 |
|
509 int tmp_len = strlen (pathstring); |
|
510 if (pathstring[tmp_len-1] == ':') |
|
511 { |
|
512 char *tmp = pathstring; |
|
513 pathstring = strconcat (pathstring, std_path); |
|
514 delete [] tmp; |
|
515 } |
|
516 } |
1
|
517 else |
224
|
518 pathstring = strsave (std_path); |
|
519 |
1
|
520 return pathstring; |
|
521 } |
|
522 |
|
523 char * |
186
|
524 default_info_file (void) |
|
525 { |
|
526 static char *info_file_string = (char *) NULL; |
|
527 delete [] info_file_string; |
|
528 char *oct_info_file = getenv ("OCTAVE_INFO_FILE"); |
|
529 if (oct_info_file != (char *) NULL) |
|
530 info_file_string = strsave (oct_info_file); |
|
531 else |
|
532 { |
|
533 char *infodir = octave_info_dir (); |
|
534 info_file_string = strconcat (infodir, "octave.info"); |
|
535 } |
|
536 return info_file_string; |
|
537 } |
|
538 |
|
539 char * |
195
|
540 default_editor (void) |
|
541 { |
|
542 static char *editor_string = (char *) NULL; |
|
543 delete [] editor_string; |
|
544 char *env_editor = getenv ("EDITOR"); |
|
545 if (env_editor == (char *) NULL || *env_editor == '\0') |
|
546 editor_string = strsave ("vi"); |
|
547 else |
|
548 editor_string = strsave (env_editor); |
|
549 return editor_string; |
|
550 } |
|
551 |
|
552 char * |
1
|
553 get_site_defaults (void) |
|
554 { |
|
555 static char *sd = (char *) NULL; |
|
556 delete [] sd; |
|
557 char *libdir = octave_lib_dir (); |
|
558 sd = strconcat (libdir, "/octaverc"); |
|
559 return sd; |
|
560 } |
|
561 |
|
562 char * |
|
563 default_pager (void) |
|
564 { |
|
565 static char *pager_binary = (char *) NULL; |
|
566 delete [] pager_binary; |
|
567 char *pgr = getenv ("PAGER"); |
|
568 if (pgr != (char *) NULL) |
|
569 pager_binary = strsave (pgr); |
|
570 else |
|
571 #ifdef DEFAULT_PAGER |
|
572 pager_binary = strsave (DEFAULT_PAGER); |
|
573 #else |
|
574 pager_binary = strsave (""); |
|
575 #endif |
|
576 |
|
577 return pager_binary; |
|
578 } |
|
579 |
|
580 /* |
|
581 * See if the given file is in the path. |
|
582 */ |
|
583 char * |
164
|
584 file_in_path (const char *name, const char *suffix) |
1
|
585 { |
240
|
586 char *retval = (char *) NULL; |
|
587 |
1
|
588 char *nm = strconcat ("/", name); |
|
589 if (suffix != (char *) NULL) |
|
590 { |
240
|
591 char *tmp = nm; |
1
|
592 nm = strconcat (tmp, suffix); |
|
593 delete [] tmp; |
|
594 } |
|
595 |
240
|
596 if (! the_current_working_directory) |
1
|
597 get_working_directory ("file_in_path"); |
|
598 |
|
599 char **path = pathstring_to_vector (user_pref.loadpath); |
|
600 |
|
601 char **ptr = path; |
|
602 if (ptr != (char **) NULL) |
|
603 { |
|
604 while (*ptr != (char *) NULL) |
|
605 { |
240
|
606 char *tmp = strconcat (*ptr, nm); |
|
607 char *p = make_absolute (tmp, the_current_working_directory); |
|
608 delete [] tmp; |
1
|
609 ifstream in_file (p); |
|
610 if (in_file) |
|
611 { |
|
612 in_file.close (); |
240
|
613 retval = p; |
|
614 goto done; |
1
|
615 } |
|
616 delete [] p; |
|
617 ptr++; |
|
618 } |
|
619 } |
|
620 |
240
|
621 done: |
1
|
622 delete [] nm; |
240
|
623 return retval; |
1
|
624 } |
|
625 |
|
626 /* |
338
|
627 * See if there is an function file in the path. If so, return the |
|
628 * full path to the file. |
1
|
629 */ |
|
630 char * |
338
|
631 fcn_file_in_path (const char *name) |
1
|
632 { |
341
|
633 return file_in_path (name, ".m"); |
1
|
634 } |
|
635 |
|
636 /* |
|
637 * Return a pretty pathname. If the first part of the pathname is the |
|
638 * same as $HOME, then replace that with `~'. |
|
639 */ |
|
640 char * |
|
641 polite_directory_format (char *name) |
|
642 { |
|
643 int l = home_directory ? strlen (home_directory) : 0; |
|
644 |
|
645 if (l > 1 && strncmp (home_directory, name, l) == 0 |
240
|
646 && (! name[l] || name[l] == '/')) |
1
|
647 { |
|
648 strcpy (tdir + 1, name + l); |
|
649 tdir[0] = '~'; |
|
650 return (tdir); |
|
651 } |
|
652 else |
|
653 return name; |
|
654 } |
|
655 |
|
656 /* |
|
657 * Return 1 if STRING contains an absolute pathname, else 0. |
|
658 */ |
|
659 int |
164
|
660 absolute_pathname (const char *string) |
1
|
661 { |
240
|
662 if (! string || ! *string) |
1
|
663 return 0; |
|
664 |
|
665 if (*string == '/') |
|
666 return 1; |
|
667 |
|
668 if (*string++ == '.') |
|
669 { |
240
|
670 if ((! *string) || *string == '/') |
1
|
671 return 1; |
|
672 |
|
673 if (*string++ == '.') |
240
|
674 if (! *string || *string == '/') |
1
|
675 return 1; |
|
676 } |
|
677 return 0; |
|
678 } |
|
679 |
|
680 /* |
|
681 * Return 1 if STRING is an absolute program name; it is absolute if |
|
682 * it contains any slashes. This is used to decide whether or not to |
|
683 * look up through $PATH. |
|
684 */ |
|
685 int |
164
|
686 absolute_program (const char *string) |
1
|
687 { |
|
688 return (strchr (string, '/') != (char *)NULL); |
|
689 } |
|
690 |
|
691 /* |
|
692 * Return the `basename' of the pathname in STRING (the stuff after |
|
693 * the last '/'). If STRING is not a full pathname, simply return it. |
|
694 */ |
|
695 char * |
|
696 base_pathname (char *string) |
|
697 { |
|
698 char *p = strrchr (string, '/'); |
|
699 |
240
|
700 if (! absolute_pathname (string)) |
1
|
701 return (string); |
|
702 |
|
703 if (p) |
|
704 return (++p); |
|
705 else |
|
706 return (string); |
|
707 } |
|
708 |
|
709 /* |
|
710 * Return the octal number parsed from STRING, or -1 to indicate that |
|
711 * the string contained a bad number. |
|
712 */ |
|
713 int |
164
|
714 read_octal (const char *string) |
1
|
715 { |
|
716 int result = 0; |
|
717 int digits = 0; |
|
718 |
|
719 while (*string && *string >= '0' && *string < '8') |
|
720 { |
|
721 digits++; |
|
722 result = (result * 8) + *string++ - '0'; |
|
723 } |
|
724 |
240
|
725 if (! digits || result > 0777 || *string) |
1
|
726 result = -1; |
|
727 |
|
728 return result; |
|
729 } |
|
730 |
|
731 /* |
|
732 * Append SOURCE to TARGET at INDEX. SIZE is the current amount of |
|
733 * space allocated to TARGET. SOURCE can be NULL, in which case |
|
734 * nothing happens. Gets rid of SOURCE by free ()ing it. Returns |
|
735 * TARGET in case the location has changed. |
|
736 */ |
|
737 char * |
|
738 sub_append_string (char *source, char *target, int *index, int *size) |
|
739 { |
|
740 if (source) |
|
741 { |
|
742 while ((int)strlen (source) >= (int)(*size - *index)) |
|
743 { |
|
744 char *tmp = new char [*size += DEFAULT_ARRAY_SIZE]; |
|
745 strcpy (tmp, target); |
|
746 delete [] target; |
|
747 target = tmp; |
|
748 } |
|
749 |
|
750 strcat (target, source); |
|
751 *index += strlen (source); |
|
752 |
|
753 delete [] source; |
|
754 } |
|
755 return target; |
|
756 } |
|
757 |
|
758 /* |
|
759 * Return a string which will be printed as a prompt. The string may |
|
760 * contain special characters which are decoded as follows: |
|
761 * |
|
762 * \t the time |
|
763 * \d the date |
|
764 * \n CRLF |
|
765 * \s the name of the shell (program) |
|
766 * \w the current working directory |
|
767 * \W the last element of PWD |
|
768 * \u your username |
|
769 * \h the hostname |
|
770 * \# the command number of this command |
|
771 * \! the history number of this command |
|
772 * \$ a $ or a # if you are root |
|
773 * \<octal> character code in octal |
|
774 * \\ a backslash |
|
775 */ |
|
776 char * |
164
|
777 decode_prompt_string (const char *string) |
1
|
778 { |
|
779 int result_size = PROMPT_GROWTH; |
|
780 int result_index = 0; |
|
781 char *result = new char [PROMPT_GROWTH]; |
|
782 int c; |
|
783 char *temp = (char *)NULL; |
|
784 |
|
785 result[0] = 0; |
|
786 while (c = *string++) |
|
787 { |
|
788 if (c == '\\') |
|
789 { |
|
790 c = *string; |
|
791 |
|
792 switch (c) |
|
793 { |
|
794 case '0': |
|
795 case '1': |
|
796 case '2': |
|
797 case '3': |
|
798 case '4': |
|
799 case '5': |
|
800 case '6': |
|
801 case '7': |
|
802 { |
|
803 char octal_string[4]; |
|
804 int n; |
|
805 |
|
806 strncpy (octal_string, string, 3); |
|
807 octal_string[3] = '\0'; |
|
808 |
|
809 n = read_octal (octal_string); |
|
810 |
|
811 temp = strsave ("\\"); |
|
812 if (n != -1) |
|
813 { |
|
814 string += 3; |
|
815 temp[0] = n; |
|
816 } |
|
817 |
|
818 c = 0; |
|
819 goto add_string; |
|
820 } |
|
821 |
|
822 case 't': |
|
823 case 'd': |
|
824 /* Make the current time/date into a string. */ |
|
825 { |
160
|
826 time_t the_time = time (0); |
1
|
827 char *ttemp = ctime (&the_time); |
|
828 temp = strsave (ttemp); |
|
829 |
|
830 if (c == 't') |
|
831 { |
|
832 strcpy (temp, temp + 11); |
|
833 temp[8] = '\0'; |
|
834 } |
|
835 else |
|
836 temp[10] = '\0'; |
|
837 |
|
838 goto add_string; |
|
839 } |
|
840 |
|
841 case 'n': |
240
|
842 if (! no_line_editing) |
1
|
843 temp = strsave ("\r\n"); |
|
844 else |
|
845 temp = strsave ("\n"); |
|
846 goto add_string; |
|
847 |
|
848 case 's': |
|
849 { |
|
850 temp = base_pathname (prog_name); |
|
851 temp = strsave (temp); |
|
852 goto add_string; |
|
853 } |
|
854 |
|
855 case 'w': |
|
856 case 'W': |
|
857 { |
|
858 char t_string[MAXPATHLEN]; |
|
859 #define EFFICIENT |
|
860 #ifdef EFFICIENT |
|
861 |
|
862 // Use the value of PWD because it is much more effecient. |
|
863 |
|
864 temp = user_pref.pwd; |
|
865 |
240
|
866 if (! temp) |
1
|
867 getcwd (t_string, MAXPATHLEN); |
|
868 else |
|
869 strcpy (t_string, temp); |
|
870 #else |
|
871 getcwd (t_string, MAXPATHLEN); |
|
872 #endif /* EFFICIENT */ |
|
873 |
|
874 if (c == 'W') |
|
875 { |
|
876 char *dir = strrchr (t_string, '/'); |
|
877 if (dir && dir != t_string) |
|
878 strcpy (t_string, dir + 1); |
|
879 temp = strsave (t_string); |
|
880 } |
|
881 else |
|
882 temp = strsave (polite_directory_format (t_string)); |
|
883 goto add_string; |
|
884 } |
|
885 |
|
886 case 'u': |
|
887 { |
|
888 temp = strsave (user_name); |
|
889 |
|
890 goto add_string; |
|
891 } |
|
892 |
|
893 case 'h': |
|
894 { |
|
895 char *t_string; |
|
896 |
|
897 temp = strsave (host_name); |
|
898 if (t_string = strchr (temp, '.')) |
|
899 *t_string = '\0'; |
|
900 |
|
901 goto add_string; |
|
902 } |
|
903 |
|
904 case '#': |
|
905 { |
278
|
906 char number_buffer[128]; |
1
|
907 sprintf (number_buffer, "%d", current_command_number); |
|
908 temp = strsave (number_buffer); |
|
909 goto add_string; |
|
910 } |
|
911 |
|
912 case '!': |
|
913 { |
278
|
914 char number_buffer[128]; |
1
|
915 int num = current_history_number (); |
|
916 if (num > 0) |
|
917 sprintf (number_buffer, "%d", num); |
|
918 else |
|
919 strcpy (number_buffer, "!"); |
|
920 temp = strsave (number_buffer); |
|
921 goto add_string; |
|
922 } |
|
923 |
|
924 case '$': |
|
925 temp = strsave (geteuid () == 0 ? "#" : "$"); |
|
926 goto add_string; |
|
927 |
|
928 case '\\': |
|
929 temp = strsave ("\\"); |
|
930 goto add_string; |
|
931 |
|
932 default: |
|
933 temp = strsave ("\\ "); |
|
934 temp[1] = c; |
|
935 |
|
936 add_string: |
|
937 if (c) |
|
938 string++; |
|
939 result = |
|
940 (char *)sub_append_string (temp, result, |
|
941 &result_index, &result_size); |
|
942 temp = (char *)NULL; /* Free ()'ed in sub_append_string (). */ |
|
943 result[result_index] = '\0'; |
|
944 break; |
|
945 } |
|
946 } |
|
947 else |
|
948 { |
|
949 while (3 + result_index > result_size) |
|
950 { |
|
951 char *tmp = new char [result_size += PROMPT_GROWTH]; |
|
952 strcpy (tmp, result); |
|
953 delete [] result; |
|
954 result = tmp; |
|
955 } |
|
956 result[result_index++] = c; |
|
957 result[result_index] = '\0'; |
|
958 } |
|
959 } |
|
960 |
|
961 #if 0 |
|
962 /* I don't really think that this is a good idea. Do you? */ |
240
|
963 if (! find_variable ("NO_PROMPT_VARS")) |
1
|
964 { |
|
965 WORD_LIST *expand_string (), *list; |
|
966 char *string_list (); |
|
967 |
|
968 list = expand_string (result, 1); |
|
969 free (result); |
|
970 result = string_list (list); |
|
971 dispose_words (list); |
|
972 } |
|
973 #endif |
|
974 |
|
975 return result; |
|
976 } |
|
977 |
|
978 /* |
|
979 * Remove the last N directories from PATH. Do not PATH blank. |
164
|
980 * PATH must contain enough space for MAXPATHLEN characters. |
1
|
981 */ |
|
982 void |
|
983 pathname_backup (char *path, int n) |
|
984 { |
|
985 register char *p; |
|
986 |
240
|
987 if (! *path) |
1
|
988 return; |
|
989 |
|
990 p = path + (strlen (path) - 1); |
|
991 |
|
992 while (n--) |
|
993 { |
|
994 while (*p == '/' && p != path) |
|
995 p--; |
|
996 |
|
997 while (*p != '/' && p != path) |
|
998 p--; |
|
999 |
|
1000 *++p = '\0'; |
|
1001 } |
|
1002 } |
|
1003 |
|
1004 /* |
|
1005 * Turn STRING (a pathname) into an absolute pathname, assuming that |
|
1006 * DOT_PATH contains the symbolic location of '.'. This always |
|
1007 * returns a new string, even if STRING was an absolute pathname to |
|
1008 * begin with. |
|
1009 */ |
|
1010 char * |
164
|
1011 make_absolute (const char *string, const char *dot_path) |
1
|
1012 { |
|
1013 static char current_path[MAXPATHLEN]; |
|
1014 register char *cp; |
|
1015 |
240
|
1016 if (! dot_path || *string == '/') |
1
|
1017 return strsave (string); |
|
1018 |
|
1019 strcpy (current_path, dot_path); |
|
1020 |
240
|
1021 if (! current_path[0]) |
1
|
1022 strcpy (current_path, "./"); |
|
1023 |
|
1024 cp = current_path + (strlen (current_path) - 1); |
|
1025 |
|
1026 if (*cp++ != '/') |
|
1027 *cp++ = '/'; |
|
1028 |
|
1029 *cp = '\0'; |
|
1030 |
|
1031 while (*string) |
|
1032 { |
|
1033 if (*string == '.') |
|
1034 { |
240
|
1035 if (! string[1]) |
1
|
1036 return strsave (current_path); |
|
1037 |
|
1038 if (string[1] == '/') |
|
1039 { |
|
1040 string += 2; |
|
1041 continue; |
|
1042 } |
|
1043 |
240
|
1044 if (string[1] == '.' && (string[2] == '/' || ! string[2])) |
1
|
1045 { |
|
1046 string += 2; |
|
1047 |
|
1048 if (*string) |
|
1049 string++; |
|
1050 |
|
1051 pathname_backup (current_path, 1); |
|
1052 cp = current_path + strlen (current_path); |
|
1053 continue; |
|
1054 } |
|
1055 } |
|
1056 |
|
1057 while (*string && *string != '/') |
|
1058 *cp++ = *string++; |
|
1059 |
|
1060 if (*string) |
|
1061 *cp++ = *string++; |
|
1062 |
|
1063 *cp = '\0'; |
|
1064 } |
|
1065 return strsave (current_path); |
|
1066 } |
|
1067 |
|
1068 /* |
|
1069 * Return a consed string which is the current working directory. |
|
1070 * FOR_WHOM is the name of the caller for error printing. |
|
1071 */ |
|
1072 char * |
164
|
1073 get_working_directory (const char *for_whom) |
1
|
1074 { |
240
|
1075 if (! follow_symbolic_links) |
1
|
1076 { |
|
1077 if (the_current_working_directory) |
|
1078 delete [] the_current_working_directory; |
|
1079 |
|
1080 the_current_working_directory = (char *)NULL; |
|
1081 } |
|
1082 |
240
|
1083 if (! the_current_working_directory) |
1
|
1084 { |
|
1085 char *directory; |
|
1086 |
|
1087 the_current_working_directory = new char [MAXPATHLEN]; |
|
1088 directory = getcwd (the_current_working_directory, MAXPATHLEN); |
240
|
1089 if (! directory) |
1
|
1090 { |
|
1091 message (for_whom, the_current_working_directory); |
|
1092 delete [] the_current_working_directory; |
|
1093 the_current_working_directory = (char *)NULL; |
|
1094 return (char *)NULL; |
|
1095 } |
|
1096 } |
|
1097 |
|
1098 return the_current_working_directory; |
|
1099 } |
|
1100 |
|
1101 /* |
|
1102 * Do the work of changing to the directory NEWDIR. Handle symbolic |
|
1103 * link following, etc. |
|
1104 */ |
|
1105 int |
164
|
1106 change_to_directory (const char *newdir) |
1
|
1107 { |
|
1108 char *t; |
|
1109 |
|
1110 if (follow_symbolic_links) |
|
1111 { |
240
|
1112 if (! the_current_working_directory) |
1
|
1113 get_working_directory ("cd_links"); |
|
1114 |
|
1115 if (the_current_working_directory) |
|
1116 t = make_absolute (newdir, the_current_working_directory); |
|
1117 else |
|
1118 t = strsave (newdir); |
|
1119 |
|
1120 /* Get rid of trailing `/'. */ |
|
1121 { |
|
1122 register int len_t = strlen (t); |
|
1123 if (len_t > 1) |
|
1124 { |
|
1125 --len_t; |
|
1126 if (t[len_t] == '/') |
|
1127 t[len_t] = '\0'; |
|
1128 } |
|
1129 } |
|
1130 |
|
1131 if (chdir (t) < 0) |
|
1132 { |
|
1133 delete [] t; |
|
1134 return 0; |
|
1135 } |
|
1136 |
|
1137 if (the_current_working_directory) |
|
1138 strcpy (the_current_working_directory, t); |
|
1139 |
|
1140 delete [] t; |
|
1141 return 1; |
|
1142 } |
|
1143 else |
|
1144 { |
|
1145 if (chdir (newdir) < 0) |
|
1146 return 0; |
|
1147 else |
|
1148 return 1; |
|
1149 } |
|
1150 } |
|
1151 |
320
|
1152 #if !defined (HAVE_GETHOSTNAME) && defined (HAVE_SYS_UTSNAME_H) |
|
1153 extern "C" |
|
1154 { |
|
1155 #include <sys/utsname.h> |
|
1156 int |
|
1157 gethostname (char *name, int namelen) |
|
1158 { |
|
1159 int i; |
|
1160 struct utsname ut; |
|
1161 |
|
1162 --namelen; |
|
1163 |
|
1164 uname (&ut); |
|
1165 i = strlen (ut.nodename) + 1; |
|
1166 strncpy (name, ut.nodename, i < namelen ? i : namelen); |
|
1167 name[namelen] = '\0'; |
|
1168 |
|
1169 return 0; |
|
1170 } |
|
1171 } |
|
1172 #endif |
|
1173 |
1
|
1174 /* |
|
1175 * Has file `A' been modified after time `T'? |
|
1176 * |
|
1177 * case: |
|
1178 * |
|
1179 * a newer than t returns 1 |
|
1180 * a older than t returns 0 |
|
1181 * stat on a fails returns -1 |
|
1182 */ |
|
1183 int |
164
|
1184 is_newer (const char *fa, time_t t) |
1
|
1185 { |
|
1186 struct stat fa_sb; |
|
1187 register int fa_stat; |
|
1188 register int status = 0; |
|
1189 |
|
1190 fa_stat = stat (fa, &fa_sb); |
|
1191 if (fa_stat != 0) |
|
1192 status = -1; |
|
1193 |
|
1194 if (status != 0) |
|
1195 return status; |
|
1196 |
|
1197 return (fa_sb.st_mtime > t); |
|
1198 } |
|
1199 |
|
1200 /* |
|
1201 * Return to the main command loop in octave.cc. |
|
1202 */ |
189
|
1203 void |
1
|
1204 jump_to_top_level (void) |
|
1205 { |
|
1206 run_all_unwind_protects (); |
|
1207 |
|
1208 longjmp (toplevel, 1); |
|
1209 } |
|
1210 |
|
1211 /* |
|
1212 * Gag. |
|
1213 */ |
|
1214 char * |
|
1215 s_plural (int i) |
|
1216 { |
|
1217 static char *empty = ""; |
|
1218 static char *s = "s"; |
|
1219 return i == 1 ? empty : s; |
|
1220 } |
|
1221 |
|
1222 char * |
|
1223 es_plural (int i) |
|
1224 { |
|
1225 static char *empty = ""; |
|
1226 static char *es = "es"; |
|
1227 return i == 1 ? es : empty; |
|
1228 } |
|
1229 |
|
1230 char * |
|
1231 save_in_tmp_file (tree_constant& t, int ndim = 2, int parametric = 0) |
|
1232 { |
|
1233 char *name = strsave (tmpnam ((char *) NULL)); |
|
1234 if (name != (char *) NULL) |
|
1235 { |
|
1236 ofstream file (name); |
|
1237 if (file) |
|
1238 { |
|
1239 switch (ndim) |
|
1240 { |
|
1241 case 2: |
|
1242 t.save (file); |
|
1243 break; |
|
1244 case 3: |
|
1245 t.save_three_d (file, parametric); |
|
1246 break; |
|
1247 default: |
|
1248 panic_impossible (); |
|
1249 break; |
|
1250 } |
|
1251 } |
|
1252 else |
|
1253 { |
|
1254 error ("couldn't open temporary output file `%s'", name); |
|
1255 delete [] name; |
|
1256 name = (char *) NULL; |
|
1257 } |
|
1258 } |
|
1259 return name; |
|
1260 } |
|
1261 |
|
1262 void |
|
1263 mark_for_deletion (const char *filename) |
|
1264 { |
|
1265 char *tmp = strsave (filename); |
|
1266 tmp_files.push (tmp); |
|
1267 } |
|
1268 |
|
1269 void |
|
1270 cleanup_tmp_files (void) |
|
1271 { |
|
1272 while (! tmp_files.empty ()) |
|
1273 { |
|
1274 char *filename = tmp_files.pop (); |
|
1275 unlink (filename); |
|
1276 delete [] filename; |
|
1277 } |
|
1278 } |
|
1279 |
|
1280 int |
|
1281 send_to_plot_stream (const char *cmd) |
|
1282 { |
|
1283 // From sighandlers.cc: |
|
1284 extern int pipe_handler_error_count; |
|
1285 |
|
1286 static int initialized = 0; |
|
1287 |
|
1288 if (! plot_stream.is_open ()) |
|
1289 { |
|
1290 char *plot_prog = user_pref.gnuplot_binary; |
|
1291 if (plot_prog != (char *) NULL) |
|
1292 { |
|
1293 plot_stream.open (plot_prog); |
|
1294 if (! plot_stream.is_open ()) |
|
1295 { |
|
1296 warning ("plot: unable to open pipe to `%s'", |
|
1297 plot_prog); |
|
1298 |
|
1299 if (strcmp (plot_prog, "gnuplot") != 0) |
|
1300 { |
217
|
1301 warning ("having trouble finding plotting program."); |
|
1302 warning ("trying again with `gnuplot'"); |
1
|
1303 goto last_chance; |
|
1304 } |
|
1305 } |
|
1306 } |
|
1307 else |
|
1308 { |
|
1309 last_chance: |
|
1310 |
|
1311 plot_stream.open ("gnuplot"); |
|
1312 |
|
1313 if (! plot_stream.is_open ()) |
|
1314 { |
|
1315 error ("plot: unable to open pipe to `%s'", plot_prog); |
|
1316 return -1; |
|
1317 } |
|
1318 } |
|
1319 } |
|
1320 |
|
1321 if (! initialized) |
|
1322 { |
|
1323 initialized = 1; |
|
1324 plot_stream << "set data style lines\n"; |
|
1325 } |
|
1326 |
|
1327 plot_stream << cmd; |
|
1328 plot_stream.flush (); |
|
1329 pipe_handler_error_count = 0; |
|
1330 |
|
1331 return 0; |
|
1332 } |
|
1333 |
|
1334 void |
|
1335 close_plot_stream (void) |
|
1336 { |
|
1337 if (plot_stream.is_open ()) |
|
1338 plot_stream.close (); |
|
1339 } |
|
1340 |
|
1341 int |
287
|
1342 almost_match (const char *std, const char *s, |
|
1343 int min_match_len = 1, int case_sens = 1) |
1
|
1344 { |
|
1345 int stdlen = strlen (std); |
|
1346 int slen = strlen (s); |
|
1347 |
|
1348 return (slen <= stdlen |
|
1349 && slen >= min_match_len |
287
|
1350 && (case_sens |
|
1351 ? (strncmp (std, s, slen) == 0) |
|
1352 : (strncasecmp (std, s, slen) == 0))); |
|
1353 } |
|
1354 |
|
1355 /* |
|
1356 * Ugh. |
|
1357 */ |
|
1358 int |
|
1359 keyword_almost_match (const char **std, int *min_len, const char *s, |
|
1360 int min_toks_to_match, int max_toks) |
|
1361 { |
|
1362 int status = 0; |
|
1363 int tok_count = 0; |
|
1364 int toks_matched = 0; |
|
1365 |
|
1366 if (s == NULL || *s == '\0' || max_toks < 1) |
|
1367 return status; |
|
1368 |
|
1369 char *kw = strsave (s); |
|
1370 |
|
1371 char *t = kw; |
|
1372 while (*t != '\0') |
|
1373 { |
|
1374 if (*t == '\t') |
|
1375 *t = ' '; |
|
1376 t++; |
|
1377 } |
|
1378 |
|
1379 char *beg = kw; |
|
1380 while (*beg == ' ') |
|
1381 beg++; |
|
1382 |
|
1383 if (*beg == '\0') |
|
1384 return status; |
|
1385 |
|
1386 |
|
1387 char **to_match = new char * [max_toks + 1]; |
|
1388 char **s1 = std; |
|
1389 char **s2 = to_match; |
|
1390 |
|
1391 if (s1 == NULL || s2 == NULL) |
|
1392 goto done; |
|
1393 |
|
1394 s2[tok_count] = beg; |
|
1395 char *end; |
|
1396 while ((end = strchr (beg, ' ')) != NULL) |
|
1397 { |
|
1398 *end = '\0'; |
|
1399 beg = end + 1; |
|
1400 |
|
1401 while (*beg == ' ') |
|
1402 beg++; |
|
1403 |
|
1404 if (*beg == '\0') |
|
1405 break; |
|
1406 |
|
1407 tok_count++; |
|
1408 if (tok_count >= max_toks) |
|
1409 goto done; |
|
1410 |
|
1411 s2[tok_count] = beg; |
|
1412 } |
|
1413 s2[tok_count+1] = NULL; |
|
1414 |
|
1415 s2 = to_match; |
|
1416 |
|
1417 for (;;) |
|
1418 { |
|
1419 if (! almost_match (*s1, *s2, min_len[toks_matched], 0)) |
|
1420 goto done; |
|
1421 |
|
1422 toks_matched++; |
|
1423 |
|
1424 s1++; |
|
1425 s2++; |
|
1426 |
|
1427 if (! *s2) |
|
1428 { |
|
1429 status = (toks_matched >= min_toks_to_match); |
|
1430 goto done; |
|
1431 } |
|
1432 |
|
1433 if (! *s1) |
|
1434 goto done; |
|
1435 } |
|
1436 |
|
1437 done: |
|
1438 |
|
1439 delete [] kw; |
|
1440 delete [] to_match; |
|
1441 |
|
1442 return status; |
1
|
1443 } |
|
1444 |
|
1445 char ** |
338
|
1446 get_fcn_file_names (int& num, const char *dir, int no_suffix) |
1
|
1447 { |
|
1448 static int num_max = 256; |
|
1449 char **retval = new char * [num_max]; |
|
1450 int i = 0; |
|
1451 |
|
1452 DIR *dirp = opendir (dir); |
|
1453 if (dirp != (DIR *) NULL) |
|
1454 { |
|
1455 struct dirent *entry; |
|
1456 while ((entry = readdir (dirp)) != (struct dirent *) NULL) |
|
1457 { |
|
1458 int len = NLENGTH (entry); |
|
1459 if (len > 2 |
|
1460 && entry->d_name[len-2] == '.' |
|
1461 && entry->d_name[len-1] == 'm') |
|
1462 { |
|
1463 retval[i] = strsave (entry->d_name); |
|
1464 if (no_suffix) |
|
1465 retval[i][len-2] = '\0'; |
|
1466 |
|
1467 i++; |
|
1468 |
|
1469 if (i == num_max - 1) |
|
1470 { |
244
|
1471 // Reallocate the array. Only copy pointers, not the strings they |
|
1472 // point to, then only delete the original array of pointers, and not |
|
1473 // the strings they point to. |
|
1474 |
1
|
1475 num_max += 256; |
|
1476 char **tmp = new char * [num_max]; |
|
1477 for (int j = 0; j < i; j++) |
|
1478 tmp[j] = retval[j]; |
|
1479 |
244
|
1480 delete [] retval; |
|
1481 |
1
|
1482 retval = tmp; |
|
1483 } |
|
1484 } |
|
1485 } |
106
|
1486 closedir (dirp); |
1
|
1487 } |
|
1488 |
|
1489 retval[i] = (char *) NULL; |
|
1490 num = i; |
|
1491 |
|
1492 return retval; |
|
1493 } |
|
1494 |
|
1495 char ** |
338
|
1496 get_fcn_file_names (int& num, int no_suffix) |
1
|
1497 { |
|
1498 static int num_max = 1024; |
|
1499 char **retval = new char * [num_max]; |
|
1500 int i = 0; |
|
1501 |
|
1502 char **path = pathstring_to_vector (user_pref.loadpath); |
|
1503 |
|
1504 char **ptr = path; |
|
1505 if (ptr != (char **) NULL) |
|
1506 { |
|
1507 while (*ptr != (char *) NULL) |
|
1508 { |
|
1509 int tmp_num; |
338
|
1510 char **names = get_fcn_file_names (tmp_num, *ptr, no_suffix); |
1
|
1511 |
|
1512 if (i + tmp_num >= num_max - 1) |
|
1513 { |
244
|
1514 // Reallocate the array. Only copy pointers, not the strings they |
|
1515 // point to, then only delete the original array of pointers, and not |
|
1516 // the strings they point to. |
|
1517 |
1
|
1518 num_max += 1024; |
|
1519 char **tmp = new char * [num_max]; |
|
1520 for (int j = 0; j < i; j++) |
|
1521 tmp[j] = retval[j]; |
|
1522 |
244
|
1523 delete [] retval; |
|
1524 |
1
|
1525 retval = tmp; |
|
1526 } |
|
1527 |
|
1528 int k = 0; |
|
1529 while (k < tmp_num) |
|
1530 retval[i++] = names[k++]; |
|
1531 |
|
1532 ptr++; |
|
1533 } |
|
1534 } |
|
1535 |
|
1536 retval[i] = (char *) NULL; |
|
1537 num = i; |
|
1538 |
|
1539 return retval; |
|
1540 } |
|
1541 |
|
1542 int |
|
1543 NINT (double x) |
|
1544 { |
|
1545 if (x > INT_MAX) |
|
1546 return INT_MAX; |
|
1547 else if (x < INT_MIN) |
|
1548 return INT_MIN; |
|
1549 else |
|
1550 return (x > 0) ? ((int) (x + 0.5)) : ((int) (x - 0.5)); |
|
1551 } |
|
1552 |
|
1553 double |
|
1554 D_NINT (double x) |
|
1555 { |
|
1556 if (xisinf (x) || xisnan (x)) |
|
1557 return x; |
|
1558 else |
|
1559 return floor (x + 0.5); |
|
1560 } |
|
1561 |
|
1562 /* |
|
1563 ;;; Local Variables: *** |
|
1564 ;;; mode: C++ *** |
|
1565 ;;; page-delimiter: "^/\\*" *** |
|
1566 ;;; End: *** |
|
1567 */ |