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