1
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
240
|
23 #ifdef HAVE_CONFIG_H |
1192
|
24 #include <config.h> |
1
|
25 #endif |
|
26 |
1346
|
27 #include <cfloat> |
1343
|
28 #include <cstddef> |
1346
|
29 #include <cstdio> |
1343
|
30 #include <cstdlib> |
|
31 #include <cstring> |
|
32 |
1728
|
33 #include <string> |
|
34 |
1742
|
35 #include <iostream.h> |
|
36 |
1350
|
37 #ifdef HAVE_UNISTD_H |
2442
|
38 #ifdef HAVE_SYS_TYPES_H |
529
|
39 #include <sys/types.h> |
2442
|
40 #endif |
529
|
41 #include <unistd.h> |
|
42 #endif |
1
|
43 |
1430
|
44 #if defined (HAVE_TERMIOS_H) |
|
45 #include <termios.h> |
|
46 #elif defined (HAVE_TERMIO_H) |
|
47 #include <termio.h> |
|
48 #elif defined (HAVE_SGTTY_H) |
|
49 #include <sgtty.h> |
|
50 #else |
|
51 LOSE! LOSE! |
|
52 #endif |
|
53 |
1463
|
54 #if defined (HAVE_FLOATINGPOINT_H) |
|
55 #include <floatingpoint.h> |
|
56 #endif |
|
57 |
2508
|
58 #if defined (HAVE_IEEEFP_H) |
|
59 #include <ieeefp.h> |
|
60 #endif |
|
61 |
1463
|
62 #if !defined (HAVE_GETHOSTNAME) && defined (HAVE_SYS_UTSNAME_H) |
|
63 #include <sys/utsname.h> |
|
64 #endif |
|
65 |
2926
|
66 #include "cmd-edit.h" |
|
67 #include "file-ops.h" |
2893
|
68 #include "lo-mappers.h" |
2317
|
69 #include "mach-info.h" |
2926
|
70 #include "oct-env.h" |
1769
|
71 #include "oct-math.h" |
|
72 |
1352
|
73 #include "defun.h" |
|
74 #include "error.h" |
529
|
75 #include "input.h" |
1755
|
76 #include "oct-obj.h" |
2370
|
77 #include "ov.h" |
1352
|
78 #include "sysdep.h" |
1755
|
79 #include "toplev.h" |
529
|
80 #include "utils.h" |
|
81 |
|
82 #ifndef STDIN_FILENO |
|
83 #define STDIN_FILENO 1 |
|
84 #endif |
444
|
85 |
2508
|
86 #if defined (__386BSD__) || defined (__FreeBSD__) |
|
87 static void |
|
88 BSD_init (void) |
|
89 { |
|
90 #if defined (HAVE_FLOATINGPOINT_H) |
|
91 // Disable trapping on common exceptions. |
|
92 fpsetmask (~(FP_X_OFL|FP_X_INV|FP_X_DZ|FP_X_DNML|FP_X_UFL|FP_X_IMP)); |
|
93 #endif |
|
94 } |
|
95 #endif |
|
96 |
2610
|
97 #if defined (NeXT) |
1
|
98 extern "C" |
|
99 { |
|
100 typedef void (*_cplus_fcn_int) (int); |
|
101 extern void (*malloc_error (_cplus_fcn_int)) (int); |
|
102 } |
|
103 |
|
104 static void |
|
105 malloc_handler (int code) |
|
106 { |
|
107 if (code == 5) |
217
|
108 warning ("hopefully recoverable malloc error: freeing wild pointer"); |
1
|
109 else |
2926
|
110 panic ("probably irrecoverable malloc error: code %d", code); |
1
|
111 } |
|
112 |
|
113 static void |
|
114 NeXT_init (void) |
|
115 { |
|
116 malloc_error (malloc_handler); |
|
117 } |
|
118 #endif |
|
119 |
2546
|
120 #if defined (__EMX__) |
|
121 OS2_init (void) |
|
122 { |
|
123 _control87 ((EM_INVALID | EM_DENORMAL | EM_ZERODIVIDE | EM_OVERFLOW |
|
124 | EM_UNDERFLOW | EM_INEXACT), MCW_EM); |
|
125 } |
|
126 #endif |
|
127 |
2508
|
128 #if defined (SCO) |
|
129 static void |
|
130 SCO_init (void) |
|
131 { |
|
132 #if defined (HAVE_IEEEFP_H) |
|
133 // Disable trapping on common exceptions. |
|
134 fpsetmask (~(FP_X_OFL|FP_X_INV|FP_X_DZ|FP_X_DNML|FP_X_UFL|FP_X_IMP)); |
|
135 #endif |
|
136 } |
|
137 #endif |
|
138 |
1
|
139 void |
|
140 sysdep_init (void) |
|
141 { |
959
|
142 #if defined (__386BSD__) || defined (__FreeBSD__) |
2508
|
143 BSD_init (); |
2610
|
144 #elif defined (NeXT) |
1
|
145 NeXT_init (); |
2610
|
146 #elif defined (__EMX__) |
2546
|
147 OS2_init (); |
2508
|
148 #elif defined (SCO) |
|
149 SCO_init (); |
1
|
150 #endif |
444
|
151 |
|
152 octave_ieee_init (); |
1
|
153 } |
|
154 |
767
|
155 // Set terminal in raw mode. From less-177. |
|
156 // |
|
157 // Change terminal to "raw mode", or restore to "normal" mode. |
|
158 // "Raw mode" means |
|
159 // 1. An outstanding read will complete on receipt of a single keystroke. |
|
160 // 2. Input is not echoed. |
|
161 // 3. On output, \n is mapped to \r\n. |
|
162 // 4. \t is NOT expanded into spaces. |
|
163 // 5. Signal-causing characters such as ctrl-C (interrupt), |
|
164 // etc. are NOT disabled. |
|
165 // It doesn't matter whether an input \n is mapped to \r, or vice versa. |
|
166 |
529
|
167 void |
|
168 raw_mode (int on) |
|
169 { |
|
170 static int curr_on = 0; |
|
171 |
|
172 int tty_fd = STDIN_FILENO; |
|
173 if (! isatty (tty_fd)) |
|
174 { |
|
175 if (interactive) |
|
176 error ("stdin is not a tty!"); |
|
177 return; |
|
178 } |
|
179 |
|
180 if (on == curr_on) |
|
181 return; |
|
182 |
|
183 #if defined (HAVE_TERMIOS_H) |
|
184 { |
|
185 struct termios s; |
|
186 static struct termios save_term; |
|
187 |
|
188 if (on) |
|
189 { |
1358
|
190 // Get terminal modes. |
529
|
191 |
|
192 tcgetattr (tty_fd, &s); |
|
193 |
1358
|
194 // Save modes and set certain variables dependent on modes. |
529
|
195 |
|
196 save_term = s; |
|
197 // ospeed = s.c_cflag & CBAUD; |
|
198 // erase_char = s.c_cc[VERASE]; |
|
199 // kill_char = s.c_cc[VKILL]; |
|
200 |
1358
|
201 // Set the modes to the way we want them. |
529
|
202 |
|
203 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL); |
|
204 s.c_oflag |= (OPOST|ONLCR); |
|
205 #if defined (OCRNL) |
|
206 s.c_oflag &= ~(OCRNL); |
|
207 #endif |
|
208 #if defined (ONOCR) |
|
209 s.c_oflag &= ~(ONOCR); |
|
210 #endif |
|
211 #if defined (ONLRET) |
|
212 s.c_oflag &= ~(ONLRET); |
|
213 #endif |
|
214 s.c_cc[VMIN] = 1; |
|
215 s.c_cc[VTIME] = 0; |
|
216 } |
|
217 else |
|
218 { |
1358
|
219 // Restore saved modes. |
|
220 |
529
|
221 s = save_term; |
|
222 } |
|
223 tcsetattr (tty_fd, TCSAFLUSH, &s); |
|
224 } |
|
225 #elif defined (HAVE_TERMIO_H) |
|
226 { |
|
227 struct termio s; |
|
228 static struct termio save_term; |
|
229 |
|
230 if (on) |
|
231 { |
1358
|
232 // Get terminal modes. |
529
|
233 |
|
234 ioctl (tty_fd, TCGETA, &s); |
|
235 |
1358
|
236 // Save modes and set certain variables dependent on modes. |
529
|
237 |
|
238 save_term = s; |
|
239 // ospeed = s.c_cflag & CBAUD; |
|
240 // erase_char = s.c_cc[VERASE]; |
|
241 // kill_char = s.c_cc[VKILL]; |
|
242 |
1358
|
243 // Set the modes to the way we want them. |
529
|
244 |
|
245 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL); |
|
246 s.c_oflag |= (OPOST|ONLCR); |
|
247 #if defined (OCRNL) |
|
248 s.c_oflag &= ~(OCRNL); |
|
249 #endif |
|
250 #if defined (ONOCR) |
|
251 s.c_oflag &= ~(ONOCR); |
|
252 #endif |
|
253 #if defined (ONLRET) |
|
254 s.c_oflag &= ~(ONLRET); |
|
255 #endif |
|
256 s.c_cc[VMIN] = 1; |
|
257 s.c_cc[VTIME] = 0; |
|
258 } |
|
259 else |
|
260 { |
1358
|
261 // Restore saved modes. |
|
262 |
529
|
263 s = save_term; |
|
264 } |
|
265 ioctl (tty_fd, TCSETAW, &s); |
|
266 } |
|
267 #elif defined (HAVE_SGTTY_H) |
|
268 { |
|
269 struct sgttyb s; |
|
270 static struct sgttyb save_term; |
|
271 |
|
272 if (on) |
|
273 { |
1358
|
274 // Get terminal modes. |
529
|
275 |
|
276 ioctl (tty_fd, TIOCGETP, &s); |
|
277 |
1358
|
278 // Save modes and set certain variables dependent on modes. |
529
|
279 |
|
280 save_term = s; |
|
281 // ospeed = s.sg_ospeed; |
|
282 // erase_char = s.sg_erase; |
|
283 // kill_char = s.sg_kill; |
|
284 |
1358
|
285 // Set the modes to the way we want them. |
529
|
286 |
|
287 s.sg_flags |= CBREAK; |
|
288 s.sg_flags &= ~(ECHO); |
|
289 } |
|
290 else |
|
291 { |
1358
|
292 // Restore saved modes. |
|
293 |
529
|
294 s = save_term; |
|
295 } |
|
296 ioctl (tty_fd, TIOCSETN, &s); |
|
297 } |
|
298 #else |
|
299 LOSE! LOSE! |
|
300 #endif |
|
301 |
|
302 curr_on = on; |
|
303 } |
|
304 |
767
|
305 // Read one character from the terminal. |
|
306 |
529
|
307 int |
|
308 kbhit (void) |
|
309 { |
|
310 int c; |
|
311 raw_mode (1); |
|
312 c = cin.get (); |
|
313 raw_mode (0); |
|
314 return c; |
|
315 } |
|
316 |
1957
|
317 DEFUN (clc, , , |
529
|
318 "clc (): clear screen") |
|
319 { |
2926
|
320 command_editor::clear_screen (); |
529
|
321 |
2926
|
322 return octave_value_list (); |
529
|
323 } |
|
324 |
549
|
325 DEFALIAS (home, clc); |
|
326 |
1957
|
327 DEFUN (getenv, args, , |
529
|
328 "getenv (STRING): get environment variable values") |
|
329 { |
2086
|
330 octave_value_list retval; |
529
|
331 |
|
332 int nargin = args.length (); |
|
333 |
712
|
334 if (nargin == 1) |
529
|
335 { |
2926
|
336 string name = args(0).string_value (); |
636
|
337 |
|
338 if (! error_state) |
2926
|
339 retval = octave_env::getenv (name); |
529
|
340 } |
|
341 else |
|
342 print_usage ("getenv"); |
|
343 |
|
344 return retval; |
|
345 } |
|
346 |
1957
|
347 DEFUN (putenv, args, , |
1706
|
348 "putenv (VAR, VALUE): define environment variable VAR=VALUE") |
|
349 { |
2086
|
350 octave_value_list retval; |
1706
|
351 |
|
352 int nargin = args.length (); |
|
353 |
|
354 if (nargin == 2) |
|
355 { |
1755
|
356 string var = args(0).string_value (); |
1706
|
357 |
|
358 if (! error_state) |
|
359 { |
1755
|
360 string val = args(1).string_value (); |
1706
|
361 |
|
362 if (! error_state) |
2926
|
363 octave_env::putenv (var, val); |
1706
|
364 else |
|
365 error ("putenv: second argument should be a string"); |
|
366 } |
|
367 else |
|
368 error ("putenv: first argument should be a string"); |
|
369 } |
|
370 else |
|
371 print_usage ("putenv"); |
|
372 |
|
373 return retval; |
|
374 } |
|
375 |
1957
|
376 DEFUN (kbhit, , , |
529
|
377 "kbhit: get a single character from the terminal") |
|
378 { |
2086
|
379 octave_value_list retval; |
529
|
380 |
1358
|
381 // XXX FIXME XXX -- add timeout and default value args? |
529
|
382 |
|
383 if (interactive) |
|
384 { |
|
385 int c = kbhit (); |
|
386 char *s = new char [2]; |
|
387 s[0] = c; |
|
388 s[1] = '\0'; |
|
389 retval = s; |
|
390 } |
|
391 |
|
392 return retval; |
|
393 } |
|
394 |
1957
|
395 DEFUN (pause, args, , |
529
|
396 "pause (seconds): suspend program execution") |
|
397 { |
2086
|
398 octave_value_list retval; |
529
|
399 |
|
400 int nargin = args.length (); |
|
401 |
712
|
402 if (! (nargin == 0 || nargin == 1)) |
529
|
403 { |
|
404 print_usage ("pause"); |
|
405 return retval; |
|
406 } |
|
407 |
1579
|
408 if (nargin == 1) |
529
|
409 { |
1579
|
410 double dval = args(0).double_value (); |
636
|
411 |
1579
|
412 if (! error_state) |
|
413 { |
|
414 if (xisnan (dval)) |
|
415 warning ("pause: NaN is an invalid delay"); |
2630
|
416 else if (xisinf (dval)) |
|
417 kbhit (); |
1579
|
418 else |
|
419 { |
|
420 int delay = NINT (dval); |
|
421 if (delay > 0) |
|
422 sleep (delay); |
|
423 } |
529
|
424 } |
|
425 } |
1579
|
426 else |
2630
|
427 kbhit (); |
|
428 |
|
429 return retval; |
|
430 } |
|
431 |
|
432 DEFUN (sleep, args, , |
|
433 "sleep (seconds): suspend program execution") |
|
434 { |
|
435 octave_value_list retval; |
|
436 |
|
437 if (args.length () == 1) |
1579
|
438 { |
2630
|
439 double dval = args(0).double_value (); |
|
440 |
|
441 if (! error_state) |
|
442 { |
|
443 if (xisnan (dval)) |
|
444 warning ("sleep: NaN is an invalid delay"); |
|
445 else |
|
446 { |
|
447 int delay = NINT (dval); |
|
448 if (delay > 0) |
|
449 sleep (delay); |
|
450 } |
|
451 } |
1579
|
452 } |
2630
|
453 else |
|
454 print_usage ("sleep"); |
|
455 |
|
456 return retval; |
|
457 } |
|
458 |
|
459 DEFUN (usleep, args, , |
|
460 "usleep (microseconds): suspend program execution") |
|
461 { |
|
462 octave_value_list retval; |
|
463 |
|
464 if (args.length () == 1) |
|
465 { |
|
466 double dval = args(0).double_value (); |
|
467 |
|
468 if (! error_state) |
|
469 { |
|
470 if (xisnan (dval)) |
|
471 warning ("usleep: NaN is an invalid delay"); |
|
472 else |
|
473 { |
2631
|
474 #if defined (HAVE_USLEEP) |
2630
|
475 int delay = NINT (dval); |
2631
|
476 |
2630
|
477 if (delay > 0) |
|
478 usleep (delay); |
|
479 #else |
2631
|
480 int delay = NINT (dval / 1e6); |
2630
|
481 |
2631
|
482 if (delay > 0) |
|
483 sleep (delay); |
2630
|
484 #endif |
|
485 } |
|
486 } |
|
487 } |
|
488 else |
|
489 print_usage ("usleep"); |
529
|
490 |
|
491 return retval; |
|
492 } |
|
493 |
862
|
494 // XXX FIXME XXX -- maybe this should only return 1 if IEEE floating |
|
495 // point functions really work. |
|
496 |
1957
|
497 DEFUN (isieee, , , |
862
|
498 "isieee (): return 1 if host uses IEEE floating point") |
|
499 { |
2317
|
500 oct_mach_info::float_format flt_fmt = |
|
501 oct_mach_info::native_float_format (); |
|
502 |
2800
|
503 return static_cast<double> (flt_fmt == oct_mach_info::ieee_little_endian |
|
504 || flt_fmt == oct_mach_info::ieee_big_endian); |
862
|
505 } |
|
506 |
1957
|
507 DEFUN (tilde_expand, args, , |
1750
|
508 "tilde_expand (STRING): perform tilde expansion on STRING") |
|
509 { |
2086
|
510 octave_value_list retval; |
1750
|
511 |
|
512 int nargin = args.length (); |
|
513 |
|
514 if (nargin == 1) |
2926
|
515 retval = file_ops::tilde_expand (args(0).all_strings ()); |
1750
|
516 else |
|
517 print_usage ("tilde_expand"); |
|
518 |
|
519 return retval; |
|
520 } |
|
521 |
2552
|
522 #if defined (__EMX__) && defined (OS2) |
|
523 |
|
524 DEFUN_TEXT (extproc, , , |
2926
|
525 "extproc: ignored by Octave") |
2552
|
526 { |
|
527 return octave_value_list (); |
|
528 } |
|
529 |
|
530 DEFALIAS (EXTPROC, extproc); |
|
531 |
|
532 #endif |
|
533 |
529
|
534 /* |
1
|
535 ;;; Local Variables: *** |
|
536 ;;; mode: C++ *** |
|
537 ;;; End: *** |
|
538 */ |