Mercurial > hg > octave-lyh
annotate liboctave/system/lo-sysdep.cc @ 16476:98155e2b6d42
get terminal size on window systems
* lo-sysdep.h, lo-sysdep.cc (w32_terminal_rows, w32_terminal_cols):
New functions.
* cmd-edit.cc (gnu_readline::do_terminal_rows,
gnu_readline::do_terminal_cols): Use them.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 09 Apr 2013 03:17:48 -0400 |
parents | 0696dcc92fc8 |
children | b98ad20e4880 |
rev | line source |
---|---|
2926 | 1 /* |
2 | |
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
12223
diff
changeset
|
3 Copyright (C) 1996-2012 John W. Eaton |
2926 | 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. | |
2926 | 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/>. | |
2926 | 20 |
21 */ | |
22 | |
23 #ifdef HAVE_CONFIG_H | |
24 #include <config.h> | |
25 #endif | |
26 | |
3503 | 27 #include <iostream> |
2926 | 28 #include <string> |
6321 | 29 #include <vector> |
2926 | 30 |
31 #include <sys/types.h> | |
32 #include <unistd.h> | |
33 | |
6321 | 34 #include <fcntl.h> |
35 | |
7695
eacf87a24f55
lo-sysdep.cc: include windows.h if windows and not cygwin
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
36 #if defined (__WIN32__) && ! defined (__CYGWIN__) |
10346
65d5776379c3
Reduce the amount of stuff included by windows.h and avoid min/max being #define-d
Michael Goffioul <michael.goffioul@gmail.com>
parents:
10314
diff
changeset
|
37 #define WIN32_LEAN_AND_MEAN |
7695
eacf87a24f55
lo-sysdep.cc: include windows.h if windows and not cygwin
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
38 #include <windows.h> |
eacf87a24f55
lo-sysdep.cc: include windows.h if windows and not cygwin
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
39 #endif |
eacf87a24f55
lo-sysdep.cc: include windows.h if windows and not cygwin
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
40 |
5872 | 41 #include "file-ops.h" |
3069 | 42 #include "lo-error.h" |
2926 | 43 #include "pathlen.h" |
6123 | 44 #include "lo-sysdep.h" |
6321 | 45 #include "str-vec.h" |
8377
25bc2d31e1bf
improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents:
7732
diff
changeset
|
46 #include "oct-locbuf.h" |
2926 | 47 |
3504 | 48 std::string |
2926 | 49 octave_getcwd (void) |
50 { | |
3504 | 51 std::string retval; |
3069 | 52 |
10250 | 53 // Using the gnulib getcwd module ensures that we have a getcwd that |
54 // will allocate a buffer as large as necessary if buf and size are | |
55 // both 0. | |
3069 | 56 |
10411 | 57 char *tmp = gnulib::getcwd (0, 0); |
2926 | 58 |
59 if (tmp) | |
10250 | 60 { |
61 retval = tmp; | |
62 free (tmp); | |
63 } | |
3069 | 64 else |
65 (*current_liboctave_error_handler) ("unable to find current directory"); | |
2926 | 66 |
67 return retval; | |
68 } | |
69 | |
70 int | |
5872 | 71 octave_chdir (const std::string& path_arg) |
2926 | 72 { |
5872 | 73 std::string path = file_ops::tilde_expand (path_arg); |
74 | |
6244 | 75 #if defined (__WIN32__) && ! defined (__CYGWIN__) |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14155
diff
changeset
|
76 if (path.length () == 2 && path[1] == ':') |
6244 | 77 path += "\\"; |
78 #endif | |
79 | |
14152
933bf1b4ab29
* lo-sysdep.cc (octave_chdir): Use gnulib::chdir.
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
80 return gnulib::chdir (path.c_str ()); |
2926 | 81 } |
82 | |
6321 | 83 #if defined (__WIN32__) && ! defined (__CYGWIN__) |
84 | |
85 pid_t | |
86 octave_popen2 (const std::string& cmd, const string_vector& args, bool sync_mode, | |
87 int *fildes, std::string& msg) | |
88 { | |
89 pid_t pid; | |
90 PROCESS_INFORMATION pi; | |
91 STARTUPINFO si; | |
92 std::string command = "\"" + cmd + "\""; | |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14155
diff
changeset
|
93 HANDLE hProcess = GetCurrentProcess (), childRead, childWrite, parentRead, parentWrite; |
6321 | 94 DWORD pipeMode; |
95 | |
96 ZeroMemory (&pi, sizeof (pi)); | |
97 ZeroMemory (&si, sizeof (si)); | |
98 si.cb = sizeof (si); | |
99 | |
100 if (! CreatePipe (&childRead, &parentWrite, 0, 0) || | |
101 ! DuplicateHandle (hProcess, childRead, hProcess, &childRead, 0, TRUE, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE)) | |
102 { | |
103 msg = "popen2: pipe creation failed"; | |
104 return -1; | |
105 } | |
106 if (! CreatePipe (&parentRead, &childWrite, 0, 0) || | |
107 ! DuplicateHandle (hProcess, childWrite, hProcess, &childWrite, 0, TRUE, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE)) | |
108 { | |
109 msg = "popen2: pipe creation failed"; | |
110 return -1; | |
111 } | |
112 if (! sync_mode) | |
113 { | |
114 pipeMode = PIPE_NOWAIT; | |
115 SetNamedPipeHandleState (parentRead, &pipeMode, 0, 0); | |
116 } | |
16314
0723ea02dcdb
use intptr_t instead of long
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
117 fildes[1] = _open_osfhandle (reinterpret_cast<intptr_t> (parentRead), _O_RDONLY | _O_BINARY); |
0723ea02dcdb
use intptr_t instead of long
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
118 fildes[0] = _open_osfhandle (reinterpret_cast<intptr_t> (parentWrite), _O_WRONLY | _O_BINARY); |
6321 | 119 si.dwFlags |= STARTF_USESTDHANDLES; |
120 si.hStdInput = childRead; | |
121 si.hStdOutput = childWrite; | |
122 | |
123 // Ignore first arg as it is the command | |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14155
diff
changeset
|
124 for (int k=1; k<args.length (); k++) |
6321 | 125 command += " \"" + args[k] + "\""; |
126 OCTAVE_LOCAL_BUFFER (char, c_command, command.length () + 1); | |
127 strcpy (c_command, command.c_str ()); | |
128 if (! CreateProcess (0, c_command, 0, 0, TRUE, 0, 0, 0, &si, &pi)) | |
129 { | |
130 msg = "popen2: process creation failed"; | |
131 return -1; | |
132 } | |
133 pid = pi.dwProcessId; | |
134 | |
135 CloseHandle (childRead); | |
136 CloseHandle (childWrite); | |
137 CloseHandle (pi.hProcess); | |
138 CloseHandle (pi.hThread); | |
139 | |
140 return pid; | |
141 } | |
142 | |
16476
98155e2b6d42
get terminal size on window systems
John W. Eaton <jwe@octave.org>
parents:
16475
diff
changeset
|
143 int |
98155e2b6d42
get terminal size on window systems
John W. Eaton <jwe@octave.org>
parents:
16475
diff
changeset
|
144 w32_terminal_rows (void) |
98155e2b6d42
get terminal size on window systems
John W. Eaton <jwe@octave.org>
parents:
16475
diff
changeset
|
145 { |
98155e2b6d42
get terminal size on window systems
John W. Eaton <jwe@octave.org>
parents:
16475
diff
changeset
|
146 int retval = 0; |
98155e2b6d42
get terminal size on window systems
John W. Eaton <jwe@octave.org>
parents:
16475
diff
changeset
|
147 |
98155e2b6d42
get terminal size on window systems
John W. Eaton <jwe@octave.org>
parents:
16475
diff
changeset
|
148 HANDLE console = GetStdHandle (STD_OUTPUT_HANDLE); |
98155e2b6d42
get terminal size on window systems
John W. Eaton <jwe@octave.org>
parents:
16475
diff
changeset
|
149 |
98155e2b6d42
get terminal size on window systems
John W. Eaton <jwe@octave.org>
parents:
16475
diff
changeset
|
150 CONSOLE_SCREEN_BUFFER_INFO csbi; |
98155e2b6d42
get terminal size on window systems
John W. Eaton <jwe@octave.org>
parents:
16475
diff
changeset
|
151 |
98155e2b6d42
get terminal size on window systems
John W. Eaton <jwe@octave.org>
parents:
16475
diff
changeset
|
152 if (GetConsoleScreenBufferInfo (console, &csbi)) |
98155e2b6d42
get terminal size on window systems
John W. Eaton <jwe@octave.org>
parents:
16475
diff
changeset
|
153 retval = csbi.dwSize.Y; |
98155e2b6d42
get terminal size on window systems
John W. Eaton <jwe@octave.org>
parents:
16475
diff
changeset
|
154 |
98155e2b6d42
get terminal size on window systems
John W. Eaton <jwe@octave.org>
parents:
16475
diff
changeset
|
155 return retval; |
98155e2b6d42
get terminal size on window systems
John W. Eaton <jwe@octave.org>
parents:
16475
diff
changeset
|
156 } |
98155e2b6d42
get terminal size on window systems
John W. Eaton <jwe@octave.org>
parents:
16475
diff
changeset
|
157 |
98155e2b6d42
get terminal size on window systems
John W. Eaton <jwe@octave.org>
parents:
16475
diff
changeset
|
158 int |
98155e2b6d42
get terminal size on window systems
John W. Eaton <jwe@octave.org>
parents:
16475
diff
changeset
|
159 w32_terminal_cols (void) |
98155e2b6d42
get terminal size on window systems
John W. Eaton <jwe@octave.org>
parents:
16475
diff
changeset
|
160 { |
98155e2b6d42
get terminal size on window systems
John W. Eaton <jwe@octave.org>
parents:
16475
diff
changeset
|
161 int retval = 0; |
98155e2b6d42
get terminal size on window systems
John W. Eaton <jwe@octave.org>
parents:
16475
diff
changeset
|
162 |
98155e2b6d42
get terminal size on window systems
John W. Eaton <jwe@octave.org>
parents:
16475
diff
changeset
|
163 HANDLE console = GetStdHandle (STD_OUTPUT_HANDLE); |
98155e2b6d42
get terminal size on window systems
John W. Eaton <jwe@octave.org>
parents:
16475
diff
changeset
|
164 |
98155e2b6d42
get terminal size on window systems
John W. Eaton <jwe@octave.org>
parents:
16475
diff
changeset
|
165 CONSOLE_SCREEN_BUFFER_INFO csbi; |
98155e2b6d42
get terminal size on window systems
John W. Eaton <jwe@octave.org>
parents:
16475
diff
changeset
|
166 |
98155e2b6d42
get terminal size on window systems
John W. Eaton <jwe@octave.org>
parents:
16475
diff
changeset
|
167 if (GetConsoleScreenBufferInfo (console, &csbi)) |
98155e2b6d42
get terminal size on window systems
John W. Eaton <jwe@octave.org>
parents:
16475
diff
changeset
|
168 retval = csbi.dwSize.X; |
98155e2b6d42
get terminal size on window systems
John W. Eaton <jwe@octave.org>
parents:
16475
diff
changeset
|
169 |
98155e2b6d42
get terminal size on window systems
John W. Eaton <jwe@octave.org>
parents:
16475
diff
changeset
|
170 return retval; |
98155e2b6d42
get terminal size on window systems
John W. Eaton <jwe@octave.org>
parents:
16475
diff
changeset
|
171 } |
98155e2b6d42
get terminal size on window systems
John W. Eaton <jwe@octave.org>
parents:
16475
diff
changeset
|
172 |
16475
0696dcc92fc8
allow windows console to be cleared
John W. Eaton <jwe@octave.org>
parents:
16314
diff
changeset
|
173 void |
0696dcc92fc8
allow windows console to be cleared
John W. Eaton <jwe@octave.org>
parents:
16314
diff
changeset
|
174 w32_clear_console_window (void) |
0696dcc92fc8
allow windows console to be cleared
John W. Eaton <jwe@octave.org>
parents:
16314
diff
changeset
|
175 { |
0696dcc92fc8
allow windows console to be cleared
John W. Eaton <jwe@octave.org>
parents:
16314
diff
changeset
|
176 HANDLE console = GetStdHandle (STD_OUTPUT_HANDLE); |
0696dcc92fc8
allow windows console to be cleared
John W. Eaton <jwe@octave.org>
parents:
16314
diff
changeset
|
177 |
0696dcc92fc8
allow windows console to be cleared
John W. Eaton <jwe@octave.org>
parents:
16314
diff
changeset
|
178 // Get the number of character cells in the current buffer. |
0696dcc92fc8
allow windows console to be cleared
John W. Eaton <jwe@octave.org>
parents:
16314
diff
changeset
|
179 |
0696dcc92fc8
allow windows console to be cleared
John W. Eaton <jwe@octave.org>
parents:
16314
diff
changeset
|
180 CONSOLE_SCREEN_BUFFER_INFO csbi; |
0696dcc92fc8
allow windows console to be cleared
John W. Eaton <jwe@octave.org>
parents:
16314
diff
changeset
|
181 |
0696dcc92fc8
allow windows console to be cleared
John W. Eaton <jwe@octave.org>
parents:
16314
diff
changeset
|
182 if (GetConsoleScreenBufferInfo (console, &csbi)) |
0696dcc92fc8
allow windows console to be cleared
John W. Eaton <jwe@octave.org>
parents:
16314
diff
changeset
|
183 { |
0696dcc92fc8
allow windows console to be cleared
John W. Eaton <jwe@octave.org>
parents:
16314
diff
changeset
|
184 DWORD screen_size = csbi.dwSize.X * csbi.dwSize.Y; |
0696dcc92fc8
allow windows console to be cleared
John W. Eaton <jwe@octave.org>
parents:
16314
diff
changeset
|
185 |
0696dcc92fc8
allow windows console to be cleared
John W. Eaton <jwe@octave.org>
parents:
16314
diff
changeset
|
186 // Fill the entire screen with blanks. |
0696dcc92fc8
allow windows console to be cleared
John W. Eaton <jwe@octave.org>
parents:
16314
diff
changeset
|
187 |
0696dcc92fc8
allow windows console to be cleared
John W. Eaton <jwe@octave.org>
parents:
16314
diff
changeset
|
188 COORD home = { 0, 0 }; |
0696dcc92fc8
allow windows console to be cleared
John W. Eaton <jwe@octave.org>
parents:
16314
diff
changeset
|
189 DWORD nchars; |
0696dcc92fc8
allow windows console to be cleared
John W. Eaton <jwe@octave.org>
parents:
16314
diff
changeset
|
190 |
0696dcc92fc8
allow windows console to be cleared
John W. Eaton <jwe@octave.org>
parents:
16314
diff
changeset
|
191 if (FillConsoleOutputCharacter (console, static_cast<TCHAR> (' '), |
0696dcc92fc8
allow windows console to be cleared
John W. Eaton <jwe@octave.org>
parents:
16314
diff
changeset
|
192 screen_size, home, &nchars)) |
0696dcc92fc8
allow windows console to be cleared
John W. Eaton <jwe@octave.org>
parents:
16314
diff
changeset
|
193 { |
0696dcc92fc8
allow windows console to be cleared
John W. Eaton <jwe@octave.org>
parents:
16314
diff
changeset
|
194 if (GetConsoleScreenBufferInfo (console, &csbi)) |
0696dcc92fc8
allow windows console to be cleared
John W. Eaton <jwe@octave.org>
parents:
16314
diff
changeset
|
195 { |
0696dcc92fc8
allow windows console to be cleared
John W. Eaton <jwe@octave.org>
parents:
16314
diff
changeset
|
196 if (FillConsoleOutputAttribute (console, csbi.wAttributes, |
0696dcc92fc8
allow windows console to be cleared
John W. Eaton <jwe@octave.org>
parents:
16314
diff
changeset
|
197 screen_size, home, &nchars)) |
0696dcc92fc8
allow windows console to be cleared
John W. Eaton <jwe@octave.org>
parents:
16314
diff
changeset
|
198 { |
0696dcc92fc8
allow windows console to be cleared
John W. Eaton <jwe@octave.org>
parents:
16314
diff
changeset
|
199 SetConsoleCursorPosition (console, home); |
0696dcc92fc8
allow windows console to be cleared
John W. Eaton <jwe@octave.org>
parents:
16314
diff
changeset
|
200 } |
0696dcc92fc8
allow windows console to be cleared
John W. Eaton <jwe@octave.org>
parents:
16314
diff
changeset
|
201 } |
0696dcc92fc8
allow windows console to be cleared
John W. Eaton <jwe@octave.org>
parents:
16314
diff
changeset
|
202 } |
0696dcc92fc8
allow windows console to be cleared
John W. Eaton <jwe@octave.org>
parents:
16314
diff
changeset
|
203 } |
0696dcc92fc8
allow windows console to be cleared
John W. Eaton <jwe@octave.org>
parents:
16314
diff
changeset
|
204 } |
0696dcc92fc8
allow windows console to be cleared
John W. Eaton <jwe@octave.org>
parents:
16314
diff
changeset
|
205 |
6321 | 206 #endif |