1
|
1 // sysdep.cc -*- C++ -*- |
|
2 /* |
|
3 |
1009
|
4 Copyright (C) 1993, 1994, 1995 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 |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
21 |
|
22 */ |
|
23 |
661
|
24 /* |
|
25 |
|
26 The function gethostname was adapted from a similar function from GNU |
|
27 Bash, the Bourne Again SHell, copyright (C) 1987, 1989, 1991 Free |
|
28 Software Foundation, Inc. |
|
29 |
|
30 */ |
|
31 |
240
|
32 #ifdef HAVE_CONFIG_H |
1192
|
33 #include <config.h> |
1
|
34 #endif |
|
35 |
1346
|
36 #include <cfloat> |
1343
|
37 #include <cmath> |
|
38 #include <cstddef> |
1346
|
39 #include <cstdio> |
1343
|
40 #include <cstdlib> |
|
41 #include <cstring> |
|
42 |
1350
|
43 #ifdef HAVE_UNISTD_H |
529
|
44 #include <sys/types.h> |
|
45 #include <unistd.h> |
|
46 #endif |
1
|
47 |
1352
|
48 #include "defun.h" |
|
49 #include "error.h" |
|
50 #include "f77-uscore.h" |
542
|
51 #include "help.h" |
529
|
52 #include "input.h" |
1352
|
53 #include "oct-obj.h" |
|
54 #include "octave.h" |
|
55 #include "sysdep.h" |
|
56 #include "tree-const.h" |
529
|
57 #include "utils.h" |
|
58 |
|
59 extern "C" |
|
60 { |
|
61 #include <readline/readline.h> |
|
62 |
|
63 extern char *term_clrpag; |
|
64 extern void _rl_output_character_function (); |
|
65 |
|
66 #if defined (HAVE_TERMIOS_H) |
|
67 #include <termios.h> |
|
68 #elif defined (HAVE_TERMIO_H) |
|
69 #include <termio.h> |
|
70 #elif defined (HAVE_SGTTY_H) |
|
71 #include <sgtty.h> |
|
72 #else |
|
73 LOSE! LOSE! |
|
74 #endif |
1226
|
75 |
1255
|
76 extern double F77_FCN (d1mach, D1MACH) (const int&); |
529
|
77 } |
|
78 |
|
79 #ifndef STDIN_FILENO |
|
80 #define STDIN_FILENO 1 |
|
81 #endif |
444
|
82 |
|
83 // Octave's idea of infinity. |
|
84 double octave_Inf; |
|
85 |
|
86 // Octave's idea of not a number. |
|
87 double octave_NaN; |
1
|
88 |
1226
|
89 // The floating point format on this system. |
|
90 floating_point_format native_float_format = OCTAVE_UNKNOWN_FLT_FMT; |
|
91 |
959
|
92 #if defined (HAVE_FLOATINGPOINT_H) |
401
|
93 #include <floatingpoint.h> |
|
94 #endif |
|
95 |
1
|
96 #ifdef NeXT |
|
97 extern "C" |
|
98 { |
|
99 typedef void (*_cplus_fcn_int) (int); |
|
100 extern void (*malloc_error (_cplus_fcn_int)) (int); |
|
101 } |
|
102 |
|
103 static void |
|
104 malloc_handler (int code) |
|
105 { |
|
106 if (code == 5) |
217
|
107 warning ("hopefully recoverable malloc error: freeing wild pointer"); |
1
|
108 else |
|
109 { |
|
110 panic ("probably irrecoverable malloc error: code %d", code); |
|
111 } |
|
112 } |
|
113 |
|
114 static void |
|
115 NeXT_init (void) |
|
116 { |
|
117 malloc_error (malloc_handler); |
|
118 } |
|
119 #endif |
|
120 |
1226
|
121 union equiv |
|
122 { |
|
123 double d; |
|
124 int i[2]; |
|
125 }; |
|
126 |
|
127 struct float_params |
|
128 { |
|
129 floating_point_format fp_fmt; |
|
130 equiv fp_par[4]; |
|
131 }; |
|
132 |
|
133 #define INIT_FLT_PAR(fp, fmt, sm1, sm2, lrg1, lrg2, rt1, rt2, dv1, dv2) \ |
|
134 do \ |
|
135 { \ |
|
136 fp.fp_fmt = (fmt); \ |
|
137 fp.fp_par[0].i[0] = (sm1); fp.fp_par[0].i[1] = (sm2); \ |
|
138 fp.fp_par[1].i[0] = (lrg1); fp.fp_par[1].i[1] = (lrg2); \ |
|
139 fp.fp_par[2].i[0] = (rt1); fp.fp_par[2].i[1] = (rt2); \ |
|
140 fp.fp_par[3].i[0] = (dv1); fp.fp_par[3].i[1] = (dv2); \ |
|
141 } \ |
|
142 while (0) |
|
143 |
|
144 static int |
|
145 equiv_compare (const equiv *std, const equiv *v, int len) |
|
146 { |
|
147 int i; |
|
148 for (i = 0; i < len; i++) |
|
149 if (v[i].i[0] != std[i].i[0] || v[i].i[1] != std[i].i[1]) |
|
150 return 0; |
|
151 return 1; |
|
152 } |
|
153 |
444
|
154 static void |
|
155 octave_ieee_init (void) |
|
156 { |
|
157 #if defined (HAVE_ISINF) || defined (HAVE_FINITE) |
|
158 |
|
159 // Some version of gcc on some old version of Linux used to crash when |
|
160 // trying to make Inf and NaN. |
|
161 |
|
162 #if defined (HAVE_INFINITY) |
470
|
163 octave_Inf = (double) infinity (); |
967
|
164 #elif defined (linux) |
444
|
165 octave_Inf = HUGE_VAL; |
967
|
166 #elif defined (__alpha__) |
529
|
167 extern unsigned int DINFINITY[2]; |
|
168 octave_Inf = (*((double *) (DINFINITY))); |
|
169 #else |
444
|
170 double tmp = 1e+10; |
|
171 octave_Inf = tmp; |
|
172 for (;;) |
|
173 { |
|
174 octave_Inf *= 1e+10; |
|
175 if (octave_Inf == tmp) |
|
176 break; |
|
177 tmp = octave_Inf; |
|
178 } |
|
179 #endif |
529
|
180 |
444
|
181 #if defined (HAVE_QUIET_NAN) |
470
|
182 octave_NaN = (double) quiet_nan (); |
967
|
183 #elif defined (linux) |
444
|
184 octave_NaN = NAN; |
967
|
185 #elif defined (__alpha__) |
529
|
186 extern unsigned int DQNAN[2]; |
|
187 octave_NaN = (*((double *) (DQNAN))); |
|
188 #else |
444
|
189 octave_NaN = octave_Inf / octave_Inf; |
|
190 #endif |
|
191 |
|
192 #else |
|
193 |
1358
|
194 // This is sort of cheesy, but what can we do, other than blowing it |
|
195 // off completely, or writing an entire IEEE emulation package? |
444
|
196 |
|
197 octave_Inf = DBL_MAX; |
|
198 octave_NaN = DBL_MAX; |
|
199 |
|
200 #endif |
1226
|
201 |
|
202 float_params fp[5]; |
|
203 |
|
204 INIT_FLT_PAR (fp[0], OCTAVE_IEEE_BIG, |
|
205 1048576, 0, |
|
206 2146435071, -1, |
|
207 1017118720, 0, |
|
208 1018167296, 0); |
|
209 |
|
210 INIT_FLT_PAR (fp[1], OCTAVE_IEEE_LITTLE, |
|
211 0, 1048576, |
|
212 -1, 2146435071, |
|
213 0, 1017118720, |
|
214 0, 1018167296); |
|
215 |
|
216 INIT_FLT_PAR (fp[2], OCTAVE_VAX_D, |
|
217 128, 0, |
|
218 -32769, -1, |
|
219 9344, 0, |
|
220 9344, 0); |
|
221 |
|
222 INIT_FLT_PAR (fp[3], OCTAVE_VAX_G, |
|
223 16, 0, |
|
224 -32769, -1, |
|
225 15552, 0, |
|
226 15552, 0); |
|
227 |
|
228 INIT_FLT_PAR (fp[4], OCTAVE_UNKNOWN_FLT_FMT, |
|
229 0, 0, |
|
230 0, 0, |
|
231 0, 0, |
|
232 0, 0); |
|
233 |
|
234 equiv mach_fp_par[4]; |
|
235 |
1255
|
236 mach_fp_par[0].d = F77_FCN (d1mach, D1MACH) (1); |
|
237 mach_fp_par[1].d = F77_FCN (d1mach, D1MACH) (2); |
|
238 mach_fp_par[2].d = F77_FCN (d1mach, D1MACH) (3); |
|
239 mach_fp_par[3].d = F77_FCN (d1mach, D1MACH) (4); |
1226
|
240 |
|
241 int i = 0; |
|
242 do |
|
243 { |
|
244 if (equiv_compare (fp[i].fp_par, mach_fp_par, 4)) |
|
245 { |
|
246 native_float_format = fp[i].fp_fmt; |
|
247 break; |
|
248 } |
|
249 } |
|
250 while (fp[++i].fp_fmt != OCTAVE_UNKNOWN_FLT_FMT); |
|
251 |
|
252 if (native_float_format == OCTAVE_UNKNOWN_FLT_FMT) |
|
253 panic ("unrecognized floating point format!"); |
444
|
254 } |
|
255 |
505
|
256 #if defined (EXCEPTION_IN_MATH) |
|
257 extern "C" |
|
258 { |
|
259 int |
|
260 matherr (struct exception *x) |
|
261 { |
1358
|
262 // Possibly print our own message someday. Should probably be |
|
263 // user-switchable. |
505
|
264 |
|
265 switch (x->type) |
|
266 { |
|
267 case DOMAIN: |
|
268 case SING: |
|
269 case OVERFLOW: |
|
270 case UNDERFLOW: |
|
271 case TLOSS: |
|
272 case PLOSS: |
|
273 default: |
|
274 break; |
|
275 } |
|
276 |
1358
|
277 // But don't print the system message. |
505
|
278 |
|
279 return 1; |
|
280 } |
|
281 } |
|
282 #endif |
|
283 |
1
|
284 void |
|
285 sysdep_init (void) |
|
286 { |
959
|
287 #if defined (__386BSD__) || defined (__FreeBSD__) |
998
|
288 #if defined (HAVE_FLOATINGPOINT_H) |
1358
|
289 // Disable trapping on common exceptions. |
401
|
290 fpsetmask (~(FP_X_OFL|FP_X_INV|FP_X_DZ|FP_X_DNML|FP_X_UFL|FP_X_IMP)); |
|
291 #endif |
959
|
292 #endif |
401
|
293 |
1
|
294 #ifdef NeXT |
|
295 NeXT_init (); |
|
296 #endif |
444
|
297 |
|
298 octave_ieee_init (); |
1
|
299 } |
|
300 |
767
|
301 // Set terminal in raw mode. From less-177. |
|
302 // |
|
303 // Change terminal to "raw mode", or restore to "normal" mode. |
|
304 // "Raw mode" means |
|
305 // 1. An outstanding read will complete on receipt of a single keystroke. |
|
306 // 2. Input is not echoed. |
|
307 // 3. On output, \n is mapped to \r\n. |
|
308 // 4. \t is NOT expanded into spaces. |
|
309 // 5. Signal-causing characters such as ctrl-C (interrupt), |
|
310 // etc. are NOT disabled. |
|
311 // It doesn't matter whether an input \n is mapped to \r, or vice versa. |
|
312 |
529
|
313 void |
|
314 raw_mode (int on) |
|
315 { |
|
316 static int curr_on = 0; |
|
317 |
|
318 int tty_fd = STDIN_FILENO; |
|
319 if (! isatty (tty_fd)) |
|
320 { |
|
321 if (interactive) |
|
322 error ("stdin is not a tty!"); |
|
323 return; |
|
324 } |
|
325 |
|
326 if (on == curr_on) |
|
327 return; |
|
328 |
|
329 #if defined (HAVE_TERMIOS_H) |
|
330 { |
|
331 struct termios s; |
|
332 static struct termios save_term; |
|
333 |
|
334 if (on) |
|
335 { |
1358
|
336 // Get terminal modes. |
529
|
337 |
|
338 tcgetattr (tty_fd, &s); |
|
339 |
1358
|
340 // Save modes and set certain variables dependent on modes. |
529
|
341 |
|
342 save_term = s; |
|
343 // ospeed = s.c_cflag & CBAUD; |
|
344 // erase_char = s.c_cc[VERASE]; |
|
345 // kill_char = s.c_cc[VKILL]; |
|
346 |
1358
|
347 // Set the modes to the way we want them. |
529
|
348 |
|
349 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL); |
|
350 s.c_oflag |= (OPOST|ONLCR); |
|
351 #if defined (OCRNL) |
|
352 s.c_oflag &= ~(OCRNL); |
|
353 #endif |
|
354 #if defined (ONOCR) |
|
355 s.c_oflag &= ~(ONOCR); |
|
356 #endif |
|
357 #if defined (ONLRET) |
|
358 s.c_oflag &= ~(ONLRET); |
|
359 #endif |
|
360 s.c_cc[VMIN] = 1; |
|
361 s.c_cc[VTIME] = 0; |
|
362 } |
|
363 else |
|
364 { |
1358
|
365 // Restore saved modes. |
|
366 |
529
|
367 s = save_term; |
|
368 } |
|
369 tcsetattr (tty_fd, TCSAFLUSH, &s); |
|
370 } |
|
371 #elif defined (HAVE_TERMIO_H) |
|
372 { |
|
373 struct termio s; |
|
374 static struct termio save_term; |
|
375 |
|
376 if (on) |
|
377 { |
1358
|
378 // Get terminal modes. |
529
|
379 |
|
380 ioctl (tty_fd, TCGETA, &s); |
|
381 |
1358
|
382 // Save modes and set certain variables dependent on modes. |
529
|
383 |
|
384 save_term = s; |
|
385 // ospeed = s.c_cflag & CBAUD; |
|
386 // erase_char = s.c_cc[VERASE]; |
|
387 // kill_char = s.c_cc[VKILL]; |
|
388 |
1358
|
389 // Set the modes to the way we want them. |
529
|
390 |
|
391 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL); |
|
392 s.c_oflag |= (OPOST|ONLCR); |
|
393 #if defined (OCRNL) |
|
394 s.c_oflag &= ~(OCRNL); |
|
395 #endif |
|
396 #if defined (ONOCR) |
|
397 s.c_oflag &= ~(ONOCR); |
|
398 #endif |
|
399 #if defined (ONLRET) |
|
400 s.c_oflag &= ~(ONLRET); |
|
401 #endif |
|
402 s.c_cc[VMIN] = 1; |
|
403 s.c_cc[VTIME] = 0; |
|
404 } |
|
405 else |
|
406 { |
1358
|
407 // Restore saved modes. |
|
408 |
529
|
409 s = save_term; |
|
410 } |
|
411 ioctl (tty_fd, TCSETAW, &s); |
|
412 } |
|
413 #elif defined (HAVE_SGTTY_H) |
|
414 { |
|
415 struct sgttyb s; |
|
416 static struct sgttyb save_term; |
|
417 |
|
418 if (on) |
|
419 { |
1358
|
420 // Get terminal modes. |
529
|
421 |
|
422 ioctl (tty_fd, TIOCGETP, &s); |
|
423 |
1358
|
424 // Save modes and set certain variables dependent on modes. |
529
|
425 |
|
426 save_term = s; |
|
427 // ospeed = s.sg_ospeed; |
|
428 // erase_char = s.sg_erase; |
|
429 // kill_char = s.sg_kill; |
|
430 |
1358
|
431 // Set the modes to the way we want them. |
529
|
432 |
|
433 s.sg_flags |= CBREAK; |
|
434 s.sg_flags &= ~(ECHO); |
|
435 } |
|
436 else |
|
437 { |
1358
|
438 // Restore saved modes. |
|
439 |
529
|
440 s = save_term; |
|
441 } |
|
442 ioctl (tty_fd, TIOCSETN, &s); |
|
443 } |
|
444 #else |
|
445 LOSE! LOSE! |
|
446 #endif |
|
447 |
|
448 curr_on = on; |
|
449 } |
|
450 |
767
|
451 // Read one character from the terminal. |
|
452 |
529
|
453 int |
|
454 kbhit (void) |
|
455 { |
|
456 int c; |
|
457 raw_mode (1); |
|
458 c = cin.get (); |
|
459 raw_mode (0); |
|
460 return c; |
|
461 } |
|
462 |
1111
|
463 char * |
1126
|
464 octave_getcwd (char *buf, int len) |
1111
|
465 { |
1113
|
466 #if defined (__EMX__) |
1111
|
467 return _getcwd2 (buf, len); |
|
468 #else |
|
469 return getcwd (buf, len); |
|
470 #endif |
|
471 } |
|
472 |
|
473 int |
1128
|
474 octave_chdir (const char *path) |
1111
|
475 { |
1113
|
476 #if defined (__EMX__) |
1111
|
477 int retval = -1; |
|
478 |
|
479 if (strlen (path) == 2 && path[1] == ':') |
|
480 { |
|
481 char *upper_case_dir_name = strupr (path); |
|
482 _chdrive (upper_case_dir_name[0]); |
|
483 if (_getdrive () == upper_case_dir_name[0]) |
|
484 retval = _chdir2 ("/"); |
|
485 } |
|
486 else |
|
487 retval = _chdir2 (path); |
|
488 |
|
489 return retval; |
|
490 #else |
|
491 return chdir (path); |
|
492 #endif |
|
493 } |
|
494 |
712
|
495 DEFUN ("clc", Fclc, Sclc, 0, 0, |
529
|
496 "clc (): clear screen") |
|
497 { |
|
498 Octave_object retval; |
|
499 |
|
500 rl_beg_of_line (); |
|
501 rl_kill_line (1); |
|
502 |
|
503 #if ! defined (_GO32_) |
|
504 if (term_clrpag) |
|
505 tputs (term_clrpag, 1, _rl_output_character_function); |
|
506 else |
|
507 crlf (); |
|
508 #else |
|
509 crlf (); |
|
510 #endif |
|
511 |
|
512 fflush (rl_outstream); |
|
513 |
|
514 return retval; |
|
515 } |
|
516 |
549
|
517 DEFALIAS (home, clc); |
|
518 |
712
|
519 DEFUN ("getenv", Fgetenv, Sgetenv, 1, 1, |
529
|
520 "getenv (STRING): get environment variable values") |
|
521 { |
|
522 Octave_object retval; |
|
523 |
|
524 int nargin = args.length (); |
|
525 |
712
|
526 if (nargin == 1) |
529
|
527 { |
712
|
528 char *name = args(0).string_value (); |
636
|
529 |
|
530 if (! error_state) |
|
531 { |
|
532 char *value = getenv (name); |
|
533 if (value) |
|
534 retval = value; |
|
535 else |
|
536 retval = ""; |
|
537 } |
529
|
538 } |
|
539 else |
|
540 print_usage ("getenv"); |
|
541 |
|
542 return retval; |
|
543 } |
|
544 |
712
|
545 DEFUN ("kbhit", Fkbhit, Skbhit, 0, 1, |
529
|
546 "kbhit: get a single character from the terminal") |
|
547 { |
|
548 Octave_object retval; |
|
549 |
1358
|
550 // XXX FIXME XXX -- add timeout and default value args? |
529
|
551 |
|
552 if (interactive) |
|
553 { |
|
554 int c = kbhit (); |
|
555 char *s = new char [2]; |
|
556 s[0] = c; |
|
557 s[1] = '\0'; |
|
558 retval = s; |
|
559 } |
|
560 |
|
561 return retval; |
|
562 } |
|
563 |
|
564 DEFUN ("pause", Fpause, Spause, 1, 1, |
|
565 "pause (seconds): suspend program execution") |
|
566 { |
|
567 Octave_object retval; |
|
568 |
|
569 int nargin = args.length (); |
|
570 |
712
|
571 if (! (nargin == 0 || nargin == 1)) |
529
|
572 { |
|
573 print_usage ("pause"); |
|
574 return retval; |
|
575 } |
|
576 |
|
577 if (interactive) |
|
578 { |
|
579 switch (nargin) |
|
580 { |
712
|
581 case 1: |
529
|
582 { |
712
|
583 double dval = args(0).double_value (); |
636
|
584 |
|
585 if (! error_state) |
529
|
586 { |
1086
|
587 if (xisnan (dval)) |
|
588 warning ("pause: NaN is an invalid delay"); |
|
589 else |
|
590 { |
|
591 int delay = NINT (dval); |
|
592 if (delay > 0) |
|
593 sleep (delay); |
|
594 } |
529
|
595 } |
|
596 } |
636
|
597 break; |
|
598 |
529
|
599 default: |
|
600 if (kbhit () == EOF) |
|
601 clean_up_and_exit (0); |
|
602 break; |
|
603 } |
|
604 } |
|
605 |
|
606 return retval; |
|
607 } |
|
608 |
862
|
609 // XXX FIXME XXX -- maybe this should only return 1 if IEEE floating |
|
610 // point functions really work. |
|
611 |
|
612 DEFUN ("isieee", Fisieee, Sisieee, 1, 1, |
|
613 "isieee (): return 1 if host uses IEEE floating point") |
|
614 { |
1226
|
615 return (double) (native_float_format == OCTAVE_IEEE_LITTLE |
|
616 || native_float_format == OCTAVE_IEEE_BIG); |
862
|
617 } |
|
618 |
529
|
619 #if !defined (HAVE_GETHOSTNAME) && defined (HAVE_SYS_UTSNAME_H) |
|
620 extern "C" |
|
621 { |
|
622 #include <sys/utsname.h> |
|
623 int |
|
624 gethostname (char *name, int namelen) |
|
625 { |
|
626 int i; |
|
627 struct utsname ut; |
|
628 |
|
629 --namelen; |
|
630 |
|
631 uname (&ut); |
|
632 i = strlen (ut.nodename) + 1; |
|
633 strncpy (name, ut.nodename, i < namelen ? i : namelen); |
|
634 name[namelen] = '\0'; |
|
635 |
|
636 return 0; |
|
637 } |
|
638 } |
|
639 #endif |
|
640 |
|
641 /* |
1
|
642 ;;; Local Variables: *** |
|
643 ;;; mode: C++ *** |
|
644 ;;; page-delimiter: "^/\\*" *** |
|
645 ;;; End: *** |
|
646 */ |