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 /* |
|
595 * See if there is an M-file in the path. If so, return the full path |
|
596 * to the file. |
|
597 */ |
|
598 char * |
164
|
599 m_file_in_path (const char *name) |
1
|
600 { |
|
601 return file_in_path (name, ".m"); |
|
602 } |
|
603 |
|
604 /* |
|
605 * Return a pretty pathname. If the first part of the pathname is the |
|
606 * same as $HOME, then replace that with `~'. |
|
607 */ |
|
608 char * |
|
609 polite_directory_format (char *name) |
|
610 { |
|
611 int l = home_directory ? strlen (home_directory) : 0; |
|
612 |
|
613 if (l > 1 && strncmp (home_directory, name, l) == 0 |
240
|
614 && (! name[l] || name[l] == '/')) |
1
|
615 { |
|
616 strcpy (tdir + 1, name + l); |
|
617 tdir[0] = '~'; |
|
618 return (tdir); |
|
619 } |
|
620 else |
|
621 return name; |
|
622 } |
|
623 |
|
624 /* |
|
625 * Return 1 if STRING contains an absolute pathname, else 0. |
|
626 */ |
|
627 int |
164
|
628 absolute_pathname (const char *string) |
1
|
629 { |
240
|
630 if (! string || ! *string) |
1
|
631 return 0; |
|
632 |
|
633 if (*string == '/') |
|
634 return 1; |
|
635 |
|
636 if (*string++ == '.') |
|
637 { |
240
|
638 if ((! *string) || *string == '/') |
1
|
639 return 1; |
|
640 |
|
641 if (*string++ == '.') |
240
|
642 if (! *string || *string == '/') |
1
|
643 return 1; |
|
644 } |
|
645 return 0; |
|
646 } |
|
647 |
|
648 /* |
|
649 * Return 1 if STRING is an absolute program name; it is absolute if |
|
650 * it contains any slashes. This is used to decide whether or not to |
|
651 * look up through $PATH. |
|
652 */ |
|
653 int |
164
|
654 absolute_program (const char *string) |
1
|
655 { |
|
656 return (strchr (string, '/') != (char *)NULL); |
|
657 } |
|
658 |
|
659 /* |
|
660 * Return the `basename' of the pathname in STRING (the stuff after |
|
661 * the last '/'). If STRING is not a full pathname, simply return it. |
|
662 */ |
|
663 char * |
|
664 base_pathname (char *string) |
|
665 { |
|
666 char *p = strrchr (string, '/'); |
|
667 |
240
|
668 if (! absolute_pathname (string)) |
1
|
669 return (string); |
|
670 |
|
671 if (p) |
|
672 return (++p); |
|
673 else |
|
674 return (string); |
|
675 } |
|
676 |
|
677 /* |
|
678 * Return the octal number parsed from STRING, or -1 to indicate that |
|
679 * the string contained a bad number. |
|
680 */ |
|
681 int |
164
|
682 read_octal (const char *string) |
1
|
683 { |
|
684 int result = 0; |
|
685 int digits = 0; |
|
686 |
|
687 while (*string && *string >= '0' && *string < '8') |
|
688 { |
|
689 digits++; |
|
690 result = (result * 8) + *string++ - '0'; |
|
691 } |
|
692 |
240
|
693 if (! digits || result > 0777 || *string) |
1
|
694 result = -1; |
|
695 |
|
696 return result; |
|
697 } |
|
698 |
|
699 /* |
|
700 * Append SOURCE to TARGET at INDEX. SIZE is the current amount of |
|
701 * space allocated to TARGET. SOURCE can be NULL, in which case |
|
702 * nothing happens. Gets rid of SOURCE by free ()ing it. Returns |
|
703 * TARGET in case the location has changed. |
|
704 */ |
|
705 char * |
|
706 sub_append_string (char *source, char *target, int *index, int *size) |
|
707 { |
|
708 if (source) |
|
709 { |
|
710 while ((int)strlen (source) >= (int)(*size - *index)) |
|
711 { |
|
712 char *tmp = new char [*size += DEFAULT_ARRAY_SIZE]; |
|
713 strcpy (tmp, target); |
|
714 delete [] target; |
|
715 target = tmp; |
|
716 } |
|
717 |
|
718 strcat (target, source); |
|
719 *index += strlen (source); |
|
720 |
|
721 delete [] source; |
|
722 } |
|
723 return target; |
|
724 } |
|
725 |
|
726 /* |
|
727 * Return a string which will be printed as a prompt. The string may |
|
728 * contain special characters which are decoded as follows: |
|
729 * |
|
730 * \t the time |
|
731 * \d the date |
|
732 * \n CRLF |
|
733 * \s the name of the shell (program) |
|
734 * \w the current working directory |
|
735 * \W the last element of PWD |
|
736 * \u your username |
|
737 * \h the hostname |
|
738 * \# the command number of this command |
|
739 * \! the history number of this command |
|
740 * \$ a $ or a # if you are root |
|
741 * \<octal> character code in octal |
|
742 * \\ a backslash |
|
743 */ |
|
744 char * |
164
|
745 decode_prompt_string (const char *string) |
1
|
746 { |
|
747 int result_size = PROMPT_GROWTH; |
|
748 int result_index = 0; |
|
749 char *result = new char [PROMPT_GROWTH]; |
|
750 int c; |
|
751 char *temp = (char *)NULL; |
|
752 |
|
753 result[0] = 0; |
|
754 while (c = *string++) |
|
755 { |
|
756 if (c == '\\') |
|
757 { |
|
758 c = *string; |
|
759 |
|
760 switch (c) |
|
761 { |
|
762 case '0': |
|
763 case '1': |
|
764 case '2': |
|
765 case '3': |
|
766 case '4': |
|
767 case '5': |
|
768 case '6': |
|
769 case '7': |
|
770 { |
|
771 char octal_string[4]; |
|
772 int n; |
|
773 |
|
774 strncpy (octal_string, string, 3); |
|
775 octal_string[3] = '\0'; |
|
776 |
|
777 n = read_octal (octal_string); |
|
778 |
|
779 temp = strsave ("\\"); |
|
780 if (n != -1) |
|
781 { |
|
782 string += 3; |
|
783 temp[0] = n; |
|
784 } |
|
785 |
|
786 c = 0; |
|
787 goto add_string; |
|
788 } |
|
789 |
|
790 case 't': |
|
791 case 'd': |
|
792 /* Make the current time/date into a string. */ |
|
793 { |
160
|
794 time_t the_time = time (0); |
1
|
795 char *ttemp = ctime (&the_time); |
|
796 temp = strsave (ttemp); |
|
797 |
|
798 if (c == 't') |
|
799 { |
|
800 strcpy (temp, temp + 11); |
|
801 temp[8] = '\0'; |
|
802 } |
|
803 else |
|
804 temp[10] = '\0'; |
|
805 |
|
806 goto add_string; |
|
807 } |
|
808 |
|
809 case 'n': |
240
|
810 if (! no_line_editing) |
1
|
811 temp = strsave ("\r\n"); |
|
812 else |
|
813 temp = strsave ("\n"); |
|
814 goto add_string; |
|
815 |
|
816 case 's': |
|
817 { |
|
818 temp = base_pathname (prog_name); |
|
819 temp = strsave (temp); |
|
820 goto add_string; |
|
821 } |
|
822 |
|
823 case 'w': |
|
824 case 'W': |
|
825 { |
|
826 char t_string[MAXPATHLEN]; |
|
827 #define EFFICIENT |
|
828 #ifdef EFFICIENT |
|
829 |
|
830 // Use the value of PWD because it is much more effecient. |
|
831 |
|
832 temp = user_pref.pwd; |
|
833 |
240
|
834 if (! temp) |
1
|
835 getcwd (t_string, MAXPATHLEN); |
|
836 else |
|
837 strcpy (t_string, temp); |
|
838 #else |
|
839 getcwd (t_string, MAXPATHLEN); |
|
840 #endif /* EFFICIENT */ |
|
841 |
|
842 if (c == 'W') |
|
843 { |
|
844 char *dir = strrchr (t_string, '/'); |
|
845 if (dir && dir != t_string) |
|
846 strcpy (t_string, dir + 1); |
|
847 temp = strsave (t_string); |
|
848 } |
|
849 else |
|
850 temp = strsave (polite_directory_format (t_string)); |
|
851 goto add_string; |
|
852 } |
|
853 |
|
854 case 'u': |
|
855 { |
|
856 temp = strsave (user_name); |
|
857 |
|
858 goto add_string; |
|
859 } |
|
860 |
|
861 case 'h': |
|
862 { |
|
863 char *t_string; |
|
864 |
|
865 temp = strsave (host_name); |
|
866 if (t_string = strchr (temp, '.')) |
|
867 *t_string = '\0'; |
|
868 |
|
869 goto add_string; |
|
870 } |
|
871 |
|
872 case '#': |
|
873 { |
278
|
874 char number_buffer[128]; |
1
|
875 sprintf (number_buffer, "%d", current_command_number); |
|
876 temp = strsave (number_buffer); |
|
877 goto add_string; |
|
878 } |
|
879 |
|
880 case '!': |
|
881 { |
278
|
882 char number_buffer[128]; |
1
|
883 int num = current_history_number (); |
|
884 if (num > 0) |
|
885 sprintf (number_buffer, "%d", num); |
|
886 else |
|
887 strcpy (number_buffer, "!"); |
|
888 temp = strsave (number_buffer); |
|
889 goto add_string; |
|
890 } |
|
891 |
|
892 case '$': |
|
893 temp = strsave (geteuid () == 0 ? "#" : "$"); |
|
894 goto add_string; |
|
895 |
|
896 case '\\': |
|
897 temp = strsave ("\\"); |
|
898 goto add_string; |
|
899 |
|
900 default: |
|
901 temp = strsave ("\\ "); |
|
902 temp[1] = c; |
|
903 |
|
904 add_string: |
|
905 if (c) |
|
906 string++; |
|
907 result = |
|
908 (char *)sub_append_string (temp, result, |
|
909 &result_index, &result_size); |
|
910 temp = (char *)NULL; /* Free ()'ed in sub_append_string (). */ |
|
911 result[result_index] = '\0'; |
|
912 break; |
|
913 } |
|
914 } |
|
915 else |
|
916 { |
|
917 while (3 + result_index > result_size) |
|
918 { |
|
919 char *tmp = new char [result_size += PROMPT_GROWTH]; |
|
920 strcpy (tmp, result); |
|
921 delete [] result; |
|
922 result = tmp; |
|
923 } |
|
924 result[result_index++] = c; |
|
925 result[result_index] = '\0'; |
|
926 } |
|
927 } |
|
928 |
|
929 #if 0 |
|
930 /* I don't really think that this is a good idea. Do you? */ |
240
|
931 if (! find_variable ("NO_PROMPT_VARS")) |
1
|
932 { |
|
933 WORD_LIST *expand_string (), *list; |
|
934 char *string_list (); |
|
935 |
|
936 list = expand_string (result, 1); |
|
937 free (result); |
|
938 result = string_list (list); |
|
939 dispose_words (list); |
|
940 } |
|
941 #endif |
|
942 |
|
943 return result; |
|
944 } |
|
945 |
|
946 /* |
|
947 * Remove the last N directories from PATH. Do not PATH blank. |
164
|
948 * PATH must contain enough space for MAXPATHLEN characters. |
1
|
949 */ |
|
950 void |
|
951 pathname_backup (char *path, int n) |
|
952 { |
|
953 register char *p; |
|
954 |
240
|
955 if (! *path) |
1
|
956 return; |
|
957 |
|
958 p = path + (strlen (path) - 1); |
|
959 |
|
960 while (n--) |
|
961 { |
|
962 while (*p == '/' && p != path) |
|
963 p--; |
|
964 |
|
965 while (*p != '/' && p != path) |
|
966 p--; |
|
967 |
|
968 *++p = '\0'; |
|
969 } |
|
970 } |
|
971 |
|
972 /* |
|
973 * Turn STRING (a pathname) into an absolute pathname, assuming that |
|
974 * DOT_PATH contains the symbolic location of '.'. This always |
|
975 * returns a new string, even if STRING was an absolute pathname to |
|
976 * begin with. |
|
977 */ |
|
978 char * |
164
|
979 make_absolute (const char *string, const char *dot_path) |
1
|
980 { |
|
981 static char current_path[MAXPATHLEN]; |
|
982 register char *cp; |
|
983 |
240
|
984 if (! dot_path || *string == '/') |
1
|
985 return strsave (string); |
|
986 |
|
987 strcpy (current_path, dot_path); |
|
988 |
240
|
989 if (! current_path[0]) |
1
|
990 strcpy (current_path, "./"); |
|
991 |
|
992 cp = current_path + (strlen (current_path) - 1); |
|
993 |
|
994 if (*cp++ != '/') |
|
995 *cp++ = '/'; |
|
996 |
|
997 *cp = '\0'; |
|
998 |
|
999 while (*string) |
|
1000 { |
|
1001 if (*string == '.') |
|
1002 { |
240
|
1003 if (! string[1]) |
1
|
1004 return strsave (current_path); |
|
1005 |
|
1006 if (string[1] == '/') |
|
1007 { |
|
1008 string += 2; |
|
1009 continue; |
|
1010 } |
|
1011 |
240
|
1012 if (string[1] == '.' && (string[2] == '/' || ! string[2])) |
1
|
1013 { |
|
1014 string += 2; |
|
1015 |
|
1016 if (*string) |
|
1017 string++; |
|
1018 |
|
1019 pathname_backup (current_path, 1); |
|
1020 cp = current_path + strlen (current_path); |
|
1021 continue; |
|
1022 } |
|
1023 } |
|
1024 |
|
1025 while (*string && *string != '/') |
|
1026 *cp++ = *string++; |
|
1027 |
|
1028 if (*string) |
|
1029 *cp++ = *string++; |
|
1030 |
|
1031 *cp = '\0'; |
|
1032 } |
|
1033 return strsave (current_path); |
|
1034 } |
|
1035 |
|
1036 /* |
|
1037 * Return a consed string which is the current working directory. |
|
1038 * FOR_WHOM is the name of the caller for error printing. |
|
1039 */ |
|
1040 char * |
164
|
1041 get_working_directory (const char *for_whom) |
1
|
1042 { |
240
|
1043 if (! follow_symbolic_links) |
1
|
1044 { |
|
1045 if (the_current_working_directory) |
|
1046 delete [] the_current_working_directory; |
|
1047 |
|
1048 the_current_working_directory = (char *)NULL; |
|
1049 } |
|
1050 |
240
|
1051 if (! the_current_working_directory) |
1
|
1052 { |
|
1053 char *directory; |
|
1054 |
|
1055 the_current_working_directory = new char [MAXPATHLEN]; |
|
1056 directory = getcwd (the_current_working_directory, MAXPATHLEN); |
240
|
1057 if (! directory) |
1
|
1058 { |
|
1059 message (for_whom, the_current_working_directory); |
|
1060 delete [] the_current_working_directory; |
|
1061 the_current_working_directory = (char *)NULL; |
|
1062 return (char *)NULL; |
|
1063 } |
|
1064 } |
|
1065 |
|
1066 return the_current_working_directory; |
|
1067 } |
|
1068 |
|
1069 /* |
|
1070 * Do the work of changing to the directory NEWDIR. Handle symbolic |
|
1071 * link following, etc. |
|
1072 */ |
|
1073 int |
164
|
1074 change_to_directory (const char *newdir) |
1
|
1075 { |
|
1076 char *t; |
|
1077 |
|
1078 if (follow_symbolic_links) |
|
1079 { |
240
|
1080 if (! the_current_working_directory) |
1
|
1081 get_working_directory ("cd_links"); |
|
1082 |
|
1083 if (the_current_working_directory) |
|
1084 t = make_absolute (newdir, the_current_working_directory); |
|
1085 else |
|
1086 t = strsave (newdir); |
|
1087 |
|
1088 /* Get rid of trailing `/'. */ |
|
1089 { |
|
1090 register int len_t = strlen (t); |
|
1091 if (len_t > 1) |
|
1092 { |
|
1093 --len_t; |
|
1094 if (t[len_t] == '/') |
|
1095 t[len_t] = '\0'; |
|
1096 } |
|
1097 } |
|
1098 |
|
1099 if (chdir (t) < 0) |
|
1100 { |
|
1101 delete [] t; |
|
1102 return 0; |
|
1103 } |
|
1104 |
|
1105 if (the_current_working_directory) |
|
1106 strcpy (the_current_working_directory, t); |
|
1107 |
|
1108 delete [] t; |
|
1109 return 1; |
|
1110 } |
|
1111 else |
|
1112 { |
|
1113 if (chdir (newdir) < 0) |
|
1114 return 0; |
|
1115 else |
|
1116 return 1; |
|
1117 } |
|
1118 } |
|
1119 |
320
|
1120 #if !defined (HAVE_GETHOSTNAME) && defined (HAVE_SYS_UTSNAME_H) |
|
1121 extern "C" |
|
1122 { |
|
1123 #include <sys/utsname.h> |
|
1124 int |
|
1125 gethostname (char *name, int namelen) |
|
1126 { |
|
1127 int i; |
|
1128 struct utsname ut; |
|
1129 |
|
1130 --namelen; |
|
1131 |
|
1132 uname (&ut); |
|
1133 i = strlen (ut.nodename) + 1; |
|
1134 strncpy (name, ut.nodename, i < namelen ? i : namelen); |
|
1135 name[namelen] = '\0'; |
|
1136 |
|
1137 return 0; |
|
1138 } |
|
1139 } |
|
1140 #endif |
|
1141 |
1
|
1142 /* |
|
1143 * Has file `A' been modified after time `T'? |
|
1144 * |
|
1145 * case: |
|
1146 * |
|
1147 * a newer than t returns 1 |
|
1148 * a older than t returns 0 |
|
1149 * stat on a fails returns -1 |
|
1150 */ |
|
1151 int |
164
|
1152 is_newer (const char *fa, time_t t) |
1
|
1153 { |
|
1154 struct stat fa_sb; |
|
1155 register int fa_stat; |
|
1156 register int status = 0; |
|
1157 |
|
1158 fa_stat = stat (fa, &fa_sb); |
|
1159 if (fa_stat != 0) |
|
1160 status = -1; |
|
1161 |
|
1162 if (status != 0) |
|
1163 return status; |
|
1164 |
|
1165 return (fa_sb.st_mtime > t); |
|
1166 } |
|
1167 |
|
1168 /* |
|
1169 * Return to the main command loop in octave.cc. |
|
1170 */ |
189
|
1171 void |
1
|
1172 jump_to_top_level (void) |
|
1173 { |
|
1174 run_all_unwind_protects (); |
|
1175 |
|
1176 longjmp (toplevel, 1); |
|
1177 } |
|
1178 |
|
1179 /* |
|
1180 * Gag. |
|
1181 */ |
|
1182 char * |
|
1183 s_plural (int i) |
|
1184 { |
|
1185 static char *empty = ""; |
|
1186 static char *s = "s"; |
|
1187 return i == 1 ? empty : s; |
|
1188 } |
|
1189 |
|
1190 char * |
|
1191 es_plural (int i) |
|
1192 { |
|
1193 static char *empty = ""; |
|
1194 static char *es = "es"; |
|
1195 return i == 1 ? es : empty; |
|
1196 } |
|
1197 |
|
1198 char * |
|
1199 save_in_tmp_file (tree_constant& t, int ndim = 2, int parametric = 0) |
|
1200 { |
|
1201 char *name = strsave (tmpnam ((char *) NULL)); |
|
1202 if (name != (char *) NULL) |
|
1203 { |
|
1204 ofstream file (name); |
|
1205 if (file) |
|
1206 { |
|
1207 switch (ndim) |
|
1208 { |
|
1209 case 2: |
|
1210 t.save (file); |
|
1211 break; |
|
1212 case 3: |
|
1213 t.save_three_d (file, parametric); |
|
1214 break; |
|
1215 default: |
|
1216 panic_impossible (); |
|
1217 break; |
|
1218 } |
|
1219 } |
|
1220 else |
|
1221 { |
|
1222 error ("couldn't open temporary output file `%s'", name); |
|
1223 delete [] name; |
|
1224 name = (char *) NULL; |
|
1225 } |
|
1226 } |
|
1227 return name; |
|
1228 } |
|
1229 |
|
1230 void |
|
1231 mark_for_deletion (const char *filename) |
|
1232 { |
|
1233 char *tmp = strsave (filename); |
|
1234 tmp_files.push (tmp); |
|
1235 } |
|
1236 |
|
1237 void |
|
1238 cleanup_tmp_files (void) |
|
1239 { |
|
1240 while (! tmp_files.empty ()) |
|
1241 { |
|
1242 char *filename = tmp_files.pop (); |
|
1243 unlink (filename); |
|
1244 delete [] filename; |
|
1245 } |
|
1246 } |
|
1247 |
|
1248 int |
|
1249 send_to_plot_stream (const char *cmd) |
|
1250 { |
|
1251 // From sighandlers.cc: |
|
1252 extern int pipe_handler_error_count; |
|
1253 |
|
1254 static int initialized = 0; |
|
1255 |
|
1256 if (! plot_stream.is_open ()) |
|
1257 { |
|
1258 char *plot_prog = user_pref.gnuplot_binary; |
|
1259 if (plot_prog != (char *) NULL) |
|
1260 { |
|
1261 plot_stream.open (plot_prog); |
|
1262 if (! plot_stream.is_open ()) |
|
1263 { |
|
1264 warning ("plot: unable to open pipe to `%s'", |
|
1265 plot_prog); |
|
1266 |
|
1267 if (strcmp (plot_prog, "gnuplot") != 0) |
|
1268 { |
217
|
1269 warning ("having trouble finding plotting program."); |
|
1270 warning ("trying again with `gnuplot'"); |
1
|
1271 goto last_chance; |
|
1272 } |
|
1273 } |
|
1274 } |
|
1275 else |
|
1276 { |
|
1277 last_chance: |
|
1278 |
|
1279 plot_stream.open ("gnuplot"); |
|
1280 |
|
1281 if (! plot_stream.is_open ()) |
|
1282 { |
|
1283 error ("plot: unable to open pipe to `%s'", plot_prog); |
|
1284 return -1; |
|
1285 } |
|
1286 } |
|
1287 } |
|
1288 |
|
1289 if (! initialized) |
|
1290 { |
|
1291 initialized = 1; |
|
1292 plot_stream << "set data style lines\n"; |
|
1293 } |
|
1294 |
|
1295 plot_stream << cmd; |
|
1296 plot_stream.flush (); |
|
1297 pipe_handler_error_count = 0; |
|
1298 |
|
1299 return 0; |
|
1300 } |
|
1301 |
|
1302 void |
|
1303 close_plot_stream (void) |
|
1304 { |
|
1305 if (plot_stream.is_open ()) |
|
1306 plot_stream.close (); |
|
1307 } |
|
1308 |
|
1309 int |
287
|
1310 almost_match (const char *std, const char *s, |
|
1311 int min_match_len = 1, int case_sens = 1) |
1
|
1312 { |
|
1313 int stdlen = strlen (std); |
|
1314 int slen = strlen (s); |
|
1315 |
|
1316 return (slen <= stdlen |
|
1317 && slen >= min_match_len |
287
|
1318 && (case_sens |
|
1319 ? (strncmp (std, s, slen) == 0) |
|
1320 : (strncasecmp (std, s, slen) == 0))); |
|
1321 } |
|
1322 |
|
1323 /* |
|
1324 * Ugh. |
|
1325 */ |
|
1326 int |
|
1327 keyword_almost_match (const char **std, int *min_len, const char *s, |
|
1328 int min_toks_to_match, int max_toks) |
|
1329 { |
|
1330 int status = 0; |
|
1331 int tok_count = 0; |
|
1332 int toks_matched = 0; |
|
1333 |
|
1334 if (s == NULL || *s == '\0' || max_toks < 1) |
|
1335 return status; |
|
1336 |
|
1337 char *kw = strsave (s); |
|
1338 |
|
1339 char *t = kw; |
|
1340 while (*t != '\0') |
|
1341 { |
|
1342 if (*t == '\t') |
|
1343 *t = ' '; |
|
1344 t++; |
|
1345 } |
|
1346 |
|
1347 char *beg = kw; |
|
1348 while (*beg == ' ') |
|
1349 beg++; |
|
1350 |
|
1351 if (*beg == '\0') |
|
1352 return status; |
|
1353 |
|
1354 |
|
1355 char **to_match = new char * [max_toks + 1]; |
|
1356 char **s1 = std; |
|
1357 char **s2 = to_match; |
|
1358 |
|
1359 if (s1 == NULL || s2 == NULL) |
|
1360 goto done; |
|
1361 |
|
1362 s2[tok_count] = beg; |
|
1363 char *end; |
|
1364 while ((end = strchr (beg, ' ')) != NULL) |
|
1365 { |
|
1366 *end = '\0'; |
|
1367 beg = end + 1; |
|
1368 |
|
1369 while (*beg == ' ') |
|
1370 beg++; |
|
1371 |
|
1372 if (*beg == '\0') |
|
1373 break; |
|
1374 |
|
1375 tok_count++; |
|
1376 if (tok_count >= max_toks) |
|
1377 goto done; |
|
1378 |
|
1379 s2[tok_count] = beg; |
|
1380 } |
|
1381 s2[tok_count+1] = NULL; |
|
1382 |
|
1383 s2 = to_match; |
|
1384 |
|
1385 for (;;) |
|
1386 { |
|
1387 if (! almost_match (*s1, *s2, min_len[toks_matched], 0)) |
|
1388 goto done; |
|
1389 |
|
1390 toks_matched++; |
|
1391 |
|
1392 s1++; |
|
1393 s2++; |
|
1394 |
|
1395 if (! *s2) |
|
1396 { |
|
1397 status = (toks_matched >= min_toks_to_match); |
|
1398 goto done; |
|
1399 } |
|
1400 |
|
1401 if (! *s1) |
|
1402 goto done; |
|
1403 } |
|
1404 |
|
1405 done: |
|
1406 |
|
1407 delete [] kw; |
|
1408 delete [] to_match; |
|
1409 |
|
1410 return status; |
1
|
1411 } |
|
1412 |
|
1413 char ** |
164
|
1414 get_m_file_names (int& num, const char *dir, int no_suffix) |
1
|
1415 { |
|
1416 static int num_max = 256; |
|
1417 char **retval = new char * [num_max]; |
|
1418 int i = 0; |
|
1419 |
|
1420 DIR *dirp = opendir (dir); |
|
1421 if (dirp != (DIR *) NULL) |
|
1422 { |
|
1423 struct dirent *entry; |
|
1424 while ((entry = readdir (dirp)) != (struct dirent *) NULL) |
|
1425 { |
|
1426 int len = NLENGTH (entry); |
|
1427 if (len > 2 |
|
1428 && entry->d_name[len-2] == '.' |
|
1429 && entry->d_name[len-1] == 'm') |
|
1430 { |
|
1431 retval[i] = strsave (entry->d_name); |
|
1432 if (no_suffix) |
|
1433 retval[i][len-2] = '\0'; |
|
1434 |
|
1435 i++; |
|
1436 |
|
1437 if (i == num_max - 1) |
|
1438 { |
244
|
1439 // Reallocate the array. Only copy pointers, not the strings they |
|
1440 // point to, then only delete the original array of pointers, and not |
|
1441 // the strings they point to. |
|
1442 |
1
|
1443 num_max += 256; |
|
1444 char **tmp = new char * [num_max]; |
|
1445 for (int j = 0; j < i; j++) |
|
1446 tmp[j] = retval[j]; |
|
1447 |
244
|
1448 delete [] retval; |
|
1449 |
1
|
1450 retval = tmp; |
|
1451 } |
|
1452 } |
|
1453 } |
106
|
1454 closedir (dirp); |
1
|
1455 } |
|
1456 |
|
1457 retval[i] = (char *) NULL; |
|
1458 num = i; |
|
1459 |
|
1460 return retval; |
|
1461 } |
|
1462 |
|
1463 char ** |
|
1464 get_m_file_names (int& num, int no_suffix) |
|
1465 { |
|
1466 static int num_max = 1024; |
|
1467 char **retval = new char * [num_max]; |
|
1468 int i = 0; |
|
1469 |
|
1470 char **path = pathstring_to_vector (user_pref.loadpath); |
|
1471 |
|
1472 char **ptr = path; |
|
1473 if (ptr != (char **) NULL) |
|
1474 { |
|
1475 while (*ptr != (char *) NULL) |
|
1476 { |
|
1477 int tmp_num; |
|
1478 char **names = get_m_file_names (tmp_num, *ptr, no_suffix); |
|
1479 |
|
1480 if (i + tmp_num >= num_max - 1) |
|
1481 { |
244
|
1482 // Reallocate the array. Only copy pointers, not the strings they |
|
1483 // point to, then only delete the original array of pointers, and not |
|
1484 // the strings they point to. |
|
1485 |
1
|
1486 num_max += 1024; |
|
1487 char **tmp = new char * [num_max]; |
|
1488 for (int j = 0; j < i; j++) |
|
1489 tmp[j] = retval[j]; |
|
1490 |
244
|
1491 delete [] retval; |
|
1492 |
1
|
1493 retval = tmp; |
|
1494 } |
|
1495 |
|
1496 int k = 0; |
|
1497 while (k < tmp_num) |
|
1498 retval[i++] = names[k++]; |
|
1499 |
|
1500 ptr++; |
|
1501 } |
|
1502 } |
|
1503 |
|
1504 retval[i] = (char *) NULL; |
|
1505 num = i; |
|
1506 |
|
1507 return retval; |
|
1508 } |
|
1509 |
|
1510 int |
|
1511 NINT (double x) |
|
1512 { |
|
1513 if (x > INT_MAX) |
|
1514 return INT_MAX; |
|
1515 else if (x < INT_MIN) |
|
1516 return INT_MIN; |
|
1517 else |
|
1518 return (x > 0) ? ((int) (x + 0.5)) : ((int) (x - 0.5)); |
|
1519 } |
|
1520 |
|
1521 double |
|
1522 D_NINT (double x) |
|
1523 { |
|
1524 if (xisinf (x) || xisnan (x)) |
|
1525 return x; |
|
1526 else |
|
1527 return floor (x + 0.5); |
|
1528 } |
|
1529 |
|
1530 /* |
|
1531 ;;; Local Variables: *** |
|
1532 ;;; mode: C++ *** |
|
1533 ;;; page-delimiter: "^/\\*" *** |
|
1534 ;;; End: *** |
|
1535 */ |