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