-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterminal_reader.js
154 lines (143 loc) · 5.85 KB
/
terminal_reader.js
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
const GLib = imports.gi.GLib;
const Gio = imports.gi.Gio;
const Lang = imports.lang;
function TerminalReader(path, command, callback) {
this._init(path, command, callback);
}
TerminalReader.prototype = {
_init: function(path, command, callback) {
this._path = path;
this._callbackPipe = callback;
this._commandPipe = command;
this.idle = true;
this._childWatch = null;
},
executeReader: function() {
if(this.idle) {
this.idle = false;
try {
let [success, argv] = GLib.shell_parse_argv("sh -c 'cd " + this._path + ";" + this._commandPipe + "'");
if(success) {
let [exit, pid, stdin, stdout, stderr] =
GLib.spawn_async_with_pipes(null,
argv,
null,
GLib.SpawnFlags.SEARCH_PATH | GLib.SpawnFlags.DO_NOT_REAP_CHILD,
null );
this._childPid = pid;
this._stdin = new Gio.UnixOutputStream({ fd: stdin, close_fd: true });
this._stdout = new Gio.UnixInputStream({ fd: stdout, close_fd: true });
this._stderr = new Gio.UnixInputStream({ fd: stderr, close_fd: true });
// We need this one too, even if don't actually care of what the process
// has to say on stderr, because otherwise the fd opened by g_spawn_async_with_pipes
// is kept open indefinitely
this._stderrStream = new Gio.DataInputStream({ base_stream: this._stderr });
this._dataStdout = new Gio.DataInputStream({ base_stream: this._stdout });
this._cancellableStderrStream = new Gio.Cancellable();
this._cancellableStdout = new Gio.Cancellable();
this.resOut = 1;
this._readStdout();
this.resErr = 1;
this._readStderror();
this._childWatch = GLib.child_watch_add(GLib.PRIORITY_DEFAULT, pid, Lang.bind(this, function(pid, status, requestObj) {
GLib.source_remove(this._childWatch);
this._childWatch = null;
this._stdin.close(null);
this.idle = true;
}));
}
//throw
} catch(err) {
if (err.code == GLib.SpawnError.G_SPAWN_ERROR_NOENT) {
err.message = _("Command not found.");
} else {
// The exception from gjs contains an error string like:
// Error invoking GLib.spawn_command_line_async: Failed to
// execute child process "foo" (No such file or directory)
// We are only interested in the part in the parentheses. (And
// we can't pattern match the text, since it gets localized.)
err.message = err.message.replace(/.*\((.+)\)/, '$1');
}
throw err;
}
}
},
destroy: function() {
try {
if(this._childWatch) {
GLib.source_remove(this._childWatch);
this._childWatch = null;
}
if(!this._dataStdout.is_closed()) {
this._cancellableStdout.cancel();
this._stdout.close_async(0, null, Lang.bind(this, this.closeStdout));
}
if(!this._stderrStream.is_closed()) {
this._cancellableStderrStream.cancel();
this._stderrStream.close_async(0, null, Lang.bind(this, this.closeStderrStream));
}
this._stdin.close(null);
this.idle = true;
}
catch(e) {
Main.notify("Error on close" + this._dataStdout.is_closed(), e.message);
}
},
closeStderrStream: function(std, result) {
try {
std.close_finish(result);
} catch(e) {
std.close_async(0, null, Lang.bind(this, this.closeStderrStream));
}
},
closeStdout: function(std, result) {
try {
std.close_finish(result);
} catch(e) {
std.close_async(0, null, Lang.bind(this, this.closeStderrStream));
}
},
_readStdout: function() {
this._dataStdout.fill_async(-1, GLib.PRIORITY_DEFAULT, this._cancellableStdout, Lang.bind(this, function(stream, result) {
try {
if(!this._dataStdout.is_closed()) {
if(this.resOut != -1)
this.resOut = this._dataStdout.fill_finish(result);// end of file
if(this.resOut == 0) {
let val = stream.peek_buffer().toString();
if(val != "")
this._callbackPipe(this._commandPipe, true, val);
this._stdout.close(this._cancellableStdout);
} else {
// Try to read more
this._dataStdout.set_buffer_size(2 * this._dataStdout.get_buffer_size());
this._readStdout();
}
}
} catch(e) {
// global.log(e);
}
}));
},
_readStderror: function() {
this._stderrStream.fill_async(-1, GLib.PRIORITY_DEFAULT, this._cancellableStderrStream, Lang.bind(this, function(stream, result) {
try {
if(!this._stderrStream.is_closed()) {
if(this.resErr != -1)
this.resErr = this._stderrStream.fill_finish(result);
if(this.resErr == 0) { // end of file
let val = stream.peek_buffer().toString();
if(val != "")
this._callbackPipe(this._commandPipe, false, val);
this._stderr.close(null);
} else {
this._stderrStream.set_buffer_size(2 * this._stderrStream.get_buffer_size());
this._readStderror();
}
}
} catch(e) {
global.log(e);
}
}));
}
};