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