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