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