Mercurial > hg > octave-nkf
annotate src/sysdep.cc @ 11273:bd2643f0ce57 ss-3-3-54
snapshot 3.3.54
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 19 Nov 2010 16:05:58 -0500 |
parents | 2718e1fdf82f |
children | 0d9640d755b1 |
rev | line source |
---|---|
1 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1999, 2000, 2002, 2003, |
8920 | 4 2005, 2006, 2007, 2008, 2009 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 | |
529 | 37 #include <sys/types.h> |
38 #include <unistd.h> | |
1 | 39 |
1430 | 40 #if defined (HAVE_TERMIOS_H) |
41 #include <termios.h> | |
42 #elif defined (HAVE_TERMIO_H) | |
43 #include <termio.h> | |
44 #elif defined (HAVE_SGTTY_H) | |
45 #include <sgtty.h> | |
4067 | 46 #endif |
47 | |
48 #if defined (HAVE_CONIO_H) | |
49 #include <conio.h> | |
1430 | 50 #endif |
51 | |
3248 | 52 #if defined (HAVE_SYS_IOCTL_H) |
53 #include <sys/ioctl.h> | |
54 #endif | |
55 | |
1463 | 56 #if defined (HAVE_FLOATINGPOINT_H) |
57 #include <floatingpoint.h> | |
58 #endif | |
59 | |
2508 | 60 #if defined (HAVE_IEEEFP_H) |
61 #include <ieeefp.h> | |
62 #endif | |
63 | |
2926 | 64 #include "cmd-edit.h" |
65 #include "file-ops.h" | |
2893 | 66 #include "lo-mappers.h" |
7231 | 67 #include "lo-math.h" |
2317 | 68 #include "mach-info.h" |
2926 | 69 #include "oct-env.h" |
5451 | 70 #include "quit.h" |
1769 | 71 |
6208 | 72 #include "Cell.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) |
6080 | 105 static void |
106 w32_set_octave_home (void) | |
107 { | |
108 int n = 1024; | |
109 | |
6086 | 110 std::string bin_dir (n, '\0'); |
6080 | 111 |
112 while (true) | |
113 { | |
6954 | 114 HMODULE hMod = GetModuleHandle ("octinterp"); |
115 int status = GetModuleFileName (hMod, &bin_dir[0], n); | |
6080 | 116 |
117 if (status < n) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
118 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
119 bin_dir.resize (status); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
120 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
121 } |
6080 | 122 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
123 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
124 n *= 2; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
125 bin_dir.resize (n); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
126 } |
6080 | 127 } |
128 | |
129 if (! bin_dir.empty ()) | |
130 { | |
131 size_t pos = bin_dir.rfind ("\\bin\\"); | |
132 | |
8021 | 133 if (pos != std::string::npos) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
134 octave_env::putenv ("OCTAVE_HOME", bin_dir.substr (0, pos)); |
6080 | 135 } |
136 } | |
137 | |
6960 | 138 void |
5451 | 139 w32_set_quiet_shutdown (void) |
140 { | |
141 // Let the user close the console window or shutdown without the | |
142 // pesky dialog. | |
143 // | |
5775 | 144 // FIXME -- should this be user configurable? |
5451 | 145 SetProcessShutdownParameters (0x280, SHUTDOWN_NORETRY); |
146 } | |
147 | |
6960 | 148 void |
5451 | 149 MINGW_signal_cleanup (void) |
150 { | |
5455 | 151 w32_set_quiet_shutdown (); |
5451 | 152 |
5455 | 153 w32_raise_final (); |
5451 | 154 } |
155 #endif | |
156 | |
157 #if defined (__MINGW32__) | |
158 static void | |
159 MINGW_init (void) | |
160 { | |
6080 | 161 w32_set_octave_home (); |
162 | |
5451 | 163 // Init mutex to protect setjmp/longjmp and get main thread context |
164 w32_sigint_init (); | |
165 | |
166 w32_set_quiet_shutdown (); | |
167 } | |
168 #endif | |
169 | |
6080 | 170 #if defined (_MSC_VER) |
171 static void | |
172 MSVC_init (void) | |
173 { | |
174 w32_set_octave_home (); | |
6135 | 175 |
176 // Init mutex to protect setjmp/longjmp and get main thread context | |
177 w32_sigint_init (); | |
178 | |
179 w32_set_quiet_shutdown (); | |
6080 | 180 } |
181 #endif | |
182 | |
4091 | 183 |
6598 | 184 // Return TRUE if FILE1 and FILE2 refer to the same (physical) file. |
185 | |
186 bool | |
187 same_file_internal (const std::string& file1, const std::string& file2) | |
188 { | |
189 #ifdef OCTAVE_USE_WINDOWS_API | |
190 | |
6691 | 191 bool retval = false; |
192 | |
6598 | 193 // Windows native code |
194 // Reference: http://msdn2.microsoft.com/en-us/library/aa363788.aspx | |
195 | |
6691 | 196 HANDLE hfile1 = CreateFile (file1.c_str (), 0, FILE_SHARE_READ, 0, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
197 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); |
6691 | 198 |
199 if (hfile1 != INVALID_HANDLE_VALUE) | |
200 { | |
201 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
|
202 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); |
6598 | 203 |
6691 | 204 if (hfile2 != INVALID_HANDLE_VALUE) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
205 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
206 BY_HANDLE_FILE_INFORMATION hfi1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
207 BY_HANDLE_FILE_INFORMATION hfi2; |
6691 | 208 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
209 if (GetFileInformationByHandle (hfile1, &hfi1) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
210 && GetFileInformationByHandle (hfile2, &hfi2)) |
6598 | 211 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
212 retval = (hfi1.dwVolumeSerialNumber == hfi2.dwVolumeSerialNumber |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
213 && hfi1.nFileIndexHigh == hfi2.nFileIndexHigh |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
214 && hfi1.nFileIndexLow == hfi2.nFileIndexLow); |
6598 | 215 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
216 CloseHandle (hfile2); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
217 } |
6691 | 218 |
6598 | 219 CloseHandle (hfile1); |
220 } | |
6691 | 221 |
222 return retval; | |
6598 | 223 |
224 #else | |
225 | |
226 // POSIX Code | |
227 | |
228 file_stat fs_file1 (file1); | |
229 file_stat fs_file2 (file2); | |
230 | |
231 return (fs_file1 && fs_file2 | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
232 && fs_file1.ino () == fs_file2.ino () |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
233 && fs_file1.dev () == fs_file2.dev ()); |
6598 | 234 |
235 #endif | |
236 } | |
237 | |
1 | 238 void |
239 sysdep_init (void) | |
240 { | |
9441
160c564d5d25
initialize floating point values properly for NetBSD systems
Aleksej Saushev <asau@inbox.ru>
parents:
9411
diff
changeset
|
241 #if defined (__386BSD__) || defined (__FreeBSD__) || defined(__NetBSD__) |
2508 | 242 BSD_init (); |
5451 | 243 #elif defined (__MINGW32__) |
244 MINGW_init (); | |
6080 | 245 #elif defined (_MSC_VER) |
246 MSVC_init (); | |
1 | 247 #endif |
248 } | |
249 | |
5451 | 250 void |
251 sysdep_cleanup (void) | |
252 { | |
253 MINGW_SIGNAL_CLEANUP (); | |
254 } | |
255 | |
767 | 256 // Set terminal in raw mode. From less-177. |
257 // | |
258 // Change terminal to "raw mode", or restore to "normal" mode. | |
259 // "Raw mode" means | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
260 // 1. An outstanding read will complete on receipt of a single keystroke. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
261 // 2. Input is not echoed. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
262 // 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
|
263 // 4. \t is NOT expanded into spaces. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
264 // 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
|
265 // etc. are NOT disabled. |
767 | 266 // It doesn't matter whether an input \n is mapped to \r, or vice versa. |
267 | |
529 | 268 void |
3657 | 269 raw_mode (bool on, bool wait) |
529 | 270 { |
3657 | 271 static bool curr_on = false; |
529 | 272 |
273 int tty_fd = STDIN_FILENO; | |
274 if (! isatty (tty_fd)) | |
275 { | |
276 if (interactive) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
277 error ("stdin is not a tty!"); |
529 | 278 return; |
279 } | |
280 | |
281 if (on == curr_on) | |
282 return; | |
283 | |
284 #if defined (HAVE_TERMIOS_H) | |
285 { | |
286 struct termios s; | |
287 static struct termios save_term; | |
288 | |
289 if (on) | |
290 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
291 // Get terminal modes. |
529 | 292 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
293 tcgetattr (tty_fd, &s); |
529 | 294 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
295 // Save modes and set certain variables dependent on modes. |
529 | 296 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
297 save_term = s; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
298 // ospeed = s.c_cflag & CBAUD; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
299 // erase_char = s.c_cc[VERASE]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
300 // kill_char = s.c_cc[VKILL]; |
529 | 301 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
302 // Set the modes to the way we want them. |
529 | 303 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
304 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
305 s.c_oflag |= (OPOST|ONLCR); |
529 | 306 #if defined (OCRNL) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
307 s.c_oflag &= ~(OCRNL); |
529 | 308 #endif |
309 #if defined (ONOCR) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
310 s.c_oflag &= ~(ONOCR); |
529 | 311 #endif |
312 #if defined (ONLRET) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
313 s.c_oflag &= ~(ONLRET); |
529 | 314 #endif |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
315 s.c_cc[VMIN] = wait ? 1 : 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
316 s.c_cc[VTIME] = 0; |
529 | 317 } |
318 else | |
319 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
320 // Restore saved modes. |
1358 | 321 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
322 s = save_term; |
529 | 323 } |
3658 | 324 |
9411
c9636d98e5cd
fix kbhit(1) to not discard waiting keystrokes
Joe Rothweiler <octaveuser@sensicomm.com>
parents:
9242
diff
changeset
|
325 tcsetattr (tty_fd, wait ? TCSAFLUSH : TCSADRAIN, &s); |
529 | 326 } |
327 #elif defined (HAVE_TERMIO_H) | |
328 { | |
329 struct termio s; | |
330 static struct termio save_term; | |
331 | |
332 if (on) | |
333 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
334 // Get terminal modes. |
529 | 335 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
336 ioctl (tty_fd, TCGETA, &s); |
529 | 337 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
338 // Save modes and set certain variables dependent on modes. |
529 | 339 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
340 save_term = s; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
341 // ospeed = s.c_cflag & CBAUD; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
342 // erase_char = s.c_cc[VERASE]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
343 // kill_char = s.c_cc[VKILL]; |
529 | 344 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
345 // Set the modes to the way we want them. |
529 | 346 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
347 s.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
348 s.c_oflag |= (OPOST|ONLCR); |
529 | 349 #if defined (OCRNL) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
350 s.c_oflag &= ~(OCRNL); |
529 | 351 #endif |
352 #if defined (ONOCR) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
353 s.c_oflag &= ~(ONOCR); |
529 | 354 #endif |
355 #if defined (ONLRET) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
356 s.c_oflag &= ~(ONLRET); |
529 | 357 #endif |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
358 s.c_cc[VMIN] = wait ? 1 : 0; |
529 | 359 } |
360 else | |
361 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
362 // Restore saved modes. |
1358 | 363 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
364 s = save_term; |
529 | 365 } |
3658 | 366 |
529 | 367 ioctl (tty_fd, TCSETAW, &s); |
368 } | |
369 #elif defined (HAVE_SGTTY_H) | |
370 { | |
371 struct sgttyb s; | |
372 static struct sgttyb save_term; | |
373 | |
374 if (on) | |
375 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
376 // Get terminal modes. |
529 | 377 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
378 ioctl (tty_fd, TIOCGETP, &s); |
529 | 379 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
380 // Save modes and set certain variables dependent on modes. |
529 | 381 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
382 save_term = s; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
383 // ospeed = s.sg_ospeed; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
384 // erase_char = s.sg_erase; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
385 // kill_char = s.sg_kill; |
529 | 386 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
387 // Set the modes to the way we want them. |
529 | 388 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
389 s.sg_flags |= CBREAK; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
390 s.sg_flags &= ~(ECHO); |
529 | 391 } |
392 else | |
393 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
394 // Restore saved modes. |
1358 | 395 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
396 s = save_term; |
529 | 397 } |
3658 | 398 |
529 | 399 ioctl (tty_fd, TIOCSETN, &s); |
400 } | |
401 #else | |
4064 | 402 warning ("no support for raw mode console I/O on this system"); |
403 | |
404 // Make sure the current mode doesn't toggle. | |
405 on = curr_on; | |
529 | 406 #endif |
407 | |
408 curr_on = on; | |
409 } | |
410 | |
6726 | 411 FILE * |
412 octave_popen (const char *command, const char *mode) | |
413 { | |
414 #if defined (__MINGW32__) || defined (_MSC_VER) | |
415 if (mode && mode[0] && ! mode[1]) | |
416 { | |
417 char tmode[3]; | |
418 tmode[0] = mode[0]; | |
419 tmode[1] = 'b'; | |
420 tmode[2] = 0; | |
421 | |
422 return _popen (command, tmode); | |
423 } | |
424 else | |
425 return _popen (command, mode); | |
426 #else | |
427 return popen (command, mode); | |
428 #endif | |
429 } | |
430 | |
431 int | |
432 octave_pclose (FILE *f) | |
433 { | |
434 #if defined (__MINGW32__) || defined (_MSC_VER) | |
435 return _pclose (f); | |
436 #else | |
437 return pclose (f); | |
438 #endif | |
439 } | |
440 | |
767 | 441 // Read one character from the terminal. |
442 | |
529 | 443 int |
4067 | 444 octave_kbhit (bool wait) |
529 | 445 { |
4067 | 446 #ifdef HAVE__KBHIT |
4081 | 447 int c = (! wait && ! _kbhit ()) ? 0 : std::cin.get (); |
4067 | 448 #else |
3658 | 449 raw_mode (true, wait); |
450 | |
5770 | 451 // Get current handler. |
452 octave_interrupt_handler saved_interrupt_handler | |
453 = octave_ignore_interrupts (); | |
454 | |
455 // Restore it, disabling system call restarts (if possible) so the | |
456 // read can be interrupted. | |
457 | |
458 octave_set_interrupt_handler (saved_interrupt_handler, false); | |
459 | |
3658 | 460 int c = std::cin.get (); |
5770 | 461 |
3658 | 462 if (std::cin.fail () || std::cin.eof ()) |
463 std::cin.clear (); | |
464 | |
5770 | 465 // Restore it, enabling system call restarts (if possible). |
466 octave_set_interrupt_handler (saved_interrupt_handler, true); | |
467 | |
3658 | 468 raw_mode (false, true); |
4067 | 469 #endif |
3658 | 470 |
529 | 471 return c; |
472 } | |
473 | |
1957 | 474 DEFUN (clc, , , |
3332 | 475 "-*- texinfo -*-\n\ |
10840 | 476 @deftypefn {Built-in Function} {} clc ()\n\ |
3332 | 477 @deftypefnx {Built-in Function} {} home ()\n\ |
478 Clear the terminal screen and move the cursor to the upper left corner.\n\ | |
3333 | 479 @end deftypefn") |
529 | 480 { |
2926 | 481 command_editor::clear_screen (); |
529 | 482 |
2926 | 483 return octave_value_list (); |
529 | 484 } |
485 | |
549 | 486 DEFALIAS (home, clc); |
487 | |
1957 | 488 DEFUN (getenv, args, , |
3301 | 489 "-*- texinfo -*-\n\ |
490 @deftypefn {Built-in Function} {} getenv (@var{var})\n\ | |
491 Return the value of the environment variable @var{var}. For example,\n\ | |
492 \n\ | |
493 @example\n\ | |
494 getenv (\"PATH\")\n\ | |
495 @end example\n\ | |
496 \n\ | |
497 @noindent\n\ | |
498 returns a string containing the value of your path.\n\ | |
499 @end deftypefn") | |
529 | 500 { |
4233 | 501 octave_value retval; |
529 | 502 |
503 int nargin = args.length (); | |
504 | |
712 | 505 if (nargin == 1) |
529 | 506 { |
3523 | 507 std::string name = args(0).string_value (); |
636 | 508 |
509 if (! error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
510 retval = octave_env::getenv (name); |
529 | 511 } |
512 else | |
5823 | 513 print_usage (); |
529 | 514 |
515 return retval; | |
516 } | |
517 | |
1957 | 518 DEFUN (putenv, args, , |
3301 | 519 "-*- texinfo -*-\n\ |
10840 | 520 @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
|
521 @deftypefnx {Built-in Function} {} setenv (@var{var}, @var{value})\n\ |
3301 | 522 Set the value of the environment variable @var{var} to @var{value}.\n\ |
523 @end deftypefn") | |
1706 | 524 { |
2086 | 525 octave_value_list retval; |
1706 | 526 |
527 int nargin = args.length (); | |
528 | |
7759
84a7f00586aa
Alias setenv to putenv. Allow single arg to putenv
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
529 if (nargin == 2 || nargin == 1) |
1706 | 530 { |
3523 | 531 std::string var = args(0).string_value (); |
1706 | 532 |
533 if (! error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
534 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
535 std::string val = (nargin == 2 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
536 ? args(1).string_value () : std::string ()); |
1706 | 537 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
538 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
539 octave_env::putenv (var, val); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
540 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
541 error ("putenv: second argument should be a string"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
542 } |
1706 | 543 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
544 error ("putenv: first argument should be a string"); |
1706 | 545 } |
546 else | |
5823 | 547 print_usage (); |
1706 | 548 |
549 return retval; | |
550 } | |
7759
84a7f00586aa
Alias setenv to putenv. Allow single arg to putenv
David Bateman <dbateman@free.fr>
parents:
7231
diff
changeset
|
551 DEFALIAS (setenv, putenv); |
1706 | 552 |
5775 | 553 // FIXME -- perhaps kbhit should also be able to print a prompt? |
3372 | 554 |
3657 | 555 DEFUN (kbhit, args, , |
3372 | 556 "-*- texinfo -*-\n\ |
557 @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
|
558 Read a single keystroke from the keyboard. If called with one\n\ |
3657 | 559 argument, don't wait for a keypress. For example,\n\ |
3372 | 560 \n\ |
561 @example\n\ | |
562 x = kbhit ();\n\ | |
563 @end example\n\ | |
564 \n\ | |
565 @noindent\n\ | |
566 will set @var{x} to the next character typed at the keyboard as soon as\n\ | |
567 it is typed.\n\ | |
3657 | 568 \n\ |
569 @example\n\ | |
570 x = kbhit (1);\n\ | |
571 @end example\n\ | |
572 \n\ | |
573 @noindent\n\ | |
574 identical to the above example, but don't wait for a keypress,\n\ | |
575 returning the empty string if no key is available.\n\ | |
3372 | 576 @end deftypefn") |
529 | 577 { |
4233 | 578 octave_value retval; |
529 | 579 |
5775 | 580 // FIXME -- add timeout and default value args? |
529 | 581 |
3676 | 582 if (interactive || forced_interactive) |
529 | 583 { |
6423 | 584 feval ("drawnow"); |
585 | |
4067 | 586 int c = octave_kbhit (args.length () == 0); |
3657 | 587 |
3658 | 588 if (c == -1) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
589 c = 0; |
3657 | 590 |
529 | 591 char *s = new char [2]; |
592 s[0] = c; | |
593 s[1] = '\0'; | |
594 retval = s; | |
595 } | |
596 | |
597 return retval; | |
598 } | |
599 | |
1957 | 600 DEFUN (pause, args, , |
3301 | 601 "-*- texinfo -*-\n\ |
602 @deftypefn {Built-in Function} {} pause (@var{seconds})\n\ | |
603 Suspend the execution of the program. If invoked without any arguments,\n\ | |
604 Octave waits until you type a character. With a numeric argument, it\n\ | |
605 pauses for the given number of seconds. For example, the following\n\ | |
606 statement prints a message and then waits 5 seconds before clearing the\n\ | |
607 screen.\n\ | |
608 \n\ | |
609 @example\n\ | |
610 @group\n\ | |
6848 | 611 fprintf (stderr, \"wait please...\\n\");\n\ |
3301 | 612 pause (5);\n\ |
613 clc;\n\ | |
614 @end group\n\ | |
615 @end example\n\ | |
616 @end deftypefn") | |
529 | 617 { |
2086 | 618 octave_value_list retval; |
529 | 619 |
620 int nargin = args.length (); | |
621 | |
712 | 622 if (! (nargin == 0 || nargin == 1)) |
529 | 623 { |
5823 | 624 print_usage (); |
529 | 625 return retval; |
626 } | |
627 | |
1579 | 628 if (nargin == 1) |
529 | 629 { |
1579 | 630 double dval = args(0).double_value (); |
636 | 631 |
1579 | 632 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
633 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
634 if (! xisnan (dval)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
635 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
636 feval ("drawnow"); |
6419 | 637 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
638 if (xisinf (dval)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
639 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
640 flush_octave_stdout (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
641 octave_kbhit (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
642 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
643 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
644 octave_sleep (dval); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
645 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
646 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
647 warning ("pause: NaN is an invalid delay"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
648 } |
529 | 649 } |
1579 | 650 else |
3234 | 651 { |
6423 | 652 feval ("drawnow"); |
3234 | 653 flush_octave_stdout (); |
4067 | 654 octave_kbhit (); |
3234 | 655 } |
2630 | 656 |
657 return retval; | |
658 } | |
659 | |
660 DEFUN (sleep, args, , | |
3301 | 661 "-*- texinfo -*-\n\ |
662 @deftypefn {Built-in Function} {} sleep (@var{seconds})\n\ | |
663 Suspend the execution of the program for the given number of seconds.\n\ | |
664 @end deftypefn") | |
2630 | 665 { |
666 octave_value_list retval; | |
667 | |
668 if (args.length () == 1) | |
1579 | 669 { |
2630 | 670 double dval = args(0).double_value (); |
671 | |
672 if (! error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
673 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
674 if (xisnan (dval)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
675 warning ("sleep: NaN is an invalid delay"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
676 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
677 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
678 feval ("drawnow"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
679 octave_sleep (dval); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
680 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
681 } |
1579 | 682 } |
2630 | 683 else |
5823 | 684 print_usage (); |
2630 | 685 |
686 return retval; | |
687 } | |
688 | |
689 DEFUN (usleep, args, , | |
3301 | 690 "-*- texinfo -*-\n\ |
691 @deftypefn {Built-in Function} {} usleep (@var{microseconds})\n\ | |
692 Suspend the execution of the program for the given number of\n\ | |
693 microseconds. On systems where it is not possible to sleep for periods\n\ | |
694 of time less than one second, @code{usleep} will pause the execution for\n\ | |
695 @code{round (@var{microseconds} / 1e6)} seconds.\n\ | |
696 @end deftypefn") | |
2630 | 697 { |
698 octave_value_list retval; | |
699 | |
700 if (args.length () == 1) | |
701 { | |
702 double dval = args(0).double_value (); | |
703 | |
704 if (! error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
705 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
706 if (xisnan (dval)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
707 warning ("usleep: NaN is an invalid delay"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
708 else |
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 feval ("drawnow"); |
6423 | 711 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
712 int delay = NINT (dval); |
2631 | 713 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
714 if (delay > 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
715 octave_usleep (delay); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
716 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
717 } |
2630 | 718 } |
719 else | |
5823 | 720 print_usage (); |
529 | 721 |
722 return retval; | |
723 } | |
724 | |
5775 | 725 // FIXME -- maybe this should only return 1 if IEEE floating |
862 | 726 // point functions really work. |
727 | |
1957 | 728 DEFUN (isieee, , , |
3301 | 729 "-*- texinfo -*-\n\ |
730 @deftypefn {Built-in Function} {} isieee ()\n\ | |
731 Return 1 if your computer claims to conform to the IEEE standard for\n\ | |
732 floating point calculations.\n\ | |
733 @end deftypefn") | |
862 | 734 { |
4600 | 735 oct_mach_info::float_format flt_fmt = oct_mach_info::native_float_format (); |
2317 | 736 |
4574 | 737 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
|
738 || flt_fmt == oct_mach_info::flt_fmt_ieee_big_endian); |
862 | 739 } |
740 | |
4600 | 741 DEFUN (native_float_format, , , |
742 "-*- texinfo -*-\n\ | |
743 @deftypefn {Built-in Function} {} native_float_format ()\n\ | |
744 Return the native floating point format as a string\n\ | |
745 @end deftypefn") | |
746 { | |
747 oct_mach_info::float_format flt_fmt = oct_mach_info::native_float_format (); | |
748 | |
749 return octave_value (oct_mach_info::float_format_as_string (flt_fmt)); | |
750 } | |
751 | |
1957 | 752 DEFUN (tilde_expand, args, , |
3301 | 753 "-*- texinfo -*-\n\ |
754 @deftypefn {Built-in Function} {} tilde_expand (@var{string})\n\ | |
755 Performs tilde expansion on @var{string}. If @var{string} begins with a\n\ | |
756 tilde character, (@samp{~}), all of the characters preceding the first\n\ | |
757 slash (or all characters, if there is no slash) are treated as a\n\ | |
758 possible user name, and the tilde and the following characters up to the\n\ | |
759 slash are replaced by the home directory of the named user. If the\n\ | |
760 tilde is followed immediately by a slash, the tilde is replaced by the\n\ | |
10840 | 761 home directory of the user running Octave. For example:\n\ |
3301 | 762 \n\ |
763 @example\n\ | |
764 @group\n\ | |
765 tilde_expand (\"~joeuser/bin\")\n\ | |
766 @result{} \"/home/joeuser/bin\"\n\ | |
767 tilde_expand (\"~/bin\")\n\ | |
768 @result{} \"/home/jwe/bin\"\n\ | |
769 @end group\n\ | |
770 @end example\n\ | |
771 @end deftypefn") | |
1750 | 772 { |
4233 | 773 octave_value retval; |
1750 | 774 |
775 int nargin = args.length (); | |
776 | |
777 if (nargin == 1) | |
6116 | 778 { |
779 octave_value arg = args(0); | |
780 | |
781 string_vector sv = arg.all_strings (); | |
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 sv = file_ops::tilde_expand (sv); |
6116 | 786 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
787 if (arg.is_cellstr ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
788 retval = Cell (arg.dims (), sv); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
789 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
790 retval = sv; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
791 } |
6116 | 792 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10266
diff
changeset
|
793 error ("tilde_expand: expecting argument to be char or cellstr object"); |
6116 | 794 } |
1750 | 795 else |
5823 | 796 print_usage (); |
1750 | 797 |
798 return retval; | |
799 } |