diff --git a/console.c b/console.c index 4470bbd27..e10e33d7c 100644 --- a/console.c +++ b/console.c @@ -560,13 +560,13 @@ static int cmd_select(int nfds, fd_set *exceptfds, struct timeval *timeout) { - int infd; fd_set local_readset; if (cmd_done()) return 0; if (!block_flag) { + int infd; /* Process any commands in input buffer */ if (!readfds) readfds = &local_readset; @@ -581,51 +581,18 @@ static int cmd_select(int nfds, FD_SET(web_fd, readfds); if (infd == STDIN_FILENO && prompt_flag) { - printf("%s", prompt); + char *cmdline = linenoise(prompt); + if (cmdline) + interpret_cmd(cmdline); fflush(stdout); prompt_flag = true; + } else if (infd != STDIN_FILENO) { + char *cmdline = readline(); + if (cmdline) + interpret_cmd(cmdline); } - - if (infd >= nfds) - nfds = infd + 1; - if (web_fd >= nfds) - nfds = web_fd + 1; - } - if (nfds == 0) - return 0; - - int result = select(nfds, readfds, writefds, exceptfds, timeout); - if (result <= 0) - return result; - - infd = buf_stack->fd; - if (readfds && FD_ISSET(infd, readfds)) { - /* Commandline input available */ - FD_CLR(infd, readfds); - result--; - - set_echo(0); - char *cmdline = readline(); - if (cmdline) - interpret_cmd(cmdline); - } else if (readfds && FD_ISSET(web_fd, readfds)) { - FD_CLR(web_fd, readfds); - result--; - struct sockaddr_in clientaddr; - socklen_t clientlen = sizeof(clientaddr); - web_connfd = - accept(web_fd, (struct sockaddr *) &clientaddr, &clientlen); - - char *p = web_recv(web_connfd, &clientaddr); - char *buffer = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\n"; - web_send(web_connfd, buffer); - - if (p) - interpret_cmd(p); - free(p); - close(web_connfd); } - return result; + return 0; } bool finish_cmd() diff --git a/linenoise.c b/linenoise.c index aa1f362e8..e2aa69b32 100644 --- a/linenoise.c +++ b/linenoise.c @@ -116,6 +116,7 @@ #include #include "linenoise.h" +#include "web.h" #define LINENOISE_DEFAULT_HISTORY_MAX_LEN 100 #define LINENOISE_MAX_LINE 4096 @@ -927,6 +928,11 @@ static int line_edit(int stdin_fd, if (write(l.ofd, prompt, l.plen) == -1) return -1; + + int result = web_select(l.buf, l.ifd); + if (result != 0) + return result; + while (1) { signed char c; int nread; @@ -1362,4 +1368,4 @@ int line_history_load(const char *filename) } fclose(fp); return 0; -} +} \ No newline at end of file diff --git a/web.c b/web.c index 0994a47f5..4382db64d 100644 --- a/web.c +++ b/web.c @@ -23,6 +23,8 @@ #define TCP_CORK TCP_NOPUSH #endif +int server_fd; + typedef struct { int fd; /* descriptor for this buf */ int count; /* unread byte in this buf */ @@ -153,6 +155,9 @@ int web_open(int port) /* Make it a listening socket ready to accept connection requests */ if (listen(listenfd, LISTENQ) < 0) return -1; + + server_fd = listenfd; + return listenfd; } @@ -228,3 +233,35 @@ char *web_recv(int fd, struct sockaddr_in *clientaddr) return ret; } + +int web_select(char *buf, int stdin_fd) +{ + fd_set listenset; + + FD_ZERO(&listenset); + FD_SET(stdin_fd, &listenset); + int max_fd = stdin_fd; + if (server_fd > 0) { + FD_SET(server_fd, &listenset); + max_fd = max_fd > server_fd ? max_fd : server_fd; + } + int result = select(max_fd + 1, &listenset, NULL, NULL, NULL); + if (result < 0) + return -1; + + if (server_fd > 0 && FD_ISSET(server_fd, &listenset)) { + FD_CLR(server_fd, &listenset); + struct sockaddr_in clientaddr; + socklen_t clientlen = sizeof(clientaddr); + int web_connfd = + accept(server_fd, (struct sockaddr *) &clientaddr, &clientlen); + + char *p = web_recv(web_connfd, &clientaddr); + char *buffer = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\n"; + web_send(web_connfd, buffer); + strncpy(buf, p, strlen(p) + 1); + free(p); + close(web_connfd); + } + return strlen(buf); +} \ No newline at end of file diff --git a/web.h b/web.h index b04bc179d..c38fe14c1 100644 --- a/web.h +++ b/web.h @@ -9,4 +9,6 @@ char *web_recv(int fd, struct sockaddr_in *clientaddr); void web_send(int out_fd, char *buffer); +int web_select(char *buf, int stdin_fd); + #endif