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