tty.c 70.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/*
 * 2020 Jacek Caban for CodeWeavers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include "wine/test.h"

#include <windows.h>

static HRESULT (WINAPI *pCreatePseudoConsole)(COORD,HANDLE,HANDLE,DWORD,HPCON*);
static void (WINAPI *pClosePseudoConsole)(HPCON);

static char console_output[4096];
static unsigned int console_output_count;
static HANDLE console_pipe;
static HANDLE child_pipe;

#define fetch_console_output() fetch_console_output_(__LINE__)
static void fetch_console_output_(unsigned int line)
{
    OVERLAPPED o;
    DWORD count;
    BOOL ret;

    if (console_output_count == sizeof(console_output)) return;

40
    memset(&o, 0, sizeof(o));
41 42 43 44 45
    o.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
    ret = ReadFile(console_pipe, console_output + console_output_count,
                   sizeof(console_output) - console_output_count, NULL, &o);
    if (!ret)
    {
46
        ok_(__FILE__,line)(GetLastError() == ERROR_IO_PENDING, "read failed: %lu\n", GetLastError());
47
        if (GetLastError() != ERROR_IO_PENDING) return;
48
        WaitForSingleObject(o.hEvent, 5000);
49 50
    }
    ret = GetOverlappedResult(console_pipe, &o, &count, FALSE);
51
    if (!ret && GetLastError() == ERROR_IO_INCOMPLETE)
52 53
        CancelIoEx(console_pipe, &o);

54
    ok_(__FILE__,line)(ret, "Read file failed: %lu\n", GetLastError());
55 56 57 58 59 60 61 62 63 64 65
    CloseHandle(o.hEvent);
    if (ret) console_output_count += count;
}

#define expect_empty_output() expect_empty_output_(__LINE__)
static void expect_empty_output_(unsigned int line)
{
    DWORD avail;
    BOOL ret;

    ret = PeekNamedPipe(console_pipe, NULL, 0, NULL, &avail, NULL);
66 67
    ok_(__FILE__,line)(ret, "PeekNamedPipe failed: %lu\n", GetLastError());
    ok_(__FILE__,line)(!avail, "avail = %lu\n", avail);
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    if (avail) fetch_console_output_(line);
    ok_(__FILE__,line)(!console_output_count, "expected empty buffer, got %s\n",
                       wine_dbgstr_an(console_output, console_output_count));
    console_output_count = 0;
}

#define expect_output_sequence(a) expect_output_sequence_(__LINE__,0,a)
#define expect_output_sequence_ctx(a,b) expect_output_sequence_(__LINE__,a,b)
static void expect_output_sequence_(unsigned int line, unsigned ctx, const char *expect)
{
    size_t len = strlen(expect);
    if (console_output_count < len) fetch_console_output_(line);
    if (len <= console_output_count && !memcmp(console_output, expect, len))
    {
        console_output_count -= len;
        memmove(console_output, console_output + len, console_output_count);
    }
    else ok_(__FILE__,line)(0, "%x: expected %s got %s\n", ctx, wine_dbgstr_a(expect),
                            wine_dbgstr_an(console_output, console_output_count));
}

#define skip_sequence(a) skip_sequence_(__LINE__,a)
static BOOL skip_sequence_(unsigned int line, const char *expect)
{
    size_t len = strlen(expect);
    DWORD avail;
    BOOL r;

    r = PeekNamedPipe(console_pipe, NULL, 0, NULL, &avail, NULL);
    if (!console_output_count && r && !avail)
    {
        Sleep(50);
        r = PeekNamedPipe(console_pipe, NULL, 0, NULL, &avail, NULL);
    }
    if (r && avail) fetch_console_output_(line);

    if (len > console_output_count || memcmp(console_output, expect, len)) return FALSE;
    console_output_count -= len;
    memmove(console_output, console_output + len, console_output_count);
    return TRUE;
}

#define skip_byte(a) skip_byte_(__LINE__,a)
static BOOL skip_byte_(unsigned int line, char ch)
{
    if (!console_output_count || console_output[0] != ch) return FALSE;
    console_output_count--;
    memmove(console_output, console_output + 1, console_output_count);
    return TRUE;
}

119 120 121 122
#define expect_hide_cursor() expect_hide_cursor_(__LINE__)
static void expect_hide_cursor_(unsigned int line)
{
    if (!console_output_count) fetch_console_output_(line);
123
    ok_(__FILE__,line)(skip_sequence_(line, "\x1b[?25l") || broken(skip_sequence_(line, "\x1b[25l")),
124 125 126
                       "expected hide cursor escape\n");
}

127 128 129 130 131 132 133
#define skip_hide_cursor() skip_hide_cursor_(__LINE__)
static BOOL skip_hide_cursor_(unsigned int line)
{
    if (!console_output_count) fetch_console_output_(line);
    return skip_sequence_(line, "\x1b[25l") || broken(skip_sequence_(line, "\x1b[?25l"));
}

134 135 136 137 138 139 140
#define expect_erase_line(a) expect_erase_line_(__LINE__,a)
static BOOL expect_erase_line_(unsigned line, unsigned int cnt)
{
    char buf[16];
    if (skip_sequence("\x1b[K")) return FALSE;
    ok(broken(1), "expected erase line\n");
    sprintf(buf, "\x1b[%uX", cnt);
141
    expect_output_sequence_(line, cnt, buf);  /* erase the rest of the line */
142
    sprintf(buf, "\x1b[%uC", cnt);
143
    expect_output_sequence_(line, cnt, buf);  /* move cursor to the end of the line */
144 145 146
    return TRUE;
}

147 148
enum req_type
{
149
    REQ_CREATE_SCREEN_BUFFER,
150
    REQ_FILL_CHAR,
151
    REQ_GET_INPUT,
152
    REQ_GET_SB_INFO,
153
    REQ_READ_CONSOLE,
154 155
    REQ_READ_CONSOLE_A,
    REQ_READ_CONSOLE_FILE,
156
    REQ_READ_CONSOLE_CONTROL,
157
    REQ_SCROLL,
158
    REQ_SET_ACTIVE,
159
    REQ_SET_CURSOR,
160
    REQ_SET_INPUT_CP,
161
    REQ_SET_INPUT_MODE,
162
    REQ_SET_OUTPUT_MODE,
163
    REQ_SET_TITLE,
164
    REQ_WRITE_CHARACTERS,
165
    REQ_WRITE_CONSOLE,
166
    REQ_WRITE_OUTPUT,
167 168 169 170 171 172 173 174
};

struct pseudoconsole_req
{
    enum req_type type;
    union
    {
        WCHAR string[1];
175
        COORD coord;
176
        HANDLE handle;
177
        DWORD mode;
178
        int cp;
179
        size_t size;
180 181 182 183 184 185 186 187 188 189 190 191 192
        struct
        {
            COORD coord;
            unsigned int len;
            WCHAR buf[1];
        } write_characters;
        struct
        {
            COORD size;
            COORD coord;
            SMALL_RECT region;
            CHAR_INFO buf[1];
        } write_output;
193
        struct
194 195 196 197 198 199
        {
            SMALL_RECT rect;
            COORD dst;
            CHAR_INFO fill;
        } scroll;
        struct
200 201 202 203 204
        {
            WCHAR ch;
            DWORD count;
            COORD coord;
        } fill;
205 206 207 208 209 210
        struct
        {
            size_t size;
            DWORD mask;
            WCHAR initial[1];
        } control;
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
    } u;
};

static void child_string_request(enum req_type type, const WCHAR *title)
{
    char buf[4096];
    struct pseudoconsole_req *req = (void *)buf;
    size_t len = lstrlenW(title) + 1;
    DWORD count;
    BOOL ret;

    req->type = type;
    memcpy(req->u.string, title, len * sizeof(WCHAR));
    ret = WriteFile(child_pipe, req, FIELD_OFFSET(struct pseudoconsole_req, u.string[len]),
                    &count, NULL);
226
    ok(ret, "WriteFile failed: %lu\n", GetLastError());
227 228
}

229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
static void child_write_characters(const WCHAR *buf, unsigned int x, unsigned int y)
{
    char req_buf[4096];
    struct pseudoconsole_req *req = (void *)req_buf;
    size_t len = lstrlenW(buf);
    DWORD count;
    BOOL ret;

    req->type = REQ_WRITE_CHARACTERS;
    req->u.write_characters.coord.X = x;
    req->u.write_characters.coord.Y = y;
    req->u.write_characters.len = len;
    memcpy(req->u.write_characters.buf, buf, len * sizeof(WCHAR));
    ret = WriteFile(child_pipe, req, FIELD_OFFSET(struct pseudoconsole_req, u.write_characters.buf[len + 1]),
                    &count, NULL);
244
    ok(ret, "WriteFile failed: %lu\n", GetLastError());
245 246 247 248 249 250 251 252 253 254 255 256
}

static void child_set_cursor(const unsigned int x, unsigned int y)
{
    struct pseudoconsole_req req;
    DWORD count;
    BOOL ret;

    req.type = REQ_SET_CURSOR;
    req.u.coord.X = x;
    req.u.coord.Y = y;
    ret = WriteFile(child_pipe, &req, sizeof(req), &count, NULL);
257
    ok(ret, "WriteFile failed: %lu\n", GetLastError());
258 259
}

260 261 262 263 264 265 266 267 268
static HANDLE child_create_screen_buffer(void)
{
    struct pseudoconsole_req req;
    HANDLE handle;
    DWORD count;
    BOOL ret;

    req.type = REQ_CREATE_SCREEN_BUFFER;
    ret = WriteFile(child_pipe, &req, sizeof(req), &count, NULL);
269
    ok(ret, "WriteFile failed: %lu\n", GetLastError());
270
    ret = ReadFile(child_pipe, &handle, sizeof(handle), &count, NULL);
271
    ok(ret, "ReadFile failed: %lu\n", GetLastError());
272 273 274 275 276 277 278 279 280 281 282 283
    return handle;
}

static void child_set_active(HANDLE handle)
{
    struct pseudoconsole_req req;
    DWORD count;
    BOOL ret;

    req.type = REQ_SET_ACTIVE;
    req.u.handle = handle;
    ret = WriteFile(child_pipe, &req, sizeof(req), &count, NULL);
284
    ok(ret, "WriteFile failed: %lu\n", GetLastError());
285 286
}

287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
#define child_write_output(a,b,c,d,e,f,g,h,j,k,l,m,n) child_write_output_(__LINE__,a,b,c,d,e,f,g,h,j,k,l,m,n)
static void child_write_output_(unsigned int line, CHAR_INFO *buf, unsigned int size_x, unsigned int size_y,
                                unsigned int coord_x, unsigned int coord_y, unsigned int left,
                                unsigned int top, unsigned int right, unsigned int bottom, unsigned int out_left,
                                unsigned int out_top, unsigned int out_right, unsigned int out_bottom)
{
    char req_buf[4096];
    struct pseudoconsole_req *req = (void *)req_buf;
    SMALL_RECT region;
    DWORD count;
    BOOL ret;

    req->type = REQ_WRITE_OUTPUT;
    req->u.write_output.size.X = size_x;
    req->u.write_output.size.Y = size_y;
    req->u.write_output.coord.X = coord_x;
    req->u.write_output.coord.Y = coord_y;
    req->u.write_output.region.Left   = left;
    req->u.write_output.region.Top    = top;
    req->u.write_output.region.Right  = right;
    req->u.write_output.region.Bottom = bottom;
    memcpy(req->u.write_output.buf, buf, size_x * size_y * sizeof(*buf));
    ret = WriteFile(child_pipe, req, FIELD_OFFSET(struct pseudoconsole_req, u.write_output.buf[size_x * size_y]), &count, NULL);
310
    ok_(__FILE__,line)(ret, "WriteFile failed: %lu\n", GetLastError());
311
    ret = ReadFile(child_pipe, &region, sizeof(region), &count, NULL);
312
    ok_(__FILE__,line)(ret, "WriteFile failed: %lu\n", GetLastError());
313 314 315 316 317 318
    ok_(__FILE__,line)(region.Left == out_left, "Left = %u\n", region.Left);
    ok_(__FILE__,line)(region.Top == out_top, "Top = %u\n", region.Top);
    ok_(__FILE__,line)(region.Right == out_right, "Right = %u\n", region.Right);
    ok_(__FILE__,line)(region.Bottom == out_bottom, "Bottom = %u\n", region.Bottom);
}

319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
static void child_scroll(unsigned int src_left, unsigned int src_top, unsigned int src_right,
                         unsigned int src_bottom, unsigned int dst_x, unsigned int dst_y, WCHAR fill)
{
    struct pseudoconsole_req req;
    DWORD count;
    BOOL ret;

    req.type = REQ_SCROLL;
    req.u.scroll.rect.Left   = src_left;
    req.u.scroll.rect.Top    = src_top;
    req.u.scroll.rect.Right  = src_right;
    req.u.scroll.rect.Bottom = src_bottom;
    req.u.scroll.dst.X = dst_x;
    req.u.scroll.dst.Y = dst_y;
    req.u.scroll.fill.Char.UnicodeChar = fill;
    req.u.scroll.fill.Attributes = 0;
    ret = WriteFile(child_pipe, &req, sizeof(req), &count, NULL);
336
    ok(ret, "WriteFile failed: %lu\n", GetLastError());
337 338
}

339 340 341 342 343 344 345 346 347 348 349
static void child_fill_character(WCHAR ch, DWORD count, int x, int y)
{
    struct pseudoconsole_req req;
    BOOL ret;

    req.type = REQ_FILL_CHAR;
    req.u.fill.ch = ch;
    req.u.fill.count = count;
    req.u.fill.coord.X = x;
    req.u.fill.coord.Y = y;
    ret = WriteFile(child_pipe, &req, sizeof(req), &count, NULL);
350
    ok(ret, "WriteFile failed: %lu\n", GetLastError());
351 352
}

353 354 355 356 357 358 359 360 361
static void child_set_input_mode(HANDLE pipe, DWORD mode)
{
    struct pseudoconsole_req req;
    DWORD count;
    BOOL ret;

    req.type = REQ_SET_INPUT_MODE;
    req.u.mode = mode;
    ret = WriteFile(pipe, &req, sizeof(req), &count, NULL);
362
    ok(ret, "WriteFile failed: %lu\n", GetLastError());
363 364
}

365 366 367 368 369 370 371 372 373
static void child_set_output_mode(DWORD mode)
{
    struct pseudoconsole_req req;
    DWORD count;
    BOOL ret;

    req.type = REQ_SET_OUTPUT_MODE;
    req.u.mode = mode;
    ret = WriteFile(child_pipe, &req, sizeof(req), &count, NULL);
374
    ok(ret, "WriteFile failed: %lu\n", GetLastError());
375 376
}

377 378 379 380 381 382 383 384 385
static void child_set_input_cp(int cp)
{
    struct pseudoconsole_req req;
    DWORD count;
    BOOL ret;

    req.type = REQ_SET_INPUT_CP;
    req.u.cp = cp;
    ret = WriteFile(child_pipe, &req, sizeof(req), &count, NULL);
386
    ok(ret, "WriteFile failed: %lu\n", GetLastError());
387 388
}

389 390 391 392 393 394 395 396 397
static void child_read_console(HANDLE pipe, size_t size)
{
    struct pseudoconsole_req req;
    DWORD count;
    BOOL ret;

    req.type = REQ_READ_CONSOLE;
    req.u.size = size;
    ret = WriteFile(pipe, &req, sizeof(req), &count, NULL);
398
    ok(ret, "WriteFile failed: %lu\n", GetLastError());
399 400
}

401 402 403 404 405 406 407 408 409
static void child_read_console_a(HANDLE pipe, size_t size)
{
    struct pseudoconsole_req req;
    DWORD count;
    BOOL ret;

    req.type = REQ_READ_CONSOLE_A;
    req.u.size = size;
    ret = WriteFile(pipe, &req, sizeof(req), &count, NULL);
410
    ok(ret, "WriteFile failed: %lu\n", GetLastError());
411 412 413 414 415 416 417 418 419 420 421
}

static void child_read_console_file(HANDLE pipe, size_t size)
{
    struct pseudoconsole_req req;
    DWORD count;
    BOOL ret;

    req.type = REQ_READ_CONSOLE_FILE;
    req.u.size = size;
    ret = WriteFile(pipe, &req, sizeof(req), &count, NULL);
422
    ok(ret, "WriteFile failed: %lu\n", GetLastError());
423 424
}

425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
static void child_read_console_control(HANDLE pipe, size_t size, DWORD control, const WCHAR* recall)
{
    char tmp[4096];
    struct pseudoconsole_req *req = (void *)tmp;
    DWORD count;
    BOOL ret;

    req->type = REQ_READ_CONSOLE_CONTROL;
    req->u.control.size = size;
    req->u.control.mask = control;
    wcscpy(req->u.control.initial, recall);
    ret = WriteFile(pipe, req, sizeof(*req) + wcslen(recall) * sizeof(WCHAR), &count, NULL);
    ok(ret, "WriteFile failed: %lu\n", GetLastError());
}

440 441 442 443 444 445 446 447 448
#define child_expect_read_result(a,b) child_expect_read_result_(__LINE__,a,b)
static void child_expect_read_result_(unsigned int line, HANDLE pipe, const WCHAR *expect)
{
    size_t exlen = wcslen(expect);
    WCHAR buf[4096];
    DWORD count;
    BOOL ret;

    ret = ReadFile(pipe, buf, sizeof(buf), &count, NULL);
449 450
    ok_(__FILE__,line)(ret, "ReadFile failed: %lu\n", GetLastError());
    ok_(__FILE__,line)(count == exlen * sizeof(WCHAR), "got %lu, expected %Iu\n",
451 452 453 454 455
                       count, exlen * sizeof(WCHAR));
    buf[count / sizeof(WCHAR)] = 0;
    ok_(__FILE__,line)(!memcmp(expect, buf, count), "unexpected data %s\n", wine_dbgstr_w(buf));
}

456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
#define child_expect_read_control_result(a,b,c) child_expect_read_control_result_(__LINE__,a,b,c)
static void child_expect_read_control_result_(unsigned int line, HANDLE pipe, const WCHAR *expect, DWORD state)
{
    size_t exlen = wcslen(expect);
    WCHAR buf[4096];
    WCHAR *ptr = (void *)((char *)buf + sizeof(DWORD));
    DWORD count;
    BOOL ret;

    ret = ReadFile(pipe, buf, sizeof(buf), &count, NULL);
    ok_(__FILE__,line)(ret, "ReadFile failed: %lu\n", GetLastError());
    ok_(__FILE__,line)(count == sizeof(DWORD) + exlen * sizeof(WCHAR), "got %lu, expected %Iu\n",
                       count, sizeof(DWORD) + exlen * sizeof(WCHAR));
    buf[count / sizeof(WCHAR)] = 0;
    todo_wine_if(*(DWORD *)buf != state && *(DWORD *)buf == 0)
    ok_(__FILE__,line)(*(DWORD *)buf == state, "keyboard state: got %lx, expected %lx\n", *(DWORD *)buf, state);
    ok_(__FILE__,line)(!memcmp(expect, ptr, count - sizeof(DWORD)), "unexpected data %s %s\n", wine_dbgstr_w(ptr), wine_dbgstr_w(expect));
}

475 476 477 478 479 480 481 482 483
#define child_expect_read_result_a(a,b) child_expect_read_result_a_(__LINE__,a,b)
static void child_expect_read_result_a_(unsigned int line, HANDLE pipe, const char *expect)
{
    size_t exlen = strlen(expect);
    char buf[4096];
    DWORD count;
    BOOL ret;

    ret = ReadFile(pipe, buf, sizeof(buf), &count, NULL);
484
    ok_(__FILE__,line)(ret, "ReadFile failed: %lu\n", GetLastError());
485
    todo_wine_if(exlen && expect[exlen - 1] == '\xcc')
486
    ok_(__FILE__,line)(count == exlen, "got %lu, expected %Iu\n", count, exlen);
487 488 489 490
    buf[count] = 0;
    ok_(__FILE__,line)(!memcmp(expect, buf, count), "unexpected data %s\n", wine_dbgstr_a(buf));
}

491 492 493 494 495 496 497 498
static void expect_input(unsigned int event_type, INPUT_RECORD *record)
{
    struct pseudoconsole_req req = { REQ_GET_INPUT };
    INPUT_RECORD input;
    DWORD read;
    BOOL ret;

    ret = WriteFile(child_pipe, &req, sizeof(req), &read, NULL);
499
    ok(ret, "WriteFile failed: %lu\n", GetLastError());
500 501

    ret = ReadFile(child_pipe, &input, sizeof(input), &read, NULL);
502
    ok(ret, "ReadFile failed: %lu\n", GetLastError());
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545

    ok(input.EventType == event_type, "EventType = %u, expected %u\n", input.EventType, event_type);
    if (record) *record = input;
}

static BOOL get_key_input(unsigned int vt, INPUT_RECORD *record)
{
    static INPUT_RECORD prev_record;
    static BOOL have_prev_record;

    if (!have_prev_record)
    {
        expect_input(KEY_EVENT, &prev_record);
        have_prev_record = TRUE;
    }

    if (vt && prev_record.Event.KeyEvent.wVirtualKeyCode != vt) return FALSE;
    *record = prev_record;
    have_prev_record = FALSE;
    return TRUE;
}

#define expect_key_input(a,b,c,d) expect_key_input_(__LINE__,0,a,b,c,d)
static void expect_key_input_(unsigned int line, unsigned int ctx, WCHAR ch, unsigned int vk,
                              BOOL down, unsigned int ctrl_state)
{
    unsigned int vs = MapVirtualKeyW(vk, MAPVK_VK_TO_VSC);
    INPUT_RECORD record;

    get_key_input(0, &record);
    ok_(__FILE__,line)(record.Event.KeyEvent.bKeyDown == down, "%x: bKeyDown = %x\n",
                       ctx, record.Event.KeyEvent.bKeyDown);
    ok_(__FILE__,line)(record.Event.KeyEvent.wRepeatCount == 1, "%x: wRepeatCount = %x\n",
                       ctx, record.Event.KeyEvent.wRepeatCount);
    ok_(__FILE__,line)(record.Event.KeyEvent.uChar.UnicodeChar == ch, "%x: UnicodeChar = %x\n",
                       ctx, record.Event.KeyEvent.uChar.UnicodeChar);
    ok_(__FILE__,line)(record.Event.KeyEvent.wVirtualKeyCode == vk,
                       "%x: wVirtualKeyCode = %x, expected %x\n", ctx,
                       record.Event.KeyEvent.wVirtualKeyCode, vk);
    ok_(__FILE__,line)(record.Event.KeyEvent.wVirtualScanCode == vs,
                       "%x: wVirtualScanCode = %x expected %x\n", ctx,
                       record.Event.KeyEvent.wVirtualScanCode, vs);
    ok_(__FILE__,line)(record.Event.KeyEvent.dwControlKeyState == ctrl_state,
546
                       "%x: dwControlKeyState = %lx expected %x\n", ctx, record.Event.KeyEvent.dwControlKeyState, ctrl_state);
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
}

#define get_input_key_vt() get_input_key_vt_(__LINE__)
static unsigned int get_input_key_vt_(unsigned int line)
{
    INPUT_RECORD record;

    get_key_input(0, &record);
    ok_(__FILE__,line)(record.Event.KeyEvent.wRepeatCount == 1, "wRepeatCount = %x\n",
                       record.Event.KeyEvent.wRepeatCount);
    return record.Event.KeyEvent.wVirtualKeyCode;
}

#define expect_key_pressed(a,b,c) expect_key_pressed_(__LINE__,0,a,b,c)
#define expect_key_pressed_ctx(a,b,c,d) expect_key_pressed_(__LINE__,a,b,c,d)
static void expect_key_pressed_(unsigned int line, unsigned int ctx, WCHAR ch, unsigned int vk,
                                unsigned int ctrl_state)
{
    if (ctrl_state & SHIFT_PRESSED)
        expect_key_input_(line, ctx, 0, VK_SHIFT, TRUE, SHIFT_PRESSED);
    if (ctrl_state & LEFT_ALT_PRESSED)
        expect_key_input_(line, ctx, 0, VK_MENU, TRUE,
                          LEFT_ALT_PRESSED | (ctrl_state & SHIFT_PRESSED));
570 571 572 573
    if (ctrl_state & RIGHT_ALT_PRESSED)
        expect_key_input_(line, ctx, 0, VK_MENU, TRUE,
                          RIGHT_ALT_PRESSED | LEFT_CTRL_PRESSED | ENHANCED_KEY | (ctrl_state & SHIFT_PRESSED));
    else if (ctrl_state & LEFT_CTRL_PRESSED)
574 575 576 577
        expect_key_input_(line, ctx, 0, VK_CONTROL, TRUE,
                          LEFT_CTRL_PRESSED | (ctrl_state & (SHIFT_PRESSED | LEFT_ALT_PRESSED)));
    expect_key_input_(line, ctx, ch, vk, TRUE, ctrl_state);
    expect_key_input_(line, ctx, ch, vk, FALSE, ctrl_state);
578 579 580
    if (ctrl_state & RIGHT_ALT_PRESSED)
        expect_key_input_(line, ctx, 0, VK_MENU, FALSE, ENHANCED_KEY | (ctrl_state & SHIFT_PRESSED));
    else if (ctrl_state & LEFT_CTRL_PRESSED)
581 582 583 584 585 586 587 588
        expect_key_input_(line, ctx, 0, VK_CONTROL, FALSE,
                          ctrl_state & (SHIFT_PRESSED | LEFT_ALT_PRESSED));
    if (ctrl_state & LEFT_ALT_PRESSED)
        expect_key_input_(line, ctx, 0, VK_MENU, FALSE, ctrl_state & SHIFT_PRESSED);
    if (ctrl_state & SHIFT_PRESSED)
        expect_key_input_(line, ctx, 0, VK_SHIFT, FALSE, 0);
}

589 590 591
#define expect_char_key(a) expect_char_key_(__LINE__,a,0)
#define expect_char_key_ctrl(a,c) expect_char_key_(__LINE__,a,c)
static void expect_char_key_(unsigned int line, WCHAR ch, unsigned int ctrl)
592
{
593
    unsigned int vk;
594

595 596 597
    vk = VkKeyScanW(ch);
    if (vk == ~0) vk = 0;
    if (vk & 0x0100) ctrl |= SHIFT_PRESSED;
598
    /* Some keyboard (like German or French one) report right alt as ctrl+alt */
599
    if ((vk & 0x0600) == 0x0600 && !(ctrl & LEFT_ALT_PRESSED)) ctrl |= RIGHT_ALT_PRESSED;
600 601 602 603 604
    if (vk & 0x0200) ctrl |= LEFT_CTRL_PRESSED;
    vk &= 0xff;
    expect_key_pressed_(line, ch, ch, vk, ctrl);
}

605
static void fetch_child_sb_info(CONSOLE_SCREEN_BUFFER_INFO *info)
606 607 608 609 610 611
{
    struct pseudoconsole_req req = { REQ_GET_SB_INFO };
    DWORD read;
    BOOL ret;

    ret = WriteFile(child_pipe, &req, sizeof(req), &read, NULL);
612
    ok(ret, "WriteFile failed: %lu\n", GetLastError());
613

614
    ret = ReadFile(child_pipe, info, sizeof(*info), &read, NULL);
615
    ok(ret, "ReadFile failed: %lu\n", GetLastError());
616 617 618 619 620 621 622 623
}

#define test_cursor_pos(a,b) _test_cursor_pos(__LINE__,a,b)
static void _test_cursor_pos(unsigned line, int expect_x, int expect_y)
{
    CONSOLE_SCREEN_BUFFER_INFO info;

    fetch_child_sb_info(&info);
624 625 626 627 628 629 630

    ok_(__FILE__,line)(info.dwCursorPosition.X == expect_x, "dwCursorPosition.X = %u, expected %u\n",
                       info.dwCursorPosition.X, expect_x);
    ok_(__FILE__,line)(info.dwCursorPosition.Y == expect_y, "dwCursorPosition.Y = %u, expected %u\n",
                       info.dwCursorPosition.Y, expect_y);
}

631 632
static void test_write_console(void)
{
633 634
    child_set_output_mode(ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING);

635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
    child_string_request(REQ_WRITE_CONSOLE, L"abc");
    skip_hide_cursor();
    expect_output_sequence("abc");
    skip_sequence("\x1b[?25h");            /* show cursor */

    child_string_request(REQ_WRITE_CONSOLE, L"\tt");
    skip_hide_cursor();
    if (!skip_sequence("\x1b[3C")) expect_output_sequence("   ");
    expect_output_sequence("t");
    skip_sequence("\x1b[?25h");            /* show cursor */
    expect_empty_output();

    child_string_request(REQ_WRITE_CONSOLE, L"x\rr");
    expect_hide_cursor();
    expect_output_sequence("\rr abc   tx");
    if (!skip_sequence("\x1b[9D"))
        expect_output_sequence("\x1b[4;2H"); /* set cursor */
    expect_output_sequence("\x1b[?25h");     /* show cursor */
    expect_empty_output();

    child_string_request(REQ_WRITE_CONSOLE, L"yz\r\n");
    skip_hide_cursor();
    expect_output_sequence("yz\r\n");
    skip_sequence("\x1b[?25h");              /* show cursor */
    expect_empty_output();

    child_string_request(REQ_WRITE_CONSOLE, L"abc\r\n123\r\ncde\r");
    skip_hide_cursor();
    expect_output_sequence("abc\r\n123\r\ncde\r");
    skip_sequence("\x1b[?25h");              /* show cursor */
    expect_empty_output();

    child_set_cursor(0, 39);
    expect_hide_cursor();
    expect_output_sequence("\x1b[40;1H");    /* set cursor */
    expect_output_sequence("\x1b[?25h");     /* show cursor */
    expect_empty_output();

    child_string_request(REQ_WRITE_CONSOLE, L"yz\r\n");
    skip_hide_cursor();
    expect_output_sequence("yz\r");
    if (skip_sequence("\x1b[?25h"))          /* show cursor */
        expect_output_sequence("\x1b[?25l"); /* hide cursor */
    expect_output_sequence("\n");            /* next line */
    if (skip_sequence("\x1b[30X"))           /* erase the line */
    {
        expect_output_sequence("\x1b[30C");  /* move cursor to the end of the line */
        expect_output_sequence("\r");
    }
    skip_sequence("\x1b[?25h");              /* show cursor */
    expect_empty_output();

    child_string_request(REQ_WRITE_CONSOLE, L"");
    expect_empty_output();

    child_string_request(REQ_WRITE_CONSOLE, L"ab\n");
    skip_hide_cursor();
    expect_output_sequence("ab");
    if (skip_sequence("\x1b[?25h"))          /* show cursor */
        expect_output_sequence("\x1b[?25l"); /* hide cursor */
    expect_output_sequence("\r\n");          /* next line */
    if (skip_sequence("\x1b[30X"))           /* erase the line */
    {
        expect_output_sequence("\x1b[30C");  /* move cursor to the end of the line */
        expect_output_sequence("\r");
    }
    skip_sequence("\x1b[?25h");              /* show cursor */
    expect_empty_output();

    child_set_cursor(28, 10);
    expect_hide_cursor();
    expect_output_sequence("\x1b[11;29H");   /* set cursor */
    expect_output_sequence("\x1b[?25h");     /* show cursor */
    expect_empty_output();

    child_string_request(REQ_WRITE_CONSOLE, L"xy");
    skip_hide_cursor();
    expect_output_sequence("xy");
713
    if (!skip_sequence("\b")) skip_sequence("\r\n");
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
    skip_sequence("\x1b[?25h");              /* show cursor */
    expect_empty_output();

    child_set_cursor(28, 10);
    fetch_console_output();
    if (!skip_sequence("\b"))
    {
        expect_hide_cursor();
        expect_output_sequence("\x1b[11;29H"); /* set cursor */
        expect_output_sequence("\x1b[?25h");   /* show cursor */
    }
    expect_empty_output();

    child_string_request(REQ_WRITE_CONSOLE, L"abc");
    skip_hide_cursor();
    expect_output_sequence("\r                            ab");
    expect_output_sequence("\r\nc");
    if (expect_erase_line(29))
        expect_output_sequence("\x1b[12;2H"); /* set cursor */
    skip_sequence("\x1b[?25h");               /* show cursor */
    expect_empty_output();

    child_set_cursor(28, 39);
    expect_hide_cursor();
    expect_output_sequence("\x1b[40;29H");    /* set cursor */
    expect_output_sequence("\x1b[?25h");      /* show cursor */
    expect_empty_output();

    child_string_request(REQ_WRITE_CONSOLE, L"abc");
    skip_hide_cursor();
    expect_output_sequence("ab");
    skip_sequence("\x1b[40;29H");             /* set cursor */
    if (skip_sequence("\x1b[?25h"))           /* show cursor */
        expect_output_sequence("\x1b[?25l");  /* hide cursor */
    else
        skip_sequence("\b");
    expect_output_sequence("\r\nc");
    if (skip_sequence("\x1b[29X"))            /* erase the line */
    {
        expect_output_sequence("\x1b[29C");   /* move cursor to the end of the line */
        expect_output_sequence("\x1b[40;2H"); /* set cursor */
    }
    skip_sequence("\x1b[?25h");               /* show cursor */
    expect_empty_output();

    child_set_cursor(28, 39);
    skip_hide_cursor();
    if (!skip_sequence("\x1b[27C"))
        expect_output_sequence("\x1b[40;29H"); /* set cursor */
    skip_sequence("\x1b[?25h");                /* show cursor */
    expect_empty_output();

    child_string_request(REQ_WRITE_CONSOLE, L"XY");
    skip_hide_cursor();
    expect_output_sequence("XY");
    skip_sequence("\x1b[40;29H");             /* set cursor */
    if (skip_sequence("\x1b[?25h"))           /* show cursor */
771 772
        skip_sequence("\x1b[?25l");           /* hide cursor */
    if (!skip_sequence("\b") && skip_sequence("\r\n"))
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
    {
        expect_output_sequence("\x1b[30X");   /* erase the line */
        expect_output_sequence("\x1b[30C");   /* move cursor to the end of the line */
        expect_output_sequence("\r");         /* set cursor */
    }
    skip_sequence("\x1b[?25h");               /* show cursor */
    expect_empty_output();

    child_string_request(REQ_WRITE_CONSOLE, L"\n");
    skip_hide_cursor();
    if (!skip_sequence("\r\n"))
    {
        expect_output_sequence("\n");
        expect_output_sequence("\x1b[30X");   /* erase the line */
        expect_output_sequence("\x1b[30C");   /* move cursor to the end of the line */
        expect_output_sequence("\r");         /* set cursor */
    }
    skip_sequence("\x1b[?25h");               /* show cursor */
    expect_empty_output();

    child_set_output_mode(ENABLE_PROCESSED_OUTPUT);

    child_set_cursor(28, 11);
    expect_hide_cursor();
    expect_output_sequence("\x1b[12;29H");    /* set cursor */
    skip_sequence("\x1b[?25h");               /* show cursor */

    child_string_request(REQ_WRITE_CONSOLE, L"xyz1234");
    skip_hide_cursor();
    expect_output_sequence("43\b");
    skip_sequence("\x1b[?25h");               /* show cursor */
    expect_empty_output();

    child_set_cursor(28, 11);
    skip_hide_cursor();
    expect_output_sequence("\b");             /* backspace */
    skip_sequence("\x1b[?25h");               /* show cursor */

    child_string_request(REQ_WRITE_CONSOLE, L"xyz123");
    expect_hide_cursor();
    expect_output_sequence("23");
    if (!skip_sequence("\x1b[2D"))
        expect_output_sequence("\x1b[12;29H");/* set cursor */
    expect_output_sequence("\x1b[?25h");      /* show cursor */
    expect_empty_output();

    child_set_cursor(28, 11);
    child_string_request(REQ_WRITE_CONSOLE, L"abcdef\n\r123456789012345678901234567890xyz");
    expect_hide_cursor();
    if (skip_sequence("\x1b[?25h")) expect_hide_cursor();
    expect_output_sequence("\r                            ef\r\n");
    expect_output_sequence("xyz456789012345678901234567890");
    if (!skip_sequence("\x1b[27D"))
        expect_output_sequence("\x1b[13;4H"); /* set cursor */
    expect_output_sequence("\x1b[?25h");      /* show cursor */
    expect_empty_output();

    child_set_cursor(28, 11);
    expect_hide_cursor();
    expect_output_sequence("\x1b[12;29H");    /* set cursor */
    expect_output_sequence("\x1b[?25h");      /* show cursor */

    child_string_request(REQ_WRITE_CONSOLE, L"AB\r\n");
    skip_hide_cursor();
    expect_output_sequence("AB\r\n");
    skip_sequence("\x1b[?25h");               /* show cursor */
    expect_empty_output();

841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
    child_set_output_mode(ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING);

    child_set_cursor(28, 12);
    skip_hide_cursor();
    expect_output_sequence("\x1b[28C");       /* move cursor to the end of the line */
    skip_sequence("\x1b[?25h");               /* show cursor */
    expect_empty_output();

    child_string_request(REQ_WRITE_CONSOLE, L"ab");
    skip_hide_cursor();
    expect_output_sequence("ab");
    skip_sequence("\b");
    skip_sequence("\x1b[?25h");               /* show cursor */
    expect_empty_output();
    test_cursor_pos(29, 12);

    child_string_request(REQ_WRITE_CONSOLE, L"c");
    skip_hide_cursor();
    expect_output_sequence("\r\n");
    expect_output_sequence("c");
    skip_sequence("\x1b[?25h");               /* show cursor */
    expect_empty_output();
    test_cursor_pos(1, 13);

    child_set_cursor(28, 14);
    skip_hide_cursor();
    expect_output_sequence("\x1b[15;29H");    /* set cursor */
    skip_sequence("\x1b[?25h");               /* show cursor */
    expect_empty_output();

    child_string_request(REQ_WRITE_CONSOLE, L"x");
    skip_hide_cursor();
    expect_output_sequence("x");
    skip_sequence("\x1b[?25h");               /* show cursor */
    expect_empty_output();
    test_cursor_pos(29, 14);

    child_string_request(REQ_WRITE_CONSOLE, L"y");
    skip_hide_cursor();
    expect_output_sequence("y");
    skip_sequence("\x1b[?25h");               /* show cursor */
    expect_empty_output();
    test_cursor_pos(29, 14);

    child_string_request(REQ_WRITE_CONSOLE, L"\b");
    skip_hide_cursor();
    expect_output_sequence("\b");
    skip_sequence("\x1b[?25h");               /* show cursor */
    expect_empty_output();
    test_cursor_pos(28, 14);

    child_string_request(REQ_WRITE_CONSOLE, L"z");
    skip_hide_cursor();
    expect_output_sequence("z");
    skip_sequence("\x1b[?25h");               /* show cursor */
    expect_empty_output();
    test_cursor_pos(29, 14);

    child_string_request(REQ_WRITE_CONSOLE, L"w");
    skip_hide_cursor();
    expect_output_sequence("w");
    skip_sequence("\x1b[?25h");               /* show cursor */
    expect_empty_output();
    test_cursor_pos(29, 14);

    child_string_request(REQ_WRITE_CONSOLE, L"X");
    skip_hide_cursor();
    expect_output_sequence("\r\n");
    expect_output_sequence("X");
    skip_sequence("\x1b[?25h");               /* show cursor */
    expect_empty_output();
    test_cursor_pos(1, 15);

914
    child_set_output_mode(ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT);
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963

    child_set_cursor(28, 20);
    skip_hide_cursor();
    expect_output_sequence("\x1b[21;29H");    /* set cursor */
    skip_sequence("\x1b[?25h");               /* show cursor */
    expect_empty_output();

    child_string_request(REQ_WRITE_CONSOLE, L"ab");
    skip_hide_cursor();
    expect_output_sequence("ab");
    expect_output_sequence("\r\n");
    skip_sequence("\x1b[?25h");               /* show cursor */
    expect_empty_output();
    test_cursor_pos(0, 21);

    child_string_request(REQ_WRITE_CONSOLE, L"c");
    skip_hide_cursor();
    expect_output_sequence("c");
    skip_sequence("\x1b[?25h");               /* show cursor */
    expect_empty_output();
    test_cursor_pos(1, 21);

    child_set_cursor(28, 22);
    skip_hide_cursor();
    expect_output_sequence("\x1b[23;29H");    /* set cursor */
    skip_sequence("\x1b[?25h");               /* show cursor */
    expect_empty_output();

    child_string_request(REQ_WRITE_CONSOLE, L"x");
    skip_hide_cursor();
    expect_output_sequence("x");
    skip_sequence("\x1b[?25h");               /* show cursor */
    expect_empty_output();
    test_cursor_pos(29, 22);

    child_string_request(REQ_WRITE_CONSOLE, L"y");
    skip_hide_cursor();
    expect_output_sequence("y");
    expect_output_sequence("\r\n");
    skip_sequence("\x1b[?25h");               /* show cursor */
    expect_empty_output();
    test_cursor_pos(0, 23);

    child_string_request(REQ_WRITE_CONSOLE, L"z");
    skip_hide_cursor();
    expect_output_sequence("z");
    skip_sequence("\x1b[?25h");               /* show cursor */
    expect_empty_output();
    test_cursor_pos(1, 23);
964 965
}

966 967 968
static void test_tty_output(void)
{
    CHAR_INFO char_info_buf[2048], char_info;
969
    HANDLE sb, sb2;
970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
    unsigned int i;

    /* simple write chars */
    child_write_characters(L"child", 3, 4);
    expect_hide_cursor();
    expect_output_sequence("\x1b[5;4H");   /* set cursor */
    expect_output_sequence("child");
    expect_output_sequence("\x1b[H");      /* set cursor */
    expect_output_sequence("\x1b[?25h");   /* show cursor */
    expect_empty_output();

    /* wrapped write chars */
    child_write_characters(L"bound", 28, 6);
    expect_hide_cursor();
    expect_output_sequence("\x1b[7;1H");   /* set cursor */
    expect_output_sequence("                            bo\r\nund");
    expect_erase_line(27);
    expect_output_sequence("\x1b[H");      /* set cursor */
    expect_output_sequence("\x1b[?25h");   /* show cursor */
    expect_empty_output();

    /* fill line 4 with a few simple writes */
    child_write_characters(L"xxx", 13, 4);
    expect_hide_cursor();
    expect_output_sequence("\x1b[5;14H");  /* set cursor */
    expect_output_sequence("xxx");
    expect_output_sequence("\x1b[H");      /* set cursor */
    expect_output_sequence("\x1b[?25h");   /* show cursor */
    expect_empty_output();

    /* write one char at the end of row */
    child_write_characters(L"y", 29, 4);
    expect_hide_cursor();
    expect_output_sequence("\x1b[5;30H");  /* set cursor */
    expect_output_sequence("y");
    expect_output_sequence("\x1b[H");      /* set cursor */
    expect_output_sequence("\x1b[?25h");   /* show cursor */
    expect_empty_output();

    /* wrapped write chars */
    child_write_characters(L"zz", 29, 4);
    expect_hide_cursor();
    expect_output_sequence("\x1b[5;1H");   /* set cursor */
    expect_output_sequence("   child     xxx             z");
    expect_output_sequence("\r\nz");
    expect_erase_line(29);
    expect_output_sequence("\x1b[H");      /* set cursor */
    expect_output_sequence("\x1b[?25h");   /* show cursor */
    expect_empty_output();

    /* trailing spaces */
    child_write_characters(L"child        ", 3, 4);
    expect_hide_cursor();
    expect_output_sequence("\x1b[5;4H");   /* set cursor */
    expect_output_sequence("child        ");
    expect_output_sequence("\x1b[H");      /* set cursor */
    expect_output_sequence("\x1b[?25h");   /* show cursor */
    expect_empty_output();

    child_set_cursor(2, 3);
    expect_hide_cursor();
    expect_output_sequence("\x1b[4;3H");   /* set cursor */
    expect_output_sequence("\x1b[?25h");   /* show cursor */
    expect_empty_output();

    child_string_request(REQ_SET_TITLE, L"new title");
    fetch_console_output();
    skip_sequence("\x1b[?25l");            /* hide cursor */
    expect_output_sequence("\x1b]0;new title\x07"); /* set title */
    skip_sequence("\x1b[?25h");            /* show cursor */
    expect_empty_output();

    for (i = 0; i < ARRAY_SIZE(char_info_buf); i++)
    {
        char_info_buf[i].Char.UnicodeChar = '0' + i % 10;
        char_info_buf[i].Attributes = 0;
    }

    child_write_output(char_info_buf, /* size */ 7, 8, /* coord */ 1, 2,
                       /* region */ 3, 7, 5, 9, /* out region */ 3, 7, 5, 9);
    expect_hide_cursor();
    expect_output_sequence("\x1b[30m");    /* foreground black */
    expect_output_sequence("\x1b[8;4H");   /* set cursor */
    expect_output_sequence("567");
    expect_output_sequence("\x1b[9;4H");   /* set cursor */
    expect_output_sequence("234");
    expect_output_sequence("\x1b[10;4H");  /* set cursor */
    expect_output_sequence("901");
    expect_output_sequence("\x1b[4;3H");   /* set cursor */
    expect_output_sequence("\x1b[?25h");   /* show cursor */
    expect_empty_output();

    child_write_output(char_info_buf, /* size */ 2, 3, /* coord */ 1, 2,
                       /* region */ 3, 8, 15, 19, /* out region */ 3, 8, 3, 8);
    expect_hide_cursor();
    if (skip_sequence("\x1b[m"))           /* default attr */
        expect_output_sequence("\x1b[30m");/* foreground black */
    expect_output_sequence("\x1b[9;4H");   /* set cursor */
    expect_output_sequence("5");
    expect_output_sequence("\x1b[4;3H");   /* set cursor */
    expect_output_sequence("\x1b[?25h");   /* show cursor */
    expect_empty_output();

    child_write_output(char_info_buf, /* size */ 3, 4, /* coord */ 1, 2,
                       /* region */ 3, 8, 15, 19, /* out region */ 3, 8, 4, 9);
    expect_hide_cursor();
    if (skip_sequence("\x1b[m"))           /* default attr */
        expect_output_sequence("\x1b[30m");/* foreground black */
    expect_output_sequence("\x1b[9;4H");   /* set cursor */
    expect_output_sequence("78");
    expect_output_sequence("\x1b[10;4H");  /* set cursor */
    expect_output_sequence("01");
    expect_output_sequence("\x1b[4;3H");   /* set cursor */
    expect_output_sequence("\x1b[?25h");   /* show cursor */
    expect_empty_output();

    child_write_output(char_info_buf, /* size */ 7, 8, /* coord */ 2, 3,
                       /* region */ 28, 38, 31, 60, /* out region */ 28, 38, 29, 39);
    expect_hide_cursor();
    if (skip_sequence("\x1b[m"))           /* default attr */
        expect_output_sequence("\x1b[30m");/* foreground black */
    expect_output_sequence("\x1b[39;29H"); /* set cursor */
    expect_output_sequence("34");
    expect_output_sequence("\x1b[40;29H"); /* set cursor */
    expect_output_sequence("01");
    expect_output_sequence("\x1b[4;3H");   /* set cursor */
    expect_output_sequence("\x1b[?25h");   /* show cursor */
    expect_empty_output();

    child_write_output(char_info_buf, /* size */ 7, 8, /* coord */ 1, 2,
                       /* region */ 0, 7, 5, 9, /* out region */ 0, 7, 5, 9);
    expect_hide_cursor();
    if (skip_sequence("\x1b[m"))           /* default attr */
        expect_output_sequence("\x1b[30m");/* foreground black */
    expect_output_sequence("\x1b[8;1H");   /* set cursor */
    expect_output_sequence("567890\r\n");
    expect_output_sequence("234567\r\n");
    expect_output_sequence("901234");
    expect_output_sequence("\x1b[4;3H");   /* set cursor */
    expect_output_sequence("\x1b[?25h");   /* show cursor */
    expect_empty_output();

1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
    child_scroll(/* scroll rect */ 0, 7, 2, 8, /* destination */ 2, 8, /* fill */ 'x');
    expect_hide_cursor();
    if (skip_sequence("\x1b[m"))           /* default attr */
        expect_output_sequence("\x1b[30m");/* foreground black */
    expect_output_sequence("\x1b[8;1H");   /* set cursor */
    expect_output_sequence("xxx89\r\n");
    expect_output_sequence("xx567\r\n");
    expect_output_sequence("90234");
    expect_output_sequence("\x1b[4;3H");   /* set cursor */
    expect_output_sequence("\x1b[?25h");   /* show cursor */
    expect_empty_output();

1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
    child_write_characters(L"xxx", 3, 10);
    expect_hide_cursor();
    expect_output_sequence("\x1b[m");      /* default attributes */
    expect_output_sequence("\x1b[11;4H");  /* set cursor */
    expect_output_sequence("xxx");
    expect_output_sequence("\x1b[4;3H");   /* set cursor */
    expect_output_sequence("\x1b[?25h");   /* show cursor */
    expect_empty_output();

    /* test attributes */
    for (i = 0; i < 0x100 - 0xff; i++)
    {
        unsigned int expect;
        char expect_buf[16];
        char_info.Char.UnicodeChar = 'a';
        char_info.Attributes = i;
        child_write_output(&char_info, /* size */ 1, 1, /* coord */ 0, 0,
                           /* region */ 12, 3, 12, 3, /* out region */ 12, 3, 12, 3);
        expect_hide_cursor();
        if (i != 0x190 && i && ((i & 0xff) != 8)) expect_output_sequence_ctx(i, "\x1b[m");
        if ((i & 0x0f) != 7)
        {
            expect = 30;
            if (i & FOREGROUND_BLUE)  expect += 4;
            if (i & FOREGROUND_GREEN) expect += 2;
            if (i & FOREGROUND_RED)   expect += 1;
            if (i & FOREGROUND_INTENSITY) expect += 60;
            sprintf(expect_buf, "\x1b[%um", expect);
            expect_output_sequence_ctx(i, expect_buf);
        }
        if (i & 0xf0)
        {
            expect = 40;
            if (i & BACKGROUND_BLUE)  expect += 4;
            if (i & BACKGROUND_GREEN) expect += 2;
            if (i & BACKGROUND_RED)   expect += 1;
            if (i & BACKGROUND_INTENSITY) expect += 60;
            sprintf(expect_buf, "\x1b[%um", expect);
            expect_output_sequence_ctx(i, expect_buf);
        }
        if (!skip_sequence("\x1b[10C"))
            expect_output_sequence_ctx(i, "\x1b[4;13H"); /* set cursor */
        expect_output_sequence("a");
        if (!skip_sequence("\x1b[11D"))
            expect_output_sequence("\x1b[4;3H"); /* set cursor */
        expect_output_sequence("\x1b[?25h");     /* show cursor */
        expect_empty_output();
    }

    char_info_buf[0].Attributes = FOREGROUND_GREEN;
    char_info_buf[1].Attributes = FOREGROUND_GREEN | BACKGROUND_RED;
    char_info_buf[2].Attributes = BACKGROUND_RED;
    child_write_output(char_info_buf, /* size */ 7, 8, /* coord */ 0, 0,
                       /* region */ 7, 0, 9, 0, /* out region */ 7, 0, 9, 0);
    expect_hide_cursor();
    skip_sequence("\x1b[m");               /* default attr */
    expect_output_sequence("\x1b[32m");    /* foreground black */
    expect_output_sequence("\x1b[1;8H");   /* set cursor */
    expect_output_sequence("0");
1183
    expect_output_sequence("\x1b[41m");    /* background red */
1184 1185 1186 1187 1188 1189
    expect_output_sequence("1");
    expect_output_sequence("\x1b[30m");    /* foreground black */
    expect_output_sequence("2");
    expect_output_sequence("\x1b[4;3H");   /* set cursor */
    expect_output_sequence("\x1b[?25h");   /* show cursor */
    expect_empty_output();
1190 1191 1192 1193 1194 1195 1196 1197 1198

    child_fill_character('i', 5, 15, 16);
    expect_hide_cursor();
    expect_output_sequence("\x1b[m");      /* default attributes */
    expect_output_sequence("\x1b[17;16H"); /* set cursor */
    expect_output_sequence("iiiii");
    expect_output_sequence("\x1b[4;3H");   /* set cursor */
    expect_output_sequence("\x1b[?25h");   /* show cursor */
    expect_empty_output();
1199

1200 1201
    test_write_console();

1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
    sb = child_create_screen_buffer();
    child_set_active(sb);
    expect_hide_cursor();
    expect_output_sequence("\x1b[H");      /* set cursor */
    for (i = 0; i < 40; i++)
    {
        expect_erase_line(30);
        if (i != 39) expect_output_sequence("\r\n");
    }
    expect_output_sequence("\x1b[H");      /* set cursor */
    expect_output_sequence("\x1b[?25h");   /* show cursor */
    expect_empty_output();

    child_write_characters(L"new sb", 0, 0);
    skip_hide_cursor();
    expect_output_sequence("new sb");
    ok(skip_sequence("\x1b[H") || skip_sequence("\r"), "expected set cursor\n");
    skip_sequence("\x1b[?25h");            /* show cursor */
    expect_empty_output();

    sb2 = child_create_screen_buffer();
    child_set_active(sb2);
    expect_hide_cursor();
    for (i = 0; i < 40; i++)
    {
        expect_erase_line(30);
        if (i != 39) expect_output_sequence("\r\n");
    }
    expect_output_sequence("\x1b[H");      /* set cursor */
    expect_output_sequence("\x1b[?25h");   /* show cursor */
    expect_empty_output();

    child_set_active(sb);
    expect_hide_cursor();
    expect_output_sequence("new sb");
    expect_erase_line(24);
    expect_output_sequence("\r\n");
    for (i = 1; i < 40; i++)
    {
        expect_erase_line(30);
        if (i != 39) expect_output_sequence("\r\n");
    }
    expect_output_sequence("\x1b[H");      /* set cursor */
    expect_output_sequence("\x1b[?25h");   /* show cursor */
    expect_empty_output();
1247 1248
}

1249 1250 1251 1252 1253
static void write_console_pipe(const char *buf)
{
    DWORD written;
    BOOL res;
    res = WriteFile(console_pipe, buf, strlen(buf), &written, NULL);
1254
    ok(res, "WriteFile failed: %lu\n", GetLastError());
1255 1256
}

1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
static void test_read_console(void)
{
    child_set_input_mode(child_pipe, ENABLE_PROCESSED_INPUT);

    child_read_console(child_pipe, 100);
    write_console_pipe("abc");
    expect_empty_output();
    child_expect_read_result(child_pipe, L"abc");
    expect_empty_output();

    child_read_console(child_pipe, 1);
    write_console_pipe("xyz");
    child_expect_read_result(child_pipe, L"x");
    child_read_console(child_pipe, 100);
    child_expect_read_result(child_pipe, L"yz");
    expect_empty_output();

1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315
    child_set_input_cp(932);

    child_read_console_a(child_pipe, 2);
    write_console_pipe("\xe3\x81\x81");
    child_expect_read_result_a(child_pipe, "\x82\x9f");
    expect_empty_output();

    child_read_console_a(child_pipe, 1);
    write_console_pipe("\xe3\x81\x81""a");
    child_expect_read_result_a(child_pipe, "\x82\xcc");
    child_read_console_a(child_pipe, 1);
    child_expect_read_result_a(child_pipe, "a");
    expect_empty_output();

    child_read_console_a(child_pipe, 2);
    write_console_pipe("a\xe3\x81\x81""b");
    child_expect_read_result_a(child_pipe, "a\x82\xcc");
    child_read_console_a(child_pipe, 1);
    child_expect_read_result_a(child_pipe, "b");
    expect_empty_output();

    child_read_console_file(child_pipe, 2);
    write_console_pipe("\xe3\x81\x81");
    child_expect_read_result_a(child_pipe, "\x82\x9f");
    expect_empty_output();

    child_read_console_file(child_pipe, 1);
    write_console_pipe("\xe3\x81\x81""a");
    child_expect_read_result_a(child_pipe, "\x82\xcc");
    child_read_console_file(child_pipe, 1);
    child_expect_read_result_a(child_pipe, "a");
    expect_empty_output();

    child_read_console_file(child_pipe, 2);
    write_console_pipe("a\xe3\x81\x81""b");
    child_expect_read_result_a(child_pipe, "a\x82\xcc");
    child_read_console_file(child_pipe, 1);
    child_expect_read_result_a(child_pipe, "b");
    expect_empty_output();

    child_set_input_cp(437);

1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
    child_set_input_mode(child_pipe, ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
                         ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT | ENABLE_INSERT_MODE |
                         ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS | ENABLE_AUTO_POSITION);

    child_read_console(child_pipe, 100);
    write_console_pipe("xyz");
    skip_hide_cursor();
    expect_output_sequence("xyz");
    skip_sequence("\x1b[?25h"); /* show cursor */
    write_console_pipe("ab\r\n");
    child_expect_read_result(child_pipe, L"xyzab\r\n");
    skip_hide_cursor();
    expect_output_sequence("ab\r\n");
    skip_sequence("\x1b[?25h"); /* show cursor */
    expect_key_input('\r', VK_RETURN, 0, FALSE);
    expect_key_pressed('\n', VK_RETURN, LEFT_CTRL_PRESSED);
    expect_empty_output();
}

1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
static void test_read_console_control(void)
{
    CONSOLE_SCREEN_BUFFER_INFO info1, info2;
    char ctrl;
    char buf[16];
    WCHAR bufw[16];

    child_set_input_mode(child_pipe, ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
                         ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT | ENABLE_INSERT_MODE |
                         ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS | ENABLE_AUTO_POSITION);

    /* test simple behavior */
    for (ctrl = 0; ctrl < ' '; ctrl++)
    {
1349 1350 1351
        DWORD state;
        SHORT cc;

1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
        /* don't play with fire */
        if (ctrl == 0 || ctrl == 3 || ctrl == '\n' || ctrl == 27) continue;

        /* Simulate initial characters
         * Note: as ReadConsole with CONTROL structure will need to back up the cursor
         * up to the length of the initial characters, all the following tests ensure that
         * this backup doesn't imply backing up to the previous line.
         * This is the "regular" behavior when using completion.
         */
        child_string_request(REQ_WRITE_CONSOLE, L"\rabc");
        skip_sequence("\x1b[25l");  /* broken hide cursor */
        skip_sequence("\x1b[?25l"); /* hide cursor */
        skip_sequence("\x1b[H");
        skip_sequence("\r");
        expect_output_sequence("abc");
        skip_sequence("\x1b[?25h"); /* show cursor */

        fetch_child_sb_info(&info1);
        child_read_console_control(child_pipe, 100, 1ul << ctrl, L"abc");
        strcpy(buf, "def."); buf[3] = ctrl;
        write_console_pipe(buf);
        wcscpy(bufw, L"abcdef."); bufw[6] = (WCHAR)ctrl;
1374 1375 1376 1377 1378 1379 1380
        /* ^H requires special handling */
        cc = (ctrl == 8) ? 0x0200 : VkKeyScanA(ctrl);
        if (cc == -1) cc = 0;
        state = 0;
        if (cc & 0x0100) state |= SHIFT_PRESSED;
        if (cc & 0x0200) state |= LEFT_CTRL_PRESSED;
        child_expect_read_control_result(child_pipe, bufw, state);
1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442
        skip_sequence("\x1b[?25l"); /* hide cursor */
        expect_output_sequence("def");
        skip_sequence("\x1b[?25h"); /* show cursor */
        fetch_child_sb_info(&info2);
        ok(info1.dwCursorPosition.X + 3 == info2.dwCursorPosition.X,
           "Bad x-position: expected %u => %u but got => %u instead\n",
           info1.dwCursorPosition.X, info1.dwCursorPosition.X + 3, info2.dwCursorPosition.X);
        ok(info1.dwCursorPosition.Y == info2.dwCursorPosition.Y, "Cursor shouldn't have changed line\n");
    }

    /* test two different control characters in input */
    fetch_child_sb_info(&info1);
    child_read_console_control(child_pipe, 100, 1ul << '\x01', L"abc");
    write_console_pipe("d\x02""ef\x01");
    child_expect_read_control_result(child_pipe, L"abcd\x02""ef\x01", LEFT_CTRL_PRESSED);
    skip_sequence("\x1b[?25l"); /* hide cursor */
    expect_output_sequence("d^Bef");
    skip_sequence("\x1b[?25h"); /* show cursor */
    fetch_child_sb_info(&info2);
    ok(info1.dwCursorPosition.X + 5 == info2.dwCursorPosition.X,
       "Bad x-position: expected %u => %u but got => %u instead\n",
       info1.dwCursorPosition.X, info1.dwCursorPosition.X + 5, info2.dwCursorPosition.X);
    ok(info1.dwCursorPosition.Y == info2.dwCursorPosition.Y, "Cursor shouldn't have changed line\n");

    /* test that ctrl character not in mask is handled as without ctrl mask */
    child_read_console_control(child_pipe, 100, 1ul << '\x01', L"abc");
    write_console_pipe("d\ref\x01");
    child_expect_read_control_result(child_pipe, L"abcd\r\n", 0);
    skip_sequence("\x1b[?25l"); /* hide cursor */
    expect_output_sequence("d\r\n");
    skip_sequence("\x1b[?25h"); /* show cursor */
    expect_empty_output();
    /* see note above... ditto */
    child_string_request(REQ_WRITE_CONSOLE, L"abc");
    skip_sequence("\x1b[?25l"); /* hide cursor */
    expect_output_sequence("abc");
    skip_sequence("\x1b[?25h"); /* show cursor */

    child_read_console_control(child_pipe, 100, 1ul << '\x01', L"abc");
    child_expect_read_control_result(child_pipe, L"abcef\x01", LEFT_CTRL_PRESSED);
    skip_sequence("\x1b[?25l"); /* hide cursor */
    expect_output_sequence("ef");
    skip_sequence("\x1b[?25h"); /* show cursor */

    /* test when output buffer becomes full before control event */
    child_read_console_control(child_pipe, 4, 1ul << '\x01', L"abc");
    write_console_pipe("def\x01");
    child_expect_read_control_result(child_pipe, L"abcd", LEFT_CTRL_PRESSED);
    skip_sequence("\x1b[?25l"); /* hide cursor */
    expect_output_sequence("def");
    skip_sequence("\x1b[?25h"); /* show cursor */
    expect_empty_output();
    child_read_console_control(child_pipe, 20, 1ul << '\x01', L"abc");
    child_expect_read_control_result(child_pipe, L"ef\x01", 0);

    /* TODO: add tests:
     * - when initial characters go back to previous line
     * - edition inside initial characters can occur
     */
    expect_empty_output();
}

1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
static void test_tty_input(void)
{
    INPUT_RECORD ir;
    unsigned int i;
    char buf[8];

    static const struct
    {
        const char *str;
        unsigned int vk;
        unsigned int ctrl;
    } escape_test[] = {
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470
        { "\x1b[A",          VK_UP,       0 },
        { "\x1b[B",          VK_DOWN,     0 },
        { "\x1b[C",          VK_RIGHT,    0 },
        { "\x1b[D",          VK_LEFT,     0 },
        { "\x1b[H",          VK_HOME,     0 },
        { "\x1b[F",          VK_END,      0 },
        { "\x1b[2~",         VK_INSERT,   0 },
        { "\x1b[3~",         VK_DELETE,   0 },
        { "\x1b[5~",         VK_PRIOR,    0 },
        { "\x1b[6~",         VK_NEXT,     0 },
        { "\x1b[15~",        VK_F5,       0 },
        { "\x1b[17~",        VK_F6,       0 },
        { "\x1b[18~",        VK_F7,       0 },
        { "\x1b[19~",        VK_F8,       0 },
        { "\x1b[20~",        VK_F9,       0 },
        { "\x1b[21~",        VK_F10,      0 },
1471
        /* 0x10 */
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
        { "\x1b[23~",        VK_F11,      0 },
        { "\x1b[24~",        VK_F12,      0 },
        { "\x1bOP",          VK_F1,       0 },
        { "\x1bOQ",          VK_F2,       0 },
        { "\x1bOR",          VK_F3,       0 },
        { "\x1bOS",          VK_F4,       0 },
        { "\x1b[1;1A",       VK_UP,       0 },
        { "\x1b[1;2A",       VK_UP,       SHIFT_PRESSED },
        { "\x1b[1;3A",       VK_UP,       LEFT_ALT_PRESSED },
        { "\x1b[1;4A",       VK_UP,       SHIFT_PRESSED | LEFT_ALT_PRESSED },
        { "\x1b[1;5A",       VK_UP,       LEFT_CTRL_PRESSED },
        { "\x1b[1;6A",       VK_UP,       SHIFT_PRESSED | LEFT_CTRL_PRESSED },
        { "\x1b[1;7A",       VK_UP,       LEFT_ALT_PRESSED  | LEFT_CTRL_PRESSED },
        { "\x1b[1;8A",       VK_UP,       SHIFT_PRESSED | LEFT_ALT_PRESSED  | LEFT_CTRL_PRESSED },
        { "\x1b[1;9A",       VK_UP,       0 },
        { "\x1b[1;10A",      VK_UP,       SHIFT_PRESSED },
1488
        /* 0x20 */
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512
        { "\x1b[1;11A",      VK_UP,       LEFT_ALT_PRESSED },
        { "\x1b[1;12A",      VK_UP,       SHIFT_PRESSED | LEFT_ALT_PRESSED },
        { "\x1b[1;13A",      VK_UP,       LEFT_CTRL_PRESSED },
        { "\x1b[1;14A",      VK_UP,       SHIFT_PRESSED | LEFT_CTRL_PRESSED },
        { "\x1b[1;15A",      VK_UP,       LEFT_ALT_PRESSED  | LEFT_CTRL_PRESSED },
        { "\x1b[1;16A",      VK_UP,       SHIFT_PRESSED | LEFT_ALT_PRESSED  | LEFT_CTRL_PRESSED },
        { "\x1b[1;2P",       VK_F1,       SHIFT_PRESSED },
        { "\x1b[2;3~",       VK_INSERT,   LEFT_ALT_PRESSED },
        { "\x1b[2;3;5;6~",   VK_INSERT,   0 },
        { "\x1b[6;2;3;5;1~", VK_NEXT,     0 },
    };
    static const struct
    {
        const char *str;
        WCHAR ch;
        unsigned int ctrl;
    } escape_char_test[] = {
        { "\x1b[Z",          0x9,    SHIFT_PRESSED },
        { "\xe4\xb8\x80",    0x4e00, 0 },
        { "\x1b\x1b",        0x1b,   LEFT_ALT_PRESSED },
        { "\x1b""1",         '1',    LEFT_ALT_PRESSED },
        { "\x1b""x",         'x',    LEFT_ALT_PRESSED },
        { "\x1b""[",         '[',    LEFT_ALT_PRESSED },
        { "\x7f",            '\b',   0 },
1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563
    };

    write_console_pipe("x");
    if (!get_input_key_vt())
    {
        skip("Skipping tests on settings that don't have VT mapping for 'x'\n");
        get_input_key_vt();
        return;
    }
    get_input_key_vt();

    write_console_pipe("aBCd");
    expect_char_key('a');
    expect_char_key('B');
    expect_char_key('C');
    expect_char_key('d');

    for (i = 1; i < 0x7f; i++)
    {
        if (i == 3 || i == '\n' || i == 0x1b || i == 0x1f) continue;
        buf[0] = i;
        buf[1] = 0;
        write_console_pipe(buf);
        if (i == 8)
            expect_key_pressed('\b', 'H', LEFT_CTRL_PRESSED);
        else if (i == 0x7f)
            expect_char_key(8);
        else
            expect_char_key(i);
    }

    write_console_pipe("\r\n");
    expect_key_pressed('\r', VK_RETURN, 0);
    expect_key_pressed('\n', VK_RETURN, LEFT_CTRL_PRESSED);

    write_console_pipe("\xc4\x85");
    if (get_key_input(VK_MENU, &ir))
    {
        expect_key_input(0x105, 'A', TRUE, LEFT_CTRL_PRESSED | RIGHT_ALT_PRESSED);
        expect_key_input(0x105, 'A', FALSE, LEFT_CTRL_PRESSED | RIGHT_ALT_PRESSED);
        expect_key_input(0, VK_MENU, FALSE, ENHANCED_KEY);
    }
    else
    {
        expect_key_input(0x105, 0, TRUE, 0);
        expect_key_input(0x105, 0, FALSE, 0);
    }

    for (i = 0; i < ARRAY_SIZE(escape_test); i++)
    {
        write_console_pipe(escape_test[i].str);
1564 1565 1566 1567 1568 1569 1570
        expect_key_pressed_ctx(i, 0, escape_test[i].vk, escape_test[i].ctrl);
    }

    for (i = 0; i < ARRAY_SIZE(escape_char_test); i++)
    {
        write_console_pipe(escape_char_test[i].str);
        expect_char_key_ctrl(escape_char_test[i].ch, escape_char_test[i].ctrl);
1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581
    }

    for (i = 0x80; i < 0x100; i += 11)
    {
        buf[0] = i;
        buf[1] = 0;
        write_console_pipe(buf);
        expect_empty_output();
    }
}

1582 1583 1584
static void child_process(HANDLE pipe)
{
    HANDLE output, input;
1585
    DWORD size, count;
1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599
    char buf[4096];
    BOOL ret;

    output = CreateFileA("CONOUT$", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
    ok(output != INVALID_HANDLE_VALUE, "could not open console output\n");

    input = CreateFileA("CONIN$", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
    ok(output != INVALID_HANDLE_VALUE, "could not open console output\n");

    while(ReadFile(pipe, buf, sizeof(buf), &size, NULL))
    {
        const struct pseudoconsole_req *req = (void *)buf;
        switch (req->type)
        {
1600 1601 1602 1603 1604 1605 1606
        case REQ_CREATE_SCREEN_BUFFER:
            {
                HANDLE handle;
                SetLastError(0xdeadbeef);
                handle = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE,
                                                   FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
                                                   CONSOLE_TEXTMODE_BUFFER, NULL);
1607
                ok(handle != INVALID_HANDLE_VALUE, "CreateConsoleScreenBuffer failed: %lu\n", GetLastError());
1608
                ret = WriteFile(pipe, &handle, sizeof(handle), &count, NULL);
1609
                ok(ret, "WriteFile failed: %lu\n", GetLastError());
1610 1611 1612
                break;
            }

1613 1614 1615 1616
        case REQ_GET_INPUT:
            {
                INPUT_RECORD record;
                ret = ReadConsoleInputW(input, &record, 1, &count);
1617 1618
                ok(ret, "ReadConsoleInputW failed: %lu\n", GetLastError());
                ok(count == 1, "count = %lu\n", count);
1619
                ret = WriteFile(pipe, &record, sizeof(record), &count, NULL);
1620
                ok(ret, "WriteFile failed: %lu\n", GetLastError());
1621 1622 1623
                break;
            }

1624 1625 1626 1627
        case REQ_GET_SB_INFO:
            {
                CONSOLE_SCREEN_BUFFER_INFO info;
                ret = GetConsoleScreenBufferInfo(output, &info);
1628
                ok(ret, "GetConsoleScreenBufferInfo failed: %lu\n", GetLastError());
1629
                ret = WriteFile(pipe, &info, sizeof(info), &count, NULL);
1630
                ok(ret, "WriteFile failed: %lu\n", GetLastError());
1631 1632 1633
                break;
            }

1634 1635
        case REQ_READ_CONSOLE:
            ret = ReadConsoleW(input, buf, req->u.size, &count, NULL );
1636
            ok(ret, "ReadConsoleW failed: %lu\n", GetLastError());
1637
            ret = WriteFile(pipe, buf, count * sizeof(WCHAR), NULL, NULL);
1638
            ok(ret, "WriteFile failed: %lu\n", GetLastError());
1639 1640
            break;

1641 1642 1643 1644
        case REQ_READ_CONSOLE_A:
            count = req->u.size;
            memset(buf, 0xcc, sizeof(buf));
            ret = ReadConsoleA(input, buf, count, &count, NULL );
1645
            ok(ret, "ReadConsoleA failed: %lu\n", GetLastError());
1646
            ret = WriteFile(pipe, buf, count, NULL, NULL);
1647
            ok(ret, "WriteFile failed: %lu\n", GetLastError());
1648 1649 1650 1651 1652 1653
            break;

        case REQ_READ_CONSOLE_FILE:
            count = req->u.size;
            memset(buf, 0xcc, sizeof(buf));
            ret = ReadFile(input, buf, count, &count, NULL );
1654
            ok(ret, "ReadFile failed: %lu\n", GetLastError());
1655
            ret = WriteFile(pipe, buf, count, NULL, NULL);
1656
            ok(ret, "WriteFile failed: %lu\n", GetLastError());
1657 1658
            break;

1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679
        case REQ_READ_CONSOLE_CONTROL:
            {
                CONSOLE_READCONSOLE_CONTROL crc;
                WCHAR result[1024];
                WCHAR *ptr = (void *)((char *)result + sizeof(DWORD));

                count = req->u.control.size;
                memset(result, 0xcc, sizeof(result));
                crc.nLength = sizeof(crc);
                crc.dwCtrlWakeupMask = req->u.control.mask;
                crc.nInitialChars = wcslen(req->u.control.initial);
                crc.dwConsoleKeyState = 0xa5;
                memcpy(ptr, req->u.control.initial, crc.nInitialChars * sizeof(WCHAR));
                ret = ReadConsoleW(input, ptr, count, &count, &crc);
                ok(ret, "ReadConsoleW failed: %lu\n", GetLastError());
                *(DWORD *)result = crc.dwConsoleKeyState;
                ret = WriteFile(pipe, result, sizeof(DWORD) + count * sizeof(WCHAR), NULL, NULL);
                ok(ret, "WriteFile failed: %lu\n", GetLastError());
            }
            break;

1680 1681
        case REQ_SCROLL:
            ret = ScrollConsoleScreenBufferW(output, &req->u.scroll.rect, NULL, req->u.scroll.dst, &req->u.scroll.fill);
1682
            ok(ret, "ScrollConsoleScreenBuffer failed: %lu\n", GetLastError());
1683 1684
            break;

1685 1686
        case REQ_FILL_CHAR:
            ret = FillConsoleOutputCharacterW(output, req->u.fill.ch, req->u.fill.count, req->u.fill.coord, &count);
1687 1688
            ok(ret, "FillConsoleOutputCharacter failed: %lu\n", GetLastError());
            ok(count == req->u.fill.count, "count = %lu, expected %lu\n", count, req->u.fill.count);
1689 1690
            break;

1691 1692 1693
        case REQ_SET_ACTIVE:
            output = req->u.handle;
            ret = SetConsoleActiveScreenBuffer(output);
1694
            ok(ret, "SetConsoleActiveScreenBuffer failed: %lu\n", GetLastError());
1695 1696
            break;

1697 1698
        case REQ_SET_CURSOR:
            ret = SetConsoleCursorPosition(output, req->u.coord);
1699
            ok(ret, "SetConsoleCursorPosition failed: %lu\n", GetLastError());
1700 1701
            break;

1702 1703
        case REQ_SET_INPUT_CP:
            ret = SetConsoleCP(req->u.cp);
1704
            ok(ret, "SetConsoleCP failed: %lu\n", GetLastError());
1705 1706
            break;

1707 1708
        case REQ_SET_INPUT_MODE:
            ret = SetConsoleMode(input, req->u.mode);
1709
            ok(ret, "SetConsoleMode failed: %lu\n", GetLastError());
1710 1711
            break;

1712 1713
        case REQ_SET_OUTPUT_MODE:
            ret = SetConsoleMode(output, req->u.mode);
1714
            ok(ret, "SetConsoleMode failed: %lu\n", GetLastError());
1715 1716
            break;

1717 1718
        case REQ_SET_TITLE:
            ret = SetConsoleTitleW(req->u.string);
1719
            ok(ret, "SetConsoleTitleW failed: %lu\n", GetLastError());
1720 1721
            break;

1722 1723 1724 1725
        case REQ_WRITE_CHARACTERS:
            ret = WriteConsoleOutputCharacterW(output, req->u.write_characters.buf,
                                               req->u.write_characters.len,
                                               req->u.write_characters.coord, &count);
1726
            ok(ret, "WriteConsoleOutputCharacterW failed: %lu\n", GetLastError());
1727 1728
            break;

1729 1730
        case REQ_WRITE_CONSOLE:
            ret = WriteConsoleW(output, req->u.string, lstrlenW(req->u.string), NULL, NULL);
1731
            ok(ret, "SetConsoleTitleW failed: %lu\n", GetLastError());
1732 1733
            break;

1734 1735 1736 1737
        case REQ_WRITE_OUTPUT:
            {
                SMALL_RECT region = req->u.write_output.region;
                ret = WriteConsoleOutputW(output, req->u.write_output.buf, req->u.write_output.size, req->u.write_output.coord, &region);
1738
                ok(ret, "WriteConsoleOutput failed: %lu\n", GetLastError());
1739
                ret = WriteFile(pipe, &region, sizeof(region), &count, NULL);
1740
                ok(ret, "WriteFile failed: %lu\n", GetLastError());
1741 1742 1743
                break;
            }

1744 1745 1746 1747
        default:
            ok(0, "unexpected request type %u\n", req->type);
        };
    }
1748
    ok(GetLastError() == ERROR_BROKEN_PIPE, "ReadFile failed: %lu\n", GetLastError());
1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770
    CloseHandle(output);
    CloseHandle(input);
}

static HANDLE run_child(HANDLE console, HANDLE pipe)
{
    STARTUPINFOEXA startup = {{ sizeof(startup) }};
    char **argv, cmdline[MAX_PATH];
    PROCESS_INFORMATION info;
    SIZE_T size;
    BOOL ret;

    InitializeProcThreadAttributeList(NULL, 1, 0, &size);
    startup.lpAttributeList = HeapAlloc(GetProcessHeap(), 0, size);
    InitializeProcThreadAttributeList(startup.lpAttributeList, 1, 0, &size);
    UpdateProcThreadAttribute(startup.lpAttributeList, 0, PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, console,
                              sizeof(console), NULL, NULL);

    winetest_get_mainargs(&argv);
    sprintf(cmdline, "\"%s\" %s child %p", argv[0], argv[1], pipe);
    ret = CreateProcessA(NULL, cmdline, NULL, NULL, TRUE, EXTENDED_STARTUPINFO_PRESENT, NULL, NULL,
                         &startup.StartupInfo, &info);
1771
    ok(ret, "CreateProcessW failed: %lu\n", GetLastError());
1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789

    CloseHandle(info.hThread);
    HeapFree(GetProcessHeap(), 0, startup.lpAttributeList);
    return info.hProcess;
}

static HPCON create_pseudo_console(HANDLE *console_pipe_end, HANDLE *child_process)
{
    SECURITY_ATTRIBUTES sec_attr = { sizeof(sec_attr), NULL, TRUE };
    HANDLE child_pipe_end;
    COORD size = { 30, 40 };
    DWORD read_mode;
    HPCON console;
    HRESULT hres;
    BOOL r;

    console_pipe = CreateNamedPipeW(L"\\\\.\\pipe\\pseudoconsoleconn", PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
                                    PIPE_WAIT | PIPE_TYPE_BYTE, 1, 4096, 4096, NMPWAIT_USE_DEFAULT_WAIT, NULL);
1790
    ok(console_pipe != INVALID_HANDLE_VALUE, "CreateNamedPipeW failed: %lu\n", GetLastError());
1791 1792

    *console_pipe_end = CreateFileW(L"\\\\.\\pipe\\pseudoconsoleconn", GENERIC_READ | GENERIC_WRITE,
1793
                                    0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
1794
    ok(*console_pipe_end != INVALID_HANDLE_VALUE, "CreateFile failed: %lu\n", GetLastError());
1795 1796 1797 1798

    child_pipe = CreateNamedPipeW(L"\\\\.\\pipe\\pseudoconsoleserver", PIPE_ACCESS_DUPLEX,
                                  PIPE_WAIT | PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE, 1, 5000, 6000,
                                  NMPWAIT_USE_DEFAULT_WAIT, NULL);
1799
    ok(child_pipe != INVALID_HANDLE_VALUE, "CreateNamedPipeW failed: %lu\n", GetLastError());
1800 1801 1802

    child_pipe_end = CreateFileW(L"\\\\.\\pipe\\pseudoconsoleserver", GENERIC_READ | GENERIC_WRITE, 0,
                                 &sec_attr, OPEN_EXISTING, 0, NULL);
1803
    ok(child_pipe_end != INVALID_HANDLE_VALUE, "CreateFile failed: %lu\n", GetLastError());
1804 1805 1806

    read_mode = PIPE_READMODE_MESSAGE;
    r = SetNamedPipeHandleState(child_pipe_end, &read_mode, NULL, NULL);
1807
    ok(r, "SetNamedPipeHandleState failed: %lu\n", GetLastError());
1808 1809

    hres = pCreatePseudoConsole(size, *console_pipe_end, *console_pipe_end, 0, &console);
1810
    ok(hres == S_OK, "CreatePseudoConsole failed: %08lx\n", hres);
1811 1812 1813 1814 1815 1816 1817 1818 1819

    *child_process = run_child(console, child_pipe_end);
    CloseHandle(child_pipe_end);
    return console;
}

static void test_pseudoconsole(void)
{
    HANDLE console_pipe_end, child_process;
1820
    BOOL broken_version;
1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831
    HPCON console;

    console = create_pseudo_console(&console_pipe_end, &child_process);

    child_string_request(REQ_SET_TITLE, L"test title");
    expect_output_sequence("\x1b[2J");   /* erase display */
    skip_hide_cursor();
    expect_output_sequence("\x1b[m");    /* default attributes */
    expect_output_sequence("\x1b[H");    /* set cursor */
    skip_sequence("\x1b[H");             /* some windows versions emit it twice */
    expect_output_sequence("\x1b]0;test title"); /* set title */
1832
    broken_version = skip_byte(0);       /* some win versions emit nullbyte */
1833 1834 1835 1836
    expect_output_sequence("\x07");
    skip_sequence("\x1b[?25h");          /* show cursor */
    expect_empty_output();

1837 1838 1839
    if (!broken_version)
    {
        test_tty_output();
1840
        test_read_console_control();
1841
        test_read_console();
1842 1843
        test_tty_input();
    }
1844 1845
    else win_skip("Skipping tty output tests on broken Windows version\n");

1846 1847 1848
    CloseHandle(child_pipe);
    wait_child_process(child_process);
    CloseHandle(child_process);
1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860

    /* native sometimes clears the screen here */
    if (skip_sequence("\x1b[25l"))
    {
        unsigned int i;
        skip_sequence("\x1b[H");
        for (i = 0; i < 40; i++)
        {
            expect_output_sequence("\x1b[K");
            if (i != 39) expect_output_sequence("\r\n");
        }
        skip_sequence("\x1b[H\x1b[?25h");
1861 1862 1863 1864 1865 1866 1867 1868 1869 1870
        /* native sometimes redraws the screen afterwards */
        if (skip_sequence("\x1b[25l"))
        {
            unsigned int line_feed = 0;
            /* not checking exact output, depends too heavily on previous tests */
            for (i = 0; i < console_output_count - 2; i++)
                if (!memcmp(console_output + i, "\r\n", 2)) line_feed++;
            ok(line_feed == 39, "Wrong number of line feeds %u\n", line_feed);
            console_output_count = 0;
        }
1871
    }
1872 1873
    expect_empty_output();

1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888
    pClosePseudoConsole(console);
    CloseHandle(console_pipe_end);
    CloseHandle(console_pipe);
}

START_TEST(tty)
{
    HMODULE kernel32 = GetModuleHandleW(L"kernel32.dll");
    char **argv;
    int argc;

    argc = winetest_get_mainargs(&argv);
    if (argc > 3)
    {
        HANDLE pipe;
1889
        DWORD mode;
1890
        sscanf(argv[3], "%p", &pipe);
1891 1892 1893
        /* if std output is console, silence debug output so it does not interfere with tests */
        if (GetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), &mode))
            winetest_debug = 0;
1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907
        child_process(pipe);
        return;
    }

    pCreatePseudoConsole = (void *)GetProcAddress(kernel32, "CreatePseudoConsole");
    pClosePseudoConsole  = (void *)GetProcAddress(kernel32, "ClosePseudoConsole");
    if (!pCreatePseudoConsole)
    {
        win_skip("CreatePseudoConsole is not available\n");
        return;
    }

    test_pseudoconsole();
}