GNU Octave  4.0.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
sysdep.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1993-2015 John W. Eaton
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 3 of the License, or (at your
10 option) any 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, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include <cfloat>
28 #include <cstddef>
29 #include <cstdio>
30 #include <cstdlib>
31 #include <cstring>
32 
33 #include <iostream>
34 #include <string>
35 
36 #include <sys/types.h>
37 #include <unistd.h>
38 
39 #if defined (HAVE_TERMIOS_H)
40 #include <termios.h>
41 #elif defined (HAVE_TERMIO_H)
42 #include <termio.h>
43 #elif defined (HAVE_SGTTY_H)
44 #include <sgtty.h>
45 #endif
46 
47 #if defined (HAVE_CONIO_H)
48 #include <conio.h>
49 #endif
50 
51 #if defined (HAVE_SYS_IOCTL_H)
52 #include <sys/ioctl.h>
53 #endif
54 
55 #if defined (HAVE_FLOATINGPOINT_H)
56 #include <floatingpoint.h>
57 #endif
58 
59 #if defined (HAVE_IEEEFP_H)
60 #include <ieeefp.h>
61 #endif
62 
63 #include "cmd-edit.h"
64 #include "file-ops.h"
65 #include "lo-mappers.h"
66 #include "lo-math.h"
67 #include "mach-info.h"
68 #include "oct-env.h"
69 #include "quit.h"
70 
71 #include "Cell.h"
72 #include "builtins.h"
73 #include "defun.h"
74 #include "display.h"
75 #include "error.h"
76 #include "input.h"
77 #include "oct-obj.h"
78 #include "ov.h"
79 #include "pager.h"
80 #include "parse.h"
81 #include "sighandlers.h"
82 #include "sysdep.h"
83 #include "toplev.h"
84 #include "utils.h"
85 #include "file-stat.h"
86 
87 #ifndef STDIN_FILENO
88 #define STDIN_FILENO 1
89 #endif
90 
91 #if defined (__386BSD__) || defined (__FreeBSD__) || defined (__NetBSD__)
92 static void
93 BSD_init (void)
94 {
95 #if defined (HAVE_FLOATINGPOINT_H)
96  // Disable trapping on common exceptions.
97 #ifndef FP_X_DNML
98 #define FP_X_DNML 0
99 #endif
100  fpsetmask (~(FP_X_OFL|FP_X_INV|FP_X_DZ|FP_X_DNML|FP_X_UFL|FP_X_IMP));
101 #endif
102 }
103 #endif
104 
105 #if defined (__WIN32__) && ! defined (_POSIX_VERSION)
106 
107 #define WIN32_LEAN_AND_MEAN
108 #include <tlhelp32.h>
109 #include <windows.h>
110 
111 static void
112 w32_set_octave_home (void)
113 {
114  std::string bin_dir;
115 
116  HANDLE h = CreateToolhelp32Snapshot (TH32CS_SNAPMODULE
117 #ifdef TH32CS_SNAPMODULE32
118  | TH32CS_SNAPMODULE32
119 #endif
120  , 0);
121 
122  if (h != INVALID_HANDLE_VALUE)
123  {
124  MODULEENTRY32 mod_info;
125 
126  ZeroMemory (&mod_info, sizeof (mod_info));
127  mod_info.dwSize = sizeof (mod_info);
128 
129  if (Module32First (h, &mod_info))
130  {
131  do
132  {
133  std::string mod_name (mod_info.szModule);
134 
135  if (mod_name.find ("octinterp") != std::string::npos)
136  {
137  bin_dir = mod_info.szExePath;
138  if (bin_dir[bin_dir.length () - 1] != '\\')
139  bin_dir.append (1, '\\');
140  break;
141  }
142  }
143  while (Module32Next (h, &mod_info));
144  }
145 
146  CloseHandle (h);
147  }
148 
149  if (! bin_dir.empty ())
150  {
151  size_t pos = bin_dir.rfind ("\\bin\\");
152 
153  if (pos != std::string::npos)
154  octave_env::putenv ("OCTAVE_HOME", bin_dir.substr (0, pos));
155  }
156 }
157 
158 void
160 {
161  // Let the user close the console window or shutdown without the
162  // pesky dialog.
163  //
164  // FIXME: should this be user configurable?
165  SetProcessShutdownParameters (0x280, SHUTDOWN_NORETRY);
166 }
167 
168 void
169 MINGW_signal_cleanup (void)
170 {
172 }
173 
174 static void
175 w32_init (void)
176 {
177  w32_set_octave_home ();
178 
180 }
181 
182 static bool
183 w32_shell_execute (const std::string& file)
184 {
185 }
186 #endif
187 
188 DEFUN (__open_with_system_app__, args, ,
189  "-*- texinfo -*-\n\
190 @deftypefn {Loadable Function} {} __open_with_system_app__ (@var{file})\n\
191 Undocumented internal function.\n\
192 @end deftypefn")
193 {
194  octave_value retval;
195 
196  if (args.length () == 1)
197  {
198  std::string file = args(0).string_value ();
199 
200  if (! error_state)
201  {
202 #if defined (__WIN32__) && ! defined (_POSIX_VERSION)
203  HINSTANCE status = ShellExecute (0, 0, file.c_str (), 0, 0,
204  SW_SHOWNORMAL);
205 
206  // ShellExecute returns a value greater than 32 if successful.
207  retval = (reinterpret_cast<ptrdiff_t> (status) > 32);
208 #else
210  = Fsystem (ovl ("xdg-open " + file + " 2> /dev/null",
211  false, "async"),
212  1);
213 
214  retval = (tmp(0).double_value () == 0);
215 #endif
216  }
217  else
218  error ("__open_with_system_app__: argument must be a file name");
219  }
220  else
221  print_usage ();
222 
223  return retval;
224 }
225 
226 #if defined (__MINGW32__)
227 static void
228 MINGW_init (void)
229 {
230  w32_init ();
231 }
232 #endif
233 
234 #if defined (_MSC_VER)
235 static void
236 MSVC_init (void)
237 {
238  w32_init ();
239 }
240 #endif
241 
242 
243 // Return TRUE if FILE1 and FILE2 refer to the same (physical) file.
244 
245 bool
246 same_file_internal (const std::string& file1, const std::string& file2)
247 {
248 #ifdef OCTAVE_USE_WINDOWS_API
249 
250  bool retval = false;
251 
252  const char *f1 = file1.c_str ();
253  const char *f2 = file2.c_str ();
254 
255  bool f1_is_dir = GetFileAttributes (f1) & FILE_ATTRIBUTE_DIRECTORY;
256  bool f2_is_dir = GetFileAttributes (f2) & FILE_ATTRIBUTE_DIRECTORY;
257 
258  // Windows native code
259  // Reference: http://msdn2.microsoft.com/en-us/library/aa363788.aspx
260 
261  DWORD share = FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE;
262 
263  HANDLE hfile1
264  = CreateFile (f1, 0, share, 0, OPEN_EXISTING,
265  f1_is_dir ? FILE_FLAG_BACKUP_SEMANTICS : 0, 0);
266 
267  if (hfile1 != INVALID_HANDLE_VALUE)
268  {
269  HANDLE hfile2
270  = CreateFile (f2, 0, share, 0, OPEN_EXISTING,
271  f2_is_dir ? FILE_FLAG_BACKUP_SEMANTICS : 0, 0);
272 
273  if (hfile2 != INVALID_HANDLE_VALUE)
274  {
275  BY_HANDLE_FILE_INFORMATION hfi1;
276  BY_HANDLE_FILE_INFORMATION hfi2;
277 
278  if (GetFileInformationByHandle (hfile1, &hfi1)
279  && GetFileInformationByHandle (hfile2, &hfi2))
280  {
281  retval = (hfi1.dwVolumeSerialNumber == hfi2.dwVolumeSerialNumber
282  && hfi1.nFileIndexHigh == hfi2.nFileIndexHigh
283  && hfi1.nFileIndexLow == hfi2.nFileIndexLow);
284  }
285 
286  CloseHandle (hfile2);
287  }
288 
289  CloseHandle (hfile1);
290  }
291 
292  return retval;
293 
294 #else
295 
296  // POSIX Code
297 
298  file_stat fs_file1 (file1);
299  file_stat fs_file2 (file2);
300 
301  return (fs_file1 && fs_file2
302  && fs_file1.ino () == fs_file2.ino ()
303  && fs_file1.dev () == fs_file2.dev ());
304 
305 #endif
306 }
307 
308 void
310 {
311 #if defined (__386BSD__) || defined (__FreeBSD__) || defined (__NetBSD__)
312  BSD_init ();
313 #elif defined (__MINGW32__)
314  MINGW_init ();
315 #elif defined (_MSC_VER)
316  MSVC_init ();
317 #endif
318 }
319 
320 void
322 {
324 }
325 
326 // Set terminal in raw mode. From less-177.
327 //
328 // Change terminal to "raw mode", or restore to "normal" mode.
329 // "Raw mode" means
330 // 1. An outstanding read will complete on receipt of a single keystroke.
331 // 2. Input is not echoed.
332 // 3. On output, \n is mapped to \r\n.
333 // 4. \t is NOT expanded into spaces.
334 // 5. Signal-causing characters such as ctrl-C (interrupt),
335 // etc. are NOT disabled.
336 // It doesn't matter whether an input \n is mapped to \r, or vice versa.
337 
338 void
339 raw_mode (bool on, bool wait)
340 {
341  static bool curr_on = false;
342 
343  int tty_fd = STDIN_FILENO;
344  if (! gnulib::isatty (tty_fd))
345  {
347  error ("stdin is not a tty!");
348  return;
349  }
350 
351  if (on == curr_on)
352  return;
353 
354 #if defined (HAVE_TERMIOS_H)
355  {
356  struct termios s;
357  static struct termios save_term;
358 
359  if (on)
360  {
361  // Get terminal modes.
362 
363  tcgetattr (tty_fd, &s);
364 
365  // Save modes and set certain variables dependent on modes.
366 
367  save_term = s;
368 // ospeed = s.c_cflag & CBAUD;
369 // erase_char = s.c_cc[VERASE];
370 // kill_char = s.c_cc[VKILL];
371 
372  // Set the modes to the way we want them.
373 
374  s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL);
375  s.c_oflag |= (OPOST|ONLCR);
376 #if defined (OCRNL)
377  s.c_oflag &= ~(OCRNL);
378 #endif
379 #if defined (ONOCR)
380  s.c_oflag &= ~(ONOCR);
381 #endif
382 #if defined (ONLRET)
383  s.c_oflag &= ~(ONLRET);
384 #endif
385  s.c_cc[VMIN] = wait ? 1 : 0;
386  s.c_cc[VTIME] = 0;
387  }
388  else
389  {
390  // Restore saved modes.
391 
392  s = save_term;
393  }
394 
395  tcsetattr (tty_fd, wait ? TCSAFLUSH : TCSADRAIN, &s);
396  }
397 #elif defined (HAVE_TERMIO_H)
398  {
399  struct termio s;
400  static struct termio save_term;
401 
402  if (on)
403  {
404  // Get terminal modes.
405 
406  ioctl (tty_fd, TCGETA, &s);
407 
408  // Save modes and set certain variables dependent on modes.
409 
410  save_term = s;
411 // ospeed = s.c_cflag & CBAUD;
412 // erase_char = s.c_cc[VERASE];
413 // kill_char = s.c_cc[VKILL];
414 
415  // Set the modes to the way we want them.
416 
417  s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL);
418  s.c_oflag |= (OPOST|ONLCR);
419 #if defined (OCRNL)
420  s.c_oflag &= ~(OCRNL);
421 #endif
422 #if defined (ONOCR)
423  s.c_oflag &= ~(ONOCR);
424 #endif
425 #if defined (ONLRET)
426  s.c_oflag &= ~(ONLRET);
427 #endif
428  s.c_cc[VMIN] = wait ? 1 : 0;
429  }
430  else
431  {
432  // Restore saved modes.
433 
434  s = save_term;
435  }
436 
437  ioctl (tty_fd, TCSETAW, &s);
438  }
439 #elif defined (HAVE_SGTTY_H)
440  {
441  struct sgttyb s;
442  static struct sgttyb save_term;
443 
444  if (on)
445  {
446  // Get terminal modes.
447 
448  ioctl (tty_fd, TIOCGETP, &s);
449 
450  // Save modes and set certain variables dependent on modes.
451 
452  save_term = s;
453 // ospeed = s.sg_ospeed;
454 // erase_char = s.sg_erase;
455 // kill_char = s.sg_kill;
456 
457  // Set the modes to the way we want them.
458 
459  s.sg_flags |= CBREAK;
460  s.sg_flags &= ~(ECHO);
461  }
462  else
463  {
464  // Restore saved modes.
465 
466  s = save_term;
467  }
468 
469  ioctl (tty_fd, TIOCSETN, &s);
470  }
471 #else
472  warning ("no support for raw mode console I/O on this system");
473 
474  // Make sure the current mode doesn't toggle.
475  on = curr_on;
476 #endif
477 
478  curr_on = on;
479 }
480 
481 FILE *
482 octave_popen (const char *command, const char *mode)
483 {
484 #if defined (__MINGW32__) || defined (_MSC_VER)
485  if (mode && mode[0] && ! mode[1])
486  {
487  char tmode[3];
488  tmode[0] = mode[0];
489  tmode[1] = 'b';
490  tmode[2] = 0;
491 
492  return _popen (command, tmode);
493  }
494  else
495  return _popen (command, mode);
496 #else
497  return popen (command, mode);
498 #endif
499 }
500 
501 int
503 {
504 #if defined (__MINGW32__) || defined (_MSC_VER)
505  return _pclose (f);
506 #else
507  return pclose (f);
508 #endif
509 }
510 
511 // Read one character from the terminal.
512 
513 int
514 octave_kbhit (bool wait)
515 {
516 #ifdef HAVE__KBHIT
517  int c = (! wait && ! _kbhit ()) ? 0 : std::cin.get ();
518 #else
519  raw_mode (true, wait);
520 
521  // Get current handler.
522  octave_interrupt_handler saved_interrupt_handler
524 
525  // Restore it, disabling system call restarts (if possible) so the
526  // read can be interrupted.
527 
528  octave_set_interrupt_handler (saved_interrupt_handler, false);
529 
530  int c = std::cin.get ();
531 
532  if (std::cin.fail () || std::cin.eof ())
533  std::cin.clear ();
534 
535  // Restore it, enabling system call restarts (if possible).
536  octave_set_interrupt_handler (saved_interrupt_handler, true);
537 
538  raw_mode (false, true);
539 #endif
540 
541  return c;
542 }
543 
544 std::string
546 {
547 #if defined (__WIN32__) && ! defined (_POSIX_VERSION)
548 
549  std::string retval;
550 
551 #if defined (P_tmpdir)
552  retval = P_tmpdir;
553 #endif
554 
555  // Apparently some versions of MinGW and MSVC either don't define
556  // P_tmpdir, or they define it to a single backslash, neither of which
557  // is particularly helpful.
558 
559  if (retval.empty () || retval == "\\")
560  {
561  retval = octave_env::getenv ("TEMP");
562 
563  if (retval.empty ())
564  retval = octave_env::getenv ("TMP");
565 
566  if (retval.empty ())
567  retval = "c:\\temp";
568  }
569 
570  return retval;
571 
572 #elif defined (P_tmpdir)
573 
574  return P_tmpdir;
575 
576 #else
577 
578  return "/tmp";
579 
580 #endif
581 }
582 
583 DEFUN (clc, , ,
584  "-*- texinfo -*-\n\
585 @deftypefn {Built-in Function} {} clc ()\n\
586 @deftypefnx {Built-in Function} {} home ()\n\
587 Clear the terminal screen and move the cursor to the upper left corner.\n\
588 @end deftypefn")
589 {
590  bool skip_redisplay = true;
591 
592  command_editor::clear_screen (skip_redisplay);
593 
594  return octave_value_list ();
595 }
596 
597 DEFALIAS (home, clc);
598 
599 DEFUN (getenv, args, ,
600  "-*- texinfo -*-\n\
601 @deftypefn {Built-in Function} {} getenv (@var{var})\n\
602 Return the value of the environment variable @var{var}.\n\
603 \n\
604 For example,\n\
605 \n\
606 @example\n\
607 getenv (\"PATH\")\n\
608 @end example\n\
609 \n\
610 @noindent\n\
611 returns a string containing the value of your path.\n\
612 @seealso{setenv, unsetenv}\n\
613 @end deftypefn")
614 {
615  octave_value retval;
616 
617  int nargin = args.length ();
618 
619  if (nargin == 1)
620  {
621  std::string name = args(0).string_value ();
622 
623  if (! error_state)
624  retval = octave_env::getenv (name);
625  }
626  else
627  print_usage ();
628 
629  return retval;
630 }
631 
632 /*
633 %!assert (ischar (getenv ("OCTAVE_HOME")))
634 */
635 
636 DEFUN (setenv, args, ,
637  "-*- texinfo -*-\n\
638 @deftypefn {Built-in Function} {} setenv (@var{var}, @var{value})\n\
639 @deftypefnx {Built-in Function} {} setenv (@var{var})\n\
640 @deftypefnx {Built-in Function} {} putenv (@dots{})\n\
641 Set the value of the environment variable @var{var} to @var{value}.\n\
642 \n\
643 If no @var{value} is specified then the variable will be assigned the null\n\
644 string.\n\
645 @seealso{unsetenv, getenv}\n\
646 @end deftypefn")
647 {
648  octave_value_list retval;
649 
650  int nargin = args.length ();
651 
652  if (nargin == 2 || nargin == 1)
653  {
654  if (args(0).is_string ())
655  {
656  std::string var = args(0).string_value ();
657 
658  std::string val = (nargin == 2
659  ? args(1).string_value () : std::string ());
660 
661  if (! error_state)
662  octave_env::putenv (var, val);
663  else
664  error ("setenv: VALUE must be a string");
665  }
666  else
667  error ("setenv: VAR must be a string");
668  }
669  else
670  print_usage ();
671 
672  return retval;
673 }
674 
675 DEFALIAS (putenv, setenv);
676 
677 /*
678 %!test
679 %! setenv ("dummy_variable_that_cannot_matter", "foobar");
680 %! assert (getenv ("dummy_variable_that_cannot_matter"), "foobar");
681 %! unsetenv ("dummy_variable_that_cannot_matter");
682 %! assert (getenv ("dummy_variable_that_cannot_matter"), "");
683 */
684 
685 DEFUN (unsetenv, args, ,
686  "-*- texinfo -*-\n\
687 @deftypefn {Built-in Function} {@var{status} =} unsetenv (@var{var})\n\
688 Delete the environment variable @var{var}.\n\
689 \n\
690 Return 0 if the variable was deleted, or did not exist, and -1 if an error\n\
691 occurred.\n\
692 @seealso{setenv, getenv}\n\
693 @end deftypefn")
694 {
695  octave_value retval;
696 
697  int nargin = args.length ();
698 
699  if (nargin == 1)
700  {
701  std::string tmp = args(0).string_value ();
702 
703  if (! error_state)
704  {
705  int status = gnulib::unsetenv (tmp.c_str ());
706  retval = status;
707  }
708  }
709  else
710  print_usage ();
711 
712  return retval;
713 }
714 
715 /*
716 ## Test for unsetenv is in setenv test
717 */
718 
719 // FIXME: perhaps kbhit should also be able to print a prompt?
720 
721 DEFUN (kbhit, args, ,
722  "-*- texinfo -*-\n\
723 @deftypefn {Built-in Function} {} kbhit ()\n\
724 @deftypefnx {Built-in Function} {} kbhit (1)\n\
725 Read a single keystroke from the keyboard.\n\
726 \n\
727 If called with an argument, don't wait for a keypress.\n\
728 \n\
729 For example,\n\
730 \n\
731 @example\n\
732 x = kbhit ();\n\
733 @end example\n\
734 \n\
735 @noindent\n\
736 will set @var{x} to the next character typed at the keyboard as soon as\n\
737 it is typed.\n\
738 \n\
739 @example\n\
740 x = kbhit (1);\n\
741 @end example\n\
742 \n\
743 @noindent\n\
744 is identical to the above example, but doesn't wait for a keypress,\n\
745 returning the empty string if no key is available.\n\
746 @seealso{input, pause}\n\
747 @end deftypefn")
748 {
749  octave_value retval;
750 
751  // FIXME: add timeout and default value args?
752 
753  if (interactive)
754  {
755  Fdrawnow ();
756 
757  int c = octave_kbhit (args.length () == 0);
758 
759  if (c == -1)
760  c = 0;
761 
762  char s[2] = { static_cast<char> (c), '\0' };
763 
764  retval = s;
765  }
766 
767  return retval;
768 }
769 
770 DEFUN (pause, args, ,
771  "-*- texinfo -*-\n\
772 @deftypefn {Built-in Function} {} pause ()\n\
773 @deftypefnx {Built-in Function} {} pause (@var{n})\n\
774 Suspend the execution of the program for @var{n} seconds.\n\
775 \n\
776 @var{n} is a positive real value and may be a fraction of a second.\n\
777 \n\
778 If invoked without an input arguments then the program is suspended until a\n\
779 character is typed.\n\
780 \n\
781 The following example prints a message and then waits 5 seconds before\n\
782 clearing the screen.\n\
783 \n\
784 @example\n\
785 @group\n\
786 fprintf (stderr, \"wait please...\\n\");\n\
787 pause (5);\n\
788 clc;\n\
789 @end group\n\
790 @end example\n\
791 @seealso{kbhit, sleep}\n\
792 @end deftypefn")
793 {
794  octave_value_list retval;
795 
796  int nargin = args.length ();
797 
798  if (! (nargin == 0 || nargin == 1))
799  {
800  print_usage ();
801  return retval;
802  }
803 
804  if (nargin == 1)
805  {
806  double dval = args(0).double_value ();
807 
808  if (! error_state)
809  {
810  if (! xisnan (dval))
811  {
812  Fdrawnow ();
813 
814  if (xisinf (dval))
815  {
817  octave_kbhit ();
818  }
819  else
820  octave_sleep (dval);
821  }
822  else
823  warning ("pause: NaN is an invalid delay");
824  }
825  }
826  else
827  {
828  Fdrawnow ();
830  octave_kbhit ();
831  }
832 
833  return retval;
834 }
835 
836 /*
837 %!test
838 %! pause (1);
839 
840 %!error (pause (1, 2))
841 */
842 
843 DEFUN (sleep, args, ,
844  "-*- texinfo -*-\n\
845 @deftypefn {Built-in Function} {} sleep (@var{seconds})\n\
846 Suspend the execution of the program for the given number of seconds.\n\
847 @seealso{usleep, pause}\n\
848 @end deftypefn")
849 {
850  octave_value_list retval;
851 
852  if (args.length () == 1)
853  {
854  double dval = args(0).double_value ();
855 
856  if (! error_state)
857  {
858  if (xisnan (dval))
859  warning ("sleep: NaN is an invalid delay");
860  else
861  {
862  Fdrawnow ();
863  octave_sleep (dval);
864  }
865  }
866  }
867  else
868  print_usage ();
869 
870  return retval;
871 }
872 
873 /*
874 %!test
875 %! sleep (1);
876 
877 %!error (sleep ())
878 %!error (sleep (1, 2))
879 */
880 
881 DEFUN (usleep, args, ,
882  "-*- texinfo -*-\n\
883 @deftypefn {Built-in Function} {} usleep (@var{microseconds})\n\
884 Suspend the execution of the program for the given number of\n\
885 microseconds.\n\
886 \n\
887 On systems where it is not possible to sleep for periods of time less than\n\
888 one second, @code{usleep} will pause the execution for @code{round\n\
889 (@var{microseconds} / 1e6)} seconds.\n\
890 @seealso{sleep, pause}\n\
891 @end deftypefn")
892 {
893  octave_value_list retval;
894 
895  if (args.length () == 1)
896  {
897  double dval = args(0).double_value ();
898 
899  if (! error_state)
900  {
901  if (xisnan (dval))
902  warning ("usleep: NaN is an invalid delay");
903  else
904  {
905  Fdrawnow ();
906 
907  int delay = NINT (dval);
908 
909  if (delay > 0)
910  octave_usleep (delay);
911  }
912  }
913  }
914  else
915  print_usage ();
916 
917  return retval;
918 }
919 
920 /*
921 %!test
922 %! usleep (1000);
923 
924 %!error (usleep ())
925 %!error (usleep (1, 2))
926 */
927 
928 // FIXME: maybe this should only return 1 if IEEE floating
929 // point functions really work.
930 
931 DEFUN (isieee, , ,
932  "-*- texinfo -*-\n\
933 @deftypefn {Built-in Function} {} isieee ()\n\
934 Return true if your computer @emph{claims} to conform to the IEEE standard\n\
935 for floating point calculations.\n\
936 \n\
937 No actual tests are performed.\n\
938 @end deftypefn")
939 {
941 
944 }
945 
946 /*
947 %!assert (islogical (isieee ()))
948 */
949 
950 DEFUN (native_float_format, , ,
951  "-*- texinfo -*-\n\
952 @deftypefn {Built-in Function} {} native_float_format ()\n\
953 Return the native floating point format as a string.\n\
954 @end deftypefn")
955 {
957 
959 }
960 
961 /*
962 %!assert (ischar (native_float_format ()))
963 */
964 
965 DEFUN (tilde_expand, args, ,
966  "-*- texinfo -*-\n\
967 @deftypefn {Built-in Function} {} tilde_expand (@var{string})\n\
968 Perform tilde expansion on @var{string}.\n\
969 \n\
970 If @var{string} begins with a tilde character, (@samp{~}), all of the\n\
971 characters preceding the first slash (or all characters, if there is no\n\
972 slash) are treated as a possible user name, and the tilde and the following\n\
973 characters up to the slash are replaced by the home directory of the named\n\
974 user. If the tilde is followed immediately by a slash, the tilde is\n\
975 replaced by the home directory of the user running Octave.\n\
976 \n\
977 For example:\n\
978 \n\
979 @example\n\
980 @group\n\
981 tilde_expand (\"~joeuser/bin\")\n\
982  @result{} \"/home/joeuser/bin\"\n\
983 tilde_expand (\"~/bin\")\n\
984  @result{} \"/home/jwe/bin\"\n\
985 @end group\n\
986 @end example\n\
987 @end deftypefn")
988 {
989  octave_value retval;
990 
991  int nargin = args.length ();
992 
993  if (nargin == 1)
994  {
995  octave_value arg = args(0);
996 
997  string_vector sv = arg.all_strings ();
998 
999  if (! error_state)
1000  {
1001  sv = file_ops::tilde_expand (sv);
1002 
1003  if (arg.is_cellstr ())
1004  retval = Cell (arg.dims (), sv);
1005  else
1006  retval = sv;
1007  }
1008  else
1009  error ("tilde_expand: expecting argument to be char or cellstr object");
1010  }
1011  else
1012  print_usage ();
1013 
1014  return retval;
1015 }
1016 
1017 /*
1018 %!test
1019 %! home = get_home_directory ();
1020 %! assert (tilde_expand ("~/foobar"), [home "/foobar"]);
1021 %! assert (tilde_expand ("/foo/bar"), "/foo/bar");
1022 %! assert (tilde_expand ("foo/bar"), "foo/bar");
1023 */
1024 
1025 DEFUN (get_home_directory, , ,
1026  "-*- texinfo -*-\n\
1027 @deftypefn {Built-in Function} {@var{homedir} =} get_home_directory ()\n\
1028 Return the current home directory.\n\
1029 \n\
1030 On most systems, this is equivalent to @code{getenv (\"HOME\")}. On Windows\n\
1031 systems, if the environment variable @env{HOME} is not set then it is\n\
1032 equivalent to\n\
1033 @code{fullfile (getenv (\"HOMEDRIVE\"), getenv (\"HOMEPATH\"))}\n\
1034 @seealso{getenv}\n\
1035 @end deftypefn")
1036 {
1038 }
1039 
1040 /*
1041 %!test
1042 %! if (! ispc ())
1043 %! assert (get_home_directory (), getenv ("HOME"));
1044 %! endif
1045 */
1046 
1047 // This function really belongs in display.cc, but including defun.h in
1048 // that file results in conflicts with symbols from headers that are
1049 // needed for X11 and Carbon functions.
1050 
1051 DEFUN (have_window_system, , ,
1052  "-*- texinfo -*-\n\
1053 @deftypefn {Built-in Function} {} have_window_system ()\n\
1054 Return true if a window system is available (X11, Windows, or Apple OS X)\n\
1055 and false otherwise.\n\
1056 @seealso{isguirunning}\n\
1057 @end deftypefn")
1058 {
1060 }
ino_t ino(void) const
Definition: file-stat.h:107
void flush_octave_stdout(void)
Definition: pager.cc:458
Definition: Cell.h:35
octave_interrupt_handler octave_set_interrupt_handler(const volatile octave_interrupt_handler &h, bool restart_syscalls)
Definition: sighandlers.cc:603
FILE * octave_popen(const char *command, const char *mode)
Definition: sysdep.cc:482
static std::string float_format_as_string(float_format)
Definition: mach-info.cc:205
dev_t dev(void) const
Definition: file-stat.h:108
bool xisnan(double x)
Definition: lo-mappers.cc:144
void raw_mode(bool on, bool wait)
Definition: sysdep.cc:339
int octave_pclose(FILE *f)
Definition: sysdep.cc:502
OCTINTERP_API void print_usage(void)
Definition: defun.cc:51
static void putenv(const std::string &name, const std::string &value)
Definition: oct-env.cc:245
octave_idx_type length(void) const
Definition: oct-obj.h:89
void w32_set_quiet_shutdown(void)
int octave_kbhit(bool wait)
Definition: sysdep.cc:514
OCTINTERP_API octave_value_list Fdrawnow(const octave_value_list &=octave_value_list(), int=0)
Definition: graphics.cc:11181
#define DEFUN(name, args_name, nargout_name, doc)
Definition: defun.h:44
void error(const char *fmt,...)
Definition: error.cc:476
bool forced_interactive
Definition: input.cc:107
void octave_sleep(unsigned int seconds)
Definition: cutils.c:36
bool xisinf(double x)
Definition: lo-mappers.cc:160
void sysdep_cleanup(void)
Definition: sysdep.cc:321
string_vector all_strings(bool pad=false) const
Definition: ov.h:894
static std::string tilde_expand(const std::string &)
Definition: file-ops.cc:286
OCTINTERP_API octave_value_list Fsystem(const octave_value_list &=octave_value_list(), int=0)
Definition: toplev.cc:1004
#define DEFALIAS(alias, name)
Definition: defun.h:63
F77_RET_T const double const double * f
bool interactive
Definition: input.cc:103
#define MINGW_SIGNAL_CLEANUP()
Definition: sysdep.h:52
bool same_file_internal(const std::string &file1, const std::string &file2)
Definition: sysdep.cc:246
std::string string_value(bool force=false) const
Definition: ov.h:897
int error_state
Definition: error.cc:101
static std::string getenv(const std::string &name)
Definition: oct-env.cc:238
bool is_cellstr(void) const
Definition: ov.h:532
octave_idx_type length(void) const
Definition: ov.cc:1525
int NINT(double x)
Definition: lo-mappers.cc:657
dim_vector dims(void) const
Definition: ov.h:470
#define ECHO
octave_interrupt_handler octave_ignore_interrupts(void)
Definition: sighandlers.cc:583
double arg(double x)
Definition: lo-mappers.h:37
void warning(const char *fmt,...)
Definition: error.cc:681
static void clear_screen(bool skip_redisplay=false)
Definition: cmd-edit.cc:1140
octave_value_list ovl(const octave_value &a0)
Definition: oct-obj.h:178
static float_format native_float_format(void)
Definition: mach-info.cc:164
static bool display_available(void)
Definition: display.h:70
std::string get_P_tmpdir(void)
Definition: sysdep.cc:545
static bool prefer_env_winsize(bool)
Definition: cmd-edit.cc:1508
void octave_usleep(unsigned int useconds)
Definition: cutils.c:42
void sysdep_init(void)
Definition: sysdep.cc:309
return octave_value(v1.char_array_value().concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string())? '\'': '"'))
static std::string get_home_directory(void)
Definition: oct-env.cc:146