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