1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "nacl_io/mount_node_tty.h"
12 #include <sys/ioctl.h>
17 #include "nacl_io/ioctl.h"
18 #include "nacl_io/kernel_handle.h"
19 #include "nacl_io/mount.h"
20 #include "nacl_io/pepper_interface.h"
21 #include "sdk_util/auto_lock.h"
23 #define CHECK_LFLAG(TERMIOS, FLAG) (TERMIOS .c_lflag & FLAG)
25 #define IS_ECHO CHECK_LFLAG(termios_, ECHO)
26 #define IS_ECHOE CHECK_LFLAG(termios_, ECHOE)
27 #define IS_ECHONL CHECK_LFLAG(termios_, ECHONL)
28 #define IS_ECHOCTL CHECK_LFLAG(termios_, ECHOCTL)
29 #define IS_ICANON CHECK_LFLAG(termios_, ICANON)
31 #define DEFAULT_TTY_COLS 80
32 #define DEFAULT_TTY_ROWS 30
36 MountNodeTty::MountNodeTty(Mount* mount)
37 : MountNodeCharDevice(mount),
38 emitter_(new EventEmitter),
39 rows_(DEFAULT_TTY_ROWS),
40 cols_(DEFAULT_TTY_COLS) {
41 output_handler_.handler = NULL;
44 // Output will never block
45 emitter_->RaiseEvents_Locked(POLLOUT);
48 void MountNodeTty::InitTermios() {
49 // Some sane values that produce good result.
50 termios_.c_iflag = ICRNL | IXON | IXOFF | IUTF8;
51 termios_.c_oflag = OPOST | ONLCR;
52 termios_.c_cflag = CREAD | 077;
54 ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN;
55 termios_.c_ispeed = B38400;
56 termios_.c_ospeed = B38400;
57 termios_.c_cc[VINTR] = 3;
58 termios_.c_cc[VQUIT] = 28;
59 termios_.c_cc[VERASE] = 127;
60 termios_.c_cc[VKILL] = 21;
61 termios_.c_cc[VEOF] = 4;
62 termios_.c_cc[VTIME] = 0;
63 termios_.c_cc[VMIN] = 1;
64 termios_.c_cc[VSWTC] = 0;
65 termios_.c_cc[VSTART] = 17;
66 termios_.c_cc[VSTOP] = 19;
67 termios_.c_cc[VSUSP] = 26;
68 termios_.c_cc[VEOL] = 0;
69 termios_.c_cc[VREPRINT] = 18;
70 termios_.c_cc[VDISCARD] = 15;
71 termios_.c_cc[VWERASE] = 23;
72 termios_.c_cc[VLNEXT] = 22;
73 termios_.c_cc[VEOL2] = 0;
76 EventEmitter* MountNodeTty::GetEventEmitter() {
77 return emitter_.get();
80 Error MountNodeTty::Write(const HandleAttr& attr,
84 AUTO_LOCK(output_lock_);
87 // No handler registered.
88 if (output_handler_.handler == NULL)
91 int rtn = output_handler_.handler(static_cast<const char*>(buf),
93 output_handler_.user_data);
95 // Negative return value means an error occured and the return
96 // value is a negated errno value.
105 Error MountNodeTty::Read(const HandleAttr& attr,
109 EventListenerLock wait(GetEventEmitter());
112 // If interrupted, return
113 Error err = wait.WaitOnEvent(POLLIN, -1);
117 size_t bytes_to_copy = std::min(count, input_buffer_.size());
119 // Only read up to (and including) the first newline
120 std::deque<char>::iterator nl = std::find(input_buffer_.begin(),
124 if (nl != input_buffer_.end()) {
125 // We found a newline in the buffer, adjust bytes_to_copy accordingly
126 size_t line_len = static_cast<size_t>(nl - input_buffer_.begin()) + 1;
127 bytes_to_copy = std::min(bytes_to_copy, line_len);
132 // Copies data from the input buffer into buf.
133 std::copy(input_buffer_.begin(), input_buffer_.begin() + bytes_to_copy,
134 static_cast<char*>(buf));
135 *out_bytes = bytes_to_copy;
136 input_buffer_.erase(input_buffer_.begin(),
137 input_buffer_.begin() + bytes_to_copy);
139 // mark input as no longer readable if we consumed
140 // the entire buffer or, in the case of buffered input,
141 // we consumed the final \n char.
144 avail = std::find(input_buffer_.begin(),
145 input_buffer_.end(), '\n') != input_buffer_.end();
147 avail = input_buffer_.size() > 0;
150 emitter_->ClearEvents_Locked(POLLIN);
155 Error MountNodeTty::Echo(const char* string, int count) {
158 Error error = Write(data, string, count, &wrote);
159 if (error != 0 || wrote != count) {
160 // TOOD(sbc): Do something more useful in response to a
168 Error MountNodeTty::ProcessInput(struct tioc_nacl_input_string* message) {
169 AUTO_LOCK(emitter_->GetLock())
171 const char* buffer = message->buffer;
172 size_t num_bytes = message->length;
174 for (size_t i = 0; i < num_bytes; i++) {
176 // Transform characters according to input flags.
178 if (termios_.c_iflag & IGNCR)
180 if (termios_.c_iflag & ICRNL)
182 } else if (c == '\n') {
183 if (termios_.c_iflag & INLCR)
189 // ICANON mode means we wait for a newline before making the
192 if (IS_ECHOE && c == termios_.c_cc[VERASE]) {
193 // Remove previous character in the line if any.
194 if (!input_buffer_.empty()) {
195 char char_to_delete = input_buffer_.back();
196 if (char_to_delete != '\n') {
197 input_buffer_.pop_back();
201 // When ECHOCTL is set the echo buffer contains an extra
202 // char for each control char.
203 if (IS_ECHOCTL && iscntrl(char_to_delete))
208 } else if (IS_ECHO || (IS_ECHONL && c == '\n')) {
209 if (c == termios_.c_cc[VEOF]) {
210 // VEOF sequence is not echoed, nor is it sent as
213 } else if (c != '\n' && iscntrl(c) && IS_ECHOCTL) {
214 // In ECHOCTL mode a control char C is echoed as '^'
215 // followed by the ascii char which at C + 0x40.
216 char visible_char = c + 0x40;
218 Echo(&visible_char, 1);
226 input_buffer_.push_back(c);
228 if (c == '\n' || c == termios_.c_cc[VEOF] || !IS_ICANON)
229 emitter_->RaiseEvents_Locked(POLLIN);
235 Error MountNodeTty::VIoctl(int request, va_list args) {
237 case TIOCNACLOUTPUT: {
238 struct tioc_nacl_output* arg = va_arg(args, struct tioc_nacl_output*);
239 AUTO_LOCK(output_lock_);
241 output_handler_.handler = NULL;
244 if (output_handler_.handler != NULL)
246 output_handler_ = *arg;
249 case TIOCNACLINPUT: {
250 // This ioctl is used to deliver data from the user to this tty node's
252 struct tioc_nacl_input_string* message =
253 va_arg(args, struct tioc_nacl_input_string*);
254 return ProcessInput(message);
257 struct winsize* size = va_arg(args, struct winsize*);
259 AUTO_LOCK(node_lock_);
260 rows_ = size->ws_row;
261 cols_ = size->ws_col;
263 kill(getpid(), SIGWINCH);
265 // Wake up any thread waiting on Read with POLLERR then immediate
266 // clear it to signal EINTR.
267 AUTO_LOCK(emitter_->GetLock())
268 emitter_->RaiseEvents_Locked(POLLERR);
269 emitter_->ClearEvents_Locked(POLLERR);
274 struct winsize* size = va_arg(args, struct winsize*);
275 size->ws_row = rows_;
276 size->ws_col = cols_;
284 Error MountNodeTty::Tcgetattr(struct termios* termios_p) {
285 AUTO_LOCK(node_lock_);
286 *termios_p = termios_;
290 Error MountNodeTty::Tcsetattr(int optional_actions,
291 const struct termios *termios_p) {
292 AUTO_LOCK(node_lock_);
293 termios_ = *termios_p;