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