1111#include < QElapsedTimer>
1212
1313#ifdef _WIN32
14+ #include < QApplication>
1415#include < windows.h>
1516#include < winpty.h>
1617#else
@@ -479,6 +480,7 @@ class ProcessWin : public AbstractProcess {
479480 std::vector<char > output_vector_; // for result
480481 void writeOutput (char const *buf, size_t len)
481482 {
483+ std::lock_guard<std::mutex> lock (mutex_);
482484 output_queue_.insert (output_queue_.end (), buf, buf + len);
483485 output_vector_.insert (output_vector_.end (), buf, buf + len);
484486 }
@@ -589,6 +591,7 @@ class ProcessWinPty : public AbstractPtyProcess {
589591 std::mutex mutex_;
590592 std::condition_variable cond_;
591593 int input_fd_ = -1 ;
594+ HANDLE hConout_ = INVALID_HANDLE_VALUE;
592595 HANDLE hInput_ = INVALID_HANDLE_VALUE;
593596 int exit_code_ = 128 ;
594597
@@ -626,7 +629,7 @@ class ProcessWinPty : public AbstractPtyProcess {
626629 return ret;
627630 }
628631
629- HANDLE hConout = CreateFileW (
632+ hConout_ = CreateFileW (
630633 winpty_conout_name (wp),
631634 GENERIC_READ, 0 , nullptr ,
632635 OPEN_EXISTING, 0 , nullptr
@@ -652,14 +655,14 @@ class ProcessWinPty : public AbstractPtyProcess {
652655
653656
654657
655- if (hConout != INVALID_HANDLE_VALUE) {
658+ if (hConout_ != INVALID_HANDLE_VALUE) {
656659 char buf[256 ];
657660 DWORD n;
658- while (ReadFile (hConout , buf, sizeof (buf), &n, nullptr ) && n > 0 ) {
659- // ret.append(buf, n );
661+ while (ReadFile (hConout_ , buf, sizeof (buf), &n, nullptr ) && n > 0 ) {
662+ std::lock_guard<std::mutex> lock (mutex_ );
660663 writeOutput (buf, n);
661664 }
662- CloseHandle (hConout );
665+ CloseHandle (hConout_ );
663666 }
664667
665668 WaitForSingleObject (hProcess, INFINITE);
@@ -679,12 +682,46 @@ class ProcessWinPty : public AbstractPtyProcess {
679682 void writeInput (const char *ptr, int len)
680683 {
681684 if (hInput_ != INVALID_HANDLE_VALUE) {
682- DWORD n;
683- WriteFile (hInput_, ptr, (DWORD)len, &n, nullptr );
685+ char const *begin = ptr;
686+ char const *end = begin + len;
687+ char const *left = begin;
688+ char const *right = begin;
689+ while (1 ) {
690+ int c = -1 ;
691+ if (right < end) {
692+ c = *right & 0xff ;
693+ }
694+ if (c == ' \r ' || c == ' \n ' || c < 0 ) {
695+ if (left < right) {
696+ DWORD written;
697+ WriteFile (hInput_, left, right - left, &written, nullptr );
698+ }
699+ if (c < 0 ) break ;
700+ right++;
701+ if (c == ' \r ' ) {
702+ if (*right == ' \n ' ) {
703+ right++;
704+ }
705+ c = ' \r ' ;
706+ } else if (c == ' \n ' ) {
707+ c = ' \r ' ;
708+ } else {
709+ c = -1 ;
710+ }
711+ if (c >= 0 ) {
712+ DWORD written;
713+ WriteFile (hInput_, &c, 1 , &written, nullptr );
714+ }
715+ left = right;
716+ } else {
717+ right++;
718+ }
719+ }
684720 }
685721 }
686722 int readOutput (char *ptr, int len)
687723 {
724+ std::lock_guard<std::mutex> lock (mutex_);
688725 int n = output_queue_.size ();
689726 if (n > len) n = len;
690727 for (int i = 0 ; i < n; i++) {
@@ -710,6 +747,10 @@ class ProcessWinPty : public AbstractPtyProcess {
710747 }
711748 void stop ()
712749 {
750+ if (hConout_ != INVALID_HANDLE_VALUE) {
751+ CloseHandle (hConout_);
752+ hConout_ = INVALID_HANDLE_VALUE;
753+ }
713754 wait ();
714755 }
715756 int getExitCode () const
@@ -967,37 +1008,48 @@ void msleep(unsigned int ms)
9671008#endif
9681009}
9691010
1011+ #include " ApplicationGlobal.h"
1012+ #include " MainWindow.h"
1013+ #include < QDir>
1014+
9701015void process_test ()
9711016{
9721017#if 0
973- ProcessWinConPTY proc;
974- proc.start("git --version", "");
975- proc.wait();
976- std::vector<char> out;
977- proc.readResult(&out);
978- std::string s(out.begin(), out.end());
979- fprintf(stderr, "[%s]\n", s.c_str());
980- #elif 0
981- ProcessWinConPTY proc;
982- proc.start("sort", "");
983- {
984- // Windowsのsortコマンドは\r\n(CRLF)を行区切りとして期待する
985- const char *input = "abc\r\ndef\r\n";
986- proc.writeInput(input, strlen(input));
1018+ ProcessWinPty proc;
1019+ proc.start("git fetch", "");
1020+
1021+ std::string text;
1022+ while (proc.isRunning()) {
1023+ char tmp[1024];
1024+ int n = proc.readOutput(tmp, sizeof(tmp));
1025+ if (n > 0) {
1026+ std::string s(tmp, n);
1027+ fprintf(stderr, "%s", s.c_str());
1028+ text += s;
1029+ } else {
1030+ if (text.find("Are you sure you want to continue connecting (yes/no/[fingerprint])?") != std::string::npos) {
1031+ std::string s = "yes\n";
1032+ proc.writeInput(s.c_str(), (int)s.size());
1033+ proc.closeInput();
1034+ break;
1035+ }
1036+ QApplication::processEvents();
1037+ }
9871038 }
988- proc.wait();
989- std::vector<char> out;
990- proc.readResult(&out);
991- std::string s(out.begin(), out.end());
992- fprintf(stderr, "[%s]\n", s.c_str());
993- #elif 0
994- ProcessPosixPty proc;
995- proc.start("sort", "");
996- {
997- const char *input = "abc\ndef\n";
998- proc.writeInput(input, strlen(input));
1039+ while (proc.isRunning()) {
1040+ char tmp[1024];
1041+ int n = proc.readOutput(tmp, sizeof(tmp));
1042+ if (n > 0) {
1043+ std::string s(tmp, n);
1044+ fprintf(stderr, "%s", s.c_str());
1045+ text += s;
1046+ } else {
1047+ QApplication::processEvents();
1048+ }
9991049 }
1000- proc.wait();
1050+
1051+
1052+ proc.stop();
10011053 std::vector<char> out;
10021054 proc.readResult(&out);
10031055 std::string s(out.begin(), out.end());
0 commit comments