Mercurial > hg > octave-nkf
annotate src/sysdep.cc @ 8019:0ef13e15319b
replace NPOS with std::string::npos
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 07 Aug 2008 15:15:33 -0400 |
parents | 84a7f00586aa |
children | 85184151822e |
rev | line source |
---|---|
1 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1999, 2000, 2002, 2003, |
4 2005, 2006, 2007 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 |
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 | |
8019
0ef13e15319b
replace NPOS with std::string::npos
John W. Eaton <jwe@octave.org>
parents:
7759
diff
changeset
|
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 #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\ | |
7759
84a7f00586aa
Alias setenv to putenv. Allow single arg to putenv
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
626 @deftypefnx {Built-in Function} {} setenv (@var{var}, @var{value})\n\ |
3301 | 627 Set the value of the environment variable @var{var} to @var{value}.\n\ |
628 @end deftypefn") | |
1706 | 629 { |
2086 | 630 octave_value_list retval; |
1706 | 631 |
632 int nargin = args.length (); | |
633 | |
7759
84a7f00586aa
Alias setenv to putenv. Allow single arg to putenv
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
634 if (nargin == 2 || nargin == 1) |
1706 | 635 { |
3523 | 636 std::string var = args(0).string_value (); |
1706 | 637 |
638 if (! error_state) | |
639 { | |
7759
84a7f00586aa
Alias setenv to putenv. Allow single arg to putenv
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
640 std::string val = (nargin == 2 |
84a7f00586aa
Alias setenv to putenv. Allow single arg to putenv
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
641 ? args(1).string_value () : std::string ()); |
1706 | 642 |
643 if (! error_state) | |
2926 | 644 octave_env::putenv (var, val); |
1706 | 645 else |
646 error ("putenv: second argument should be a string"); | |
647 } | |
648 else | |
649 error ("putenv: first argument should be a string"); | |
650 } | |
651 else | |
5823 | 652 print_usage (); |
1706 | 653 |
654 return retval; | |
655 } | |
7759
84a7f00586aa
Alias setenv to putenv. Allow single arg to putenv
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
656 DEFALIAS (setenv, putenv); |
1706 | 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 */ |