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