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