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