diff --git a/console.c b/console.c index 4470bbd27..9a70badfd 100644 --- a/console.c +++ b/console.c @@ -189,7 +189,7 @@ static bool interpret_cmda(int argc, char *argv[]) return ok; } - +int web_connfd; /* Execute a command from a command line */ static bool interpret_cmd(char *cmdline) { @@ -203,6 +203,9 @@ static bool interpret_cmd(char *cmdline) free_string(argv[i]); free_array(argv, argc, sizeof(char *)); + if (web_connfd) { + close(web_connfd); + } return ok; } @@ -392,7 +395,7 @@ static bool do_time(int argc, char *argv[]) } static bool use_linenoise = true; -static int web_fd; +int web_fd = -1; static bool do_web(int argc, char *argv[]) { @@ -403,9 +406,11 @@ static bool do_web(int argc, char *argv[]) } web_fd = web_open(port); + int flag = fcntl(web_fd, F_GETFL, 0); + fcntl(web_fd, F_SETFL, flag | O_NONBLOCK); if (web_fd > 0) { printf("listen on port %d, fd is %d\n", port, web_fd); - use_linenoise = false; + // use_linenoise = false; } else { perror("ERROR"); exit(web_fd); @@ -553,13 +558,14 @@ static bool cmd_done() * nfds should be set to the maximum file descriptor for network sockets. * If nfds == 0, this indicates that there is no pending network activity */ -int web_connfd; + static int cmd_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout) { + printf("cmd_select\n"); int infd; fd_set local_readset; @@ -684,7 +690,6 @@ bool run_console(char *infile_name) report(1, "ERROR: Could not open source file '%s'", infile_name); return false; } - if (!has_infile) { char *cmdline; while (use_linenoise && (cmdline = linenoise(prompt))) { @@ -692,14 +697,11 @@ bool run_console(char *infile_name) line_history_add(cmdline); /* Add to the history. */ line_history_save(HISTORY_FILE); /* Save the history on disk. */ line_free(cmdline); - while (buf_stack && buf_stack->fd != STDIN_FILENO) + while (buf_stack && buf_stack->fd != STDIN_FILENO) { cmd_select(0, NULL, NULL, NULL, NULL); + } has_infile = false; } - if (!use_linenoise) { - while (!cmd_done()) - cmd_select(0, NULL, NULL, NULL, NULL); - } } else { while (!cmd_done()) cmd_select(0, NULL, NULL, NULL, NULL); diff --git a/linenoise.c b/linenoise.c index aa1f362e8..27384d24a 100644 --- a/linenoise.c +++ b/linenoise.c @@ -893,6 +893,14 @@ static void line_edit_next_word(struct line_state *l) * * The function returns the length of the current buffer. */ +#include +#include +#include +#include +#include "linenoise.h" +#include "web.h" +extern int web_fd; +extern int web_connfd; static int line_edit(int stdin_fd, int stdout_fd, char *buf, @@ -924,14 +932,64 @@ static int line_edit(int stdin_fd, * initially is just an empty string. */ line_history_add(""); - if (write(l.ofd, prompt, l.plen) == -1) return -1; + + fd_set set; + FD_ZERO(&set); + int nfd = l.ifd + 1; + if (web_fd != -1) { + FD_SET(web_fd, &set); // set socket + nfd = web_fd + 1; + } + FD_SET(l.ifd, &set); // set stdin + + while (1) { signed char c; int nread; char seq[5]; + int rv = select(nfd, &set, NULL, NULL, NULL); + struct sockaddr_in clientaddr; + socklen_t client_len = sizeof(clientaddr); + + switch (rv) { + case -1: + perror("select"); + break; + case 0: + printf("timeout\n"); + continue; + default: + if (web_fd != -1 && FD_ISSET(web_fd, &set)) { + FD_CLR(web_fd, &set); // auto clear? + int connfd; + connfd = accept(web_fd, (struct sockaddr *) &clientaddr, + &client_len); + char *p = web_recv(connfd, &clientaddr); + char *buffer = + "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\n"; + web_send(connfd, buffer); + + // web_send(web_connfd, "fuck"); + int len = 0; + if (p) { + strncpy(buf, p, strlen(p) + 1); + len = strlen(p); + } + web_connfd = connfd; + free(p); + return len; + } + // else if(FD_ISSET(l.ifd, &set)){ + // nread = read(l.ifd, &c, 1); + // if (nread <= 0) + // return l.len; + // } + break; + } + nread = read(l.ifd, &c, 1); if (nread <= 0) return l.len;