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