FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
rstudio/src/cpp/session/SessionConsoleProcessInfo.cpp at master · pearsonca/rstudio · GitHub
pearsonca
/
rstudio
Public
forked from
rstudio/rstudio
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
rstudio
/
src
/
cpp
/
session
/
SessionConsoleProcessInfo.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
392 lines (326 loc) · 12.5 KB
Breadcrumbs
rstudio
/
src
/
cpp
/
session
/
SessionConsoleProcessInfo.cpp
Copy path
File metadata and controls
392 lines (326 loc) · 12.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
* SessionConsoleProcessInfo.cpp
*
* Copyright (C) 2009-19 by RStudio, PBC
*
* Unless you have received this program directly from RStudio pursuant
* to the terms of a commercial license agreement with RStudio, then
* this program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/
#
include
<
session/SessionConsoleProcessInfo.hpp
>
#
include
<
core/system/System.hpp
>
#
include
<
core/text/TermBufferParser.hpp
>
#
include
"
session-config.h
"
#
ifdef
RSTUDIO_SERVER
#
include
<
server_core/UrlPorts.hpp
>
#
endif
#
include
<
session/SessionConsoleProcessPersist.hpp
>
#
include
<
session/SessionModuleContext.hpp
>
#
include
<
session/SessionPersistentState.hpp
>
#
include
<
session/SessionOptions.hpp
>
#
include
<
session/prefs/UserPrefs.hpp
>
using
namespace
rstudio
::core
;
namespace
rstudio
{
namespace
session
{
namespace
console_process
{
const
int
kDefaultMaxOutputLines
=
500
;
const
int
kDefaultTerminalMaxOutputLines
=
1000
;
//
xterm.js scrollback constant
const
int
kNoTerminal
=
0
;
//
terminal sequence number for a non-terminal
const
int
kNewTerminal
= -
1
;
//
new terminal, sequence number yet to be determined
const
size_t
kOutputBufferSize
=
8192
;
ConsoleProcessInfo::ConsoleProcessInfo
()
{
//
When we retrieve from outputBuffer, we only want complete lines. Add a
//
dummy \n so we can tell the first line is a complete line.
outputBuffer_.
push_back
(
'
\n
'
);
}
//
The constant values in the initializer list (i.e. those not supplied as arguments
//
to the constructor) should match the constants set in the client
//
ConsoleProcessInfo.createNewTerminalInfo constructor. A new terminal can be
//
created from the client side via the UI using the client-side constructor, or on
//
the server-side via the R API (e.g. terminalCreate) which uses this C++ constructor.
ConsoleProcessInfo::ConsoleProcessInfo
(
const
std::string& caption,
const
std::string& title,
const
std::string& handle,
const
int
terminalSequence,
TerminalShell::ShellType shellType,
bool
altBufferActive,
const
core::FilePath& cwd,
int
cols,
int
rows,
bool
zombie,
bool
trackEnv)
: caption_(caption), title_(title), handle_(handle),
terminalSequence_
(terminalSequence), allowRestart_(
true
),
interactionMode_(InteractionAlways), maxOutputLines_(
kDefaultTerminalMaxOutputLines
),
altBufferActive_(altBufferActive), shellType_(shellType),
cwd_(cwd), cols_(cols), rows_(rows),
zombie_(zombie), trackEnv_(trackEnv)
{
}
ConsoleProcessInfo::ConsoleProcessInfo
(
const
std::string& caption,
InteractionMode mode,
int
maxOutputLines)
: caption_(caption), interactionMode_(mode), maxOutputLines_(maxOutputLines)
{
}
void
ConsoleProcessInfo::ensureHandle
()
{
if
(handle_.
empty
())
handle_ =
core::system::generateShortenedUuid
();
}
void
ConsoleProcessInfo::setExitCode
(
int
exitCode)
{
exitCode_.
reset
(exitCode);
}
void
ConsoleProcessInfo::resetExitCode
()
{
exitCode_.
reset
();
}
void
ConsoleProcessInfo::appendToOutputBuffer
(
const
std::string &str)
{
//
For modal console procs, store terminal output directly in the
//
ConsoleProcInfo INDEX
if
(
getTerminalSequence
() ==
kNoTerminal
)
{
std::copy
(str.
begin
(), str.
end
(),
std::back_inserter
(outputBuffer_));
return
;
}
//
For terminal tabs, store in a separate file, first removing any
//
output targeting the alternate terminal buffer.
std::string mainBufferStr =
core::text::stripSecondaryBuffer
(str, &altBufferActive_);
console_persist::appendToOutputBuffer
(handle_, mainBufferStr);
}
void
ConsoleProcessInfo::appendToOutputBuffer
(
char
ch)
{
outputBuffer_.
push_back
(ch);
}
std::string
ConsoleProcessInfo::getSavedBufferChunk
(
int
requestedChunk,
bool
* pMoreAvailable)
const
{
//
We read the entire buffer into memory to return a given chunk. This is
//
ok for our current usage pattern, where the buffer-size is bounded
//
to a certain number of lines and is infrequently reloaded (only upon
//
browser refresh or reloading a session). If this pattern becomes
//
more frequent and/or we support much larger buffers, we'd want to
//
reconsider this implementation.
//
Read buffer (trims to maxOutputLines_ when chunk zero is requested)
std::string buffer =
console_persist::getSavedBuffer
(
handle_,
requestedChunk ==
0
? maxOutputLines_ :
0
);
*pMoreAvailable =
false
;
//
Common case, entire buffer fits in chunk zero
if
(requestedChunk ==
0
&& (buffer.
length
() <=
kOutputBufferSize
))
return
buffer;
//
Chunk requested past end of buffer?
if
(requestedChunk *
kOutputBufferSize
>= buffer.
length
())
return
std::string
();
//
Otherwise return substring for the chunk (substr doesn't mind if you ask
//
for more characters than are available in the string, as long as the
//
starting position is within the string)
std::string chunk = buffer.
substr
(
requestedChunk *
kOutputBufferSize
,
kOutputBufferSize
);
if
(requestedChunk *
kOutputBufferSize
+ chunk.
length
() < buffer.
length
())
*pMoreAvailable =
true
;
return
chunk;
}
std::string
ConsoleProcessInfo::getFullSavedBuffer
()
const
{
//
Read buffer (trims to maxOutputLines_)
return
console_persist::getSavedBuffer
(handle_, maxOutputLines_);
}
int
ConsoleProcessInfo::getBufferLineCount
()
const
{
return
console_persist::getSavedBufferLineCount
(handle_, maxOutputLines_);
}
std::string
ConsoleProcessInfo::bufferedOutput
()
const
{
boost::circular_buffer<
char
>::const_iterator pos =
std::find
(outputBuffer_.
begin
(), outputBuffer_.
end
(),
'
\n
'
);
std::string result;
if
(pos != outputBuffer_.
end
())
pos++;
std::copy
(pos, outputBuffer_.
end
(),
std::back_inserter
(result));
//
Will be empty if the buffer was overflowed by a single line
return
result;
}
void
ConsoleProcessInfo::deleteLogFile
(
bool
lastLineOnly)
const
{
console_persist::deleteLogFile
(handle_, lastLineOnly);
}
void
ConsoleProcessInfo::deleteEnvFile
()
const
{
console_persist::deleteEnvFile
(handle_);
}
core::json::Object
ConsoleProcessInfo::toJson
(SerializationMode serialMode)
const
{
json::Object result;
result[
"
handle
"
] = handle_;
result[
"
caption
"
] = caption_;
result[
"
show_on_output
"
] = showOnOutput_;
result[
"
interaction_mode
"
] =
static_cast
<
int
>(interactionMode_);
result[
"
max_output_lines
"
] = maxOutputLines_;
result[
"
buffered_output
"
] =
bufferedOutput
();
if
(exitCode_)
result[
"
exit_code
"
] = *exitCode_;
else
result[
"
exit_code
"
] =
json::Value
();
//
newly added in v1.1
result[
"
terminal_sequence
"
] = terminalSequence_;
result[
"
allow_restart
"
] = allowRestart_;
result[
"
title
"
] = title_;
result[
"
child_procs
"
] = childProcs_;
result[
"
shell_type
"
] =
TerminalShell::getShellId
(shellType_);
result[
"
channel_mode
"
] =
static_cast
<
int
>(channelMode_);
result[
"
channel_id
"
] = channelId_;
result[
"
alt_buffer
"
] = altBufferActive_;
result[
"
cwd
"
] =
module_context::createAliasedPath
(cwd_);
result[
"
cols
"
] = cols_;
result[
"
rows
"
] = rows_;
result[
"
restarted
"
] = restarted_;
result[
"
autoclose
"
] =
static_cast
<
int
>(autoClose_);
result[
"
zombie
"
] = zombie_;
result[
"
track_env
"
] = trackEnv_;
#
ifdef
RSTUDIO_SERVER
//
in server mode, we may need to provide the client with an obscured form of the port when
//
connecting via websockets
if
(
options
().
programMode
() ==
kSessionProgramModeServer
&&
serialMode == ClientSerialization &&
channelMode_ == Websocket)
{
auto
port = safe_convert::stringTo<
int
>(channelId_);
if
(port)
{
result[
"
channel_id
"
] =
server_core::transformPort
(
persistentState
().
portToken
(), *port);
}
}
#
endif
return
result;
}
boost::shared_ptr<ConsoleProcessInfo>
ConsoleProcessInfo::fromJson
(
const
core::json::Object& obj)
{
boost::shared_ptr<ConsoleProcessInfo>
pProc
(
new
ConsoleProcessInfo
());
Error error =
json::getOptionalParam
(obj,
"
handle
"
,
std::string
(), &pProc->
handle_
);
if
(error)
LOG_ERROR
(error);
error =
json::getOptionalParam
(obj,
"
caption
"
,
std::string
(), &pProc->
caption_
);
if
(error)
LOG_ERROR
(error);
error =
json::getOptionalParam
(obj,
"
show_on_output
"
,
false
, &pProc->
showOnOutput_
);
if
(error)
LOG_ERROR
(error);
int
mode =
0
;
error =
json::getOptionalParam
(obj,
"
interaction_mode
"
,
0
, &mode);
if
(error)
LOG_ERROR
(error);
pProc->
interactionMode_
=
static_cast
<InteractionMode>(mode);
error =
json::getOptionalParam
(obj,
"
max_output_lines
"
,
kDefaultMaxOutputLines
, &pProc->
maxOutputLines_
);
if
(error)
LOG_ERROR
(error);
std::string bufferedOutput;
error =
json::getOptionalParam
(obj,
"
buffered_output
"
,
std::string
(), &bufferedOutput);
if
(error)
LOG_ERROR
(error);
if
(!bufferedOutput.
empty
())
{
std::copy
(bufferedOutput.
begin
(), bufferedOutput.
end
(),
std::back_inserter
(pProc->
outputBuffer_
));
}
error =
json::getOptionalParam
(obj,
"
exit_code
"
, &pProc->
exitCode_
);
if
(error)
LOG_ERROR
(error);
error =
json::getOptionalParam
(obj,
"
terminal_sequence
"
,
0
, &pProc->
terminalSequence_
);
if
(error)
LOG_ERROR
(error);
error =
json::getOptionalParam
(obj,
"
allow_restart
"
,
false
, &pProc->
allowRestart_
);
if
(error)
LOG_ERROR
(error);
error =
json::getOptionalParam
(obj,
"
title
"
,
std::string
(), &pProc->
title_
);
if
(error)
LOG_ERROR
(error);
error =
json::getOptionalParam
(obj,
"
child_procs
"
,
false
, &pProc->
childProcs_
);
if
(error)
LOG_ERROR
(error);
std::string shellType;
error =
json::getOptionalParam
(obj,
"
shell_type
"
,
std::string
(
"
default
"
), &shellType);
if
(error)
LOG_ERROR
(error);
pProc->
shellType_
=
TerminalShell::shellTypeFromString
(shellType);
int
channelModeInt =
0
;
error =
json::getOptionalParam
(obj,
"
channel_mode
"
,
0
, &channelModeInt);
if
(error)
LOG_ERROR
(error);
pProc->
channelMode_
=
static_cast
<ChannelMode>(channelModeInt);
error =
json::getOptionalParam
(obj,
"
channel_id
"
,
std::string
(), &pProc->
channelId_
);
if
(error)
LOG_ERROR
(error);
error =
json::getOptionalParam
(obj,
"
alt_buffer
"
,
false
, &pProc->
altBufferActive_
);
if
(error)
LOG_ERROR
(error);
std::string cwd;
error =
json::getOptionalParam
(obj,
"
cwd
"
,
std::string
(), &cwd);
if
(error)
LOG_ERROR
(error);
if
(!cwd.
empty
())
pProc->
cwd_
=
module_context::resolveAliasedPath
(cwd);
error =
json::getOptionalParam
(obj,
"
cols
"
,
0
, &pProc->
cols_
);
if
(error)
LOG_ERROR
(error);
error =
json::getOptionalParam
(obj,
"
rows
"
,
0
, &pProc->
rows_
);
if
(error)
LOG_ERROR
(error);
error =
json::getOptionalParam
(obj,
"
restarted
"
,
false
, &pProc->
restarted_
);
if
(error)
LOG_ERROR
(error);
int
autoCloseInt =
0
;
error =
json::getOptionalParam
(obj,
"
autoclose
"
,
0
, &autoCloseInt);
if
(error)
LOG_ERROR
(error);
pProc->
autoClose_
=
static_cast
<AutoCloseMode>(autoCloseInt);
error =
json::getOptionalParam
(obj,
"
zombie
"
,
false
, &pProc->
zombie_
);
if
(error)
LOG_ERROR
(error);
error =
json::getOptionalParam
(obj,
"
track_env
"
,
false
, &pProc->
trackEnv_
);
if
(error)
LOG_ERROR
(error);
return
pProc;
}
std::string
ConsoleProcessInfo::loadConsoleProcessMetadata
()
{
return
console_persist::loadConsoleProcessMetadata
();
}
void
ConsoleProcessInfo::deleteOrphanedLogs
(
bool
(*validHandle)(
const
std::string&))
{
console_persist::deleteOrphanedLogs
(validHandle);
}
void
ConsoleProcessInfo::saveConsoleProcesses
(
const
std::string& metadata)
{
console_persist::saveConsoleProcesses
(metadata);
}
void
ConsoleProcessInfo::saveConsoleEnvironment
(
const
core::system::Options& environment)
{
console_persist::saveConsoleEnvironment
(handle_, environment);
}
void
ConsoleProcessInfo::loadConsoleEnvironment
(
const
std::string& handle, core::system::Options* pEnv)
{
console_persist::loadConsoleEnvironment
(handle, pEnv);
}
AutoCloseMode
ConsoleProcessInfo::closeModeFromPref
(std::string prefValue)
{
if
(prefValue ==
kTerminalCloseBehaviorAlways
)
return
AlwaysAutoClose;
else
if
(prefValue ==
kTerminalCloseBehaviorClean
)
return
CleanExitAutoClose;
else
if
(prefValue ==
kTerminalCloseBehaviorNever
)
return
NeverAutoClose;
else
return
NeverAutoClose;
}
}
//
namespace console_process_info
}
//
namespace session
}
//
namespace rstudio
Back
|
FazBrowse Home
|
New Git URL