-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathlibtty.js
More file actions
109 lines (108 loc) · 3.09 KB
/
libtty.js
File metadata and controls
109 lines (108 loc) · 3.09 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
/**
* @license
* Copyright 2013 The Emscripten Authors
* SPDX-License-Identifier: MIT
*/
addToLibrary({
$TTY__deps: [
'$DEV',
'$FS',
'$UTF8ArrayToString',
'$FS_stdin_getChar',
],
$TTY: {
ttys: {},
register(dev, ops) {
const tty = { input: [], output: [], ops };
TTY.ttys[dev] = tty;
const devops = {
tty,
write(devops, buffer) {
if (!ops.put_char) {
throw new FS.ErrnoError({{{ cDefs.ENXIO }}});
}
for (var i = 0; i < buffer.length; i++) {
ops.put_char(tty, buffer[i]);
}
return i;
},
read(devops, buffer) {
if (!ops.get_char) {
throw new FS.ErrnoError({{{ cDefs.ENXIO }}});
}
var bytesRead = 0;
for (var i = 0; i < buffer.length; i++) {
var result = ops.get_char(tty);
if (result === undefined && bytesRead === 0) {
throw new FS.ErrnoError({{{ cDefs.EAGAIN }}});
}
if (result === null || result === undefined) break;
bytesRead++;
buffer[i] = result;
}
return bytesRead;
},
};
if (ops.fsync) {
devops.fsync = (devops) => ops.fsync(tty)
}
DEV.register(dev, devops);
},
default_tty_ops: {
get_char(tty) {
return FS_stdin_getChar();
},
put_char(tty, val) {
if (val === null || val === {{{ charCode('\n') }}}) {
out(UTF8ArrayToString(tty.output));
tty.output = [];
} else {
if (val != 0) tty.output.push(val); // val == 0 would cut text output off in the middle.
}
},
fsync(tty) {
if (tty.output?.length > 0) {
out(UTF8ArrayToString(tty.output));
tty.output = [];
}
},
ioctl_tcgets(tty) {
// typical setting
return {
c_iflag: {{{ cDefs.ICRNL | cDefs.IXON | cDefs.IMAXBEL | cDefs.IUTF8 }}},
c_oflag: {{{ cDefs.OPOST | cDefs.ONLCR }}},
c_cflag: {{{ cDefs.B38400 | cDefs.CSIZE | cDefs.CREAD }}},
c_lflag: {{{ cDefs.ISIG | cDefs.ICANON | cDefs.ECHO | cDefs.ECHOE | cDefs.ECHOK | cDefs.ECHOCTL | cDefs.ECHOKE | cDefs.IEXTEN }}},
c_cc: [
0x03, 0x1c, 0x7f, 0x15, 0x04, 0x00, 0x01, 0x00, 0x11, 0x13, 0x1a, 0x00,
0x12, 0x0f, 0x17, 0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
]
};
},
ioctl_tcsets(tty, optional_actions, data) {
// currently just ignore
return 0;
},
ioctl_tiocgwinsz(tty) {
return [24, 80];
}
},
default_tty1_ops: {
put_char(tty, val) {
if (val === null || val === {{{ charCode('\n') }}}) {
err(UTF8ArrayToString(tty.output));
tty.output = [];
} else {
if (val != 0) tty.output.push(val);
}
},
fsync(tty) {
if (tty.output?.length > 0) {
err(UTF8ArrayToString(tty.output));
tty.output = [];
}
}
}
}
});