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