static void ui_handler(evutil_socket_t fd, short what, void *arg) { struct uiclient *client = arg; if (evbuffer_read(client->inbuf, fd, -1) <= 0) { ui_dead(client); return; } for (;;) { size_t eol_len; struct evbuffer_ptr line = evbuffer_search_eol(client->inbuf, NULL, &eol_len, EVBUFFER_EOL_LF); if (line.pos == -1) break; char *p = (char *)malloc(line.pos + 1); if (p != NULL) { evbuffer_remove(client->inbuf, (void *)p, line.pos); p[line.pos] = '\0'; /* ensure termination */ evbuffer_drain(client->inbuf, eol_len); ui_handle_command(client->outbuf, p); free(p); } } ui_write_prompt(client); event_add(client->ev_read, NULL); }
void ui_handle_keydown(void) { if (keyp == KEY_ERROR) return; switch (keys[keyp]) { case KEY_VOLUMEUP: ui_menu_item_up(); break; case KEY_VOLUMEDOWN: ui_menu_item_down(); break; case KEY_SEND: // dial ui_handle_command(); break; case KEY_CLEAR: // hang up case KEY_BACK: // go back break; } }
/* * A half-complex implementation of reading from a file descriptor * line by line without resorting to stdio which apparently have * troubles with non-blocking fifos. */ void ui_handler(void) { static char *buf = 0; static char *p; static size_t sz; static size_t resid; ssize_t n; char *new_buf; /* If no buffer, set it up. */ if (!buf) { sz = BUF_SZ; buf = malloc(sz); if (!buf) { log_print("ui_handler: malloc (%lu) failed", (unsigned long)sz); return; } p = buf; resid = sz; } /* If no place left in the buffer reallocate twice as large. */ if (!resid) { new_buf = realloc(buf, sz * 2); if (!new_buf) { log_print("ui_handler: realloc (%p, %lu) failed", buf, (unsigned long)sz * 2); free(buf); buf = 0; return; } buf = new_buf; p = buf + sz; resid = sz; sz *= 2; } n = read(ui_socket, p, resid); if (n == -1) { log_error("ui_handler: read (%d, %p, %lu)", ui_socket, p, (unsigned long)resid); return; } if (!n) return; resid -= n; while (n--) { /* * When we find a newline, cut off the line and feed it to the * command processor. Then move the rest up-front. */ if (*p == '\n') { *p = '\0'; ui_handle_command(buf); memcpy(buf, p + 1, n); p = buf; resid = sz - n; continue; } p++; } }