Mercurial > octave-nkf
annotate libinterp/interpfcn/sysdep.cc @ 16015:d910b9211cca
Plug memory leak in kbhit().
* libinterp/interpfcn/sysdep.cc(Fkbhit): Don't use new to get 2 bytes
of storage. Use a local variable instead.
author | Rik <rik@octave.org> |
---|---|
date | Thu, 07 Feb 2013 16:46:01 -0800 |
parents | 53d073233fa4 |
children | 119ce9f5e1a3 |
rev | line source |
---|---|
1 | 1 /* |
2 | |
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
13962
diff
changeset
|
3 Copyright (C) 1993-2012 John W. Eaton |
1 | 4 |
5 This file is part of Octave. | |
6 | |
7 Octave is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
7016 | 9 Free Software Foundation; either version 3 of the License, or (at your |
10 option) any later version. | |
1 | 11 |
12 Octave is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
19 <http://www.gnu.org/licenses/>. | |
1 | 20 |
21 */ | |
22 | |
240 | 23 #ifdef HAVE_CONFIG_H |
1192 | 24 #include <config.h> |
1 | 25 #endif |
26 | |
1346 | 27 #include <cfloat> |
1343 | 28 #include <cstddef> |
1346 | 29 #include <cstdio> |
1343 | 30 #include <cstdlib> |
31 #include <cstring> | |
32 | |
3503 | 33 #include <iostream> |
1728 | 34 #include <string> |
35 | |
529 | 36 #include <sys/types.h> |
37 #include <unistd.h> | |
1 | 38 |
1430 | 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> | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
45 #endif |
4067 | 46 |
47 #if defined (HAVE_CONIO_H) | |
48 #include <conio.h> | |
1430 | 49 #endif |
50 | |
3248 | 51 #if defined (HAVE_SYS_IOCTL_H) |
52 #include <sys/ioctl.h> | |
53 #endif | |
54 | |
1463 | 55 #if defined (HAVE_FLOATINGPOINT_H) |
56 #include <floatingpoint.h> | |
57 #endif | |
58 | |
2508 | 59 #if defined (HAVE_IEEEFP_H) |
60 #include <ieeefp.h> | |
61 #endif | |
62 | |
2926 | 63 #include "cmd-edit.h" |
64 #include "file-ops.h" | |
2893 | 65 #include "lo-mappers.h" |
7231 | 66 #include "lo-math.h" |
2317 | 67 #include "mach-info.h" |
2926 | 68 #include "oct-env.h" |
5451 | 69 #include "quit.h" |
1769 | 70 |
6208 | 71 #include "Cell.h" |
15423
53d073233fa4
call more built-in functions directly
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
72 #include "builtins.h" |
1352 | 73 #include "defun.h" |
74 #include "error.h" | |
529 | 75 #include "input.h" |
1755 | 76 #include "oct-obj.h" |
2370 | 77 #include "ov.h" |
3234 | 78 #include "pager.h" |
6419 | 79 #include "parse.h" |
5770 | 80 #include "sighandlers.h" |
1352 | 81 #include "sysdep.h" |
1755 | 82 #include "toplev.h" |
529 | 83 #include "utils.h" |
6598 | 84 #include "file-stat.h" |
529 | 85 |
86 #ifndef STDIN_FILENO | |
87 #define STDIN_FILENO 1 | |
88 #endif | |
444 | 89 |
9441
160c564d5d25
initialize floating point values properly for NetBSD systems
Aleksej Saushev <asau@inbox.ru>
parents:
9411
diff
changeset
|
90 #if defined (__386BSD__) || defined (__FreeBSD__) || defined (__NetBSD__) |
2508 | 91 static void |
92 BSD_init (void) | |
93 { | |
94 #if defined (HAVE_FLOATINGPOINT_H) | |
95 // Disable trapping on common exceptions. | |
4164 | 96 #ifndef FP_X_DNML |
97 #define FP_X_DNML 0 | |
98 #endif | |
2508 | 99 fpsetmask (~(FP_X_OFL|FP_X_INV|FP_X_DZ|FP_X_DNML|FP_X_UFL|FP_X_IMP)); |
100 #endif | |
101 } | |
102 #endif | |
103 | |
6956 | 104 #if defined (__WIN32__) && ! defined (_POSIX_VERSION) |
13771
80b30e186b73
[Win32] Use Toolhelp32 APi to find octinterp module path.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
13751
diff
changeset
|
105 |
80b30e186b73
[Win32] Use Toolhelp32 APi to find octinterp module path.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
13751
diff
changeset
|
106 #define WIN32_LEAN_AND_MEAN |
80b30e186b73
[Win32] Use Toolhelp32 APi to find octinterp module path.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
13751
diff
changeset
|
107 #include <tlhelp32.h> |
80b30e186b73
[Win32] Use Toolhelp32 APi to find octinterp module path.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
13751
diff
changeset
|
108 |
6080 | 109 static void |
110 w32_set_octave_home (void) | |
111 { | |
13771
80b30e186b73
[Win32] Use Toolhelp32 APi to find octinterp module path.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
13751
diff
changeset
|
112 std::string bin_dir; |
6080 | 113 |
13885
2f42a7b0cf94
Fix MinGW compilation problem with tlhelp32.h
Michael Goffioul <michael.goffioul@gmail.com>
parents:
13771
diff
changeset
|
114 HANDLE h = CreateToolhelp32Snapshot (TH32CS_SNAPMODULE |
2f42a7b0cf94
Fix MinGW compilation problem with tlhelp32.h
Michael Goffioul <michael.goffioul@gmail.com>
parents:
13771
diff
changeset
|
115 #ifdef TH32CS_SNAPMODULE32 |
2f42a7b0cf94
Fix MinGW compilation problem with tlhelp32.h
Michael Goffioul <michael.goffioul@gmail.com>
parents:
13771
diff
changeset
|
116 | TH32CS_SNAPMODULE32 |
2f42a7b0cf94
Fix MinGW compilation problem with tlhelp32.h
Michael Goffioul <michael.goffioul@gmail.com>
parents:
13771
diff
changeset
|
117 #endif |
2f42a7b0cf94
Fix MinGW compilation problem with tlhelp32.h
Michael Goffioul <michael.goffioul@gmail.com>
parents:
13771
diff
changeset
|
118 , 0); |
6080 | 119 |
13771
80b30e186b73
[Win32] Use Toolhelp32 APi to find octinterp module path.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
13751
diff
changeset
|
120 if (h != INVALID_HANDLE_VALUE) |
6080 | 121 { |
13771
80b30e186b73
[Win32] Use Toolhelp32 APi to find octinterp module path.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
13751
diff
changeset
|
122 MODULEENTRY32 mod_info; |
80b30e186b73
[Win32] Use Toolhelp32 APi to find octinterp module path.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
13751
diff
changeset
|
123 |
80b30e186b73
[Win32] Use Toolhelp32 APi to find octinterp module path.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
13751
diff
changeset
|
124 ZeroMemory (&mod_info, sizeof (mod_info)); |
80b30e186b73
[Win32] Use Toolhelp32 APi to find octinterp module path.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
13751
diff
changeset
|
125 mod_info.dwSize = sizeof (mod_info); |
13751
be7ff59cbc7a
Fix octinterp DLL searching on Win32.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
13282
diff
changeset
|
126 |
13771
80b30e186b73
[Win32] Use Toolhelp32 APi to find octinterp module path.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
13751
diff
changeset
|
127 if (Module32First (h, &mod_info)) |
14445 | 128 { |
129 do | |
130 { | |
131 std::string mod_name (mod_info.szModule); | |
6080 | 132 |
14445 | 133 if (mod_name.find ("octinterp") != std::string::npos) |
134 { | |
135 bin_dir = mod_info.szExePath; | |
136 if (bin_dir[bin_dir.length () - 1] != '\\') | |
137 bin_dir.append (1, '\\'); | |
138 break; | |
139 } | |
140 } | |
141 while (Module32Next (h, &mod_info)); | |
142 } | |
13771
80b30e186b73
[Win32] Use Toolhelp32 APi to find octinterp module path.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
13751
diff
changeset
|
143 |
80b30e186b73
[Win32] Use Toolhelp32 APi to find octinterp module path.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
13751
diff
changeset
|
144 CloseHandle (h); |
6080 | 145 } |
146 | |
147 if (! bin_dir.empty ()) | |
148 { | |
149 size_t pos = bin_dir.rfind ("\\bin\\"); | |
150 | |
8021 | 151 if (pos != std::string::npos) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
152 octave_env::putenv ("OCTAVE_HOME", bin_dir.substr (0, pos)); |
6080 | 153 } |
154 } | |
155 | |
6960 | 156 void |
5451 | 157 w32_set_quiet_shutdown (void) |
158 { | |
159 // Let the user close the console window or shutdown without the | |
160 // pesky dialog. | |
161 // | |
5775 | 162 // FIXME -- should this be user configurable? |
5451 | 163 SetProcessShutdownParameters (0x280, SHUTDOWN_NORETRY); |
164 } | |
165 | |
6960 | 166 void |
5451 | 167 MINGW_signal_cleanup (void) |
168 { | |
5455 | 169 w32_set_quiet_shutdown (); |
5451 | 170 |
5455 | 171 w32_raise_final (); |
5451 | 172 } |
173 #endif | |
174 | |
175 #if defined (__MINGW32__) | |
176 static void | |
177 MINGW_init (void) | |
178 { | |
6080 | 179 w32_set_octave_home (); |
180 | |
5451 | 181 // Init mutex to protect setjmp/longjmp and get main thread context |
182 w32_sigint_init (); | |
183 | |
184 w32_set_quiet_shutdown (); | |
185 } | |
186 #endif | |
187 | |
6080 | 188 #if defined (_MSC_VER) |
189 static void | |
190 MSVC_init (void) | |
191 { | |
192 w32_set_octave_home (); | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
193 |
6135 | 194 // Init mutex to protect setjmp/longjmp and get main thread context |
195 w32_sigint_init (); | |
196 | |
197 w32_set_quiet_shutdown (); | |
6080 | 198 } |
199 #endif | |
200 | |
4091 | 201 |
6598 | 202 // Return TRUE if FILE1 and FILE2 refer to the same (physical) file. |
203 | |
204 bool | |
205 same_file_internal (const std::string& file1, const std::string& file2) | |
206 { | |
207 #ifdef OCTAVE_USE_WINDOWS_API | |
208 | |
6691 | 209 bool retval = false; |
210 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
211 // Windows native code |
6598 | 212 // Reference: http://msdn2.microsoft.com/en-us/library/aa363788.aspx |
213 | |
6691 | 214 HANDLE hfile1 = CreateFile (file1.c_str (), 0, FILE_SHARE_READ, 0, |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
215 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); |
6691 | 216 |
217 if (hfile1 != INVALID_HANDLE_VALUE) | |
218 { | |
219 HANDLE hfile2 = CreateFile (file2.c_str (), 0, FILE_SHARE_READ, 0, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
220 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); |
6598 | 221 |
6691 | 222 if (hfile2 != INVALID_HANDLE_VALUE) |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
223 { |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
224 BY_HANDLE_FILE_INFORMATION hfi1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
225 BY_HANDLE_FILE_INFORMATION hfi2; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
226 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
227 if (GetFileInformationByHandle (hfile1, &hfi1) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
228 && GetFileInformationByHandle (hfile2, &hfi2)) |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
229 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
230 retval = (hfi1.dwVolumeSerialNumber == hfi2.dwVolumeSerialNumber |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
231 && hfi1.nFileIndexHigh == hfi2.nFileIndexHigh |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
232 && hfi1.nFileIndexLow == hfi2.nFileIndexLow); |
6598 | 233 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
234 CloseHandle (hfile2); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
235 } |
6691 | 236 |
6598 | 237 CloseHandle (hfile1); |
238 } | |
6691 | 239 |
240 return retval; | |
6598 | 241 |
242 #else | |
243 | |
244 // POSIX Code | |
245 | |
246 file_stat fs_file1 (file1); | |
247 file_stat fs_file2 (file2); | |
248 | |
249 return (fs_file1 && fs_file2 | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
250 && fs_file1.ino () == fs_file2.ino () |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
251 && fs_file1.dev () == fs_file2.dev ()); |
6598 | 252 |
253 #endif | |
254 } | |
255 | |
1 | 256 void |
257 sysdep_init (void) | |
258 { | |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14445
diff
changeset
|
259 #if defined (__386BSD__) || defined (__FreeBSD__) || defined (__NetBSD__) |
2508 | 260 BSD_init (); |
5451 | 261 #elif defined (__MINGW32__) |
262 MINGW_init (); | |
6080 | 263 #elif defined (_MSC_VER) |
264 MSVC_init (); | |
1 | 265 #endif |
266 } | |
267 | |
5451 | 268 void |
269 sysdep_cleanup (void) | |
270 { | |
271 MINGW_SIGNAL_CLEANUP (); | |
272 } | |
273 | |
767 | 274 // Set terminal in raw mode. From less-177. |
275 // | |
276 // Change terminal to "raw mode", or restore to "normal" mode. | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
277 // "Raw mode" means |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
278 // 1. An outstanding read will complete on receipt of a single keystroke. |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
279 // 2. Input is not echoed. |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
280 // 3. On output, \n is mapped to \r\n. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
281 // 4. \t is NOT expanded into spaces. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
282 // 5. Signal-causing characters such as ctrl-C (interrupt), |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
283 // etc. are NOT disabled. |
767 | 284 // It doesn't matter whether an input \n is mapped to \r, or vice versa. |
285 | |
529 | 286 void |
3657 | 287 raw_mode (bool on, bool wait) |
529 | 288 { |
3657 | 289 static bool curr_on = false; |
529 | 290 |
291 int tty_fd = STDIN_FILENO; | |
14160 | 292 if (! gnulib::isatty (tty_fd)) |
529 | 293 { |
294 if (interactive) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
295 error ("stdin is not a tty!"); |
529 | 296 return; |
297 } | |
298 | |
299 if (on == curr_on) | |
300 return; | |
301 | |
302 #if defined (HAVE_TERMIOS_H) | |
303 { | |
304 struct termios s; | |
305 static struct termios save_term; | |
306 | |
307 if (on) | |
308 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
309 // Get terminal modes. |
529 | 310 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
311 tcgetattr (tty_fd, &s); |
529 | 312 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
313 // Save modes and set certain variables dependent on modes. |
529 | 314 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
315 save_term = s; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
316 // ospeed = s.c_cflag & CBAUD; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
317 // erase_char = s.c_cc[VERASE]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
318 // kill_char = s.c_cc[VKILL]; |
529 | 319 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
320 // Set the modes to the way we want them. |
529 | 321 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
322 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
323 s.c_oflag |= (OPOST|ONLCR); |
529 | 324 #if defined (OCRNL) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
325 s.c_oflag &= ~(OCRNL); |
529 | 326 #endif |
327 #if defined (ONOCR) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
328 s.c_oflag &= ~(ONOCR); |
529 | 329 #endif |
330 #if defined (ONLRET) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
331 s.c_oflag &= ~(ONLRET); |
529 | 332 #endif |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
333 s.c_cc[VMIN] = wait ? 1 : 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
334 s.c_cc[VTIME] = 0; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
335 } |
529 | 336 else |
337 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
338 // Restore saved modes. |
1358 | 339 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
340 s = save_term; |
529 | 341 } |
3658 | 342 |
9411
c9636d98e5cd
fix kbhit(1) to not discard waiting keystrokes
Joe Rothweiler <octaveuser@sensicomm.com>
parents:
9242
diff
changeset
|
343 tcsetattr (tty_fd, wait ? TCSAFLUSH : TCSADRAIN, &s); |
529 | 344 } |
345 #elif defined (HAVE_TERMIO_H) | |
346 { | |
347 struct termio s; | |
348 static struct termio save_term; | |
349 | |
350 if (on) | |
351 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
352 // Get terminal modes. |
529 | 353 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
354 ioctl (tty_fd, TCGETA, &s); |
529 | 355 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
356 // Save modes and set certain variables dependent on modes. |
529 | 357 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
358 save_term = s; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
359 // ospeed = s.c_cflag & CBAUD; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
360 // erase_char = s.c_cc[VERASE]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
361 // kill_char = s.c_cc[VKILL]; |
529 | 362 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
363 // Set the modes to the way we want them. |
529 | 364 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
365 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
366 s.c_oflag |= (OPOST|ONLCR); |
529 | 367 #if defined (OCRNL) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
368 s.c_oflag &= ~(OCRNL); |
529 | 369 #endif |
370 #if defined (ONOCR) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
371 s.c_oflag &= ~(ONOCR); |
529 | 372 #endif |
373 #if defined (ONLRET) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
374 s.c_oflag &= ~(ONLRET); |
529 | 375 #endif |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
376 s.c_cc[VMIN] = wait ? 1 : 0; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
377 } |
529 | 378 else |
379 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
380 // Restore saved modes. |
1358 | 381 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
382 s = save_term; |
529 | 383 } |
3658 | 384 |
529 | 385 ioctl (tty_fd, TCSETAW, &s); |
386 } | |
387 #elif defined (HAVE_SGTTY_H) | |
388 { | |
389 struct sgttyb s; | |
390 static struct sgttyb save_term; | |
391 | |
392 if (on) | |
393 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
394 // Get terminal modes. |
529 | 395 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
396 ioctl (tty_fd, TIOCGETP, &s); |
529 | 397 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
398 // Save modes and set certain variables dependent on modes. |
529 | 399 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
400 save_term = s; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
401 // ospeed = s.sg_ospeed; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
402 // erase_char = s.sg_erase; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
403 // kill_char = s.sg_kill; |
529 | 404 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
405 // Set the modes to the way we want them. |
529 | 406 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
407 s.sg_flags |= CBREAK; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
408 s.sg_flags &= ~(ECHO); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
409 } |
529 | 410 else |
411 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
412 // Restore saved modes. |
1358 | 413 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
414 s = save_term; |
529 | 415 } |
3658 | 416 |
529 | 417 ioctl (tty_fd, TIOCSETN, &s); |
418 } | |
419 #else | |
4064 | 420 warning ("no support for raw mode console I/O on this system"); |
421 | |
422 // Make sure the current mode doesn't toggle. | |
423 on = curr_on; | |
529 | 424 #endif |
425 | |
426 curr_on = on; | |
427 } | |
428 | |
6726 | 429 FILE * |
430 octave_popen (const char *command, const char *mode) | |
431 { | |
432 #if defined (__MINGW32__) || defined (_MSC_VER) | |
433 if (mode && mode[0] && ! mode[1]) | |
434 { | |
435 char tmode[3]; | |
436 tmode[0] = mode[0]; | |
437 tmode[1] = 'b'; | |
438 tmode[2] = 0; | |
439 | |
440 return _popen (command, tmode); | |
441 } | |
442 else | |
443 return _popen (command, mode); | |
444 #else | |
445 return popen (command, mode); | |
446 #endif | |
447 } | |
448 | |
449 int | |
450 octave_pclose (FILE *f) | |
451 { | |
452 #if defined (__MINGW32__) || defined (_MSC_VER) | |
453 return _pclose (f); | |
454 #else | |
455 return pclose (f); | |
456 #endif | |
457 } | |
458 | |
767 | 459 // Read one character from the terminal. |
460 | |
529 | 461 int |
4067 | 462 octave_kbhit (bool wait) |
529 | 463 { |
4067 | 464 #ifdef HAVE__KBHIT |
4081 | 465 int c = (! wait && ! _kbhit ()) ? 0 : std::cin.get (); |
4067 | 466 #else |
3658 | 467 raw_mode (true, wait); |
468 | |
5770 | 469 // Get current handler. |
470 octave_interrupt_handler saved_interrupt_handler | |
471 = octave_ignore_interrupts (); | |
472 | |
473 // Restore it, disabling system call restarts (if possible) so the | |
474 // read can be interrupted. | |
475 | |
476 octave_set_interrupt_handler (saved_interrupt_handler, false); | |
477 | |
3658 | 478 int c = std::cin.get (); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
479 |
3658 | 480 if (std::cin.fail () || std::cin.eof ()) |
481 std::cin.clear (); | |
482 | |
5770 | 483 // Restore it, enabling system call restarts (if possible). |
484 octave_set_interrupt_handler (saved_interrupt_handler, true); | |
485 | |
3658 | 486 raw_mode (false, true); |
4067 | 487 #endif |
3658 | 488 |
529 | 489 return c; |
490 } | |
491 | |
12228
0f70c5db58c3
try to get better value than \ for P_tmpdir on Windows systems
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
492 std::string |
0f70c5db58c3
try to get better value than \ for P_tmpdir on Windows systems
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
493 get_P_tmpdir (void) |
0f70c5db58c3
try to get better value than \ for P_tmpdir on Windows systems
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
494 { |
0f70c5db58c3
try to get better value than \ for P_tmpdir on Windows systems
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
495 #if defined (__WIN32__) && ! defined (_POSIX_VERSION) |
0f70c5db58c3
try to get better value than \ for P_tmpdir on Windows systems
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
496 |
0f70c5db58c3
try to get better value than \ for P_tmpdir on Windows systems
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
497 std::string retval; |
0f70c5db58c3
try to get better value than \ for P_tmpdir on Windows systems
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
498 |
0f70c5db58c3
try to get better value than \ for P_tmpdir on Windows systems
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
499 #if defined (P_tmpdir) |
0f70c5db58c3
try to get better value than \ for P_tmpdir on Windows systems
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
500 retval = P_tmpdir; |
0f70c5db58c3
try to get better value than \ for P_tmpdir on Windows systems
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
501 #endif |
0f70c5db58c3
try to get better value than \ for P_tmpdir on Windows systems
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
502 |
0f70c5db58c3
try to get better value than \ for P_tmpdir on Windows systems
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
503 // Apparently some versions of MinGW and MSVC either don't define |
0f70c5db58c3
try to get better value than \ for P_tmpdir on Windows systems
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
504 // P_tmpdir, or they define it to a single backslash, neither of which |
0f70c5db58c3
try to get better value than \ for P_tmpdir on Windows systems
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
505 // is particularly helpful. |
0f70c5db58c3
try to get better value than \ for P_tmpdir on Windows systems
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
506 |
0f70c5db58c3
try to get better value than \ for P_tmpdir on Windows systems
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
507 if (retval.empty () || retval == "\\") |
0f70c5db58c3
try to get better value than \ for P_tmpdir on Windows systems
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
508 { |
0f70c5db58c3
try to get better value than \ for P_tmpdir on Windows systems
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
509 retval = octave_env::getenv ("TEMP"); |
0f70c5db58c3
try to get better value than \ for P_tmpdir on Windows systems
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
510 |
0f70c5db58c3
try to get better value than \ for P_tmpdir on Windows systems
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
511 if (retval.empty ()) |
0f70c5db58c3
try to get better value than \ for P_tmpdir on Windows systems
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
512 retval = octave_env::getenv ("TMP"); |
0f70c5db58c3
try to get better value than \ for P_tmpdir on Windows systems
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
513 |
0f70c5db58c3
try to get better value than \ for P_tmpdir on Windows systems
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
514 if (retval.empty ()) |
0f70c5db58c3
try to get better value than \ for P_tmpdir on Windows systems
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
515 retval = "c:\\temp"; |
0f70c5db58c3
try to get better value than \ for P_tmpdir on Windows systems
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
516 } |
0f70c5db58c3
try to get better value than \ for P_tmpdir on Windows systems
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
517 |
0f70c5db58c3
try to get better value than \ for P_tmpdir on Windows systems
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
518 return retval; |
0f70c5db58c3
try to get better value than \ for P_tmpdir on Windows systems
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
519 |
0f70c5db58c3
try to get better value than \ for P_tmpdir on Windows systems
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
520 #elif defined (P_tmpdir) |
0f70c5db58c3
try to get better value than \ for P_tmpdir on Windows systems
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
521 |
0f70c5db58c3
try to get better value than \ for P_tmpdir on Windows systems
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
522 return P_tmpdir; |
0f70c5db58c3
try to get better value than \ for P_tmpdir on Windows systems
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
523 |
0f70c5db58c3
try to get better value than \ for P_tmpdir on Windows systems
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
524 #else |
0f70c5db58c3
try to get better value than \ for P_tmpdir on Windows systems
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
525 |
0f70c5db58c3
try to get better value than \ for P_tmpdir on Windows systems
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
526 return "/tmp"; |
0f70c5db58c3
try to get better value than \ for P_tmpdir on Windows systems
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
527 |
0f70c5db58c3
try to get better value than \ for P_tmpdir on Windows systems
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
528 #endif |
0f70c5db58c3
try to get better value than \ for P_tmpdir on Windows systems
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
529 } |
0f70c5db58c3
try to get better value than \ for P_tmpdir on Windows systems
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
530 |
1957 | 531 DEFUN (clc, , , |
3332 | 532 "-*- texinfo -*-\n\ |
10840 | 533 @deftypefn {Built-in Function} {} clc ()\n\ |
3332 | 534 @deftypefnx {Built-in Function} {} home ()\n\ |
535 Clear the terminal screen and move the cursor to the upper left corner.\n\ | |
3333 | 536 @end deftypefn") |
529 | 537 { |
2926 | 538 command_editor::clear_screen (); |
529 | 539 |
2926 | 540 return octave_value_list (); |
529 | 541 } |
542 | |
549 | 543 DEFALIAS (home, clc); |
544 | |
1957 | 545 DEFUN (getenv, args, , |
3301 | 546 "-*- texinfo -*-\n\ |
547 @deftypefn {Built-in Function} {} getenv (@var{var})\n\ | |
548 Return the value of the environment variable @var{var}. For example,\n\ | |
549 \n\ | |
550 @example\n\ | |
551 getenv (\"PATH\")\n\ | |
552 @end example\n\ | |
553 \n\ | |
554 @noindent\n\ | |
555 returns a string containing the value of your path.\n\ | |
556 @end deftypefn") | |
529 | 557 { |
4233 | 558 octave_value retval; |
529 | 559 |
560 int nargin = args.length (); | |
561 | |
712 | 562 if (nargin == 1) |
529 | 563 { |
3523 | 564 std::string name = args(0).string_value (); |
636 | 565 |
566 if (! error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
567 retval = octave_env::getenv (name); |
529 | 568 } |
569 else | |
5823 | 570 print_usage (); |
529 | 571 |
572 return retval; | |
573 } | |
574 | |
1957 | 575 DEFUN (putenv, args, , |
3301 | 576 "-*- texinfo -*-\n\ |
10840 | 577 @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
|
578 @deftypefnx {Built-in Function} {} setenv (@var{var}, @var{value})\n\ |
3301 | 579 Set the value of the environment variable @var{var} to @var{value}.\n\ |
580 @end deftypefn") | |
1706 | 581 { |
2086 | 582 octave_value_list retval; |
1706 | 583 |
584 int nargin = args.length (); | |
585 | |
7759
84a7f00586aa
Alias setenv to putenv. Allow single arg to putenv
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
586 if (nargin == 2 || nargin == 1) |
1706 | 587 { |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
588 std::string var = args(0).string_value (); |
1706 | 589 |
590 if (! error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
591 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
592 std::string val = (nargin == 2 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
593 ? args(1).string_value () : std::string ()); |
1706 | 594 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
595 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
596 octave_env::putenv (var, val); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
597 else |
12483
7a5aacf65f81
Rewrite error strings in src/ to use variables named in documentation.
Rik <octave@nomad.inbox5.com>
parents:
12228
diff
changeset
|
598 error ("putenv: VALUE must be a string"); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
599 } |
1706 | 600 else |
12483
7a5aacf65f81
Rewrite error strings in src/ to use variables named in documentation.
Rik <octave@nomad.inbox5.com>
parents:
12228
diff
changeset
|
601 error ("putenv: VAR must be a string"); |
1706 | 602 } |
603 else | |
5823 | 604 print_usage (); |
1706 | 605 |
606 return retval; | |
607 } | |
12825
a1dcb854a4f9
codesprint: new tests for sysdep.cc functions
John W. Eaton <jwe@octave.org>
parents:
12483
diff
changeset
|
608 |
7759
84a7f00586aa
Alias setenv to putenv. Allow single arg to putenv
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
609 DEFALIAS (setenv, putenv); |
1706 | 610 |
12825
a1dcb854a4f9
codesprint: new tests for sysdep.cc functions
John W. Eaton <jwe@octave.org>
parents:
12483
diff
changeset
|
611 /* |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14160
diff
changeset
|
612 %!assert (ischar (getenv ("OCTAVE_HOME"))) |
12825
a1dcb854a4f9
codesprint: new tests for sysdep.cc functions
John W. Eaton <jwe@octave.org>
parents:
12483
diff
changeset
|
613 %!test |
a1dcb854a4f9
codesprint: new tests for sysdep.cc functions
John W. Eaton <jwe@octave.org>
parents:
12483
diff
changeset
|
614 %! setenv ("dummy_variable_that_cannot_matter", "foobar"); |
a1dcb854a4f9
codesprint: new tests for sysdep.cc functions
John W. Eaton <jwe@octave.org>
parents:
12483
diff
changeset
|
615 %! assert (getenv ("dummy_variable_that_cannot_matter"), "foobar"); |
a1dcb854a4f9
codesprint: new tests for sysdep.cc functions
John W. Eaton <jwe@octave.org>
parents:
12483
diff
changeset
|
616 */ |
a1dcb854a4f9
codesprint: new tests for sysdep.cc functions
John W. Eaton <jwe@octave.org>
parents:
12483
diff
changeset
|
617 |
5775 | 618 // FIXME -- perhaps kbhit should also be able to print a prompt? |
3372 | 619 |
3657 | 620 DEFUN (kbhit, args, , |
3372 | 621 "-*- texinfo -*-\n\ |
622 @deftypefn {Built-in Function} {} kbhit ()\n\ | |
9039
51dc9691f23f
Cleanup documentation files errors.texi, debug.texi, io.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
623 Read a single keystroke from the keyboard. If called with one\n\ |
3657 | 624 argument, don't wait for a keypress. For example,\n\ |
3372 | 625 \n\ |
626 @example\n\ | |
627 x = kbhit ();\n\ | |
628 @end example\n\ | |
629 \n\ | |
630 @noindent\n\ | |
631 will set @var{x} to the next character typed at the keyboard as soon as\n\ | |
632 it is typed.\n\ | |
3657 | 633 \n\ |
634 @example\n\ | |
635 x = kbhit (1);\n\ | |
636 @end example\n\ | |
637 \n\ | |
638 @noindent\n\ | |
639 identical to the above example, but don't wait for a keypress,\n\ | |
640 returning the empty string if no key is available.\n\ | |
3372 | 641 @end deftypefn") |
529 | 642 { |
4233 | 643 octave_value retval; |
529 | 644 |
5775 | 645 // FIXME -- add timeout and default value args? |
529 | 646 |
3676 | 647 if (interactive || forced_interactive) |
529 | 648 { |
15423
53d073233fa4
call more built-in functions directly
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
649 Fdrawnow (); |
6423 | 650 |
4067 | 651 int c = octave_kbhit (args.length () == 0); |
3657 | 652 |
3658 | 653 if (c == -1) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
654 c = 0; |
3657 | 655 |
16015 | 656 char s[2] = {c, '\0'}; |
657 | |
529 | 658 retval = s; |
659 } | |
660 | |
661 return retval; | |
662 } | |
663 | |
1957 | 664 DEFUN (pause, args, , |
3301 | 665 "-*- texinfo -*-\n\ |
666 @deftypefn {Built-in Function} {} pause (@var{seconds})\n\ | |
667 Suspend the execution of the program. If invoked without any arguments,\n\ | |
668 Octave waits until you type a character. With a numeric argument, it\n\ | |
669 pauses for the given number of seconds. For example, the following\n\ | |
670 statement prints a message and then waits 5 seconds before clearing the\n\ | |
671 screen.\n\ | |
672 \n\ | |
673 @example\n\ | |
674 @group\n\ | |
6848 | 675 fprintf (stderr, \"wait please...\\n\");\n\ |
3301 | 676 pause (5);\n\ |
677 clc;\n\ | |
678 @end group\n\ | |
679 @end example\n\ | |
680 @end deftypefn") | |
529 | 681 { |
2086 | 682 octave_value_list retval; |
529 | 683 |
684 int nargin = args.length (); | |
685 | |
712 | 686 if (! (nargin == 0 || nargin == 1)) |
529 | 687 { |
5823 | 688 print_usage (); |
529 | 689 return retval; |
690 } | |
691 | |
1579 | 692 if (nargin == 1) |
529 | 693 { |
1579 | 694 double dval = args(0).double_value (); |
636 | 695 |
1579 | 696 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
697 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
698 if (! xisnan (dval)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
699 { |
15423
53d073233fa4
call more built-in functions directly
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
700 Fdrawnow (); |
6419 | 701 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
702 if (xisinf (dval)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
703 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
704 flush_octave_stdout (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
705 octave_kbhit (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
706 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
707 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
708 octave_sleep (dval); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
709 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
710 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
711 warning ("pause: NaN is an invalid delay"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
712 } |
529 | 713 } |
1579 | 714 else |
3234 | 715 { |
15423
53d073233fa4
call more built-in functions directly
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
716 Fdrawnow (); |
3234 | 717 flush_octave_stdout (); |
4067 | 718 octave_kbhit (); |
3234 | 719 } |
2630 | 720 |
721 return retval; | |
722 } | |
723 | |
12825
a1dcb854a4f9
codesprint: new tests for sysdep.cc functions
John W. Eaton <jwe@octave.org>
parents:
12483
diff
changeset
|
724 /* |
a1dcb854a4f9
codesprint: new tests for sysdep.cc functions
John W. Eaton <jwe@octave.org>
parents:
12483
diff
changeset
|
725 %!test |
a1dcb854a4f9
codesprint: new tests for sysdep.cc functions
John W. Eaton <jwe@octave.org>
parents:
12483
diff
changeset
|
726 %! pause (1); |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14160
diff
changeset
|
727 |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14160
diff
changeset
|
728 %!error (pause (1, 2)) |
12825
a1dcb854a4f9
codesprint: new tests for sysdep.cc functions
John W. Eaton <jwe@octave.org>
parents:
12483
diff
changeset
|
729 */ |
a1dcb854a4f9
codesprint: new tests for sysdep.cc functions
John W. Eaton <jwe@octave.org>
parents:
12483
diff
changeset
|
730 |
2630 | 731 DEFUN (sleep, args, , |
3301 | 732 "-*- texinfo -*-\n\ |
733 @deftypefn {Built-in Function} {} sleep (@var{seconds})\n\ | |
734 Suspend the execution of the program for the given number of seconds.\n\ | |
735 @end deftypefn") | |
2630 | 736 { |
737 octave_value_list retval; | |
738 | |
739 if (args.length () == 1) | |
1579 | 740 { |
2630 | 741 double dval = args(0).double_value (); |
742 | |
743 if (! error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
744 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
745 if (xisnan (dval)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
746 warning ("sleep: NaN is an invalid delay"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
747 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
748 { |
15423
53d073233fa4
call more built-in functions directly
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
749 Fdrawnow (); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
750 octave_sleep (dval); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
751 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
752 } |
1579 | 753 } |
2630 | 754 else |
5823 | 755 print_usage (); |
2630 | 756 |
757 return retval; | |
758 } | |
759 | |
12825
a1dcb854a4f9
codesprint: new tests for sysdep.cc functions
John W. Eaton <jwe@octave.org>
parents:
12483
diff
changeset
|
760 /* |
a1dcb854a4f9
codesprint: new tests for sysdep.cc functions
John W. Eaton <jwe@octave.org>
parents:
12483
diff
changeset
|
761 %!test |
a1dcb854a4f9
codesprint: new tests for sysdep.cc functions
John W. Eaton <jwe@octave.org>
parents:
12483
diff
changeset
|
762 %! sleep (1); |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14160
diff
changeset
|
763 |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14160
diff
changeset
|
764 %!error (sleep ()) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14160
diff
changeset
|
765 %!error (sleep (1, 2)) |
12825
a1dcb854a4f9
codesprint: new tests for sysdep.cc functions
John W. Eaton <jwe@octave.org>
parents:
12483
diff
changeset
|
766 */ |
a1dcb854a4f9
codesprint: new tests for sysdep.cc functions
John W. Eaton <jwe@octave.org>
parents:
12483
diff
changeset
|
767 |
2630 | 768 DEFUN (usleep, args, , |
3301 | 769 "-*- texinfo -*-\n\ |
770 @deftypefn {Built-in Function} {} usleep (@var{microseconds})\n\ | |
771 Suspend the execution of the program for the given number of\n\ | |
772 microseconds. On systems where it is not possible to sleep for periods\n\ | |
773 of time less than one second, @code{usleep} will pause the execution for\n\ | |
774 @code{round (@var{microseconds} / 1e6)} seconds.\n\ | |
775 @end deftypefn") | |
2630 | 776 { |
777 octave_value_list retval; | |
778 | |
779 if (args.length () == 1) | |
780 { | |
781 double dval = args(0).double_value (); | |
782 | |
783 if (! error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
784 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
785 if (xisnan (dval)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
786 warning ("usleep: NaN is an invalid delay"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
787 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
788 { |
15423
53d073233fa4
call more built-in functions directly
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
789 Fdrawnow (); |
6423 | 790 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
791 int delay = NINT (dval); |
2631 | 792 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
793 if (delay > 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
794 octave_usleep (delay); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
795 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
796 } |
2630 | 797 } |
798 else | |
5823 | 799 print_usage (); |
529 | 800 |
801 return retval; | |
802 } | |
803 | |
12825
a1dcb854a4f9
codesprint: new tests for sysdep.cc functions
John W. Eaton <jwe@octave.org>
parents:
12483
diff
changeset
|
804 /* |
a1dcb854a4f9
codesprint: new tests for sysdep.cc functions
John W. Eaton <jwe@octave.org>
parents:
12483
diff
changeset
|
805 %!test |
a1dcb854a4f9
codesprint: new tests for sysdep.cc functions
John W. Eaton <jwe@octave.org>
parents:
12483
diff
changeset
|
806 %! usleep (1000); |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14160
diff
changeset
|
807 |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14160
diff
changeset
|
808 %!error (usleep ()) |
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14160
diff
changeset
|
809 %!error (usleep (1, 2)) |
12825
a1dcb854a4f9
codesprint: new tests for sysdep.cc functions
John W. Eaton <jwe@octave.org>
parents:
12483
diff
changeset
|
810 */ |
a1dcb854a4f9
codesprint: new tests for sysdep.cc functions
John W. Eaton <jwe@octave.org>
parents:
12483
diff
changeset
|
811 |
5775 | 812 // FIXME -- maybe this should only return 1 if IEEE floating |
862 | 813 // point functions really work. |
814 | |
1957 | 815 DEFUN (isieee, , , |
3301 | 816 "-*- texinfo -*-\n\ |
817 @deftypefn {Built-in Function} {} isieee ()\n\ | |
11431
0d9640d755b1
Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents:
11234
diff
changeset
|
818 Return true if your computer @emph{claims} to conform to the IEEE standard\n\ |
0d9640d755b1
Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents:
11234
diff
changeset
|
819 for floating point calculations. No actual tests are performed.\n\ |
3301 | 820 @end deftypefn") |
862 | 821 { |
4600 | 822 oct_mach_info::float_format flt_fmt = oct_mach_info::native_float_format (); |
2317 | 823 |
4574 | 824 return octave_value (flt_fmt == oct_mach_info::flt_fmt_ieee_little_endian |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
825 || flt_fmt == oct_mach_info::flt_fmt_ieee_big_endian); |
862 | 826 } |
827 | |
12825
a1dcb854a4f9
codesprint: new tests for sysdep.cc functions
John W. Eaton <jwe@octave.org>
parents:
12483
diff
changeset
|
828 /* |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14160
diff
changeset
|
829 %!assert (islogical (isieee ())) |
12825
a1dcb854a4f9
codesprint: new tests for sysdep.cc functions
John W. Eaton <jwe@octave.org>
parents:
12483
diff
changeset
|
830 */ |
a1dcb854a4f9
codesprint: new tests for sysdep.cc functions
John W. Eaton <jwe@octave.org>
parents:
12483
diff
changeset
|
831 |
4600 | 832 DEFUN (native_float_format, , , |
833 "-*- texinfo -*-\n\ | |
834 @deftypefn {Built-in Function} {} native_float_format ()\n\ | |
835 Return the native floating point format as a string\n\ | |
836 @end deftypefn") | |
837 { | |
838 oct_mach_info::float_format flt_fmt = oct_mach_info::native_float_format (); | |
839 | |
840 return octave_value (oct_mach_info::float_format_as_string (flt_fmt)); | |
841 } | |
842 | |
12825
a1dcb854a4f9
codesprint: new tests for sysdep.cc functions
John W. Eaton <jwe@octave.org>
parents:
12483
diff
changeset
|
843 /* |
14429
eff4a5933e28
Update %!tests in src/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14160
diff
changeset
|
844 %!assert (ischar (native_float_format ())) |
12825
a1dcb854a4f9
codesprint: new tests for sysdep.cc functions
John W. Eaton <jwe@octave.org>
parents:
12483
diff
changeset
|
845 */ |
a1dcb854a4f9
codesprint: new tests for sysdep.cc functions
John W. Eaton <jwe@octave.org>
parents:
12483
diff
changeset
|
846 |
1957 | 847 DEFUN (tilde_expand, args, , |
3301 | 848 "-*- texinfo -*-\n\ |
849 @deftypefn {Built-in Function} {} tilde_expand (@var{string})\n\ | |
11572
7d6d8c1e471f
Grammarcheck Texinfo for files in src directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
850 Perform tilde expansion on @var{string}. If @var{string} begins with a\n\ |
3301 | 851 tilde character, (@samp{~}), all of the characters preceding the first\n\ |
852 slash (or all characters, if there is no slash) are treated as a\n\ | |
853 possible user name, and the tilde and the following characters up to the\n\ | |
854 slash are replaced by the home directory of the named user. If the\n\ | |
855 tilde is followed immediately by a slash, the tilde is replaced by the\n\ | |
10840 | 856 home directory of the user running Octave. For example:\n\ |
3301 | 857 \n\ |
858 @example\n\ | |
859 @group\n\ | |
860 tilde_expand (\"~joeuser/bin\")\n\ | |
861 @result{} \"/home/joeuser/bin\"\n\ | |
862 tilde_expand (\"~/bin\")\n\ | |
863 @result{} \"/home/jwe/bin\"\n\ | |
864 @end group\n\ | |
865 @end example\n\ | |
866 @end deftypefn") | |
1750 | 867 { |
4233 | 868 octave_value retval; |
1750 | 869 |
870 int nargin = args.length (); | |
871 | |
872 if (nargin == 1) | |
6116 | 873 { |
874 octave_value arg = args(0); | |
875 | |
876 string_vector sv = arg.all_strings (); | |
877 | |
878 if (! error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
879 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
880 sv = file_ops::tilde_expand (sv); |
6116 | 881 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
882 if (arg.is_cellstr ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
883 retval = Cell (arg.dims (), sv); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
884 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
885 retval = sv; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
886 } |
6116 | 887 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
888 error ("tilde_expand: expecting argument to be char or cellstr object"); |
6116 | 889 } |
1750 | 890 else |
5823 | 891 print_usage (); |
1750 | 892 |
893 return retval; | |
894 } | |
12825
a1dcb854a4f9
codesprint: new tests for sysdep.cc functions
John W. Eaton <jwe@octave.org>
parents:
12483
diff
changeset
|
895 |
a1dcb854a4f9
codesprint: new tests for sysdep.cc functions
John W. Eaton <jwe@octave.org>
parents:
12483
diff
changeset
|
896 /* |
a1dcb854a4f9
codesprint: new tests for sysdep.cc functions
John W. Eaton <jwe@octave.org>
parents:
12483
diff
changeset
|
897 %!test |
a1dcb854a4f9
codesprint: new tests for sysdep.cc functions
John W. Eaton <jwe@octave.org>
parents:
12483
diff
changeset
|
898 %! if (isempty (getenv ("HOME"))) |
a1dcb854a4f9
codesprint: new tests for sysdep.cc functions
John W. Eaton <jwe@octave.org>
parents:
12483
diff
changeset
|
899 %! setenv ("HOME", "foobar"); |
a1dcb854a4f9
codesprint: new tests for sysdep.cc functions
John W. Eaton <jwe@octave.org>
parents:
12483
diff
changeset
|
900 %! endif |
a1dcb854a4f9
codesprint: new tests for sysdep.cc functions
John W. Eaton <jwe@octave.org>
parents:
12483
diff
changeset
|
901 %! home = getenv ("HOME"); |
13962
efa658122cc9
Fix tilde_expand %!test bug on MinGW (Bug #33862)
Rik <octave@nomad.inbox5.com>
parents:
13885
diff
changeset
|
902 %! assert (tilde_expand ("~/foobar"), strcat (home, "/foobar")); |
12825
a1dcb854a4f9
codesprint: new tests for sysdep.cc functions
John W. Eaton <jwe@octave.org>
parents:
12483
diff
changeset
|
903 %! assert (tilde_expand ("/foo/bar"), "/foo/bar"); |
a1dcb854a4f9
codesprint: new tests for sysdep.cc functions
John W. Eaton <jwe@octave.org>
parents:
12483
diff
changeset
|
904 %! assert (tilde_expand ("foo/bar"), "foo/bar"); |
a1dcb854a4f9
codesprint: new tests for sysdep.cc functions
John W. Eaton <jwe@octave.org>
parents:
12483
diff
changeset
|
905 */ |