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