This repository has been archived by the owner on Apr 16, 2018. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
console_server_child.coffee
104 lines (89 loc) · 3.23 KB
/
console_server_child.coffee
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
###############################################################################
#
# SageMathCloud: A collaborative web-based interface to Sage, IPython, LaTeX and the Terminal.
#
# Copyright (C) 2014, William Stein
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################
pty = require 'pty.js'
message = require 'message'
{defaults, required} = require 'misc'
{setrlimit} = require 'posix'
process.on 'message', (opts, socket) ->
opts = defaults opts,
rows : required
cols : required
command : required
args : required
path : undefined
term_opts =
name : 'xterm'
rows : opts.rows
cols : opts.cols
if opts.path?
term_opts.cwd = opts.path
if opts.home?
term_opts.home = opts.home
misc = require('misc')
#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)}")
term = pty.fork(opts.command, opts.args, term_opts)
# See http://invisible-island.net/xterm/ctlseqs/ctlseqs.txt
# CSI Ps ; Ps ; Ps t
# CSI[4];[height];[width]t
CSI = String.fromCharCode(0x9b)
resize_sequence = undefined
parse_resize = (data) ->
i = data.indexOf('t')
if i == -1
resize_sequence += data
return data.length
else
# Got complete sequence
s = (resize_sequence + data.slice(0,i)).slice(3)
resize_sequence = undefined
j = s.indexOf(';')
if j != -1
rows = parseInt(s.slice(0,j))
cols = parseInt(s.slice(j+1))
term.resize(cols, rows)
return i+1
CSI_code = (data) ->
s = data.toString('ascii')
if resize_sequence?
start = 0
end = parse_resize(s)
else
i = s.lastIndexOf(CSI)
if i != -1
resize_sequence = ''
start = i
end = start + parse_resize(s.slice(i))
if start?
# skip data consumed in CSI
data = data.slice(0,start) + data.slice(end+1)
return data
socket.on 'data', (data) ->
data = CSI_code(data)
term.write data
term.on 'data', (data) ->
socket.write data
term.on 'exit', () ->
socket.end()
socket.on 'end', () ->
# If the hub connection dies, there is no point in
# letting this process continue running, since it can't send
# its output anywhere. So we terminate.
process.exit(1)