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 |
3503
|
34 #include <iostream> |
1728
|
35 #include <string> |
|
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> |
4067
|
50 #endif |
|
51 |
|
52 #if defined (HAVE_CONIO_H) |
|
53 #include <conio.h> |
1430
|
54 #endif |
|
55 |
3248
|
56 #if defined (HAVE_SYS_IOCTL_H) |
|
57 #include <sys/ioctl.h> |
|
58 #endif |
|
59 |
1463
|
60 #if defined (HAVE_FLOATINGPOINT_H) |
|
61 #include <floatingpoint.h> |
|
62 #endif |
|
63 |
2508
|
64 #if defined (HAVE_IEEEFP_H) |
|
65 #include <ieeefp.h> |
|
66 #endif |
|
67 |
1463
|
68 #if !defined (HAVE_GETHOSTNAME) && defined (HAVE_SYS_UTSNAME_H) |
|
69 #include <sys/utsname.h> |
|
70 #endif |
|
71 |
2926
|
72 #include "cmd-edit.h" |
|
73 #include "file-ops.h" |
2893
|
74 #include "lo-mappers.h" |
2317
|
75 #include "mach-info.h" |
2926
|
76 #include "oct-env.h" |
1769
|
77 |
1352
|
78 #include "defun.h" |
|
79 #include "error.h" |
529
|
80 #include "input.h" |
1755
|
81 #include "oct-obj.h" |
2370
|
82 #include "ov.h" |
3234
|
83 #include "pager.h" |
1352
|
84 #include "sysdep.h" |
1755
|
85 #include "toplev.h" |
529
|
86 #include "utils.h" |
|
87 |
|
88 #ifndef STDIN_FILENO |
|
89 #define STDIN_FILENO 1 |
|
90 #endif |
444
|
91 |
2508
|
92 #if defined (__386BSD__) || defined (__FreeBSD__) |
|
93 static void |
|
94 BSD_init (void) |
|
95 { |
|
96 #if defined (HAVE_FLOATINGPOINT_H) |
|
97 // Disable trapping on common exceptions. |
|
98 fpsetmask (~(FP_X_OFL|FP_X_INV|FP_X_DZ|FP_X_DNML|FP_X_UFL|FP_X_IMP)); |
|
99 #endif |
|
100 } |
|
101 #endif |
|
102 |
2610
|
103 #if defined (NeXT) |
1
|
104 extern "C" |
|
105 { |
|
106 typedef void (*_cplus_fcn_int) (int); |
|
107 extern void (*malloc_error (_cplus_fcn_int)) (int); |
|
108 } |
|
109 |
|
110 static void |
|
111 malloc_handler (int code) |
|
112 { |
|
113 if (code == 5) |
217
|
114 warning ("hopefully recoverable malloc error: freeing wild pointer"); |
1
|
115 else |
2926
|
116 panic ("probably irrecoverable malloc error: code %d", code); |
1
|
117 } |
|
118 |
|
119 static void |
|
120 NeXT_init (void) |
|
121 { |
|
122 malloc_error (malloc_handler); |
|
123 } |
3333
|
124 #endif |
1
|
125 |
2546
|
126 #if defined (__EMX__) |
|
127 OS2_init (void) |
|
128 { |
|
129 _control87 ((EM_INVALID | EM_DENORMAL | EM_ZERODIVIDE | EM_OVERFLOW |
|
130 | EM_UNDERFLOW | EM_INEXACT), MCW_EM); |
|
131 } |
|
132 #endif |
|
133 |
2508
|
134 #if defined (SCO) |
|
135 static void |
|
136 SCO_init (void) |
|
137 { |
|
138 #if defined (HAVE_IEEEFP_H) |
|
139 // Disable trapping on common exceptions. |
|
140 fpsetmask (~(FP_X_OFL|FP_X_INV|FP_X_DZ|FP_X_DNML|FP_X_UFL|FP_X_IMP)); |
|
141 #endif |
|
142 } |
|
143 #endif |
|
144 |
1
|
145 void |
|
146 sysdep_init (void) |
|
147 { |
959
|
148 #if defined (__386BSD__) || defined (__FreeBSD__) |
2508
|
149 BSD_init (); |
2610
|
150 #elif defined (NeXT) |
1
|
151 NeXT_init (); |
2610
|
152 #elif defined (__EMX__) |
2546
|
153 OS2_init (); |
2508
|
154 #elif defined (SCO) |
|
155 SCO_init (); |
1
|
156 #endif |
444
|
157 |
|
158 octave_ieee_init (); |
1
|
159 } |
|
160 |
767
|
161 // Set terminal in raw mode. From less-177. |
|
162 // |
|
163 // Change terminal to "raw mode", or restore to "normal" mode. |
|
164 // "Raw mode" means |
|
165 // 1. An outstanding read will complete on receipt of a single keystroke. |
|
166 // 2. Input is not echoed. |
|
167 // 3. On output, \n is mapped to \r\n. |
|
168 // 4. \t is NOT expanded into spaces. |
|
169 // 5. Signal-causing characters such as ctrl-C (interrupt), |
|
170 // etc. are NOT disabled. |
|
171 // It doesn't matter whether an input \n is mapped to \r, or vice versa. |
|
172 |
529
|
173 void |
3657
|
174 raw_mode (bool on, bool wait) |
529
|
175 { |
3657
|
176 static bool curr_on = false; |
529
|
177 |
|
178 int tty_fd = STDIN_FILENO; |
|
179 if (! isatty (tty_fd)) |
|
180 { |
|
181 if (interactive) |
|
182 error ("stdin is not a tty!"); |
|
183 return; |
|
184 } |
|
185 |
|
186 if (on == curr_on) |
|
187 return; |
|
188 |
|
189 #if defined (HAVE_TERMIOS_H) |
|
190 { |
|
191 struct termios s; |
|
192 static struct termios save_term; |
|
193 |
|
194 if (on) |
|
195 { |
1358
|
196 // Get terminal modes. |
529
|
197 |
|
198 tcgetattr (tty_fd, &s); |
|
199 |
1358
|
200 // Save modes and set certain variables dependent on modes. |
529
|
201 |
|
202 save_term = s; |
|
203 // ospeed = s.c_cflag & CBAUD; |
|
204 // erase_char = s.c_cc[VERASE]; |
|
205 // kill_char = s.c_cc[VKILL]; |
|
206 |
1358
|
207 // Set the modes to the way we want them. |
529
|
208 |
|
209 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL); |
|
210 s.c_oflag |= (OPOST|ONLCR); |
|
211 #if defined (OCRNL) |
|
212 s.c_oflag &= ~(OCRNL); |
|
213 #endif |
|
214 #if defined (ONOCR) |
|
215 s.c_oflag &= ~(ONOCR); |
|
216 #endif |
|
217 #if defined (ONLRET) |
|
218 s.c_oflag &= ~(ONLRET); |
|
219 #endif |
3658
|
220 s.c_cc[VMIN] = wait ? 1 : 0; |
529
|
221 s.c_cc[VTIME] = 0; |
|
222 } |
|
223 else |
|
224 { |
1358
|
225 // Restore saved modes. |
|
226 |
529
|
227 s = save_term; |
|
228 } |
3658
|
229 |
529
|
230 tcsetattr (tty_fd, TCSAFLUSH, &s); |
|
231 } |
|
232 #elif defined (HAVE_TERMIO_H) |
|
233 { |
|
234 struct termio s; |
|
235 static struct termio save_term; |
|
236 |
|
237 if (on) |
|
238 { |
1358
|
239 // Get terminal modes. |
529
|
240 |
|
241 ioctl (tty_fd, TCGETA, &s); |
|
242 |
1358
|
243 // Save modes and set certain variables dependent on modes. |
529
|
244 |
|
245 save_term = s; |
|
246 // ospeed = s.c_cflag & CBAUD; |
|
247 // erase_char = s.c_cc[VERASE]; |
|
248 // kill_char = s.c_cc[VKILL]; |
|
249 |
1358
|
250 // Set the modes to the way we want them. |
529
|
251 |
|
252 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL); |
|
253 s.c_oflag |= (OPOST|ONLCR); |
|
254 #if defined (OCRNL) |
|
255 s.c_oflag &= ~(OCRNL); |
|
256 #endif |
|
257 #if defined (ONOCR) |
|
258 s.c_oflag &= ~(ONOCR); |
|
259 #endif |
|
260 #if defined (ONLRET) |
|
261 s.c_oflag &= ~(ONLRET); |
|
262 #endif |
3658
|
263 s.c_cc[VMIN] = wait ? 1 : 0; |
529
|
264 } |
|
265 else |
|
266 { |
1358
|
267 // Restore saved modes. |
|
268 |
529
|
269 s = save_term; |
|
270 } |
3658
|
271 |
529
|
272 ioctl (tty_fd, TCSETAW, &s); |
|
273 } |
|
274 #elif defined (HAVE_SGTTY_H) |
|
275 { |
|
276 struct sgttyb s; |
|
277 static struct sgttyb save_term; |
|
278 |
|
279 if (on) |
|
280 { |
1358
|
281 // Get terminal modes. |
529
|
282 |
|
283 ioctl (tty_fd, TIOCGETP, &s); |
|
284 |
1358
|
285 // Save modes and set certain variables dependent on modes. |
529
|
286 |
|
287 save_term = s; |
|
288 // ospeed = s.sg_ospeed; |
|
289 // erase_char = s.sg_erase; |
|
290 // kill_char = s.sg_kill; |
|
291 |
1358
|
292 // Set the modes to the way we want them. |
529
|
293 |
|
294 s.sg_flags |= CBREAK; |
|
295 s.sg_flags &= ~(ECHO); |
|
296 } |
|
297 else |
|
298 { |
1358
|
299 // Restore saved modes. |
|
300 |
529
|
301 s = save_term; |
|
302 } |
3658
|
303 |
529
|
304 ioctl (tty_fd, TIOCSETN, &s); |
|
305 } |
|
306 #else |
4064
|
307 warning ("no support for raw mode console I/O on this system"); |
|
308 |
|
309 // Make sure the current mode doesn't toggle. |
|
310 on = curr_on; |
529
|
311 #endif |
|
312 |
|
313 curr_on = on; |
|
314 } |
|
315 |
767
|
316 // Read one character from the terminal. |
|
317 |
529
|
318 int |
4067
|
319 octave_kbhit (bool wait) |
529
|
320 { |
4067
|
321 #ifdef HAVE__KBHIT |
4081
|
322 int c = (! wait && ! _kbhit ()) ? 0 : std::cin.get (); |
4067
|
323 #else |
3658
|
324 raw_mode (true, wait); |
|
325 |
|
326 int c = std::cin.get (); |
|
327 |
|
328 if (std::cin.fail () || std::cin.eof ()) |
|
329 std::cin.clear (); |
|
330 |
|
331 raw_mode (false, true); |
4067
|
332 #endif |
3658
|
333 |
529
|
334 return c; |
|
335 } |
|
336 |
1957
|
337 DEFUN (clc, , , |
3332
|
338 "-*- texinfo -*-\n\ |
|
339 @deftypefn {Built-in Function} {} clc ()\n\ |
|
340 @deftypefnx {Built-in Function} {} home ()\n\ |
|
341 Clear the terminal screen and move the cursor to the upper left corner.\n\ |
3333
|
342 @end deftypefn") |
529
|
343 { |
2926
|
344 command_editor::clear_screen (); |
529
|
345 |
2926
|
346 return octave_value_list (); |
529
|
347 } |
|
348 |
549
|
349 DEFALIAS (home, clc); |
|
350 |
1957
|
351 DEFUN (getenv, args, , |
3301
|
352 "-*- texinfo -*-\n\ |
|
353 @deftypefn {Built-in Function} {} getenv (@var{var})\n\ |
|
354 Return the value of the environment variable @var{var}. For example,\n\ |
|
355 \n\ |
|
356 @example\n\ |
|
357 getenv (\"PATH\")\n\ |
|
358 @end example\n\ |
|
359 \n\ |
|
360 @noindent\n\ |
|
361 returns a string containing the value of your path.\n\ |
|
362 @end deftypefn") |
529
|
363 { |
2086
|
364 octave_value_list retval; |
529
|
365 |
|
366 int nargin = args.length (); |
|
367 |
712
|
368 if (nargin == 1) |
529
|
369 { |
3523
|
370 std::string name = args(0).string_value (); |
636
|
371 |
|
372 if (! error_state) |
2926
|
373 retval = octave_env::getenv (name); |
529
|
374 } |
|
375 else |
|
376 print_usage ("getenv"); |
|
377 |
|
378 return retval; |
|
379 } |
|
380 |
1957
|
381 DEFUN (putenv, args, , |
3301
|
382 "-*- texinfo -*-\n\ |
|
383 @deftypefn {Built-in Function} {} putenv (@var{var}, @var{value})\n\ |
|
384 Set the value of the environment variable @var{var} to @var{value}.\n\ |
|
385 @end deftypefn") |
1706
|
386 { |
2086
|
387 octave_value_list retval; |
1706
|
388 |
|
389 int nargin = args.length (); |
|
390 |
|
391 if (nargin == 2) |
|
392 { |
3523
|
393 std::string var = args(0).string_value (); |
1706
|
394 |
|
395 if (! error_state) |
|
396 { |
3523
|
397 std::string val = args(1).string_value (); |
1706
|
398 |
|
399 if (! error_state) |
2926
|
400 octave_env::putenv (var, val); |
1706
|
401 else |
|
402 error ("putenv: second argument should be a string"); |
|
403 } |
|
404 else |
|
405 error ("putenv: first argument should be a string"); |
|
406 } |
|
407 else |
|
408 print_usage ("putenv"); |
|
409 |
|
410 return retval; |
|
411 } |
|
412 |
3372
|
413 // XXX FIXME XXX -- perhaps kbhit should also be able to print a prompt? |
|
414 |
3657
|
415 DEFUN (kbhit, args, , |
3372
|
416 "-*- texinfo -*-\n\ |
|
417 @deftypefn {Built-in Function} {} kbhit ()\n\ |
3657
|
418 Read a single keystroke from the keyboard. If called with one\n\ |
|
419 argument, don't wait for a keypress. For example,\n\ |
3372
|
420 \n\ |
|
421 @example\n\ |
|
422 x = kbhit ();\n\ |
|
423 @end example\n\ |
|
424 \n\ |
|
425 @noindent\n\ |
|
426 will set @var{x} to the next character typed at the keyboard as soon as\n\ |
|
427 it is typed.\n\ |
3657
|
428 \n\ |
|
429 @example\n\ |
|
430 x = kbhit (1);\n\ |
|
431 @end example\n\ |
|
432 \n\ |
|
433 @noindent\n\ |
|
434 identical to the above example, but don't wait for a keypress,\n\ |
|
435 returning the empty string if no key is available.\n\ |
3372
|
436 @end deftypefn") |
529
|
437 { |
2086
|
438 octave_value_list retval; |
529
|
439 |
1358
|
440 // XXX FIXME XXX -- add timeout and default value args? |
529
|
441 |
3676
|
442 if (interactive || forced_interactive) |
529
|
443 { |
4067
|
444 int c = octave_kbhit (args.length () == 0); |
3657
|
445 |
3658
|
446 if (c == -1) |
|
447 c = 0; |
3657
|
448 |
529
|
449 char *s = new char [2]; |
|
450 s[0] = c; |
|
451 s[1] = '\0'; |
|
452 retval = s; |
|
453 } |
|
454 |
|
455 return retval; |
|
456 } |
|
457 |
1957
|
458 DEFUN (pause, args, , |
3301
|
459 "-*- texinfo -*-\n\ |
|
460 @deftypefn {Built-in Function} {} pause (@var{seconds})\n\ |
|
461 Suspend the execution of the program. If invoked without any arguments,\n\ |
|
462 Octave waits until you type a character. With a numeric argument, it\n\ |
|
463 pauses for the given number of seconds. For example, the following\n\ |
|
464 statement prints a message and then waits 5 seconds before clearing the\n\ |
|
465 screen.\n\ |
|
466 \n\ |
|
467 @example\n\ |
|
468 @group\n\ |
|
469 fprintf (stderr, \"wait please...\n\");\n\ |
|
470 pause (5);\n\ |
|
471 clc;\n\ |
|
472 @end group\n\ |
|
473 @end example\n\ |
|
474 @end deftypefn") |
529
|
475 { |
2086
|
476 octave_value_list retval; |
529
|
477 |
|
478 int nargin = args.length (); |
|
479 |
712
|
480 if (! (nargin == 0 || nargin == 1)) |
529
|
481 { |
|
482 print_usage ("pause"); |
|
483 return retval; |
|
484 } |
|
485 |
1579
|
486 if (nargin == 1) |
529
|
487 { |
1579
|
488 double dval = args(0).double_value (); |
636
|
489 |
1579
|
490 if (! error_state) |
|
491 { |
|
492 if (xisnan (dval)) |
|
493 warning ("pause: NaN is an invalid delay"); |
2630
|
494 else if (xisinf (dval)) |
3234
|
495 { |
|
496 flush_octave_stdout (); |
4067
|
497 octave_kbhit (); |
3234
|
498 } |
1579
|
499 else |
4086
|
500 octave_sleep (dval); |
529
|
501 } |
|
502 } |
1579
|
503 else |
3234
|
504 { |
|
505 flush_octave_stdout (); |
4067
|
506 octave_kbhit (); |
3234
|
507 } |
2630
|
508 |
|
509 return retval; |
|
510 } |
|
511 |
|
512 DEFUN (sleep, args, , |
3301
|
513 "-*- texinfo -*-\n\ |
|
514 @deftypefn {Built-in Function} {} sleep (@var{seconds})\n\ |
|
515 Suspend the execution of the program for the given number of seconds.\n\ |
|
516 @end deftypefn") |
2630
|
517 { |
|
518 octave_value_list retval; |
|
519 |
|
520 if (args.length () == 1) |
1579
|
521 { |
2630
|
522 double dval = args(0).double_value (); |
|
523 |
|
524 if (! error_state) |
|
525 { |
|
526 if (xisnan (dval)) |
|
527 warning ("sleep: NaN is an invalid delay"); |
|
528 else |
4086
|
529 octave_sleep (dval); |
2630
|
530 } |
1579
|
531 } |
2630
|
532 else |
|
533 print_usage ("sleep"); |
|
534 |
|
535 return retval; |
|
536 } |
|
537 |
|
538 DEFUN (usleep, args, , |
3301
|
539 "-*- texinfo -*-\n\ |
|
540 @deftypefn {Built-in Function} {} usleep (@var{microseconds})\n\ |
|
541 Suspend the execution of the program for the given number of\n\ |
|
542 microseconds. On systems where it is not possible to sleep for periods\n\ |
|
543 of time less than one second, @code{usleep} will pause the execution for\n\ |
|
544 @code{round (@var{microseconds} / 1e6)} seconds.\n\ |
|
545 @end deftypefn") |
2630
|
546 { |
|
547 octave_value_list retval; |
|
548 |
|
549 if (args.length () == 1) |
|
550 { |
|
551 double dval = args(0).double_value (); |
|
552 |
|
553 if (! error_state) |
|
554 { |
|
555 if (xisnan (dval)) |
|
556 warning ("usleep: NaN is an invalid delay"); |
|
557 else |
|
558 { |
|
559 int delay = NINT (dval); |
2631
|
560 |
2630
|
561 if (delay > 0) |
3308
|
562 octave_usleep (delay); |
2630
|
563 } |
|
564 } |
|
565 } |
|
566 else |
|
567 print_usage ("usleep"); |
529
|
568 |
|
569 return retval; |
|
570 } |
|
571 |
862
|
572 // XXX FIXME XXX -- maybe this should only return 1 if IEEE floating |
|
573 // point functions really work. |
|
574 |
1957
|
575 DEFUN (isieee, , , |
3301
|
576 "-*- texinfo -*-\n\ |
|
577 @deftypefn {Built-in Function} {} isieee ()\n\ |
|
578 Return 1 if your computer claims to conform to the IEEE standard for\n\ |
|
579 floating point calculations.\n\ |
|
580 @end deftypefn") |
862
|
581 { |
2317
|
582 oct_mach_info::float_format flt_fmt = |
|
583 oct_mach_info::native_float_format (); |
|
584 |
2800
|
585 return static_cast<double> (flt_fmt == oct_mach_info::ieee_little_endian |
|
586 || flt_fmt == oct_mach_info::ieee_big_endian); |
862
|
587 } |
|
588 |
1957
|
589 DEFUN (tilde_expand, args, , |
3301
|
590 "-*- texinfo -*-\n\ |
|
591 @deftypefn {Built-in Function} {} tilde_expand (@var{string})\n\ |
|
592 Performs tilde expansion on @var{string}. If @var{string} begins with a\n\ |
|
593 tilde character, (@samp{~}), all of the characters preceding the first\n\ |
|
594 slash (or all characters, if there is no slash) are treated as a\n\ |
|
595 possible user name, and the tilde and the following characters up to the\n\ |
|
596 slash are replaced by the home directory of the named user. If the\n\ |
|
597 tilde is followed immediately by a slash, the tilde is replaced by the\n\ |
|
598 home directory of the user running Octave. For example,\n\ |
|
599 \n\ |
|
600 @example\n\ |
|
601 @group\n\ |
|
602 tilde_expand (\"~joeuser/bin\")\n\ |
|
603 @result{} \"/home/joeuser/bin\"\n\ |
|
604 tilde_expand (\"~/bin\")\n\ |
|
605 @result{} \"/home/jwe/bin\"\n\ |
|
606 @end group\n\ |
|
607 @end example\n\ |
|
608 @end deftypefn") |
1750
|
609 { |
2086
|
610 octave_value_list retval; |
1750
|
611 |
|
612 int nargin = args.length (); |
|
613 |
|
614 if (nargin == 1) |
2926
|
615 retval = file_ops::tilde_expand (args(0).all_strings ()); |
1750
|
616 else |
|
617 print_usage ("tilde_expand"); |
|
618 |
|
619 return retval; |
|
620 } |
|
621 |
2552
|
622 #if defined (__EMX__) && defined (OS2) |
|
623 |
|
624 DEFUN_TEXT (extproc, , , |
2926
|
625 "extproc: ignored by Octave") |
2552
|
626 { |
|
627 return octave_value_list (); |
|
628 } |
|
629 |
|
630 DEFALIAS (EXTPROC, extproc); |
|
631 |
|
632 #endif |
|
633 |
529
|
634 /* |
1
|
635 ;;; Local Variables: *** |
|
636 ;;; mode: C++ *** |
|
637 ;;; End: *** |
|
638 */ |