rbuv 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (233) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +5 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +5 -0
  5. data/Gemfile +0 -1
  6. data/README.md +6 -1
  7. data/Rakefile +42 -0
  8. data/deps/libuv/.gitignore +34 -0
  9. data/deps/libuv/.mailmap +16 -0
  10. data/deps/libuv/AUTHORS +81 -0
  11. data/deps/libuv/ChangeLog +45 -0
  12. data/deps/libuv/LICENSE +41 -0
  13. data/deps/libuv/Makefile +53 -0
  14. data/deps/libuv/README.md +118 -0
  15. data/deps/libuv/build.mk +164 -0
  16. data/deps/libuv/checksparse.sh +230 -0
  17. data/deps/libuv/common.gypi +197 -0
  18. data/deps/libuv/config-mingw.mk +48 -0
  19. data/deps/libuv/config-unix.mk +167 -0
  20. data/deps/libuv/gyp_uv +98 -0
  21. data/deps/libuv/include/uv-private/ngx-queue.h +129 -0
  22. data/deps/libuv/include/uv-private/stdint-msvc2008.h +247 -0
  23. data/deps/libuv/include/uv-private/tree.h +768 -0
  24. data/deps/libuv/include/uv-private/uv-bsd.h +34 -0
  25. data/deps/libuv/include/uv-private/uv-darwin.h +61 -0
  26. data/deps/libuv/include/uv-private/uv-linux.h +34 -0
  27. data/deps/libuv/include/uv-private/uv-sunos.h +44 -0
  28. data/deps/libuv/include/uv-private/uv-unix.h +332 -0
  29. data/deps/libuv/include/uv-private/uv-win.h +585 -0
  30. data/deps/libuv/include/uv.h +1987 -0
  31. data/deps/libuv/src/fs-poll.c +248 -0
  32. data/deps/libuv/src/inet.c +298 -0
  33. data/deps/libuv/src/unix/aix.c +393 -0
  34. data/deps/libuv/src/unix/async.c +281 -0
  35. data/deps/libuv/src/unix/core.c +714 -0
  36. data/deps/libuv/src/unix/cygwin.c +93 -0
  37. data/deps/libuv/src/unix/darwin-proctitle.m +78 -0
  38. data/deps/libuv/src/unix/darwin.c +431 -0
  39. data/deps/libuv/src/unix/dl.c +83 -0
  40. data/deps/libuv/src/unix/error.c +109 -0
  41. data/deps/libuv/src/unix/freebsd.c +343 -0
  42. data/deps/libuv/src/unix/fs.c +869 -0
  43. data/deps/libuv/src/unix/fsevents.c +299 -0
  44. data/deps/libuv/src/unix/getaddrinfo.c +159 -0
  45. data/deps/libuv/src/unix/internal.h +259 -0
  46. data/deps/libuv/src/unix/kqueue.c +347 -0
  47. data/deps/libuv/src/unix/linux-core.c +724 -0
  48. data/deps/libuv/src/unix/linux-inotify.c +236 -0
  49. data/deps/libuv/src/unix/linux-syscalls.c +388 -0
  50. data/deps/libuv/src/unix/linux-syscalls.h +150 -0
  51. data/deps/libuv/src/unix/loop-watcher.c +64 -0
  52. data/deps/libuv/src/unix/loop.c +114 -0
  53. data/deps/libuv/src/unix/netbsd.c +353 -0
  54. data/deps/libuv/src/unix/openbsd.c +304 -0
  55. data/deps/libuv/src/unix/pipe.c +261 -0
  56. data/deps/libuv/src/unix/poll.c +108 -0
  57. data/deps/libuv/src/unix/process.c +501 -0
  58. data/deps/libuv/src/unix/proctitle.c +103 -0
  59. data/deps/libuv/src/unix/signal.c +455 -0
  60. data/deps/libuv/src/unix/stream.c +1380 -0
  61. data/deps/libuv/src/unix/sunos.c +647 -0
  62. data/deps/libuv/src/unix/tcp.c +357 -0
  63. data/deps/libuv/src/unix/thread.c +431 -0
  64. data/deps/libuv/src/unix/threadpool.c +286 -0
  65. data/deps/libuv/src/unix/timer.c +153 -0
  66. data/deps/libuv/src/unix/tty.c +179 -0
  67. data/deps/libuv/src/unix/udp.c +715 -0
  68. data/deps/libuv/src/uv-common.c +431 -0
  69. data/deps/libuv/src/uv-common.h +204 -0
  70. data/deps/libuv/src/version.c +60 -0
  71. data/deps/libuv/src/win/async.c +99 -0
  72. data/deps/libuv/src/win/atomicops-inl.h +56 -0
  73. data/deps/libuv/src/win/core.c +310 -0
  74. data/deps/libuv/src/win/dl.c +86 -0
  75. data/deps/libuv/src/win/error.c +164 -0
  76. data/deps/libuv/src/win/fs-event.c +506 -0
  77. data/deps/libuv/src/win/fs.c +1951 -0
  78. data/deps/libuv/src/win/getaddrinfo.c +365 -0
  79. data/deps/libuv/src/win/handle-inl.h +164 -0
  80. data/deps/libuv/src/win/handle.c +153 -0
  81. data/deps/libuv/src/win/internal.h +346 -0
  82. data/deps/libuv/src/win/loop-watcher.c +124 -0
  83. data/deps/libuv/src/win/pipe.c +1656 -0
  84. data/deps/libuv/src/win/poll.c +615 -0
  85. data/deps/libuv/src/win/process-stdio.c +503 -0
  86. data/deps/libuv/src/win/process.c +1048 -0
  87. data/deps/libuv/src/win/req-inl.h +224 -0
  88. data/deps/libuv/src/win/req.c +25 -0
  89. data/deps/libuv/src/win/signal.c +354 -0
  90. data/deps/libuv/src/win/stream-inl.h +67 -0
  91. data/deps/libuv/src/win/stream.c +198 -0
  92. data/deps/libuv/src/win/tcp.c +1422 -0
  93. data/deps/libuv/src/win/thread.c +666 -0
  94. data/deps/libuv/src/win/threadpool.c +82 -0
  95. data/deps/libuv/src/win/timer.c +230 -0
  96. data/deps/libuv/src/win/tty.c +1857 -0
  97. data/deps/libuv/src/win/udp.c +744 -0
  98. data/deps/libuv/src/win/util.c +946 -0
  99. data/deps/libuv/src/win/winapi.c +152 -0
  100. data/deps/libuv/src/win/winapi.h +4476 -0
  101. data/deps/libuv/src/win/winsock.c +560 -0
  102. data/deps/libuv/src/win/winsock.h +171 -0
  103. data/deps/libuv/test/benchmark-async-pummel.c +119 -0
  104. data/deps/libuv/test/benchmark-async.c +139 -0
  105. data/deps/libuv/test/benchmark-fs-stat.c +136 -0
  106. data/deps/libuv/test/benchmark-getaddrinfo.c +91 -0
  107. data/deps/libuv/test/benchmark-list.h +163 -0
  108. data/deps/libuv/test/benchmark-loop-count.c +90 -0
  109. data/deps/libuv/test/benchmark-million-async.c +112 -0
  110. data/deps/libuv/test/benchmark-million-timers.c +77 -0
  111. data/deps/libuv/test/benchmark-multi-accept.c +432 -0
  112. data/deps/libuv/test/benchmark-ping-pongs.c +212 -0
  113. data/deps/libuv/test/benchmark-pound.c +325 -0
  114. data/deps/libuv/test/benchmark-pump.c +459 -0
  115. data/deps/libuv/test/benchmark-sizes.c +45 -0
  116. data/deps/libuv/test/benchmark-spawn.c +163 -0
  117. data/deps/libuv/test/benchmark-tcp-write-batch.c +141 -0
  118. data/deps/libuv/test/benchmark-thread.c +64 -0
  119. data/deps/libuv/test/benchmark-udp-pummel.c +238 -0
  120. data/deps/libuv/test/blackhole-server.c +118 -0
  121. data/deps/libuv/test/dns-server.c +329 -0
  122. data/deps/libuv/test/echo-server.c +384 -0
  123. data/deps/libuv/test/fixtures/empty_file +0 -0
  124. data/deps/libuv/test/fixtures/load_error.node +1 -0
  125. data/deps/libuv/test/run-benchmarks.c +64 -0
  126. data/deps/libuv/test/run-tests.c +159 -0
  127. data/deps/libuv/test/runner-unix.c +328 -0
  128. data/deps/libuv/test/runner-unix.h +36 -0
  129. data/deps/libuv/test/runner-win.c +318 -0
  130. data/deps/libuv/test/runner-win.h +43 -0
  131. data/deps/libuv/test/runner.c +394 -0
  132. data/deps/libuv/test/runner.h +165 -0
  133. data/deps/libuv/test/task.h +122 -0
  134. data/deps/libuv/test/test-active.c +83 -0
  135. data/deps/libuv/test/test-async.c +136 -0
  136. data/deps/libuv/test/test-barrier.c +98 -0
  137. data/deps/libuv/test/test-callback-order.c +77 -0
  138. data/deps/libuv/test/test-callback-stack.c +204 -0
  139. data/deps/libuv/test/test-condvar.c +173 -0
  140. data/deps/libuv/test/test-connection-fail.c +150 -0
  141. data/deps/libuv/test/test-cwd-and-chdir.c +64 -0
  142. data/deps/libuv/test/test-delayed-accept.c +189 -0
  143. data/deps/libuv/test/test-dlerror.c +58 -0
  144. data/deps/libuv/test/test-embed.c +136 -0
  145. data/deps/libuv/test/test-error.c +59 -0
  146. data/deps/libuv/test/test-fail-always.c +29 -0
  147. data/deps/libuv/test/test-fs-event.c +504 -0
  148. data/deps/libuv/test/test-fs-poll.c +148 -0
  149. data/deps/libuv/test/test-fs.c +1899 -0
  150. data/deps/libuv/test/test-get-currentexe.c +63 -0
  151. data/deps/libuv/test/test-get-loadavg.c +36 -0
  152. data/deps/libuv/test/test-get-memory.c +38 -0
  153. data/deps/libuv/test/test-getaddrinfo.c +120 -0
  154. data/deps/libuv/test/test-getsockname.c +344 -0
  155. data/deps/libuv/test/test-hrtime.c +54 -0
  156. data/deps/libuv/test/test-idle.c +82 -0
  157. data/deps/libuv/test/test-ipc-send-recv.c +218 -0
  158. data/deps/libuv/test/test-ipc.c +625 -0
  159. data/deps/libuv/test/test-list.h +492 -0
  160. data/deps/libuv/test/test-loop-handles.c +337 -0
  161. data/deps/libuv/test/test-loop-stop.c +73 -0
  162. data/deps/libuv/test/test-multiple-listen.c +103 -0
  163. data/deps/libuv/test/test-mutexes.c +63 -0
  164. data/deps/libuv/test/test-pass-always.c +28 -0
  165. data/deps/libuv/test/test-ping-pong.c +250 -0
  166. data/deps/libuv/test/test-pipe-bind-error.c +144 -0
  167. data/deps/libuv/test/test-pipe-connect-error.c +98 -0
  168. data/deps/libuv/test/test-platform-output.c +87 -0
  169. data/deps/libuv/test/test-poll-close.c +73 -0
  170. data/deps/libuv/test/test-poll.c +575 -0
  171. data/deps/libuv/test/test-process-title.c +49 -0
  172. data/deps/libuv/test/test-ref.c +415 -0
  173. data/deps/libuv/test/test-run-nowait.c +46 -0
  174. data/deps/libuv/test/test-run-once.c +49 -0
  175. data/deps/libuv/test/test-semaphore.c +111 -0
  176. data/deps/libuv/test/test-shutdown-close.c +105 -0
  177. data/deps/libuv/test/test-shutdown-eof.c +184 -0
  178. data/deps/libuv/test/test-signal-multiple-loops.c +270 -0
  179. data/deps/libuv/test/test-signal.c +152 -0
  180. data/deps/libuv/test/test-spawn.c +938 -0
  181. data/deps/libuv/test/test-stdio-over-pipes.c +250 -0
  182. data/deps/libuv/test/test-tcp-bind-error.c +198 -0
  183. data/deps/libuv/test/test-tcp-bind6-error.c +159 -0
  184. data/deps/libuv/test/test-tcp-close-while-connecting.c +81 -0
  185. data/deps/libuv/test/test-tcp-close.c +130 -0
  186. data/deps/libuv/test/test-tcp-connect-error-after-write.c +96 -0
  187. data/deps/libuv/test/test-tcp-connect-error.c +71 -0
  188. data/deps/libuv/test/test-tcp-connect-timeout.c +86 -0
  189. data/deps/libuv/test/test-tcp-connect6-error.c +69 -0
  190. data/deps/libuv/test/test-tcp-flags.c +52 -0
  191. data/deps/libuv/test/test-tcp-open.c +175 -0
  192. data/deps/libuv/test/test-tcp-read-stop.c +73 -0
  193. data/deps/libuv/test/test-tcp-shutdown-after-write.c +132 -0
  194. data/deps/libuv/test/test-tcp-unexpected-read.c +114 -0
  195. data/deps/libuv/test/test-tcp-write-to-half-open-connection.c +136 -0
  196. data/deps/libuv/test/test-tcp-writealot.c +171 -0
  197. data/deps/libuv/test/test-thread.c +183 -0
  198. data/deps/libuv/test/test-threadpool-cancel.c +311 -0
  199. data/deps/libuv/test/test-threadpool.c +77 -0
  200. data/deps/libuv/test/test-timer-again.c +142 -0
  201. data/deps/libuv/test/test-timer.c +266 -0
  202. data/deps/libuv/test/test-tty.c +111 -0
  203. data/deps/libuv/test/test-udp-dgram-too-big.c +87 -0
  204. data/deps/libuv/test/test-udp-ipv6.c +158 -0
  205. data/deps/libuv/test/test-udp-multicast-join.c +140 -0
  206. data/deps/libuv/test/test-udp-multicast-ttl.c +87 -0
  207. data/deps/libuv/test/test-udp-open.c +154 -0
  208. data/deps/libuv/test/test-udp-options.c +87 -0
  209. data/deps/libuv/test/test-udp-send-and-recv.c +210 -0
  210. data/deps/libuv/test/test-util.c +97 -0
  211. data/deps/libuv/test/test-walk-handles.c +78 -0
  212. data/deps/libuv/uv.gyp +431 -0
  213. data/deps/libuv/vcbuild.bat +128 -0
  214. data/ext/rbuv/debug.h +27 -0
  215. data/ext/rbuv/error.c +7 -0
  216. data/ext/rbuv/error.h +10 -0
  217. data/ext/rbuv/extconf.rb +35 -0
  218. data/ext/rbuv/handle.c +40 -0
  219. data/ext/rbuv/handle.h +14 -0
  220. data/ext/rbuv/libuv.mk +12 -0
  221. data/ext/rbuv/loop.c +50 -0
  222. data/ext/rbuv/loop.h +13 -0
  223. data/ext/rbuv/rbuv.c +15 -0
  224. data/ext/rbuv/rbuv.h +27 -0
  225. data/ext/rbuv/timer.c +133 -0
  226. data/ext/rbuv/timer.h +13 -0
  227. data/lib/rbuv/timer.rb +7 -0
  228. data/lib/rbuv/version.rb +1 -1
  229. data/lib/rbuv.rb +24 -2
  230. data/rbuv.gemspec +5 -1
  231. data/spec/spec_helper.rb +22 -0
  232. data/spec/timer_spec.rb +144 -0
  233. metadata +278 -9
@@ -0,0 +1,1857 @@
1
+ /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2
+ *
3
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ * of this software and associated documentation files (the "Software"), to
5
+ * deal in the Software without restriction, including without limitation the
6
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7
+ * sell copies of the Software, and to permit persons to whom the Software is
8
+ * furnished to do so, subject to the following conditions:
9
+ *
10
+ * The above copyright notice and this permission notice shall be included in
11
+ * all copies or substantial portions of the Software.
12
+ *
13
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19
+ * IN THE SOFTWARE.
20
+ */
21
+
22
+ #include <assert.h>
23
+ #include <io.h>
24
+ #include <string.h>
25
+
26
+ #if defined(_MSC_VER) && _MSC_VER < 1600
27
+ # include "uv-private/stdint-msvc2008.h"
28
+ #else
29
+ # include <stdint.h>
30
+ #endif
31
+
32
+ #include "uv.h"
33
+ #include "internal.h"
34
+ #include "handle-inl.h"
35
+ #include "stream-inl.h"
36
+ #include "req-inl.h"
37
+
38
+
39
+ #define UNICODE_REPLACEMENT_CHARACTER (0xfffd)
40
+
41
+ #define ANSI_NORMAL 0x00
42
+ #define ANSI_ESCAPE_SEEN 0x02
43
+ #define ANSI_CSI 0x04
44
+ #define ANSI_ST_CONTROL 0x08
45
+ #define ANSI_IGNORE 0x10
46
+ #define ANSI_IN_ARG 0x20
47
+ #define ANSI_IN_STRING 0x40
48
+ #define ANSI_BACKSLASH_SEEN 0x80
49
+
50
+
51
+ static void uv_tty_update_virtual_window(CONSOLE_SCREEN_BUFFER_INFO* info);
52
+
53
+
54
+ /* Null uv_buf_t */
55
+ static const uv_buf_t uv_null_buf_ = { 0, NULL };
56
+
57
+
58
+ /*
59
+ * The console virtual window.
60
+ *
61
+ * Normally cursor movement in windows is relative to the console screen buffer,
62
+ * e.g. the application is allowed to overwrite the 'history'. This is very
63
+ * inconvenient, it makes absolute cursor movement pretty useless. There is
64
+ * also the concept of 'client rect' which is defined by the actual size of
65
+ * the console window and the scroll position of the screen buffer, but it's
66
+ * very volatile because it changes when the user scrolls.
67
+ *
68
+ * To make cursor movement behave sensibly we define a virtual window to which
69
+ * cursor movement is confined. The virtual window is always as wide as the
70
+ * console screen buffer, but it's height is defined by the size of the
71
+ * console window. The top of the virtual window aligns with the position
72
+ * of the caret when the first stdout/err handle is created, unless that would
73
+ * mean that it would extend beyond the bottom of the screen buffer - in that
74
+ * that case it's located as far down as possible.
75
+ *
76
+ * When the user writes a long text or many newlines, such that the output
77
+ * reaches beyond the bottom of the virtual window, the virtual window is
78
+ * shifted downwards, but not resized.
79
+ *
80
+ * Since all tty i/o happens on the same console, this window is shared
81
+ * between all stdout/stderr handles.
82
+ */
83
+
84
+ static int uv_tty_virtual_offset = -1;
85
+ static int uv_tty_virtual_height = -1;
86
+ static int uv_tty_virtual_width = -1;
87
+
88
+ static CRITICAL_SECTION uv_tty_output_lock;
89
+
90
+ static HANDLE uv_tty_output_handle = INVALID_HANDLE_VALUE;
91
+
92
+
93
+ void uv_console_init() {
94
+ InitializeCriticalSection(&uv_tty_output_lock);
95
+ }
96
+
97
+
98
+ int uv_tty_init(uv_loop_t* loop, uv_tty_t* tty, uv_file fd, int readable) {
99
+ HANDLE handle = INVALID_HANDLE_VALUE;
100
+ CONSOLE_SCREEN_BUFFER_INFO screen_buffer_info;
101
+
102
+ handle = (HANDLE) _get_osfhandle(fd);
103
+ if (handle == INVALID_HANDLE_VALUE) {
104
+ uv__set_artificial_error(loop, UV_EBADF);
105
+ return -1;
106
+ }
107
+
108
+ if (!readable) {
109
+ /* Obtain the screen buffer info with the output handle. */
110
+ if (!GetConsoleScreenBufferInfo(handle, &screen_buffer_info)) {
111
+ uv__set_sys_error(loop, GetLastError());
112
+ return -1;
113
+ }
114
+
115
+ /* Obtain the the tty_output_lock because the virtual window state is */
116
+ /* shared between all uv_tty_t handles. */
117
+ EnterCriticalSection(&uv_tty_output_lock);
118
+
119
+ /* Store the global tty output handle. This handle is used by TTY read */
120
+ /* streams to update the virtual window when a CONSOLE_BUFFER_SIZE_EVENT */
121
+ /* is received. */
122
+ uv_tty_output_handle = handle;
123
+
124
+ uv_tty_update_virtual_window(&screen_buffer_info);
125
+
126
+ LeaveCriticalSection(&uv_tty_output_lock);
127
+ }
128
+
129
+
130
+ uv_stream_init(loop, (uv_stream_t*) tty, UV_TTY);
131
+ uv_connection_init((uv_stream_t*) tty);
132
+
133
+ tty->handle = handle;
134
+ tty->reqs_pending = 0;
135
+ tty->flags |= UV_HANDLE_BOUND;
136
+
137
+ if (readable) {
138
+ /* Initialize TTY input specific fields. */
139
+ tty->flags |= UV_HANDLE_TTY_READABLE | UV_HANDLE_READABLE;
140
+ tty->read_line_handle = NULL;
141
+ tty->read_line_buffer = uv_null_buf_;
142
+ tty->read_raw_wait = NULL;
143
+
144
+ /* Init keycode-to-vt100 mapper state. */
145
+ tty->last_key_len = 0;
146
+ tty->last_key_offset = 0;
147
+ tty->last_utf16_high_surrogate = 0;
148
+ memset(&tty->last_input_record, 0, sizeof tty->last_input_record);
149
+ } else {
150
+ /* TTY output specific fields. */
151
+ tty->flags |= UV_HANDLE_WRITABLE;
152
+
153
+ /* Init utf8-to-utf16 conversion state. */
154
+ tty->utf8_bytes_left = 0;
155
+ tty->utf8_codepoint = 0;
156
+
157
+ /* Initialize eol conversion state */
158
+ tty->previous_eol = 0;
159
+
160
+ /* Init ANSI parser state. */
161
+ tty->ansi_parser_state = ANSI_NORMAL;
162
+ }
163
+
164
+ return 0;
165
+ }
166
+
167
+
168
+ int uv_tty_set_mode(uv_tty_t* tty, int mode) {
169
+ DWORD flags;
170
+ unsigned char was_reading;
171
+ uv_alloc_cb alloc_cb;
172
+ uv_read_cb read_cb;
173
+
174
+ if (!(tty->flags & UV_HANDLE_TTY_READABLE)) {
175
+ uv__set_artificial_error(tty->loop, UV_EINVAL);
176
+ return -1;
177
+ }
178
+
179
+ if (!!mode == !!(tty->flags & UV_HANDLE_TTY_RAW)) {
180
+ return 0;
181
+ }
182
+
183
+ if (mode) {
184
+ /* Raw input */
185
+ flags = ENABLE_WINDOW_INPUT;
186
+ } else {
187
+ /* Line-buffered mode. */
188
+ flags = ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT;
189
+ }
190
+
191
+ if (!SetConsoleMode(tty->handle, flags)) {
192
+ uv__set_sys_error(tty->loop, GetLastError());
193
+ return -1;
194
+ }
195
+
196
+ /* If currently reading, stop, and restart reading. */
197
+ if (tty->flags & UV_HANDLE_READING) {
198
+ was_reading = 1;
199
+ alloc_cb = tty->alloc_cb;
200
+ read_cb = tty->read_cb;
201
+
202
+ if (was_reading && uv_tty_read_stop(tty) != 0) {
203
+ return -1;
204
+ }
205
+ } else {
206
+ was_reading = 0;
207
+ }
208
+
209
+ /* Update flag. */
210
+ tty->flags &= ~UV_HANDLE_TTY_RAW;
211
+ tty->flags |= mode ? UV_HANDLE_TTY_RAW : 0;
212
+
213
+ /* If we just stopped reading, restart. */
214
+ if (was_reading && uv_tty_read_start(tty, alloc_cb, read_cb) != 0) {
215
+ return -1;
216
+ }
217
+
218
+ return 0;
219
+ }
220
+
221
+
222
+ int uv_is_tty(uv_file file) {
223
+ DWORD result;
224
+ return GetConsoleMode((HANDLE) _get_osfhandle(file), &result) != 0;
225
+ }
226
+
227
+
228
+ int uv_tty_get_winsize(uv_tty_t* tty, int* width, int* height) {
229
+ CONSOLE_SCREEN_BUFFER_INFO info;
230
+
231
+ if (!GetConsoleScreenBufferInfo(tty->handle, &info)) {
232
+ uv__set_sys_error(tty->loop, GetLastError());
233
+ return -1;
234
+ }
235
+
236
+ EnterCriticalSection(&uv_tty_output_lock);
237
+ uv_tty_update_virtual_window(&info);
238
+ LeaveCriticalSection(&uv_tty_output_lock);
239
+
240
+ *width = uv_tty_virtual_width;
241
+ *height = uv_tty_virtual_height;
242
+
243
+ return 0;
244
+ }
245
+
246
+
247
+ static void CALLBACK uv_tty_post_raw_read(void* data, BOOLEAN didTimeout) {
248
+ uv_loop_t* loop;
249
+ uv_tty_t* handle;
250
+ uv_req_t* req;
251
+
252
+ assert(data);
253
+ assert(!didTimeout);
254
+
255
+ req = (uv_req_t*) data;
256
+ handle = (uv_tty_t*) req->data;
257
+ loop = handle->loop;
258
+
259
+ UnregisterWait(handle->read_raw_wait);
260
+ handle->read_raw_wait = NULL;
261
+
262
+ SET_REQ_SUCCESS(req);
263
+ POST_COMPLETION_FOR_REQ(loop, req);
264
+ }
265
+
266
+
267
+ static void uv_tty_queue_read_raw(uv_loop_t* loop, uv_tty_t* handle) {
268
+ uv_read_t* req;
269
+ BOOL r;
270
+
271
+ assert(handle->flags & UV_HANDLE_READING);
272
+ assert(!(handle->flags & UV_HANDLE_READ_PENDING));
273
+
274
+ assert(handle->handle && handle->handle != INVALID_HANDLE_VALUE);
275
+
276
+ handle->read_line_buffer = uv_null_buf_;
277
+
278
+ req = &handle->read_req;
279
+ memset(&req->overlapped, 0, sizeof(req->overlapped));
280
+
281
+ r = RegisterWaitForSingleObject(&handle->read_raw_wait,
282
+ handle->handle,
283
+ uv_tty_post_raw_read,
284
+ (void*) req,
285
+ INFINITE,
286
+ WT_EXECUTEINWAITTHREAD | WT_EXECUTEONLYONCE);
287
+ if (!r) {
288
+ handle->read_raw_wait = NULL;
289
+ SET_REQ_ERROR(req, GetLastError());
290
+ uv_insert_pending_req(loop, (uv_req_t*)req);
291
+ }
292
+
293
+ handle->flags |= UV_HANDLE_READ_PENDING;
294
+ handle->reqs_pending++;
295
+ }
296
+
297
+
298
+ static DWORD CALLBACK uv_tty_line_read_thread(void* data) {
299
+ uv_loop_t* loop;
300
+ uv_tty_t* handle;
301
+ uv_req_t* req;
302
+ DWORD bytes, read_bytes;
303
+
304
+ assert(data);
305
+
306
+ req = (uv_req_t*) data;
307
+ handle = (uv_tty_t*) req->data;
308
+ loop = handle->loop;
309
+
310
+ assert(handle->read_line_buffer.base != NULL);
311
+ assert(handle->read_line_buffer.len > 0);
312
+
313
+ /* ReadConsole can't handle big buffers. */
314
+ if (handle->read_line_buffer.len < 8192) {
315
+ bytes = handle->read_line_buffer.len;
316
+ } else {
317
+ bytes = 8192;
318
+ }
319
+
320
+ /* Todo: Unicode */
321
+ if (ReadConsoleA(handle->read_line_handle,
322
+ (void*) handle->read_line_buffer.base,
323
+ bytes,
324
+ &read_bytes,
325
+ NULL)) {
326
+ SET_REQ_SUCCESS(req);
327
+ req->overlapped.InternalHigh = read_bytes;
328
+ } else {
329
+ SET_REQ_ERROR(req, GetLastError());
330
+ }
331
+
332
+ POST_COMPLETION_FOR_REQ(loop, req);
333
+ return 0;
334
+ }
335
+
336
+
337
+ static void uv_tty_queue_read_line(uv_loop_t* loop, uv_tty_t* handle) {
338
+ uv_read_t* req;
339
+ BOOL r;
340
+
341
+ assert(handle->flags & UV_HANDLE_READING);
342
+ assert(!(handle->flags & UV_HANDLE_READ_PENDING));
343
+ assert(handle->handle && handle->handle != INVALID_HANDLE_VALUE);
344
+
345
+ req = &handle->read_req;
346
+ memset(&req->overlapped, 0, sizeof(req->overlapped));
347
+
348
+ handle->read_line_buffer = handle->alloc_cb((uv_handle_t*) handle, 8192);
349
+ assert(handle->read_line_buffer.base != NULL);
350
+ assert(handle->read_line_buffer.len > 0);
351
+
352
+ /* Duplicate the console handle, so if we want to cancel the read, we can */
353
+ /* just close this handle duplicate. */
354
+ if (handle->read_line_handle == NULL) {
355
+ HANDLE this_process = GetCurrentProcess();
356
+ r = DuplicateHandle(this_process,
357
+ handle->handle,
358
+ this_process,
359
+ &handle->read_line_handle,
360
+ 0,
361
+ 0,
362
+ DUPLICATE_SAME_ACCESS);
363
+ if (!r) {
364
+ handle->read_line_handle = NULL;
365
+ SET_REQ_ERROR(req, GetLastError());
366
+ uv_insert_pending_req(loop, (uv_req_t*)req);
367
+ goto out;
368
+ }
369
+ }
370
+
371
+ r = QueueUserWorkItem(uv_tty_line_read_thread,
372
+ (void*) req,
373
+ WT_EXECUTELONGFUNCTION);
374
+ if (!r) {
375
+ SET_REQ_ERROR(req, GetLastError());
376
+ uv_insert_pending_req(loop, (uv_req_t*)req);
377
+ }
378
+
379
+ out:
380
+ handle->flags |= UV_HANDLE_READ_PENDING;
381
+ handle->reqs_pending++;
382
+ }
383
+
384
+
385
+ static void uv_tty_queue_read(uv_loop_t* loop, uv_tty_t* handle) {
386
+ if (handle->flags & UV_HANDLE_TTY_RAW) {
387
+ uv_tty_queue_read_raw(loop, handle);
388
+ } else {
389
+ uv_tty_queue_read_line(loop, handle);
390
+ }
391
+ }
392
+
393
+
394
+ static const char* get_vt100_fn_key(DWORD code, char shift, char ctrl,
395
+ size_t* len) {
396
+ #define VK_CASE(vk, normal_str, shift_str, ctrl_str, shift_ctrl_str) \
397
+ case (vk): \
398
+ if (shift && ctrl) { \
399
+ *len = sizeof shift_ctrl_str; \
400
+ return "\033" shift_ctrl_str; \
401
+ } else if (shift) { \
402
+ *len = sizeof shift_str ; \
403
+ return "\033" shift_str; \
404
+ } else if (ctrl) { \
405
+ *len = sizeof ctrl_str; \
406
+ return "\033" ctrl_str; \
407
+ } else { \
408
+ *len = sizeof normal_str; \
409
+ return "\033" normal_str; \
410
+ }
411
+
412
+ switch (code) {
413
+ /* These mappings are the same as Cygwin's. Unmodified and alt-modified */
414
+ /* keypad keys comply with linux console, modifiers comply with xterm */
415
+ /* modifier usage. F1..f12 and shift-f1..f10 comply with linux console, */
416
+ /* f6..f12 with and without modifiers comply with rxvt. */
417
+ VK_CASE(VK_INSERT, "[2~", "[2;2~", "[2;5~", "[2;6~")
418
+ VK_CASE(VK_END, "[4~", "[4;2~", "[4;5~", "[4;6~")
419
+ VK_CASE(VK_DOWN, "[B", "[1;2B", "[1;5B", "[1;6B")
420
+ VK_CASE(VK_NEXT, "[6~", "[6;2~", "[6;5~", "[6;6~")
421
+ VK_CASE(VK_LEFT, "[D", "[1;2D", "[1;5D", "[1;6D")
422
+ VK_CASE(VK_CLEAR, "[G", "[1;2G", "[1;5G", "[1;6G")
423
+ VK_CASE(VK_RIGHT, "[C", "[1;2C", "[1;5C", "[1;6C")
424
+ VK_CASE(VK_UP, "[A", "[1;2A", "[1;5A", "[1;6A")
425
+ VK_CASE(VK_HOME, "[1~", "[1;2~", "[1;5~", "[1;6~")
426
+ VK_CASE(VK_PRIOR, "[5~", "[5;2~", "[5;5~", "[5;6~")
427
+ VK_CASE(VK_DELETE, "[3~", "[3;2~", "[3;5~", "[3;6~")
428
+ VK_CASE(VK_NUMPAD0, "[2~", "[2;2~", "[2;5~", "[2;6~")
429
+ VK_CASE(VK_NUMPAD1, "[4~", "[4;2~", "[4;5~", "[4;6~")
430
+ VK_CASE(VK_NUMPAD2, "[B", "[1;2B", "[1;5B", "[1;6B")
431
+ VK_CASE(VK_NUMPAD3, "[6~", "[6;2~", "[6;5~", "[6;6~")
432
+ VK_CASE(VK_NUMPAD4, "[D", "[1;2D", "[1;5D", "[1;6D")
433
+ VK_CASE(VK_NUMPAD5, "[G", "[1;2G", "[1;5G", "[1;6G")
434
+ VK_CASE(VK_NUMPAD6, "[C", "[1;2C", "[1;5C", "[1;6C")
435
+ VK_CASE(VK_NUMPAD7, "[A", "[1;2A", "[1;5A", "[1;6A")
436
+ VK_CASE(VK_NUMPAD8, "[1~", "[1;2~", "[1;5~", "[1;6~")
437
+ VK_CASE(VK_NUMPAD9, "[5~", "[5;2~", "[5;5~", "[5;6~")
438
+ VK_CASE(VK_DECIMAL, "[3~", "[3;2~", "[3;5~", "[3;6~")
439
+ VK_CASE(VK_F1, "[[A", "[23~", "[11^", "[23^" )
440
+ VK_CASE(VK_F2, "[[B", "[24~", "[12^", "[24^" )
441
+ VK_CASE(VK_F3, "[[C", "[25~", "[13^", "[25^" )
442
+ VK_CASE(VK_F4, "[[D", "[26~", "[14^", "[26^" )
443
+ VK_CASE(VK_F5, "[[E", "[28~", "[15^", "[28^" )
444
+ VK_CASE(VK_F6, "[17~", "[29~", "[17^", "[29^" )
445
+ VK_CASE(VK_F7, "[18~", "[31~", "[18^", "[31^" )
446
+ VK_CASE(VK_F8, "[19~", "[32~", "[19^", "[32^" )
447
+ VK_CASE(VK_F9, "[20~", "[33~", "[20^", "[33^" )
448
+ VK_CASE(VK_F10, "[21~", "[34~", "[21^", "[34^" )
449
+ VK_CASE(VK_F11, "[23~", "[23$", "[23^", "[23@" )
450
+ VK_CASE(VK_F12, "[24~", "[24$", "[24^", "[24@" )
451
+
452
+ default:
453
+ *len = 0;
454
+ return NULL;
455
+ }
456
+ #undef VK_CASE
457
+ }
458
+
459
+
460
+ void uv_process_tty_read_raw_req(uv_loop_t* loop, uv_tty_t* handle,
461
+ uv_req_t* req) {
462
+ /* Shortcut for handle->last_input_record.Event.KeyEvent. */
463
+ #define KEV handle->last_input_record.Event.KeyEvent
464
+
465
+ DWORD records_left, records_read;
466
+ uv_buf_t buf;
467
+ off_t buf_used;
468
+
469
+ assert(handle->type == UV_TTY);
470
+ assert(handle->flags & UV_HANDLE_TTY_READABLE);
471
+ handle->flags &= ~UV_HANDLE_READ_PENDING;
472
+
473
+ if (!(handle->flags & UV_HANDLE_READING) ||
474
+ !(handle->flags & UV_HANDLE_TTY_RAW)) {
475
+ goto out;
476
+ }
477
+
478
+ if (!REQ_SUCCESS(req)) {
479
+ /* An error occurred while waiting for the event. */
480
+ if ((handle->flags & UV_HANDLE_READING)) {
481
+ handle->flags &= ~UV_HANDLE_READING;
482
+ uv__set_sys_error(loop, GET_REQ_ERROR(req));
483
+ handle->read_cb((uv_stream_t*)handle, -1, uv_null_buf_);
484
+ }
485
+ goto out;
486
+ }
487
+
488
+ /* Fetch the number of events */
489
+ if (!GetNumberOfConsoleInputEvents(handle->handle, &records_left)) {
490
+ uv__set_sys_error(loop, GetLastError());
491
+ handle->flags &= ~UV_HANDLE_READING;
492
+ DECREASE_ACTIVE_COUNT(loop, handle);
493
+ handle->read_cb((uv_stream_t*)handle, -1, uv_null_buf_);
494
+ goto out;
495
+ }
496
+
497
+ /* Windows sends a lot of events that we're not interested in, so buf */
498
+ /* will be allocated on demand, when there's actually something to emit. */
499
+ buf = uv_null_buf_;
500
+ buf_used = 0;
501
+
502
+ while ((records_left > 0 || handle->last_key_len > 0) &&
503
+ (handle->flags & UV_HANDLE_READING)) {
504
+ if (handle->last_key_len == 0) {
505
+ /* Read the next input record */
506
+ if (!ReadConsoleInputW(handle->handle,
507
+ &handle->last_input_record,
508
+ 1,
509
+ &records_read)) {
510
+ uv__set_sys_error(loop, GetLastError());
511
+ handle->flags &= ~UV_HANDLE_READING;
512
+ DECREASE_ACTIVE_COUNT(loop, handle);
513
+ handle->read_cb((uv_stream_t*) handle, -1, buf);
514
+ goto out;
515
+ }
516
+ records_left--;
517
+
518
+ /* If the window was resized, recompute the virtual window size. This */
519
+ /* will trigger a SIGWINCH signal if the window size changed in an */
520
+ /* way that matters to libuv. */
521
+ if (handle->last_input_record.EventType == WINDOW_BUFFER_SIZE_EVENT) {
522
+ CONSOLE_SCREEN_BUFFER_INFO info;
523
+
524
+ EnterCriticalSection(&uv_tty_output_lock);
525
+
526
+ if (uv_tty_output_handle != INVALID_HANDLE_VALUE &&
527
+ GetConsoleScreenBufferInfo(uv_tty_output_handle, &info)) {
528
+ uv_tty_update_virtual_window(&info);
529
+ }
530
+
531
+ LeaveCriticalSection(&uv_tty_output_lock);
532
+
533
+ continue;
534
+ }
535
+
536
+ /* Ignore other events that are not key or resize events. */
537
+ if (handle->last_input_record.EventType != KEY_EVENT) {
538
+ continue;
539
+ }
540
+
541
+ /* Ignore keyup events, unless the left alt key was held and a valid */
542
+ /* unicode character was emitted. */
543
+ if (!KEV.bKeyDown && !(((KEV.dwControlKeyState & LEFT_ALT_PRESSED) ||
544
+ KEV.wVirtualKeyCode==VK_MENU) && KEV.uChar.UnicodeChar != 0)) {
545
+ continue;
546
+ }
547
+
548
+ /* Ignore keypresses to numpad number keys if the left alt is held */
549
+ /* because the user is composing a character, or windows simulating */
550
+ /* this. */
551
+ if ((KEV.dwControlKeyState & LEFT_ALT_PRESSED) &&
552
+ !(KEV.dwControlKeyState & ENHANCED_KEY) &&
553
+ (KEV.wVirtualKeyCode == VK_INSERT ||
554
+ KEV.wVirtualKeyCode == VK_END ||
555
+ KEV.wVirtualKeyCode == VK_DOWN ||
556
+ KEV.wVirtualKeyCode == VK_NEXT ||
557
+ KEV.wVirtualKeyCode == VK_LEFT ||
558
+ KEV.wVirtualKeyCode == VK_CLEAR ||
559
+ KEV.wVirtualKeyCode == VK_RIGHT ||
560
+ KEV.wVirtualKeyCode == VK_HOME ||
561
+ KEV.wVirtualKeyCode == VK_UP ||
562
+ KEV.wVirtualKeyCode == VK_PRIOR ||
563
+ KEV.wVirtualKeyCode == VK_NUMPAD0 ||
564
+ KEV.wVirtualKeyCode == VK_NUMPAD1 ||
565
+ KEV.wVirtualKeyCode == VK_NUMPAD2 ||
566
+ KEV.wVirtualKeyCode == VK_NUMPAD3 ||
567
+ KEV.wVirtualKeyCode == VK_NUMPAD4 ||
568
+ KEV.wVirtualKeyCode == VK_NUMPAD5 ||
569
+ KEV.wVirtualKeyCode == VK_NUMPAD6 ||
570
+ KEV.wVirtualKeyCode == VK_NUMPAD7 ||
571
+ KEV.wVirtualKeyCode == VK_NUMPAD8 ||
572
+ KEV.wVirtualKeyCode == VK_NUMPAD9)) {
573
+ continue;
574
+ }
575
+
576
+ if (KEV.uChar.UnicodeChar != 0) {
577
+ int prefix_len, char_len;
578
+
579
+ /* Character key pressed */
580
+ if (KEV.uChar.UnicodeChar >= 0xD800 &&
581
+ KEV.uChar.UnicodeChar < 0xDC00) {
582
+ /* UTF-16 high surrogate */
583
+ handle->last_utf16_high_surrogate = KEV.uChar.UnicodeChar;
584
+ continue;
585
+ }
586
+
587
+ /* Prefix with \u033 if alt was held, but alt was not used as part */
588
+ /* a compose sequence. */
589
+ if ((KEV.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED))
590
+ && !(KEV.dwControlKeyState & (LEFT_CTRL_PRESSED |
591
+ RIGHT_CTRL_PRESSED)) && KEV.bKeyDown) {
592
+ handle->last_key[0] = '\033';
593
+ prefix_len = 1;
594
+ } else {
595
+ prefix_len = 0;
596
+ }
597
+
598
+ if (KEV.uChar.UnicodeChar >= 0xDC00 &&
599
+ KEV.uChar.UnicodeChar < 0xE000) {
600
+ /* UTF-16 surrogate pair */
601
+ WCHAR utf16_buffer[2] = { handle->last_utf16_high_surrogate,
602
+ KEV.uChar.UnicodeChar};
603
+ char_len = WideCharToMultiByte(CP_UTF8,
604
+ 0,
605
+ utf16_buffer,
606
+ 2,
607
+ &handle->last_key[prefix_len],
608
+ sizeof handle->last_key,
609
+ NULL,
610
+ NULL);
611
+ } else {
612
+ /* Single UTF-16 character */
613
+ char_len = WideCharToMultiByte(CP_UTF8,
614
+ 0,
615
+ &KEV.uChar.UnicodeChar,
616
+ 1,
617
+ &handle->last_key[prefix_len],
618
+ sizeof handle->last_key,
619
+ NULL,
620
+ NULL);
621
+ }
622
+
623
+ /* Whatever happened, the last character wasn't a high surrogate. */
624
+ handle->last_utf16_high_surrogate = 0;
625
+
626
+ /* If the utf16 character(s) couldn't be converted something must */
627
+ /* be wrong. */
628
+ if (!char_len) {
629
+ uv__set_sys_error(loop, GetLastError());
630
+ handle->flags &= ~UV_HANDLE_READING;
631
+ DECREASE_ACTIVE_COUNT(loop, handle);
632
+ handle->read_cb((uv_stream_t*) handle, -1, buf);
633
+ goto out;
634
+ }
635
+
636
+ handle->last_key_len = (unsigned char) (prefix_len + char_len);
637
+ handle->last_key_offset = 0;
638
+ continue;
639
+
640
+ } else {
641
+ /* Function key pressed */
642
+ const char* vt100;
643
+ size_t prefix_len, vt100_len;
644
+
645
+ vt100 = get_vt100_fn_key(KEV.wVirtualKeyCode,
646
+ !!(KEV.dwControlKeyState & SHIFT_PRESSED),
647
+ !!(KEV.dwControlKeyState & (
648
+ LEFT_CTRL_PRESSED |
649
+ RIGHT_CTRL_PRESSED)),
650
+ &vt100_len);
651
+
652
+ /* If we were unable to map to a vt100 sequence, just ignore. */
653
+ if (!vt100) {
654
+ continue;
655
+ }
656
+
657
+ /* Prefix with \x033 when the alt key was held. */
658
+ if (KEV.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
659
+ handle->last_key[0] = '\033';
660
+ prefix_len = 1;
661
+ } else {
662
+ prefix_len = 0;
663
+ }
664
+
665
+ /* Copy the vt100 sequence to the handle buffer. */
666
+ assert(prefix_len + vt100_len < sizeof handle->last_key);
667
+ memcpy(&handle->last_key[prefix_len], vt100, vt100_len);
668
+
669
+ handle->last_key_len = (unsigned char) (prefix_len + vt100_len);
670
+ handle->last_key_offset = 0;
671
+ continue;
672
+ }
673
+ } else {
674
+ /* Copy any bytes left from the last keypress to the user buffer. */
675
+ if (handle->last_key_offset < handle->last_key_len) {
676
+ /* Allocate a buffer if needed */
677
+ if (buf_used == 0) {
678
+ buf = handle->alloc_cb((uv_handle_t*) handle, 1024);
679
+ }
680
+
681
+ buf.base[buf_used++] = handle->last_key[handle->last_key_offset++];
682
+
683
+ /* If the buffer is full, emit it */
684
+ if (buf_used == buf.len) {
685
+ handle->read_cb((uv_stream_t*) handle, buf_used, buf);
686
+ buf = uv_null_buf_;
687
+ buf_used = 0;
688
+ }
689
+
690
+ continue;
691
+ }
692
+
693
+ /* Apply dwRepeat from the last input record. */
694
+ if (--KEV.wRepeatCount > 0) {
695
+ handle->last_key_offset = 0;
696
+ continue;
697
+ }
698
+
699
+ handle->last_key_len = 0;
700
+ continue;
701
+ }
702
+ }
703
+
704
+ /* Send the buffer back to the user */
705
+ if (buf_used > 0) {
706
+ handle->read_cb((uv_stream_t*) handle, buf_used, buf);
707
+ }
708
+
709
+ out:
710
+ /* Wait for more input events. */
711
+ if ((handle->flags & UV_HANDLE_READING) &&
712
+ !(handle->flags & UV_HANDLE_READ_PENDING)) {
713
+ uv_tty_queue_read(loop, handle);
714
+ }
715
+
716
+ DECREASE_PENDING_REQ_COUNT(handle);
717
+
718
+ #undef KEV
719
+ }
720
+
721
+
722
+
723
+ void uv_process_tty_read_line_req(uv_loop_t* loop, uv_tty_t* handle,
724
+ uv_req_t* req) {
725
+ uv_buf_t buf;
726
+
727
+ assert(handle->type == UV_TTY);
728
+ assert(handle->flags & UV_HANDLE_TTY_READABLE);
729
+
730
+ buf = handle->read_line_buffer;
731
+
732
+ handle->flags &= ~UV_HANDLE_READ_PENDING;
733
+ handle->read_line_buffer = uv_null_buf_;
734
+
735
+ if (!REQ_SUCCESS(req)) {
736
+ /* Read was not successful */
737
+ if ((handle->flags & UV_HANDLE_READING) &&
738
+ handle->read_line_handle != NULL) {
739
+ /* Real error */
740
+ handle->flags &= ~UV_HANDLE_READING;
741
+ DECREASE_ACTIVE_COUNT(loop, handle);
742
+ uv__set_sys_error(loop, GET_REQ_ERROR(req));
743
+ handle->read_cb((uv_stream_t*) handle, -1, buf);
744
+ } else {
745
+ /* The read was cancelled, or whatever we don't care */
746
+ uv__set_sys_error(loop, WSAEWOULDBLOCK); /* maps to UV_EAGAIN */
747
+ handle->read_cb((uv_stream_t*) handle, 0, buf);
748
+ }
749
+
750
+ } else {
751
+ /* Read successful */
752
+ /* TODO: read unicode, convert to utf-8 */
753
+ DWORD bytes = req->overlapped.InternalHigh;
754
+ if (bytes == 0) {
755
+ uv__set_sys_error(loop, WSAEWOULDBLOCK); /* maps to UV_EAGAIN */
756
+ }
757
+ handle->read_cb((uv_stream_t*) handle, bytes, buf);
758
+ }
759
+
760
+ /* Wait for more input events. */
761
+ if ((handle->flags & UV_HANDLE_READING) &&
762
+ !(handle->flags & UV_HANDLE_READ_PENDING)) {
763
+ uv_tty_queue_read(loop, handle);
764
+ }
765
+
766
+ DECREASE_PENDING_REQ_COUNT(handle);
767
+ }
768
+
769
+
770
+ void uv_process_tty_read_req(uv_loop_t* loop, uv_tty_t* handle,
771
+ uv_req_t* req) {
772
+ assert(handle->type == UV_TTY);
773
+ assert(handle->flags & UV_HANDLE_TTY_READABLE);
774
+
775
+ /* If the read_line_buffer member is zero, it must have been an raw read. */
776
+ /* Otherwise it was a line-buffered read. */
777
+ /* FIXME: This is quite obscure. Use a flag or something. */
778
+ if (handle->read_line_buffer.len == 0) {
779
+ uv_process_tty_read_raw_req(loop, handle, req);
780
+ } else {
781
+ uv_process_tty_read_line_req(loop, handle, req);
782
+ }
783
+ }
784
+
785
+
786
+ int uv_tty_read_start(uv_tty_t* handle, uv_alloc_cb alloc_cb,
787
+ uv_read_cb read_cb) {
788
+ uv_loop_t* loop = handle->loop;
789
+
790
+ if (!(handle->flags & UV_HANDLE_TTY_READABLE)) {
791
+ uv__set_artificial_error(handle->loop, UV_EINVAL);
792
+ return -1;
793
+ }
794
+
795
+ handle->flags |= UV_HANDLE_READING;
796
+ INCREASE_ACTIVE_COUNT(loop, handle);
797
+ handle->read_cb = read_cb;
798
+ handle->alloc_cb = alloc_cb;
799
+
800
+ /* If reading was stopped and then started again, there could still be a */
801
+ /* read request pending. */
802
+ if (handle->flags & UV_HANDLE_READ_PENDING) {
803
+ return 0;
804
+ }
805
+
806
+ /* Maybe the user stopped reading half-way while processing key events. */
807
+ /* Short-circuit if this could be the case. */
808
+ if (handle->last_key_len > 0) {
809
+ SET_REQ_SUCCESS(&handle->read_req);
810
+ uv_insert_pending_req(handle->loop, (uv_req_t*) &handle->read_req);
811
+ return 0;
812
+ }
813
+
814
+ uv_tty_queue_read(loop, handle);
815
+
816
+ return 0;
817
+ }
818
+
819
+
820
+ int uv_tty_read_stop(uv_tty_t* handle) {
821
+ uv_loop_t* loop = handle->loop;
822
+
823
+ handle->flags &= ~UV_HANDLE_READING;
824
+ DECREASE_ACTIVE_COUNT(loop, handle);
825
+
826
+ /* Cancel raw read */
827
+ if ((handle->flags & UV_HANDLE_READ_PENDING) &&
828
+ (handle->flags & UV_HANDLE_TTY_RAW)) {
829
+ /* Write some bullshit event to force the console wait to return. */
830
+ INPUT_RECORD record;
831
+ DWORD written;
832
+ memset(&record, 0, sizeof record);
833
+ if (!WriteConsoleInputW(handle->handle, &record, 1, &written)) {
834
+ uv__set_sys_error(loop, GetLastError());
835
+ return -1;
836
+ }
837
+ }
838
+
839
+ /* Cancel line-buffered read */
840
+ if (handle->read_line_handle != NULL) {
841
+ /* Closing this handle will cancel the ReadConsole operation */
842
+ CloseHandle(handle->read_line_handle);
843
+ handle->read_line_handle = NULL;
844
+ }
845
+
846
+
847
+ return 0;
848
+ }
849
+
850
+
851
+ static void uv_tty_update_virtual_window(CONSOLE_SCREEN_BUFFER_INFO* info) {
852
+ int old_virtual_width = uv_tty_virtual_width;
853
+ int old_virtual_height = uv_tty_virtual_height;
854
+
855
+ uv_tty_virtual_width = info->dwSize.X;
856
+ uv_tty_virtual_height = info->srWindow.Bottom - info->srWindow.Top + 1;
857
+
858
+ /* Recompute virtual window offset row. */
859
+ if (uv_tty_virtual_offset == -1) {
860
+ uv_tty_virtual_offset = info->dwCursorPosition.Y;
861
+ } else if (uv_tty_virtual_offset < info->dwCursorPosition.Y -
862
+ uv_tty_virtual_height + 1) {
863
+ /* If suddenly find the cursor outside of the virtual window, it must */
864
+ /* have somehow scrolled. Update the virtual window offset. */
865
+ uv_tty_virtual_offset = info->dwCursorPosition.Y -
866
+ uv_tty_virtual_height + 1;
867
+ }
868
+ if (uv_tty_virtual_offset + uv_tty_virtual_height > info->dwSize.Y) {
869
+ uv_tty_virtual_offset = info->dwSize.Y - uv_tty_virtual_height;
870
+ }
871
+ if (uv_tty_virtual_offset < 0) {
872
+ uv_tty_virtual_offset = 0;
873
+ }
874
+
875
+ /* If the virtual window size changed, emit a SIGWINCH signal. Don't emit */
876
+ /* if this was the first time the virtual window size was computed. */
877
+ if (old_virtual_width != -1 && old_virtual_height != -1 &&
878
+ (uv_tty_virtual_width != old_virtual_width ||
879
+ uv_tty_virtual_height != old_virtual_height)) {
880
+ uv__signal_dispatch(SIGWINCH);
881
+ }
882
+ }
883
+
884
+
885
+ static COORD uv_tty_make_real_coord(uv_tty_t* handle,
886
+ CONSOLE_SCREEN_BUFFER_INFO* info, int x, unsigned char x_relative, int y,
887
+ unsigned char y_relative) {
888
+ COORD result;
889
+
890
+ uv_tty_update_virtual_window(info);
891
+
892
+ /* Adjust y position */
893
+ if (y_relative) {
894
+ y = info->dwCursorPosition.Y + y;
895
+ } else {
896
+ y = uv_tty_virtual_offset + y;
897
+ }
898
+ /* Clip y to virtual client rectangle */
899
+ if (y < uv_tty_virtual_offset) {
900
+ y = uv_tty_virtual_offset;
901
+ } else if (y >= uv_tty_virtual_offset + uv_tty_virtual_height) {
902
+ y = uv_tty_virtual_offset + uv_tty_virtual_height - 1;
903
+ }
904
+
905
+ /* Adjust x */
906
+ if (x_relative) {
907
+ x = info->dwCursorPosition.X + x;
908
+ }
909
+ /* Clip x */
910
+ if (x < 0) {
911
+ x = 0;
912
+ } else if (x >= uv_tty_virtual_width) {
913
+ x = uv_tty_virtual_width - 1;
914
+ }
915
+
916
+ result.X = (unsigned short) x;
917
+ result.Y = (unsigned short) y;
918
+ return result;
919
+ }
920
+
921
+
922
+ static int uv_tty_emit_text(uv_tty_t* handle, WCHAR buffer[], DWORD length,
923
+ DWORD* error) {
924
+ DWORD written;
925
+
926
+ if (*error != ERROR_SUCCESS) {
927
+ return -1;
928
+ }
929
+
930
+ if (!WriteConsoleW(handle->handle,
931
+ (void*) buffer,
932
+ length,
933
+ &written,
934
+ NULL)) {
935
+ *error = GetLastError();
936
+ return -1;
937
+ }
938
+
939
+ return 0;
940
+ }
941
+
942
+
943
+ static int uv_tty_move_caret(uv_tty_t* handle, int x, unsigned char x_relative,
944
+ int y, unsigned char y_relative, DWORD* error) {
945
+ CONSOLE_SCREEN_BUFFER_INFO info;
946
+ COORD pos;
947
+
948
+ if (*error != ERROR_SUCCESS) {
949
+ return -1;
950
+ }
951
+
952
+ retry:
953
+ if (!GetConsoleScreenBufferInfo(handle->handle, &info)) {
954
+ *error = GetLastError();
955
+ }
956
+
957
+ pos = uv_tty_make_real_coord(handle, &info, x, x_relative, y, y_relative);
958
+
959
+ if (!SetConsoleCursorPosition(handle->handle, pos)) {
960
+ if (GetLastError() == ERROR_INVALID_PARAMETER) {
961
+ /* The console may be resized - retry */
962
+ goto retry;
963
+ } else {
964
+ *error = GetLastError();
965
+ return -1;
966
+ }
967
+ }
968
+
969
+ return 0;
970
+ }
971
+
972
+
973
+ static int uv_tty_reset(uv_tty_t* handle, DWORD* error) {
974
+ const COORD origin = {0, 0};
975
+ const WORD char_attrs = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
976
+ CONSOLE_SCREEN_BUFFER_INFO info;
977
+ DWORD count, written;
978
+
979
+ if (*error != ERROR_SUCCESS) {
980
+ return -1;
981
+ }
982
+
983
+ /* Reset original text attributes. */
984
+ if (!SetConsoleTextAttribute(handle->handle, char_attrs)) {
985
+ *error = GetLastError();
986
+ return -1;
987
+ }
988
+
989
+ /* Move the cursor position to (0, 0). */
990
+ if (!SetConsoleCursorPosition(handle->handle, origin)) {
991
+ *error = GetLastError();
992
+ return -1;
993
+ }
994
+
995
+ /* Clear the screen buffer. */
996
+ retry:
997
+ if (!GetConsoleScreenBufferInfo(handle->handle, &info)) {
998
+ *error = GetLastError();
999
+ return -1;
1000
+ }
1001
+
1002
+ count = info.dwSize.X * info.dwSize.Y;
1003
+
1004
+ if (!(FillConsoleOutputCharacterW(handle->handle,
1005
+ L'\x20',
1006
+ count,
1007
+ origin,
1008
+ &written) &&
1009
+ FillConsoleOutputAttribute(handle->handle,
1010
+ char_attrs,
1011
+ written,
1012
+ origin,
1013
+ &written))) {
1014
+ if (GetLastError() == ERROR_INVALID_PARAMETER) {
1015
+ /* The console may be resized - retry */
1016
+ goto retry;
1017
+ } else {
1018
+ *error = GetLastError();
1019
+ return -1;
1020
+ }
1021
+ }
1022
+
1023
+ /* Move the virtual window up to the top. */
1024
+ uv_tty_virtual_offset = 0;
1025
+ uv_tty_update_virtual_window(&info);
1026
+
1027
+ return 0;
1028
+ }
1029
+
1030
+
1031
+ static int uv_tty_clear(uv_tty_t* handle, int dir, char entire_screen,
1032
+ DWORD* error) {
1033
+ CONSOLE_SCREEN_BUFFER_INFO info;
1034
+ COORD start, end;
1035
+ DWORD count, written;
1036
+
1037
+ int x1, x2, y1, y2;
1038
+ int x1r, x2r, y1r, y2r;
1039
+
1040
+ if (*error != ERROR_SUCCESS) {
1041
+ return -1;
1042
+ }
1043
+
1044
+ if (dir == 0) {
1045
+ /* Clear from current position */
1046
+ x1 = 0;
1047
+ x1r = 1;
1048
+ } else {
1049
+ /* Clear from column 0 */
1050
+ x1 = 0;
1051
+ x1r = 0;
1052
+ }
1053
+
1054
+ if (dir == 1) {
1055
+ /* Clear to current position */
1056
+ x2 = 0;
1057
+ x2r = 1;
1058
+ } else {
1059
+ /* Clear to end of row. We pretend the console is 65536 characters wide, */
1060
+ /* uv_tty_make_real_coord will clip it to the actual console width. */
1061
+ x2 = 0xffff;
1062
+ x2r = 0;
1063
+ }
1064
+
1065
+ if (!entire_screen) {
1066
+ /* Stay on our own row */
1067
+ y1 = y2 = 0;
1068
+ y1r = y2r = 1;
1069
+ } else {
1070
+ /* Apply columns direction to row */
1071
+ y1 = x1;
1072
+ y1r = x1r;
1073
+ y2 = x2;
1074
+ y2r = x2r;
1075
+ }
1076
+
1077
+ retry:
1078
+ if (!GetConsoleScreenBufferInfo(handle->handle, &info)) {
1079
+ *error = GetLastError();
1080
+ return -1;
1081
+ }
1082
+
1083
+ start = uv_tty_make_real_coord(handle, &info, x1, x1r, y1, y1r);
1084
+ end = uv_tty_make_real_coord(handle, &info, x2, x2r, y2, y2r);
1085
+ count = (end.Y * info.dwSize.X + end.X) -
1086
+ (start.Y * info.dwSize.X + start.X) + 1;
1087
+
1088
+ if (!(FillConsoleOutputCharacterW(handle->handle,
1089
+ L'\x20',
1090
+ count,
1091
+ start,
1092
+ &written) &&
1093
+ FillConsoleOutputAttribute(handle->handle,
1094
+ info.wAttributes,
1095
+ written,
1096
+ start,
1097
+ &written))) {
1098
+ if (GetLastError() == ERROR_INVALID_PARAMETER) {
1099
+ /* The console may be resized - retry */
1100
+ goto retry;
1101
+ } else {
1102
+ *error = GetLastError();
1103
+ return -1;
1104
+ }
1105
+ }
1106
+
1107
+ return 0;
1108
+ }
1109
+
1110
+
1111
+ static int uv_tty_set_style(uv_tty_t* handle, DWORD* error) {
1112
+ unsigned short argc = handle->ansi_csi_argc;
1113
+ unsigned short* argv = handle->ansi_csi_argv;
1114
+ int i;
1115
+ CONSOLE_SCREEN_BUFFER_INFO info;
1116
+
1117
+ char fg_color = -1, bg_color = -1;
1118
+ char fg_bright = -1, bg_bright = -1;
1119
+
1120
+ if (argc == 0) {
1121
+ /* Reset mode */
1122
+ fg_color = 7;
1123
+ bg_color = 0;
1124
+ fg_bright = 0;
1125
+ bg_bright = 0;
1126
+ }
1127
+
1128
+ for (i = 0; i < argc; i++) {
1129
+ short arg = argv[i];
1130
+
1131
+ if (arg == 0) {
1132
+ /* Reset mode */
1133
+ fg_color = 7;
1134
+ bg_color = 0;
1135
+ fg_bright = 0;
1136
+ bg_bright = 0;
1137
+
1138
+ } else if (arg == 1) {
1139
+ /* Foreground bright on */
1140
+ fg_bright = 1;
1141
+
1142
+ } else if (arg == 2) {
1143
+ /* Both bright off */
1144
+ fg_bright = 0;
1145
+ bg_bright = 0;
1146
+
1147
+ } else if (arg == 5) {
1148
+ /* Background bright on */
1149
+ bg_bright = 1;
1150
+
1151
+ } else if (arg == 21 || arg == 22) {
1152
+ /* Foreground bright off */
1153
+ fg_bright = 0;
1154
+
1155
+ } else if (arg == 25) {
1156
+ /* Background bright off */
1157
+ bg_bright = 0;
1158
+
1159
+ } else if (arg >= 30 && arg <= 37) {
1160
+ /* Set foreground color */
1161
+ fg_color = arg - 30;
1162
+
1163
+ } else if (arg == 39) {
1164
+ /* Default text color */
1165
+ fg_color = 7;
1166
+ fg_bright = 0;
1167
+
1168
+ } else if (arg >= 40 && arg <= 47) {
1169
+ /* Set background color */
1170
+ bg_color = arg - 40;
1171
+
1172
+ } else if (arg == 49) {
1173
+ /* Default background color */
1174
+ bg_color = 0;
1175
+ bg_bright = 0;
1176
+
1177
+ } else if (arg >= 90 && arg <= 97) {
1178
+ /* Set bold foreground color */
1179
+ fg_bright = 1;
1180
+ fg_color = arg - 90;
1181
+
1182
+ } else if (arg >= 100 && arg <= 107) {
1183
+ /* Set bold background color */
1184
+ bg_bright = 1;
1185
+ bg_color = arg - 100;
1186
+
1187
+ }
1188
+ }
1189
+
1190
+ if (fg_color == -1 && bg_color == -1 && fg_bright == -1 &&
1191
+ bg_bright == -1) {
1192
+ /* Nothing changed */
1193
+ return 0;
1194
+ }
1195
+
1196
+ if (!GetConsoleScreenBufferInfo(handle->handle, &info)) {
1197
+ *error = GetLastError();
1198
+ return -1;
1199
+ }
1200
+
1201
+ if (fg_color != -1) {
1202
+ info.wAttributes &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
1203
+ if (fg_color & 1) info.wAttributes |= FOREGROUND_RED;
1204
+ if (fg_color & 2) info.wAttributes |= FOREGROUND_GREEN;
1205
+ if (fg_color & 4) info.wAttributes |= FOREGROUND_BLUE;
1206
+ }
1207
+
1208
+ if (fg_bright != -1) {
1209
+ if (fg_bright) {
1210
+ info.wAttributes |= FOREGROUND_INTENSITY;
1211
+ } else {
1212
+ info.wAttributes &= ~FOREGROUND_INTENSITY;
1213
+ }
1214
+ }
1215
+
1216
+ if (bg_color != -1) {
1217
+ info.wAttributes &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
1218
+ if (bg_color & 1) info.wAttributes |= BACKGROUND_RED;
1219
+ if (bg_color & 2) info.wAttributes |= BACKGROUND_GREEN;
1220
+ if (bg_color & 4) info.wAttributes |= BACKGROUND_BLUE;
1221
+ }
1222
+
1223
+ if (bg_bright != -1) {
1224
+ if (bg_bright) {
1225
+ info.wAttributes |= BACKGROUND_INTENSITY;
1226
+ } else {
1227
+ info.wAttributes &= ~BACKGROUND_INTENSITY;
1228
+ }
1229
+ }
1230
+
1231
+ if (!SetConsoleTextAttribute(handle->handle, info.wAttributes)) {
1232
+ *error = GetLastError();
1233
+ return -1;
1234
+ }
1235
+
1236
+ return 0;
1237
+ }
1238
+
1239
+
1240
+ static int uv_tty_save_state(uv_tty_t* handle, unsigned char save_attributes,
1241
+ DWORD* error) {
1242
+ CONSOLE_SCREEN_BUFFER_INFO info;
1243
+
1244
+ if (*error != ERROR_SUCCESS) {
1245
+ return -1;
1246
+ }
1247
+
1248
+ if (!GetConsoleScreenBufferInfo(handle->handle, &info)) {
1249
+ *error = GetLastError();
1250
+ return -1;
1251
+ }
1252
+
1253
+ uv_tty_update_virtual_window(&info);
1254
+
1255
+ handle->saved_position.X = info.dwCursorPosition.X;
1256
+ handle->saved_position.Y = info.dwCursorPosition.Y - uv_tty_virtual_offset;
1257
+ handle->flags |= UV_HANDLE_TTY_SAVED_POSITION;
1258
+
1259
+ if (save_attributes) {
1260
+ handle->saved_attributes = info.wAttributes &
1261
+ (FOREGROUND_INTENSITY | BACKGROUND_INTENSITY);
1262
+ handle->flags |= UV_HANDLE_TTY_SAVED_ATTRIBUTES;
1263
+ }
1264
+
1265
+ return 0;
1266
+ }
1267
+
1268
+
1269
+ static int uv_tty_restore_state(uv_tty_t* handle,
1270
+ unsigned char restore_attributes, DWORD* error) {
1271
+ CONSOLE_SCREEN_BUFFER_INFO info;
1272
+ WORD new_attributes;
1273
+
1274
+ if (*error != ERROR_SUCCESS) {
1275
+ return -1;
1276
+ }
1277
+
1278
+ if (handle->flags & UV_HANDLE_TTY_SAVED_POSITION) {
1279
+ if (uv_tty_move_caret(handle,
1280
+ handle->saved_position.X,
1281
+ 0,
1282
+ handle->saved_position.Y,
1283
+ 0,
1284
+ error) != 0) {
1285
+ return -1;
1286
+ }
1287
+ }
1288
+
1289
+ if (restore_attributes &&
1290
+ (handle->flags & UV_HANDLE_TTY_SAVED_ATTRIBUTES)) {
1291
+ if (!GetConsoleScreenBufferInfo(handle->handle, &info)) {
1292
+ *error = GetLastError();
1293
+ return -1;
1294
+ }
1295
+
1296
+ new_attributes = info.wAttributes;
1297
+ new_attributes &= ~(FOREGROUND_INTENSITY | BACKGROUND_INTENSITY);
1298
+ new_attributes |= handle->saved_attributes;
1299
+
1300
+ if (!SetConsoleTextAttribute(handle->handle, new_attributes)) {
1301
+ *error = GetLastError();
1302
+ return -1;
1303
+ }
1304
+ }
1305
+
1306
+ return 0;
1307
+ }
1308
+
1309
+
1310
+ static int uv_tty_write_bufs(uv_tty_t* handle, uv_buf_t bufs[], int bufcnt,
1311
+ DWORD* error) {
1312
+ /* We can only write 8k characters at a time. Windows can't handle */
1313
+ /* much more characters in a single console write anyway. */
1314
+ WCHAR utf16_buf[8192];
1315
+ DWORD utf16_buf_used = 0;
1316
+ int i;
1317
+
1318
+ #define FLUSH_TEXT() \
1319
+ do { \
1320
+ if (utf16_buf_used > 0) { \
1321
+ uv_tty_emit_text(handle, utf16_buf, utf16_buf_used, error); \
1322
+ utf16_buf_used = 0; \
1323
+ } \
1324
+ } while (0)
1325
+
1326
+ /* Cache for fast access */
1327
+ unsigned char utf8_bytes_left = handle->utf8_bytes_left;
1328
+ unsigned int utf8_codepoint = handle->utf8_codepoint;
1329
+ unsigned char previous_eol = handle->previous_eol;
1330
+ unsigned char ansi_parser_state = handle->ansi_parser_state;
1331
+
1332
+ /* Store the error here. If we encounter an error, stop trying to do i/o */
1333
+ /* but keep parsing the buffer so we leave the parser in a consistent */
1334
+ /* state. */
1335
+ *error = ERROR_SUCCESS;
1336
+
1337
+ EnterCriticalSection(&uv_tty_output_lock);
1338
+
1339
+ for (i = 0; i < bufcnt; i++) {
1340
+ uv_buf_t buf = bufs[i];
1341
+ unsigned int j;
1342
+
1343
+ for (j = 0; j < buf.len; j++) {
1344
+ unsigned char c = buf.base[j];
1345
+
1346
+ /* Run the character through the utf8 decoder We happily accept non */
1347
+ /* shortest form encodings and invalid code points - there's no real */
1348
+ /* harm that can be done. */
1349
+ if (utf8_bytes_left == 0) {
1350
+ /* Read utf-8 start byte */
1351
+ DWORD first_zero_bit;
1352
+ unsigned char not_c = ~c;
1353
+ #ifdef _MSC_VER /* msvc */
1354
+ if (_BitScanReverse(&first_zero_bit, not_c)) {
1355
+ #else /* assume gcc */
1356
+ if (c != 0) {
1357
+ first_zero_bit = (sizeof(int) * 8) - 1 - __builtin_clz(not_c);
1358
+ #endif
1359
+ if (first_zero_bit == 7) {
1360
+ /* Ascii - pass right through */
1361
+ utf8_codepoint = (unsigned int) c;
1362
+
1363
+ } else if (first_zero_bit <= 5) {
1364
+ /* Multibyte sequence */
1365
+ utf8_codepoint = (0xff >> (8 - first_zero_bit)) & c;
1366
+ utf8_bytes_left = (char) (6 - first_zero_bit);
1367
+
1368
+ } else {
1369
+ /* Invalid continuation */
1370
+ utf8_codepoint = UNICODE_REPLACEMENT_CHARACTER;
1371
+ }
1372
+
1373
+ } else {
1374
+ /* 0xff -- invalid */
1375
+ utf8_codepoint = UNICODE_REPLACEMENT_CHARACTER;
1376
+ }
1377
+
1378
+ } else if ((c & 0xc0) == 0x80) {
1379
+ /* Valid continuation of utf-8 multibyte sequence */
1380
+ utf8_bytes_left--;
1381
+ utf8_codepoint <<= 6;
1382
+ utf8_codepoint |= ((unsigned int) c & 0x3f);
1383
+
1384
+ } else {
1385
+ /* Start byte where continuation was expected. */
1386
+ utf8_bytes_left = 0;
1387
+ utf8_codepoint = UNICODE_REPLACEMENT_CHARACTER;
1388
+ /* Patch buf offset so this character will be parsed again as a */
1389
+ /* start byte. */
1390
+ j--;
1391
+ }
1392
+
1393
+ /* Maybe we need to parse more bytes to find a character. */
1394
+ if (utf8_bytes_left != 0) {
1395
+ continue;
1396
+ }
1397
+
1398
+ /* Parse vt100/ansi escape codes */
1399
+ if (ansi_parser_state == ANSI_NORMAL) {
1400
+ switch (utf8_codepoint) {
1401
+ case '\033':
1402
+ ansi_parser_state = ANSI_ESCAPE_SEEN;
1403
+ continue;
1404
+
1405
+ case 0233:
1406
+ ansi_parser_state = ANSI_CSI;
1407
+ handle->ansi_csi_argc = 0;
1408
+ continue;
1409
+ }
1410
+
1411
+ } else if (ansi_parser_state == ANSI_ESCAPE_SEEN) {
1412
+ switch (utf8_codepoint) {
1413
+ case '[':
1414
+ ansi_parser_state = ANSI_CSI;
1415
+ handle->ansi_csi_argc = 0;
1416
+ continue;
1417
+
1418
+ case '^':
1419
+ case '_':
1420
+ case 'P':
1421
+ case ']':
1422
+ /* Not supported, but we'll have to parse until we see a stop */
1423
+ /* code, e.g. ESC \ or BEL. */
1424
+ ansi_parser_state = ANSI_ST_CONTROL;
1425
+ continue;
1426
+
1427
+ case '\033':
1428
+ /* Ignore double escape. */
1429
+ continue;
1430
+
1431
+ case 'c':
1432
+ /* Full console reset. */
1433
+ FLUSH_TEXT();
1434
+ uv_tty_reset(handle, error);
1435
+ ansi_parser_state = ANSI_NORMAL;
1436
+ continue;
1437
+
1438
+ case '7':
1439
+ /* Save the cursor position and text attributes. */
1440
+ FLUSH_TEXT();
1441
+ uv_tty_save_state(handle, 1, error);
1442
+ ansi_parser_state = ANSI_NORMAL;
1443
+ continue;
1444
+
1445
+ case '8':
1446
+ /* Restore the cursor position and text attributes */
1447
+ FLUSH_TEXT();
1448
+ uv_tty_restore_state(handle, 1, error);
1449
+ ansi_parser_state = ANSI_NORMAL;
1450
+ continue;
1451
+
1452
+ default:
1453
+ if (utf8_codepoint >= '@' && utf8_codepoint <= '_') {
1454
+ /* Single-char control. */
1455
+ ansi_parser_state = ANSI_NORMAL;
1456
+ continue;
1457
+ } else {
1458
+ /* Invalid - proceed as normal, */
1459
+ ansi_parser_state = ANSI_NORMAL;
1460
+ }
1461
+ }
1462
+
1463
+ } else if (ansi_parser_state & ANSI_CSI) {
1464
+ if (!(ansi_parser_state & ANSI_IGNORE)) {
1465
+ if (utf8_codepoint >= '0' && utf8_codepoint <= '9') {
1466
+ /* Parsing a numerical argument */
1467
+
1468
+ if (!(ansi_parser_state & ANSI_IN_ARG)) {
1469
+ /* We were not currently parsing a number */
1470
+
1471
+ /* Check for too many arguments */
1472
+ if (handle->ansi_csi_argc >= ARRAY_SIZE(handle->ansi_csi_argv)) {
1473
+ ansi_parser_state |= ANSI_IGNORE;
1474
+ continue;
1475
+ }
1476
+
1477
+ ansi_parser_state |= ANSI_IN_ARG;
1478
+ handle->ansi_csi_argc++;
1479
+ handle->ansi_csi_argv[handle->ansi_csi_argc - 1] =
1480
+ (unsigned short) utf8_codepoint - '0';
1481
+ continue;
1482
+ } else {
1483
+ /* We were already parsing a number. Parse next digit. */
1484
+ uint32_t value = 10 *
1485
+ handle->ansi_csi_argv[handle->ansi_csi_argc - 1];
1486
+
1487
+ /* Check for overflow. */
1488
+ if (value > UINT16_MAX) {
1489
+ ansi_parser_state |= ANSI_IGNORE;
1490
+ continue;
1491
+ }
1492
+
1493
+ handle->ansi_csi_argv[handle->ansi_csi_argc - 1] =
1494
+ (unsigned short) value + (utf8_codepoint - '0');
1495
+ continue;
1496
+ }
1497
+
1498
+ } else if (utf8_codepoint == ';') {
1499
+ /* Denotes the end of an argument. */
1500
+ if (ansi_parser_state & ANSI_IN_ARG) {
1501
+ ansi_parser_state &= ~ANSI_IN_ARG;
1502
+ continue;
1503
+
1504
+ } else {
1505
+ /* If ANSI_IN_ARG is not set, add another argument and */
1506
+ /* default it to 0. */
1507
+ /* Check for too many arguments */
1508
+ if (handle->ansi_csi_argc >= ARRAY_SIZE(handle->ansi_csi_argv)) {
1509
+ ansi_parser_state |= ANSI_IGNORE;
1510
+ continue;
1511
+ }
1512
+
1513
+ handle->ansi_csi_argc++;
1514
+ handle->ansi_csi_argv[handle->ansi_csi_argc - 1] = 0;
1515
+ continue;
1516
+ }
1517
+
1518
+ } else if (utf8_codepoint >= '@' && utf8_codepoint <= '~' &&
1519
+ (handle->ansi_csi_argc > 0 || utf8_codepoint != '[')) {
1520
+ int x, y, d;
1521
+
1522
+ /* Command byte */
1523
+ switch (utf8_codepoint) {
1524
+ case 'A':
1525
+ /* cursor up */
1526
+ FLUSH_TEXT();
1527
+ y = -(handle->ansi_csi_argc ? handle->ansi_csi_argv[0] : 1);
1528
+ uv_tty_move_caret(handle, 0, 1, y, 1, error);
1529
+ break;
1530
+
1531
+ case 'B':
1532
+ /* cursor down */
1533
+ FLUSH_TEXT();
1534
+ y = handle->ansi_csi_argc ? handle->ansi_csi_argv[0] : 1;
1535
+ uv_tty_move_caret(handle, 0, 1, y, 1, error);
1536
+ break;
1537
+
1538
+ case 'C':
1539
+ /* cursor forward */
1540
+ FLUSH_TEXT();
1541
+ x = handle->ansi_csi_argc ? handle->ansi_csi_argv[0] : 1;
1542
+ uv_tty_move_caret(handle, x, 1, 0, 1, error);
1543
+ break;
1544
+
1545
+ case 'D':
1546
+ /* cursor back */
1547
+ FLUSH_TEXT();
1548
+ x = -(handle->ansi_csi_argc ? handle->ansi_csi_argv[0] : 1);
1549
+ uv_tty_move_caret(handle, x, 1, 0, 1, error);
1550
+ break;
1551
+
1552
+ case 'E':
1553
+ /* cursor next line */
1554
+ FLUSH_TEXT();
1555
+ y = handle->ansi_csi_argc ? handle->ansi_csi_argv[0] : 1;
1556
+ uv_tty_move_caret(handle, 0, 0, y, 1, error);
1557
+ break;
1558
+
1559
+ case 'F':
1560
+ /* cursor previous line */
1561
+ FLUSH_TEXT();
1562
+ y = -(handle->ansi_csi_argc ? handle->ansi_csi_argv[0] : 1);
1563
+ uv_tty_move_caret(handle, 0, 0, y, 1, error);
1564
+ break;
1565
+
1566
+ case 'G':
1567
+ /* cursor horizontal move absolute */
1568
+ FLUSH_TEXT();
1569
+ x = (handle->ansi_csi_argc >= 1 && handle->ansi_csi_argv[0])
1570
+ ? handle->ansi_csi_argv[0] - 1 : 0;
1571
+ uv_tty_move_caret(handle, x, 0, 0, 1, error);
1572
+ break;
1573
+
1574
+ case 'H':
1575
+ case 'f':
1576
+ /* cursor move absolute */
1577
+ FLUSH_TEXT();
1578
+ y = (handle->ansi_csi_argc >= 1 && handle->ansi_csi_argv[0])
1579
+ ? handle->ansi_csi_argv[0] - 1 : 0;
1580
+ x = (handle->ansi_csi_argc >= 2 && handle->ansi_csi_argv[1])
1581
+ ? handle->ansi_csi_argv[1] - 1 : 0;
1582
+ uv_tty_move_caret(handle, x, 0, y, 0, error);
1583
+ break;
1584
+
1585
+ case 'J':
1586
+ /* Erase screen */
1587
+ FLUSH_TEXT();
1588
+ d = handle->ansi_csi_argc ? handle->ansi_csi_argv[0] : 0;
1589
+ if (d >= 0 && d <= 2) {
1590
+ uv_tty_clear(handle, d, 1, error);
1591
+ }
1592
+ break;
1593
+
1594
+ case 'K':
1595
+ /* Erase line */
1596
+ FLUSH_TEXT();
1597
+ d = handle->ansi_csi_argc ? handle->ansi_csi_argv[0] : 0;
1598
+ if (d >= 0 && d <= 2) {
1599
+ uv_tty_clear(handle, d, 0, error);
1600
+ }
1601
+ break;
1602
+
1603
+ case 'm':
1604
+ /* Set style */
1605
+ FLUSH_TEXT();
1606
+ uv_tty_set_style(handle, error);
1607
+ break;
1608
+
1609
+ case 's':
1610
+ /* Save the cursor position. */
1611
+ FLUSH_TEXT();
1612
+ uv_tty_save_state(handle, 0, error);
1613
+ break;
1614
+
1615
+ case 'u':
1616
+ /* Restore the cursor position */
1617
+ FLUSH_TEXT();
1618
+ uv_tty_restore_state(handle, 0, error);
1619
+ break;
1620
+ }
1621
+
1622
+ /* Sequence ended - go back to normal state. */
1623
+ ansi_parser_state = ANSI_NORMAL;
1624
+ continue;
1625
+
1626
+ } else {
1627
+ /* We don't support commands that use private mode characters or */
1628
+ /* intermediaries. Ignore the rest of the sequence. */
1629
+ ansi_parser_state |= ANSI_IGNORE;
1630
+ continue;
1631
+ }
1632
+ } else {
1633
+ /* We're ignoring this command. Stop only on command character. */
1634
+ if (utf8_codepoint >= '@' && utf8_codepoint <= '~') {
1635
+ ansi_parser_state = ANSI_NORMAL;
1636
+ }
1637
+ continue;
1638
+ }
1639
+
1640
+ } else if (ansi_parser_state & ANSI_ST_CONTROL) {
1641
+ /* Unsupported control code */
1642
+ /* Ignore everything until we see BEL or ESC \ */
1643
+ if (ansi_parser_state & ANSI_IN_STRING) {
1644
+ if (!(ansi_parser_state & ANSI_BACKSLASH_SEEN)) {
1645
+ if (utf8_codepoint == '"') {
1646
+ ansi_parser_state &= ~ANSI_IN_STRING;
1647
+ } else if (utf8_codepoint == '\\') {
1648
+ ansi_parser_state |= ANSI_BACKSLASH_SEEN;
1649
+ }
1650
+ } else {
1651
+ ansi_parser_state &= ~ANSI_BACKSLASH_SEEN;
1652
+ }
1653
+ } else {
1654
+ if (utf8_codepoint == '\007' || (utf8_codepoint == '\\' &&
1655
+ (ansi_parser_state & ANSI_ESCAPE_SEEN))) {
1656
+ /* End of sequence */
1657
+ ansi_parser_state = ANSI_NORMAL;
1658
+ } else if (utf8_codepoint == '\033') {
1659
+ /* Escape character */
1660
+ ansi_parser_state |= ANSI_ESCAPE_SEEN;
1661
+ } else if (utf8_codepoint == '"') {
1662
+ /* String starting */
1663
+ ansi_parser_state |= ANSI_IN_STRING;
1664
+ ansi_parser_state &= ~ANSI_ESCAPE_SEEN;
1665
+ ansi_parser_state &= ~ANSI_BACKSLASH_SEEN;
1666
+ } else {
1667
+ ansi_parser_state &= ~ANSI_ESCAPE_SEEN;
1668
+ }
1669
+ }
1670
+ continue;
1671
+ } else {
1672
+ /* Inconsistent state */
1673
+ abort();
1674
+ }
1675
+
1676
+ /* We wouldn't mind emitting utf-16 surrogate pairs. Too bad, the */
1677
+ /* windows console doesn't really support UTF-16, so just emit the */
1678
+ /* replacement character. */
1679
+ if (utf8_codepoint > 0xffff) {
1680
+ utf8_codepoint = UNICODE_REPLACEMENT_CHARACTER;
1681
+ }
1682
+
1683
+ if (utf8_codepoint == 0x0a || utf8_codepoint == 0x0d) {
1684
+ /* EOL conversion - emit \r\n, when we see either \r or \n. */
1685
+ /* If a \n immediately follows a \r or vice versa, ignore it. */
1686
+ if (previous_eol == 0 || utf8_codepoint == previous_eol) {
1687
+ /* If there's no room in the utf16 buf, flush it first. */
1688
+ if (2 > ARRAY_SIZE(utf16_buf) - utf16_buf_used) {
1689
+ uv_tty_emit_text(handle, utf16_buf, utf16_buf_used, error);
1690
+ utf16_buf_used = 0;
1691
+ }
1692
+
1693
+ utf16_buf[utf16_buf_used++] = L'\r';
1694
+ utf16_buf[utf16_buf_used++] = L'\n';
1695
+ previous_eol = (char) utf8_codepoint;
1696
+ } else {
1697
+ /* Ignore this newline, but don't ignore later ones. */
1698
+ previous_eol = 0;
1699
+ }
1700
+
1701
+ } else if (utf8_codepoint <= 0xffff) {
1702
+ /* Encode character into utf-16 buffer. */
1703
+
1704
+ /* If there's no room in the utf16 buf, flush it first. */
1705
+ if (1 > ARRAY_SIZE(utf16_buf) - utf16_buf_used) {
1706
+ uv_tty_emit_text(handle, utf16_buf, utf16_buf_used, error);
1707
+ utf16_buf_used = 0;
1708
+ }
1709
+
1710
+ utf16_buf[utf16_buf_used++] = (WCHAR) utf8_codepoint;
1711
+ previous_eol = 0;
1712
+ }
1713
+ }
1714
+ }
1715
+
1716
+ /* Flush remaining characters */
1717
+ FLUSH_TEXT();
1718
+
1719
+ /* Copy cached values back to struct. */
1720
+ handle->utf8_bytes_left = utf8_bytes_left;
1721
+ handle->utf8_codepoint = utf8_codepoint;
1722
+ handle->previous_eol = previous_eol;
1723
+ handle->ansi_parser_state = ansi_parser_state;
1724
+
1725
+ LeaveCriticalSection(&uv_tty_output_lock);
1726
+
1727
+ if (*error == STATUS_SUCCESS) {
1728
+ return 0;
1729
+ } else {
1730
+ return -1;
1731
+ }
1732
+
1733
+ #undef FLUSH_TEXT
1734
+ }
1735
+
1736
+
1737
+ int uv_tty_write(uv_loop_t* loop, uv_write_t* req, uv_tty_t* handle,
1738
+ uv_buf_t bufs[], int bufcnt, uv_write_cb cb) {
1739
+ DWORD error;
1740
+
1741
+ uv_req_init(loop, (uv_req_t*) req);
1742
+ req->type = UV_WRITE;
1743
+ req->handle = (uv_stream_t*) handle;
1744
+ req->cb = cb;
1745
+
1746
+ handle->reqs_pending++;
1747
+ handle->write_reqs_pending++;
1748
+ REGISTER_HANDLE_REQ(loop, handle, req);
1749
+
1750
+ req->queued_bytes = 0;
1751
+
1752
+ if (!uv_tty_write_bufs(handle, bufs, bufcnt, &error)) {
1753
+ SET_REQ_SUCCESS(req);
1754
+ } else {
1755
+ SET_REQ_ERROR(req, error);
1756
+ }
1757
+
1758
+ uv_insert_pending_req(loop, (uv_req_t*) req);
1759
+
1760
+ return 0;
1761
+ }
1762
+
1763
+
1764
+ void uv_process_tty_write_req(uv_loop_t* loop, uv_tty_t* handle,
1765
+ uv_write_t* req) {
1766
+
1767
+ handle->write_queue_size -= req->queued_bytes;
1768
+ UNREGISTER_HANDLE_REQ(loop, handle, req);
1769
+
1770
+ if (req->cb) {
1771
+ uv__set_sys_error(loop, GET_REQ_ERROR(req));
1772
+ ((uv_write_cb)req->cb)(req, loop->last_err.code == UV_OK ? 0 : -1);
1773
+ }
1774
+
1775
+ handle->write_reqs_pending--;
1776
+ if (handle->shutdown_req != NULL &&
1777
+ handle->write_reqs_pending == 0) {
1778
+ uv_want_endgame(loop, (uv_handle_t*)handle);
1779
+ }
1780
+
1781
+ DECREASE_PENDING_REQ_COUNT(handle);
1782
+ }
1783
+
1784
+
1785
+ void uv_tty_close(uv_tty_t* handle) {
1786
+ CloseHandle(handle->handle);
1787
+
1788
+ if (handle->flags & UV_HANDLE_READING)
1789
+ uv_tty_read_stop(handle);
1790
+
1791
+ handle->flags &= ~(UV_HANDLE_READABLE | UV_HANDLE_WRITABLE);
1792
+ uv__handle_closing(handle);
1793
+
1794
+ if (handle->reqs_pending == 0) {
1795
+ uv_want_endgame(handle->loop, (uv_handle_t*) handle);
1796
+ }
1797
+ }
1798
+
1799
+
1800
+ void uv_tty_endgame(uv_loop_t* loop, uv_tty_t* handle) {
1801
+ if (!(handle->flags && UV_HANDLE_TTY_READABLE) &&
1802
+ handle->shutdown_req != NULL &&
1803
+ handle->write_reqs_pending == 0) {
1804
+ UNREGISTER_HANDLE_REQ(loop, handle, handle->shutdown_req);
1805
+
1806
+ /* TTY shutdown is really just a no-op */
1807
+ if (handle->shutdown_req->cb) {
1808
+ if (handle->flags & UV__HANDLE_CLOSING) {
1809
+ uv__set_artificial_error(loop, UV_ECANCELED);
1810
+ handle->shutdown_req->cb(handle->shutdown_req, -1);
1811
+ } else {
1812
+ handle->shutdown_req->cb(handle->shutdown_req, 0);
1813
+ }
1814
+ }
1815
+
1816
+ handle->shutdown_req = NULL;
1817
+
1818
+ DECREASE_PENDING_REQ_COUNT(handle);
1819
+ return;
1820
+ }
1821
+
1822
+ if (handle->flags & UV__HANDLE_CLOSING &&
1823
+ handle->reqs_pending == 0) {
1824
+ /* The console handle duplicate used for line reading should be destroyed */
1825
+ /* by uv_tty_read_stop. */
1826
+ assert(!(handle->flags & UV_HANDLE_TTY_READABLE) ||
1827
+ handle->read_line_handle == NULL);
1828
+
1829
+ /* The wait handle used for raw reading should be unregistered when the */
1830
+ /* wait callback runs. */
1831
+ assert(!(handle->flags & UV_HANDLE_TTY_READABLE) ||
1832
+ handle->read_raw_wait == NULL);
1833
+
1834
+ assert(!(handle->flags & UV_HANDLE_CLOSED));
1835
+ uv__handle_close(handle);
1836
+ }
1837
+ }
1838
+
1839
+
1840
+ /* TODO: remove me */
1841
+ void uv_process_tty_accept_req(uv_loop_t* loop, uv_tty_t* handle,
1842
+ uv_req_t* raw_req) {
1843
+ abort();
1844
+ }
1845
+
1846
+
1847
+ /* TODO: remove me */
1848
+ void uv_process_tty_connect_req(uv_loop_t* loop, uv_tty_t* handle,
1849
+ uv_connect_t* req) {
1850
+ abort();
1851
+ }
1852
+
1853
+
1854
+ void uv_tty_reset_mode(void) {
1855
+ /* Not necessary to do anything. */
1856
+ ;
1857
+ }