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