Mercurial > hg > octave-nkf
annotate src/sysdep.cc @ 12111:050b8c0e1531 release-3-2-x
correct previous patch
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Thu, 26 Nov 2009 14:39:55 +0100 |
parents | efdc5df061b6 |
children | cd96d29c5efa |
rev | line source |
---|---|
1 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1999, 2000, 2002, 2003, |
8920 | 4 2005, 2006, 2007, 2008, 2009 John W. Eaton |
1 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
1 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
1 | 21 |
22 */ | |
23 | |
240 | 24 #ifdef HAVE_CONFIG_H |
1192 | 25 #include <config.h> |
1 | 26 #endif |
27 | |
1346 | 28 #include <cfloat> |
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" |
7231 | 75 #include "lo-math.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 |
12046
efdc5df061b6
initialize floating point values properly for NetBSD systems
Aleksej Saushev <asau@inbox.ru>
parents:
12028
diff
changeset
|
98 #if defined (__386BSD__) || defined (__FreeBSD__) || defined (__NetBSD__) |
2508 | 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 | |
6956 | 112 #if defined (__WIN32__) && ! defined (_POSIX_VERSION) |
6080 | 113 static void |
114 w32_set_octave_home (void) | |
115 { | |
116 int n = 1024; | |
117 | |
6086 | 118 std::string bin_dir (n, '\0'); |
6080 | 119 |
120 while (true) | |
121 { | |
6954 | 122 HMODULE hMod = GetModuleHandle ("octinterp"); |
123 int status = GetModuleFileName (hMod, &bin_dir[0], n); | |
6080 | 124 |
125 if (status < n) | |
126 { | |
127 bin_dir.resize (status); | |
128 break; | |
129 } | |
130 else | |
131 { | |
132 n *= 2; | |
133 bin_dir.resize (n); | |
134 } | |
135 } | |
136 | |
137 if (! bin_dir.empty ()) | |
138 { | |
139 size_t pos = bin_dir.rfind ("\\bin\\"); | |
140 | |
8021 | 141 if (pos != std::string::npos) |
6080 | 142 octave_env::putenv ("OCTAVE_HOME", bin_dir.substr (0, pos)); |
143 } | |
144 } | |
145 | |
6960 | 146 void |
5451 | 147 w32_set_quiet_shutdown (void) |
148 { | |
149 // Let the user close the console window or shutdown without the | |
150 // pesky dialog. | |
151 // | |
5775 | 152 // FIXME -- should this be user configurable? |
5451 | 153 SetProcessShutdownParameters (0x280, SHUTDOWN_NORETRY); |
154 } | |
155 | |
6960 | 156 void |
5451 | 157 MINGW_signal_cleanup (void) |
158 { | |
5455 | 159 w32_set_quiet_shutdown (); |
5451 | 160 |
5455 | 161 w32_raise_final (); |
5451 | 162 } |
163 #endif | |
164 | |
165 #if defined (__MINGW32__) | |
166 static void | |
167 MINGW_init (void) | |
168 { | |
6080 | 169 w32_set_octave_home (); |
170 | |
5451 | 171 // Init mutex to protect setjmp/longjmp and get main thread context |
172 w32_sigint_init (); | |
173 | |
174 w32_set_quiet_shutdown (); | |
175 } | |
176 #endif | |
177 | |
6080 | 178 #if defined (_MSC_VER) |
179 static void | |
180 MSVC_init (void) | |
181 { | |
182 w32_set_octave_home (); | |
6135 | 183 |
184 // Init mutex to protect setjmp/longjmp and get main thread context | |
185 w32_sigint_init (); | |
186 | |
187 w32_set_quiet_shutdown (); | |
6080 | 188 } |
189 #endif | |
190 | |
4091 | 191 |
6598 | 192 // Return TRUE if FILE1 and FILE2 refer to the same (physical) file. |
193 | |
194 bool | |
195 same_file_internal (const std::string& file1, const std::string& file2) | |
196 { | |
197 #ifdef OCTAVE_USE_WINDOWS_API | |
198 | |
6691 | 199 bool retval = false; |
200 | |
6598 | 201 // Windows native code |
202 // Reference: http://msdn2.microsoft.com/en-us/library/aa363788.aspx | |
203 | |
6691 | 204 HANDLE hfile1 = CreateFile (file1.c_str (), 0, FILE_SHARE_READ, 0, |
205 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); | |
206 | |
207 if (hfile1 != INVALID_HANDLE_VALUE) | |
208 { | |
209 HANDLE hfile2 = CreateFile (file2.c_str (), 0, FILE_SHARE_READ, 0, | |
210 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); | |
6598 | 211 |
6691 | 212 if (hfile2 != INVALID_HANDLE_VALUE) |
213 { | |
214 BY_HANDLE_FILE_INFORMATION hfi1; | |
215 BY_HANDLE_FILE_INFORMATION hfi2; | |
216 | |
217 if (GetFileInformationByHandle (hfile1, &hfi1) | |
218 && GetFileInformationByHandle (hfile2, &hfi2)) | |
6598 | 219 |
6691 | 220 retval = (hfi1.dwVolumeSerialNumber == hfi2.dwVolumeSerialNumber |
221 && hfi1.nFileIndexHigh == hfi2.nFileIndexHigh | |
222 && hfi1.nFileIndexLow == hfi2.nFileIndexLow); | |
6598 | 223 |
6691 | 224 CloseHandle (hfile2); |
225 } | |
226 | |
6598 | 227 CloseHandle (hfile1); |
228 } | |
6691 | 229 |
230 return retval; | |
6598 | 231 |
232 #else | |
233 | |
234 // POSIX Code | |
235 | |
236 file_stat fs_file1 (file1); | |
237 file_stat fs_file2 (file2); | |
238 | |
239 return (fs_file1 && fs_file2 | |
240 && fs_file1.ino () == fs_file2.ino () | |
241 && fs_file1.dev () == fs_file2.dev ()); | |
242 | |
243 #endif | |
244 } | |
245 | |
4284 | 246 #if defined (__DECCXX) |
247 | |
248 // These don't seem to be instantiated automatically... | |
249 | |
250 template std::istream& | |
251 std::operator >> (std::istream&, std::complex<double>&); | |
252 | |
253 template std::string& | |
254 std::string::append (const std::string&, size_t, size_t); | |
255 | |
256 #endif | |
257 | |
2610 | 258 #if defined (NeXT) |
1 | 259 extern "C" |
260 { | |
261 typedef void (*_cplus_fcn_int) (int); | |
262 extern void (*malloc_error (_cplus_fcn_int)) (int); | |
263 } | |
264 | |
265 static void | |
266 malloc_handler (int code) | |
267 { | |
268 if (code == 5) | |
217 | 269 warning ("hopefully recoverable malloc error: freeing wild pointer"); |
1 | 270 else |
2926 | 271 panic ("probably irrecoverable malloc error: code %d", code); |
1 | 272 } |
273 | |
274 static void | |
275 NeXT_init (void) | |
276 { | |
277 malloc_error (malloc_handler); | |
278 } | |
3333 | 279 #endif |
1 | 280 |
2546 | 281 #if defined (__EMX__) |
282 OS2_init (void) | |
283 { | |
284 _control87 ((EM_INVALID | EM_DENORMAL | EM_ZERODIVIDE | EM_OVERFLOW | |
285 | EM_UNDERFLOW | EM_INEXACT), MCW_EM); | |
286 } | |
287 #endif | |
288 | |
2508 | 289 #if defined (SCO) |
290 static void | |
291 SCO_init (void) | |
292 { | |
293 #if defined (HAVE_IEEEFP_H) | |
294 // Disable trapping on common exceptions. | |
295 fpsetmask (~(FP_X_OFL|FP_X_INV|FP_X_DZ|FP_X_DNML|FP_X_UFL|FP_X_IMP)); | |
296 #endif | |
297 } | |
298 #endif | |
299 | |
1 | 300 void |
301 sysdep_init (void) | |
302 { | |
12046
efdc5df061b6
initialize floating point values properly for NetBSD systems
Aleksej Saushev <asau@inbox.ru>
parents:
12028
diff
changeset
|
303 #if defined (__386BSD__) || defined (__FreeBSD__) || defined(__NetBSD__) |
2508 | 304 BSD_init (); |
5451 | 305 #elif defined (__MINGW32__) |
306 MINGW_init (); | |
6080 | 307 #elif defined (_MSC_VER) |
308 MSVC_init (); | |
2610 | 309 #elif defined (NeXT) |
1 | 310 NeXT_init (); |
2610 | 311 #elif defined (__EMX__) |
2546 | 312 OS2_init (); |
2508 | 313 #elif defined (SCO) |
314 SCO_init (); | |
1 | 315 #endif |
444 | 316 |
317 octave_ieee_init (); | |
1 | 318 } |
319 | |
5451 | 320 void |
321 sysdep_cleanup (void) | |
322 { | |
323 MINGW_SIGNAL_CLEANUP (); | |
324 } | |
325 | |
767 | 326 // Set terminal in raw mode. From less-177. |
327 // | |
328 // Change terminal to "raw mode", or restore to "normal" mode. | |
329 // "Raw mode" means | |
330 // 1. An outstanding read will complete on receipt of a single keystroke. | |
331 // 2. Input is not echoed. | |
332 // 3. On output, \n is mapped to \r\n. | |
333 // 4. \t is NOT expanded into spaces. | |
334 // 5. Signal-causing characters such as ctrl-C (interrupt), | |
335 // etc. are NOT disabled. | |
336 // It doesn't matter whether an input \n is mapped to \r, or vice versa. | |
337 | |
529 | 338 void |
3657 | 339 raw_mode (bool on, bool wait) |
529 | 340 { |
3657 | 341 static bool curr_on = false; |
529 | 342 |
343 int tty_fd = STDIN_FILENO; | |
344 if (! isatty (tty_fd)) | |
345 { | |
346 if (interactive) | |
347 error ("stdin is not a tty!"); | |
348 return; | |
349 } | |
350 | |
351 if (on == curr_on) | |
352 return; | |
353 | |
354 #if defined (HAVE_TERMIOS_H) | |
355 { | |
356 struct termios s; | |
357 static struct termios save_term; | |
358 | |
359 if (on) | |
360 { | |
1358 | 361 // Get terminal modes. |
529 | 362 |
363 tcgetattr (tty_fd, &s); | |
364 | |
1358 | 365 // Save modes and set certain variables dependent on modes. |
529 | 366 |
367 save_term = s; | |
368 // ospeed = s.c_cflag & CBAUD; | |
369 // erase_char = s.c_cc[VERASE]; | |
370 // kill_char = s.c_cc[VKILL]; | |
371 | |
1358 | 372 // Set the modes to the way we want them. |
529 | 373 |
374 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL); | |
375 s.c_oflag |= (OPOST|ONLCR); | |
376 #if defined (OCRNL) | |
377 s.c_oflag &= ~(OCRNL); | |
378 #endif | |
379 #if defined (ONOCR) | |
380 s.c_oflag &= ~(ONOCR); | |
381 #endif | |
382 #if defined (ONLRET) | |
383 s.c_oflag &= ~(ONLRET); | |
384 #endif | |
3658 | 385 s.c_cc[VMIN] = wait ? 1 : 0; |
529 | 386 s.c_cc[VTIME] = 0; |
387 } | |
388 else | |
389 { | |
1358 | 390 // Restore saved modes. |
391 | |
529 | 392 s = save_term; |
393 } | |
3658 | 394 |
12028
2de1ae881cea
fix kbhit(1) to not discard waiting keystrokes
Joe Rothweiler <octaveuser@sensicomm.com>
parents:
9242
diff
changeset
|
395 tcsetattr (tty_fd, wait ? TCSAFLUSH : TCSADRAIN, &s); |
529 | 396 } |
397 #elif defined (HAVE_TERMIO_H) | |
398 { | |
399 struct termio s; | |
400 static struct termio save_term; | |
401 | |
402 if (on) | |
403 { | |
1358 | 404 // Get terminal modes. |
529 | 405 |
406 ioctl (tty_fd, TCGETA, &s); | |
407 | |
1358 | 408 // Save modes and set certain variables dependent on modes. |
529 | 409 |
410 save_term = s; | |
411 // ospeed = s.c_cflag & CBAUD; | |
412 // erase_char = s.c_cc[VERASE]; | |
413 // kill_char = s.c_cc[VKILL]; | |
414 | |
1358 | 415 // Set the modes to the way we want them. |
529 | 416 |
417 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL); | |
418 s.c_oflag |= (OPOST|ONLCR); | |
419 #if defined (OCRNL) | |
420 s.c_oflag &= ~(OCRNL); | |
421 #endif | |
422 #if defined (ONOCR) | |
423 s.c_oflag &= ~(ONOCR); | |
424 #endif | |
425 #if defined (ONLRET) | |
426 s.c_oflag &= ~(ONLRET); | |
427 #endif | |
3658 | 428 s.c_cc[VMIN] = wait ? 1 : 0; |
529 | 429 } |
430 else | |
431 { | |
1358 | 432 // Restore saved modes. |
433 | |
529 | 434 s = save_term; |
435 } | |
3658 | 436 |
529 | 437 ioctl (tty_fd, TCSETAW, &s); |
438 } | |
439 #elif defined (HAVE_SGTTY_H) | |
440 { | |
441 struct sgttyb s; | |
442 static struct sgttyb save_term; | |
443 | |
444 if (on) | |
445 { | |
1358 | 446 // Get terminal modes. |
529 | 447 |
448 ioctl (tty_fd, TIOCGETP, &s); | |
449 | |
1358 | 450 // Save modes and set certain variables dependent on modes. |
529 | 451 |
452 save_term = s; | |
453 // ospeed = s.sg_ospeed; | |
454 // erase_char = s.sg_erase; | |
455 // kill_char = s.sg_kill; | |
456 | |
1358 | 457 // Set the modes to the way we want them. |
529 | 458 |
459 s.sg_flags |= CBREAK; | |
460 s.sg_flags &= ~(ECHO); | |
461 } | |
462 else | |
463 { | |
1358 | 464 // Restore saved modes. |
465 | |
529 | 466 s = save_term; |
467 } | |
3658 | 468 |
529 | 469 ioctl (tty_fd, TIOCSETN, &s); |
470 } | |
471 #else | |
4064 | 472 warning ("no support for raw mode console I/O on this system"); |
473 | |
474 // Make sure the current mode doesn't toggle. | |
475 on = curr_on; | |
529 | 476 #endif |
477 | |
478 curr_on = on; | |
479 } | |
480 | |
6726 | 481 FILE * |
482 octave_popen (const char *command, const char *mode) | |
483 { | |
484 #if defined (__MINGW32__) || defined (_MSC_VER) | |
485 if (mode && mode[0] && ! mode[1]) | |
486 { | |
487 char tmode[3]; | |
488 tmode[0] = mode[0]; | |
489 tmode[1] = 'b'; | |
490 tmode[2] = 0; | |
491 | |
492 return _popen (command, tmode); | |
493 } | |
494 else | |
495 return _popen (command, mode); | |
496 #else | |
497 return popen (command, mode); | |
498 #endif | |
499 } | |
500 | |
501 int | |
502 octave_pclose (FILE *f) | |
503 { | |
504 #if defined (__MINGW32__) || defined (_MSC_VER) | |
505 return _pclose (f); | |
506 #else | |
507 return pclose (f); | |
508 #endif | |
509 } | |
510 | |
767 | 511 // Read one character from the terminal. |
512 | |
529 | 513 int |
4067 | 514 octave_kbhit (bool wait) |
529 | 515 { |
4067 | 516 #ifdef HAVE__KBHIT |
4081 | 517 int c = (! wait && ! _kbhit ()) ? 0 : std::cin.get (); |
4067 | 518 #else |
3658 | 519 raw_mode (true, wait); |
520 | |
5770 | 521 // Get current handler. |
522 octave_interrupt_handler saved_interrupt_handler | |
523 = octave_ignore_interrupts (); | |
524 | |
525 // Restore it, disabling system call restarts (if possible) so the | |
526 // read can be interrupted. | |
527 | |
528 octave_set_interrupt_handler (saved_interrupt_handler, false); | |
529 | |
3658 | 530 int c = std::cin.get (); |
5770 | 531 |
3658 | 532 if (std::cin.fail () || std::cin.eof ()) |
533 std::cin.clear (); | |
534 | |
5770 | 535 // Restore it, enabling system call restarts (if possible). |
536 octave_set_interrupt_handler (saved_interrupt_handler, true); | |
537 | |
3658 | 538 raw_mode (false, true); |
4067 | 539 #endif |
3658 | 540 |
529 | 541 return c; |
542 } | |
543 | |
1957 | 544 DEFUN (clc, , , |
3332 | 545 "-*- texinfo -*-\n\ |
546 @deftypefn {Built-in Function} {} clc ()\n\ | |
547 @deftypefnx {Built-in Function} {} home ()\n\ | |
548 Clear the terminal screen and move the cursor to the upper left corner.\n\ | |
3333 | 549 @end deftypefn") |
529 | 550 { |
2926 | 551 command_editor::clear_screen (); |
529 | 552 |
2926 | 553 return octave_value_list (); |
529 | 554 } |
555 | |
549 | 556 DEFALIAS (home, clc); |
557 | |
1957 | 558 DEFUN (getenv, args, , |
3301 | 559 "-*- texinfo -*-\n\ |
560 @deftypefn {Built-in Function} {} getenv (@var{var})\n\ | |
561 Return the value of the environment variable @var{var}. For example,\n\ | |
562 \n\ | |
563 @example\n\ | |
564 getenv (\"PATH\")\n\ | |
565 @end example\n\ | |
566 \n\ | |
567 @noindent\n\ | |
568 returns a string containing the value of your path.\n\ | |
569 @end deftypefn") | |
529 | 570 { |
4233 | 571 octave_value retval; |
529 | 572 |
573 int nargin = args.length (); | |
574 | |
712 | 575 if (nargin == 1) |
529 | 576 { |
3523 | 577 std::string name = args(0).string_value (); |
636 | 578 |
579 if (! error_state) | |
2926 | 580 retval = octave_env::getenv (name); |
529 | 581 } |
582 else | |
5823 | 583 print_usage (); |
529 | 584 |
585 return retval; | |
586 } | |
587 | |
1957 | 588 DEFUN (putenv, args, , |
3301 | 589 "-*- texinfo -*-\n\ |
590 @deftypefn {Built-in Function} {} putenv (@var{var}, @var{value})\n\ | |
7759
84a7f00586aa
Alias setenv to putenv. Allow single arg to putenv
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
591 @deftypefnx {Built-in Function} {} setenv (@var{var}, @var{value})\n\ |
3301 | 592 Set the value of the environment variable @var{var} to @var{value}.\n\ |
593 @end deftypefn") | |
1706 | 594 { |
2086 | 595 octave_value_list retval; |
1706 | 596 |
597 int nargin = args.length (); | |
598 | |
7759
84a7f00586aa
Alias setenv to putenv. Allow single arg to putenv
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
599 if (nargin == 2 || nargin == 1) |
1706 | 600 { |
3523 | 601 std::string var = args(0).string_value (); |
1706 | 602 |
603 if (! error_state) | |
604 { | |
7759
84a7f00586aa
Alias setenv to putenv. Allow single arg to putenv
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
605 std::string val = (nargin == 2 |
84a7f00586aa
Alias setenv to putenv. Allow single arg to putenv
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
606 ? args(1).string_value () : std::string ()); |
1706 | 607 |
608 if (! error_state) | |
2926 | 609 octave_env::putenv (var, val); |
1706 | 610 else |
611 error ("putenv: second argument should be a string"); | |
612 } | |
613 else | |
614 error ("putenv: first argument should be a string"); | |
615 } | |
616 else | |
5823 | 617 print_usage (); |
1706 | 618 |
619 return retval; | |
620 } | |
7759
84a7f00586aa
Alias setenv to putenv. Allow single arg to putenv
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
621 DEFALIAS (setenv, putenv); |
1706 | 622 |
5775 | 623 // FIXME -- perhaps kbhit should also be able to print a prompt? |
3372 | 624 |
3657 | 625 DEFUN (kbhit, args, , |
3372 | 626 "-*- texinfo -*-\n\ |
627 @deftypefn {Built-in Function} {} kbhit ()\n\ | |
9039
51dc9691f23f
Cleanup documentation files errors.texi, debug.texi, io.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
628 Read a single keystroke from the keyboard. If called with one\n\ |
3657 | 629 argument, don't wait for a keypress. For example,\n\ |
3372 | 630 \n\ |
631 @example\n\ | |
632 x = kbhit ();\n\ | |
633 @end example\n\ | |
634 \n\ | |
635 @noindent\n\ | |
636 will set @var{x} to the next character typed at the keyboard as soon as\n\ | |
637 it is typed.\n\ | |
3657 | 638 \n\ |
639 @example\n\ | |
640 x = kbhit (1);\n\ | |
641 @end example\n\ | |
642 \n\ | |
643 @noindent\n\ | |
644 identical to the above example, but don't wait for a keypress,\n\ | |
645 returning the empty string if no key is available.\n\ | |
3372 | 646 @end deftypefn") |
529 | 647 { |
4233 | 648 octave_value retval; |
529 | 649 |
5775 | 650 // FIXME -- add timeout and default value args? |
529 | 651 |
3676 | 652 if (interactive || forced_interactive) |
529 | 653 { |
6423 | 654 feval ("drawnow"); |
655 | |
4067 | 656 int c = octave_kbhit (args.length () == 0); |
3657 | 657 |
3658 | 658 if (c == -1) |
659 c = 0; | |
3657 | 660 |
529 | 661 char *s = new char [2]; |
662 s[0] = c; | |
663 s[1] = '\0'; | |
664 retval = s; | |
665 } | |
666 | |
667 return retval; | |
668 } | |
669 | |
1957 | 670 DEFUN (pause, args, , |
3301 | 671 "-*- texinfo -*-\n\ |
672 @deftypefn {Built-in Function} {} pause (@var{seconds})\n\ | |
673 Suspend the execution of the program. If invoked without any arguments,\n\ | |
674 Octave waits until you type a character. With a numeric argument, it\n\ | |
675 pauses for the given number of seconds. For example, the following\n\ | |
676 statement prints a message and then waits 5 seconds before clearing the\n\ | |
677 screen.\n\ | |
678 \n\ | |
679 @example\n\ | |
680 @group\n\ | |
6848 | 681 fprintf (stderr, \"wait please...\\n\");\n\ |
3301 | 682 pause (5);\n\ |
683 clc;\n\ | |
684 @end group\n\ | |
685 @end example\n\ | |
686 @end deftypefn") | |
529 | 687 { |
2086 | 688 octave_value_list retval; |
529 | 689 |
690 int nargin = args.length (); | |
691 | |
712 | 692 if (! (nargin == 0 || nargin == 1)) |
529 | 693 { |
5823 | 694 print_usage (); |
529 | 695 return retval; |
696 } | |
697 | |
1579 | 698 if (nargin == 1) |
529 | 699 { |
1579 | 700 double dval = args(0).double_value (); |
636 | 701 |
1579 | 702 if (! error_state) |
703 { | |
6419 | 704 if (! xisnan (dval)) |
3234 | 705 { |
6419 | 706 feval ("drawnow"); |
707 | |
708 if (xisinf (dval)) | |
709 { | |
710 flush_octave_stdout (); | |
711 octave_kbhit (); | |
712 } | |
713 else | |
714 octave_sleep (dval); | |
3234 | 715 } |
1579 | 716 else |
6419 | 717 warning ("pause: NaN is an invalid delay"); |
529 | 718 } |
719 } | |
1579 | 720 else |
3234 | 721 { |
6423 | 722 feval ("drawnow"); |
3234 | 723 flush_octave_stdout (); |
4067 | 724 octave_kbhit (); |
3234 | 725 } |
2630 | 726 |
727 return retval; | |
728 } | |
729 | |
730 DEFUN (sleep, args, , | |
3301 | 731 "-*- texinfo -*-\n\ |
732 @deftypefn {Built-in Function} {} sleep (@var{seconds})\n\ | |
733 Suspend the execution of the program for the given number of seconds.\n\ | |
734 @end deftypefn") | |
2630 | 735 { |
736 octave_value_list retval; | |
737 | |
738 if (args.length () == 1) | |
1579 | 739 { |
2630 | 740 double dval = args(0).double_value (); |
741 | |
742 if (! error_state) | |
743 { | |
744 if (xisnan (dval)) | |
745 warning ("sleep: NaN is an invalid delay"); | |
746 else | |
6423 | 747 { |
748 feval ("drawnow"); | |
749 octave_sleep (dval); | |
750 } | |
2630 | 751 } |
1579 | 752 } |
2630 | 753 else |
5823 | 754 print_usage (); |
2630 | 755 |
756 return retval; | |
757 } | |
758 | |
759 DEFUN (usleep, args, , | |
3301 | 760 "-*- texinfo -*-\n\ |
761 @deftypefn {Built-in Function} {} usleep (@var{microseconds})\n\ | |
762 Suspend the execution of the program for the given number of\n\ | |
763 microseconds. On systems where it is not possible to sleep for periods\n\ | |
764 of time less than one second, @code{usleep} will pause the execution for\n\ | |
765 @code{round (@var{microseconds} / 1e6)} seconds.\n\ | |
766 @end deftypefn") | |
2630 | 767 { |
768 octave_value_list retval; | |
769 | |
770 if (args.length () == 1) | |
771 { | |
772 double dval = args(0).double_value (); | |
773 | |
774 if (! error_state) | |
775 { | |
776 if (xisnan (dval)) | |
777 warning ("usleep: NaN is an invalid delay"); | |
778 else | |
779 { | |
6423 | 780 feval ("drawnow"); |
781 | |
2630 | 782 int delay = NINT (dval); |
2631 | 783 |
2630 | 784 if (delay > 0) |
3308 | 785 octave_usleep (delay); |
2630 | 786 } |
787 } | |
788 } | |
789 else | |
5823 | 790 print_usage (); |
529 | 791 |
792 return retval; | |
793 } | |
794 | |
5775 | 795 // FIXME -- maybe this should only return 1 if IEEE floating |
862 | 796 // point functions really work. |
797 | |
1957 | 798 DEFUN (isieee, , , |
3301 | 799 "-*- texinfo -*-\n\ |
800 @deftypefn {Built-in Function} {} isieee ()\n\ | |
801 Return 1 if your computer claims to conform to the IEEE standard for\n\ | |
802 floating point calculations.\n\ | |
803 @end deftypefn") | |
862 | 804 { |
4600 | 805 oct_mach_info::float_format flt_fmt = oct_mach_info::native_float_format (); |
2317 | 806 |
4574 | 807 return octave_value (flt_fmt == oct_mach_info::flt_fmt_ieee_little_endian |
808 || flt_fmt == oct_mach_info::flt_fmt_ieee_big_endian); | |
862 | 809 } |
810 | |
4600 | 811 DEFUN (native_float_format, , , |
812 "-*- texinfo -*-\n\ | |
813 @deftypefn {Built-in Function} {} native_float_format ()\n\ | |
814 Return the native floating point format as a string\n\ | |
815 @end deftypefn") | |
816 { | |
817 oct_mach_info::float_format flt_fmt = oct_mach_info::native_float_format (); | |
818 | |
819 return octave_value (oct_mach_info::float_format_as_string (flt_fmt)); | |
820 } | |
821 | |
1957 | 822 DEFUN (tilde_expand, args, , |
3301 | 823 "-*- texinfo -*-\n\ |
824 @deftypefn {Built-in Function} {} tilde_expand (@var{string})\n\ | |
825 Performs tilde expansion on @var{string}. If @var{string} begins with a\n\ | |
826 tilde character, (@samp{~}), all of the characters preceding the first\n\ | |
827 slash (or all characters, if there is no slash) are treated as a\n\ | |
828 possible user name, and the tilde and the following characters up to the\n\ | |
829 slash are replaced by the home directory of the named user. If the\n\ | |
830 tilde is followed immediately by a slash, the tilde is replaced by the\n\ | |
831 home directory of the user running Octave. For example,\n\ | |
832 \n\ | |
833 @example\n\ | |
834 @group\n\ | |
835 tilde_expand (\"~joeuser/bin\")\n\ | |
836 @result{} \"/home/joeuser/bin\"\n\ | |
837 tilde_expand (\"~/bin\")\n\ | |
838 @result{} \"/home/jwe/bin\"\n\ | |
839 @end group\n\ | |
840 @end example\n\ | |
841 @end deftypefn") | |
1750 | 842 { |
4233 | 843 octave_value retval; |
1750 | 844 |
845 int nargin = args.length (); | |
846 | |
847 if (nargin == 1) | |
6116 | 848 { |
849 octave_value arg = args(0); | |
850 | |
851 string_vector sv = arg.all_strings (); | |
852 | |
853 if (! error_state) | |
854 { | |
855 sv = file_ops::tilde_expand (sv); | |
856 | |
857 if (arg.is_cellstr ()) | |
858 retval = Cell (arg.dims (), sv); | |
859 else | |
860 retval = sv; | |
861 } | |
862 else | |
863 error ("tilde_expand: expecting argument to be char or cellstr object"); | |
864 } | |
1750 | 865 else |
5823 | 866 print_usage (); |
1750 | 867 |
868 return retval; | |
869 } | |
870 | |
2552 | 871 #if defined (__EMX__) && defined (OS2) |
872 | |
8746
5dd06f19e9be
handle commands in the lexer
John W. Eaton <jwe@octave.org>
parents:
8021
diff
changeset
|
873 DEFUN (extproc, , , |
2926 | 874 "extproc: ignored by Octave") |
2552 | 875 { |
876 return octave_value_list (); | |
877 } | |
878 | |
879 DEFALIAS (EXTPROC, extproc); | |
880 | |
881 #endif | |
882 | |
529 | 883 /* |
1 | 884 ;;; Local Variables: *** |
885 ;;; mode: C++ *** | |
886 ;;; End: *** | |
887 */ |