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