| 208 | } |
| 209 | |
| 210 | static int |
| 211 | data_event_handle(struct conn *conn, |
| 212 | int fd_client) |
| 213 | { |
| 214 | ssize_t len, i, status; |
| 215 | |
| 216 | /* Read input message */ |
| 217 | |
| 218 | len = read(fd_client, |
| 219 | conn->buf, |
| 220 | conn->buf_size); |
| 221 | if (len == -1) { |
| 222 | if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) |
| 223 | return 0; |
| 224 | |
| 225 | return -1; |
| 226 | } |
| 227 | if (len == 0) |
| 228 | return 0; |
| 229 | |
| 230 | /* Handle input messages */ |
| 231 | for (i = 0; i < len; i++) { |
| 232 | if (conn->buf[i] == '\n') { |
| 233 | size_t n; |
| 234 | |
| 235 | conn->msg_in[conn->msg_in_len] = 0; |
| 236 | conn->msg_out[0] = 0; |
| 237 | |
| 238 | conn->msg_handle(conn->msg_in, |
| 239 | conn->msg_out, |
| 240 | conn->msg_out_len_max, |
| 241 | conn->msg_handle_arg); |
| 242 | |
| 243 | n = strlen(conn->msg_out); |
| 244 | if (n) { |
| 245 | status = write(fd_client, |
| 246 | conn->msg_out, |
| 247 | n); |
| 248 | if (status == -1) |
| 249 | return status; |
| 250 | } |
| 251 | |
| 252 | conn->msg_in_len = 0; |
| 253 | } else if (conn->msg_in_len < conn->msg_in_len_max) { |
| 254 | conn->msg_in[conn->msg_in_len] = conn->buf[i]; |
| 255 | conn->msg_in_len++; |
| 256 | } else { |
| 257 | status = write(fd_client, |
| 258 | MSG_CMD_TOO_LONG, |
| 259 | strlen(MSG_CMD_TOO_LONG)); |
| 260 | if (status == -1) |
| 261 | return status; |
| 262 | |
| 263 | conn->msg_in_len = 0; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | /* Write prompt */ |
no test coverage detected