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 |
401
|
84 #if defined (__386BSD__) && defined (HAVE_FLOATINGPOINT_H) |
|
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 { |
401
|
201 #if defined (__386BSD__) && defined (HAVE_FLOATINGPOINT_H) |
|
202 // Disable trapping on common exceptions. |
|
203 fpsetmask (~(FP_X_OFL|FP_X_INV|FP_X_DZ|FP_X_DNML|FP_X_UFL|FP_X_IMP)); |
|
204 #endif |
|
205 |
1
|
206 #ifdef NeXT |
|
207 NeXT_init (); |
|
208 #endif |
444
|
209 |
|
210 octave_ieee_init (); |
1
|
211 } |
|
212 |
767
|
213 // Set terminal in raw mode. From less-177. |
|
214 // |
|
215 // Change terminal to "raw mode", or restore to "normal" mode. |
|
216 // "Raw mode" means |
|
217 // 1. An outstanding read will complete on receipt of a single keystroke. |
|
218 // 2. Input is not echoed. |
|
219 // 3. On output, \n is mapped to \r\n. |
|
220 // 4. \t is NOT expanded into spaces. |
|
221 // 5. Signal-causing characters such as ctrl-C (interrupt), |
|
222 // etc. are NOT disabled. |
|
223 // It doesn't matter whether an input \n is mapped to \r, or vice versa. |
|
224 |
529
|
225 void |
|
226 raw_mode (int on) |
|
227 { |
|
228 static int curr_on = 0; |
|
229 |
|
230 int tty_fd = STDIN_FILENO; |
|
231 if (! isatty (tty_fd)) |
|
232 { |
|
233 if (interactive) |
|
234 error ("stdin is not a tty!"); |
|
235 return; |
|
236 } |
|
237 |
|
238 if (on == curr_on) |
|
239 return; |
|
240 |
|
241 #if defined (HAVE_TERMIOS_H) |
|
242 { |
|
243 struct termios s; |
|
244 static struct termios save_term; |
|
245 |
|
246 if (on) |
|
247 { |
|
248 // Get terminal modes. |
|
249 |
|
250 tcgetattr (tty_fd, &s); |
|
251 |
|
252 // Save modes and set certain variables dependent on modes. |
|
253 |
|
254 save_term = s; |
|
255 // ospeed = s.c_cflag & CBAUD; |
|
256 // erase_char = s.c_cc[VERASE]; |
|
257 // kill_char = s.c_cc[VKILL]; |
|
258 |
|
259 // Set the modes to the way we want them. |
|
260 |
|
261 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL); |
|
262 s.c_oflag |= (OPOST|ONLCR); |
|
263 #if defined (OCRNL) |
|
264 s.c_oflag &= ~(OCRNL); |
|
265 #endif |
|
266 #if defined (ONOCR) |
|
267 s.c_oflag &= ~(ONOCR); |
|
268 #endif |
|
269 #if defined (ONLRET) |
|
270 s.c_oflag &= ~(ONLRET); |
|
271 #endif |
|
272 s.c_cc[VMIN] = 1; |
|
273 s.c_cc[VTIME] = 0; |
|
274 } |
|
275 else |
|
276 { |
|
277 // Restore saved modes. |
|
278 s = save_term; |
|
279 } |
|
280 tcsetattr (tty_fd, TCSAFLUSH, &s); |
|
281 } |
|
282 #elif defined (HAVE_TERMIO_H) |
|
283 { |
|
284 struct termio s; |
|
285 static struct termio save_term; |
|
286 |
|
287 if (on) |
|
288 { |
|
289 // Get terminal modes. |
|
290 |
|
291 ioctl (tty_fd, TCGETA, &s); |
|
292 |
|
293 // Save modes and set certain variables dependent on modes. |
|
294 |
|
295 save_term = s; |
|
296 // ospeed = s.c_cflag & CBAUD; |
|
297 // erase_char = s.c_cc[VERASE]; |
|
298 // kill_char = s.c_cc[VKILL]; |
|
299 |
|
300 // Set the modes to the way we want them. |
|
301 |
|
302 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL); |
|
303 s.c_oflag |= (OPOST|ONLCR); |
|
304 #if defined (OCRNL) |
|
305 s.c_oflag &= ~(OCRNL); |
|
306 #endif |
|
307 #if defined (ONOCR) |
|
308 s.c_oflag &= ~(ONOCR); |
|
309 #endif |
|
310 #if defined (ONLRET) |
|
311 s.c_oflag &= ~(ONLRET); |
|
312 #endif |
|
313 s.c_cc[VMIN] = 1; |
|
314 s.c_cc[VTIME] = 0; |
|
315 } |
|
316 else |
|
317 { |
|
318 // Restore saved modes. |
|
319 s = save_term; |
|
320 } |
|
321 ioctl (tty_fd, TCSETAW, &s); |
|
322 } |
|
323 #elif defined (HAVE_SGTTY_H) |
|
324 { |
|
325 struct sgttyb s; |
|
326 static struct sgttyb save_term; |
|
327 |
|
328 if (on) |
|
329 { |
|
330 // Get terminal modes. |
|
331 |
|
332 ioctl (tty_fd, TIOCGETP, &s); |
|
333 |
|
334 // Save modes and set certain variables dependent on modes. |
|
335 |
|
336 save_term = s; |
|
337 // ospeed = s.sg_ospeed; |
|
338 // erase_char = s.sg_erase; |
|
339 // kill_char = s.sg_kill; |
|
340 |
|
341 // Set the modes to the way we want them. |
|
342 |
|
343 s.sg_flags |= CBREAK; |
|
344 s.sg_flags &= ~(ECHO); |
|
345 } |
|
346 else |
|
347 { |
|
348 // Restore saved modes. |
|
349 s = save_term; |
|
350 } |
|
351 ioctl (tty_fd, TIOCSETN, &s); |
|
352 } |
|
353 #else |
|
354 LOSE! LOSE! |
|
355 #endif |
|
356 |
|
357 curr_on = on; |
|
358 } |
|
359 |
767
|
360 // Read one character from the terminal. |
|
361 |
529
|
362 int |
|
363 kbhit (void) |
|
364 { |
|
365 int c; |
|
366 raw_mode (1); |
|
367 c = cin.get (); |
|
368 raw_mode (0); |
|
369 return c; |
|
370 } |
|
371 |
712
|
372 DEFUN ("clc", Fclc, Sclc, 0, 0, |
529
|
373 "clc (): clear screen") |
|
374 { |
|
375 Octave_object retval; |
|
376 |
|
377 rl_beg_of_line (); |
|
378 rl_kill_line (1); |
|
379 |
|
380 #if ! defined (_GO32_) |
|
381 if (term_clrpag) |
|
382 tputs (term_clrpag, 1, _rl_output_character_function); |
|
383 else |
|
384 crlf (); |
|
385 #else |
|
386 crlf (); |
|
387 #endif |
|
388 |
|
389 fflush (rl_outstream); |
|
390 |
|
391 return retval; |
|
392 } |
|
393 |
549
|
394 DEFALIAS (home, clc); |
|
395 |
712
|
396 DEFUN ("getenv", Fgetenv, Sgetenv, 1, 1, |
529
|
397 "getenv (STRING): get environment variable values") |
|
398 { |
|
399 Octave_object retval; |
|
400 |
|
401 int nargin = args.length (); |
|
402 |
712
|
403 if (nargin == 1) |
529
|
404 { |
712
|
405 char *name = args(0).string_value (); |
636
|
406 |
|
407 if (! error_state) |
|
408 { |
|
409 char *value = getenv (name); |
|
410 if (value) |
|
411 retval = value; |
|
412 else |
|
413 retval = ""; |
|
414 } |
529
|
415 } |
|
416 else |
|
417 print_usage ("getenv"); |
|
418 |
|
419 return retval; |
|
420 } |
|
421 |
712
|
422 DEFUN ("kbhit", Fkbhit, Skbhit, 0, 1, |
529
|
423 "kbhit: get a single character from the terminal") |
|
424 { |
|
425 Octave_object retval; |
|
426 |
|
427 // XXX FIXME XXX -- add timeout and default value args? |
|
428 |
|
429 if (interactive) |
|
430 { |
|
431 int c = kbhit (); |
|
432 char *s = new char [2]; |
|
433 s[0] = c; |
|
434 s[1] = '\0'; |
|
435 retval = s; |
|
436 } |
|
437 |
|
438 return retval; |
|
439 } |
|
440 |
|
441 DEFUN ("pause", Fpause, Spause, 1, 1, |
|
442 "pause (seconds): suspend program execution") |
|
443 { |
|
444 Octave_object retval; |
|
445 |
|
446 int nargin = args.length (); |
|
447 |
712
|
448 if (! (nargin == 0 || nargin == 1)) |
529
|
449 { |
|
450 print_usage ("pause"); |
|
451 return retval; |
|
452 } |
|
453 |
|
454 if (interactive) |
|
455 { |
|
456 switch (nargin) |
|
457 { |
712
|
458 case 1: |
529
|
459 { |
712
|
460 double dval = args(0).double_value (); |
636
|
461 |
|
462 if (! error_state) |
529
|
463 { |
636
|
464 int delay = NINT (dval); |
|
465 if (delay > 0) |
|
466 sleep (delay); |
529
|
467 } |
|
468 } |
636
|
469 break; |
|
470 |
529
|
471 default: |
|
472 if (kbhit () == EOF) |
|
473 clean_up_and_exit (0); |
|
474 break; |
|
475 } |
|
476 } |
|
477 |
|
478 return retval; |
|
479 } |
|
480 |
|
481 #if !defined (HAVE_GETHOSTNAME) && defined (HAVE_SYS_UTSNAME_H) |
|
482 extern "C" |
|
483 { |
|
484 #include <sys/utsname.h> |
|
485 int |
|
486 gethostname (char *name, int namelen) |
|
487 { |
|
488 int i; |
|
489 struct utsname ut; |
|
490 |
|
491 --namelen; |
|
492 |
|
493 uname (&ut); |
|
494 i = strlen (ut.nodename) + 1; |
|
495 strncpy (name, ut.nodename, i < namelen ? i : namelen); |
|
496 name[namelen] = '\0'; |
|
497 |
|
498 return 0; |
|
499 } |
|
500 } |
|
501 #endif |
|
502 |
|
503 /* |
1
|
504 ;;; Local Variables: *** |
|
505 ;;; mode: C++ *** |
|
506 ;;; page-delimiter: "^/\\*" *** |
|
507 ;;; End: *** |
|
508 */ |