1
|
1 // utils.cc -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1992, 1993 John W. Eaton |
|
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 |
|
35 change_to_directory |
|
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 |
|
1120 /* |
|
1121 * Has file `A' been modified after time `T'? |
|
1122 * |
|
1123 * case: |
|
1124 * |
|
1125 * a newer than t returns 1 |
|
1126 * a older than t returns 0 |
|
1127 * stat on a fails returns -1 |
|
1128 */ |
|
1129 int |
164
|
1130 is_newer (const char *fa, time_t t) |
1
|
1131 { |
|
1132 struct stat fa_sb; |
|
1133 register int fa_stat; |
|
1134 register int status = 0; |
|
1135 |
|
1136 fa_stat = stat (fa, &fa_sb); |
|
1137 if (fa_stat != 0) |
|
1138 status = -1; |
|
1139 |
|
1140 if (status != 0) |
|
1141 return status; |
|
1142 |
|
1143 return (fa_sb.st_mtime > t); |
|
1144 } |
|
1145 |
|
1146 /* |
|
1147 * Return to the main command loop in octave.cc. |
|
1148 */ |
189
|
1149 void |
1
|
1150 jump_to_top_level (void) |
|
1151 { |
|
1152 run_all_unwind_protects (); |
|
1153 |
|
1154 longjmp (toplevel, 1); |
|
1155 } |
|
1156 |
|
1157 /* |
|
1158 * Gag. |
|
1159 */ |
|
1160 char * |
|
1161 s_plural (int i) |
|
1162 { |
|
1163 static char *empty = ""; |
|
1164 static char *s = "s"; |
|
1165 return i == 1 ? empty : s; |
|
1166 } |
|
1167 |
|
1168 char * |
|
1169 es_plural (int i) |
|
1170 { |
|
1171 static char *empty = ""; |
|
1172 static char *es = "es"; |
|
1173 return i == 1 ? es : empty; |
|
1174 } |
|
1175 |
|
1176 char * |
|
1177 save_in_tmp_file (tree_constant& t, int ndim = 2, int parametric = 0) |
|
1178 { |
|
1179 char *name = strsave (tmpnam ((char *) NULL)); |
|
1180 if (name != (char *) NULL) |
|
1181 { |
|
1182 ofstream file (name); |
|
1183 if (file) |
|
1184 { |
|
1185 switch (ndim) |
|
1186 { |
|
1187 case 2: |
|
1188 t.save (file); |
|
1189 break; |
|
1190 case 3: |
|
1191 t.save_three_d (file, parametric); |
|
1192 break; |
|
1193 default: |
|
1194 panic_impossible (); |
|
1195 break; |
|
1196 } |
|
1197 } |
|
1198 else |
|
1199 { |
|
1200 error ("couldn't open temporary output file `%s'", name); |
|
1201 delete [] name; |
|
1202 name = (char *) NULL; |
|
1203 } |
|
1204 } |
|
1205 return name; |
|
1206 } |
|
1207 |
|
1208 void |
|
1209 mark_for_deletion (const char *filename) |
|
1210 { |
|
1211 char *tmp = strsave (filename); |
|
1212 tmp_files.push (tmp); |
|
1213 } |
|
1214 |
|
1215 void |
|
1216 cleanup_tmp_files (void) |
|
1217 { |
|
1218 while (! tmp_files.empty ()) |
|
1219 { |
|
1220 char *filename = tmp_files.pop (); |
|
1221 unlink (filename); |
|
1222 delete [] filename; |
|
1223 } |
|
1224 } |
|
1225 |
|
1226 int |
|
1227 send_to_plot_stream (const char *cmd) |
|
1228 { |
|
1229 // From sighandlers.cc: |
|
1230 extern int pipe_handler_error_count; |
|
1231 |
|
1232 static int initialized = 0; |
|
1233 |
|
1234 if (! plot_stream.is_open ()) |
|
1235 { |
|
1236 char *plot_prog = user_pref.gnuplot_binary; |
|
1237 if (plot_prog != (char *) NULL) |
|
1238 { |
|
1239 plot_stream.open (plot_prog); |
|
1240 if (! plot_stream.is_open ()) |
|
1241 { |
|
1242 warning ("plot: unable to open pipe to `%s'", |
|
1243 plot_prog); |
|
1244 |
|
1245 if (strcmp (plot_prog, "gnuplot") != 0) |
|
1246 { |
217
|
1247 warning ("having trouble finding plotting program."); |
|
1248 warning ("trying again with `gnuplot'"); |
1
|
1249 goto last_chance; |
|
1250 } |
|
1251 } |
|
1252 } |
|
1253 else |
|
1254 { |
|
1255 last_chance: |
|
1256 |
|
1257 plot_stream.open ("gnuplot"); |
|
1258 |
|
1259 if (! plot_stream.is_open ()) |
|
1260 { |
|
1261 error ("plot: unable to open pipe to `%s'", plot_prog); |
|
1262 return -1; |
|
1263 } |
|
1264 } |
|
1265 } |
|
1266 |
|
1267 if (! initialized) |
|
1268 { |
|
1269 initialized = 1; |
|
1270 plot_stream << "set data style lines\n"; |
|
1271 } |
|
1272 |
|
1273 plot_stream << cmd; |
|
1274 plot_stream.flush (); |
|
1275 pipe_handler_error_count = 0; |
|
1276 |
|
1277 return 0; |
|
1278 } |
|
1279 |
|
1280 void |
|
1281 close_plot_stream (void) |
|
1282 { |
|
1283 if (plot_stream.is_open ()) |
|
1284 plot_stream.close (); |
|
1285 } |
|
1286 |
|
1287 int |
287
|
1288 almost_match (const char *std, const char *s, |
|
1289 int min_match_len = 1, int case_sens = 1) |
1
|
1290 { |
|
1291 int stdlen = strlen (std); |
|
1292 int slen = strlen (s); |
|
1293 |
|
1294 return (slen <= stdlen |
|
1295 && slen >= min_match_len |
287
|
1296 && (case_sens |
|
1297 ? (strncmp (std, s, slen) == 0) |
|
1298 : (strncasecmp (std, s, slen) == 0))); |
|
1299 } |
|
1300 |
|
1301 /* |
|
1302 * Ugh. |
|
1303 */ |
|
1304 int |
|
1305 keyword_almost_match (const char **std, int *min_len, const char *s, |
|
1306 int min_toks_to_match, int max_toks) |
|
1307 { |
|
1308 int status = 0; |
|
1309 int tok_count = 0; |
|
1310 int toks_matched = 0; |
|
1311 |
|
1312 if (s == NULL || *s == '\0' || max_toks < 1) |
|
1313 return status; |
|
1314 |
|
1315 char *kw = strsave (s); |
|
1316 |
|
1317 char *t = kw; |
|
1318 while (*t != '\0') |
|
1319 { |
|
1320 if (*t == '\t') |
|
1321 *t = ' '; |
|
1322 t++; |
|
1323 } |
|
1324 |
|
1325 char *beg = kw; |
|
1326 while (*beg == ' ') |
|
1327 beg++; |
|
1328 |
|
1329 if (*beg == '\0') |
|
1330 return status; |
|
1331 |
|
1332 |
|
1333 char **to_match = new char * [max_toks + 1]; |
|
1334 char **s1 = std; |
|
1335 char **s2 = to_match; |
|
1336 |
|
1337 if (s1 == NULL || s2 == NULL) |
|
1338 goto done; |
|
1339 |
|
1340 s2[tok_count] = beg; |
|
1341 char *end; |
|
1342 while ((end = strchr (beg, ' ')) != NULL) |
|
1343 { |
|
1344 *end = '\0'; |
|
1345 beg = end + 1; |
|
1346 |
|
1347 while (*beg == ' ') |
|
1348 beg++; |
|
1349 |
|
1350 if (*beg == '\0') |
|
1351 break; |
|
1352 |
|
1353 tok_count++; |
|
1354 if (tok_count >= max_toks) |
|
1355 goto done; |
|
1356 |
|
1357 s2[tok_count] = beg; |
|
1358 } |
|
1359 s2[tok_count+1] = NULL; |
|
1360 |
|
1361 s2 = to_match; |
|
1362 |
|
1363 for (;;) |
|
1364 { |
|
1365 if (! almost_match (*s1, *s2, min_len[toks_matched], 0)) |
|
1366 goto done; |
|
1367 |
|
1368 toks_matched++; |
|
1369 |
|
1370 s1++; |
|
1371 s2++; |
|
1372 |
|
1373 if (! *s2) |
|
1374 { |
|
1375 status = (toks_matched >= min_toks_to_match); |
|
1376 goto done; |
|
1377 } |
|
1378 |
|
1379 if (! *s1) |
|
1380 goto done; |
|
1381 } |
|
1382 |
|
1383 done: |
|
1384 |
|
1385 delete [] kw; |
|
1386 delete [] to_match; |
|
1387 |
|
1388 return status; |
1
|
1389 } |
|
1390 |
|
1391 char ** |
164
|
1392 get_m_file_names (int& num, const char *dir, int no_suffix) |
1
|
1393 { |
|
1394 static int num_max = 256; |
|
1395 char **retval = new char * [num_max]; |
|
1396 int i = 0; |
|
1397 |
|
1398 DIR *dirp = opendir (dir); |
|
1399 if (dirp != (DIR *) NULL) |
|
1400 { |
|
1401 struct dirent *entry; |
|
1402 while ((entry = readdir (dirp)) != (struct dirent *) NULL) |
|
1403 { |
|
1404 int len = NLENGTH (entry); |
|
1405 if (len > 2 |
|
1406 && entry->d_name[len-2] == '.' |
|
1407 && entry->d_name[len-1] == 'm') |
|
1408 { |
|
1409 retval[i] = strsave (entry->d_name); |
|
1410 if (no_suffix) |
|
1411 retval[i][len-2] = '\0'; |
|
1412 |
|
1413 i++; |
|
1414 |
|
1415 if (i == num_max - 1) |
|
1416 { |
244
|
1417 // Reallocate the array. Only copy pointers, not the strings they |
|
1418 // point to, then only delete the original array of pointers, and not |
|
1419 // the strings they point to. |
|
1420 |
1
|
1421 num_max += 256; |
|
1422 char **tmp = new char * [num_max]; |
|
1423 for (int j = 0; j < i; j++) |
|
1424 tmp[j] = retval[j]; |
|
1425 |
244
|
1426 delete [] retval; |
|
1427 |
1
|
1428 retval = tmp; |
|
1429 } |
|
1430 } |
|
1431 } |
106
|
1432 closedir (dirp); |
1
|
1433 } |
|
1434 |
|
1435 retval[i] = (char *) NULL; |
|
1436 num = i; |
|
1437 |
|
1438 return retval; |
|
1439 } |
|
1440 |
|
1441 char ** |
|
1442 get_m_file_names (int& num, int no_suffix) |
|
1443 { |
|
1444 static int num_max = 1024; |
|
1445 char **retval = new char * [num_max]; |
|
1446 int i = 0; |
|
1447 |
|
1448 char **path = pathstring_to_vector (user_pref.loadpath); |
|
1449 |
|
1450 char **ptr = path; |
|
1451 if (ptr != (char **) NULL) |
|
1452 { |
|
1453 while (*ptr != (char *) NULL) |
|
1454 { |
|
1455 int tmp_num; |
|
1456 char **names = get_m_file_names (tmp_num, *ptr, no_suffix); |
|
1457 |
|
1458 if (i + tmp_num >= num_max - 1) |
|
1459 { |
244
|
1460 // Reallocate the array. Only copy pointers, not the strings they |
|
1461 // point to, then only delete the original array of pointers, and not |
|
1462 // the strings they point to. |
|
1463 |
1
|
1464 num_max += 1024; |
|
1465 char **tmp = new char * [num_max]; |
|
1466 for (int j = 0; j < i; j++) |
|
1467 tmp[j] = retval[j]; |
|
1468 |
244
|
1469 delete [] retval; |
|
1470 |
1
|
1471 retval = tmp; |
|
1472 } |
|
1473 |
|
1474 int k = 0; |
|
1475 while (k < tmp_num) |
|
1476 retval[i++] = names[k++]; |
|
1477 |
|
1478 ptr++; |
|
1479 } |
|
1480 } |
|
1481 |
|
1482 retval[i] = (char *) NULL; |
|
1483 num = i; |
|
1484 |
|
1485 return retval; |
|
1486 } |
|
1487 |
|
1488 int |
|
1489 NINT (double x) |
|
1490 { |
|
1491 if (x > INT_MAX) |
|
1492 return INT_MAX; |
|
1493 else if (x < INT_MIN) |
|
1494 return INT_MIN; |
|
1495 else |
|
1496 return (x > 0) ? ((int) (x + 0.5)) : ((int) (x - 0.5)); |
|
1497 } |
|
1498 |
|
1499 double |
|
1500 D_NINT (double x) |
|
1501 { |
|
1502 if (xisinf (x) || xisnan (x)) |
|
1503 return x; |
|
1504 else |
|
1505 return floor (x + 0.5); |
|
1506 } |
|
1507 |
|
1508 /* |
|
1509 ;;; Local Variables: *** |
|
1510 ;;; mode: C++ *** |
|
1511 ;;; page-delimiter: "^/\\*" *** |
|
1512 ;;; End: *** |
|
1513 */ |