| 20 | #define MSG_CMD_TOO_LONG "Command too long." |
| 21 | |
| 22 | static int |
| 23 | data_event_handle(struct conn *conn, int fd_client) |
| 24 | { |
| 25 | ssize_t len, i, rc = 0; |
| 26 | |
| 27 | /* Read input message */ |
| 28 | len = read(fd_client, conn->buf, conn->buf_size); |
| 29 | if (len == -1) { |
| 30 | if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) |
| 31 | return 0; |
| 32 | |
| 33 | return -1; |
| 34 | } |
| 35 | |
| 36 | if (len == 0) |
| 37 | return rc; |
| 38 | |
| 39 | /* Handle input messages */ |
| 40 | for (i = 0; i < len; i++) { |
| 41 | if (conn->buf[i] == '\n') { |
| 42 | size_t n; |
| 43 | |
| 44 | conn->msg_in[conn->msg_in_len] = 0; |
| 45 | conn->msg_out[0] = 0; |
| 46 | |
| 47 | conn->msg_handle(conn->msg_in, conn->msg_out, conn->msg_out_len_max, |
| 48 | conn->msg_handle_arg); |
| 49 | |
| 50 | n = strlen(conn->msg_out); |
| 51 | if (n) { |
| 52 | rc = write(fd_client, conn->msg_out, n); |
| 53 | if (rc == -1) |
| 54 | goto exit; |
| 55 | } |
| 56 | |
| 57 | conn->msg_in_len = 0; |
| 58 | } else if (conn->msg_in_len < conn->msg_in_len_max) { |
| 59 | conn->msg_in[conn->msg_in_len] = conn->buf[i]; |
| 60 | conn->msg_in_len++; |
| 61 | } else { |
| 62 | rc = write(fd_client, MSG_CMD_TOO_LONG, strlen(MSG_CMD_TOO_LONG)); |
| 63 | if (rc == -1) |
| 64 | goto exit; |
| 65 | |
| 66 | conn->msg_in_len = 0; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /* Write prompt */ |
| 71 | rc = write(fd_client, conn->prompt, strlen(conn->prompt)); |
| 72 | rc = (rc == -1) ? -1 : 0; |
| 73 | |
| 74 | exit: |
| 75 | return rc; |
| 76 | } |
| 77 | |
| 78 | static int |
| 79 | control_event_handle(struct conn *conn, int fd_client) |
no test coverage detected