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