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