diff options
author | Ron Yorston <rmy@pobox.com> | 2014-03-20 11:22:40 +0000 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2014-03-20 11:22:40 +0000 |
commit | 992902efa7d40f2bc52d5bda0e51da27a2a2dbcc (patch) | |
tree | 1e10b1edf019be8c1f15d59cc07696803be987e8 /win32 | |
parent | ec386adabb3e6b83ecd8e0429eb75dc431b65c40 (diff) | |
download | busybox-w32-992902efa7d40f2bc52d5bda0e51da27a2a2dbcc.tar.gz busybox-w32-992902efa7d40f2bc52d5bda0e51da27a2a2dbcc.tar.bz2 busybox-w32-992902efa7d40f2bc52d5bda0e51da27a2a2dbcc.zip |
Implement write replacement to handle OEM codepages
Diffstat (limited to 'win32')
-rw-r--r-- | win32/winansi.c | 76 |
1 files changed, 75 insertions, 1 deletions
diff --git a/win32/winansi.c b/win32/winansi.c index ba3167425..8f2bb72e4 100644 --- a/win32/winansi.c +++ b/win32/winansi.c | |||
@@ -15,7 +15,7 @@ | |||
15 | #undef putchar | 15 | #undef putchar |
16 | #undef fwrite | 16 | #undef fwrite |
17 | #undef puts | 17 | #undef puts |
18 | /* TODO: write */ | 18 | #undef write |
19 | 19 | ||
20 | /* | 20 | /* |
21 | ANSI codes used by git: m, K | 21 | ANSI codes used by git: m, K |
@@ -537,3 +537,77 @@ int winansi_get_terminal_width_height(struct winsize *win) | |||
537 | 537 | ||
538 | return ret ? 0 : -1; | 538 | return ret ? 0 : -1; |
539 | } | 539 | } |
540 | |||
541 | static int ansi_emulate_write(int fd, const void *buf, size_t count) | ||
542 | { | ||
543 | int rv = 0, i; | ||
544 | const char *s = (const char *)buf; | ||
545 | char *pos, *str; | ||
546 | size_t len, out_len; | ||
547 | static size_t max_len = 0; | ||
548 | static char *mem = NULL; | ||
549 | |||
550 | /* if no special treatment is required output the string as-is */ | ||
551 | for ( i=0; i<count; ++i ) { | ||
552 | if ( s[i] == '\033' || s[i] > 0x7f ) { | ||
553 | break; | ||
554 | } | ||
555 | } | ||
556 | |||
557 | if ( i == count ) { | ||
558 | return write(fd, buf, count); | ||
559 | } | ||
560 | |||
561 | /* make a writable copy of the data and retain it for reuse */ | ||
562 | if ( count > max_len ) { | ||
563 | free(mem); | ||
564 | mem = xmalloc(count+1); | ||
565 | max_len = count; | ||
566 | } | ||
567 | memcpy(mem, buf, count); | ||
568 | mem[count] = '\0'; | ||
569 | pos = str = mem; | ||
570 | |||
571 | /* we're writing to the console so we assume the data isn't binary */ | ||
572 | while (*pos) { | ||
573 | pos = strstr(str, "\033["); | ||
574 | if (pos) { | ||
575 | len = pos - str; | ||
576 | |||
577 | if (len) { | ||
578 | CharToOemBuff(str, str, len); | ||
579 | out_len = write(fd, buf, len); | ||
580 | rv += out_len; | ||
581 | if (out_len < len) | ||
582 | return rv; | ||
583 | } | ||
584 | |||
585 | str = pos + 2; | ||
586 | rv += 2; | ||
587 | |||
588 | pos = (char *)set_attr(str); | ||
589 | rv += pos - str; | ||
590 | str = pos; | ||
591 | } else { | ||
592 | len = strlen(str); | ||
593 | rv += len; | ||
594 | CharToOem(str, str); | ||
595 | write(fd, str, len); | ||
596 | return rv; | ||
597 | } | ||
598 | } | ||
599 | return rv; | ||
600 | } | ||
601 | |||
602 | int winansi_write(int fd, const void *buf, size_t count) | ||
603 | { | ||
604 | if (!isatty(fd)) | ||
605 | return write(fd, buf, count); | ||
606 | |||
607 | init(); | ||
608 | |||
609 | if (!console) | ||
610 | return write(fd, buf, count); | ||
611 | |||
612 | return ansi_emulate_write(fd, buf, count); | ||
613 | } | ||