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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
1
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
1
|
26 #endif |
|
27 |
1346
|
28 #include <cfloat> |
3124
|
29 #include <cmath> |
1343
|
30 #include <cstddef> |
1346
|
31 #include <cstdio> |
1343
|
32 #include <cstdlib> |
|
33 #include <cstring> |
|
34 |
3503
|
35 #include <iostream> |
1728
|
36 #include <string> |
|
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> |
4067
|
51 #endif |
|
52 |
|
53 #if defined (HAVE_CONIO_H) |
|
54 #include <conio.h> |
1430
|
55 #endif |
|
56 |
3248
|
57 #if defined (HAVE_SYS_IOCTL_H) |
|
58 #include <sys/ioctl.h> |
|
59 #endif |
|
60 |
1463
|
61 #if defined (HAVE_FLOATINGPOINT_H) |
|
62 #include <floatingpoint.h> |
|
63 #endif |
|
64 |
2508
|
65 #if defined (HAVE_IEEEFP_H) |
|
66 #include <ieeefp.h> |
|
67 #endif |
|
68 |
1463
|
69 #if !defined (HAVE_GETHOSTNAME) && defined (HAVE_SYS_UTSNAME_H) |
|
70 #include <sys/utsname.h> |
|
71 #endif |
|
72 |
2926
|
73 #include "cmd-edit.h" |
|
74 #include "file-ops.h" |
2893
|
75 #include "lo-mappers.h" |
2317
|
76 #include "mach-info.h" |
2926
|
77 #include "oct-env.h" |
5451
|
78 #include "quit.h" |
1769
|
79 |
6208
|
80 #include "Cell.h" |
1352
|
81 #include "defun.h" |
|
82 #include "error.h" |
529
|
83 #include "input.h" |
1755
|
84 #include "oct-obj.h" |
2370
|
85 #include "ov.h" |
3234
|
86 #include "pager.h" |
6419
|
87 #include "parse.h" |
5770
|
88 #include "sighandlers.h" |
1352
|
89 #include "sysdep.h" |
1755
|
90 #include "toplev.h" |
529
|
91 #include "utils.h" |
6598
|
92 #include "file-stat.h" |
529
|
93 |
|
94 #ifndef STDIN_FILENO |
|
95 #define STDIN_FILENO 1 |
|
96 #endif |
444
|
97 |
2508
|
98 #if defined (__386BSD__) || defined (__FreeBSD__) |
|
99 static void |
|
100 BSD_init (void) |
|
101 { |
|
102 #if defined (HAVE_FLOATINGPOINT_H) |
|
103 // Disable trapping on common exceptions. |
4164
|
104 #ifndef FP_X_DNML |
|
105 #define FP_X_DNML 0 |
|
106 #endif |
2508
|
107 fpsetmask (~(FP_X_OFL|FP_X_INV|FP_X_DZ|FP_X_DNML|FP_X_UFL|FP_X_IMP)); |
|
108 #endif |
|
109 } |
|
110 #endif |
|
111 |
6080
|
112 static void |
|
113 w32_set_octave_home (void) |
|
114 { |
|
115 #if defined (__WIN32__) && ! defined (_POSIX_VERSION) |
|
116 int n = 1024; |
|
117 |
6086
|
118 std::string bin_dir (n, '\0'); |
6080
|
119 |
|
120 while (true) |
|
121 { |
|
122 int status = GetModuleFileName (0, &bin_dir[0], n); |
|
123 |
|
124 if (status < n) |
|
125 { |
|
126 bin_dir.resize (status); |
|
127 break; |
|
128 } |
|
129 else |
|
130 { |
|
131 n *= 2; |
|
132 bin_dir.resize (n); |
|
133 } |
|
134 } |
|
135 |
|
136 if (! bin_dir.empty ()) |
|
137 { |
|
138 size_t pos = bin_dir.rfind ("\\bin\\"); |
|
139 |
|
140 if (pos != NPOS) |
|
141 octave_env::putenv ("OCTAVE_HOME", bin_dir.substr (0, pos)); |
|
142 } |
|
143 #endif |
|
144 } |
|
145 |
5451
|
146 void |
|
147 w32_set_quiet_shutdown (void) |
|
148 { |
|
149 #if defined (__WIN32__) && ! defined (_POSIX_VERSION) |
|
150 // Let the user close the console window or shutdown without the |
|
151 // pesky dialog. |
|
152 // |
5775
|
153 // FIXME -- should this be user configurable? |
5451
|
154 SetProcessShutdownParameters (0x280, SHUTDOWN_NORETRY); |
|
155 #endif |
|
156 } |
|
157 |
|
158 #if defined (__WIN32__) && ! defined (_POSIX_VERSION) |
|
159 void |
|
160 MINGW_signal_cleanup (void) |
|
161 { |
5455
|
162 w32_set_quiet_shutdown (); |
5451
|
163 |
5455
|
164 w32_raise_final (); |
5451
|
165 } |
|
166 #endif |
|
167 |
|
168 #if defined (__MINGW32__) |
|
169 static void |
|
170 MINGW_init (void) |
|
171 { |
6080
|
172 w32_set_octave_home (); |
|
173 |
5451
|
174 // Init mutex to protect setjmp/longjmp and get main thread context |
|
175 w32_sigint_init (); |
|
176 |
|
177 w32_set_quiet_shutdown (); |
|
178 } |
|
179 #endif |
|
180 |
6080
|
181 #if defined (_MSC_VER) |
|
182 static void |
|
183 MSVC_init (void) |
|
184 { |
|
185 w32_set_octave_home (); |
6135
|
186 |
|
187 // Init mutex to protect setjmp/longjmp and get main thread context |
|
188 w32_sigint_init (); |
|
189 |
|
190 w32_set_quiet_shutdown (); |
6080
|
191 } |
|
192 #endif |
|
193 |
4091
|
194 #if defined (__CYGWIN__) |
|
195 |
|
196 #include <limits.h> |
|
197 #include <sys/cygwin.h> |
|
198 |
|
199 static void |
|
200 CYGWIN_init (void) |
|
201 { |
4470
|
202 // Make sure TMPDIR contains an absolute windows path. Use '/' |
|
203 // rather than '\\' so that sh expansion won't mess file names. |
|
204 |
4091
|
205 std::string tmpdir = octave_env::getenv ("TMPDIR"); |
|
206 |
|
207 if (tmpdir.empty ()) |
4470
|
208 tmpdir = "/tmp"; |
|
209 |
|
210 char buf [PATH_MAX]; |
4091
|
211 |
4470
|
212 if (cygwin32_conv_to_full_win32_path (tmpdir.c_str (), buf) < 0) |
|
213 panic ("CYGWIN_init: unable to convert TMPDIR (= \"%s\") to Windows directory name", |
|
214 tmpdir.c_str ()); |
|
215 else |
|
216 { |
|
217 tmpdir = buf; |
4091
|
218 |
4470
|
219 for (size_t i = 0; i < tmpdir.length (); i++) |
|
220 if (tmpdir[i] == '\\') |
|
221 tmpdir[i] = '/'; |
4091
|
222 } |
4470
|
223 |
|
224 octave_env::putenv ("TMPDIR", tmpdir); |
4091
|
225 } |
|
226 #endif |
|
227 |
6598
|
228 // Return TRUE if FILE1 and FILE2 refer to the same (physical) file. |
|
229 |
|
230 bool |
|
231 same_file_internal (const std::string& file1, const std::string& file2) |
|
232 { |
|
233 #ifdef OCTAVE_USE_WINDOWS_API |
|
234 |
|
235 // Windows native code |
|
236 // Reference: http://msdn2.microsoft.com/en-us/library/aa363788.aspx |
|
237 |
|
238 HANDLE hfile1; |
|
239 HANDLE hfile2; |
|
240 |
|
241 BY_HANDLE_FILE_INFORMATION hfi1; |
|
242 BY_HANDLE_FILE_INFORMATION hfi2; |
|
243 |
|
244 hfile1 = CreateFile (file1.c_str (), 0, FILE_SHARE_READ, 0, |
|
245 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); |
|
246 |
|
247 if (hfile1 == INVALID_FILE_HANDLE) |
|
248 return false; |
|
249 |
|
250 hfile2 = CreateFile (file2.c_str (), 0, FILE_SHARE_READ, 0, |
|
251 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); |
|
252 |
|
253 if (hfile2 == INVALID_FILE_HANDLE) |
|
254 { |
|
255 CloseHandle (hfile1); |
|
256 return false; |
|
257 } |
|
258 |
|
259 if (! GetFileInformationByHandle (hfile1, &hfi1)) |
|
260 { |
|
261 CloseHandle (hfile1); |
|
262 CloseHandle (hfile2); |
|
263 return false; |
|
264 } |
|
265 |
|
266 if (! GetFileInformationByHandle (hfile2, &hfi2)) |
|
267 { |
|
268 CloseHandle (hfile1); |
|
269 CloseHandle (hfile2); |
|
270 return false; |
|
271 } |
|
272 |
|
273 return (hfi1.dwVolumeSerialNumber == hfi2.dwVolumeSerialNumber |
|
274 && hfi1.nFileIndexHigh == hfi2.nFileIndexHigh |
|
275 && hfi1.nFileIndexLow == hfi2.nFileIndexLow); |
|
276 |
|
277 #else |
|
278 |
|
279 // POSIX Code |
|
280 |
|
281 file_stat fs_file1 (file1); |
|
282 file_stat fs_file2 (file2); |
|
283 |
|
284 return (fs_file1 && fs_file2 |
|
285 && fs_file1.ino () == fs_file2.ino () |
|
286 && fs_file1.dev () == fs_file2.dev ()); |
|
287 |
|
288 #endif |
|
289 } |
|
290 |
4284
|
291 #if defined (__DECCXX) |
|
292 |
|
293 // These don't seem to be instantiated automatically... |
|
294 |
|
295 template std::istream& |
|
296 std::operator >> (std::istream&, std::complex<double>&); |
|
297 |
|
298 template std::string& |
|
299 std::string::append (const std::string&, size_t, size_t); |
|
300 |
|
301 #endif |
|
302 |
2610
|
303 #if defined (NeXT) |
1
|
304 extern "C" |
|
305 { |
|
306 typedef void (*_cplus_fcn_int) (int); |
|
307 extern void (*malloc_error (_cplus_fcn_int)) (int); |
|
308 } |
|
309 |
|
310 static void |
|
311 malloc_handler (int code) |
|
312 { |
|
313 if (code == 5) |
217
|
314 warning ("hopefully recoverable malloc error: freeing wild pointer"); |
1
|
315 else |
2926
|
316 panic ("probably irrecoverable malloc error: code %d", code); |
1
|
317 } |
|
318 |
|
319 static void |
|
320 NeXT_init (void) |
|
321 { |
|
322 malloc_error (malloc_handler); |
|
323 } |
3333
|
324 #endif |
1
|
325 |
2546
|
326 #if defined (__EMX__) |
|
327 OS2_init (void) |
|
328 { |
|
329 _control87 ((EM_INVALID | EM_DENORMAL | EM_ZERODIVIDE | EM_OVERFLOW |
|
330 | EM_UNDERFLOW | EM_INEXACT), MCW_EM); |
|
331 } |
|
332 #endif |
|
333 |
2508
|
334 #if defined (SCO) |
|
335 static void |
|
336 SCO_init (void) |
|
337 { |
|
338 #if defined (HAVE_IEEEFP_H) |
|
339 // Disable trapping on common exceptions. |
|
340 fpsetmask (~(FP_X_OFL|FP_X_INV|FP_X_DZ|FP_X_DNML|FP_X_UFL|FP_X_IMP)); |
|
341 #endif |
|
342 } |
|
343 #endif |
|
344 |
1
|
345 void |
|
346 sysdep_init (void) |
|
347 { |
959
|
348 #if defined (__386BSD__) || defined (__FreeBSD__) |
2508
|
349 BSD_init (); |
4091
|
350 #elif defined (__CYGWIN__) |
|
351 CYGWIN_init (); |
5451
|
352 #elif defined (__MINGW32__) |
|
353 MINGW_init (); |
6080
|
354 #elif defined (_MSC_VER) |
|
355 MSVC_init (); |
2610
|
356 #elif defined (NeXT) |
1
|
357 NeXT_init (); |
2610
|
358 #elif defined (__EMX__) |
2546
|
359 OS2_init (); |
2508
|
360 #elif defined (SCO) |
|
361 SCO_init (); |
1
|
362 #endif |
444
|
363 |
|
364 octave_ieee_init (); |
1
|
365 } |
|
366 |
5451
|
367 void |
|
368 sysdep_cleanup (void) |
|
369 { |
|
370 MINGW_SIGNAL_CLEANUP (); |
|
371 } |
|
372 |
767
|
373 // Set terminal in raw mode. From less-177. |
|
374 // |
|
375 // Change terminal to "raw mode", or restore to "normal" mode. |
|
376 // "Raw mode" means |
|
377 // 1. An outstanding read will complete on receipt of a single keystroke. |
|
378 // 2. Input is not echoed. |
|
379 // 3. On output, \n is mapped to \r\n. |
|
380 // 4. \t is NOT expanded into spaces. |
|
381 // 5. Signal-causing characters such as ctrl-C (interrupt), |
|
382 // etc. are NOT disabled. |
|
383 // It doesn't matter whether an input \n is mapped to \r, or vice versa. |
|
384 |
529
|
385 void |
3657
|
386 raw_mode (bool on, bool wait) |
529
|
387 { |
3657
|
388 static bool curr_on = false; |
529
|
389 |
|
390 int tty_fd = STDIN_FILENO; |
|
391 if (! isatty (tty_fd)) |
|
392 { |
|
393 if (interactive) |
|
394 error ("stdin is not a tty!"); |
|
395 return; |
|
396 } |
|
397 |
|
398 if (on == curr_on) |
|
399 return; |
|
400 |
|
401 #if defined (HAVE_TERMIOS_H) |
|
402 { |
|
403 struct termios s; |
|
404 static struct termios save_term; |
|
405 |
|
406 if (on) |
|
407 { |
1358
|
408 // Get terminal modes. |
529
|
409 |
|
410 tcgetattr (tty_fd, &s); |
|
411 |
1358
|
412 // Save modes and set certain variables dependent on modes. |
529
|
413 |
|
414 save_term = s; |
|
415 // ospeed = s.c_cflag & CBAUD; |
|
416 // erase_char = s.c_cc[VERASE]; |
|
417 // kill_char = s.c_cc[VKILL]; |
|
418 |
1358
|
419 // Set the modes to the way we want them. |
529
|
420 |
|
421 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL); |
|
422 s.c_oflag |= (OPOST|ONLCR); |
|
423 #if defined (OCRNL) |
|
424 s.c_oflag &= ~(OCRNL); |
|
425 #endif |
|
426 #if defined (ONOCR) |
|
427 s.c_oflag &= ~(ONOCR); |
|
428 #endif |
|
429 #if defined (ONLRET) |
|
430 s.c_oflag &= ~(ONLRET); |
|
431 #endif |
3658
|
432 s.c_cc[VMIN] = wait ? 1 : 0; |
529
|
433 s.c_cc[VTIME] = 0; |
|
434 } |
|
435 else |
|
436 { |
1358
|
437 // Restore saved modes. |
|
438 |
529
|
439 s = save_term; |
|
440 } |
3658
|
441 |
529
|
442 tcsetattr (tty_fd, TCSAFLUSH, &s); |
|
443 } |
|
444 #elif defined (HAVE_TERMIO_H) |
|
445 { |
|
446 struct termio s; |
|
447 static struct termio save_term; |
|
448 |
|
449 if (on) |
|
450 { |
1358
|
451 // Get terminal modes. |
529
|
452 |
|
453 ioctl (tty_fd, TCGETA, &s); |
|
454 |
1358
|
455 // Save modes and set certain variables dependent on modes. |
529
|
456 |
|
457 save_term = s; |
|
458 // ospeed = s.c_cflag & CBAUD; |
|
459 // erase_char = s.c_cc[VERASE]; |
|
460 // kill_char = s.c_cc[VKILL]; |
|
461 |
1358
|
462 // Set the modes to the way we want them. |
529
|
463 |
|
464 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL); |
|
465 s.c_oflag |= (OPOST|ONLCR); |
|
466 #if defined (OCRNL) |
|
467 s.c_oflag &= ~(OCRNL); |
|
468 #endif |
|
469 #if defined (ONOCR) |
|
470 s.c_oflag &= ~(ONOCR); |
|
471 #endif |
|
472 #if defined (ONLRET) |
|
473 s.c_oflag &= ~(ONLRET); |
|
474 #endif |
3658
|
475 s.c_cc[VMIN] = wait ? 1 : 0; |
529
|
476 } |
|
477 else |
|
478 { |
1358
|
479 // Restore saved modes. |
|
480 |
529
|
481 s = save_term; |
|
482 } |
3658
|
483 |
529
|
484 ioctl (tty_fd, TCSETAW, &s); |
|
485 } |
|
486 #elif defined (HAVE_SGTTY_H) |
|
487 { |
|
488 struct sgttyb s; |
|
489 static struct sgttyb save_term; |
|
490 |
|
491 if (on) |
|
492 { |
1358
|
493 // Get terminal modes. |
529
|
494 |
|
495 ioctl (tty_fd, TIOCGETP, &s); |
|
496 |
1358
|
497 // Save modes and set certain variables dependent on modes. |
529
|
498 |
|
499 save_term = s; |
|
500 // ospeed = s.sg_ospeed; |
|
501 // erase_char = s.sg_erase; |
|
502 // kill_char = s.sg_kill; |
|
503 |
1358
|
504 // Set the modes to the way we want them. |
529
|
505 |
|
506 s.sg_flags |= CBREAK; |
|
507 s.sg_flags &= ~(ECHO); |
|
508 } |
|
509 else |
|
510 { |
1358
|
511 // Restore saved modes. |
|
512 |
529
|
513 s = save_term; |
|
514 } |
3658
|
515 |
529
|
516 ioctl (tty_fd, TIOCSETN, &s); |
|
517 } |
|
518 #else |
4064
|
519 warning ("no support for raw mode console I/O on this system"); |
|
520 |
|
521 // Make sure the current mode doesn't toggle. |
|
522 on = curr_on; |
529
|
523 #endif |
|
524 |
|
525 curr_on = on; |
|
526 } |
|
527 |
767
|
528 // Read one character from the terminal. |
|
529 |
529
|
530 int |
4067
|
531 octave_kbhit (bool wait) |
529
|
532 { |
4067
|
533 #ifdef HAVE__KBHIT |
4081
|
534 int c = (! wait && ! _kbhit ()) ? 0 : std::cin.get (); |
4067
|
535 #else |
3658
|
536 raw_mode (true, wait); |
|
537 |
5770
|
538 // Get current handler. |
|
539 octave_interrupt_handler saved_interrupt_handler |
|
540 = octave_ignore_interrupts (); |
|
541 |
|
542 // Restore it, disabling system call restarts (if possible) so the |
|
543 // read can be interrupted. |
|
544 |
|
545 octave_set_interrupt_handler (saved_interrupt_handler, false); |
|
546 |
3658
|
547 int c = std::cin.get (); |
5770
|
548 |
3658
|
549 if (std::cin.fail () || std::cin.eof ()) |
|
550 std::cin.clear (); |
|
551 |
5770
|
552 // Restore it, enabling system call restarts (if possible). |
|
553 octave_set_interrupt_handler (saved_interrupt_handler, true); |
|
554 |
3658
|
555 raw_mode (false, true); |
4067
|
556 #endif |
3658
|
557 |
529
|
558 return c; |
|
559 } |
|
560 |
1957
|
561 DEFUN (clc, , , |
3332
|
562 "-*- texinfo -*-\n\ |
|
563 @deftypefn {Built-in Function} {} clc ()\n\ |
|
564 @deftypefnx {Built-in Function} {} home ()\n\ |
|
565 Clear the terminal screen and move the cursor to the upper left corner.\n\ |
3333
|
566 @end deftypefn") |
529
|
567 { |
2926
|
568 command_editor::clear_screen (); |
529
|
569 |
2926
|
570 return octave_value_list (); |
529
|
571 } |
|
572 |
549
|
573 DEFALIAS (home, clc); |
|
574 |
1957
|
575 DEFUN (getenv, args, , |
3301
|
576 "-*- texinfo -*-\n\ |
|
577 @deftypefn {Built-in Function} {} getenv (@var{var})\n\ |
|
578 Return the value of the environment variable @var{var}. For example,\n\ |
|
579 \n\ |
|
580 @example\n\ |
|
581 getenv (\"PATH\")\n\ |
|
582 @end example\n\ |
|
583 \n\ |
|
584 @noindent\n\ |
|
585 returns a string containing the value of your path.\n\ |
|
586 @end deftypefn") |
529
|
587 { |
4233
|
588 octave_value retval; |
529
|
589 |
|
590 int nargin = args.length (); |
|
591 |
712
|
592 if (nargin == 1) |
529
|
593 { |
3523
|
594 std::string name = args(0).string_value (); |
636
|
595 |
|
596 if (! error_state) |
2926
|
597 retval = octave_env::getenv (name); |
529
|
598 } |
|
599 else |
5823
|
600 print_usage (); |
529
|
601 |
|
602 return retval; |
|
603 } |
|
604 |
1957
|
605 DEFUN (putenv, args, , |
3301
|
606 "-*- texinfo -*-\n\ |
|
607 @deftypefn {Built-in Function} {} putenv (@var{var}, @var{value})\n\ |
|
608 Set the value of the environment variable @var{var} to @var{value}.\n\ |
|
609 @end deftypefn") |
1706
|
610 { |
2086
|
611 octave_value_list retval; |
1706
|
612 |
|
613 int nargin = args.length (); |
|
614 |
|
615 if (nargin == 2) |
|
616 { |
3523
|
617 std::string var = args(0).string_value (); |
1706
|
618 |
|
619 if (! error_state) |
|
620 { |
3523
|
621 std::string val = args(1).string_value (); |
1706
|
622 |
|
623 if (! error_state) |
2926
|
624 octave_env::putenv (var, val); |
1706
|
625 else |
|
626 error ("putenv: second argument should be a string"); |
|
627 } |
|
628 else |
|
629 error ("putenv: first argument should be a string"); |
|
630 } |
|
631 else |
5823
|
632 print_usage (); |
1706
|
633 |
|
634 return retval; |
|
635 } |
|
636 |
5775
|
637 // FIXME -- perhaps kbhit should also be able to print a prompt? |
3372
|
638 |
3657
|
639 DEFUN (kbhit, args, , |
3372
|
640 "-*- texinfo -*-\n\ |
|
641 @deftypefn {Built-in Function} {} kbhit ()\n\ |
3657
|
642 Read a single keystroke from the keyboard. If called with one\n\ |
|
643 argument, don't wait for a keypress. For example,\n\ |
3372
|
644 \n\ |
|
645 @example\n\ |
|
646 x = kbhit ();\n\ |
|
647 @end example\n\ |
|
648 \n\ |
|
649 @noindent\n\ |
|
650 will set @var{x} to the next character typed at the keyboard as soon as\n\ |
|
651 it is typed.\n\ |
3657
|
652 \n\ |
|
653 @example\n\ |
|
654 x = kbhit (1);\n\ |
|
655 @end example\n\ |
|
656 \n\ |
|
657 @noindent\n\ |
|
658 identical to the above example, but don't wait for a keypress,\n\ |
|
659 returning the empty string if no key is available.\n\ |
3372
|
660 @end deftypefn") |
529
|
661 { |
4233
|
662 octave_value retval; |
529
|
663 |
5775
|
664 // FIXME -- add timeout and default value args? |
529
|
665 |
3676
|
666 if (interactive || forced_interactive) |
529
|
667 { |
6423
|
668 feval ("drawnow"); |
|
669 |
4067
|
670 int c = octave_kbhit (args.length () == 0); |
3657
|
671 |
3658
|
672 if (c == -1) |
|
673 c = 0; |
3657
|
674 |
529
|
675 char *s = new char [2]; |
|
676 s[0] = c; |
|
677 s[1] = '\0'; |
|
678 retval = s; |
|
679 } |
|
680 |
|
681 return retval; |
|
682 } |
|
683 |
1957
|
684 DEFUN (pause, args, , |
3301
|
685 "-*- texinfo -*-\n\ |
|
686 @deftypefn {Built-in Function} {} pause (@var{seconds})\n\ |
|
687 Suspend the execution of the program. If invoked without any arguments,\n\ |
|
688 Octave waits until you type a character. With a numeric argument, it\n\ |
|
689 pauses for the given number of seconds. For example, the following\n\ |
|
690 statement prints a message and then waits 5 seconds before clearing the\n\ |
|
691 screen.\n\ |
|
692 \n\ |
|
693 @example\n\ |
|
694 @group\n\ |
|
695 fprintf (stderr, \"wait please...\n\");\n\ |
|
696 pause (5);\n\ |
|
697 clc;\n\ |
|
698 @end group\n\ |
|
699 @end example\n\ |
|
700 @end deftypefn") |
529
|
701 { |
2086
|
702 octave_value_list retval; |
529
|
703 |
|
704 int nargin = args.length (); |
|
705 |
712
|
706 if (! (nargin == 0 || nargin == 1)) |
529
|
707 { |
5823
|
708 print_usage (); |
529
|
709 return retval; |
|
710 } |
|
711 |
1579
|
712 if (nargin == 1) |
529
|
713 { |
1579
|
714 double dval = args(0).double_value (); |
636
|
715 |
1579
|
716 if (! error_state) |
|
717 { |
6419
|
718 if (! xisnan (dval)) |
3234
|
719 { |
6419
|
720 feval ("drawnow"); |
|
721 |
|
722 if (xisinf (dval)) |
|
723 { |
|
724 flush_octave_stdout (); |
|
725 octave_kbhit (); |
|
726 } |
|
727 else |
|
728 octave_sleep (dval); |
3234
|
729 } |
1579
|
730 else |
6419
|
731 warning ("pause: NaN is an invalid delay"); |
529
|
732 } |
|
733 } |
1579
|
734 else |
3234
|
735 { |
6423
|
736 feval ("drawnow"); |
3234
|
737 flush_octave_stdout (); |
4067
|
738 octave_kbhit (); |
3234
|
739 } |
2630
|
740 |
|
741 return retval; |
|
742 } |
|
743 |
|
744 DEFUN (sleep, args, , |
3301
|
745 "-*- texinfo -*-\n\ |
|
746 @deftypefn {Built-in Function} {} sleep (@var{seconds})\n\ |
|
747 Suspend the execution of the program for the given number of seconds.\n\ |
|
748 @end deftypefn") |
2630
|
749 { |
|
750 octave_value_list retval; |
|
751 |
|
752 if (args.length () == 1) |
1579
|
753 { |
2630
|
754 double dval = args(0).double_value (); |
|
755 |
|
756 if (! error_state) |
|
757 { |
|
758 if (xisnan (dval)) |
|
759 warning ("sleep: NaN is an invalid delay"); |
|
760 else |
6423
|
761 { |
|
762 feval ("drawnow"); |
|
763 octave_sleep (dval); |
|
764 } |
2630
|
765 } |
1579
|
766 } |
2630
|
767 else |
5823
|
768 print_usage (); |
2630
|
769 |
|
770 return retval; |
|
771 } |
|
772 |
|
773 DEFUN (usleep, args, , |
3301
|
774 "-*- texinfo -*-\n\ |
|
775 @deftypefn {Built-in Function} {} usleep (@var{microseconds})\n\ |
|
776 Suspend the execution of the program for the given number of\n\ |
|
777 microseconds. On systems where it is not possible to sleep for periods\n\ |
|
778 of time less than one second, @code{usleep} will pause the execution for\n\ |
|
779 @code{round (@var{microseconds} / 1e6)} seconds.\n\ |
|
780 @end deftypefn") |
2630
|
781 { |
|
782 octave_value_list retval; |
|
783 |
|
784 if (args.length () == 1) |
|
785 { |
|
786 double dval = args(0).double_value (); |
|
787 |
|
788 if (! error_state) |
|
789 { |
|
790 if (xisnan (dval)) |
|
791 warning ("usleep: NaN is an invalid delay"); |
|
792 else |
|
793 { |
6423
|
794 feval ("drawnow"); |
|
795 |
2630
|
796 int delay = NINT (dval); |
2631
|
797 |
2630
|
798 if (delay > 0) |
3308
|
799 octave_usleep (delay); |
2630
|
800 } |
|
801 } |
|
802 } |
|
803 else |
5823
|
804 print_usage (); |
529
|
805 |
|
806 return retval; |
|
807 } |
|
808 |
5775
|
809 // FIXME -- maybe this should only return 1 if IEEE floating |
862
|
810 // point functions really work. |
|
811 |
1957
|
812 DEFUN (isieee, , , |
3301
|
813 "-*- texinfo -*-\n\ |
|
814 @deftypefn {Built-in Function} {} isieee ()\n\ |
|
815 Return 1 if your computer claims to conform to the IEEE standard for\n\ |
|
816 floating point calculations.\n\ |
|
817 @end deftypefn") |
862
|
818 { |
4600
|
819 oct_mach_info::float_format flt_fmt = oct_mach_info::native_float_format (); |
2317
|
820 |
4574
|
821 return octave_value (flt_fmt == oct_mach_info::flt_fmt_ieee_little_endian |
|
822 || flt_fmt == oct_mach_info::flt_fmt_ieee_big_endian); |
862
|
823 } |
|
824 |
4600
|
825 DEFUN (native_float_format, , , |
|
826 "-*- texinfo -*-\n\ |
|
827 @deftypefn {Built-in Function} {} native_float_format ()\n\ |
|
828 Return the native floating point format as a string\n\ |
|
829 @end deftypefn") |
|
830 { |
|
831 oct_mach_info::float_format flt_fmt = oct_mach_info::native_float_format (); |
|
832 |
|
833 return octave_value (oct_mach_info::float_format_as_string (flt_fmt)); |
|
834 } |
|
835 |
1957
|
836 DEFUN (tilde_expand, args, , |
3301
|
837 "-*- texinfo -*-\n\ |
|
838 @deftypefn {Built-in Function} {} tilde_expand (@var{string})\n\ |
|
839 Performs tilde expansion on @var{string}. If @var{string} begins with a\n\ |
|
840 tilde character, (@samp{~}), all of the characters preceding the first\n\ |
|
841 slash (or all characters, if there is no slash) are treated as a\n\ |
|
842 possible user name, and the tilde and the following characters up to the\n\ |
|
843 slash are replaced by the home directory of the named user. If the\n\ |
|
844 tilde is followed immediately by a slash, the tilde is replaced by the\n\ |
|
845 home directory of the user running Octave. For example,\n\ |
|
846 \n\ |
|
847 @example\n\ |
|
848 @group\n\ |
|
849 tilde_expand (\"~joeuser/bin\")\n\ |
|
850 @result{} \"/home/joeuser/bin\"\n\ |
|
851 tilde_expand (\"~/bin\")\n\ |
|
852 @result{} \"/home/jwe/bin\"\n\ |
|
853 @end group\n\ |
|
854 @end example\n\ |
|
855 @end deftypefn") |
1750
|
856 { |
4233
|
857 octave_value retval; |
1750
|
858 |
|
859 int nargin = args.length (); |
|
860 |
|
861 if (nargin == 1) |
6116
|
862 { |
|
863 octave_value arg = args(0); |
|
864 |
|
865 string_vector sv = arg.all_strings (); |
|
866 |
|
867 if (! error_state) |
|
868 { |
|
869 sv = file_ops::tilde_expand (sv); |
|
870 |
|
871 if (arg.is_cellstr ()) |
|
872 retval = Cell (arg.dims (), sv); |
|
873 else |
|
874 retval = sv; |
|
875 } |
|
876 else |
|
877 error ("tilde_expand: expecting argument to be char or cellstr object"); |
|
878 } |
1750
|
879 else |
5823
|
880 print_usage (); |
1750
|
881 |
|
882 return retval; |
|
883 } |
|
884 |
2552
|
885 #if defined (__EMX__) && defined (OS2) |
|
886 |
4208
|
887 DEFCMD (extproc, , , |
2926
|
888 "extproc: ignored by Octave") |
2552
|
889 { |
|
890 return octave_value_list (); |
|
891 } |
|
892 |
|
893 DEFALIAS (EXTPROC, extproc); |
|
894 |
|
895 #endif |
|
896 |
529
|
897 /* |
1
|
898 ;;; Local Variables: *** |
|
899 ;;; mode: C++ *** |
|
900 ;;; End: *** |
|
901 */ |