| 314 | }; |
| 315 | |
| 316 | static DWORD WINAPI console_thread(LPVOID data UNUSED) |
| 317 | { |
| 318 | unsigned char buffer[BUFFER_SIZE]; |
| 319 | DWORD bytes; |
| 320 | int start, end = 0, c, parampos = 0, state = TEXT; |
| 321 | int params[MAX_PARAMS]; |
| 322 | |
| 323 | while (1) { |
| 324 | /* read next chunk of bytes from the pipe */ |
| 325 | if (!ReadFile(hread, buffer + end, BUFFER_SIZE - end, &bytes, |
| 326 | NULL)) { |
| 327 | /* exit if pipe has been closed or disconnected */ |
| 328 | if (GetLastError() == ERROR_PIPE_NOT_CONNECTED || |
| 329 | GetLastError() == ERROR_BROKEN_PIPE) |
| 330 | break; |
| 331 | /* ignore other errors */ |
| 332 | continue; |
| 333 | } |
| 334 | |
| 335 | /* scan the bytes and handle ANSI control codes */ |
| 336 | bytes += end; |
| 337 | start = end = 0; |
| 338 | while (end < bytes) { |
| 339 | c = buffer[end++]; |
| 340 | switch (state) { |
| 341 | case TEXT: |
| 342 | if (c == ESCAPE) { |
| 343 | /* print text seen so far */ |
| 344 | if (end - 1 > start) |
| 345 | write_console(buffer + start, |
| 346 | end - 1 - start); |
| 347 | |
| 348 | /* then start parsing escape sequence */ |
| 349 | start = end - 1; |
| 350 | memset(params, 0, sizeof(params)); |
| 351 | parampos = 0; |
| 352 | state = ESCAPE; |
| 353 | } |
| 354 | break; |
| 355 | |
| 356 | case ESCAPE: |
| 357 | /* continue if "\033[", otherwise bail out */ |
| 358 | state = (c == BRACKET) ? BRACKET : TEXT; |
| 359 | break; |
| 360 | |
| 361 | case BRACKET: |
| 362 | /* parse [0-9;]* into array of parameters */ |
| 363 | if (c >= '0' && c <= '9') { |
| 364 | params[parampos] *= 10; |
| 365 | params[parampos] += c - '0'; |
| 366 | } else if (c == ';') { |
| 367 | /* |
| 368 | * next parameter, bail out if out of |
| 369 | * bounds |
| 370 | */ |
| 371 | parampos++; |
| 372 | if (parampos >= MAX_PARAMS) |
| 373 | state = TEXT; |
nothing calls this directly
no test coverage detected