Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 39535
1
###############################################################################
2
#
3
# CoCalc: Collaborative Calculation in the Cloud
4
#
5
# Copyright (C) 2016, Sagemath Inc.
6
#
7
# This program is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation, either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
#
20
###############################################################################
21
22
23
pty = require('pty.js')
24
{setrlimit} = require('posix')
25
26
message = require('smc-util/message')
27
misc = require('smc-util/misc')
28
{defaults, required} = misc
29
30
process.on 'message', (opts, socket) ->
31
opts = defaults opts,
32
rows : required
33
cols : required
34
command : required
35
args : required
36
path : undefined
37
filename : undefined
38
39
# I noticed that LATELY sometimes we don't see output until hitting input from the client side.
40
# Setting the socket to immediately send when written to might help.
41
# See https://nodejs.org/api/net.html#net_socket_setnodelay_nodelay
42
socket.setNoDelay()
43
44
fn = misc.path_split(opts.filename).tail
45
env = misc.merge({SMC_FILENAME: fn}, process.env)
46
47
term_opts =
48
name : 'xterm'
49
rows : opts.rows
50
cols : opts.cols
51
env : env
52
53
if opts.path?
54
term_opts.cwd = opts.path
55
if opts.home?
56
term_opts.home = opts.home
57
58
#console.log("about to pty.fork with: opts.command=#{opts.command}, opts.args=#{misc.to_json(opts.args)}, term_opts=#{misc.to_json(term_opts)}")
59
term = pty.fork(opts.command, opts.args, term_opts)
60
61
# See http://invisible-island.net/xterm/ctlseqs/ctlseqs.txt
62
# CSI Ps ; Ps ; Ps t
63
# CSI[4];[height];[width]t
64
CSI = String.fromCharCode(0x9b)
65
resize_sequence = undefined
66
67
parse_resize = (data) ->
68
i = data.indexOf('t')
69
if i == -1
70
resize_sequence += data
71
return data.length
72
else
73
# Got complete sequence
74
s = (resize_sequence + data.slice(0,i)).slice(3)
75
resize_sequence = undefined
76
j = s.indexOf(';')
77
if j != -1
78
rows = parseInt(s.slice(0,j))
79
cols = parseInt(s.slice(j+1))
80
term.resize(cols, rows)
81
return i+1
82
83
CSI_code = (data) ->
84
s = data.toString('utf-8')
85
if resize_sequence?
86
start = 0
87
end = parse_resize(s)
88
else
89
i = s.lastIndexOf(CSI)
90
if i != -1
91
resize_sequence = ''
92
start = i
93
end = start + parse_resize(s.slice(i))
94
95
if start?
96
# skip data consumed in CSI
97
data = data.slice(0,start) + data.slice(end+1)
98
99
return data
100
101
socket.on 'data', (data) ->
102
data = CSI_code(data)
103
term.write(data)
104
105
term.on 'data', (data) ->
106
socket.write(data)
107
108
term.on 'exit', () ->
109
socket.end()
110
111
socket.on 'end', () ->
112
# If the hub connection dies, there is no point in
113
# letting this process continue running, since it can't send
114
# its output anywhere. So we terminate.
115
process.exit(1)
116