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 |
529
|
43 #include <sys/types.h> |
|
44 #ifdef HAVE_UNISTD_H |
|
45 #include <unistd.h> |
|
46 #endif |
1
|
47 |
529
|
48 #include "tree-const.h" |
|
49 #include "octave.h" |
542
|
50 #include "help.h" |
529
|
51 #include "input.h" |
|
52 #include "utils.h" |
|
53 #include "oct-obj.h" |
1
|
54 #include "error.h" |
444
|
55 #include "sysdep.h" |
529
|
56 #include "defun.h" |
1226
|
57 #include "f77-uscore.h" |
529
|
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 |
|
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? |
|
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 { |
|
262 // Possibly print our own message someday. Should probably be |
|
263 // user-switchable. |
|
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 |
|
277 // But don't print the system message. |
|
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) |
401
|
289 // Disable trapping on common exceptions. |
|
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 { |
|
336 // Get terminal modes. |
|
337 |
|
338 tcgetattr (tty_fd, &s); |
|
339 |
|
340 // Save modes and set certain variables dependent on modes. |
|
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 |
|
347 // Set the modes to the way we want them. |
|
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 { |
|
365 // Restore saved modes. |
|
366 s = save_term; |
|
367 } |
|
368 tcsetattr (tty_fd, TCSAFLUSH, &s); |
|
369 } |
|
370 #elif defined (HAVE_TERMIO_H) |
|
371 { |
|
372 struct termio s; |
|
373 static struct termio save_term; |
|
374 |
|
375 if (on) |
|
376 { |
|
377 // Get terminal modes. |
|
378 |
|
379 ioctl (tty_fd, TCGETA, &s); |
|
380 |
|
381 // Save modes and set certain variables dependent on modes. |
|
382 |
|
383 save_term = s; |
|
384 // ospeed = s.c_cflag & CBAUD; |
|
385 // erase_char = s.c_cc[VERASE]; |
|
386 // kill_char = s.c_cc[VKILL]; |
|
387 |
|
388 // Set the modes to the way we want them. |
|
389 |
|
390 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL); |
|
391 s.c_oflag |= (OPOST|ONLCR); |
|
392 #if defined (OCRNL) |
|
393 s.c_oflag &= ~(OCRNL); |
|
394 #endif |
|
395 #if defined (ONOCR) |
|
396 s.c_oflag &= ~(ONOCR); |
|
397 #endif |
|
398 #if defined (ONLRET) |
|
399 s.c_oflag &= ~(ONLRET); |
|
400 #endif |
|
401 s.c_cc[VMIN] = 1; |
|
402 s.c_cc[VTIME] = 0; |
|
403 } |
|
404 else |
|
405 { |
|
406 // Restore saved modes. |
|
407 s = save_term; |
|
408 } |
|
409 ioctl (tty_fd, TCSETAW, &s); |
|
410 } |
|
411 #elif defined (HAVE_SGTTY_H) |
|
412 { |
|
413 struct sgttyb s; |
|
414 static struct sgttyb save_term; |
|
415 |
|
416 if (on) |
|
417 { |
|
418 // Get terminal modes. |
|
419 |
|
420 ioctl (tty_fd, TIOCGETP, &s); |
|
421 |
|
422 // Save modes and set certain variables dependent on modes. |
|
423 |
|
424 save_term = s; |
|
425 // ospeed = s.sg_ospeed; |
|
426 // erase_char = s.sg_erase; |
|
427 // kill_char = s.sg_kill; |
|
428 |
|
429 // Set the modes to the way we want them. |
|
430 |
|
431 s.sg_flags |= CBREAK; |
|
432 s.sg_flags &= ~(ECHO); |
|
433 } |
|
434 else |
|
435 { |
|
436 // Restore saved modes. |
|
437 s = save_term; |
|
438 } |
|
439 ioctl (tty_fd, TIOCSETN, &s); |
|
440 } |
|
441 #else |
|
442 LOSE! LOSE! |
|
443 #endif |
|
444 |
|
445 curr_on = on; |
|
446 } |
|
447 |
767
|
448 // Read one character from the terminal. |
|
449 |
529
|
450 int |
|
451 kbhit (void) |
|
452 { |
|
453 int c; |
|
454 raw_mode (1); |
|
455 c = cin.get (); |
|
456 raw_mode (0); |
|
457 return c; |
|
458 } |
|
459 |
1111
|
460 char * |
1126
|
461 octave_getcwd (char *buf, int len) |
1111
|
462 { |
1113
|
463 #if defined (__EMX__) |
1111
|
464 return _getcwd2 (buf, len); |
|
465 #else |
|
466 return getcwd (buf, len); |
|
467 #endif |
|
468 } |
|
469 |
|
470 int |
1128
|
471 octave_chdir (const char *path) |
1111
|
472 { |
1113
|
473 #if defined (__EMX__) |
1111
|
474 int retval = -1; |
|
475 |
|
476 if (strlen (path) == 2 && path[1] == ':') |
|
477 { |
|
478 char *upper_case_dir_name = strupr (path); |
|
479 _chdrive (upper_case_dir_name[0]); |
|
480 if (_getdrive () == upper_case_dir_name[0]) |
|
481 retval = _chdir2 ("/"); |
|
482 } |
|
483 else |
|
484 retval = _chdir2 (path); |
|
485 |
|
486 return retval; |
|
487 #else |
|
488 return chdir (path); |
|
489 #endif |
|
490 } |
|
491 |
712
|
492 DEFUN ("clc", Fclc, Sclc, 0, 0, |
529
|
493 "clc (): clear screen") |
|
494 { |
|
495 Octave_object retval; |
|
496 |
|
497 rl_beg_of_line (); |
|
498 rl_kill_line (1); |
|
499 |
|
500 #if ! defined (_GO32_) |
|
501 if (term_clrpag) |
|
502 tputs (term_clrpag, 1, _rl_output_character_function); |
|
503 else |
|
504 crlf (); |
|
505 #else |
|
506 crlf (); |
|
507 #endif |
|
508 |
|
509 fflush (rl_outstream); |
|
510 |
|
511 return retval; |
|
512 } |
|
513 |
549
|
514 DEFALIAS (home, clc); |
|
515 |
712
|
516 DEFUN ("getenv", Fgetenv, Sgetenv, 1, 1, |
529
|
517 "getenv (STRING): get environment variable values") |
|
518 { |
|
519 Octave_object retval; |
|
520 |
|
521 int nargin = args.length (); |
|
522 |
712
|
523 if (nargin == 1) |
529
|
524 { |
712
|
525 char *name = args(0).string_value (); |
636
|
526 |
|
527 if (! error_state) |
|
528 { |
|
529 char *value = getenv (name); |
|
530 if (value) |
|
531 retval = value; |
|
532 else |
|
533 retval = ""; |
|
534 } |
529
|
535 } |
|
536 else |
|
537 print_usage ("getenv"); |
|
538 |
|
539 return retval; |
|
540 } |
|
541 |
712
|
542 DEFUN ("kbhit", Fkbhit, Skbhit, 0, 1, |
529
|
543 "kbhit: get a single character from the terminal") |
|
544 { |
|
545 Octave_object retval; |
|
546 |
|
547 // XXX FIXME XXX -- add timeout and default value args? |
|
548 |
|
549 if (interactive) |
|
550 { |
|
551 int c = kbhit (); |
|
552 char *s = new char [2]; |
|
553 s[0] = c; |
|
554 s[1] = '\0'; |
|
555 retval = s; |
|
556 } |
|
557 |
|
558 return retval; |
|
559 } |
|
560 |
|
561 DEFUN ("pause", Fpause, Spause, 1, 1, |
|
562 "pause (seconds): suspend program execution") |
|
563 { |
|
564 Octave_object retval; |
|
565 |
|
566 int nargin = args.length (); |
|
567 |
712
|
568 if (! (nargin == 0 || nargin == 1)) |
529
|
569 { |
|
570 print_usage ("pause"); |
|
571 return retval; |
|
572 } |
|
573 |
|
574 if (interactive) |
|
575 { |
|
576 switch (nargin) |
|
577 { |
712
|
578 case 1: |
529
|
579 { |
712
|
580 double dval = args(0).double_value (); |
636
|
581 |
|
582 if (! error_state) |
529
|
583 { |
1086
|
584 if (xisnan (dval)) |
|
585 warning ("pause: NaN is an invalid delay"); |
|
586 else |
|
587 { |
|
588 int delay = NINT (dval); |
|
589 if (delay > 0) |
|
590 sleep (delay); |
|
591 } |
529
|
592 } |
|
593 } |
636
|
594 break; |
|
595 |
529
|
596 default: |
|
597 if (kbhit () == EOF) |
|
598 clean_up_and_exit (0); |
|
599 break; |
|
600 } |
|
601 } |
|
602 |
|
603 return retval; |
|
604 } |
|
605 |
862
|
606 // XXX FIXME XXX -- maybe this should only return 1 if IEEE floating |
|
607 // point functions really work. |
|
608 |
|
609 DEFUN ("isieee", Fisieee, Sisieee, 1, 1, |
|
610 "isieee (): return 1 if host uses IEEE floating point") |
|
611 { |
1226
|
612 return (double) (native_float_format == OCTAVE_IEEE_LITTLE |
|
613 || native_float_format == OCTAVE_IEEE_BIG); |
862
|
614 } |
|
615 |
529
|
616 #if !defined (HAVE_GETHOSTNAME) && defined (HAVE_SYS_UTSNAME_H) |
|
617 extern "C" |
|
618 { |
|
619 #include <sys/utsname.h> |
|
620 int |
|
621 gethostname (char *name, int namelen) |
|
622 { |
|
623 int i; |
|
624 struct utsname ut; |
|
625 |
|
626 --namelen; |
|
627 |
|
628 uname (&ut); |
|
629 i = strlen (ut.nodename) + 1; |
|
630 strncpy (name, ut.nodename, i < namelen ? i : namelen); |
|
631 name[namelen] = '\0'; |
|
632 |
|
633 return 0; |
|
634 } |
|
635 } |
|
636 #endif |
|
637 |
|
638 /* |
1
|
639 ;;; Local Variables: *** |
|
640 ;;; mode: C++ *** |
|
641 ;;; page-delimiter: "^/\\*" *** |
|
642 ;;; End: *** |
|
643 */ |