forked from facebookarchive/fb-adb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.c
163 lines (139 loc) · 3.83 KB
/
chat.c
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
155
156
157
158
159
160
161
162
163
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in
* the LICENSE file in the root directory of this source tree. An
* additional grant of patent rights can be found in the PATENTS file
* in the same directory.
*
*/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include "util.h"
#include "chat.h"
#include "fs.h"
__attribute__((noreturn))
static void chat_die(void)
{
die(ECOMM, "lost connection to child");
}
struct chat*
chat_new(int to, int from)
{
struct chat* cc = xcalloc(sizeof (*cc));
cc->to = xfdopen(to, "w");
cc->from = xfdopen(from, "r");
return cc;
}
char
chat_getc(struct chat* cc)
{
int c = getc(cc->from);
if (c == EOF)
chat_die();
return c;
}
void
chat_expect(struct chat* cc, char expected)
{
int c = chat_getc(cc);
if ((char)c != expected) {
die(ECOMM,
"[child] expected 0x%02x %c, found 0x%02x %c",
expected,
isprint(expected) ? expected : '.',
(char) c,
isprint(c) ? c : '.');
}
}
void
chat_expect_maybe(struct chat* cc, char expected)
{
char c = chat_getc(cc);
if (c != expected)
ungetc(c, cc->from);
}
void
chat_swallow_prompt(struct chat* cc)
{
/* 100% reliable prompt detection */
unsigned csi_arg = 0;
enum { S_NORMAL, S_AFTER_ESC, S_AFTER_CSI } state = S_NORMAL;
for (;;) {
char c = chat_getc(cc);
if (c == '#' || c == '$')
break;
// Some systems run resize(1), part of busybox, when an
// interactive shell connects. The state machine below looks
// for status queries from this program and replies with a
// fake answer. Ignoring the query leads to busybox waiting
// three seconds for an answer before giving up.
// For details of the control codes, see
// http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
switch (state) {
default:
case S_NORMAL:
if (c == '\033')
state = S_AFTER_ESC;
break;
case S_AFTER_ESC:
if (c == '[') {
state = S_AFTER_CSI;
csi_arg = 0;
} else {
state = S_NORMAL;
}
break;
case S_AFTER_CSI:
if ('0' <= c && c <= '9') {
csi_arg = 10 * csi_arg + c-'0';
} else if (c == 'n') {
if (csi_arg == 5) {
if (fputs("\033[0n", cc->to) == EOF)
chat_die();
} else if (csi_arg == 6) {
if (fputs("\033[25;80R", cc->to) == EOF)
chat_die();
}
if (fflush(cc->to) == EOF)
chat_die();
state = S_NORMAL;
} else {
state = S_NORMAL;
}
break;
}
}
chat_expect(cc, ' ');
}
void
chat_talk_at(struct chat* cc, const char* what)
{
if (fputs(what, cc->to) == EOF)
chat_die();
if (putc('\n', cc->to) == EOF)
chat_die();
if (fflush(cc->to) == EOF)
chat_die();
/* We expect the child to echo us, so read back the echoed
* characters. */
while (*what)
chat_expect(cc, *what++);
/* Yes, this is really what comes back after a \n. */
chat_expect(cc, '\r');
chat_expect_maybe(cc, '\r');
chat_expect(cc, '\n');
}
char*
chat_read_line(struct chat* cc)
{
size_t linesz;
char* line = slurp_line(cc->from, &linesz);
if (line == NULL)
die(ECOMM, "lost connection to child");
rtrim(line, &linesz, "\r\n");
return line;
}