This commit is contained in:
Kovid Goyal 2018-06-18 12:18:04 +05:30
parent 94273941ad
commit d7007f77aa
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -34,7 +34,9 @@ add_char(char buf[CMD_BUF_SZ], size_t *pos, char ch, PyObject *ans) {
} }
static inline bool static inline bool
read_response(int fd, double timeout, char buf[CMD_BUF_SZ], size_t *pos, PyObject *ans) { read_response(int fd, double timeout, PyObject *ans) {
static char buf[CMD_BUF_SZ];
size_t pos = 0;
enum ReadState {START, STARTING_ESC, P, AT, K, I, T, T2, Y, HYPHEN, C, M, BODY, TRAILING_ESC}; enum ReadState {START, STARTING_ESC, P, AT, K, I, T, T2, Y, HYPHEN, C, M, BODY, TRAILING_ESC};
enum ReadState state = START; enum ReadState state = START;
char ch; char ch;
@ -68,13 +70,13 @@ read_response(int fd, double timeout, char buf[CMD_BUF_SZ], size_t *pos, PyObjec
case BODY: case BODY:
if (ch == 0x1b) { state = TRAILING_ESC; } if (ch == 0x1b) { state = TRAILING_ESC; }
else { else {
if (!add_char(buf, pos, ch, ans)) return false; if (!add_char(buf, &pos, ch, ans)) return false;
} }
break; break;
case TRAILING_ESC: case TRAILING_ESC:
if (ch == '\\') return append_buf(buf, pos, ans); if (ch == '\\') return append_buf(buf, &pos, ans);
if (!add_char(buf, pos, 0x1b, ans)) return false; if (!add_char(buf, &pos, 0x1b, ans)) return false;
if (!add_char(buf, pos, ch, ans)) return false; if (!add_char(buf, &pos, ch, ans)) return false;
state = BODY; state = BODY;
break; break;
} }
@ -89,9 +91,7 @@ read_command_response(PyObject *self UNUSED, PyObject *args) {
int fd; int fd;
PyObject *ans; PyObject *ans;
if (!PyArg_ParseTuple(args, "idO!", &fd, &timeout, &PyList_Type, &ans)) return NULL; if (!PyArg_ParseTuple(args, "idO!", &fd, &timeout, &PyList_Type, &ans)) return NULL;
static char buf[CMD_BUF_SZ]; if (!read_response(fd, timeout, ans)) return NULL;
size_t pos = 0;
if (!read_response(fd, timeout, buf, &pos, ans)) return NULL;
Py_RETURN_NONE; Py_RETURN_NONE;
} }