diff options
author | Ron Yorston <rmy@pobox.com> | 2018-03-06 12:36:46 +0000 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2018-03-06 12:36:46 +0000 |
commit | 7478b47cb678bcfbca16e41443a496b3bd8496f9 (patch) | |
tree | 1e241a73629d3b77a65dd0e9ac1a9cc8fa3ad60a /win32 | |
parent | 85165f87a8161946c683da5c4ecf58bbb6e46652 (diff) | |
download | busybox-w32-7478b47cb678bcfbca16e41443a496b3bd8496f9.tar.gz busybox-w32-7478b47cb678bcfbca16e41443a496b3bd8496f9.tar.bz2 busybox-w32-7478b47cb678bcfbca16e41443a496b3bd8496f9.zip |
winansi: create and use functions to test for console
Diffstat (limited to 'win32')
-rw-r--r-- | win32/winansi.c | 71 |
1 files changed, 20 insertions, 51 deletions
diff --git a/win32/winansi.c b/win32/winansi.c index b357fc642..da1f406e9 100644 --- a/win32/winansi.c +++ b/win32/winansi.c | |||
@@ -60,6 +60,18 @@ static void init(void) | |||
60 | initialized = 1; | 60 | initialized = 1; |
61 | } | 61 | } |
62 | 62 | ||
63 | static int is_console(int fd) | ||
64 | { | ||
65 | init(); | ||
66 | return isatty(fd) && console; | ||
67 | } | ||
68 | |||
69 | static int is_console_in(int fd) | ||
70 | { | ||
71 | init(); | ||
72 | return isatty(fd) && console_in; | ||
73 | } | ||
74 | |||
63 | static int skip_ansi_emulation(void) | 75 | static int skip_ansi_emulation(void) |
64 | { | 76 | { |
65 | static char *var = NULL; | 77 | static char *var = NULL; |
@@ -447,12 +459,7 @@ int winansi_putchar(int c) | |||
447 | char t = c; | 459 | char t = c; |
448 | char *s = &t; | 460 | char *s = &t; |
449 | 461 | ||
450 | if (!isatty(STDOUT_FILENO)) | 462 | if (!is_console(STDOUT_FILENO)) |
451 | return putchar(c); | ||
452 | |||
453 | init(); | ||
454 | |||
455 | if (!console) | ||
456 | return putchar(c); | 463 | return putchar(c); |
457 | 464 | ||
458 | CharToOemBuff(s, s, 1); | 465 | CharToOemBuff(s, s, 1); |
@@ -463,12 +470,7 @@ int winansi_puts(const char *s) | |||
463 | { | 470 | { |
464 | int rv; | 471 | int rv; |
465 | 472 | ||
466 | if (!isatty(STDOUT_FILENO)) | 473 | if (!is_console(STDOUT_FILENO)) |
467 | return puts(s); | ||
468 | |||
469 | init(); | ||
470 | |||
471 | if (!console) | ||
472 | return puts(s); | 474 | return puts(s); |
473 | 475 | ||
474 | rv = ansi_emulate(s, stdout); | 476 | rv = ansi_emulate(s, stdout); |
@@ -485,15 +487,7 @@ size_t winansi_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) | |||
485 | 487 | ||
486 | lsize = MIN(size, nmemb); | 488 | lsize = MIN(size, nmemb); |
487 | lmemb = MAX(size, nmemb); | 489 | lmemb = MAX(size, nmemb); |
488 | if (lsize != 1) | 490 | if (lsize != 1 || !is_console(fileno(stream))) |
489 | return fwrite(ptr, size, nmemb, stream); | ||
490 | |||
491 | if (!isatty(fileno(stream))) | ||
492 | return fwrite(ptr, size, nmemb, stream); | ||
493 | |||
494 | init(); | ||
495 | |||
496 | if (!console) | ||
497 | return fwrite(ptr, size, nmemb, stream); | 491 | return fwrite(ptr, size, nmemb, stream); |
498 | 492 | ||
499 | str = xmalloc(lmemb+1); | 493 | str = xmalloc(lmemb+1); |
@@ -510,12 +504,7 @@ int winansi_fputs(const char *str, FILE *stream) | |||
510 | { | 504 | { |
511 | int rv; | 505 | int rv; |
512 | 506 | ||
513 | if (!isatty(fileno(stream))) | 507 | if (!is_console(fileno(stream))) |
514 | return fputs(str, stream); | ||
515 | |||
516 | init(); | ||
517 | |||
518 | if (!console) | ||
519 | return fputs(str, stream); | 508 | return fputs(str, stream); |
520 | 509 | ||
521 | rv = ansi_emulate(str, stream); | 510 | rv = ansi_emulate(str, stream); |
@@ -533,12 +522,7 @@ int winansi_vfprintf(FILE *stream, const char *format, va_list list) | |||
533 | char *buf = small_buf; | 522 | char *buf = small_buf; |
534 | va_list cp; | 523 | va_list cp; |
535 | 524 | ||
536 | if (!isatty(fileno(stream))) | 525 | if (!is_console(fileno(stream))) |
537 | goto abort; | ||
538 | |||
539 | init(); | ||
540 | |||
541 | if (!console) | ||
542 | goto abort; | 526 | goto abort; |
543 | 527 | ||
544 | va_copy(cp, list); | 528 | va_copy(cp, list); |
@@ -680,12 +664,7 @@ static int ansi_emulate_write(int fd, const void *buf, size_t count) | |||
680 | 664 | ||
681 | int winansi_write(int fd, const void *buf, size_t count) | 665 | int winansi_write(int fd, const void *buf, size_t count) |
682 | { | 666 | { |
683 | if (!isatty(fd)) | 667 | if (!is_console(fd)) |
684 | return write(fd, buf, count); | ||
685 | |||
686 | init(); | ||
687 | |||
688 | if (!console) | ||
689 | return write(fd, buf, count); | 668 | return write(fd, buf, count); |
690 | 669 | ||
691 | return ansi_emulate_write(fd, buf, count); | 670 | return ansi_emulate_write(fd, buf, count); |
@@ -696,12 +675,7 @@ int winansi_read(int fd, void *buf, size_t count) | |||
696 | int rv; | 675 | int rv; |
697 | 676 | ||
698 | rv = mingw_read(fd, buf, count); | 677 | rv = mingw_read(fd, buf, count); |
699 | if (!isatty(fd)) | 678 | if (!is_console_in(fd)) |
700 | return rv; | ||
701 | |||
702 | init(); | ||
703 | |||
704 | if (!console_in) | ||
705 | return rv; | 679 | return rv; |
706 | 680 | ||
707 | if ( rv > 0 ) { | 681 | if ( rv > 0 ) { |
@@ -716,12 +690,7 @@ int winansi_getc(FILE *stream) | |||
716 | int rv; | 690 | int rv; |
717 | 691 | ||
718 | rv = getc(stream); | 692 | rv = getc(stream); |
719 | if (!isatty(fileno(stream))) | 693 | if (!is_console_in(fileno(stream))) |
720 | return rv; | ||
721 | |||
722 | init(); | ||
723 | |||
724 | if (!console_in) | ||
725 | return rv; | 694 | return rv; |
726 | 695 | ||
727 | if ( rv != EOF ) { | 696 | if ( rv != EOF ) { |