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