-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathGlobal.cpp
More file actions
executable file
·130 lines (91 loc) · 2.64 KB
/
Global.cpp
File metadata and controls
executable file
·130 lines (91 loc) · 2.64 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
119
120
121
122
123
124
125
126
127
128
129
130
/*-----------------------------------------------------------------------------
Lua Studio
Copyright (c) 1996-2008 Michal Kowalski
-----------------------------------------------------------------------------*/
#include "StdAfx.h"
#include "Global.h"
#include "Broadcast.h"
#include <io.h>
#include <fcntl.h>
CGlobal::CGlobal()
{
debugger_.reset(new Debugger());
// redirect 'stdout' to the pipe
char path[MAX_PATH];
::GetTempPath(MAX_PATH, path);
char temp[MAX_PATH];
::GetTempFileName(path, "lu-", ::GetTickCount(), temp);
// open stdout first, or else dup2 won't work
if (freopen(temp, "w", stdout) == 0)
throw std::exception("error redirecting stdout to temp file");
if (_pipe(stdout_fds_, 0, _O_BINARY) != 0 || _dup2(stdout_fds_[1], _fileno(stdout)) != 0)
throw std::exception("error redirecting stdout to the pipe");
// no buffering for stdout, to make output window refresh instant
setvbuf(stdout, NULL, _IONBF, 0);
//TODO: delete temp
::DeleteFile(temp);
output_position_ = 0;
output_.reserve(200);
thread_ = ::AfxBeginThread(ReadStdoutText, this, 0, 0, CREATE_SUSPENDED);
if (thread_ == 0)
throw std::exception("cannot create worker thread");
thread_->m_bAutoDelete = false;
thread_->ResumeThread();
}
CGlobal::~CGlobal()
{
//TODO: close the pipe without hanging
//_close(stdout_fds_[0]);
//_close(stdout_fds_[1]);
// ::WaitForSingleObject(thread_, 10);
thread_->ExitInstance();
}
// worker thread sitting on a pipe and reading redirected 'stdout' text
UINT AFX_CDECL CGlobal::ReadStdoutText(LPVOID self)
{
CGlobal* obj= static_cast<CGlobal*>(self);
for (;;)
{
const size_t chunk= 1024;
char buf[chunk];
int len= _read(obj->stdout_fds_[0], buf, chunk);
if (len == -1)
break;
obj->BufferOutput(buf, len);
}
return 0;
}
void CGlobal::BufferOutput(const char* buf, int len)
{
if (len <= 0)
return;
CSingleLock lock(&lock_output_, true);
output_.insert(output_.end(), buf, buf + len);
lock.Unlock();
// notify clients
CBroadcast::SendMessageToPopups(CBroadcast::WM_APP_OUTPUT);
}
void CGlobal::GetOutput(std::vector<char>& out)
{
CSingleLock lock(&lock_output_, true);
if (output_position_ > output_.size())
{
ASSERT(false);
output_position_ = output_.size();
}
out.assign(output_.begin() + output_position_, output_.end());
output_position_ = output_.size();
}
void CGlobal::ClearOutput()
{
CSingleLock lock(&lock_output_, true);
output_position_ = 0;
output_.clear();
lock.Unlock();
CBroadcast::SendMessageToPopups(CBroadcast::WM_APP_OUTPUT);
}
//-----------------------------------------------------------------------------
Debugger& CGlobal::GetDebugger() const
{
return *debugger_;
}