diff options
Diffstat (limited to 'shell/shell_common.c')
-rw-r--r-- | shell/shell_common.c | 57 |
1 files changed, 56 insertions, 1 deletions
diff --git a/shell/shell_common.c b/shell/shell_common.c index e0582adfb..d1df5888c 100644 --- a/shell/shell_common.c +++ b/shell/shell_common.c | |||
@@ -19,7 +19,11 @@ | |||
19 | #include "libbb.h" | 19 | #include "libbb.h" |
20 | #include "shell_common.h" | 20 | #include "shell_common.h" |
21 | 21 | ||
22 | #if !ENABLE_PLATFORM_MINGW32 | ||
22 | const char defifsvar[] ALIGN1 = "IFS= \t\n"; | 23 | const char defifsvar[] ALIGN1 = "IFS= \t\n"; |
24 | #else | ||
25 | const char defifsvar[] ALIGN1 = "IFS= \t\n\r"; | ||
26 | #endif | ||
23 | const char defoptindvar[] ALIGN1 = "OPTIND=1"; | 27 | const char defoptindvar[] ALIGN1 = "OPTIND=1"; |
24 | 28 | ||
25 | /* read builtin */ | 29 | /* read builtin */ |
@@ -43,7 +47,9 @@ shell_builtin_read(struct builtin_read_params *params) | |||
43 | char **pp; | 47 | char **pp; |
44 | char *buffer; | 48 | char *buffer; |
45 | char delim; | 49 | char delim; |
50 | #if !ENABLE_PLATFORM_MINGW32 | ||
46 | struct termios tty, old_tty; | 51 | struct termios tty, old_tty; |
52 | #endif | ||
47 | const char *retval; | 53 | const char *retval; |
48 | int bufpos; /* need to be able to hold -1 */ | 54 | int bufpos; /* need to be able to hold -1 */ |
49 | int startword; | 55 | int startword; |
@@ -123,6 +129,18 @@ shell_builtin_read(struct builtin_read_params *params) | |||
123 | * bash seems to ignore -p PROMPT for this use case. | 129 | * bash seems to ignore -p PROMPT for this use case. |
124 | */ | 130 | */ |
125 | int r; | 131 | int r; |
132 | #if ENABLE_PLATFORM_MINGW32 | ||
133 | HANDLE handle = (HANDLE)_get_osfhandle(fd); | ||
134 | DWORD filetype = FILE_TYPE_UNKNOWN; | ||
135 | |||
136 | if (handle != INVALID_HANDLE_VALUE) | ||
137 | filetype = GetFileType(handle); | ||
138 | /* poll uses WaitForSingleObject which can't handle disk files */ | ||
139 | if (filetype == FILE_TYPE_DISK || filetype == FILE_TYPE_UNKNOWN) | ||
140 | return (const char *)(uintptr_t)(0); | ||
141 | if (isatty(fd)) | ||
142 | return (const char *)(uintptr_t)(1); | ||
143 | #endif | ||
126 | pfd[0].events = POLLIN; | 144 | pfd[0].events = POLLIN; |
127 | r = poll(pfd, 1, /*timeout:*/ 0); | 145 | r = poll(pfd, 1, /*timeout:*/ 0); |
128 | /* Return 0 only if poll returns 1 ("one fd ready"), else return 1: */ | 146 | /* Return 0 only if poll returns 1 ("one fd ready"), else return 1: */ |
@@ -139,6 +157,7 @@ shell_builtin_read(struct builtin_read_params *params) | |||
139 | ifs = defifs; | 157 | ifs = defifs; |
140 | 158 | ||
141 | read_flags = params->read_flags; | 159 | read_flags = params->read_flags; |
160 | #if !ENABLE_PLATFORM_MINGW32 | ||
142 | if (nchars || (read_flags & BUILTIN_READ_SILENT)) { | 161 | if (nchars || (read_flags & BUILTIN_READ_SILENT)) { |
143 | tcgetattr(fd, &tty); | 162 | tcgetattr(fd, &tty); |
144 | old_tty = tty; | 163 | old_tty = tty; |
@@ -161,6 +180,7 @@ shell_builtin_read(struct builtin_read_params *params) | |||
161 | * Ignoring, it's harmless. */ | 180 | * Ignoring, it's harmless. */ |
162 | tcsetattr(fd, TCSANOW, &tty); | 181 | tcsetattr(fd, TCSANOW, &tty); |
163 | } | 182 | } |
183 | #endif | ||
164 | 184 | ||
165 | retval = (const char *)(uintptr_t)0; | 185 | retval = (const char *)(uintptr_t)0; |
166 | startword = 1; | 186 | startword = 1; |
@@ -190,6 +210,7 @@ shell_builtin_read(struct builtin_read_params *params) | |||
190 | } | 210 | } |
191 | } | 211 | } |
192 | 212 | ||
213 | #if !ENABLE_PLATFORM_MINGW32 | ||
193 | /* We must poll even if timeout is -1: | 214 | /* We must poll even if timeout is -1: |
194 | * we want to be interrupted if signal arrives, | 215 | * we want to be interrupted if signal arrives, |
195 | * regardless of SA_RESTART-ness of that signal! | 216 | * regardless of SA_RESTART-ness of that signal! |
@@ -207,9 +228,34 @@ shell_builtin_read(struct builtin_read_params *params) | |||
207 | retval = (const char *)(uintptr_t)1; | 228 | retval = (const char *)(uintptr_t)1; |
208 | break; | 229 | break; |
209 | } | 230 | } |
231 | #else | ||
232 | errno = 0; | ||
233 | if (isatty(fd)) { | ||
234 | int64_t key; | ||
235 | |||
236 | key = read_key(fd, NULL, timeout); | ||
237 | if (key == 0x03 || key == -1) { | ||
238 | /* ^C or timeout */ | ||
239 | retval = (const char *)(uintptr_t)1; | ||
240 | goto ret; | ||
241 | } | ||
242 | buffer[bufpos] = key == '\r' ? '\n' : key; | ||
243 | if (!(read_flags & BUILTIN_READ_SILENT)) { | ||
244 | /* echo input if not in silent mode */ | ||
245 | putchar(buffer[bufpos]); | ||
246 | } | ||
247 | } | ||
248 | else { | ||
249 | if (read(fd, &buffer[bufpos], 1) != 1) { | ||
250 | err = errno; | ||
251 | retval = (const char *)(uintptr_t)1; | ||
252 | break; | ||
253 | } | ||
254 | } | ||
255 | #endif | ||
210 | 256 | ||
211 | c = buffer[bufpos]; | 257 | c = buffer[bufpos]; |
212 | if (c == '\0') | 258 | if (c == '\0' || (ENABLE_PLATFORM_MINGW32 && c == '\r')) |
213 | continue; | 259 | continue; |
214 | if (!(read_flags & BUILTIN_READ_RAW)) { | 260 | if (!(read_flags & BUILTIN_READ_RAW)) { |
215 | if (backslash) { | 261 | if (backslash) { |
@@ -309,8 +355,10 @@ shell_builtin_read(struct builtin_read_params *params) | |||
309 | 355 | ||
310 | ret: | 356 | ret: |
311 | free(buffer); | 357 | free(buffer); |
358 | #if !ENABLE_PLATFORM_MINGW32 | ||
312 | if (read_flags & BUILTIN_READ_SILENT) | 359 | if (read_flags & BUILTIN_READ_SILENT) |
313 | tcsetattr(fd, TCSANOW, &old_tty); | 360 | tcsetattr(fd, TCSANOW, &old_tty); |
361 | #endif | ||
314 | 362 | ||
315 | errno = err; | 363 | errno = err; |
316 | return retval; | 364 | return retval; |
@@ -319,6 +367,7 @@ shell_builtin_read(struct builtin_read_params *params) | |||
319 | 367 | ||
320 | /* ulimit builtin */ | 368 | /* ulimit builtin */ |
321 | 369 | ||
370 | #if !ENABLE_PLATFORM_MINGW32 | ||
322 | struct limits { | 371 | struct limits { |
323 | uint8_t cmd; /* RLIMIT_xxx fit into it */ | 372 | uint8_t cmd; /* RLIMIT_xxx fit into it */ |
324 | uint8_t factor_shift; /* shift by to get rlim_{cur,max} values */ | 373 | uint8_t factor_shift; /* shift by to get rlim_{cur,max} values */ |
@@ -633,3 +682,9 @@ shell_builtin_ulimit(char **argv) | |||
633 | 682 | ||
634 | return EXIT_SUCCESS; | 683 | return EXIT_SUCCESS; |
635 | } | 684 | } |
685 | #else | ||
686 | int FAST_FUNC shell_builtin_ulimit(char **argv UNUSED_PARAM) | ||
687 | { | ||
688 | return 1; | ||
689 | } | ||
690 | #endif | ||